2009-10-07 21:47:43 +04:00
|
|
|
/***************************** BEGIN LICENSE BLOCK *****************************
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
|
|
|
* the specific language governing rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is Weave Status.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009 the Initial
|
|
|
|
* Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Edward Lee <edilee@mozilla.com> (original author)
|
2011-10-16 02:29:11 +04:00
|
|
|
* Richard Newman <rnewman@mozilla.com>
|
2009-10-07 21:47:43 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of either
|
|
|
|
* the GNU General Public License Version 2 or later (the "GPL"), or the GNU
|
|
|
|
* Lesser General Public License Version 2.1 or later (the "LGPL"), in which case
|
|
|
|
* the provisions of the GPL or the LGPL are applicable instead of those above.
|
|
|
|
* If you wish to allow use of your version of this file only under the terms of
|
|
|
|
* either the GPL or the LGPL, and not to allow others to use your version of
|
|
|
|
* this file under the terms of the MPL, indicate your decision by deleting the
|
|
|
|
* provisions above and replace them with the notice and other provisions
|
|
|
|
* required by the GPL or the LGPL. If you do not delete the provisions above, a
|
|
|
|
* recipient may use your version of this file under the terms of any one of the
|
|
|
|
* MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
****************************** END LICENSE BLOCK ******************************/
|
|
|
|
|
|
|
|
const EXPORTED_SYMBOLS = ["Status"];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2010-06-17 01:30:08 +04:00
|
|
|
Cu.import("resource://services-sync/constants.js");
|
2011-10-16 02:29:11 +04:00
|
|
|
Cu.import("resource://services-sync/log4moz.js");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2009-10-07 21:47:43 +04:00
|
|
|
|
|
|
|
let Status = {
|
2011-10-16 02:29:11 +04:00
|
|
|
_log: Log4Moz.repository.getLogger("Sync.Status"),
|
2011-02-18 00:41:13 +03:00
|
|
|
ready: false,
|
|
|
|
|
2011-10-16 02:29:11 +04:00
|
|
|
get service() {
|
|
|
|
return this._service;
|
|
|
|
},
|
|
|
|
|
|
|
|
set service(code) {
|
|
|
|
this._log.debug("Status.service: " + this._service + " => " + code);
|
|
|
|
this._service = code;
|
|
|
|
},
|
|
|
|
|
|
|
|
get login() {
|
|
|
|
return this._login;
|
|
|
|
},
|
|
|
|
|
2009-10-07 21:47:43 +04:00
|
|
|
set login(code) {
|
2011-10-16 02:29:11 +04:00
|
|
|
this._log.debug("Status.login: " + this._login + " => " + code);
|
2009-10-07 21:47:43 +04:00
|
|
|
this._login = code;
|
|
|
|
|
2010-04-22 04:40:42 +04:00
|
|
|
if (code == LOGIN_FAILED_NO_USERNAME ||
|
|
|
|
code == LOGIN_FAILED_NO_PASSWORD ||
|
2011-10-16 02:29:11 +04:00
|
|
|
code == LOGIN_FAILED_NO_PASSPHRASE) {
|
2010-04-22 04:40:42 +04:00
|
|
|
this.service = CLIENT_NOT_CONFIGURED;
|
2011-10-16 02:29:11 +04:00
|
|
|
} else if (code != LOGIN_SUCCEEDED) {
|
2009-10-07 21:47:43 +04:00
|
|
|
this.service = LOGIN_FAILED;
|
2011-10-16 02:29:11 +04:00
|
|
|
} else {
|
2009-10-07 21:47:43 +04:00
|
|
|
this.service = STATUS_OK;
|
2011-10-16 02:29:11 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get sync() {
|
|
|
|
return this._sync;
|
2009-10-07 21:47:43 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
set sync(code) {
|
2011-10-16 02:29:11 +04:00
|
|
|
this._log.debug("Status.sync: " + this._sync + " => " + code);
|
2009-10-07 21:47:43 +04:00
|
|
|
this._sync = code;
|
2010-03-26 00:24:41 +03:00
|
|
|
this.service = code == SYNC_SUCCEEDED ? STATUS_OK : SYNC_FAILED;
|
2009-10-07 21:47:43 +04:00
|
|
|
},
|
|
|
|
|
2011-10-16 02:29:11 +04:00
|
|
|
get engines() {
|
|
|
|
return this._engines;
|
|
|
|
},
|
|
|
|
|
2009-10-07 21:47:43 +04:00
|
|
|
set engines([name, code]) {
|
2011-10-16 02:29:11 +04:00
|
|
|
this._log.debug("Status for engine " + name + ": " + code);
|
2009-10-07 21:47:43 +04:00
|
|
|
this._engines[name] = code;
|
|
|
|
|
2011-10-16 02:29:11 +04:00
|
|
|
if (code != ENGINE_SUCCEEDED) {
|
2009-10-07 21:47:43 +04:00
|
|
|
this.service = SYNC_FAILED_PARTIAL;
|
2011-10-16 02:29:11 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Implement toString because adding a logger introduces a cyclic object
|
|
|
|
// value, so we can't trivially debug-print Status as JSON.
|
|
|
|
toString: function toString() {
|
|
|
|
return "<Status" +
|
|
|
|
": login: " + Status.login +
|
|
|
|
", service: " + Status.service +
|
|
|
|
", sync: " + Status.sync + ">";
|
2009-10-07 21:47:43 +04:00
|
|
|
},
|
|
|
|
|
2010-08-26 02:49:45 +04:00
|
|
|
checkSetup: function checkSetup() {
|
|
|
|
// Check whether we have a username without importing The World(tm).
|
|
|
|
let prefs = Cc["@mozilla.org/preferences-service;1"]
|
|
|
|
.getService(Ci.nsIPrefService)
|
|
|
|
.getBranch(PREFS_BRANCH);
|
|
|
|
let username;
|
|
|
|
try {
|
|
|
|
username = prefs.getCharPref("username");
|
|
|
|
} catch(ex) {}
|
2010-11-30 03:41:17 +03:00
|
|
|
|
2010-08-26 02:49:45 +04:00
|
|
|
if (!username) {
|
|
|
|
Status.login = LOGIN_FAILED_NO_USERNAME;
|
|
|
|
return Status.service;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
|
|
|
Cu.import("resource://services-sync/identity.js");
|
2011-01-19 03:23:30 +03:00
|
|
|
Cu.import("resource://services-sync/record.js");
|
2010-08-26 02:49:45 +04:00
|
|
|
if (!Utils.mpLocked()) {
|
|
|
|
let id = ID.get("WeaveID");
|
2011-10-16 02:29:11 +04:00
|
|
|
if (!id) {
|
2010-08-26 02:49:45 +04:00
|
|
|
id = ID.set("WeaveID", new Identity(PWDMGR_PASSWORD_REALM, username));
|
2011-10-16 02:29:11 +04:00
|
|
|
}
|
2010-08-26 02:49:45 +04:00
|
|
|
|
|
|
|
if (!id.password) {
|
|
|
|
Status.login = LOGIN_FAILED_NO_PASSWORD;
|
|
|
|
return Status.service;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = ID.get("WeaveCryptoID");
|
2011-10-16 02:29:11 +04:00
|
|
|
if (!id) {
|
2010-08-26 02:49:45 +04:00
|
|
|
id = ID.set("WeaveCryptoID",
|
2010-11-30 03:41:17 +03:00
|
|
|
new SyncKeyBundle(PWDMGR_PASSPHRASE_REALM, username));
|
2011-10-16 02:29:11 +04:00
|
|
|
}
|
2010-08-26 02:49:45 +04:00
|
|
|
|
2010-11-30 03:41:17 +03:00
|
|
|
if (!id.keyStr) {
|
2010-08-26 02:49:45 +04:00
|
|
|
Status.login = LOGIN_FAILED_NO_PASSPHRASE;
|
|
|
|
return Status.service;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status.service = STATUS_OK;
|
|
|
|
},
|
|
|
|
|
2009-10-07 21:47:43 +04:00
|
|
|
resetBackoff: function resetBackoff() {
|
|
|
|
this.enforceBackoff = false;
|
|
|
|
this.backoffInterval = 0;
|
|
|
|
this.minimumNextSync = 0;
|
|
|
|
},
|
2011-10-16 02:29:11 +04:00
|
|
|
|
2009-10-07 21:47:43 +04:00
|
|
|
resetSync: function resetSync() {
|
2011-10-16 02:29:11 +04:00
|
|
|
// Logger setup.
|
|
|
|
let logPref = PREFS_BRANCH + "log.logger.status";
|
|
|
|
let logLevel = "Trace";
|
|
|
|
try {
|
|
|
|
logLevel = Services.prefs.getCharPref(logPref);
|
|
|
|
} catch (ex) {
|
|
|
|
// Use default.
|
|
|
|
}
|
|
|
|
this._log.level = Log4Moz.Level[logLevel];
|
|
|
|
|
|
|
|
this._log.info("Resetting Status.");
|
2010-06-02 02:12:25 +04:00
|
|
|
this.service = STATUS_OK;
|
|
|
|
this._login = LOGIN_SUCCEEDED;
|
|
|
|
this._sync = SYNC_SUCCEEDED;
|
2009-10-07 21:47:43 +04:00
|
|
|
this._engines = {};
|
|
|
|
this.partial = false;
|
2011-10-16 02:29:11 +04:00
|
|
|
}
|
2009-10-07 21:47:43 +04:00
|
|
|
};
|
|
|
|
|
2011-10-16 02:29:11 +04:00
|
|
|
// Initialize various status values.
|
2009-10-07 21:47:43 +04:00
|
|
|
Status.resetBackoff();
|
|
|
|
Status.resetSync();
|