Bug 1321740 - Avoid a full sync after signing in due to fxa reauthentication. r=markh
authorThom Chiovoloni <tchiovoloni@mozilla.com>
Tue, 21 Feb 2017 16:18:43 -0500
changeset 394411 5ea643137f3e153666f9d8b5b3b3e19384ea2be6
parent 394410 32524eee28c5d59138e42526255d77ee884ffb38
child 394412 4af8a2761308d415940e367fdffb435b380fd554
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmarkh
bugs1321740
milestone54.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1321740 - Avoid a full sync after signing in due to fxa reauthentication. r=markh MozReview-Commit-ID: 8wVq1RYojUS
services/sync/modules/browserid_identity.js
--- a/services/sync/modules/browserid_identity.js
+++ b/services/sync/modules/browserid_identity.js
@@ -249,28 +249,47 @@ this.BrowserIDManager.prototype = {
     // authentication header, we will notice the lack of the token and fetch a
     // new one.
     this._token = null;
   },
 
   observe(subject, topic, data) {
     this._log.debug("observed " + topic);
     switch (topic) {
-    case fxAccountsCommon.ONLOGIN_NOTIFICATION:
+    case fxAccountsCommon.ONLOGIN_NOTIFICATION: {
       // This should only happen if we've been initialized without a current
       // user - otherwise we'd have seen the LOGOUT notification and been
       // thrown away.
       // The exception is when we've initialized with a user that needs to
       // reauth with the server - in that case we will also get here, but
-      // should have the same identity.
+      // should have the same identity, and so we pass `false` into
+      // initializeWithCurrentIdentity so that we won't do a full sync for our
+      // first sync if we can avoid it.
       // initializeWithCurrentIdentity will throw and log if these constraints
       // aren't met (indirectly, via _updateSignedInUser()), so just go ahead
       // and do the init.
-      this.initializeWithCurrentIdentity(true);
-      break;
+      let firstLogin = !this.username;
+      this.initializeWithCurrentIdentity(firstLogin);
+
+      if (!firstLogin) {
+        // We still want to trigger these even if it isn't our first login.
+        // Note that the promise returned by `initializeWithCurrentIdentity`
+        // is resolved at the start of authentication, but we don't want to fire
+        // this event or start the next sync until after authentication is done
+        // (which is signaled by `this.whenReadyToAuthenticate.promise` resolving).
+        this.whenReadyToAuthenticate.promise.then(() => {
+          Services.obs.notifyObservers(null, "weave:service:setup-complete", null);
+          return new Promise(resolve => { Weave.Utils.nextTick(resolve, null); })
+        }).then(() => {
+          Weave.Service.sync();
+        }).catch(e => {
+          this._log.warn("Failed to trigger setup complete notification", e);
+        });
+      }
+    } break;
 
     case fxAccountsCommon.ONLOGOUT_NOTIFICATION:
       Weave.Service.startOver();
       // startOver will cause this instance to be thrown away, so there's
       // nothing else to do.
       break;
 
     case fxAccountsCommon.ON_ACCOUNT_STATE_CHANGE_NOTIFICATION: