services/sync/modules/status.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/modules/status.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,148 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +* License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +this.EXPORTED_SYMBOLS = ["Status"];
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cr = Components.results;
    1.13 +const Cu = Components.utils;
    1.14 +
    1.15 +Cu.import("resource://services-sync/constants.js");
    1.16 +Cu.import("resource://gre/modules/Log.jsm");
    1.17 +Cu.import("resource://services-sync/identity.js");
    1.18 +Cu.import("resource://services-sync/browserid_identity.js");
    1.19 +Cu.import("resource://gre/modules/Services.jsm");
    1.20 +Cu.import("resource://services-common/async.js");
    1.21 +
    1.22 +this.Status = {
    1.23 +  _log: Log.repository.getLogger("Sync.Status"),
    1.24 +  __authManager: null,
    1.25 +  ready: false,
    1.26 +
    1.27 +  get _authManager() {
    1.28 +    if (this.__authManager) {
    1.29 +      return this.__authManager;
    1.30 +    }
    1.31 +    let service = Components.classes["@mozilla.org/weave/service;1"]
    1.32 +                    .getService(Components.interfaces.nsISupports)
    1.33 +                    .wrappedJSObject;
    1.34 +    let idClass = service.fxAccountsEnabled ? BrowserIDManager : IdentityManager;
    1.35 +    this.__authManager = new idClass();
    1.36 +    // .initialize returns a promise, so we need to spin until it resolves.
    1.37 +    let cb = Async.makeSpinningCallback();
    1.38 +    this.__authManager.initialize().then(cb, cb);
    1.39 +    cb.wait();
    1.40 +    return this.__authManager;
    1.41 +  },
    1.42 +
    1.43 +  get service() {
    1.44 +    return this._service;
    1.45 +  },
    1.46 +
    1.47 +  set service(code) {
    1.48 +    this._log.debug("Status.service: " + this._service + " => " + code);
    1.49 +    this._service = code;
    1.50 +  },
    1.51 +
    1.52 +  get login() {
    1.53 +    return this._login;
    1.54 +  },
    1.55 +
    1.56 +  set login(code) {
    1.57 +    this._log.debug("Status.login: " + this._login + " => " + code);
    1.58 +    this._login = code;
    1.59 +
    1.60 +    if (code == LOGIN_FAILED_NO_USERNAME ||
    1.61 +        code == LOGIN_FAILED_NO_PASSWORD ||
    1.62 +        code == LOGIN_FAILED_NO_PASSPHRASE) {
    1.63 +      this.service = CLIENT_NOT_CONFIGURED;
    1.64 +    } else if (code != LOGIN_SUCCEEDED) {
    1.65 +      this.service = LOGIN_FAILED;
    1.66 +    } else {
    1.67 +      this.service = STATUS_OK;
    1.68 +    }
    1.69 +  },
    1.70 +
    1.71 +  get sync() {
    1.72 +    return this._sync;
    1.73 +  },
    1.74 +
    1.75 +  set sync(code) {
    1.76 +    this._log.debug("Status.sync: " + this._sync + " => " + code);
    1.77 +    this._sync = code;
    1.78 +    this.service = code == SYNC_SUCCEEDED ? STATUS_OK : SYNC_FAILED;
    1.79 +  },
    1.80 +
    1.81 +  get eol() {
    1.82 +    let modePref = PREFS_BRANCH + "errorhandler.alert.mode";
    1.83 +    try {
    1.84 +      return Services.prefs.getCharPref(modePref) == "hard-eol";
    1.85 +    } catch (ex) {
    1.86 +      return false;
    1.87 +    }
    1.88 +  },
    1.89 +
    1.90 +  get engines() {
    1.91 +    return this._engines;
    1.92 +  },
    1.93 +
    1.94 +  set engines([name, code]) {
    1.95 +    this._log.debug("Status for engine " + name + ": " + code);
    1.96 +    this._engines[name] = code;
    1.97 +
    1.98 +    if (code != ENGINE_SUCCEEDED) {
    1.99 +      this.service = SYNC_FAILED_PARTIAL;
   1.100 +    }
   1.101 +  },
   1.102 +
   1.103 +  // Implement toString because adding a logger introduces a cyclic object
   1.104 +  // value, so we can't trivially debug-print Status as JSON.
   1.105 +  toString: function toString() {
   1.106 +    return "<Status" +
   1.107 +           ": login: "   + Status.login +
   1.108 +           ", service: " + Status.service +
   1.109 +           ", sync: "    + Status.sync + ">";
   1.110 +  },
   1.111 +
   1.112 +  checkSetup: function checkSetup() {
   1.113 +    let result = this._authManager.currentAuthState;
   1.114 +    if (result == STATUS_OK) {
   1.115 +      Status.service = result;
   1.116 +      return result;
   1.117 +    }
   1.118 +
   1.119 +    Status.login = result;
   1.120 +    return Status.service;
   1.121 +  },
   1.122 +
   1.123 +  resetBackoff: function resetBackoff() {
   1.124 +    this.enforceBackoff = false;
   1.125 +    this.backoffInterval = 0;
   1.126 +    this.minimumNextSync = 0;
   1.127 +  },
   1.128 +
   1.129 +  resetSync: function resetSync() {
   1.130 +    // Logger setup.
   1.131 +    let logPref = PREFS_BRANCH + "log.logger.status";
   1.132 +    let logLevel = "Trace";
   1.133 +    try {
   1.134 +      logLevel = Services.prefs.getCharPref(logPref);
   1.135 +    } catch (ex) {
   1.136 +      // Use default.
   1.137 +    }
   1.138 +    this._log.level = Log.Level[logLevel];
   1.139 +
   1.140 +    this._log.info("Resetting Status.");
   1.141 +    this.service = STATUS_OK;
   1.142 +    this._login = LOGIN_SUCCEEDED;
   1.143 +    this._sync = SYNC_SUCCEEDED;
   1.144 +    this._engines = {};
   1.145 +    this.partial = false;
   1.146 +  }
   1.147 +};
   1.148 +
   1.149 +// Initialize various status values.
   1.150 +Status.resetBackoff();
   1.151 +Status.resetSync();

mercurial