1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/push/src/Push.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,147 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +// Don't modify this, instead set services.push.debug. 1.11 +let gDebuggingEnabled = false; 1.12 + 1.13 +function debug(s) { 1.14 + if (gDebuggingEnabled) 1.15 + dump("-*- Push.js: " + s + "\n"); 1.16 +} 1.17 + 1.18 +const Cc = Components.classes; 1.19 +const Ci = Components.interfaces; 1.20 +const Cu = Components.utils; 1.21 + 1.22 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.23 +Cu.import("resource://gre/modules/Services.jsm"); 1.24 +Cu.import("resource://gre/modules/DOMRequestHelper.jsm"); 1.25 +Cu.import("resource://gre/modules/AppsUtils.jsm"); 1.26 + 1.27 +const PUSH_CID = Components.ID("{cde1d019-fad8-4044-b141-65fb4fb7a245}"); 1.28 + 1.29 +/** 1.30 + * The Push component runs in the child process and exposes the SimplePush API 1.31 + * to the web application. The PushService running in the parent process is the 1.32 + * one actually performing all operations. 1.33 + */ 1.34 +function Push() { 1.35 + debug("Push Constructor"); 1.36 +} 1.37 + 1.38 +Push.prototype = { 1.39 + __proto__: DOMRequestIpcHelper.prototype, 1.40 + 1.41 + contractID: "@mozilla.org/push/PushManager;1", 1.42 + 1.43 + classID : PUSH_CID, 1.44 + 1.45 + QueryInterface : XPCOMUtils.generateQI([Ci.nsIDOMGlobalPropertyInitializer, 1.46 + Ci.nsISupportsWeakReference, 1.47 + Ci.nsIObserver]), 1.48 + 1.49 + init: function(aWindow) { 1.50 + // Set debug first so that all debugging actually works. 1.51 + // NOTE: We don't add an observer here like in PushService. Flipping the 1.52 + // pref will require a reload of the app/page, which seems acceptable. 1.53 + gDebuggingEnabled = Services.prefs.getBoolPref("services.push.debug"); 1.54 + debug("init()"); 1.55 + 1.56 + let principal = aWindow.document.nodePrincipal; 1.57 + let appsService = Cc["@mozilla.org/AppsService;1"] 1.58 + .getService(Ci.nsIAppsService); 1.59 + 1.60 + this._manifestURL = appsService.getManifestURLByLocalId(principal.appId); 1.61 + this._pageURL = principal.URI; 1.62 + 1.63 + this.initDOMRequestHelper(aWindow, [ 1.64 + "PushService:Register:OK", 1.65 + "PushService:Register:KO", 1.66 + "PushService:Unregister:OK", 1.67 + "PushService:Unregister:KO", 1.68 + "PushService:Registrations:OK", 1.69 + "PushService:Registrations:KO" 1.70 + ]); 1.71 + 1.72 + this._cpmm = Cc["@mozilla.org/childprocessmessagemanager;1"] 1.73 + .getService(Ci.nsISyncMessageSender); 1.74 + }, 1.75 + 1.76 + receiveMessage: function(aMessage) { 1.77 + debug("receiveMessage()"); 1.78 + let request = this.getRequest(aMessage.data.requestID); 1.79 + let json = aMessage.data; 1.80 + if (!request) { 1.81 + debug("No request " + json.requestID); 1.82 + return; 1.83 + } 1.84 + 1.85 + switch (aMessage.name) { 1.86 + case "PushService:Register:OK": 1.87 + Services.DOMRequest.fireSuccess(request, json.pushEndpoint); 1.88 + break; 1.89 + case "PushService:Register:KO": 1.90 + Services.DOMRequest.fireError(request, json.error); 1.91 + break; 1.92 + case "PushService:Unregister:OK": 1.93 + Services.DOMRequest.fireSuccess(request, json.pushEndpoint); 1.94 + break; 1.95 + case "PushService:Unregister:KO": 1.96 + Services.DOMRequest.fireError(request, json.error); 1.97 + break; 1.98 + case "PushService:Registrations:OK": 1.99 + Services.DOMRequest.fireSuccess(request, json.registrations); 1.100 + break; 1.101 + case "PushService:Registrations:KO": 1.102 + Services.DOMRequest.fireError(request, json.error); 1.103 + break; 1.104 + default: 1.105 + debug("NOT IMPLEMENTED! receiveMessage for " + aMessage.name); 1.106 + } 1.107 + }, 1.108 + 1.109 + register: function() { 1.110 + debug("register()"); 1.111 + let req = this.createRequest(); 1.112 + if (!Services.prefs.getBoolPref("services.push.connection.enabled")) { 1.113 + // If push socket is disabled by the user, immediately error rather than 1.114 + // timing out. 1.115 + Services.DOMRequest.fireErrorAsync(req, "NetworkError"); 1.116 + return req; 1.117 + } 1.118 + 1.119 + this._cpmm.sendAsyncMessage("Push:Register", { 1.120 + pageURL: this._pageURL.spec, 1.121 + manifestURL: this._manifestURL, 1.122 + requestID: this.getRequestId(req) 1.123 + }); 1.124 + return req; 1.125 + }, 1.126 + 1.127 + unregister: function(aPushEndpoint) { 1.128 + debug("unregister(" + aPushEndpoint + ")"); 1.129 + let req = this.createRequest(); 1.130 + this._cpmm.sendAsyncMessage("Push:Unregister", { 1.131 + pageURL: this._pageURL.spec, 1.132 + manifestURL: this._manifestURL, 1.133 + requestID: this.getRequestId(req), 1.134 + pushEndpoint: aPushEndpoint 1.135 + }); 1.136 + return req; 1.137 + }, 1.138 + 1.139 + registrations: function() { 1.140 + debug("registrations()"); 1.141 + let req = this.createRequest(); 1.142 + this._cpmm.sendAsyncMessage("Push:Registrations", { 1.143 + manifestURL: this._manifestURL, 1.144 + requestID: this.getRequestId(req) 1.145 + }); 1.146 + return req; 1.147 + } 1.148 +} 1.149 + 1.150 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([Push]);