michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: // Don't modify this, instead set services.push.debug. michael@0: let gDebuggingEnabled = false; michael@0: michael@0: function debug(s) { michael@0: if (gDebuggingEnabled) michael@0: dump("-*- Push.js: " + s + "\n"); michael@0: } michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); michael@0: Cu.import("resource://gre/modules/AppsUtils.jsm"); michael@0: michael@0: const PUSH_CID = Components.ID("{cde1d019-fad8-4044-b141-65fb4fb7a245}"); michael@0: michael@0: /** michael@0: * The Push component runs in the child process and exposes the SimplePush API michael@0: * to the web application. The PushService running in the parent process is the michael@0: * one actually performing all operations. michael@0: */ michael@0: function Push() { michael@0: debug("Push Constructor"); michael@0: } michael@0: michael@0: Push.prototype = { michael@0: __proto__: DOMRequestIpcHelper.prototype, michael@0: michael@0: contractID: "@mozilla.org/push/PushManager;1", michael@0: michael@0: classID : PUSH_CID, michael@0: michael@0: QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, michael@0: Ci.nsISupportsWeakReference, michael@0: Ci.nsIObserver]), michael@0: michael@0: init: function(aWindow) { michael@0: // Set debug first so that all debugging actually works. michael@0: // NOTE: We don't add an observer here like in PushService. Flipping the michael@0: // pref will require a reload of the app/page, which seems acceptable. michael@0: gDebuggingEnabled = Services.prefs.getBoolPref("services.push.debug"); michael@0: debug("init()"); michael@0: michael@0: let principal = aWindow.document.nodePrincipal; michael@0: let appsService = Cc["@mozilla.org/AppsService;1"] michael@0: .getService(Ci.nsIAppsService); michael@0: michael@0: this._manifestURL = appsService.getManifestURLByLocalId(principal.appId); michael@0: this._pageURL = principal.URI; michael@0: michael@0: this.initDOMRequestHelper(aWindow, [ michael@0: "PushService:Register:OK", michael@0: "PushService:Register:KO", michael@0: "PushService:Unregister:OK", michael@0: "PushService:Unregister:KO", michael@0: "PushService:Registrations:OK", michael@0: "PushService:Registrations:KO" michael@0: ]); michael@0: michael@0: this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] michael@0: .getService(Ci.nsISyncMessageSender); michael@0: }, michael@0: michael@0: receiveMessage: function(aMessage) { michael@0: debug("receiveMessage()"); michael@0: let request = this.getRequest(aMessage.data.requestID); michael@0: let json = aMessage.data; michael@0: if (!request) { michael@0: debug("No request " + json.requestID); michael@0: return; michael@0: } michael@0: michael@0: switch (aMessage.name) { michael@0: case "PushService:Register:OK": michael@0: Services.DOMRequest.fireSuccess(request, json.pushEndpoint); michael@0: break; michael@0: case "PushService:Register:KO": michael@0: Services.DOMRequest.fireError(request, json.error); michael@0: break; michael@0: case "PushService:Unregister:OK": michael@0: Services.DOMRequest.fireSuccess(request, json.pushEndpoint); michael@0: break; michael@0: case "PushService:Unregister:KO": michael@0: Services.DOMRequest.fireError(request, json.error); michael@0: break; michael@0: case "PushService:Registrations:OK": michael@0: Services.DOMRequest.fireSuccess(request, json.registrations); michael@0: break; michael@0: case "PushService:Registrations:KO": michael@0: Services.DOMRequest.fireError(request, json.error); michael@0: break; michael@0: default: michael@0: debug("NOT IMPLEMENTED! receiveMessage for " + aMessage.name); michael@0: } michael@0: }, michael@0: michael@0: register: function() { michael@0: debug("register()"); michael@0: let req = this.createRequest(); michael@0: if (!Services.prefs.getBoolPref("services.push.connection.enabled")) { michael@0: // If push socket is disabled by the user, immediately error rather than michael@0: // timing out. michael@0: Services.DOMRequest.fireErrorAsync(req, "NetworkError"); michael@0: return req; michael@0: } michael@0: michael@0: this._cpmm.sendAsyncMessage("Push:Register", { michael@0: pageURL: this._pageURL.spec, michael@0: manifestURL: this._manifestURL, michael@0: requestID: this.getRequestId(req) michael@0: }); michael@0: return req; michael@0: }, michael@0: michael@0: unregister: function(aPushEndpoint) { michael@0: debug("unregister(" + aPushEndpoint + ")"); michael@0: let req = this.createRequest(); michael@0: this._cpmm.sendAsyncMessage("Push:Unregister", { michael@0: pageURL: this._pageURL.spec, michael@0: manifestURL: this._manifestURL, michael@0: requestID: this.getRequestId(req), michael@0: pushEndpoint: aPushEndpoint michael@0: }); michael@0: return req; michael@0: }, michael@0: michael@0: registrations: function() { michael@0: debug("registrations()"); michael@0: let req = this.createRequest(); michael@0: this._cpmm.sendAsyncMessage("Push:Registrations", { michael@0: manifestURL: this._manifestURL, michael@0: requestID: this.getRequestId(req) michael@0: }); michael@0: return req; michael@0: } michael@0: } michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Push]);