Bug 1416322 - Wait until the network is back up to sync after waking-up from sleep. r?markh draft
authorEdouard Oger <eoger@fastmail.com>
Thu, 16 Nov 2017 13:12:31 -0500
changeset 699801 03ce6344ccd1ffea1c41c120273177ef50babb72
parent 699799 7f123bd54a1727b469d307c7dbed2ca37096e69c
child 740732 8411a41a87c32948f4d396e70a29156c364be5d1
push id89685
push userbmo:eoger@fastmail.com
push dateFri, 17 Nov 2017 19:29:40 +0000
reviewersmarkh
bugs1416322
milestone59.0a1
Bug 1416322 - Wait until the network is back up to sync after waking-up from sleep. r?markh MozReview-Commit-ID: 2vzmj3UkvtD
services/sync/modules/policies.js
--- a/services/sync/modules/policies.js
+++ b/services/sync/modules/policies.js
@@ -62,16 +62,20 @@ SyncScheduler.prototype = {
     this.idleInterval         = getThrottledIntervalPreference("scheduler.idleInterval");
     this.activeInterval       = getThrottledIntervalPreference("scheduler.activeInterval");
     this.immediateInterval    = getThrottledIntervalPreference("scheduler.immediateInterval");
     this.eolInterval          = getThrottledIntervalPreference("scheduler.eolInterval");
 
     // A user is non-idle on startup by default.
     this.idle = false;
 
+    // That flag will be flipped if we resume from sleep and the network link
+    // is down.
+    this.shouldSyncWhenLinkComesUp = false;
+
     this.hasIncomingItems = false;
     // This is the last number of clients we saw when previously updating the
     // client mode. If this != currentNumClients (obtained from prefs written
     // by the clients engine) then we need to transition to and from
     // single and multi-device mode.
     this.numClientsLastSync = 0;
 
     this._resyncs = 0;
@@ -182,18 +186,25 @@ SyncScheduler.prototype = {
     this._log.trace("Handling " + topic);
     switch (topic) {
       case "weave:engine:score:updated":
         if (Status.login == LOGIN_SUCCEEDED) {
           CommonUtils.namedTimer(this.calculateScore, SCORE_UPDATE_DELAY, this,
                            "_scoreTimer");
         }
         break;
+      case "network:link-status-changed":
+        if (this.shouldSyncWhenLinkComesUp && !this.offline) {
+          this._log.debug("Network link is up for the first time since we woke-up. Syncing.");
+          this.shouldSyncWhenLinkComesUp = false;
+          this.scheduleNextSync(0);
+          break;
+        }
+        // Intended fallthrough
       case "network:offline-status-changed":
-      case "network:link-status-changed":
       case "captive-portal-detected":
         // Whether online or offline, we'll reschedule syncs
         this._log.trace("Network offline status change: " + data);
         this.checkSyncStatus();
         break;
       case "weave:service:sync:start":
         // Clear out any potentially pending syncs now that we're syncing
         this.clearSyncTriggers();
@@ -353,25 +364,32 @@ SyncScheduler.prototype = {
           if (this.numClients > 1) {
             this.scheduleNextSync(0);
           }
         }, IDLE_OBSERVER_BACK_DELAY, this, "idleDebouncerTimer");
         break;
       case "wake_notification":
         this._log.debug("Woke from sleep.");
         CommonUtils.nextTick(() => {
-          // Trigger a sync if we have multiple clients. We give it 5 seconds
-          // incase the network is still in the process of coming back up.
+          // Trigger a sync if we have multiple clients. We give it 2 seconds
+          // so the browser can recover from the wake and do more important
+          // operations first (timers etc).
           if (this.numClients > 1) {
-            this._log.debug("More than 1 client. Will sync in 5s.");
-            this.scheduleNextSync(5000);
+            if (!this.offline) {
+              this._log.debug("Online, will sync in 2s.");
+              this.scheduleNextSync(2000);
+            } else {
+              this._log.debug("Offline, will sync when link comes up.");
+              this.shouldSyncWhenLinkComesUp = true;
+            }
           }
         });
         break;
       case "captive-portal-login-success":
+        this.shouldSyncWhenLinkComesUp = false;
         this._log.debug("Captive portal login success. Scheduling a sync.");
         CommonUtils.nextTick(() => {
           this.scheduleNextSync(3000);
         });
         break;
     }
   },