Bug 1337026 - Cache the FxA profile in the UI. r=markh, a=gchang
authorEdouard Oger <eoger@fastmail.com>
Thu, 16 Feb 2017 07:28:00 -0500
changeset 378514 c589ef7d5db52fe5de13e0a2d905f5c69ca18d48
parent 378513 a6e42f39aa98f16fcd93bb6f740db58953b57c65
child 378515 232dd77a7c38f8524f64356d1c43b7ff31c7488c
push id1419
push userjlund@mozilla.com
push dateMon, 10 Apr 2017 20:44:07 +0000
treeherdermozilla-release@5e6801b73ef6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmarkh, gchang
bugs1337026
milestone53.0a2
Bug 1337026 - Cache the FxA profile in the UI. r=markh, a=gchang MozReview-Commit-ID: Chmrj3otMhv
browser/base/content/browser-fxaccounts.js
browser/base/content/test/general/browser_fxaccounts.js
--- a/browser/base/content/browser-fxaccounts.js
+++ b/browser/base/content/browser-fxaccounts.js
@@ -3,17 +3,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 var gFxAccounts = {
 
   SYNC_MIGRATION_NOTIFICATION_TITLE: "fxa-migration",
 
   _initialized: false,
   _inCustomizationMode: false,
-  _profileFetched: false,
+  _cachedProfile: null,
 
   get weave() {
     delete this.weave;
     return this.weave = Cc["@mozilla.org/weave/service;1"]
                           .getService(Ci.nsISupports)
                           .wrappedJSObject;
   },
 
@@ -143,17 +143,17 @@ var gFxAccounts = {
   },
 
   observe(subject, topic, data) {
     switch (topic) {
       case "fxa-migration:state-changed":
         this.onMigrationStateChanged(data, subject);
         break;
       case this.FxAccountsCommon.ON_PROFILE_CHANGE_NOTIFICATION:
-        this._profileFetched = false;
+        this._cachedProfile = null;
         // Fallthrough intended
       default:
         this.updateUI();
         break;
     }
   },
 
   onMigrationStateChanged() {
@@ -316,29 +316,32 @@ var gFxAccounts = {
       }
     }
 
     return fxAccounts.getSignedInUser().then(userData => {
       // userData may be null here when the user is not signed-in, but that's expected
       updateWithUserData(userData);
       // unverified users cause us to spew log errors fetching an OAuth token
       // to fetch the profile, so don't even try in that case.
-      if (!userData || !userData.verified || !profileInfoEnabled || this._profileFetched) {
+      if (!userData || !userData.verified || !profileInfoEnabled) {
         return null; // don't even try to grab the profile.
       }
+      if (this._cachedProfile) {
+        return this._cachedProfile;
+      }
       return fxAccounts.getSignedInUserProfile().catch(err => {
         // Not fetching the profile is sad but the FxA logs will already have noise.
         return null;
       });
     }).then(profile => {
       if (!profile) {
         return;
       }
       updateWithProfile(profile);
-      this._profileFetched = true; // Try to avoid fetching the profile on every UI update
+      this._cachedProfile = profile; // Try to avoid fetching the profile on every UI update
     }).catch(error => {
       // This is most likely in tests, were we quickly log users in and out.
       // The most likely scenario is a user logged out, so reflect that.
       // Bug 995134 calls for better errors so we could retry if we were
       // sure this was the failure reason.
       this.FxAccountsCommon.log.error("Error updating FxA account info", error);
       updateWithUserData(null);
     });
--- a/browser/base/content/test/general/browser_fxaccounts.js
+++ b/browser/base/content/test/general/browser_fxaccounts.js
@@ -111,17 +111,17 @@ add_task(function* test_unverifiedUser()
 });
 */
 
 add_task(function* test_verifiedUserEmptyProfile() {
   // We see 2 updateAppMenuItem() calls - one for the signedInUser and one after
   // we first fetch the profile. We want them both to fire or we aren't testing
   // the state we think we are testing.
   let promiseUpdateDone = promiseObserver("test:browser_fxaccounts:updateAppMenuItem", 2);
-  gFxAccounts._profileFetched = false;
+  gFxAccounts._cachedProfile = null;
   configureProfileURL({}); // successful but empty profile.
   yield setSignedInUser(true); // this will fire the observer that does the update.
   yield promiseUpdateDone;
 
   // Check the world.
   Assert.ok(isFooterVisible())
   Assert.equal(panelUILabel.getAttribute("label"), "foo@example.com");
   Assert.equal(panelUIStatus.getAttribute("tooltiptext"),
@@ -131,32 +131,33 @@ add_task(function* test_verifiedUserEmpt
   let promisePreferencesOpened = promiseObserver("test:browser_fxaccounts:openPreferences");
   panelUIStatus.click();
   yield promisePreferencesOpened;
   yield signOut();
 });
 
 add_task(function* test_verifiedUserDisplayName() {
   let promiseUpdateDone = promiseObserver("test:browser_fxaccounts:updateAppMenuItem", 2);
-  gFxAccounts._profileFetched = false;
+  gFxAccounts._cachedProfile = null;
   configureProfileURL({ displayName: "Test User Display Name" });
   yield setSignedInUser(true); // this will fire the observer that does the update.
   yield promiseUpdateDone;
 
   Assert.ok(isFooterVisible())
   Assert.equal(panelUILabel.getAttribute("label"), "Test User Display Name");
   Assert.equal(panelUIStatus.getAttribute("tooltiptext"),
                panelUIStatus.getAttribute("signedinTooltiptext"));
   Assert.equal(panelUIFooter.getAttribute("fxastatus"), "signedin");
   yield signOut();
 });
 
 add_task(function* test_verifiedUserProfileFailure() {
   // profile failure means only one observer fires.
   let promiseUpdateDone = promiseObserver("test:browser_fxaccounts:updateAppMenuItem", 1);
+  gFxAccounts._cachedProfile = null;
   configureProfileURL(null, 500);
   yield setSignedInUser(true); // this will fire the observer that does the update.
   yield promiseUpdateDone;
 
   Assert.ok(isFooterVisible())
   Assert.equal(panelUILabel.getAttribute("label"), "foo@example.com");
   Assert.equal(panelUIStatus.getAttribute("tooltiptext"),
                panelUIStatus.getAttribute("signedinTooltiptext"));