Bug 1337026 - Cache the FxA profile in the UI. r?markh draft
authorEdouard Oger <eoger@fastmail.com>
Mon, 06 Feb 2017 12:13:06 -0500
changeset 479434 f79ac0faca32f2a703ef154b71e3028234179665
parent 479417 7f1b358fb17dfd982c5e18c34d5735cd481c7f7c
child 544684 5cbba9570aa98971f119bf1a49c361020970e68c
push id44254
push userbmo:eoger@fastmail.com
push dateMon, 06 Feb 2017 17:11:39 +0000
reviewersmarkh
bugs1337026
milestone54.0a1
Bug 1337026 - Cache the FxA profile in the UI. r?markh MozReview-Commit-ID: 9qZ81phzd3a
browser/base/content/browser-fxaccounts.js
--- a/browser/base/content/browser-fxaccounts.js
+++ b/browser/base/content/browser-fxaccounts.js
@@ -1,17 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 var gFxAccounts = {
 
   _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;
   },
 
@@ -137,31 +137,32 @@ var gFxAccounts = {
     }
 
     this._initialized = false;
   },
 
   observe(subject, topic, data) {
     switch (topic) {
       case this.FxAccountsCommon.ON_PROFILE_CHANGE_NOTIFICATION:
-        this._profileFetched = false;
+        this._cachedProfile = null;
         // Fallthrough intended
       default:
         this.updateUI();
         break;
     }
   },
 
   handleEvent(event) {
     this._inCustomizationMode = event.type == "customizationstarting";
     this.updateUI();
   },
 
   // Note that updateUI() returns a Promise that's only used by tests.
   updateUI() {
+    debugger
     let profileInfoEnabled = false;
     try {
       profileInfoEnabled = Services.prefs.getBoolPref("identity.fxaccounts.profile_image.enabled");
     } catch (e) { }
 
     this.panelUIFooter.hidden = false;
 
     // Make sure the button is disabled in customization mode.
@@ -250,29 +251,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);
     });