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