From 505e18c2a062cb51d8ba3ae453dfc10b0079e773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Jim=C3=A9nez?= Date: Fri, 13 Dec 2013 12:37:55 +0100 Subject: [PATCH] Bug 943998 - Need debug and uri pref for FirefoxAccounts. r=markh --HG-- rename : services/fxaccounts/FxAccountsConsts.js => services/fxaccounts/FxAccountsCommon.js --- b2g/components/FxAccountsMgmtService.jsm | 2 + b2g/components/FxAccountsUIGlue.js | 13 +++++-- services/fxaccounts/FxAccounts.jsm | 18 +-------- services/fxaccounts/FxAccountsClient.jsm | 8 ++++ ...xAccountsConsts.js => FxAccountsCommon.js} | 27 ++++++++++++++ services/fxaccounts/FxAccountsManager.jsm | 37 ++++++++++++++++++- services/fxaccounts/moz.build | 2 +- .../fxaccounts/tests/xpcshell/test_manager.js | 2 +- 8 files changed, 84 insertions(+), 25 deletions(-) rename services/fxaccounts/{FxAccountsConsts.js => FxAccountsCommon.js} (84%) diff --git a/b2g/components/FxAccountsMgmtService.jsm b/b2g/components/FxAccountsMgmtService.jsm index fea70f1ab41..d737fd1109b 100644 --- a/b2g/components/FxAccountsMgmtService.jsm +++ b/b2g/components/FxAccountsMgmtService.jsm @@ -25,6 +25,7 @@ const { classes: Cc, interfaces: Ci, utils: Cu } = Components; Cu.import("resource://gre/modules/Services.jsm"); Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/ObjectWrapper.jsm"); +Cu.import("resource://gre/modules/FxAccountsCommon.js"); XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsManager", "resource://gre/modules/FxAccountsManager.jsm"); @@ -66,6 +67,7 @@ this.FxAccountsMgmtService = { handleEvent: function(aEvent) { let msg = aEvent.detail; + log.debug("Got content msg " + JSON.stringify(msg)); let self = FxAccountsMgmtService; if (!msg.id) { diff --git a/b2g/components/FxAccountsUIGlue.js b/b2g/components/FxAccountsUIGlue.js index a2650237cc9..8fa0987278c 100644 --- a/b2g/components/FxAccountsUIGlue.js +++ b/b2g/components/FxAccountsUIGlue.js @@ -10,6 +10,7 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/Services.jsm"); Cu.import("resource://gre/modules/ObjectWrapper.jsm"); Cu.import("resource://gre/modules/Promise.jsm"); +Cu.import("resource://gre/modules/FxAccountsCommon.js"); XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", "@mozilla.org/uuid-generator;1", @@ -43,6 +44,8 @@ FxAccountsUIGlue.prototype = { return; } + log.debug("Got content event " + JSON.stringify(msg)); + if (msg.error) { deferred.reject(msg); } else { @@ -52,10 +55,12 @@ FxAccountsUIGlue.prototype = { onContentEvent); }); - this._browser.shell.sendCustomEvent("mozFxAccountsRPChromeEvent", { - method: "openFlow", - id: id - }); + let detail = { + method: "openFlow", + id: id + }; + log.debug("Send chrome event " + JSON.stringify(detail)); + this._browser.shell.sendCustomEvent("mozFxAccountsRPChromeEvent", detail); return deferred.promise; }, diff --git a/services/fxaccounts/FxAccounts.jsm b/services/fxaccounts/FxAccounts.jsm index c7efa8429b9..50f9e5a42f5 100644 --- a/services/fxaccounts/FxAccounts.jsm +++ b/services/fxaccounts/FxAccounts.jsm @@ -16,27 +16,11 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/Timer.jsm"); Cu.import("resource://gre/modules/Task.jsm"); Cu.import("resource://gre/modules/FxAccountsClient.jsm"); -Cu.import("resource://gre/modules/FxAccountsConsts.js"); +Cu.import("resource://gre/modules/FxAccountsCommon.js"); XPCOMUtils.defineLazyModuleGetter(this, "jwcrypto", "resource://gre/modules/identity/jwcrypto.jsm"); -// loglevel preference should be one of: "FATAL", "ERROR", "WARN", "INFO", -// "CONFIG", "DEBUG", "TRACE" or "ALL". We will be logging error messages by -// default. -const PREF_LOG_LEVEL = "identity.fxaccounts.loglevel"; -try { - this.LOG_LEVEL = - Services.prefs.getPrefType(PREF_LOG_LEVEL) == Ci.nsIPrefBranch.PREF_STRING - && Services.prefs.getCharPref(PREF_LOG_LEVEL); -} catch (e) { - this.LOG_LEVEL = Log.Level.Error; -} - -let log = Log.repository.getLogger("Services.FxAccounts"); -log.level = LOG_LEVEL; -log.addAppender(new Log.ConsoleAppender(new Log.BasicFormatter())); - InternalMethods = function(mock) { this.cert = null; this.keyPair = null; diff --git a/services/fxaccounts/FxAccountsClient.jsm b/services/fxaccounts/FxAccountsClient.jsm index 5f5df829b77..09438adbe11 100644 --- a/services/fxaccounts/FxAccountsClient.jsm +++ b/services/fxaccounts/FxAccountsClient.jsm @@ -10,6 +10,7 @@ Cu.import("resource://gre/modules/Promise.jsm"); Cu.import("resource://gre/modules/Services.jsm"); Cu.import("resource://services-common/utils.js"); Cu.import("resource://services-crypto/utils.js"); +Cu.import("resource://gre/modules/FxAccountsCommon.js"); // Default can be changed by the preference 'identity.fxaccounts.auth.uri' let _host = "https://api-accounts.dev.lcip.org/v1"; @@ -283,6 +284,9 @@ this.FxAccountsClient.prototype = { payload = JSON.stringify(jsonPayload); } + log.debug("(HAWK request) - Path: " + path + " - Method: " + method + + " - Payload: " + payload); + xhr.open(method, URI); xhr.channel.loadFlags = Ci.nsIChannel.LOAD_BYPASS_CACHE | Ci.nsIChannel.INHIBIT_CACHING; @@ -300,12 +304,16 @@ this.FxAccountsClient.prototype = { xhr.onload = function onload() { try { let response = JSON.parse(xhr.responseText); + log.debug("(Response) Code: " + xhr.status + " - Status text: " + + xhr.statusText + " - Response text: " + xhr.responseText); if (xhr.status !== 200 || response.error) { // In this case, the response is an object with error information. return deferred.reject(response); } deferred.resolve(response); } catch (e) { + log.error("(Response) Code: " + xhr.status + " - Status text: " + + xhr.statusText); deferred.reject(constructError(e)); } }; diff --git a/services/fxaccounts/FxAccountsConsts.js b/services/fxaccounts/FxAccountsCommon.js similarity index 84% rename from services/fxaccounts/FxAccountsConsts.js rename to services/fxaccounts/FxAccountsCommon.js index 51e8427b038..1c65449bcae 100644 --- a/services/fxaccounts/FxAccountsConsts.js +++ b/services/fxaccounts/FxAccountsCommon.js @@ -2,6 +2,33 @@ * 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/. */ +const { interfaces: Ci, utils: Cu } = Components; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); +Cu.import("resource://gre/modules/Services.jsm"); +Cu.import("resource://gre/modules/Log.jsm"); + +// loglevel should be one of "Fatal", "Error", "Warn", "Info", "Config", +// "Debug", "Trace" or "All". If none is specified, "Error" will be used by +// default. +const PREF_LOG_LEVEL = "identity.fxaccounts.loglevel"; + +XPCOMUtils.defineLazyGetter(this, 'log', function() { + let log = Log.repository.getLogger("FirefoxAccounts"); + log.addAppender(new Log.DumpAppender()); + log.level = Log.Level.Error; + try { + let level = + Services.prefs.getPrefType(PREF_LOG_LEVEL) == Ci.nsIPrefBranch.PREF_STRING + && Services.prefs.getCharPref(PREF_LOG_LEVEL); + log.level = Log.Level[level] || Log.Level.Error; + } catch (e) { + log.error(e); + } + + return log; +}); + this.DATA_FORMAT_VERSION = 1; this.DEFAULT_STORAGE_FILENAME = "signedInUser.json"; diff --git a/services/fxaccounts/FxAccountsManager.jsm b/services/fxaccounts/FxAccountsManager.jsm index ea45c30cbb8..cbae6cd0f95 100644 --- a/services/fxaccounts/FxAccountsManager.jsm +++ b/services/fxaccounts/FxAccountsManager.jsm @@ -19,7 +19,7 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm"); Cu.import("resource://gre/modules/Services.jsm"); Cu.import("resource://gre/modules/FxAccounts.jsm"); Cu.import("resource://gre/modules/Promise.jsm"); -Cu.import("resource://gre/modules/FxAccountsConsts.js"); +Cu.import("resource://gre/modules/FxAccountsCommon.js"); XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsClient", "resource://gre/modules/FxAccountsClient.jsm"); @@ -50,7 +50,9 @@ this.FxAccountsManager = { if (!aServerResponse || !aServerResponse.error || !aServerResponse.error.errno) { return; } - return SERVER_ERRNO_TO_ERROR[aServerResponse.error.errno]; + let error = SERVER_ERRNO_TO_ERROR[aServerResponse.error.errno]; + log.error(error); + return error; }, _serverError: function(aServerResponse) { @@ -69,18 +71,21 @@ this.FxAccountsManager = { _signInSignUp: function(aMethod, aAccountId, aPassword) { if (Services.io.offline) { + log.error(ERROR_OFFLINE); return Promise.reject({ error: ERROR_OFFLINE }); } if (!aAccountId) { + log.error(ERROR_INVALID_ACCOUNTID); return Promise.reject({ error: ERROR_INVALID_ACCOUNTID }); } if (!aPassword) { + log.error(ERROR_INVALID_PASSWORD); return Promise.reject({ error: ERROR_INVALID_PASSWORD }); @@ -88,6 +93,7 @@ this.FxAccountsManager = { // Check that there is no signed in account first. if (this._activeSession) { + log.error(ERROR_ALREADY_SIGNED_IN_USER); return Promise.reject({ error: ERROR_ALREADY_SIGNED_IN_USER, details: { @@ -100,6 +106,7 @@ this.FxAccountsManager = { return this._fxAccounts.getSignedInUser().then( user => { if (user) { + log.error(ERROR_ALREADY_SIGNED_IN_USER); return Promise.reject({ error: ERROR_ALREADY_SIGNED_IN_USER, details: { @@ -113,6 +120,7 @@ this.FxAccountsManager = { user => { let error = this._getError(user); if (!user || !user.uid || !user.sessionToken || error) { + log.error(error ? error : ERROR_INTERNAL_INVALID_USER); return Promise.reject({ error: error ? error : ERROR_INTERNAL_INVALID_USER, details: { @@ -126,6 +134,8 @@ this.FxAccountsManager = { return this._fxAccounts.setSignedInUser(user, false).then( () => { this._activeSession = user; + log.debug("User signed in: " + JSON.stringify(this._user) + + " - Account created " + (aMethod == "signUp")); return Promise.resolve({ accountCreated: aMethod === "signUp", user: this._user @@ -169,6 +179,7 @@ this.FxAccountsManager = { details: result }); } + log.debug("Signed out"); return Promise.resolve(); }, reason => { @@ -218,6 +229,7 @@ this.FxAccountsManager = { return this.verificationStatus(this._activeSession); } + log.debug("Account " + JSON.stringify(this._user)); return Promise.resolve(this._user); } @@ -225,6 +237,7 @@ this.FxAccountsManager = { return this._fxAccounts.getSignedInUser().then( user => { if (!user || !user.email) { + log.debug("No signed in account"); return Promise.resolve(null); } @@ -233,16 +246,20 @@ this.FxAccountsManager = { // we check this information with the server, update the stored // data if needed and finally return the account details. if (!user.verified && !Services.io.offline) { + log.debug("Unverified account"); return this.verificationStatus(user); } + log.debug("Account " + JSON.stringify(this._user)); return Promise.resolve(this._user); } ); }, queryAccount: function(aAccountId) { + log.debug("queryAccount " + aAccountId); if (Services.io.offline) { + log.error(ERROR_OFFLINE); return Promise.reject({ error: ERROR_OFFLINE }); @@ -251,6 +268,7 @@ this.FxAccountsManager = { let deferred = Promise.defer(); if (!aAccountId) { + log.error(ERROR_INVALID_ACCOUNTID); return Promise.reject({ error: ERROR_INVALID_ACCOUNTID }); @@ -259,6 +277,7 @@ this.FxAccountsManager = { let client = this._createFxAccountsClient(); return client.accountExists(aAccountId).then( result => { + log.debug("Account " + result ? "" : "does not" + " exists"); let error = this._getError(result); if (error) { return Promise.reject({ @@ -276,7 +295,9 @@ this.FxAccountsManager = { }, verificationStatus: function() { + log.debug("verificationStatus"); if (!this._activeSession || !this._activeSession.sessionToken) { + log.error(ERROR_NO_TOKEN_SESSION); return Promise.reject({ error: ERROR_NO_TOKEN_SESSION }); @@ -285,10 +306,12 @@ this.FxAccountsManager = { // There is no way to unverify an already verified account, so we just // return the account details of a verified account if (this._activeSession.verified) { + log.debug("Account already verified"); return Promise.resolve(this._user); } if (Services.io.offline) { + log.error(ERROR_OFFLINE); return Promise.reject({ error: ERROR_OFFLINE }); @@ -312,10 +335,12 @@ this.FxAccountsManager = { this._activeSession.verified = data.verified; return this._fxAccounts.setSignedInUser(this._activeSession).then( () => { + log.debug(JSON.stringify(this._user)); return Promise.resolve(this._user); } ); } + log.debug(JSON.stringify(this._user)); return Promise.resolve(this._user); }, reason => { return this._serverError(reason); } @@ -323,13 +348,16 @@ this.FxAccountsManager = { }, getAssertion: function(aAudience) { + log.debug("getAssertion " + aAudience); if (!aAudience) { + log.error(ERROR_INVALID_AUDIENCE); return Promise.reject({ error: ERROR_INVALID_AUDIENCE }); } if (Services.io.offline) { + log.error(ERROR_OFFLINE); return Promise.reject({ error: ERROR_OFFLINE }); @@ -343,6 +371,7 @@ this.FxAccountsManager = { return this._getAssertion(aAudience); } + log.error(ERROR_UNVERIFIED_ACCOUNT); return Promise.reject({ error: ERROR_UNVERIFIED_ACCOUNT, details: { @@ -351,6 +380,7 @@ this.FxAccountsManager = { }); } + log.debug("No signed in user"); // If there is no currently signed in user, we trigger the signIn UI // flow. let ui = Cc["@mozilla.org/fxaccounts/fxaccounts-ui-glue;1"] @@ -362,6 +392,8 @@ this.FxAccountsManager = { if (result && result.verified) { return this._getAssertion(aAudience); } + + log.error(ERROR_UNVERIFIED_ACCOUNT); return Promise.reject({ error: ERROR_UNVERIFIED_ACCOUNT, details: { @@ -370,6 +402,7 @@ this.FxAccountsManager = { }); }, error => { + log.error(ERROR_UI_ERROR + " " + error); return Promise.reject({ error: ERROR_UI_ERROR, details: error diff --git a/services/fxaccounts/moz.build b/services/fxaccounts/moz.build index 060361da2e7..a2e055171d3 100644 --- a/services/fxaccounts/moz.build +++ b/services/fxaccounts/moz.build @@ -11,7 +11,7 @@ TEST_DIRS += ['tests'] EXTRA_JS_MODULES += [ 'FxAccounts.jsm', 'FxAccountsClient.jsm', - 'FxAccountsConsts.js' + 'FxAccountsCommon.js' ] # For now, we will only be using the FxA manager in B2G. diff --git a/services/fxaccounts/tests/xpcshell/test_manager.js b/services/fxaccounts/tests/xpcshell/test_manager.js index 64ca648a637..0261f6826e9 100644 --- a/services/fxaccounts/tests/xpcshell/test_manager.js +++ b/services/fxaccounts/tests/xpcshell/test_manager.js @@ -6,7 +6,7 @@ const Cm = Components.manager; Cu.import("resource://gre/modules/FxAccounts.jsm"); -Cu.import("resource://gre/modules/FxAccountsConsts.js"); +Cu.import("resource://gre/modules/FxAccountsCommon.js"); Cu.import("resource://gre/modules/FxAccountsManager.jsm"); Cu.import("resource://gre/modules/Promise.jsm");