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/. */
7 // Don't modify this, instead set services.push.debug.
8 let gDebuggingEnabled = false;
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}");
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.
32 debug("Push Constructor");
36 __proto__: DOMRequestIpcHelper.prototype,
38 contractID: "@mozilla.org/push/PushManager;1",
42 QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer,
43 Ci.nsISupportsWeakReference,
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");
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"
69 this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"]
70 .getService(Ci.nsISyncMessageSender);
73 receiveMessage: function(aMessage) {
74 debug("receiveMessage()");
75 let request = this.getRequest(aMessage.data.requestID);
76 let json = aMessage.data;
78 debug("No request " + json.requestID);
82 switch (aMessage.name) {
83 case "PushService:Register:OK":
84 Services.DOMRequest.fireSuccess(request, json.pushEndpoint);
86 case "PushService:Register:KO":
87 Services.DOMRequest.fireError(request, json.error);
89 case "PushService:Unregister:OK":
90 Services.DOMRequest.fireSuccess(request, json.pushEndpoint);
92 case "PushService:Unregister:KO":
93 Services.DOMRequest.fireError(request, json.error);
95 case "PushService:Registrations:OK":
96 Services.DOMRequest.fireSuccess(request, json.registrations);
98 case "PushService:Registrations:KO":
99 Services.DOMRequest.fireError(request, json.error);
102 debug("NOT IMPLEMENTED! receiveMessage for " + aMessage.name);
106 register: function() {
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
112 Services.DOMRequest.fireErrorAsync(req, "NetworkError");
116 this._cpmm.sendAsyncMessage("Push:Register", {
117 pageURL: this._pageURL.spec,
118 manifestURL: this._manifestURL,
119 requestID: this.getRequestId(req)
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
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)
147 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Push]);