michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: this.EXPORTED_SYMBOLS = ["Status"]; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://services-sync/constants.js"); michael@0: Cu.import("resource://gre/modules/Log.jsm"); michael@0: Cu.import("resource://services-sync/identity.js"); michael@0: Cu.import("resource://services-sync/browserid_identity.js"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://services-common/async.js"); michael@0: michael@0: this.Status = { michael@0: _log: Log.repository.getLogger("Sync.Status"), michael@0: __authManager: null, michael@0: ready: false, michael@0: michael@0: get _authManager() { michael@0: if (this.__authManager) { michael@0: return this.__authManager; michael@0: } michael@0: let service = Components.classes["@mozilla.org/weave/service;1"] michael@0: .getService(Components.interfaces.nsISupports) michael@0: .wrappedJSObject; michael@0: let idClass = service.fxAccountsEnabled ? BrowserIDManager : IdentityManager; michael@0: this.__authManager = new idClass(); michael@0: // .initialize returns a promise, so we need to spin until it resolves. michael@0: let cb = Async.makeSpinningCallback(); michael@0: this.__authManager.initialize().then(cb, cb); michael@0: cb.wait(); michael@0: return this.__authManager; michael@0: }, michael@0: michael@0: get service() { michael@0: return this._service; michael@0: }, michael@0: michael@0: set service(code) { michael@0: this._log.debug("Status.service: " + this._service + " => " + code); michael@0: this._service = code; michael@0: }, michael@0: michael@0: get login() { michael@0: return this._login; michael@0: }, michael@0: michael@0: set login(code) { michael@0: this._log.debug("Status.login: " + this._login + " => " + code); michael@0: this._login = code; michael@0: michael@0: if (code == LOGIN_FAILED_NO_USERNAME || michael@0: code == LOGIN_FAILED_NO_PASSWORD || michael@0: code == LOGIN_FAILED_NO_PASSPHRASE) { michael@0: this.service = CLIENT_NOT_CONFIGURED; michael@0: } else if (code != LOGIN_SUCCEEDED) { michael@0: this.service = LOGIN_FAILED; michael@0: } else { michael@0: this.service = STATUS_OK; michael@0: } michael@0: }, michael@0: michael@0: get sync() { michael@0: return this._sync; michael@0: }, michael@0: michael@0: set sync(code) { michael@0: this._log.debug("Status.sync: " + this._sync + " => " + code); michael@0: this._sync = code; michael@0: this.service = code == SYNC_SUCCEEDED ? STATUS_OK : SYNC_FAILED; michael@0: }, michael@0: michael@0: get eol() { michael@0: let modePref = PREFS_BRANCH + "errorhandler.alert.mode"; michael@0: try { michael@0: return Services.prefs.getCharPref(modePref) == "hard-eol"; michael@0: } catch (ex) { michael@0: return false; michael@0: } michael@0: }, michael@0: michael@0: get engines() { michael@0: return this._engines; michael@0: }, michael@0: michael@0: set engines([name, code]) { michael@0: this._log.debug("Status for engine " + name + ": " + code); michael@0: this._engines[name] = code; michael@0: michael@0: if (code != ENGINE_SUCCEEDED) { michael@0: this.service = SYNC_FAILED_PARTIAL; michael@0: } michael@0: }, michael@0: michael@0: // Implement toString because adding a logger introduces a cyclic object michael@0: // value, so we can't trivially debug-print Status as JSON. michael@0: toString: function toString() { michael@0: return ""; michael@0: }, michael@0: michael@0: checkSetup: function checkSetup() { michael@0: let result = this._authManager.currentAuthState; michael@0: if (result == STATUS_OK) { michael@0: Status.service = result; michael@0: return result; michael@0: } michael@0: michael@0: Status.login = result; michael@0: return Status.service; michael@0: }, michael@0: michael@0: resetBackoff: function resetBackoff() { michael@0: this.enforceBackoff = false; michael@0: this.backoffInterval = 0; michael@0: this.minimumNextSync = 0; michael@0: }, michael@0: michael@0: resetSync: function resetSync() { michael@0: // Logger setup. michael@0: let logPref = PREFS_BRANCH + "log.logger.status"; michael@0: let logLevel = "Trace"; michael@0: try { michael@0: logLevel = Services.prefs.getCharPref(logPref); michael@0: } catch (ex) { michael@0: // Use default. michael@0: } michael@0: this._log.level = Log.Level[logLevel]; michael@0: michael@0: this._log.info("Resetting Status."); michael@0: this.service = STATUS_OK; michael@0: this._login = LOGIN_SUCCEEDED; michael@0: this._sync = SYNC_SUCCEEDED; michael@0: this._engines = {}; michael@0: this.partial = false; michael@0: } michael@0: }; michael@0: michael@0: // Initialize various status values. michael@0: Status.resetBackoff(); michael@0: Status.resetSync();