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>
|
2009-07-23 16:52:28 -07:00
|
|
|
* Anant Narayanan <anant@kix.in>
|
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
|
|
|
|
2009-07-22 20:48:41 -07:00
|
|
|
// how long we should wait before actually syncing on idle
|
|
|
|
const IDLE_TIME = 5; // xxxmpc: in seconds, should be preffable
|
|
|
|
|
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
|
|
|
|
2009-08-19 18:22:22 -07:00
|
|
|
// How long before refreshing the cluster
|
|
|
|
const CLUSTER_BACKOFF = SCHEDULED_SYNC_INTERVAL;
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2009-07-15 11:26:22 -07:00
|
|
|
Cu.import("resource://weave/ext/Sync.js");
|
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-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");
|
|
|
|
Cu.import("resource://weave/identity.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-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);
|
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);
|
2009-04-07 14:45:41 -07:00
|
|
|
|
|
|
|
Cu.import("resource://weave/engines/bookmarks.js", Weave);
|
2009-01-06 13:54:18 -08:00
|
|
|
Cu.import("resource://weave/engines/clientData.js", Weave);
|
2009-05-14 09:43:56 -07:00
|
|
|
Cu.import("resource://weave/engines/cookies.js", Weave);
|
2009-05-14 09:50:59 -07:00
|
|
|
Cu.import("resource://weave/engines/extensions.js", Weave);
|
2009-04-07 14:45:41 -07:00
|
|
|
Cu.import("resource://weave/engines/forms.js", Weave);
|
|
|
|
Cu.import("resource://weave/engines/history.js", Weave);
|
2009-05-14 09:43:56 -07:00
|
|
|
Cu.import("resource://weave/engines/input.js", Weave);
|
2009-05-23 17:11:05 -07:00
|
|
|
Cu.import("resource://weave/engines/prefs.js", Weave);
|
2009-05-14 09:50:59 -07:00
|
|
|
Cu.import("resource://weave/engines/microformats.js", Weave);
|
2009-04-07 14:45:41 -07:00
|
|
|
Cu.import("resource://weave/engines/passwords.js", Weave);
|
2009-05-14 09:50:59 -07:00
|
|
|
Cu.import("resource://weave/engines/plugins.js", Weave);
|
2009-04-07 14:45:41 -07:00
|
|
|
Cu.import("resource://weave/engines/tabs.js", Weave);
|
2009-05-14 09:50:59 -07:00
|
|
|
Cu.import("resource://weave/engines/themes.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() {
|
2009-06-05 22:21:14 -07:00
|
|
|
this._notify = Utils.notify("weave:service:");
|
2009-02-22 00:04:58 -08:00
|
|
|
}
|
2008-03-25 15:14:00 -07:00
|
|
|
WeaveSvc.prototype = {
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2009-06-06 11:52:49 -07:00
|
|
|
_lock: Utils.lock,
|
2009-06-05 22:21:14 -07:00
|
|
|
_catch: Utils.catch,
|
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
|
|
|
},
|
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
get password password() ID.get("WeaveID").password,
|
|
|
|
set password password(value) ID.get("WeaveID").password = value,
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
get passphrase passphrase() ID.get("WeaveCryptoID").password,
|
|
|
|
set passphrase passphrase(value) ID.get("WeaveCryptoID").password = value,
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-11-06 17:37:17 -08:00
|
|
|
get baseURL() {
|
2009-08-26 15:01:28 -07:00
|
|
|
return Utils.getURLPref("serverURL");
|
2008-11-06 17:37:17 -08:00
|
|
|
},
|
|
|
|
set baseURL(value) {
|
2009-02-10 00:56:42 -08:00
|
|
|
Svc.Prefs.set("serverURL", value);
|
|
|
|
},
|
|
|
|
|
2009-08-26 15:01:28 -07:00
|
|
|
get miscURL() {
|
|
|
|
return Utils.getURLPref("miscURL");
|
|
|
|
},
|
|
|
|
set miscURL(value) {
|
|
|
|
Svc.Prefs.set("miscURL", value);
|
|
|
|
},
|
|
|
|
|
2009-02-10 00:56:42 -08:00
|
|
|
get clusterURL() {
|
2009-08-26 15:01:28 -07:00
|
|
|
return Utils.getURLPref("clusterURL", null, "0.5/");
|
2009-02-10 00:56:42 -08:00
|
|
|
},
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2009-09-03 21:30:40 -07:00
|
|
|
get userURL() this.clusterURL + this.username,
|
|
|
|
get infoURL() this.userURL + "/info/collections",
|
2009-08-25 17:15:36 -07: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 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-08-25 17:15:36 -07:00
|
|
|
let url = this.userURL;
|
2009-08-25 17:06:13 -07:00
|
|
|
PubKeys.defaultKeyUri = url + "/storage/keys/pubkey";
|
|
|
|
PrivKeys.defaultKeyUri = url + "/storage/keys/privkey";
|
2008-12-01 18:43:43 -08:00
|
|
|
},
|
|
|
|
|
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
|
2009-06-06 16:24:06 -07:00
|
|
|
onStartup: function WeaveSvc_onStartup() {
|
2008-11-06 17:37:17 -08:00
|
|
|
this._initLogs();
|
2009-02-10 00:56:42 -08:00
|
|
|
this._log.info("Weave " + WEAVE_VERSION + " initializing");
|
2009-04-08 12:39:14 -07:00
|
|
|
this._registerEngines();
|
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);
|
2009-07-22 20:48:41 -07:00
|
|
|
Svc.Observer.addObserver(this, "weave:service:sync:finish", true);
|
|
|
|
Svc.Observer.addObserver(this, "weave:service:sync:error", true);
|
2008-11-06 17:37:17 -08:00
|
|
|
|
|
|
|
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-08-19 20:27:22 -07:00
|
|
|
if (Svc.Prefs.get("autoconnect"))
|
|
|
|
this._autoConnect();
|
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-09-07 18:24:21 -07:00
|
|
|
let verbose = Svc.Directory.get("ProfD", Ci.nsIFile);
|
|
|
|
verbose.QueryInterface(Ci.nsILocalFile);
|
|
|
|
verbose.append("weave");
|
|
|
|
verbose.append("logs");
|
2007-12-19 19:49:28 -08:00
|
|
|
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._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._debugApp.clear();
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2009-04-08 12:39:14 -07:00
|
|
|
/**
|
|
|
|
* Register the built-in engines for certain applications
|
|
|
|
*/
|
|
|
|
_registerEngines: function WeaveSvc__registerEngines() {
|
|
|
|
let engines = [];
|
2009-05-18 11:11:07 -07:00
|
|
|
switch (Svc.AppInfo.ID) {
|
|
|
|
case FENNEC_ID:
|
2009-04-08 12:39:14 -07:00
|
|
|
engines = ["Bookmarks", "History", "Password", "Tab"];
|
|
|
|
break;
|
|
|
|
|
2009-05-18 11:11:07 -07:00
|
|
|
case FIREFOX_ID:
|
2009-09-03 19:57:02 -07:00
|
|
|
engines = ["Bookmarks", "Form", "History", "Password", "Prefs", "Tab"];
|
2009-04-08 12:39:14 -07:00
|
|
|
break;
|
|
|
|
|
2009-05-18 11:11:07 -07:00
|
|
|
case SEAMONKEY_ID:
|
2009-04-14 09:05:30 -07:00
|
|
|
engines = ["Form", "History", "Password"];
|
|
|
|
break;
|
|
|
|
|
2009-05-18 11:11:07 -07:00
|
|
|
case THUNDERBIRD_ID:
|
2009-04-08 12:39:14 -07:00
|
|
|
engines = ["Cookie", "Password"];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the actual engine and register them
|
|
|
|
Engines.register(engines.map(function(name) Weave[name + "Engine"]));
|
|
|
|
},
|
|
|
|
|
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
|
2009-07-22 20:48:41 -07:00
|
|
|
this._checkSyncStatus();
|
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);
|
2009-07-22 20:48:41 -07:00
|
|
|
this._checkSyncStatus();
|
2009-03-10 04:30:36 -07:00
|
|
|
break;
|
|
|
|
case "private-browsing":
|
|
|
|
// Entering or exiting private browsing? Reschedule syncs
|
|
|
|
this._log.debug("Private browsing change: " + data);
|
2009-07-22 20:48:41 -07:00
|
|
|
this._checkSyncStatus();
|
2009-03-10 04:30:36 -07:00
|
|
|
break;
|
2008-06-23 16:10:31 -07:00
|
|
|
case "quit-application":
|
|
|
|
this._onQuitApplication();
|
|
|
|
break;
|
2009-07-22 20:48:41 -07:00
|
|
|
case "weave:service:sync:error":
|
|
|
|
this._handleSyncError();
|
|
|
|
break;
|
|
|
|
case "weave:service:sync:finish":
|
|
|
|
this._scheduleNextSync();
|
|
|
|
this._serverErrors = 0;
|
|
|
|
break;
|
|
|
|
case "idle":
|
2009-07-22 21:40:18 -07:00
|
|
|
this._log.trace("Idle time hit, trying to sync");
|
2009-07-22 20:48:41 -07:00
|
|
|
Svc.Idle.removeIdleObserver(this, IDLE_TIME);
|
|
|
|
this.sync(false);
|
|
|
|
break;
|
2008-06-23 16:10:31 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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-09-03 21:11:32 -07:00
|
|
|
_findCluster: function _findCluster() {
|
|
|
|
this._log.debug("Finding cluster for user " + this.username);
|
2009-02-10 15:56:37 -08:00
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
let res = new Resource(this.baseURL + "1/" + this.username + "/node/weave");
|
2009-06-04 23:24:15 -07:00
|
|
|
try {
|
2009-08-26 15:32:46 -07:00
|
|
|
let node = res.get();
|
|
|
|
switch (node.status) {
|
2009-08-19 20:27:22 -07:00
|
|
|
case 404:
|
|
|
|
this._log.debug("Using serverURL as data cluster (multi-cluster support disabled)");
|
2009-08-26 15:01:28 -07:00
|
|
|
return this.baseURL;
|
2009-08-26 15:32:46 -07:00
|
|
|
case 0:
|
2009-08-19 20:27:22 -07:00
|
|
|
case 200:
|
2009-08-26 16:09:48 -07:00
|
|
|
return node;
|
2009-08-19 20:27:22 -07:00
|
|
|
default:
|
2009-08-26 15:32:46 -07:00
|
|
|
this._log.debug("Unexpected response code: " + node.status);
|
2009-08-19 20:27:22 -07:00
|
|
|
break;
|
|
|
|
}
|
2009-08-20 12:14:34 -07:00
|
|
|
} catch (e) {
|
|
|
|
this._log.debug("Network error on findCluster");
|
|
|
|
this._setSyncFailure(LOGIN_FAILED_NETWORK_ERROR);
|
|
|
|
throw e;
|
|
|
|
}
|
2009-02-10 00:56:42 -08:00
|
|
|
},
|
|
|
|
|
2009-03-27 00:46:10 -07:00
|
|
|
// gets cluster from central LDAP server and sets this.clusterURL
|
2009-09-03 21:11:32 -07:00
|
|
|
_setCluster: function _setCluster() {
|
|
|
|
let cluster = this._findCluster();
|
2009-06-06 16:24:14 -07:00
|
|
|
if (cluster) {
|
2009-08-19 20:27:22 -07:00
|
|
|
if (cluster == this.clusterURL)
|
|
|
|
return false;
|
|
|
|
|
2009-06-06 16:24:14 -07:00
|
|
|
this._log.debug("Saving cluster setting");
|
|
|
|
this.clusterURL = cluster;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
this._log.debug("Error setting cluster for user " + this.username);
|
2009-06-06 16:24:14 -07:00
|
|
|
return false;
|
2009-03-27 00:46:10 -07:00
|
|
|
},
|
2009-08-19 20:27:22 -07:00
|
|
|
|
|
|
|
// update cluster if required. returns false if the update was not required
|
2009-09-03 21:11:32 -07:00
|
|
|
_updateCluster: function _updateCluster() {
|
2009-08-19 18:22:22 -07:00
|
|
|
let cTime = Date.now();
|
|
|
|
let lastUp = parseFloat(Svc.Prefs.get("lastClusterUpdate"));
|
|
|
|
if (!lastUp || ((cTime - lastUp) >= CLUSTER_BACKOFF)) {
|
2009-09-03 21:11:32 -07:00
|
|
|
if (this._setCluster()) {
|
2009-08-19 20:27:22 -07:00
|
|
|
Svc.Prefs.set("lastClusterUpdate", cTime.toString());
|
|
|
|
return true;
|
|
|
|
}
|
2009-08-19 18:22:22 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2009-08-19 20:27:22 -07:00
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
_verifyLogin: function _verifyLogin()
|
2009-06-05 22:21:16 -07:00
|
|
|
this._catch(this._notify("verify-login", "", function() {
|
2009-09-03 21:11:32 -07:00
|
|
|
this._log.debug("Verifying login for user " + this.username);
|
2009-02-10 00:56:42 -08:00
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
this._setCluster();
|
2009-09-03 21:30:40 -07:00
|
|
|
let res = new Resource(this.infoURL);
|
2009-08-19 18:22:22 -07:00
|
|
|
try {
|
2009-08-26 15:32:46 -07:00
|
|
|
let test = res.get();
|
|
|
|
switch (test.status) {
|
2009-08-19 20:27:22 -07:00
|
|
|
case 200:
|
2009-09-03 21:11:32 -07:00
|
|
|
if (!this._verifyPassphrase()) {
|
2009-08-19 20:27:22 -07:00
|
|
|
this._setSyncFailure(LOGIN_FAILED_INVALID_PASSPHRASE);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case 401:
|
2009-09-03 21:11:32 -07:00
|
|
|
if (this._updateCluster())
|
|
|
|
return this._verifyLogin();
|
2009-08-19 20:27:22 -07:00
|
|
|
|
|
|
|
this._setSyncFailure(LOGIN_FAILED_LOGIN_REJECTED);
|
|
|
|
this._log.debug("verifyLogin failed: login failed")
|
|
|
|
return false;
|
|
|
|
default:
|
2009-08-26 15:32:46 -07:00
|
|
|
throw "unexpected HTTP response: " + test.status;
|
2009-08-19 18:22:22 -07:00
|
|
|
}
|
2009-08-19 20:27:22 -07:00
|
|
|
} catch (e) {
|
|
|
|
// if we get here, we have either a busted channel or a network error
|
|
|
|
this._log.debug("verifyLogin failed: " + e)
|
|
|
|
this._setSyncFailure(LOGIN_FAILED_NETWORK_ERROR);
|
2009-08-19 18:22:22 -07:00
|
|
|
throw e;
|
|
|
|
}
|
2009-06-05 22:21:16 -07:00
|
|
|
}))(),
|
2008-06-30 16:50:19 -07:00
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
_verifyPassphrase: function _verifyPassphrase()
|
2009-07-21 21:05:23 -07:00
|
|
|
this._catch(this._notify("verify-passphrase", "", function() {
|
2009-07-23 17:12:32 -07:00
|
|
|
this._log.debug("Verifying passphrase");
|
|
|
|
try {
|
|
|
|
let pubkey = PubKeys.getDefaultKey();
|
|
|
|
let privkey = PrivKeys.get(pubkey.privateKeyUri);
|
|
|
|
return Svc.Crypto.verifyPassphrase(
|
2009-09-03 21:11:32 -07:00
|
|
|
privkey.payload.keyData, this.passphrase,
|
2009-07-23 17:12:32 -07:00
|
|
|
privkey.payload.salt, privkey.payload.iv
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
// this means no keys are present (or there's a network error)
|
|
|
|
return true;
|
2009-07-21 21:05:23 -07:00
|
|
|
}
|
|
|
|
}))(),
|
2008-07-11 17:40:52 -07:00
|
|
|
|
2009-07-21 14:31:16 -07:00
|
|
|
changePassphrase: function WeaveSvc_changePassphrase(newphrase)
|
|
|
|
this._catch(this._notify("changepph", "", function() {
|
2009-07-21 16:54:46 -07:00
|
|
|
let pubkey = PubKeys.getDefaultKey();
|
|
|
|
let privkey = PrivKeys.get(pubkey.privateKeyUri);
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-21 16:54:46 -07:00
|
|
|
/* Re-encrypt with new passphrase.
|
|
|
|
* FIXME: verifyPassphrase first!
|
|
|
|
*/
|
|
|
|
let newkey = Svc.Crypto.rewrapPrivateKey(privkey.payload.keyData,
|
|
|
|
this.passphrase, privkey.payload.salt,
|
|
|
|
privkey.payload.iv, newphrase);
|
|
|
|
privkey.payload.keyData = newkey;
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-08-26 15:32:46 -07:00
|
|
|
let resp = new Resource(privkey.uri).put(privkey.serialize());
|
|
|
|
if (!resp.success)
|
|
|
|
throw resp;
|
|
|
|
|
2009-07-21 16:54:46 -07:00
|
|
|
this.passphrase = newphrase;
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-21 16:54:46 -07:00
|
|
|
return true;
|
2009-07-21 14:31:16 -07:00
|
|
|
}))(),
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-21 20:59:02 -07:00
|
|
|
changePassword: function WeaveSvc_changePassword(newpass)
|
|
|
|
this._catch(this._notify("changepwd", "", function() {
|
2009-08-26 21:20:08 -07:00
|
|
|
let url = this.baseURL + '1/' + this.username + "/password";
|
2009-07-21 20:59:02 -07:00
|
|
|
let res = new Weave.Resource(url);
|
2009-08-25 17:06:13 -07:00
|
|
|
let resp = res.post(newpass);
|
2009-08-26 15:32:46 -07:00
|
|
|
if (resp.status != 200) {
|
2009-07-21 20:59:02 -07:00
|
|
|
this._log.info("Password change failed: " + resp);
|
|
|
|
throw "Could not change password";
|
|
|
|
}
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-21 20:59:02 -07:00
|
|
|
this.password = newpass;
|
|
|
|
return true;
|
|
|
|
}))(),
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-21 14:31:16 -07:00
|
|
|
resetPassphrase: function WeaveSvc_resetPassphrase(newphrase)
|
|
|
|
this._catch(this._notify("resetpph", "", function() {
|
|
|
|
/* Make remote commands ready so we have a list of clients beforehand */
|
|
|
|
this.prepCommand("logout", []);
|
|
|
|
let clientsBackup = Clients._store.clients;
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-21 14:31:16 -07:00
|
|
|
/* Wipe */
|
|
|
|
this.wipeServer();
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-21 14:31:16 -07:00
|
|
|
/* Set remote commands before syncing */
|
|
|
|
Clients._store.clients = clientsBackup;
|
|
|
|
let username = this.username;
|
|
|
|
let password = this.password;
|
|
|
|
this.logout();
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-21 14:31:16 -07:00
|
|
|
/* Set this so UI is updated on next run */
|
|
|
|
this.passphrase = newphrase;
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-21 14:31:16 -07:00
|
|
|
/* Login in sync: this also generates new keys */
|
|
|
|
this.login(username, password, newphrase);
|
|
|
|
this.sync(true);
|
|
|
|
return true;
|
|
|
|
}))(),
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-08-19 20:27:22 -07:00
|
|
|
_autoConnectAttempts: 0,
|
|
|
|
_autoConnect: function WeaveSvc__attemptAutoConnect() {
|
|
|
|
try {
|
|
|
|
if (!this.username || !this.password || !this.passphrase)
|
|
|
|
return;
|
2009-08-19 20:27:22 -07:00
|
|
|
|
2009-08-19 20:27:22 -07:00
|
|
|
let failureReason;
|
|
|
|
if (Svc.IO.offline)
|
|
|
|
failureReason = "Application is offline";
|
|
|
|
else if (this.login()) {
|
|
|
|
this.syncOnIdle();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
failureReason = this.detailedStatus.sync;
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
failureReason = ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.debug("Autoconnect failed: " + failureReason);
|
|
|
|
|
|
|
|
let listener = new Utils.EventListener(Utils.bind2(this,
|
|
|
|
function WeaveSvc__autoConnectCallback(timer) {
|
|
|
|
this._autoConnectTimer = null;
|
|
|
|
this._autoConnect();
|
|
|
|
}));
|
|
|
|
this._autoConnectTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
|
|
|
|
// back off slowly, with some random fuzz to avoid repeated collisions
|
2009-08-19 22:53:04 -07:00
|
|
|
let interval = Math.floor(Math.random() * SCHEDULED_SYNC_INTERVAL +
|
2009-08-19 20:27:22 -07:00
|
|
|
SCHEDULED_SYNC_INTERVAL * this._autoConnectAttempts);
|
|
|
|
this._autoConnectAttempts++;
|
|
|
|
this._autoConnectTimer.initWithCallback(listener, interval,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
this._log.debug("Scheduling next autoconnect attempt in " +
|
|
|
|
interval / 1000 + " seconds.");
|
|
|
|
},
|
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
persistLogin: function persistLogin() {
|
|
|
|
// Canceled master password prompt can prevent these from succeeding
|
|
|
|
try {
|
|
|
|
ID.get("WeaveID").persist();
|
|
|
|
ID.get("WeaveCryptoID").persist();
|
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
},
|
|
|
|
|
2009-06-06 11:52:54 -07:00
|
|
|
login: function WeaveSvc_login(username, password, passphrase)
|
|
|
|
this._catch(this._lock(this._notify("login", "", function() {
|
2008-12-27 12:15:45 -08:00
|
|
|
this._loggedIn = false;
|
2009-03-24 19:23:53 -07:00
|
|
|
this._detailedStatus = new StatusRecord();
|
2009-08-19 20:27:22 -07:00
|
|
|
if (Svc.IO.offline)
|
|
|
|
throw "Application is offline, login should not be called";
|
2008-03-30 08:40:23 -07:00
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
if (username != null)
|
2009-06-06 11:52:54 -07:00
|
|
|
this.username = username;
|
2009-09-03 21:11:32 -07:00
|
|
|
if (password != null)
|
|
|
|
this.password = password;
|
|
|
|
if (passphrase != null)
|
|
|
|
this.passphrase = passphrase;
|
2009-03-11 18:29:18 -07:00
|
|
|
|
|
|
|
if (!this.username) {
|
2009-03-27 20:57:38 -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-27 20:57:38 -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);
|
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
if (!this._verifyLogin()) {
|
2009-08-19 20:27:22 -07:00
|
|
|
// verifyLogin sets the failure states here
|
|
|
|
throw "Login failed: " + this.detailedStatus.sync;
|
2009-03-11 18:29:18 -07:00
|
|
|
}
|
|
|
|
|
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-07-22 20:48:41 -07:00
|
|
|
this._checkSyncStatus();
|
2009-08-19 20:27:22 -07:00
|
|
|
if (this._autoConnectTimer) {
|
|
|
|
this._autoConnectTimer.cancel();
|
|
|
|
this._autoConnectTimer = null;
|
|
|
|
}
|
2009-03-12 13:01:04 -07:00
|
|
|
|
2009-06-06 11:52:54 -07:00
|
|
|
return true;
|
|
|
|
})))(),
|
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 = {};
|
2009-03-10 04:15:52 -07:00
|
|
|
|
|
|
|
// Cancel the sync timer now that we're logged out
|
2009-07-22 20:48:41 -07:00
|
|
|
this._checkSyncStatus();
|
2009-03-10 04:15:52 -07:00
|
|
|
|
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-08-18 20:03:11 -07:00
|
|
|
_errorStr: function WeaveSvc__errorStr(code) {
|
2009-08-26 15:32:46 -07:00
|
|
|
switch (code.toString()) {
|
2009-08-26 16:44:30 -07:00
|
|
|
case "1":
|
|
|
|
return "illegal-method";
|
|
|
|
case "2":
|
|
|
|
return "invalid-captcha";
|
|
|
|
case "3":
|
|
|
|
return "invalid-username";
|
|
|
|
case "4":
|
|
|
|
return "cannot-overwrite-resource";
|
|
|
|
case "5":
|
|
|
|
return "userid-mismatch";
|
|
|
|
case "6":
|
|
|
|
return "json-parse-failure";
|
|
|
|
case "7":
|
|
|
|
return "invalid-password";
|
|
|
|
case "8":
|
|
|
|
return "invalid-record";
|
|
|
|
case "9":
|
|
|
|
return "weak-password";
|
2009-08-18 20:03:11 -07:00
|
|
|
default:
|
|
|
|
return "generic-server-error";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
checkUsername: function WeaveSvc_checkUsername(username) {
|
2009-08-26 15:01:28 -07:00
|
|
|
let url = this.baseURL + "1/" + username;
|
2009-08-18 20:03:11 -07:00
|
|
|
let res = new Resource(url);
|
|
|
|
res.authenticator = new NoOpAuthenticator();
|
|
|
|
|
2009-08-20 10:20:01 -07:00
|
|
|
let data = "";
|
|
|
|
try {
|
|
|
|
data = res.get();
|
2009-08-26 15:32:46 -07:00
|
|
|
if (data.status == 200 && data == "0")
|
2009-08-18 20:03:11 -07:00
|
|
|
return "available";
|
2009-08-20 10:20:01 -07:00
|
|
|
}
|
|
|
|
catch(ex) {}
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-08-20 10:20:01 -07:00
|
|
|
// Convert to the error string, or default to generic on exception
|
2009-08-18 20:03:11 -07:00
|
|
|
return this._errorStr(data);
|
|
|
|
},
|
|
|
|
|
2009-06-06 16:24:17 -07:00
|
|
|
createAccount: function WeaveSvc_createAccount(username, password, email,
|
2009-08-25 17:15:36 -07:00
|
|
|
captchaChallenge, captchaResponse)
|
2009-08-25 17:06:13 -07:00
|
|
|
{
|
|
|
|
let payload = JSON.stringify({
|
|
|
|
"password": password, "email": email,
|
2009-08-26 16:45:24 -07:00
|
|
|
"captcha-challenge": captchaChallenge,
|
|
|
|
"captcha-response": captchaResponse
|
2009-08-25 17:06:13 -07:00
|
|
|
});
|
|
|
|
|
2009-08-26 15:01:28 -07:00
|
|
|
let url = this.baseURL + '1/' + username;
|
2009-08-18 20:03:11 -07:00
|
|
|
let res = new Resource(url);
|
2009-06-06 16:24:17 -07:00
|
|
|
res.authenticator = new Weave.NoOpAuthenticator();
|
|
|
|
|
2009-08-26 15:32:46 -07:00
|
|
|
let error = "generic-server-error";
|
2009-06-06 16:24:17 -07:00
|
|
|
try {
|
2009-08-26 16:09:48 -07:00
|
|
|
let register = res.put(payload);
|
2009-08-26 15:32:46 -07:00
|
|
|
if (register.success) {
|
|
|
|
this._log.info("Account created: " + register);
|
|
|
|
return;
|
|
|
|
}
|
2009-08-26 16:09:48 -07:00
|
|
|
|
2009-08-26 15:32:46 -07:00
|
|
|
// Must have failed, so figure out the reason
|
2009-08-26 17:59:50 -07:00
|
|
|
if (register.status == 400)
|
|
|
|
error = this._errorStr(register);
|
2009-08-18 20:03:11 -07:00
|
|
|
}
|
2009-08-26 15:32:46 -07:00
|
|
|
catch(ex) {
|
|
|
|
this._log.warn("Failed to create account: " + ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
2009-03-27 00:46:10 -07:00
|
|
|
},
|
|
|
|
|
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() {
|
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-08-25 17:15:36 -07:00
|
|
|
let meta = Records.import(this.userURL + "/storage/meta/global");
|
2009-03-07 00:55:47 -08:00
|
|
|
|
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-08-18 20:03:11 -07:00
|
|
|
this._log.debug(["Weave Version:", WEAVE_VERSION, "Compatible:",
|
2009-07-22 18:49:07 -07:00
|
|
|
COMPATIBLE_VERSION, "Remote:", remoteVersion].join(" "));
|
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-07-22 18:49:07 -07:00
|
|
|
Svc.Version.compare(COMPATIBLE_VERSION, remoteVersion) > 0) {
|
2009-03-03 11:57:30 -08:00
|
|
|
|
2009-02-23 16:27:41 -08:00
|
|
|
// abort the server wipe if the GET status was anything other than 404 or 200
|
2009-08-26 15:32:46 -07:00
|
|
|
let status = Records.response.status;
|
2009-02-23 16:27:41 -08:00
|
|
|
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-06-06 11:53:23 -07:00
|
|
|
return false;
|
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-07-22 18:49:07 -07:00
|
|
|
if (Svc.Version.compare(COMPATIBLE_VERSION, remoteVersion) > 0)
|
|
|
|
this._log.info("Server data is older than what Weave supports, 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-06-06 11:53:23 -07:00
|
|
|
return false;
|
2009-03-18 13:22:40 -07:00
|
|
|
}
|
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-06-05 22:21:29 -07:00
|
|
|
this._freshStart();
|
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-07-22 18:49:07 -07:00
|
|
|
"upgrade (" + remoteVersion + " -> " + WEAVE_VERSION + ")");
|
2009-02-23 12:36:55 -08:00
|
|
|
|
2009-07-22 18:49:07 -07: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.");
|
2009-06-06 11:53:23 -07:00
|
|
|
return false;
|
2009-02-23 12:36:55 -08:00
|
|
|
|
|
|
|
} 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-06-05 22:21:22 -07:00
|
|
|
this.resetClient();
|
2009-02-26 22:36:14 -08:00
|
|
|
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-07-22 18:49:07 -07:00
|
|
|
this._updateRemoteVersion(meta);
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
2009-07-22 18:49:07 -07:00
|
|
|
// We didn't wipe the server and we're not out of date, so update remote
|
|
|
|
else
|
|
|
|
this._updateRemoteVersion(meta);
|
2009-02-11 23:54:20 -08:00
|
|
|
|
2008-11-19 16:18:35 -08:00
|
|
|
let needKeys = true;
|
2009-06-04 17:04:51 -07:00
|
|
|
let pubkey = PubKeys.getDefaultKey();
|
2009-04-13 14:39:29 -07:00
|
|
|
if (!pubkey)
|
|
|
|
this._log.debug("Could not get public key");
|
|
|
|
else if (pubkey.keyData == null)
|
|
|
|
this._log.debug("Public key has no key data");
|
|
|
|
else {
|
2008-11-19 16:18:35 -08:00
|
|
|
// make sure we have a matching privkey
|
2009-06-04 16:50:57 -07:00
|
|
|
let privkey = PrivKeys.get(pubkey.privateKeyUri);
|
2009-04-13 14:39:29 -07:00
|
|
|
if (!privkey)
|
|
|
|
this._log.debug("Could not get private key");
|
|
|
|
else if (privkey.keyData == null)
|
|
|
|
this._log.debug("Private key has no key data");
|
2009-06-06 11:53:23 -07:00
|
|
|
else
|
|
|
|
return true;
|
2008-11-19 16:18:35 -08:00
|
|
|
}
|
2009-04-13 14:39:29 -07:00
|
|
|
|
2009-02-11 23:54:20 -08:00
|
|
|
if (needKeys) {
|
2009-08-26 15:32:46 -07:00
|
|
|
if (PubKeys.response.status != 404 && PrivKeys.response.status != 404) {
|
2009-03-12 08:34:12 -07:00
|
|
|
this._log.warn("Couldn't download keys from server, aborting sync");
|
2009-08-26 15:32:46 -07:00
|
|
|
this._log.debug("PubKey HTTP status: " + PubKeys.response.status);
|
|
|
|
this._log.debug("PrivKey HTTP status: " + PrivKeys.response.status);
|
2009-03-24 19:23:53 -07:00
|
|
|
this._setSyncFailure(KEYS_DOWNLOAD_FAIL);
|
2009-06-06 11:53:23 -07:00
|
|
|
return false;
|
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-06-06 11:53:23 -07:00
|
|
|
return false;
|
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.");
|
2009-06-05 22:21:29 -07:00
|
|
|
this._freshStart();
|
2009-03-12 08:34:12 -07:00
|
|
|
this._log.info("Server data wiped to ensure consistency due to missing keys");
|
2009-02-19 04:09:55 -08:00
|
|
|
}
|
|
|
|
|
2009-07-14 19:04:32 -07:00
|
|
|
let passphrase = ID.get("WeaveCryptoID");
|
2009-08-21 17:32:41 -07:00
|
|
|
if (passphrase.password) {
|
2009-07-14 19:04:32 -07:00
|
|
|
let keys = PubKeys.createKeypair(passphrase, PubKeys.defaultKeyUri,
|
2008-11-19 16:18:35 -08:00
|
|
|
PrivKeys.defaultKeyUri);
|
|
|
|
try {
|
2009-07-16 23:13:58 -07:00
|
|
|
// Upload and cache the keypair
|
2009-06-04 17:09:32 -07:00
|
|
|
PubKeys.uploadKeypair(keys);
|
2009-07-16 23:13:58 -07:00
|
|
|
PubKeys.set(keys.pubkey.uri, keys.pubkey);
|
|
|
|
PrivKeys.set(keys.privkey.uri, keys.privkey);
|
2009-06-06 11:53:23 -07:00
|
|
|
return true;
|
2008-11-19 16:18:35 -08:00
|
|
|
} 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));
|
|
|
|
}
|
|
|
|
} 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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-06 11:53:23 -07:00
|
|
|
return false;
|
2008-11-19 16:18:35 -08:00
|
|
|
},
|
|
|
|
|
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
|
|
|
/**
|
2009-07-22 20:48:41 -07:00
|
|
|
* Determine if a sync should run.
|
2009-03-10 04:15:52 -07:00
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
|
2009-07-22 20:48:41 -07:00
|
|
|
return reason;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we should be syncing and schedule the next sync, if it's not scheduled
|
|
|
|
*/
|
|
|
|
_checkSyncStatus: function WeaveSvc__checkSyncStatus() {
|
|
|
|
// Should we be syncing now, if not, cancel any sync timers and return
|
|
|
|
if (this._checkSync()) {
|
2009-03-10 04:15:52 -07:00
|
|
|
if (this._syncTimer) {
|
|
|
|
this._syncTimer.cancel();
|
|
|
|
this._syncTimer = null;
|
|
|
|
}
|
2009-07-22 20:48:41 -07:00
|
|
|
|
|
|
|
try {
|
|
|
|
Svc.Idle.removeIdleObserver(this, IDLE_TIME);
|
|
|
|
} catch(e) {} // this throws if there isn't an observer, but that's fine
|
|
|
|
|
|
|
|
return;
|
2009-03-10 04:15:52 -07:00
|
|
|
}
|
2009-07-22 20:48:41 -07:00
|
|
|
|
|
|
|
// otherwise, schedule the sync
|
|
|
|
this._scheduleNextSync();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call sync() on an idle timer
|
2009-08-18 20:03:11 -07:00
|
|
|
*
|
2009-07-22 20:48:41 -07:00
|
|
|
*/
|
|
|
|
syncOnIdle: function WeaveSvc_syncOnIdle() {
|
|
|
|
this._log.debug("Idle timer created for sync, will sync after " +
|
|
|
|
IDLE_TIME + " seconds of inactivity.");
|
|
|
|
Svc.Idle.addIdleObserver(this, IDLE_TIME);
|
|
|
|
},
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-22 20:48:41 -07:00
|
|
|
/**
|
|
|
|
* Set a timer for the next sync
|
|
|
|
*/
|
|
|
|
_scheduleNextSync: function WeaveSvc__scheduleNextSync(interval) {
|
|
|
|
if (!interval)
|
|
|
|
interval = SCHEDULED_SYNC_INTERVAL;
|
|
|
|
|
|
|
|
// if there's an existing timer, cancel it and restart
|
|
|
|
if (this._syncTimer)
|
|
|
|
this._syncTimer.cancel();
|
|
|
|
else
|
2009-03-10 04:15:52 -07:00
|
|
|
this._syncTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-22 20:48:41 -07:00
|
|
|
let listener = new Utils.EventListener(Utils.bind2(this,
|
|
|
|
function WeaveSvc__scheduleNextSyncCallback(timer) {
|
|
|
|
this._syncTimer = null;
|
|
|
|
this.syncOnIdle();
|
|
|
|
}));
|
|
|
|
this._syncTimer.initWithCallback(listener, interval,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
this._log.debug("Next sync call in: " + this._syncTimer.delay / 1000 + " seconds.")
|
|
|
|
},
|
|
|
|
|
|
|
|
_serverErrors: 0,
|
|
|
|
/**
|
|
|
|
* Deal with sync errors appropriately
|
|
|
|
*/
|
|
|
|
_handleSyncError: function WeaveSvc__handleSyncError() {
|
|
|
|
let shouldBackoff = false;
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-22 20:48:41 -07:00
|
|
|
let err = Weave.Service.detailedStatus.sync;
|
|
|
|
// we'll assume the server is just borked a little for these
|
|
|
|
switch (err) {
|
|
|
|
case METARECORD_DOWNLOAD_FAIL:
|
|
|
|
case KEYS_DOWNLOAD_FAIL:
|
|
|
|
case KEYS_UPLOAD_FAIL:
|
|
|
|
shouldBackoff = true;
|
2009-03-10 04:15:52 -07:00
|
|
|
}
|
|
|
|
|
2009-07-22 20:48:41 -07:00
|
|
|
// specifcally handle 500, 502, 503, 504 errors
|
|
|
|
// xxxmpc: what else should be in this list?
|
2009-08-18 20:03:11 -07:00
|
|
|
// this is sort of pseudocode, need a way to get at the
|
2009-08-19 20:27:22 -07:00
|
|
|
if (!shouldBackoff) {
|
|
|
|
try {
|
2009-08-26 15:32:46 -07:00
|
|
|
shouldBackoff = Utils.checkStatus(Records.response.status, null,
|
|
|
|
[500, [502, 504]]);
|
2009-08-19 20:27:22 -07:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// if responseStatus throws, we have a network issue in play
|
|
|
|
shouldBackoff = true;
|
|
|
|
}
|
2009-07-22 20:48:41 -07:00
|
|
|
}
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-07-22 20:48:41 -07:00
|
|
|
// if this is a client error, do the next sync as normal and return
|
|
|
|
if (!shouldBackoff) {
|
|
|
|
this._scheduleNextSync();
|
2009-08-18 20:03:11 -07:00
|
|
|
return;
|
2009-07-22 20:48:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ok, something failed connecting to the server, rev the counter
|
|
|
|
this._serverErrors++;
|
|
|
|
|
|
|
|
// do nothing on the first failure, if we fail again we'll back off
|
|
|
|
if (this._serverErrors < 2) {
|
|
|
|
this._scheduleNextSync();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 30-60 minute backoff interval, increasing each time
|
|
|
|
const MINIMUM_BACKOFF_INTERVAL = 15 * 60 * 1000; // 15 minutes * >= 2
|
|
|
|
const MAXIMUM_BACKOFF_INTERVAL = 8 * 60 * 60 * 1000; // 8 hours
|
|
|
|
let backoffInterval = this._serverErrors *
|
|
|
|
(Math.floor(Math.random() * MINIMUM_BACKOFF_INTERVAL) +
|
|
|
|
MINIMUM_BACKOFF_INTERVAL);
|
|
|
|
backoffInterval = Math.min(backoffInterval, MAXIMUM_BACKOFF_INTERVAL);
|
|
|
|
this._scheduleNextSync(backoffInterval);
|
|
|
|
|
|
|
|
let d = new Date(Date.now() + backoffInterval);
|
|
|
|
this._log.config("Starting backoff, next sync at:" + d.toString());
|
2009-03-10 04:15:52 -07:00
|
|
|
},
|
|
|
|
|
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
|
|
|
*/
|
2009-06-06 16:23:58 -07:00
|
|
|
sync: function WeaveSvc_sync(fullSync)
|
|
|
|
this._catch(this._lock(this._notify("sync", "", function() {
|
2009-07-17 16:55:15 -07:00
|
|
|
|
2009-06-06 16:23:58 -07:00
|
|
|
fullSync = true; // not doing thresholds yet
|
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
|
|
|
|
2009-06-06 11:53:23 -07:00
|
|
|
if (!(this._remoteSetup()))
|
2008-11-19 16:18:35 -08:00
|
|
|
throw "aborting sync, remote setup failed";
|
2009-01-03 01:40:22 -08:00
|
|
|
|
2009-09-03 21:30:40 -07:00
|
|
|
// Figure out what the last modified time is for each collection
|
|
|
|
let info = new Resource(this.infoURL).get();
|
|
|
|
if (!info.success)
|
|
|
|
throw "aborting sync, failed to get collections";
|
|
|
|
|
|
|
|
// Convert the response to an object and read out the modified times
|
|
|
|
info = JSON.parse(info);
|
|
|
|
for each (let engine in [Clients].concat(Engines.getEnabled()))
|
|
|
|
engine.lastModified = info[engine.name] || 0;
|
|
|
|
|
2009-01-03 01:40:22 -08:00
|
|
|
this._log.debug("Refreshing client list");
|
2009-06-05 15:34:32 -07:00
|
|
|
Clients.sync();
|
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-06-05 22:21:25 -07:00
|
|
|
if (!(this.processCommands())) {
|
2009-03-24 19:23:53 -07:00
|
|
|
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
|
2009-06-06 11:53:23 -07:00
|
|
|
if (!(this._remoteSetup()))
|
2009-03-11 23:33:14 -07:00
|
|
|
throw "aborting sync, remote setup failed after processing commands";
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
// Always immediately push back the local client (now without commands)
|
2009-06-05 15:34:32 -07:00
|
|
|
Clients.sync();
|
2009-03-11 23:33:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2009-06-05 22:21:34 -07:00
|
|
|
if (!(this._syncEngine(engine))) {
|
2009-03-10 04:07:24 -07:00
|
|
|
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 {
|
2008-07-08 17:44:00 -07:00
|
|
|
this._syncError = false;
|
|
|
|
}
|
2009-06-06 16:23:58 -07:00
|
|
|
})))(),
|
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) {
|
2009-02-11 22:08:56 -08:00
|
|
|
try {
|
2009-06-05 15:34:32 -07:00
|
|
|
engine.sync();
|
2009-08-20 16:26:22 -07:00
|
|
|
return true;
|
2009-02-11 22:08:56 -08:00
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
catch(e) {
|
2009-08-19 18:22:22 -07:00
|
|
|
// maybe a 401, cluster update needed?
|
2009-09-03 21:11:32 -07:00
|
|
|
if (e.status == 401 && this._updateCluster())
|
2009-08-26 15:32:46 -07:00
|
|
|
return this._syncEngine(engine);
|
|
|
|
|
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-08-31 17:17:20 -07:00
|
|
|
this._log.debug(Utils.exceptionStr(e));
|
|
|
|
return 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() {
|
2009-06-05 22:21:22 -07:00
|
|
|
this.resetClient();
|
2009-02-26 22:36:14 -08:00
|
|
|
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-06-05 22:21:27 -07:00
|
|
|
this.wipeServer();
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2009-07-16 16:31:54 -07:00
|
|
|
// XXX Bug 504125 Wait a while after wiping so that the DELETEs replicate
|
|
|
|
Sync.sleep(2000);
|
|
|
|
|
2009-02-19 04:09:55 -08:00
|
|
|
this._log.debug("Uploading new metadata record");
|
2009-08-25 17:15:36 -07:00
|
|
|
meta = new WBORecord(this.userURL + "/storage/meta/global");
|
2009-02-19 04:09:55 -08:00
|
|
|
meta.payload.syncID = Clients.syncID;
|
2009-07-22 18:49:07 -07:00
|
|
|
this._updateRemoteVersion(meta);
|
|
|
|
},
|
|
|
|
|
|
|
|
_updateRemoteVersion: function WeaveSvc__updateRemoteVersion(meta) {
|
|
|
|
// Don't update if the remote version is already newer
|
|
|
|
if (Svc.Version.compare(meta.payload.storageVersion, WEAVE_VERSION) >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._log.debug("Setting meta payload storage version to " + WEAVE_VERSION);
|
|
|
|
meta.payload.storageVersion = WEAVE_VERSION;
|
2009-08-26 15:32:46 -07:00
|
|
|
let resp = new Resource(meta.uri).put(meta.serialize());
|
|
|
|
if (!resp.success)
|
|
|
|
throw resp;
|
2009-02-19 04:09:55 -08:00
|
|
|
},
|
|
|
|
|
2009-02-26 22:14:32 -08:00
|
|
|
/**
|
|
|
|
* Wipe all user data from the server.
|
2009-03-11 23:33:14 -07:00
|
|
|
*
|
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-06-05 22:21:27 -07:00
|
|
|
wipeServer: function WeaveSvc_wipeServer(engines)
|
|
|
|
this._catch(this._notify("wipe-server", "", function() {
|
2009-02-26 22:14:32 -08:00
|
|
|
// Grab all the collections for the user
|
2009-09-03 21:30:40 -07:00
|
|
|
let res = new Resource(this.infoURL);
|
2009-06-04 10:17:27 -07:00
|
|
|
res.get();
|
2009-02-26 22:14:32 -08:00
|
|
|
|
|
|
|
// Get the array of collections and delete each one
|
2009-03-31 12:09:38 -07:00
|
|
|
let allCollections = JSON.parse(res.data);
|
2009-08-25 17:43:40 -07:00
|
|
|
for (let name in allCollections) {
|
2009-02-26 22:14:32 -08:00
|
|
|
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-08-25 17:43:40 -07:00
|
|
|
new Resource(this.userURL + "/storage/" + name).delete();
|
2009-02-26 22:14:32 -08:00
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
this._log.debug("Exception on wipe of '" + name + "': " + Utils.exceptionStr(ex));
|
|
|
|
}
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
2009-06-05 22:21:27 -07: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.
|
|
|
|
*
|
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-06-05 22:21:20 -07:00
|
|
|
wipeClient: function WeaveSvc_wipeClient(engines)
|
|
|
|
this._catch(this._notify("wipe-client", "", function() {
|
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
|
2009-06-05 22:21:18 -07:00
|
|
|
this.resetService();
|
2009-03-20 00:13:16 -07:00
|
|
|
|
|
|
|
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-06-05 15:34:35 -07:00
|
|
|
engine.wipeClient();
|
2009-06-05 22:21:20 -07:00
|
|
|
}))(),
|
2009-03-11 23:33:14 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wipe all remote user data by wiping the server then telling each remote
|
|
|
|
* client to wipe itself.
|
|
|
|
*
|
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-06-05 22:21:24 -07:00
|
|
|
wipeRemote: function WeaveSvc_wipeRemote(engines)
|
|
|
|
this._catch(this._notify("wipe-remote", "", function() {
|
2009-03-11 23:33:14 -07:00
|
|
|
// Clear out any server data
|
2009-06-05 22:21:24 -07:00
|
|
|
//this.wipeServer(engines);
|
2009-03-20 00:13:16 -07:00
|
|
|
|
|
|
|
// 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", []);
|
2009-06-05 22:21:24 -07:00
|
|
|
}))(),
|
2009-03-11 23:33:14 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset local service information like logs, sync times, caches.
|
|
|
|
*/
|
2009-06-05 22:21:18 -07:00
|
|
|
resetService: function WeaveSvc_resetService()
|
|
|
|
this._catch(this._notify("reset-service", "", function() {
|
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-06-05 22:21:18 -07:00
|
|
|
}))(),
|
2009-03-11 23:33:14 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the client by getting rid of any local server data and client data.
|
|
|
|
*
|
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-06-05 22:21:22 -07:00
|
|
|
resetClient: function WeaveSvc_resetClient(engines)
|
|
|
|
this._catch(this._notify("reset-client", "", function() {
|
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
|
2009-06-05 22:21:18 -07:00
|
|
|
this.resetService();
|
2009-03-20 00:13:16 -07:00
|
|
|
|
|
|
|
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-06-05 15:34:34 -07:00
|
|
|
engine.resetClient();
|
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-06-05 22:21:22 -07:00
|
|
|
}))(),
|
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"],
|
2009-07-21 14:31:16 -07:00
|
|
|
["logout", 0, "Log out client"],
|
2009-03-03 15:29:35 -08:00
|
|
|
].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.
|
|
|
|
*
|
|
|
|
* @return False to abort sync
|
|
|
|
*/
|
2009-06-05 22:21:25 -07:00
|
|
|
processCommands: function WeaveSvc_processCommands()
|
|
|
|
this._notify("process-commands", "", function() {
|
2009-03-11 23:33:14 -07:00
|
|
|
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-08-18 20:03:11 -07:00
|
|
|
|
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":
|
2009-06-05 22:21:22 -07:00
|
|
|
this.resetClient(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":
|
2009-06-05 22:21:20 -07:00
|
|
|
this.wipeClient(engines);
|
2009-03-11 23:33:14 -07:00
|
|
|
break;
|
2009-07-21 14:31:16 -07:00
|
|
|
case "logout":
|
|
|
|
this.logout();
|
|
|
|
return false;
|
2009-03-11 23:33:14 -07:00
|
|
|
default:
|
|
|
|
this._log.debug("Received an unknown command: " + command);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-05 22:21:25 -07:00
|
|
|
return true;
|
|
|
|
})(),
|
2009-03-11 23:33:14 -07:00
|
|
|
|
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
|
2009-03-31 12:09:38 -07:00
|
|
|
let jsonArgs = JSON.stringify(args);
|
2009-03-03 15:29:35 -08:00
|
|
|
let notDupe = function(action) action.command != command ||
|
2009-03-31 12:09:38 -07:00
|
|
|
JSON.stringify(action.args) != jsonArgs;
|
2009-03-03 15:29:35 -08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|