Bug 1440467 - Add a pref to always connect to the Push server without existing subscriptions. r?mt draft
authorKit Cambridge <kit@yakshaving.ninja>
Thu, 22 Feb 2018 19:58:20 -0800
changeset 758870 1d99ad8ed1abd74694a1b84a842f9ed7d842cead
parent 758142 994a684a7564c2735d98d6910a78d079a68f0b25
push id100200
push userbmo:kit@mozilla.com
push dateFri, 23 Feb 2018 05:13:54 +0000
reviewersmt
bugs1440467
milestone60.0a1
Bug 1440467 - Add a pref to always connect to the Push server without existing subscriptions. r?mt MozReview-Commit-ID: ARXbgfaMJDd
dom/push/PushService.jsm
dom/push/PushServiceWebSocket.jsm
modules/libpref/init/all.js
--- a/dom/push/PushService.jsm
+++ b/dom/push/PushService.jsm
@@ -210,46 +210,49 @@ this.PushService = {
         }
       }
       this._notifyActivated = null;
       this._activated = null;
     }
     this._state = aNewState;
   },
 
-  _changeStateOfflineEvent: function(offline, calledFromConnEnabledEvent) {
+  async _changeStateOfflineEvent(offline, calledFromConnEnabledEvent) {
     console.debug("changeStateOfflineEvent()", offline);
 
     if (this._state < PUSH_SERVICE_ACTIVE_OFFLINE &&
         this._state != PUSH_SERVICE_ACTIVATING &&
         !calledFromConnEnabledEvent) {
-      return Promise.resolve();
+      return;
     }
 
     if (offline) {
       if (this._state == PUSH_SERVICE_RUNNING) {
         this._service.disconnect();
       }
       this._setState(PUSH_SERVICE_ACTIVE_OFFLINE);
-      return Promise.resolve();
+      return;
     }
 
     if (this._state == PUSH_SERVICE_RUNNING) {
       // PushService was not in the offline state, but got notification to
       // go online (a offline notification has not been sent).
       // Disconnect first.
       this._service.disconnect();
     }
-    return this.getAllUnexpired().then(records => {
-      this._setState(PUSH_SERVICE_RUNNING);
-      if (records.length > 0) {
-        // if there are request waiting
-        this._service.connect(records);
-      }
-    });
+
+    let records = await this.getAllUnexpired();
+
+    this._setState(PUSH_SERVICE_RUNNING);
+
+    if (records.length > 0 || prefs.get("alwaysConnect")) {
+      // Connect if we have existing subscriptions, or if the always-on pref
+      // is set.
+      this._service.connect(records);
+    }
   },
 
   _changeStateConnectionEnabledEvent: function(enabled) {
     console.debug("changeStateConnectionEnabledEvent()", enabled);
 
     if (this._state < PUSH_SERVICE_CONNECTION_DISABLE &&
         this._state != PUSH_SERVICE_ACTIVATING) {
       return Promise.resolve();
--- a/dom/push/PushServiceWebSocket.jsm
+++ b/dom/push/PushServiceWebSocket.jsm
@@ -509,20 +509,17 @@ this.PushServiceWebSocket = {
       console.error("beginWSSetup: Error opening websocket.",
         "asyncOpen failed", e);
       this._reconnect();
     }
   },
 
   connect: function(records) {
     console.debug("connect()");
-    // Check to see if we need to do anything.
-    if (records.length > 0) {
-      this._beginWSSetup();
-    }
+    this._beginWSSetup();
   },
 
   isConnected: function() {
     return !!this._ws;
   },
 
   /**
    * Protocol handler invoked by server message.
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -5161,16 +5161,17 @@ pref("dom.vibrator.max_vibrate_list_len"
 pref("dom.battery.enabled", true);
 
 // Streams API
 pref("dom.streams.enabled", false);
 
 // Push
 
 pref("dom.push.enabled", false);
+pref("dom.push.alwaysConnect", false);
 
 pref("dom.push.loglevel", "error");
 
 pref("dom.push.serverURL", "wss://push.services.mozilla.com/");
 pref("dom.push.userAgentID", "");
 
 // The maximum number of push messages that a service worker can receive
 // without user interaction.