Bumping gaia.json for 1 gaia revision(s) a=gaia-bump
[gecko.git] / dom / push / Push.js
blob81a253a25146de0cc2fea263c73abb3cdfc3faa2
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 // Don't modify this, instead set services.push.debug.
8 let gDebuggingEnabled = false;
10 function debug(s) {
11   if (gDebuggingEnabled)
12     dump("-*- Push.js: " + s + "\n");
15 const Cc = Components.classes;
16 const Ci = Components.interfaces;
17 const Cu = Components.utils;
19 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
20 Cu.import("resource://gre/modules/Services.jsm");
21 Cu.import("resource://gre/modules/DOMRequestHelper.jsm");
22 Cu.import("resource://gre/modules/AppsUtils.jsm");
24 const PUSH_CID = Components.ID("{cde1d019-fad8-4044-b141-65fb4fb7a245}");
26 /**
27  * The Push component runs in the child process and exposes the SimplePush API
28  * to the web application. The PushService running in the parent process is the
29  * one actually performing all operations.
30  */
31 function Push() {
32   debug("Push Constructor");
35 Push.prototype = {
36   __proto__: DOMRequestIpcHelper.prototype,
38   contractID: "@mozilla.org/push/PushManager;1",
40   classID : PUSH_CID,
42   QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
43                                           Ci.nsISupportsWeakReference,
44                                           Ci.nsIObserver]),
46   init: function(aWindow) {
47     // Set debug first so that all debugging actually works.
48     // NOTE: We don't add an observer here like in PushService. Flipping the
49     // pref will require a reload of the app/page, which seems acceptable.
50     gDebuggingEnabled = Services.prefs.getBoolPref("services.push.debug");
51     debug("init()");
53     let principal = aWindow.document.nodePrincipal;
54     let appsService = Cc["@mozilla.org/AppsService;1"]
55                         .getService(Ci.nsIAppsService);
57     this._manifestURL = appsService.getManifestURLByLocalId(principal.appId);
58     this._pageURL = principal.URI;
60     this.initDOMRequestHelper(aWindow, [
61       "PushService:Register:OK",
62       "PushService:Register:KO",
63       "PushService:Unregister:OK",
64       "PushService:Unregister:KO",
65       "PushService:Registrations:OK",
66       "PushService:Registrations:KO"
67     ]);
69     this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
70                    .getService(Ci.nsISyncMessageSender);
71   },
73   receiveMessage: function(aMessage) {
74     debug("receiveMessage()");
75     let request = this.getRequest(aMessage.data.requestID);
76     let json = aMessage.data;
77     if (!request) {
78       debug("No request " + json.requestID);
79       return;
80     }
82     switch (aMessage.name) {
83       case "PushService:Register:OK":
84         Services.DOMRequest.fireSuccess(request, json.pushEndpoint);
85         break;
86       case "PushService:Register:KO":
87         Services.DOMRequest.fireError(request, json.error);
88         break;
89       case "PushService:Unregister:OK":
90         Services.DOMRequest.fireSuccess(request, json.pushEndpoint);
91         break;
92       case "PushService:Unregister:KO":
93         Services.DOMRequest.fireError(request, json.error);
94         break;
95       case "PushService:Registrations:OK":
96         Services.DOMRequest.fireSuccess(request, json.registrations);
97         break;
98       case "PushService:Registrations:KO":
99         Services.DOMRequest.fireError(request, json.error);
100         break;
101       default:
102         debug("NOT IMPLEMENTED! receiveMessage for " + aMessage.name);
103     }
104   },
106   register: function() {
107     debug("register()");
108     let req = this.createRequest();
109     if (!Services.prefs.getBoolPref("services.push.connection.enabled")) {
110       // If push socket is disabled by the user, immediately error rather than
111       // timing out.
112       Services.DOMRequest.fireErrorAsync(req, "NetworkError");
113       return req;
114     }
116     this._cpmm.sendAsyncMessage("Push:Register", {
117                                   pageURL: this._pageURL.spec,
118                                   manifestURL: this._manifestURL,
119                                   requestID: this.getRequestId(req)
120                                 });
121     return req;
122   },
124   unregister: function(aPushEndpoint) {
125     debug("unregister(" + aPushEndpoint + ")");
126     let req = this.createRequest();
127     this._cpmm.sendAsyncMessage("Push:Unregister", {
128                                   pageURL: this._pageURL.spec,
129                                   manifestURL: this._manifestURL,
130                                   requestID: this.getRequestId(req),
131                                   pushEndpoint: aPushEndpoint
132                                 });
133     return req;
134   },
136   registrations: function() {
137     debug("registrations()");
138     let req = this.createRequest();
139     this._cpmm.sendAsyncMessage("Push:Registrations", {
140                                   manifestURL: this._manifestURL,
141                                   requestID: this.getRequestId(req)
142                                 });
143     return req;
144   }
147 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Push]);