|
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/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 // Don't modify this, instead set services.push.debug. |
|
8 let gDebuggingEnabled = false; |
|
9 |
|
10 function debug(s) { |
|
11 if (gDebuggingEnabled) |
|
12 dump("-*- Push.js: " + s + "\n"); |
|
13 } |
|
14 |
|
15 const Cc = Components.classes; |
|
16 const Ci = Components.interfaces; |
|
17 const Cu = Components.utils; |
|
18 |
|
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"); |
|
23 |
|
24 const PUSH_CID = Components.ID("{cde1d019-fad8-4044-b141-65fb4fb7a245}"); |
|
25 |
|
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"); |
|
33 } |
|
34 |
|
35 Push.prototype = { |
|
36 __proto__: DOMRequestIpcHelper.prototype, |
|
37 |
|
38 contractID: "@mozilla.org/push/PushManager;1", |
|
39 |
|
40 classID : PUSH_CID, |
|
41 |
|
42 QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, |
|
43 Ci.nsISupportsWeakReference, |
|
44 Ci.nsIObserver]), |
|
45 |
|
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()"); |
|
52 |
|
53 let principal = aWindow.document.nodePrincipal; |
|
54 let appsService = Cc["@mozilla.org/AppsService;1"] |
|
55 .getService(Ci.nsIAppsService); |
|
56 |
|
57 this._manifestURL = appsService.getManifestURLByLocalId(principal.appId); |
|
58 this._pageURL = principal.URI; |
|
59 |
|
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 ]); |
|
68 |
|
69 this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] |
|
70 .getService(Ci.nsISyncMessageSender); |
|
71 }, |
|
72 |
|
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 } |
|
81 |
|
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 }, |
|
105 |
|
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 } |
|
115 |
|
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 }, |
|
123 |
|
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 }, |
|
135 |
|
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 } |
|
145 } |
|
146 |
|
147 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Push]); |