Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / b2g / components / KillSwitch.js
blob6f4649ca1ed344547fc8db65110715c073b6c0f6
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/. */
5 "use strict";
7 const DEBUG = false;
9 function debug(s) {
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",
23                                    "nsIMessageSender");
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() {
37   this._window       = null;
40 KillSwitch.prototype = {
42   __proto__: DOMRequestIpcHelper.prototype,
44   init: function(aWindow) {
45     DEBUG && debug("init");
46     this._window = aWindow;
47     this.initDOMRequestHelper(this._window);
48   },
50   enable: function() {
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({
58           resolve: aResolve,
59           reject: aReject
60         })
61       });
62     });
63   },
65   disable: function() {
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({
73           resolve: aResolve,
74           reject: aReject
75         })
76       });
77     });
78   },
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:
93         req.reject(false);
94         break;
96       case kEnableKillSwitchOK:
97       case kDisableKillSwitchOK:
98         req.resolve(true);
99         break;
101       default:
102         DEBUG && debug("Unrecognized message: " + message.name);
103         break;
104     }
105   },
107   classID : Components.ID(KILLSWITCH_CID),
108   contractID : KILLSWITCH_CONTRACTID,
109   QueryInterface: XPCOMUtils.generateQI([Ci.nsIKillSwitch,
110                                          Ci.nsIDOMGlobalPropertyInitializer,
111                                          Ci.nsIObserver,
112                                          Ci.nsIMessageListener,
113                                          Ci.nsISupportsWeakReference]),
116 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([KillSwitch]);