Bug 1242972 - Make services/fxaccounts eslintable. r?markh draft
authorHector Zhao <bzhao@mozilla.com>
Tue, 26 Jan 2016 23:07:56 +0800
changeset 325813 3df6ead76a9fa212e855290acbdae16f060db4a9
parent 325812 885a4cc8dd3bb137c06c41a36a6c430d198ef5fb
child 325814 9737e72d6413dcb2c69fbca9dc2dab7e1b6d3cd6
push id10051
push userbzhao@mozilla.com
push dateTue, 26 Jan 2016 15:36:15 +0000
reviewersmarkh
bugs1242972
milestone47.0a1
Bug 1242972 - Make services/fxaccounts eslintable. r?markh
services/fxaccounts/FxAccountsProfileClient.jsm
services/fxaccounts/FxAccountsStorage.jsm
services/fxaccounts/moz.build
services/fxaccounts/tests/xpcshell/test_accounts.js
services/fxaccounts/tests/xpcshell/test_profile.js
--- a/services/fxaccounts/FxAccountsProfileClient.jsm
+++ b/services/fxaccounts/FxAccountsProfileClient.jsm
@@ -83,30 +83,36 @@ this.FxAccountsProfileClient.prototype =
   _createRequest: Task.async(function* (path, method = "GET") {
     let token = this.token;
     if (!token) {
       // tokens are cached, so getting them each request is cheap.
       token = yield this.fxa.getOAuthToken(this.oauthOptions);
     }
     try {
       return (yield this._rawRequest(path, method, token));
-    } catch (ex if ex instanceof FxAccountsProfileClientError && ex.code == 401) {
+    } catch (ex) {
+      if (!ex instanceof FxAccountsProfileClientError || ex.code != 401) {
+        throw ex;
+      }
       // If this object was instantiated with a token then we don't refresh it.
       if (this.token) {
         throw ex;
       }
       // it's an auth error - assume our token expired and retry.
       log.info("Fetching the profile returned a 401 - revoking our token and retrying");
       yield this.fxa.removeCachedOAuthToken({token});
       token = yield this.fxa.getOAuthToken(this.oauthOptions);
       // and try with the new token - if that also fails then we fail after
       // revoking the token.
       try {
         return (yield this._rawRequest(path, method, token));
-      } catch (ex if ex instanceof FxAccountsProfileClientError && ex.code == 401) {
+      } catch (ex) {
+        if (!ex instanceof FxAccountsProfileClientError || ex.code != 401) {
+          throw ex;
+        }
         log.info("Retry fetching the profile still returned a 401 - revoking our token and failing");
         yield this.fxa.removeCachedOAuthToken({token});
         throw ex;
       }
     }
   }),
 
   /**
--- a/services/fxaccounts/FxAccountsStorage.jsm
+++ b/services/fxaccounts/FxAccountsStorage.jsm
@@ -4,16 +4,17 @@
 "use strict";
 
 this.EXPORTED_SYMBOLS = [
   "FxAccountsStorageManager",
 ];
 
 const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
 
+Cu.import("resource://gre/modules/AppConstants.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/Task.jsm");
 Cu.import("resource://gre/modules/FxAccountsCommon.js");
 Cu.import("resource://gre/modules/osfile.jsm");
 Cu.import("resource://services-common/utils.js");
 
 this.FxAccountsStorageManager = function(options = {}) {
   this.options = {
@@ -332,21 +333,23 @@ this.FxAccountsStorageManager.prototype 
           }
         }
         if (needWrite) {
           log.debug("successfully read secure data; writing updated data back")
           yield this._doWriteSecure();
         }
       }
       this._needToReadSecure = false;
-    } catch (ex if ex instanceof this.secureStorage.STORAGE_LOCKED) {
-      log.debug("setAccountData: secure storage is locked trying to read");
     } catch (ex) {
-      log.error("failed to read secure storage", ex);
-      throw ex;
+      if (ex instanceof this.secureStorage.STORAGE_LOCKED) {
+        log.debug("setAccountData: secure storage is locked trying to read");
+      } else {
+        log.error("failed to read secure storage", ex);
+        throw ex;
+      }
     }
   }),
 
   _write() {
     // We don't want multiple writes happening concurrently, and we also need to
     // know when an "old" storage manager is done (this.finalize() waits for this)
     return this._queueStorageOperation(() => this.__write());
   },
@@ -384,30 +387,33 @@ this.FxAccountsStorageManager.prototype 
     }
     log.debug("writing secure storage", Object.keys(this.cachedSecure));
     let toWriteSecure = {
       version: DATA_FORMAT_VERSION,
       accountData: this.cachedSecure,
     }
     try {
       yield this.secureStorage.set(this.cachedPlain.email, toWriteSecure);
-    } catch (ex if ex instanceof this.secureStorage.STORAGE_LOCKED) {
+    } catch (ex) {
+      if (!ex instanceof this.secureStorage.STORAGE_LOCKED) {
+        throw ex;
+      }
       // This shouldn't be possible as once it is unlocked it can't be
       // re-locked, and we can only be here if we've previously managed to
       // read.
       log.error("setAccountData: secure storage is locked trying to write");
     }
   }),
 
   // Delete the data for an account - ie, called on "sign out".
   deleteAccountData() {
     return this._queueStorageOperation(() => this._deleteAccountData());
   },
 
-  _deleteAccountData: Task.async(function() {
+  _deleteAccountData: Task.async(function* () {
     log.debug("removing account data");
     yield this._promiseInitialized;
     yield this.plainStorage.set(null);
     if (this.secureStorage) {
       yield this.secureStorage.set(null);
     }
     this._clearCachedData();
     log.debug("account data reset");
@@ -534,22 +540,24 @@ LoginManagerStorage.prototype = {
       let existingLogins = Services.logins.findLogins({}, FXA_PWDMGR_HOST, null,
                                                       FXA_PWDMGR_REALM);
       if (existingLogins.length) {
         Services.logins.modifyLogin(existingLogins[0], login);
       } else {
         Services.logins.addLogin(login);
       }
       log.trace("finished write of user data to the login manager");
-    } catch (ex if ex instanceof this.STORAGE_LOCKED) {
-      throw ex;
     } catch (ex) {
-      // just log and consume the error here - it may be a 3rd party login
-      // manager replacement that's simply broken.
-      log.error("Failed to save data to the login manager", ex);
+      if (ex instanceof this.STORAGE_LOCKED) {
+        throw ex;
+      } else {
+        // just log and consume the error here - it may be a 3rd party login
+        // manager replacement that's simply broken.
+        log.error("Failed to save data to the login manager", ex);
+      }
     }
   }),
 
   get: Task.async(function* (uid, email) {
     log.trace("starting fetch of user data from the login manager");
 
     try { // Services.logins might be third-party and broken...
       // read the data from the login manager and merge it for return.
@@ -570,29 +578,26 @@ LoginManagerStorage.prototype = {
       // Support either the uid or the email as the username - we plan to move
       // to storing the uid once Fx41 hits the release channel as the code below
       // that handles either first landed in 41. Bug 1183951 is to store the uid.
       if (login.username == uid || login.username == email) {
         return JSON.parse(login.password);
       }
       log.info("username in the login manager doesn't match - ignoring it");
       yield this._clearLoginMgrData();
-    } catch (ex if ex instanceof this.STORAGE_LOCKED) {
-      throw ex;
     } catch (ex) {
-      // just log and consume the error here - it may be a 3rd party login
-      // manager replacement that's simply broken.
-      log.error("Failed to get data from the login manager", ex);
+      if (ex instanceof this.STORAGE_LOCKED) {
+        throw ex;
+      } else {
+        // just log and consume the error here - it may be a 3rd party login
+        // manager replacement that's simply broken.
+        log.error("Failed to get data from the login manager", ex);
+      }
     }
     return null;
   }),
 }
 
 // A global variable to indicate if the login manager is available - it doesn't
 // exist on b2g. Defined here as the use of preprocessor directives skews line
 // numbers in the runtime, meaning stack-traces etc end up off by a few lines.
 // Doing it at the end of the file makes that less of a pita.
-var haveLoginManager =
-#if defined(MOZ_B2G)
-                       false;
-#else
-                       true;
-#endif
+var haveLoginManager = !AppConstants.MOZ_B2G;
--- a/services/fxaccounts/moz.build
+++ b/services/fxaccounts/moz.build
@@ -14,18 +14,15 @@ EXTRA_JS_MODULES += [
   'Credentials.jsm',
   'FxAccounts.jsm',
   'FxAccountsClient.jsm',
   'FxAccountsCommon.js',
   'FxAccountsOAuthClient.jsm',
   'FxAccountsOAuthGrantClient.jsm',
   'FxAccountsProfile.jsm',
   'FxAccountsProfileClient.jsm',
+  'FxAccountsStorage.jsm',
   'FxAccountsWebChannel.jsm',
 ]
 
-EXTRA_PP_JS_MODULES += [
-  'FxAccountsStorage.jsm',
-]
-
 # For now, we will only be using the FxA manager in B2G.
 if CONFIG['MOZ_B2G']:
   EXTRA_JS_MODULES += ['FxAccountsManager.jsm']
--- a/services/fxaccounts/tests/xpcshell/test_accounts.js
+++ b/services/fxaccounts/tests/xpcshell/test_accounts.js
@@ -563,17 +563,17 @@ add_test(function test_overlapping_signi
       });
     });
   });
 });
 
 add_task(function* test_getAssertion() {
   let fxa = new MockFxAccounts();
 
-  do_check_throws(function() {
+  do_check_throws(function* () {
     yield fxa.getAssertion("nonaudience");
   });
 
   let creds = {
     sessionToken: "sessionToken",
     kA: expandHex("11"),
     kB: expandHex("66"),
     verified: true
--- a/services/fxaccounts/tests/xpcshell/test_profile.js
+++ b/services/fxaccounts/tests/xpcshell/test_profile.js
@@ -229,21 +229,29 @@ add_task(function* fetchAndCacheProfileO
 
   // reject the promise.
   rejectProfile("oh noes");
 
   // both requests should reject.
   try {
     yield request1;
     throw new Error("should have rejected");
-  } catch (ex if ex == "oh noes") {}
+  } catch (ex) {
+    if (ex != "oh noes") {
+      throw ex;
+    }
+  }
   try {
     yield request2;
     throw new Error("should have rejected");
-  } catch (ex if ex == "oh noes") {}
+  } catch (ex) {
+    if (ex != "oh noes") {
+      throw ex;
+    }
+  }
 
   // but a new request should work.
   client.fetchProfile = function () {
     return Promise.resolve({ avatar: "myimg"});
   };
 
   let got = yield profile._fetchAndCacheProfile();
   do_check_eq(got.avatar, "myimg");