2007-12-10 21:38:53 -08: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 Bookmarks Sync.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
2008-06-12 12:36:58 -07:00
|
|
|
* Myk Melez <myk@mozilla.org>
|
2007-12-10 21:38:53 -08: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 ***** */
|
|
|
|
|
2008-03-25 15:14:00 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['Weave'];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2008-06-11 10:38:25 -07:00
|
|
|
// The following constants determine when Weave will automatically sync data.
|
|
|
|
|
|
|
|
// An interval of one minute, initial threshold of 100, and step of 5 means
|
|
|
|
// that we'll try to sync each engine 21 times, once per minute, at
|
|
|
|
// consecutively lower thresholds (from 100 down to 5 in steps of 5 and then
|
|
|
|
// one more time with the threshold set to the minimum 1) before resetting
|
|
|
|
// the engine's threshold to the initial value and repeating the cycle
|
|
|
|
// until at some point the engine's score exceeds the threshold, at which point
|
|
|
|
// we'll sync it, reset its threshold to the initial value, rinse, and repeat.
|
|
|
|
|
|
|
|
// How long we wait between sync checks.
|
2008-07-08 21:24:59 -07:00
|
|
|
const SCHEDULED_SYNC_INTERVAL = 60 * 1000 * 5; // five minutes
|
2008-06-11 10:38:25 -07:00
|
|
|
|
|
|
|
// INITIAL_THRESHOLD represents the value an engine's score has to exceed
|
|
|
|
// in order for us to sync it the first time we start up (and the first time
|
|
|
|
// we do a sync check after having synced the engine or reset the threshold).
|
2008-07-08 21:24:59 -07:00
|
|
|
const INITIAL_THRESHOLD = 75;
|
2008-06-11 10:38:25 -07:00
|
|
|
|
|
|
|
// THRESHOLD_DECREMENT_STEP is the amount by which we decrement an engine's
|
|
|
|
// threshold each time we do a sync check and don't sync that engine.
|
2008-07-08 21:24:59 -07:00
|
|
|
const THRESHOLD_DECREMENT_STEP = 25;
|
2008-06-11 10:38:25 -07:00
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2007-12-10 21:38:53 -08:00
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
Cu.import("resource://weave/constants.js");
|
|
|
|
Cu.import("resource://weave/util.js");
|
2008-03-27 19:12:53 -07:00
|
|
|
Cu.import("resource://weave/wrap.js");
|
2008-07-11 14:47:15 -07:00
|
|
|
Cu.import("resource://weave/faultTolerance.js");
|
2008-11-06 17:37:17 -08:00
|
|
|
Cu.import("resource://weave/auth.js");
|
|
|
|
Cu.import("resource://weave/resource.js");
|
2009-02-11 23:54:20 -08:00
|
|
|
Cu.import("resource://weave/base_records/wbo.js");
|
2009-02-19 04:09:55 -08:00
|
|
|
Cu.import("resource://weave/base_records/crypto.js");
|
2008-11-06 17:37:17 -08:00
|
|
|
Cu.import("resource://weave/base_records/keys.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
Cu.import("resource://weave/engines.js");
|
2008-08-04 17:34:21 -07:00
|
|
|
Cu.import("resource://weave/oauth.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
Cu.import("resource://weave/identity.js");
|
2008-03-07 01:56:36 -08:00
|
|
|
Cu.import("resource://weave/async.js");
|
2009-01-06 13:54:18 -08:00
|
|
|
Cu.import("resource://weave/engines/clientData.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
Function.prototype.async = Async.sugar;
|
2008-03-25 15:14:00 -07:00
|
|
|
|
|
|
|
// for export
|
|
|
|
let Weave = {};
|
2008-03-31 07:20:09 -07:00
|
|
|
Cu.import("resource://weave/constants.js", Weave);
|
|
|
|
Cu.import("resource://weave/util.js", Weave);
|
|
|
|
Cu.import("resource://weave/async.js", Weave);
|
2008-07-11 14:47:15 -07:00
|
|
|
Cu.import("resource://weave/faultTolerance.js", Weave);
|
2008-11-06 17:37:17 -08:00
|
|
|
Cu.import("resource://weave/auth.js", Weave);
|
|
|
|
Cu.import("resource://weave/resource.js", Weave);
|
|
|
|
Cu.import("resource://weave/base_records/keys.js", Weave);
|
2008-06-25 13:51:39 -07:00
|
|
|
Cu.import("resource://weave/notifications.js", Weave);
|
2008-03-31 07:20:09 -07:00
|
|
|
Cu.import("resource://weave/identity.js", Weave);
|
|
|
|
Cu.import("resource://weave/stores.js", Weave);
|
|
|
|
Cu.import("resource://weave/engines.js", Weave);
|
2008-08-04 17:34:21 -07:00
|
|
|
Cu.import("resource://weave/oauth.js", Weave);
|
2009-01-06 13:54:18 -08:00
|
|
|
Cu.import("resource://weave/engines/clientData.js", Weave);
|
2008-07-21 18:12:27 -07:00
|
|
|
|
2008-03-25 15:14:00 -07:00
|
|
|
Utils.lazy(Weave, 'Service', WeaveSvc);
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2009-03-24 19:23:53 -07:00
|
|
|
/*
|
|
|
|
* Service status query system. See constants defined in constants.js.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function StatusRecord() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
StatusRecord.prototype = {
|
|
|
|
_init: function() {
|
|
|
|
this.server = [];
|
|
|
|
this.sync = null;
|
|
|
|
this.engines = {};
|
|
|
|
},
|
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
addServerStatus: function(statusCode) {
|
2009-03-24 19:23:53 -07:00
|
|
|
this.server.push(statusCode);
|
|
|
|
},
|
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
setSyncStatus: function(statusCode) {
|
2009-03-24 19:23:53 -07:00
|
|
|
this.sync = statusCode;
|
|
|
|
},
|
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
setEngineStatus: function(engineName, statusCode) {
|
2009-03-24 19:23:53 -07:00
|
|
|
this.engines[engineName] = statusCode;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
/*
|
|
|
|
* Service singleton
|
|
|
|
* Main entry point into Weave's sync framework
|
|
|
|
*/
|
|
|
|
|
2009-02-22 00:04:58 -08:00
|
|
|
function WeaveSvc() {
|
|
|
|
this._notify = Wrap.notify("weave:service:");
|
|
|
|
}
|
2008-03-25 15:14:00 -07:00
|
|
|
WeaveSvc.prototype = {
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-27 19:12:53 -07:00
|
|
|
_localLock: Wrap.localLock,
|
2008-07-11 14:47:15 -07:00
|
|
|
_catchAll: Wrap.catchAll,
|
2008-07-09 17:17:24 -07:00
|
|
|
_isQuitting: false,
|
2008-06-12 12:36:58 -07:00
|
|
|
_loggedIn: false,
|
2008-06-30 14:00:06 -07:00
|
|
|
_syncInProgress: false,
|
2008-12-21 14:41:17 -08:00
|
|
|
_keyGenEnabled: true,
|
2009-03-24 19:23:53 -07:00
|
|
|
|
|
|
|
// WEAVE_STATUS_OK, WEAVE_STATUS_FAILED, or WEAVE_STATUS_PARTIAL
|
|
|
|
_weaveStatusCode: null,
|
|
|
|
// More detailed status info:
|
|
|
|
_detailedStatus: null,
|
2008-02-13 14:30:44 -08:00
|
|
|
|
2008-07-08 18:37:14 -07:00
|
|
|
// object for caching public and private keys
|
|
|
|
_keyPair: {},
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
// Timer object for automagically syncing
|
2009-03-10 04:15:52 -07:00
|
|
|
_syncTimer: null,
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
get username() {
|
2009-02-26 00:47:30 -08:00
|
|
|
return Svc.Prefs.get("username", "");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
set username(value) {
|
2007-12-20 12:18:41 -08:00
|
|
|
if (value)
|
2009-02-10 00:56:42 -08:00
|
|
|
Svc.Prefs.set("username", value);
|
2007-12-20 12:18:41 -08:00
|
|
|
else
|
2009-02-10 00:56:42 -08:00
|
|
|
Svc.Prefs.reset("username");
|
2007-12-20 12:18:41 -08:00
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
// fixme - need to loop over all Identity objects - needs some rethinking...
|
2008-04-14 18:53:35 -07:00
|
|
|
ID.get('WeaveID').username = value;
|
|
|
|
ID.get('WeaveCryptoID').username = value;
|
2008-12-01 18:43:43 -08:00
|
|
|
|
2008-12-01 18:58:28 -08:00
|
|
|
// FIXME: need to also call this whenever the username pref changes
|
2008-12-01 18:43:43 -08:00
|
|
|
this._genKeyURLs();
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-04-14 18:53:35 -07:00
|
|
|
get password() { return ID.get('WeaveID').password; },
|
|
|
|
set password(value) { ID.get('WeaveID').password = value; },
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-04-14 18:53:35 -07:00
|
|
|
get passphrase() { return ID.get('WeaveCryptoID').password; },
|
|
|
|
set passphrase(value) { ID.get('WeaveCryptoID').password = value; },
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-11-19 16:18:35 -08:00
|
|
|
// chrome-provided callbacks for when the service needs a password/passphrase
|
|
|
|
set onGetPassword(value) {
|
|
|
|
ID.get('WeaveID').onGetPassword = value;
|
|
|
|
},
|
|
|
|
set onGetPassphrase(value) {
|
|
|
|
ID.get('WeaveCryptoID').onGetPassword = value;
|
|
|
|
},
|
|
|
|
|
2008-11-06 17:37:17 -08:00
|
|
|
get baseURL() {
|
2009-02-10 00:56:42 -08:00
|
|
|
let url = Svc.Prefs.get("serverURL");
|
|
|
|
if (!url)
|
|
|
|
throw "No server URL set";
|
|
|
|
if (url[url.length-1] != '/')
|
2008-12-23 13:51:30 -08:00
|
|
|
url += '/';
|
2009-02-10 00:56:42 -08:00
|
|
|
url += "0.3/";
|
2008-11-06 17:37:17 -08:00
|
|
|
return url;
|
|
|
|
},
|
|
|
|
set baseURL(value) {
|
2009-02-10 00:56:42 -08:00
|
|
|
Svc.Prefs.set("serverURL", value);
|
|
|
|
},
|
|
|
|
|
|
|
|
get clusterURL() {
|
|
|
|
let url = Svc.Prefs.get("clusterURL");
|
|
|
|
if (!url)
|
|
|
|
return null;
|
|
|
|
if (url[url.length-1] != '/')
|
|
|
|
url += '/';
|
|
|
|
url += "0.3/user/";
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
set clusterURL(value) {
|
|
|
|
Svc.Prefs.set("clusterURL", value);
|
2008-12-01 18:43:43 -08:00
|
|
|
this._genKeyURLs();
|
2008-11-06 17:37:17 -08:00
|
|
|
},
|
|
|
|
|
2008-06-06 14:18:50 -07:00
|
|
|
get userPath() { return ID.get('WeaveID').username; },
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2009-02-11 22:08:56 -08:00
|
|
|
get isLoggedIn() { return this._loggedIn; },
|
2008-07-09 17:17:24 -07:00
|
|
|
|
2009-02-11 22:08:56 -08:00
|
|
|
get isQuitting() { return this._isQuitting; },
|
2008-07-09 17:17:24 -07:00
|
|
|
set isQuitting(value) { this._isQuitting = value; },
|
|
|
|
|
2009-02-11 22:08:56 -08:00
|
|
|
get cancelRequested() { return Engines.cancelRequested; },
|
|
|
|
set cancelRequested(value) { Engines.cancelRequested = value; },
|
2008-07-09 17:17:24 -07:00
|
|
|
|
2009-02-11 22:08:56 -08:00
|
|
|
get keyGenEnabled() { return this._keyGenEnabled; },
|
2008-12-21 14:41:17 -08:00
|
|
|
set keyGenEnabled(value) { this._keyGenEnabled = value; },
|
|
|
|
|
2009-02-11 22:08:56 -08:00
|
|
|
get enabled() { return Svc.Prefs.get("enabled"); },
|
|
|
|
set enabled(value) { Svc.Prefs.set("enabled", value); },
|
2008-02-13 16:07:11 -08:00
|
|
|
|
2009-03-24 19:23:53 -07:00
|
|
|
get statusCode() { return this._weaveStatusCode; },
|
|
|
|
get detailedStatus() { return this._detailedStatus; },
|
2009-03-04 15:49:58 -08:00
|
|
|
|
2009-02-11 22:08:56 -08:00
|
|
|
get locked() { return this._locked; },
|
2008-11-03 14:59:45 -08:00
|
|
|
lock: function Svc_lock() {
|
|
|
|
if (this._locked)
|
|
|
|
return false;
|
|
|
|
this._locked = true;
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
unlock: function Svc_unlock() {
|
|
|
|
this._locked = false;
|
2008-05-23 12:08:03 -07:00
|
|
|
},
|
|
|
|
|
2009-03-24 19:23:53 -07:00
|
|
|
_setSyncFailure: function WeavSvc__setSyncFailure(code) {
|
|
|
|
this._weaveStatusCode = WEAVE_STATUS_FAILED;
|
|
|
|
this._detailedStatus.setSyncStatus(code);
|
|
|
|
},
|
|
|
|
|
2008-12-01 18:43:43 -08:00
|
|
|
_genKeyURLs: function WeaveSvc__genKeyURLs() {
|
2009-02-10 00:56:42 -08:00
|
|
|
let url = this.clusterURL + this.username;
|
2008-12-01 18:43:43 -08:00
|
|
|
PubKeys.defaultKeyUri = url + "/keys/pubkey";
|
|
|
|
PrivKeys.defaultKeyUri = url + "/keys/privkey";
|
|
|
|
},
|
|
|
|
|
2009-02-10 00:56:42 -08:00
|
|
|
_checkCrypto: function WeaveSvc__checkCrypto() {
|
|
|
|
let ok = false;
|
|
|
|
|
|
|
|
try {
|
2009-02-11 13:38:50 -08:00
|
|
|
let iv = Svc.Crypto.generateRandomIV();
|
2009-02-10 00:56:42 -08:00
|
|
|
if (iv.length == 24)
|
2009-03-12 08:34:12 -07:00
|
|
|
ok = true;
|
2009-02-10 00:56:42 -08:00
|
|
|
|
2009-02-11 13:38:50 -08:00
|
|
|
} catch (e) {
|
|
|
|
this._log.debug("Crypto check failed: " + e);
|
|
|
|
}
|
2009-02-10 00:56:42 -08:00
|
|
|
|
|
|
|
return ok;
|
|
|
|
},
|
|
|
|
|
2009-03-12 13:41:46 -07:00
|
|
|
onWindowOpened: function WeaveSvc__onWindowOpened() {
|
2009-02-10 00:56:42 -08:00
|
|
|
},
|
|
|
|
|
2008-11-06 17:37:17 -08:00
|
|
|
// one-time initialization like setting up observers and the like
|
|
|
|
// xxx we might need to split some of this out into something we can call
|
|
|
|
// again when username/server/etc changes
|
|
|
|
_onStartup: function WeaveSvc__onStartup() {
|
|
|
|
let self = yield;
|
|
|
|
this._initLogs();
|
2009-02-10 00:56:42 -08:00
|
|
|
this._log.info("Weave " + WEAVE_VERSION + " initializing");
|
|
|
|
|
2009-03-24 19:23:53 -07:00
|
|
|
this._detailedStatus = new StatusRecord();
|
|
|
|
|
2009-02-26 22:36:14 -08:00
|
|
|
// Reset our sync id if we're upgrading, so sync knows to reset local data
|
2009-03-02 14:15:46 -08:00
|
|
|
if (WEAVE_VERSION != Svc.Prefs.get("lastversion")) {
|
2009-02-26 22:36:14 -08:00
|
|
|
this._log.info("Resetting client syncID from _onStartup.");
|
|
|
|
Clients.resetSyncID();
|
2009-03-02 14:15:46 -08:00
|
|
|
}
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2009-02-10 00:56:42 -08:00
|
|
|
let ua = Cc["@mozilla.org/network/protocol;1?name=http"].
|
|
|
|
getService(Ci.nsIHttpProtocolHandler).userAgent;
|
|
|
|
this._log.info(ua);
|
|
|
|
|
|
|
|
if (!this._checkCrypto()) {
|
|
|
|
this.enabled = false;
|
|
|
|
this._log.error("Could not load the Weave crypto component. Disabling " +
|
2009-03-12 08:34:12 -07:00
|
|
|
"Weave, since it will not work correctly.");
|
2009-02-10 00:56:42 -08:00
|
|
|
}
|
2008-11-06 17:37:17 -08:00
|
|
|
|
|
|
|
Utils.prefs.addObserver("", this, false);
|
2009-03-10 04:30:36 -07:00
|
|
|
Svc.Observer.addObserver(this, "network:offline-status-changed", true);
|
|
|
|
Svc.Observer.addObserver(this, "private-browsing", true);
|
2009-03-10 04:30:30 -07:00
|
|
|
Svc.Observer.addObserver(this, "quit-application", true);
|
2008-11-06 17:37:17 -08:00
|
|
|
FaultTolerance.Service; // initialize FT service
|
|
|
|
|
|
|
|
if (!this.enabled)
|
|
|
|
this._log.info("Weave Sync disabled");
|
|
|
|
|
|
|
|
// Create Weave identities (for logging in, and for encryption)
|
|
|
|
ID.set('WeaveID', new Identity('Mozilla Services Password', this.username));
|
|
|
|
Auth.defaultAuthenticator = new BasicAuthenticator(ID.get('WeaveID'));
|
|
|
|
|
|
|
|
ID.set('WeaveCryptoID',
|
|
|
|
new Identity('Mozilla Services Encryption Passphrase', this.username));
|
|
|
|
|
2008-12-01 18:43:43 -08:00
|
|
|
this._genKeyURLs();
|
2008-11-06 17:37:17 -08:00
|
|
|
|
2009-02-10 00:56:42 -08:00
|
|
|
if (Svc.Prefs.get("autoconnect") && this.username) {
|
2008-11-06 17:37:17 -08:00
|
|
|
try {
|
2009-03-12 08:34:12 -07:00
|
|
|
if (yield this.login(self.cb))
|
|
|
|
yield this.sync(self.cb, true);
|
2008-11-06 17:37:17 -08:00
|
|
|
} catch (e) {}
|
2009-02-10 00:56:42 -08:00
|
|
|
}
|
2008-11-06 17:37:17 -08:00
|
|
|
self.done();
|
|
|
|
},
|
2009-01-14 11:23:08 -08:00
|
|
|
onStartup: function WeaveSvc_onStartup(callback) {
|
|
|
|
this._onStartup.async(this, callback);
|
2008-11-06 17:37:17 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_initLogs: function WeaveSvc__initLogs() {
|
2008-11-03 14:59:45 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger("Service.Main");
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.level =
|
2009-02-10 00:56:42 -08:00
|
|
|
Log4Moz.Level[Svc.Prefs.get("log.logger.service.main")];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-05-29 18:15:50 -07:00
|
|
|
let formatter = new Log4Moz.BasicFormatter();
|
2008-11-03 14:59:45 -08:00
|
|
|
let root = Log4Moz.repository.rootLogger;
|
2009-02-10 00:56:42 -08:00
|
|
|
root.level = Log4Moz.Level[Svc.Prefs.get("log.rootLogger")];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-05-29 18:15:50 -07:00
|
|
|
let capp = new Log4Moz.ConsoleAppender(formatter);
|
2009-02-10 00:56:42 -08:00
|
|
|
capp.level = Log4Moz.Level[Svc.Prefs.get("log.appender.console")];
|
2007-12-10 21:38:53 -08:00
|
|
|
root.addAppender(capp);
|
|
|
|
|
2008-05-29 18:15:50 -07:00
|
|
|
let dapp = new Log4Moz.DumpAppender(formatter);
|
2009-02-10 00:56:42 -08:00
|
|
|
dapp.level = Log4Moz.Level[Svc.Prefs.get("log.appender.dump")];
|
2007-12-10 21:38:53 -08:00
|
|
|
root.addAppender(dapp);
|
|
|
|
|
2009-03-10 04:30:30 -07:00
|
|
|
let brief = Svc.Directory.get("ProfD", Ci.nsIFile);
|
2007-12-19 19:49:28 -08:00
|
|
|
brief.QueryInterface(Ci.nsILocalFile);
|
|
|
|
brief.append("weave");
|
|
|
|
brief.append("logs");
|
|
|
|
brief.append("brief-log.txt");
|
|
|
|
if (!brief.exists())
|
|
|
|
brief.create(brief.NORMAL_FILE_TYPE, PERMS_FILE);
|
|
|
|
|
|
|
|
let verbose = brief.parent.clone();
|
|
|
|
verbose.append("verbose-log.txt");
|
|
|
|
if (!verbose.exists())
|
|
|
|
verbose.create(verbose.NORMAL_FILE_TYPE, PERMS_FILE);
|
|
|
|
|
2008-05-29 18:15:50 -07:00
|
|
|
this._briefApp = new Log4Moz.RotatingFileAppender(brief, formatter);
|
2009-02-10 00:56:42 -08:00
|
|
|
this._briefApp.level = Log4Moz.Level[Svc.Prefs.get("log.appender.briefLog")];
|
2008-03-28 19:36:11 -07:00
|
|
|
root.addAppender(this._briefApp);
|
2008-05-29 18:15:50 -07:00
|
|
|
this._debugApp = new Log4Moz.RotatingFileAppender(verbose, formatter);
|
2009-02-10 00:56:42 -08:00
|
|
|
this._debugApp.level = Log4Moz.Level[Svc.Prefs.get("log.appender.debugLog")];
|
2008-03-28 19:36:11 -07:00
|
|
|
root.addAppender(this._debugApp);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-03-28 19:36:11 -07:00
|
|
|
clearLogs: function WeaveSvc_clearLogs() {
|
|
|
|
this._briefApp.clear();
|
|
|
|
this._debugApp.clear();
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-23 16:10:31 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
observe: function WeaveSvc__observe(subject, topic, data) {
|
2008-06-23 16:10:31 -07:00
|
|
|
switch (topic) {
|
|
|
|
case "nsPref:changed":
|
|
|
|
switch (data) {
|
2009-03-10 04:15:52 -07:00
|
|
|
case "enabled":
|
2008-06-23 16:10:31 -07:00
|
|
|
case "schedule":
|
2009-03-10 04:15:52 -07:00
|
|
|
// Potentially we'll want to reschedule syncs
|
|
|
|
this._checkSync();
|
2008-06-23 16:10:31 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2009-03-10 04:30:36 -07:00
|
|
|
case "network:offline-status-changed":
|
|
|
|
// Whether online or offline, we'll reschedule syncs
|
|
|
|
this._log.debug("Network offline status change: " + data);
|
|
|
|
this._checkSync();
|
|
|
|
break;
|
|
|
|
case "private-browsing":
|
|
|
|
// Entering or exiting private browsing? Reschedule syncs
|
|
|
|
this._log.debug("Private browsing change: " + data);
|
|
|
|
this._checkSync();
|
|
|
|
break;
|
2008-06-23 16:10:31 -07:00
|
|
|
case "quit-application":
|
|
|
|
this._onQuitApplication();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
_onQuitApplication: function WeaveSvc__onQuitApplication() {
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// These are global (for all engines)
|
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
// gets cluster from central LDAP server and returns it, or null on error
|
2009-02-10 00:56:42 -08:00
|
|
|
findCluster: function WeaveSvc_findCluster(onComplete, username) {
|
|
|
|
let fn = function WeaveSvc__findCluster() {
|
|
|
|
let self = yield;
|
2009-03-27 00:46:10 -07:00
|
|
|
let ret = null;
|
2009-02-10 15:56:37 -08:00
|
|
|
|
2009-02-10 00:56:42 -08:00
|
|
|
this._log.debug("Finding cluster for user " + username);
|
2009-02-10 17:57:29 -08:00
|
|
|
|
|
|
|
let res = new Resource(this.baseURL + "api/register/chknode/" + username);
|
2009-02-10 17:50:40 -08:00
|
|
|
try {
|
2009-03-12 08:34:12 -07:00
|
|
|
yield res.get(self.cb);
|
2009-02-10 17:50:40 -08:00
|
|
|
} catch (e) { /* we check status below */ }
|
2009-02-10 15:56:37 -08:00
|
|
|
|
|
|
|
if (res.lastChannel.responseStatus == 404) {
|
|
|
|
this._log.debug("Using serverURL as data cluster (multi-cluster support disabled)");
|
2009-03-27 00:46:10 -07:00
|
|
|
ret = Svc.Prefs.get("serverURL");
|
2009-02-10 15:56:37 -08:00
|
|
|
} else if (res.lastChannel.responseStatus == 200) {
|
2009-02-26 20:52:56 -08:00
|
|
|
// XXX Bug 480480 Work around the server sending a trailing newline
|
2009-03-27 00:46:10 -07:00
|
|
|
ret = 'https://' + res.data.trim() + '/';
|
2009-02-10 00:56:42 -08:00
|
|
|
}
|
2009-03-27 00:46:10 -07:00
|
|
|
|
2009-02-10 15:56:37 -08:00
|
|
|
self.done(ret);
|
2009-02-10 00:56:42 -08:00
|
|
|
};
|
|
|
|
fn.async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
// gets cluster from central LDAP server and sets this.clusterURL
|
|
|
|
setCluster: function WeaveSvc_setCluster(onComplete, username) {
|
|
|
|
let fn = function WeaveSvc__setCluster() {
|
|
|
|
let self = yield;
|
|
|
|
let ret = false;
|
2008-12-27 12:15:45 -08:00
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
let cluster = yield this.findCluster(self.cb, username);
|
|
|
|
if (cluster) {
|
|
|
|
this._log.debug("Saving cluster setting");
|
|
|
|
this.clusterURL = cluster;
|
|
|
|
ret = true;
|
|
|
|
} else
|
|
|
|
this._log.debug("Error setting cluster for user " + username);
|
|
|
|
|
|
|
|
self.done(ret);
|
|
|
|
};
|
|
|
|
fn.async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
|
|
|
verifyLogin: function WeaveSvc_verifyLogin(onComplete, username, password, isLogin) {
|
2008-12-27 12:15:45 -08:00
|
|
|
let fn = function WeaveSvc__verifyLogin() {
|
|
|
|
let self = yield;
|
2009-03-27 00:46:10 -07:00
|
|
|
this._log.debug("Verifying login for user " + username);
|
2009-02-10 00:56:42 -08:00
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
let url = yield this.findCluster(self.cb, username);
|
|
|
|
if (isLogin)
|
|
|
|
this.clusterURL = url;
|
2009-02-10 00:56:42 -08:00
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
if (url[url.length-1] != '/')
|
|
|
|
url += '/';
|
|
|
|
url += "0.3/user/";
|
2009-02-10 00:56:42 -08:00
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
let res = new Resource(url + username);
|
|
|
|
res.authenticator = {
|
|
|
|
onRequest: function(headers) {
|
|
|
|
headers['Authorization'] = 'Basic ' + btoa(username + ':' + password);
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
yield res.get(self.cb);
|
2009-02-10 00:56:42 -08:00
|
|
|
|
|
|
|
//Svc.Json.decode(res.data); // throws if not json
|
2009-01-24 16:49:23 -08:00
|
|
|
self.done(true);
|
2008-12-27 12:15:45 -08:00
|
|
|
};
|
2009-02-11 22:08:56 -08:00
|
|
|
this._catchAll(this._notify("verify-login", "", fn)).async(this, onComplete);
|
2008-06-30 16:50:19 -07:00
|
|
|
},
|
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
verifyPassphrase: function WeaveSvc_verifyPassphrase(onComplete, username,
|
|
|
|
password, passphrase) {
|
|
|
|
let fn = function WeaveSvc__verifyPassphrase() {
|
|
|
|
let self = yield;
|
2008-06-30 16:50:19 -07:00
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
this._log.debug("Verifying passphrase");
|
2008-06-30 16:50:19 -07:00
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
this.username = username;
|
|
|
|
ID.get('WeaveID').setTempPassword(password);
|
2008-12-19 15:48:40 -08:00
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
let id = new Identity('Passphrase Verification', username);
|
|
|
|
id.setTempPassword(passphrase);
|
2008-07-12 20:31:27 -07:00
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
let pubkey = yield PubKeys.getDefaultKey(self.cb);
|
|
|
|
let privkey = yield PrivKeys.get(self.cb, pubkey.PrivKeyUri);
|
2008-07-11 17:40:52 -07:00
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
// fixme: decrypt something here
|
|
|
|
};
|
|
|
|
this._catchAll(
|
|
|
|
this._localLock(
|
|
|
|
this._notify("verify-passphrase", "", fn))).async(this, onComplete);
|
2008-06-25 00:22:53 -07:00
|
|
|
},
|
2008-07-11 17:40:52 -07:00
|
|
|
|
2008-12-27 12:15:45 -08:00
|
|
|
login: function WeaveSvc_login(onComplete, username, password, passphrase) {
|
|
|
|
let user = username, pass = password, passp = passphrase;
|
2008-06-25 00:22:53 -07:00
|
|
|
|
2008-12-27 12:15:45 -08:00
|
|
|
let fn = function WeaveSvc__login() {
|
|
|
|
let self = yield;
|
2008-07-11 17:40:52 -07:00
|
|
|
|
2008-12-27 12:15:45 -08:00
|
|
|
this._loggedIn = false;
|
2009-03-24 19:23:53 -07:00
|
|
|
this._detailedStatus = new StatusRecord();
|
2008-03-30 08:40:23 -07:00
|
|
|
|
2009-03-11 18:29:18 -07:00
|
|
|
if (typeof(user) != 'undefined')
|
|
|
|
this.username = user;
|
|
|
|
if (typeof(pass) != 'undefined')
|
|
|
|
ID.get('WeaveID').setTempPassword(pass);
|
|
|
|
if (typeof(passp) != 'undefined')
|
|
|
|
ID.get('WeaveCryptoID').setTempPassword(passp);
|
|
|
|
|
|
|
|
if (!this.username) {
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure( LOGIN_FAILED_NO_USERNAME );
|
2009-03-11 18:29:18 -07:00
|
|
|
throw "No username set, login failed";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.password) {
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure( LOGIN_FAILED_NO_PASSWORD );
|
2009-03-11 18:29:18 -07:00
|
|
|
throw "No password given or found in password manager";
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.debug("Logging in user " + this.username);
|
|
|
|
|
|
|
|
if (!(yield this.verifyLogin(self.cb, this.username, this.password, true))) {
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure( LOGIN_FAILED_REJECTED );
|
2009-03-11 18:29:18 -07:00
|
|
|
throw "Login failed";
|
|
|
|
}
|
|
|
|
|
2009-03-12 13:01:04 -07:00
|
|
|
// Try starting the sync timer now that we're logged in
|
2009-03-11 18:29:18 -07:00
|
|
|
this._loggedIn = true;
|
2009-03-12 13:01:04 -07:00
|
|
|
this._checkSync();
|
|
|
|
|
2009-03-11 18:29:18 -07:00
|
|
|
self.done(true);
|
2008-12-27 12:15:45 -08:00
|
|
|
};
|
2009-03-12 08:34:12 -07:00
|
|
|
this._catchAll(this._localLock(this._notify("login", "", fn))).
|
|
|
|
async(this, onComplete);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-30 14:00:06 -07:00
|
|
|
logout: function WeaveSvc_logout() {
|
2007-12-10 21:38:53 -08:00
|
|
|
this._log.info("Logging out");
|
2008-04-10 21:38:15 -07:00
|
|
|
this._loggedIn = false;
|
2008-07-08 18:37:14 -07:00
|
|
|
this._keyPair = {};
|
2008-04-14 18:53:35 -07:00
|
|
|
ID.get('WeaveID').setTempPassword(null); // clear cached password
|
|
|
|
ID.get('WeaveCryptoID').setTempPassword(null); // and passphrase
|
2009-03-10 04:15:52 -07:00
|
|
|
|
|
|
|
// Cancel the sync timer now that we're logged out
|
|
|
|
this._checkSync();
|
|
|
|
|
2009-03-10 04:30:30 -07:00
|
|
|
Svc.Observer.notifyObservers(null, "weave:service:logout:finish", "");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
createAccount: function WeaveSvc_createAccount(onComplete, username, password, email,
|
|
|
|
captchaChallenge, captchaResponse) {
|
|
|
|
let fn = function WeaveSvc__createAccount() {
|
2009-03-27 00:56:38 -07:00
|
|
|
let self = yield;
|
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
function enc(x) encodeURIComponent(x);
|
|
|
|
let message = "uid=" + enc(username) + "&password=" + enc(password) +
|
|
|
|
"&mail=" + enc(email) +
|
|
|
|
"&recaptcha_challenge_field=" + enc(captchaChallenge) +
|
|
|
|
"&recaptcha_response_field=" + enc(captchaResponse);
|
|
|
|
|
|
|
|
let url = Svc.Prefs.get('tmpServerURL') + '0.3/api/register/new';
|
|
|
|
let res = new Weave.Resource(url);
|
|
|
|
res.authenticator = new Weave.NoOpAuthenticator();
|
|
|
|
res.setHeader("Content-Type", "application/x-www-form-urlencoded",
|
|
|
|
"Content-Length", message.length);
|
|
|
|
|
|
|
|
// fixme: Resource throws on error - it really shouldn't :-/
|
|
|
|
let resp;
|
|
|
|
try {
|
|
|
|
resp = yield res.post(self.cb, message);
|
2009-03-27 00:56:38 -07:00
|
|
|
} catch (e) {
|
|
|
|
this._log.trace("Create account error: " + e);
|
|
|
|
}
|
2009-03-27 00:46:10 -07:00
|
|
|
|
|
|
|
if (res.lastChannel.responseStatus != 200 &&
|
|
|
|
res.lastChannel.responseStatus != 201)
|
|
|
|
this._log.info("Failed to create account. " +
|
|
|
|
"status: " + res.lastChannel.responseStatus + ", " +
|
|
|
|
"response: " + resp);
|
|
|
|
else
|
|
|
|
this._log.info("Account created: " + resp);
|
|
|
|
|
|
|
|
self.done(res.lastChannel.responseStatus);
|
|
|
|
};
|
|
|
|
fn.async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
2008-11-19 16:18:35 -08:00
|
|
|
// stuff we need to to after login, before we can really do
|
|
|
|
// anything (e.g. key setup)
|
|
|
|
_remoteSetup: function WeaveSvc__remoteSetup() {
|
|
|
|
let self = yield;
|
|
|
|
let ret = false; // false to abort sync
|
2009-02-19 04:09:55 -08:00
|
|
|
let reset = false;
|
2008-11-19 16:18:35 -08:00
|
|
|
|
2009-02-11 23:54:20 -08:00
|
|
|
this._log.debug("Fetching global metadata record");
|
2009-03-12 08:34:12 -07:00
|
|
|
let meta = yield Records.import(self.cb, this.clusterURL + this.username +
|
|
|
|
"/meta/global");
|
2009-03-07 00:55:47 -08:00
|
|
|
|
2009-03-11 21:37:47 -07:00
|
|
|
// XXX Bug 482878 Old payloads weren't array-wrapped, so migrate by wiping
|
|
|
|
if (meta && meta.payload == null) {
|
|
|
|
this._log.debug("Migrating to minimal payloads by wiping the server");
|
|
|
|
meta = null;
|
|
|
|
}
|
|
|
|
|
2009-03-03 11:57:30 -08:00
|
|
|
let remoteVersion = (meta && meta.payload.storageVersion)?
|
|
|
|
meta.payload.storageVersion : "";
|
2009-03-02 14:15:46 -08:00
|
|
|
|
2009-03-03 11:57:30 -08:00
|
|
|
this._log.debug("Min supported storage version is " + MIN_SERVER_STORAGE_VERSION);
|
|
|
|
this._log.debug("Remote storage version is " + remoteVersion);
|
2009-03-02 14:15:46 -08:00
|
|
|
|
2009-02-23 12:36:55 -08:00
|
|
|
if (!meta || !meta.payload.storageVersion || !meta.payload.syncID ||
|
2009-03-03 11:57:30 -08:00
|
|
|
Svc.Version.compare(MIN_SERVER_STORAGE_VERSION, remoteVersion) > 0) {
|
|
|
|
|
2009-02-23 16:27:41 -08:00
|
|
|
// abort the server wipe if the GET status was anything other than 404 or 200
|
|
|
|
let status = Records.lastResource.lastChannel.responseStatus;
|
|
|
|
if (status != 200 && status != 404) {
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure(METARECORD_DOWNLOAD_FAIL);
|
2009-03-12 08:34:12 -07:00
|
|
|
this._log.warn("Unknown error while downloading metadata record. " +
|
2009-03-03 11:57:30 -08:00
|
|
|
"Aborting sync.");
|
2009-03-12 08:34:12 -07:00
|
|
|
self.done(false);
|
|
|
|
return;
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
2009-02-23 16:56:23 -08:00
|
|
|
|
2009-03-03 11:57:30 -08:00
|
|
|
if (!meta)
|
2009-03-12 08:34:12 -07:00
|
|
|
this._log.info("No metadata record, server wipe needed");
|
2009-03-03 11:57:30 -08:00
|
|
|
if (meta && !meta.payload.syncID)
|
2009-03-12 08:34:12 -07:00
|
|
|
this._log.warn("No sync id, server wipe needed");
|
2009-03-03 11:57:30 -08:00
|
|
|
if (Svc.Version.compare(MIN_SERVER_STORAGE_VERSION, remoteVersion) > 0)
|
2009-03-12 08:34:12 -07:00
|
|
|
this._log.info("Server storage version no longer supported, server wipe needed");
|
2009-03-03 11:57:30 -08:00
|
|
|
|
2009-03-18 13:22:40 -07:00
|
|
|
if (!this._keyGenEnabled) {
|
|
|
|
this._log.info("...and key generation is disabled. Not wiping. " +
|
|
|
|
"Aborting sync.");
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure(DESKTOP_VERSION_OUT_OF_DATE);
|
2009-03-18 13:22:40 -07:00
|
|
|
self.done(false);
|
|
|
|
return;
|
|
|
|
}
|
2009-02-23 12:36:55 -08:00
|
|
|
reset = true;
|
2009-03-03 11:57:30 -08:00
|
|
|
this._log.info("Wiping server data");
|
2009-02-23 12:36:55 -08:00
|
|
|
yield this._freshStart.async(this, self.cb);
|
2009-02-23 16:56:23 -08:00
|
|
|
|
|
|
|
if (status == 404)
|
|
|
|
this._log.info("Metadata record not found, server wiped to ensure " +
|
|
|
|
"consistency.");
|
|
|
|
else // 200
|
|
|
|
this._log.info("Server data wiped to ensure consistency after client " +
|
2009-03-03 11:57:30 -08:00
|
|
|
"upgrade (" + remoteVersion + " -> " + WEAVE_VERSION + ")");
|
2009-02-23 12:36:55 -08:00
|
|
|
|
2009-03-03 11:57:30 -08:00
|
|
|
} else if (Svc.Version.compare(remoteVersion, WEAVE_VERSION) > 0) {
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure(VERSION_OUT_OF_DATE);
|
2009-02-23 12:36:55 -08:00
|
|
|
this._log.warn("Server data is of a newer Weave version, this client " +
|
|
|
|
"needs to be upgraded. Aborting sync.");
|
|
|
|
self.done(false);
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (meta.payload.syncID != Clients.syncID) {
|
2009-03-12 08:34:12 -07:00
|
|
|
this._log.warn("Meta.payload.syncID is " + meta.payload.syncID +
|
|
|
|
", Clients.syncID is " + Clients.syncID);
|
2009-02-26 22:36:14 -08:00
|
|
|
yield this.resetClient(self.cb);
|
|
|
|
this._log.info("Reset client because of syncID mismatch.");
|
2009-02-23 12:36:55 -08:00
|
|
|
Clients.syncID = meta.payload.syncID;
|
2009-02-26 22:36:14 -08:00
|
|
|
this._log.info("Reset the client after a server/client sync ID mismatch");
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
|
|
|
|
2008-11-19 16:18:35 -08:00
|
|
|
let needKeys = true;
|
|
|
|
let pubkey = yield PubKeys.getDefaultKey(self.cb);
|
|
|
|
if (pubkey) {
|
|
|
|
// make sure we have a matching privkey
|
|
|
|
let privkey = yield PrivKeys.get(self.cb, pubkey.privateKeyUri);
|
|
|
|
if (privkey) {
|
|
|
|
needKeys = false;
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
2009-02-11 23:54:20 -08:00
|
|
|
if (needKeys) {
|
|
|
|
if (PubKeys.lastResource.lastChannel.responseStatus != 404 &&
|
2009-03-12 08:34:12 -07:00
|
|
|
PrivKeys.lastResource.lastChannel.responseStatus != 404) {
|
|
|
|
this._log.warn("Couldn't download keys from server, aborting sync");
|
|
|
|
this._log.debug("PubKey HTTP response status: " +
|
|
|
|
PubKeys.lastResource.lastChannel.responseStatus);
|
|
|
|
this._log.debug("PrivKey HTTP response status: " +
|
|
|
|
PrivKeys.lastResource.lastChannel.responseStatus);
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure(KEYS_DOWNLOAD_FAIL);
|
2009-03-12 08:34:12 -07:00
|
|
|
self.done(false);
|
|
|
|
return;
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._keyGenEnabled) {
|
2009-03-12 08:34:12 -07:00
|
|
|
this._log.warn("Couldn't download keys from server, and key generation" +
|
|
|
|
"is disabled. Aborting sync");
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure(NO_KEYS_NO_KEYGEN);
|
2009-03-12 08:34:12 -07:00
|
|
|
self.done(false);
|
|
|
|
return;
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
|
|
|
|
2009-02-19 04:09:55 -08:00
|
|
|
if (!reset) {
|
2009-03-12 08:34:12 -07:00
|
|
|
this._log.warn("Calling freshStart from !reset case.");
|
|
|
|
yield this._freshStart.async(this, self.cb);
|
|
|
|
this._log.info("Server data wiped to ensure consistency due to missing keys");
|
2009-02-19 04:09:55 -08:00
|
|
|
}
|
|
|
|
|
2008-11-19 16:18:35 -08:00
|
|
|
let pass = yield ID.get('WeaveCryptoID').getPassword(self.cb);
|
|
|
|
if (pass) {
|
|
|
|
let keys = PubKeys.createKeypair(pass, PubKeys.defaultKeyUri,
|
|
|
|
PrivKeys.defaultKeyUri);
|
|
|
|
try {
|
2009-03-12 08:34:12 -07:00
|
|
|
yield PubKeys.uploadKeypair(self.cb, keys);
|
2008-11-19 16:18:35 -08:00
|
|
|
ret = true;
|
|
|
|
} catch (e) {
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure(KEYS_UPLOAD_FAIL);
|
2008-11-19 16:18:35 -08:00
|
|
|
this._log.error("Could not upload keys: " + Utils.exceptionStr(e));
|
2009-03-12 08:34:12 -07:00
|
|
|
// FIXME no lastRequest anymore
|
2009-02-10 00:56:42 -08:00
|
|
|
//this._log.error(keys.pubkey.lastRequest.responseText);
|
2008-11-19 16:18:35 -08:00
|
|
|
}
|
|
|
|
} else {
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure(SETUP_FAILED_NO_PASSPHRASE);
|
2008-11-19 16:18:35 -08:00
|
|
|
this._log.warn("Could not get encryption passphrase");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.done(ret);
|
|
|
|
},
|
|
|
|
|
2009-03-10 04:07:24 -07:00
|
|
|
/**
|
|
|
|
* Engine scores must meet or exceed this value before we sync them when
|
|
|
|
* using thresholds. These are engine-specific, as different kinds of data
|
|
|
|
* change at different rates, so we store them in a hash by engine name.
|
|
|
|
*/
|
|
|
|
_syncThresh: {},
|
|
|
|
|
2009-03-10 04:15:52 -07:00
|
|
|
/**
|
|
|
|
* Determine if a sync should run. If so, schedule a repeating sync;
|
|
|
|
* otherwise, cancel future syncs and return a reason.
|
|
|
|
*
|
|
|
|
* @return Reason for not syncing; not-truthy if sync should run
|
|
|
|
*/
|
2009-03-12 13:41:46 -07:00
|
|
|
_checkSync: function WeaveSvc__checkSync() {
|
2009-03-10 04:15:52 -07:00
|
|
|
let reason = "";
|
|
|
|
if (!this.enabled)
|
|
|
|
reason = kSyncWeaveDisabled;
|
|
|
|
else if (!this._loggedIn)
|
|
|
|
reason = kSyncNotLoggedIn;
|
2009-03-10 04:30:36 -07:00
|
|
|
else if (Svc.IO.offline)
|
|
|
|
reason = kSyncNetworkOffline;
|
2009-03-10 17:12:59 -07:00
|
|
|
else if (Svc.Private && Svc.Private.privateBrowsingEnabled)
|
|
|
|
// Svc.Private doesn't exist on Fennec -- don't assume it's there.
|
2009-03-10 04:30:36 -07:00
|
|
|
reason = kSyncInPrivateBrowsing;
|
2009-03-10 04:15:52 -07:00
|
|
|
else if (Svc.Prefs.get("schedule", 0) != 1)
|
|
|
|
reason = kSyncNotScheduled;
|
|
|
|
|
|
|
|
// A truthy reason means we shouldn't continue to sync
|
|
|
|
if (reason) {
|
|
|
|
// Cancel any future syncs
|
|
|
|
if (this._syncTimer) {
|
|
|
|
this._syncTimer.cancel();
|
|
|
|
this._syncTimer = null;
|
|
|
|
}
|
|
|
|
this._log.config("Weave scheduler disabled: " + reason);
|
|
|
|
}
|
|
|
|
// We're good to sync, so schedule a repeating sync if we haven't yet
|
|
|
|
else if (!this._syncTimer) {
|
|
|
|
this._syncTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
2009-03-12 13:41:46 -07:00
|
|
|
let listener = new Utils.EventListener(Utils.bind2(this,
|
|
|
|
function WeaveSvc__checkSyncCallback(timer) this.sync(null, false)));
|
2009-03-10 04:15:52 -07:00
|
|
|
this._syncTimer.initWithCallback(listener, SCHEDULED_SYNC_INTERVAL,
|
|
|
|
Ci.nsITimer.TYPE_REPEATING_SLACK);
|
|
|
|
this._log.config("Weave scheduler enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
return reason;
|
|
|
|
},
|
|
|
|
|
2009-03-10 04:07:24 -07:00
|
|
|
/**
|
|
|
|
* Sync up engines with the server.
|
|
|
|
*
|
2009-03-10 12:12:05 -07:00
|
|
|
* @param fullSync
|
|
|
|
* True to unconditionally sync all engines
|
2009-03-10 04:07:24 -07:00
|
|
|
* @throw Reason for not syncing
|
|
|
|
*/
|
2009-03-10 12:13:18 -07:00
|
|
|
_sync: function WeaveSvc__sync(fullSync) {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2009-03-10 12:12:05 -07:00
|
|
|
// Use thresholds to determine what to sync only if it's not a full sync
|
|
|
|
let useThresh = !fullSync;
|
|
|
|
|
2009-03-10 04:15:52 -07:00
|
|
|
// Make sure we should sync or record why we shouldn't. We always obey the
|
|
|
|
// reason if we're using thresholds (not a full sync); otherwise, allow
|
|
|
|
// "not scheduled" as future syncs have already been canceled by checkSync.
|
|
|
|
let reason = this._checkSync();
|
|
|
|
if (reason && (useThresh || reason != kSyncNotScheduled)) {
|
2009-03-24 19:23:53 -07:00
|
|
|
// this is a purposeful abort rather than a failure, so don't set
|
|
|
|
// WEAVE_STATUS_FAILED; instead, leave it as it was.
|
|
|
|
this._detailedStatus.setSyncStatus(reason);
|
2009-03-10 04:15:52 -07:00
|
|
|
reason = "Can't sync: " + reason;
|
|
|
|
throw reason;
|
2009-02-03 15:50:41 -08:00
|
|
|
}
|
2008-12-27 12:15:45 -08:00
|
|
|
|
2008-11-19 16:18:35 -08:00
|
|
|
if (!(yield this._remoteSetup.async(this, self.cb))) {
|
|
|
|
throw "aborting sync, remote setup failed";
|
|
|
|
}
|
2009-01-03 01:40:22 -08:00
|
|
|
|
|
|
|
this._log.debug("Refreshing client list");
|
2009-01-06 13:54:18 -08:00
|
|
|
yield Clients.sync(self.cb);
|
2008-07-31 01:02:41 -07:00
|
|
|
|
2009-03-11 23:33:14 -07:00
|
|
|
// Process the incoming commands if we have any
|
|
|
|
if (Clients.getClients()[Clients.clientID].commands) {
|
|
|
|
try {
|
2009-03-24 19:23:53 -07:00
|
|
|
if (!(yield this.processCommands(self.cb))) {
|
|
|
|
this._detailedStatus.setSyncStatus(ABORT_SYNC_COMMAND);
|
2009-03-11 23:33:14 -07:00
|
|
|
throw "aborting sync, process commands said so";
|
2009-03-24 19:23:53 -07:00
|
|
|
}
|
2009-03-11 23:33:14 -07:00
|
|
|
|
|
|
|
// Repeat remoteSetup in-case the commands forced us to reset
|
|
|
|
if (!(yield this._remoteSetup.async(this, self.cb)))
|
|
|
|
throw "aborting sync, remote setup failed after processing commands";
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
// Always immediately push back the local client (now without commands)
|
|
|
|
yield Clients.sync(self.cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
try {
|
2009-03-10 04:07:24 -07:00
|
|
|
for each (let engine in Engines.getAll()) {
|
|
|
|
let name = engine.name;
|
2009-02-03 15:50:41 -08:00
|
|
|
|
2009-03-10 04:07:24 -07:00
|
|
|
// Nothing to do for disabled engines
|
2009-01-06 13:54:18 -08:00
|
|
|
if (!engine.enabled)
|
|
|
|
continue;
|
|
|
|
|
2009-03-10 04:07:24 -07:00
|
|
|
// Conditionally reset the threshold for the current engine
|
|
|
|
let resetThresh = Utils.bind2(this, function WeaveSvc__resetThresh(cond)
|
|
|
|
cond ? this._syncThresh[name] = INITIAL_THRESHOLD : undefined);
|
|
|
|
|
|
|
|
// Initialize the threshold if it doesn't exist yet
|
|
|
|
resetThresh(!(name in this._syncThresh));
|
|
|
|
|
|
|
|
// Determine if we should sync if using thresholds
|
|
|
|
if (useThresh) {
|
|
|
|
let score = engine.score;
|
|
|
|
let thresh = this._syncThresh[name];
|
|
|
|
if (score >= thresh)
|
|
|
|
this._log.debug("Syncing " + name + "; " +
|
|
|
|
"score " + score + " >= thresh " + thresh);
|
|
|
|
else {
|
|
|
|
this._log.debug("Not syncing " + name + "; " +
|
|
|
|
"score " + score + " < thresh " + thresh);
|
|
|
|
|
|
|
|
// Decrement the threshold by a standard amount with a lower bound of 1
|
|
|
|
this._syncThresh[name] = Math.max(thresh - THRESHOLD_DECREMENT_STEP, 1);
|
|
|
|
|
|
|
|
// No need to sync this engine for now
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-06 13:54:18 -08:00
|
|
|
}
|
2009-03-10 04:07:24 -07:00
|
|
|
|
|
|
|
// If there's any problems with syncing the engine, report the failure
|
|
|
|
if (!(yield this._syncEngine.async(this, self.cb, engine))) {
|
|
|
|
this._log.info("Aborting sync");
|
|
|
|
break;
|
2009-01-06 13:54:18 -08:00
|
|
|
}
|
2009-03-10 04:07:24 -07:00
|
|
|
|
|
|
|
// We've successfully synced, so reset the threshold. We do this after
|
|
|
|
// a successful sync so failures can try again on next sync, but this
|
|
|
|
// could trigger too many syncs if the server is having problems.
|
|
|
|
resetThresh(useThresh);
|
2008-04-15 17:21:34 -07:00
|
|
|
}
|
2008-07-08 17:44:00 -07:00
|
|
|
|
2009-03-10 04:07:24 -07:00
|
|
|
if (this._syncError)
|
2009-02-19 04:09:55 -08:00
|
|
|
this._log.warn("Some engines did not sync correctly");
|
2009-03-10 04:07:24 -07:00
|
|
|
else {
|
|
|
|
Svc.Prefs.set("lastSync", new Date().toString());
|
2009-03-24 19:23:53 -07:00
|
|
|
this._weaveStatusCode = WEAVE_STATUS_OK;
|
2009-03-10 04:07:24 -07:00
|
|
|
this._log.info("Sync completed successfully");
|
|
|
|
}
|
2009-01-06 13:54:18 -08:00
|
|
|
} finally {
|
2009-03-10 04:07:24 -07:00
|
|
|
this.cancelRequested = false;
|
2008-07-08 17:44:00 -07:00
|
|
|
this._syncError = false;
|
|
|
|
}
|
2008-03-30 08:40:23 -07:00
|
|
|
},
|
2008-06-11 10:38:25 -07:00
|
|
|
|
2009-03-10 04:07:24 -07:00
|
|
|
/**
|
|
|
|
* Do a synchronized sync (only one sync at a time).
|
|
|
|
*
|
|
|
|
* @param onComplete
|
|
|
|
* Callback when this method completes
|
|
|
|
* @param fullSync
|
|
|
|
* True to unconditionally sync all engines
|
|
|
|
*/
|
|
|
|
sync: function WeaveSvc_sync(onComplete, fullSync) {
|
2009-03-10 12:12:05 -07:00
|
|
|
fullSync = true; // not doing thresholds yet
|
2009-03-10 04:07:24 -07:00
|
|
|
this._catchAll(this._notify("sync", "", this._localLock(this._sync))).
|
2009-03-10 12:12:05 -07:00
|
|
|
async(this, onComplete, fullSync);
|
2009-03-10 04:07:24 -07:00
|
|
|
},
|
|
|
|
|
2009-02-11 22:08:56 -08:00
|
|
|
// returns true if sync should proceed
|
|
|
|
// false / no return value means sync should be aborted
|
2008-03-30 08:40:23 -07:00
|
|
|
_syncEngine: function WeaveSvc__syncEngine(engine) {
|
|
|
|
let self = yield;
|
2009-02-11 22:08:56 -08:00
|
|
|
try {
|
|
|
|
yield engine.sync(self.cb);
|
|
|
|
if (!this.cancelRequested)
|
2009-03-12 08:34:12 -07:00
|
|
|
self.done(true);
|
2009-02-11 22:08:56 -08:00
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
catch(e) {
|
2009-01-06 13:54:18 -08:00
|
|
|
this._syncError = true;
|
2009-03-24 19:23:53 -07:00
|
|
|
this._weaveStatusCode = WEAVE_STATUS_PARTIAL;
|
|
|
|
this._detailedStatus.setEngineStatus(engine.name, e);
|
2009-02-11 22:08:56 -08:00
|
|
|
if (FaultTolerance.Service.onException(e))
|
2009-03-12 08:34:12 -07:00
|
|
|
self.done(true);
|
2008-05-23 23:58:53 -07:00
|
|
|
}
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
|
|
|
|
2009-02-19 04:09:55 -08:00
|
|
|
_freshStart: function WeaveSvc__freshStart() {
|
|
|
|
let self = yield;
|
2009-02-26 22:36:14 -08:00
|
|
|
yield this.resetClient(self.cb);
|
|
|
|
this._log.info("Reset client data from freshStart.");
|
2009-02-19 04:09:55 -08:00
|
|
|
this._log.info("Client metadata wiped, deleting server data");
|
2009-02-26 22:14:32 -08:00
|
|
|
yield this.wipeServer(self.cb);
|
2009-02-19 04:09:55 -08:00
|
|
|
|
|
|
|
this._log.debug("Uploading new metadata record");
|
|
|
|
meta = new WBORecord(this.clusterURL + this.username + "/meta/global");
|
2009-02-27 18:28:26 -08:00
|
|
|
this._log.debug("Setting meta payload storage version to " + WEAVE_VERSION);
|
2009-02-19 04:09:55 -08:00
|
|
|
meta.payload.storageVersion = WEAVE_VERSION;
|
|
|
|
meta.payload.syncID = Clients.syncID;
|
|
|
|
let res = new Resource(meta.uri);
|
|
|
|
yield res.put(self.cb, meta.serialize());
|
|
|
|
},
|
|
|
|
|
2009-02-26 22:14:32 -08:00
|
|
|
/**
|
|
|
|
* Wipe all user data from the server.
|
2009-03-11 23:33:14 -07:00
|
|
|
*
|
|
|
|
* @param onComplete
|
|
|
|
* Callback when this method completes
|
2009-03-20 00:13:16 -07:00
|
|
|
* @param engines [optional]
|
|
|
|
* Array of engine names to wipe. If not given, all engines are used.
|
2009-02-26 22:14:32 -08:00
|
|
|
*/
|
2009-03-20 00:13:16 -07:00
|
|
|
wipeServer: function WeaveSvc_wipeServer(onComplete, engines) {
|
2009-02-26 22:14:32 -08:00
|
|
|
let fn = function WeaveSvc__wipeServer() {
|
|
|
|
let self = yield;
|
2008-03-31 07:20:09 -07:00
|
|
|
|
2009-02-26 22:14:32 -08:00
|
|
|
// Grab all the collections for the user
|
|
|
|
let userURL = this.clusterURL + this.username + "/";
|
|
|
|
let res = new Resource(userURL);
|
|
|
|
yield res.get(self.cb);
|
|
|
|
|
|
|
|
// Get the array of collections and delete each one
|
|
|
|
let allCollections = Svc.Json.decode(res.data);
|
|
|
|
for each (let name in allCollections) {
|
|
|
|
try {
|
2009-03-20 00:13:16 -07:00
|
|
|
// If we have a list of engines, make sure it's one we want
|
|
|
|
if (engines && engines.indexOf(name) == -1)
|
|
|
|
continue;
|
|
|
|
|
2009-02-26 22:14:32 -08:00
|
|
|
yield new Resource(userURL + name).delete(self.cb);
|
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
this._log.debug("Exception on wipe of '" + name + "': " + Utils.exceptionStr(ex));
|
|
|
|
}
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
2009-02-26 22:14:32 -08:00
|
|
|
};
|
|
|
|
this._catchAll(this._notify("wipe-server", "", fn)).async(this, onComplete);
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2009-02-26 22:36:14 -08:00
|
|
|
/**
|
2009-03-11 23:33:14 -07:00
|
|
|
* Wipe all local user data.
|
|
|
|
*
|
|
|
|
* @param onComplete
|
|
|
|
* Callback when this method completes
|
2009-03-20 00:13:16 -07:00
|
|
|
* @param engines [optional]
|
|
|
|
* Array of engine names to wipe. If not given, all engines are used.
|
2009-02-26 22:36:14 -08:00
|
|
|
*/
|
2009-03-20 00:13:16 -07:00
|
|
|
wipeClient: function WeaveSvc_wipeClient(onComplete, engines) {
|
2009-03-11 23:33:14 -07:00
|
|
|
let fn = function WeaveSvc__wipeClient() {
|
|
|
|
let self = yield;
|
|
|
|
|
2009-03-20 00:13:16 -07:00
|
|
|
// If we don't have any engines, reset the service and wipe all engines
|
|
|
|
if (!engines) {
|
|
|
|
// Clear out any service data
|
|
|
|
yield this.resetService(self.cb);
|
|
|
|
|
|
|
|
engines = [Clients].concat(Engines.getAll());
|
|
|
|
}
|
|
|
|
// Convert the array of names into engines
|
|
|
|
else
|
|
|
|
engines = Engines.get(engines);
|
2009-03-11 23:33:14 -07:00
|
|
|
|
|
|
|
// Fully wipe each engine
|
2009-03-20 00:13:16 -07:00
|
|
|
for each (let engine in engines)
|
2009-03-11 23:33:14 -07:00
|
|
|
yield engine.wipeClient(self.cb);
|
|
|
|
};
|
|
|
|
this._catchAll(this._notify("wipe-client", "", fn)).async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wipe all remote user data by wiping the server then telling each remote
|
|
|
|
* client to wipe itself.
|
|
|
|
*
|
|
|
|
* @param onComplete
|
|
|
|
* Callback when this method completes
|
2009-03-20 00:13:16 -07:00
|
|
|
* @param engines [optional]
|
|
|
|
* Array of engine names to wipe. If not given, all engines are used.
|
2009-03-11 23:33:14 -07:00
|
|
|
*/
|
2009-03-20 00:13:16 -07:00
|
|
|
wipeRemote: function WeaveSvc_wipeRemote(onComplete, engines) {
|
2009-03-11 23:33:14 -07:00
|
|
|
let fn = function WeaveSvc__wipeRemote() {
|
|
|
|
let self = yield;
|
|
|
|
|
|
|
|
// Clear out any server data
|
2009-03-20 00:13:16 -07:00
|
|
|
//yield this.wipeServer(self.cb, engines);
|
|
|
|
|
|
|
|
// Only wipe the engines provided
|
|
|
|
if (engines) {
|
|
|
|
engines.forEach(function(e) this.prepCommand("wipeEngine", [e]), this);
|
|
|
|
return;
|
|
|
|
}
|
2009-03-11 23:33:14 -07:00
|
|
|
|
|
|
|
// Tell the remote machines to wipe themselves
|
|
|
|
this.prepCommand("wipeAll", []);
|
|
|
|
};
|
|
|
|
this._catchAll(this._notify("wipe-remote", "", fn)).async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset local service information like logs, sync times, caches.
|
|
|
|
*
|
|
|
|
* @param onComplete
|
|
|
|
* Callback when this method completes
|
|
|
|
*/
|
|
|
|
resetService: function WeaveSvc__resetService(onComplete) {
|
|
|
|
let fn = function WeaveSvc__resetService() {
|
2009-02-26 22:36:14 -08:00
|
|
|
let self = yield;
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2009-02-26 22:36:14 -08:00
|
|
|
// First drop old logs to track client resetting behavior
|
|
|
|
this.clearLogs();
|
2009-03-11 23:33:14 -07:00
|
|
|
this._log.info("Logs reinitialized for service reset");
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2009-02-26 22:36:14 -08:00
|
|
|
// Pretend we've never synced to the server and drop cached data
|
|
|
|
Clients.resetSyncID();
|
|
|
|
Svc.Prefs.reset("lastSync");
|
|
|
|
for each (let cache in [PubKeys, PrivKeys, CryptoMetas, Records])
|
|
|
|
cache.clearCache();
|
2009-03-11 23:33:14 -07:00
|
|
|
};
|
|
|
|
this._catchAll(this._notify("reset-service", "", fn)).async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the client by getting rid of any local server data and client data.
|
|
|
|
*
|
|
|
|
* @param onComplete
|
|
|
|
* Callback when this method completes
|
2009-03-20 00:13:16 -07:00
|
|
|
* @param engines [optional]
|
|
|
|
* Array of engine names to reset. If not given, all engines are used.
|
2009-03-11 23:33:14 -07:00
|
|
|
*/
|
2009-03-20 00:13:16 -07:00
|
|
|
resetClient: function WeaveSvc_resetClient(onComplete, engines) {
|
2009-03-11 23:33:14 -07:00
|
|
|
let fn = function WeaveSvc__resetClient() {
|
|
|
|
let self = yield;
|
|
|
|
|
2009-03-20 00:13:16 -07:00
|
|
|
// If we don't have any engines, reset everything including the service
|
|
|
|
if (!engines) {
|
|
|
|
// Clear out any service data
|
|
|
|
yield this.resetService(self.cb);
|
|
|
|
|
|
|
|
engines = [Clients].concat(Engines.getAll());
|
|
|
|
}
|
|
|
|
// Convert the array of names into engines
|
|
|
|
else
|
|
|
|
engines = Engines.get(engines);
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2009-02-26 22:36:14 -08:00
|
|
|
// Have each engine drop any temporary meta data
|
2009-03-20 00:13:16 -07:00
|
|
|
for each (let engine in engines)
|
2009-02-26 22:36:14 -08:00
|
|
|
yield engine.resetClient(self.cb);
|
2009-03-03 11:57:30 -08:00
|
|
|
|
2009-02-26 22:36:14 -08:00
|
|
|
// XXX Bug 480448: Delete any snapshots from old code
|
|
|
|
try {
|
2009-03-10 04:30:30 -07:00
|
|
|
let cruft = Svc.Directory.get("ProfD", Ci.nsIFile);
|
2009-02-26 22:36:14 -08:00
|
|
|
cruft.QueryInterface(Ci.nsILocalFile);
|
|
|
|
cruft.append("weave");
|
|
|
|
cruft.append("snapshots");
|
|
|
|
if (cruft.exists())
|
|
|
|
cruft.remove(true);
|
|
|
|
} catch (e) {
|
|
|
|
this._log.debug("Could not remove old snapshots: " + Utils.exceptionStr(e));
|
|
|
|
}
|
2009-03-11 23:33:14 -07:00
|
|
|
};
|
2009-02-26 22:36:14 -08:00
|
|
|
this._catchAll(this._notify("reset-client", "", fn)).async(this, onComplete);
|
2009-03-03 15:29:35 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A hash of valid commands that the client knows about. The key is a command
|
|
|
|
* and the value is a hash containing information about the command such as
|
|
|
|
* number of arguments and description.
|
|
|
|
*/
|
|
|
|
_commands: [
|
|
|
|
["resetAll", 0, "Clear temporary local data for all engines"],
|
|
|
|
["resetEngine", 1, "Clear temporary local data for engine"],
|
|
|
|
["wipeAll", 0, "Delete all client data for all engines"],
|
|
|
|
["wipeEngine", 1, "Delete all client data for engine"],
|
|
|
|
].reduce(function WeaveSvc__commands(commands, entry) {
|
|
|
|
commands[entry[0]] = {};
|
|
|
|
for (let [i, attr] in Iterator(["args", "desc"]))
|
|
|
|
commands[entry[0]][attr] = entry[i + 1];
|
|
|
|
return commands;
|
|
|
|
}, {}),
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2009-03-11 23:33:14 -07:00
|
|
|
/**
|
|
|
|
* Check if the local client has any remote commands and perform them.
|
|
|
|
*
|
|
|
|
* @param onComplete
|
|
|
|
* Callback when this method completes
|
|
|
|
* @return False to abort sync
|
|
|
|
*/
|
|
|
|
processCommands: function WeaveSvc_processCommands(onComplete) {
|
|
|
|
let fn = function WeaveSvc__processCommands() {
|
|
|
|
let self = yield;
|
|
|
|
let info = Clients.getInfo(Clients.clientID);
|
|
|
|
let commands = info.commands;
|
|
|
|
|
|
|
|
// Immediately clear out the commands as we've got them locally
|
|
|
|
delete info.commands;
|
|
|
|
Clients.setInfo(Clients.clientID, info);
|
|
|
|
|
|
|
|
// Process each command in order
|
|
|
|
for each ({command: command, args: args} in commands) {
|
|
|
|
this._log.debug("Processing command: " + command + "(" + args + ")");
|
|
|
|
|
2009-03-20 00:13:16 -07:00
|
|
|
let engines = [args[0]];
|
2009-03-11 23:33:14 -07:00
|
|
|
switch (command) {
|
|
|
|
case "resetAll":
|
2009-03-20 00:13:16 -07:00
|
|
|
engines = null;
|
|
|
|
// Fallthrough
|
|
|
|
case "resetEngine":
|
|
|
|
yield this.resetClient(self.cb, engines);
|
2009-03-11 23:33:14 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "wipeAll":
|
2009-03-20 00:13:16 -07:00
|
|
|
engines = null;
|
|
|
|
// Fallthrough
|
|
|
|
case "wipeEngine":
|
|
|
|
yield this.wipeClient(self.cb, engines);
|
2009-03-11 23:33:14 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
this._log.debug("Received an unknown command: " + command);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.done(true);
|
|
|
|
};
|
|
|
|
this._notify("process-commands", "", fn).async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
2009-03-03 15:29:35 -08:00
|
|
|
/**
|
|
|
|
* Prepare to send a command to each remote client. Calling this doesn't
|
|
|
|
* actually sync the command data to the server. If the client already has
|
|
|
|
* the command/args pair, it won't get a duplicate action.
|
|
|
|
*
|
|
|
|
* @param command
|
|
|
|
* Command to invoke on remote clients
|
|
|
|
* @param args
|
|
|
|
* Array of arguments to give to the command
|
|
|
|
*/
|
|
|
|
prepCommand: function WeaveSvc_prepCommand(command, args) {
|
|
|
|
let commandData = this._commands[command];
|
|
|
|
// Don't send commands that we don't know about
|
|
|
|
if (commandData == null) {
|
|
|
|
this._log.error("Unknown command to send: " + command);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Don't send a command with the wrong number of arguments
|
|
|
|
else if (args == null || args.length != commandData.args) {
|
|
|
|
this._log.error("Expected " + commandData.args + " args for '" +
|
|
|
|
command + "', but got " + args);
|
|
|
|
return;
|
|
|
|
}
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2009-03-03 15:29:35 -08:00
|
|
|
// Package the command/args pair into an object
|
|
|
|
let action = {
|
|
|
|
command: command,
|
|
|
|
args: args,
|
|
|
|
};
|
|
|
|
let actionStr = command + "(" + args + ")";
|
|
|
|
|
|
|
|
// Convert args into a string to simplify array comparisons
|
|
|
|
let jsonArgs = Svc.Json.encode(args);
|
|
|
|
let notDupe = function(action) action.command != command ||
|
|
|
|
Svc.Json.encode(action.args) != jsonArgs;
|
|
|
|
|
|
|
|
this._log.info("Sending clients: " + actionStr + "; " + commandData.desc);
|
|
|
|
|
|
|
|
// Add the action to each remote client
|
|
|
|
for (let guid in Clients.getClients()) {
|
|
|
|
// Don't send commands to the local client
|
|
|
|
if (guid == Clients.clientID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
let info = Clients.getInfo(guid);
|
|
|
|
// Set the action to be a new commands array if none exists
|
|
|
|
if (info.commands == null)
|
|
|
|
info.commands = [action];
|
|
|
|
// Add the new action if there are no duplicates
|
|
|
|
else if (info.commands.every(notDupe))
|
|
|
|
info.commands.push(action);
|
|
|
|
// Must have been a dupe.. skip!
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Clients.setInfo(guid, info);
|
|
|
|
this._log.trace("Client " + guid + " got a new action: " + actionStr);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|