1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
10 dump("-*- KillSwitch.js: " + s + "\n");
13 const {interfaces: Ci, utils: Cu} = Components;
15 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
16 Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
18 XPCOMUtils.defineLazyModuleGetter(this, "Services",
19 "resource://gre/modules/Services.jsm");
21 XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
22 "@mozilla.org/childprocessmessagemanager;1",
25 const KILLSWITCH_CID = "{b6eae5c6-971c-4772-89e5-5df626bf3f09}";
26 const KILLSWITCH_CONTRACTID = "@mozilla.org/moz-kill-switch;1";
28 const kEnableKillSwitch = "KillSwitch:Enable";
29 const kEnableKillSwitchOK = "KillSwitch:Enable:OK";
30 const kEnableKillSwitchKO = "KillSwitch:Enable:KO";
32 const kDisableKillSwitch = "KillSwitch:Disable";
33 const kDisableKillSwitchOK = "KillSwitch:Disable:OK";
34 const kDisableKillSwitchKO = "KillSwitch:Disable:KO";
36 function KillSwitch() {
40 KillSwitch.prototype = {
42 __proto__: DOMRequestIpcHelper.prototype,
44 init: function(aWindow) {
45 DEBUG && debug("init");
46 this._window = aWindow;
47 this.initDOMRequestHelper(this._window);
51 DEBUG && debug("KillSwitch: enable");
53 cpmm.addMessageListener(kEnableKillSwitchOK, this);
54 cpmm.addMessageListener(kEnableKillSwitchKO, this);
55 return this.createPromise((aResolve, aReject) => {
56 cpmm.sendAsyncMessage(kEnableKillSwitch, {
57 requestID: this.getPromiseResolverId({
66 DEBUG && debug("KillSwitch: disable");
68 cpmm.addMessageListener(kDisableKillSwitchOK, this);
69 cpmm.addMessageListener(kDisableKillSwitchKO, this);
70 return this.createPromise((aResolve, aReject) => {
71 cpmm.sendAsyncMessage(kDisableKillSwitch, {
72 requestID: this.getPromiseResolverId({
80 receiveMessage: function(message) {
81 DEBUG && debug("Received: " + message.name);
83 cpmm.removeMessageListener(kEnableKillSwitchOK, this);
84 cpmm.removeMessageListener(kEnableKillSwitchKO, this);
85 cpmm.removeMessageListener(kDisableKillSwitchOK, this);
86 cpmm.removeMessageListener(kDisableKillSwitchKO, this);
88 let req = this.takePromiseResolver(message.data.requestID);
90 switch (message.name) {
91 case kEnableKillSwitchKO:
92 case kDisableKillSwitchKO:
96 case kEnableKillSwitchOK:
97 case kDisableKillSwitchOK:
102 DEBUG && debug("Unrecognized message: " + message.name);
107 classID : Components.ID(KILLSWITCH_CID),
108 contractID : KILLSWITCH_CONTRACTID,
109 QueryInterface: XPCOMUtils.generateQI([Ci.nsIKillSwitch,
110 Ci.nsIDOMGlobalPropertyInitializer,
112 Ci.nsIMessageListener,
113 Ci.nsISupportsWeakReference]),
116 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([KillSwitch]);