Bug 1240615 - Make PushService protocol init method return a Promise. r?kitcambridge
authorNick Alexander <nalexander@mozilla.com>
Wed, 18 Nov 2015 15:55:13 -0800
changeset 326877 e81adecdc007349bb9063ed65be5b32842bf439d
parent 326876 248232b19142b95a229c69f10096d7a7a7dcd5c7
child 326878 694bfb769575f113c13cb301656678a2b75bf568
push id10169
push userdminor@mozilla.com
push dateThu, 28 Jan 2016 13:10:48 +0000
reviewerskitcambridge
bugs1240615
milestone46.0a1
Bug 1240615 - Make PushService protocol init method return a Promise. r?kitcambridge I'd like to make `PushService.init` fallible, but it's not obvious how to act on failure. This patch at least allows each service to block _startService until service-specific work initialization is complete.
dom/push/PushService.jsm
dom/push/PushServiceHttp2.jsm
dom/push/PushServiceWebSocket.jsm
--- a/dom/push/PushService.jsm
+++ b/dom/push/PushService.jsm
@@ -508,29 +508,31 @@ this.PushService = {
     // permissions.
     Services.obs.addObserver(this, "perm-changed", false);
   },
 
   _startService: function(service, serverURI, options = {}) {
     console.debug("startService()");
 
     if (this._state != PUSH_SERVICE_ACTIVATING) {
-      return;
+      return Promise.reject();
     }
 
     this._service = service;
 
     this._db = options.db;
     if (!this._db) {
       this._db = this._service.newPushDB();
     }
 
-    this._service.init(options, this, serverURI);
-    this._startObservers();
-    return this._dropExpiredRegistrations();
+    return this._service.init(options, this, serverURI)
+      .then(() => {
+        this._startObservers();
+        return this._dropExpiredRegistrations();
+      });
   },
 
   /**
    * PushService uninitialization is divided into 3 parts:
    * stopObservers() - stot observers started in startObservers.
    * stopService() - It stops listening for broadcasted messages, stops db and
    *                 PushService connection (WebSocket).
    *                 state is changed to PUSH_SERVICE_INIT.
--- a/dom/push/PushServiceHttp2.jsm
+++ b/dom/push/PushServiceHttp2.jsm
@@ -567,16 +567,18 @@ this.PushServiceHttp2 = {
     // so we don't listen for response.
     this._deleteResource(aAckUri);
   },
 
   init: function(aOptions, aMainPushService, aServerURL) {
     console.debug("init()");
     this._mainPushService = aMainPushService;
     this._serverURI = aServerURL;
+
+    return Promise.resolve();
   },
 
   _retryAfterBackoff: function(aSubscriptionUri, retryAfter) {
     console.debug("retryAfterBackoff()");
 
     var resetRetryCount = prefs.get("http2.reset_retry_count_after_ms");
     // If it was running for some time, reset retry counter.
     if ((Date.now() - this._conns[aSubscriptionUri].lastStartListening) >
--- a/dom/push/PushServiceWebSocket.jsm
+++ b/dom/push/PushServiceWebSocket.jsm
@@ -294,16 +294,18 @@ this.PushServiceWebSocket = {
     this._networkInfo = options.networkInfo;
     if (!this._networkInfo) {
       this._networkInfo = PushNetworkInfo;
     }
 
     this._requestTimeout = prefs.get("requestTimeout");
     this._adaptiveEnabled = prefs.get('adaptive.enabled');
     this._upperLimit = prefs.get('adaptive.upperLimit');
+
+    return Promise.resolve();
   },
 
   _reconnect: function () {
     console.debug("reconnect()");
     this._shutdownWS(false);
     this._reconnectAfterBackoff();
   },