Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | // Don't modify this, instead set services.push.debug. |
michael@0 | 8 | let gDebuggingEnabled = false; |
michael@0 | 9 | |
michael@0 | 10 | function debug(s) { |
michael@0 | 11 | if (gDebuggingEnabled) |
michael@0 | 12 | dump("-*- Push.js: " + s + "\n"); |
michael@0 | 13 | } |
michael@0 | 14 | |
michael@0 | 15 | const Cc = Components.classes; |
michael@0 | 16 | const Ci = Components.interfaces; |
michael@0 | 17 | const Cu = Components.utils; |
michael@0 | 18 | |
michael@0 | 19 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 20 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 21 | Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); |
michael@0 | 22 | Cu.import("resource://gre/modules/AppsUtils.jsm"); |
michael@0 | 23 | |
michael@0 | 24 | const PUSH_CID = Components.ID("{cde1d019-fad8-4044-b141-65fb4fb7a245}"); |
michael@0 | 25 | |
michael@0 | 26 | /** |
michael@0 | 27 | * The Push component runs in the child process and exposes the SimplePush API |
michael@0 | 28 | * to the web application. The PushService running in the parent process is the |
michael@0 | 29 | * one actually performing all operations. |
michael@0 | 30 | */ |
michael@0 | 31 | function Push() { |
michael@0 | 32 | debug("Push Constructor"); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | Push.prototype = { |
michael@0 | 36 | __proto__: DOMRequestIpcHelper.prototype, |
michael@0 | 37 | |
michael@0 | 38 | contractID: "@mozilla.org/push/PushManager;1", |
michael@0 | 39 | |
michael@0 | 40 | classID : PUSH_CID, |
michael@0 | 41 | |
michael@0 | 42 | QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, |
michael@0 | 43 | Ci.nsISupportsWeakReference, |
michael@0 | 44 | Ci.nsIObserver]), |
michael@0 | 45 | |
michael@0 | 46 | init: function(aWindow) { |
michael@0 | 47 | // Set debug first so that all debugging actually works. |
michael@0 | 48 | // NOTE: We don't add an observer here like in PushService. Flipping the |
michael@0 | 49 | // pref will require a reload of the app/page, which seems acceptable. |
michael@0 | 50 | gDebuggingEnabled = Services.prefs.getBoolPref("services.push.debug"); |
michael@0 | 51 | debug("init()"); |
michael@0 | 52 | |
michael@0 | 53 | let principal = aWindow.document.nodePrincipal; |
michael@0 | 54 | let appsService = Cc["@mozilla.org/AppsService;1"] |
michael@0 | 55 | .getService(Ci.nsIAppsService); |
michael@0 | 56 | |
michael@0 | 57 | this._manifestURL = appsService.getManifestURLByLocalId(principal.appId); |
michael@0 | 58 | this._pageURL = principal.URI; |
michael@0 | 59 | |
michael@0 | 60 | this.initDOMRequestHelper(aWindow, [ |
michael@0 | 61 | "PushService:Register:OK", |
michael@0 | 62 | "PushService:Register:KO", |
michael@0 | 63 | "PushService:Unregister:OK", |
michael@0 | 64 | "PushService:Unregister:KO", |
michael@0 | 65 | "PushService:Registrations:OK", |
michael@0 | 66 | "PushService:Registrations:KO" |
michael@0 | 67 | ]); |
michael@0 | 68 | |
michael@0 | 69 | this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] |
michael@0 | 70 | .getService(Ci.nsISyncMessageSender); |
michael@0 | 71 | }, |
michael@0 | 72 | |
michael@0 | 73 | receiveMessage: function(aMessage) { |
michael@0 | 74 | debug("receiveMessage()"); |
michael@0 | 75 | let request = this.getRequest(aMessage.data.requestID); |
michael@0 | 76 | let json = aMessage.data; |
michael@0 | 77 | if (!request) { |
michael@0 | 78 | debug("No request " + json.requestID); |
michael@0 | 79 | return; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | switch (aMessage.name) { |
michael@0 | 83 | case "PushService:Register:OK": |
michael@0 | 84 | Services.DOMRequest.fireSuccess(request, json.pushEndpoint); |
michael@0 | 85 | break; |
michael@0 | 86 | case "PushService:Register:KO": |
michael@0 | 87 | Services.DOMRequest.fireError(request, json.error); |
michael@0 | 88 | break; |
michael@0 | 89 | case "PushService:Unregister:OK": |
michael@0 | 90 | Services.DOMRequest.fireSuccess(request, json.pushEndpoint); |
michael@0 | 91 | break; |
michael@0 | 92 | case "PushService:Unregister:KO": |
michael@0 | 93 | Services.DOMRequest.fireError(request, json.error); |
michael@0 | 94 | break; |
michael@0 | 95 | case "PushService:Registrations:OK": |
michael@0 | 96 | Services.DOMRequest.fireSuccess(request, json.registrations); |
michael@0 | 97 | break; |
michael@0 | 98 | case "PushService:Registrations:KO": |
michael@0 | 99 | Services.DOMRequest.fireError(request, json.error); |
michael@0 | 100 | break; |
michael@0 | 101 | default: |
michael@0 | 102 | debug("NOT IMPLEMENTED! receiveMessage for " + aMessage.name); |
michael@0 | 103 | } |
michael@0 | 104 | }, |
michael@0 | 105 | |
michael@0 | 106 | register: function() { |
michael@0 | 107 | debug("register()"); |
michael@0 | 108 | let req = this.createRequest(); |
michael@0 | 109 | if (!Services.prefs.getBoolPref("services.push.connection.enabled")) { |
michael@0 | 110 | // If push socket is disabled by the user, immediately error rather than |
michael@0 | 111 | // timing out. |
michael@0 | 112 | Services.DOMRequest.fireErrorAsync(req, "NetworkError"); |
michael@0 | 113 | return req; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | this._cpmm.sendAsyncMessage("Push:Register", { |
michael@0 | 117 | pageURL: this._pageURL.spec, |
michael@0 | 118 | manifestURL: this._manifestURL, |
michael@0 | 119 | requestID: this.getRequestId(req) |
michael@0 | 120 | }); |
michael@0 | 121 | return req; |
michael@0 | 122 | }, |
michael@0 | 123 | |
michael@0 | 124 | unregister: function(aPushEndpoint) { |
michael@0 | 125 | debug("unregister(" + aPushEndpoint + ")"); |
michael@0 | 126 | let req = this.createRequest(); |
michael@0 | 127 | this._cpmm.sendAsyncMessage("Push:Unregister", { |
michael@0 | 128 | pageURL: this._pageURL.spec, |
michael@0 | 129 | manifestURL: this._manifestURL, |
michael@0 | 130 | requestID: this.getRequestId(req), |
michael@0 | 131 | pushEndpoint: aPushEndpoint |
michael@0 | 132 | }); |
michael@0 | 133 | return req; |
michael@0 | 134 | }, |
michael@0 | 135 | |
michael@0 | 136 | registrations: function() { |
michael@0 | 137 | debug("registrations()"); |
michael@0 | 138 | let req = this.createRequest(); |
michael@0 | 139 | this._cpmm.sendAsyncMessage("Push:Registrations", { |
michael@0 | 140 | manifestURL: this._manifestURL, |
michael@0 | 141 | requestID: this.getRequestId(req) |
michael@0 | 142 | }); |
michael@0 | 143 | return req; |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Push]); |