services/sync/modules/status.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 this.EXPORTED_SYMBOLS = ["Status"];
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cr = Components.results;
michael@0 10 const Cu = Components.utils;
michael@0 11
michael@0 12 Cu.import("resource://services-sync/constants.js");
michael@0 13 Cu.import("resource://gre/modules/Log.jsm");
michael@0 14 Cu.import("resource://services-sync/identity.js");
michael@0 15 Cu.import("resource://services-sync/browserid_identity.js");
michael@0 16 Cu.import("resource://gre/modules/Services.jsm");
michael@0 17 Cu.import("resource://services-common/async.js");
michael@0 18
michael@0 19 this.Status = {
michael@0 20 _log: Log.repository.getLogger("Sync.Status"),
michael@0 21 __authManager: null,
michael@0 22 ready: false,
michael@0 23
michael@0 24 get _authManager() {
michael@0 25 if (this.__authManager) {
michael@0 26 return this.__authManager;
michael@0 27 }
michael@0 28 let service = Components.classes["@mozilla.org/weave/service;1"]
michael@0 29 .getService(Components.interfaces.nsISupports)
michael@0 30 .wrappedJSObject;
michael@0 31 let idClass = service.fxAccountsEnabled ? BrowserIDManager : IdentityManager;
michael@0 32 this.__authManager = new idClass();
michael@0 33 // .initialize returns a promise, so we need to spin until it resolves.
michael@0 34 let cb = Async.makeSpinningCallback();
michael@0 35 this.__authManager.initialize().then(cb, cb);
michael@0 36 cb.wait();
michael@0 37 return this.__authManager;
michael@0 38 },
michael@0 39
michael@0 40 get service() {
michael@0 41 return this._service;
michael@0 42 },
michael@0 43
michael@0 44 set service(code) {
michael@0 45 this._log.debug("Status.service: " + this._service + " => " + code);
michael@0 46 this._service = code;
michael@0 47 },
michael@0 48
michael@0 49 get login() {
michael@0 50 return this._login;
michael@0 51 },
michael@0 52
michael@0 53 set login(code) {
michael@0 54 this._log.debug("Status.login: " + this._login + " => " + code);
michael@0 55 this._login = code;
michael@0 56
michael@0 57 if (code == LOGIN_FAILED_NO_USERNAME ||
michael@0 58 code == LOGIN_FAILED_NO_PASSWORD ||
michael@0 59 code == LOGIN_FAILED_NO_PASSPHRASE) {
michael@0 60 this.service = CLIENT_NOT_CONFIGURED;
michael@0 61 } else if (code != LOGIN_SUCCEEDED) {
michael@0 62 this.service = LOGIN_FAILED;
michael@0 63 } else {
michael@0 64 this.service = STATUS_OK;
michael@0 65 }
michael@0 66 },
michael@0 67
michael@0 68 get sync() {
michael@0 69 return this._sync;
michael@0 70 },
michael@0 71
michael@0 72 set sync(code) {
michael@0 73 this._log.debug("Status.sync: " + this._sync + " => " + code);
michael@0 74 this._sync = code;
michael@0 75 this.service = code == SYNC_SUCCEEDED ? STATUS_OK : SYNC_FAILED;
michael@0 76 },
michael@0 77
michael@0 78 get eol() {
michael@0 79 let modePref = PREFS_BRANCH + "errorhandler.alert.mode";
michael@0 80 try {
michael@0 81 return Services.prefs.getCharPref(modePref) == "hard-eol";
michael@0 82 } catch (ex) {
michael@0 83 return false;
michael@0 84 }
michael@0 85 },
michael@0 86
michael@0 87 get engines() {
michael@0 88 return this._engines;
michael@0 89 },
michael@0 90
michael@0 91 set engines([name, code]) {
michael@0 92 this._log.debug("Status for engine " + name + ": " + code);
michael@0 93 this._engines[name] = code;
michael@0 94
michael@0 95 if (code != ENGINE_SUCCEEDED) {
michael@0 96 this.service = SYNC_FAILED_PARTIAL;
michael@0 97 }
michael@0 98 },
michael@0 99
michael@0 100 // Implement toString because adding a logger introduces a cyclic object
michael@0 101 // value, so we can't trivially debug-print Status as JSON.
michael@0 102 toString: function toString() {
michael@0 103 return "<Status" +
michael@0 104 ": login: " + Status.login +
michael@0 105 ", service: " + Status.service +
michael@0 106 ", sync: " + Status.sync + ">";
michael@0 107 },
michael@0 108
michael@0 109 checkSetup: function checkSetup() {
michael@0 110 let result = this._authManager.currentAuthState;
michael@0 111 if (result == STATUS_OK) {
michael@0 112 Status.service = result;
michael@0 113 return result;
michael@0 114 }
michael@0 115
michael@0 116 Status.login = result;
michael@0 117 return Status.service;
michael@0 118 },
michael@0 119
michael@0 120 resetBackoff: function resetBackoff() {
michael@0 121 this.enforceBackoff = false;
michael@0 122 this.backoffInterval = 0;
michael@0 123 this.minimumNextSync = 0;
michael@0 124 },
michael@0 125
michael@0 126 resetSync: function resetSync() {
michael@0 127 // Logger setup.
michael@0 128 let logPref = PREFS_BRANCH + "log.logger.status";
michael@0 129 let logLevel = "Trace";
michael@0 130 try {
michael@0 131 logLevel = Services.prefs.getCharPref(logPref);
michael@0 132 } catch (ex) {
michael@0 133 // Use default.
michael@0 134 }
michael@0 135 this._log.level = Log.Level[logLevel];
michael@0 136
michael@0 137 this._log.info("Resetting Status.");
michael@0 138 this.service = STATUS_OK;
michael@0 139 this._login = LOGIN_SUCCEEDED;
michael@0 140 this._sync = SYNC_SUCCEEDED;
michael@0 141 this._engines = {};
michael@0 142 this.partial = false;
michael@0 143 }
michael@0 144 };
michael@0 145
michael@0 146 // Initialize various status values.
michael@0 147 Status.resetBackoff();
michael@0 148 Status.resetSync();

mercurial