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;
|
|
|
|
|
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
|
|
|
|
|
2009-08-19 18:22:22 -07:00
|
|
|
// How long before refreshing the cluster
|
2009-09-25 15:05:09 -07:00
|
|
|
const CLUSTER_BACKOFF = 5 * 60 * 1000; // 5 minutes
|
2009-08-19 18:22:22 -07:00
|
|
|
|
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-10-07 10:47:43 -07:00
|
|
|
Cu.import("resource://weave/status.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);
|
2009-10-07 10:47:43 -07:00
|
|
|
Cu.import("resource://weave/status.js", Weave);
|
2008-03-31 07:20:09 -07:00
|
|
|
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-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-23 17:11:05 -07:00
|
|
|
Cu.import("resource://weave/engines/prefs.js", Weave);
|
2009-04-07 14:45:41 -07:00
|
|
|
Cu.import("resource://weave/engines/passwords.js", Weave);
|
|
|
|
Cu.import("resource://weave/engines/tabs.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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2008-07-08 18:37:14 -07:00
|
|
|
// object for caching public and private keys
|
|
|
|
_keyPair: {},
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
get username() {
|
2009-12-02 18:25:18 -08:00
|
|
|
return Svc.Prefs.get("username", "").toLowerCase();
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
set username(value) {
|
2009-12-02 18:25:18 -08:00
|
|
|
if (value) {
|
|
|
|
// Make sure all uses of this new username is lowercase
|
|
|
|
value = value.toLowerCase();
|
2009-02-10 00:56:42 -08:00
|
|
|
Svc.Prefs.set("username", value);
|
2009-12-02 18:25:18 -08:00
|
|
|
}
|
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
|
2009-09-21 17:13:41 -07:00
|
|
|
this._updateCachedURLs();
|
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
|
|
|
|
2009-09-21 17:13:41 -07:00
|
|
|
get serverURL() Svc.Prefs.get("serverURL"),
|
2009-09-22 01:00:43 -07:00
|
|
|
set serverURL(value) {
|
|
|
|
// Only do work if it's actually changing
|
|
|
|
if (value == this.serverURL)
|
|
|
|
return;
|
2009-11-20 14:34:20 -08:00
|
|
|
|
2009-09-22 01:00:43 -07:00
|
|
|
// A new server most likely uses a different cluster, so clear that
|
|
|
|
Svc.Prefs.set("serverURL", value);
|
|
|
|
Svc.Prefs.reset("clusterURL");
|
|
|
|
},
|
2009-08-26 15:01:28 -07:00
|
|
|
|
2009-09-21 18:03:56 -07:00
|
|
|
get clusterURL() Svc.Prefs.get("clusterURL", ""),
|
2009-02-10 00:56:42 -08:00
|
|
|
set clusterURL(value) {
|
|
|
|
Svc.Prefs.set("clusterURL", value);
|
2009-09-21 17:13:41 -07:00
|
|
|
this._updateCachedURLs();
|
2008-11-06 17:37:17 -08:00
|
|
|
},
|
|
|
|
|
2009-09-21 17:13:41 -07:00
|
|
|
get miscAPI() {
|
|
|
|
// Append to the serverURL if it's a relative fragment
|
|
|
|
let misc = Svc.Prefs.get("miscURL");
|
|
|
|
if (misc.indexOf(":") == -1)
|
|
|
|
misc = this.serverURL + misc;
|
|
|
|
return misc + "1/";
|
|
|
|
},
|
2009-08-25 17:15:36 -07:00
|
|
|
|
2009-09-21 17:13:41 -07:00
|
|
|
get userAPI() {
|
|
|
|
// Append to the serverURL if it's a relative fragment
|
|
|
|
let user = Svc.Prefs.get("userURL");
|
|
|
|
if (user.indexOf(":") == -1)
|
|
|
|
user = this.serverURL + user;
|
|
|
|
return user + "1/";
|
|
|
|
},
|
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-09-25 11:46:29 -07:00
|
|
|
// nextSync is in milliseconds, but prefs can't hold that much
|
|
|
|
get nextSync() Svc.Prefs.get("nextSync", 0) * 1000,
|
|
|
|
set nextSync(value) Svc.Prefs.set("nextSync", Math.floor(value / 1000)),
|
|
|
|
|
2009-10-13 11:56:46 -07:00
|
|
|
get syncInterval() {
|
|
|
|
// If we have a partial download, sync sooner if we're not mobile
|
|
|
|
if (Status.partial && Clients.clientType != "mobile")
|
|
|
|
return PARTIAL_DATA_SYNC;
|
|
|
|
return Svc.Prefs.get("syncInterval", MULTI_MOBILE_SYNC);
|
|
|
|
},
|
2009-09-25 15:41:27 -07:00
|
|
|
set syncInterval(value) Svc.Prefs.set("syncInterval", value),
|
|
|
|
|
2009-11-09 09:57:58 -08:00
|
|
|
get syncThreshold() Svc.Prefs.get("syncThreshold", SINGLE_USER_THRESHOLD),
|
2009-11-09 13:30:37 -08:00
|
|
|
set syncThreshold(value) Svc.Prefs.set("syncThreshold", value),
|
2009-11-09 09:57:58 -08:00
|
|
|
|
|
|
|
get globalScore() Svc.Prefs.get("globalScore", 0),
|
|
|
|
set globalScore(value) Svc.Prefs.set("globalScore", value),
|
|
|
|
|
2009-09-25 15:05:09 -07:00
|
|
|
get numClients() Svc.Prefs.get("numClients", 0),
|
|
|
|
set numClients(value) Svc.Prefs.set("numClients", value),
|
|
|
|
|
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-09-21 17:13:41 -07:00
|
|
|
_updateCachedURLs: function _updateCachedURLs() {
|
2009-09-21 18:03:56 -07:00
|
|
|
// Nothing to cache yet if we don't have the building blocks
|
|
|
|
if (this.clusterURL == "" || this.username == "")
|
|
|
|
return;
|
|
|
|
|
2009-11-10 15:24:31 -08:00
|
|
|
let storageAPI = this.clusterURL + Svc.Prefs.get("storageAPI") + "/";
|
2009-09-21 17:13:41 -07:00
|
|
|
let userBase = storageAPI + this.username + "/";
|
|
|
|
this._log.debug("Caching URLs under storage user base: " + userBase);
|
|
|
|
|
|
|
|
// Generate and cache various URLs under the storage API for this user
|
|
|
|
this.infoURL = userBase + "info/collections";
|
|
|
|
this.storageURL = userBase + "storage/";
|
|
|
|
this.metaURL = this.storageURL + "meta/global";
|
|
|
|
PubKeys.defaultKeyUri = this.storageURL + "keys/pubkey";
|
|
|
|
PrivKeys.defaultKeyUri = this.storageURL + "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-09-25 08:13:12 -07:00
|
|
|
/**
|
|
|
|
* Prepare to initialize the rest of Weave after waiting a little bit
|
|
|
|
*/
|
|
|
|
onStartup: function onStartup() {
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.service = STATUS_DELAYED;
|
2009-09-25 08:13:12 -07:00
|
|
|
|
|
|
|
// Figure out how many seconds to delay loading Weave based on the app
|
|
|
|
let wait = 0;
|
|
|
|
switch (Svc.AppInfo.ID) {
|
|
|
|
case FIREFOX_ID:
|
|
|
|
// Add one second delay for each tab in every window
|
|
|
|
let enum = Svc.WinMediator.getEnumerator("navigator:browser");
|
|
|
|
while (enum.hasMoreElements())
|
|
|
|
wait += enum.getNext().gBrowser.mTabs.length;
|
2009-12-10 12:20:16 -08:00
|
|
|
break;
|
2009-09-25 08:13:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we wait a little but but not too long in the worst case
|
|
|
|
wait = Math.ceil(Math.max(5, Math.min(20, wait)));
|
|
|
|
|
|
|
|
this._initLogs();
|
|
|
|
this._log.info("Loading Weave " + WEAVE_VERSION + " in " + wait + " sec.");
|
|
|
|
Utils.delay(this._onStartup, wait * 1000, this, "_startupTimer");
|
|
|
|
},
|
|
|
|
|
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-09-25 08:13:12 -07:00
|
|
|
_onStartup: function _onStartup() {
|
2009-10-07 10:47:55 -07:00
|
|
|
Status.service = STATUS_OK;
|
2009-11-04 15:27:08 -08:00
|
|
|
this.enabled = true;
|
2009-10-07 10:47:55 -07:00
|
|
|
|
2009-04-08 12:39:14 -07:00
|
|
|
this._registerEngines();
|
2009-03-24 19:23:53 -07: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
|
|
|
|
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);
|
2009-09-15 18:38:52 -07:00
|
|
|
Svc.Observer.addObserver(this, "weave:service:backoff:interval", true);
|
2009-11-09 09:57:58 -08:00
|
|
|
Svc.Observer.addObserver(this, "weave:engine:score:updated", 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));
|
|
|
|
|
2009-09-21 17:13:41 -07:00
|
|
|
this._updateCachedURLs();
|
2008-11-06 17:37:17 -08:00
|
|
|
|
2009-12-15 14:21:13 -08:00
|
|
|
// Send an event now that Weave service is ready
|
|
|
|
Svc.Obs.notify("weave:service:ready");
|
|
|
|
|
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);
|
|
|
|
|
2009-11-04 17:01:49 -08:00
|
|
|
let maxSize = 65536; // 64 * 1024 (64KB)
|
|
|
|
this._debugApp = new Log4Moz.RotatingFileAppender(verbose, formatter, maxSize);
|
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-12-01 11:49:23 -08:00
|
|
|
engines = ["Form", "History", "Password", "Tab"];
|
2009-04-14 09:05:30 -07:00
|
|
|
break;
|
2009-04-08 12:39:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2009-03-10 04:30:36 -07:00
|
|
|
case "network:offline-status-changed":
|
|
|
|
// Whether online or offline, we'll reschedule syncs
|
2009-11-16 17:11:10 -08:00
|
|
|
this._log.trace("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
|
2009-11-16 17:11:10 -08:00
|
|
|
this._log.trace("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();
|
2009-09-15 18:38:52 -07:00
|
|
|
this._syncErrors = 0;
|
|
|
|
break;
|
|
|
|
case "weave:service:backoff:interval":
|
|
|
|
let interval = data + Math.random() * data * 0.25; // required backoff + up to 25%
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.backoffInterval = interval;
|
|
|
|
Status.minimumNextSync = Date.now() + data;
|
2009-07-22 20:48:41 -07:00
|
|
|
break;
|
2009-11-09 09:57:58 -08:00
|
|
|
case "weave:engine:score:updated":
|
|
|
|
this._handleScoreUpdate();
|
|
|
|
break;
|
2009-07-22 20:48:41 -07:00
|
|
|
case "idle":
|
2009-07-22 21:40:18 -07:00
|
|
|
this._log.trace("Idle time hit, trying to sync");
|
2009-12-03 12:03:39 -08:00
|
|
|
Svc.Idle.removeIdleObserver(this, this._idleTime);
|
|
|
|
this._idleTime = 0;
|
2009-09-25 17:28:48 -07:00
|
|
|
Utils.delay(function() this.sync(false), 0, this);
|
2009-07-22 20:48:41 -07:00
|
|
|
break;
|
2008-06-23 16:10:31 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-11-09 09:57:58 -08:00
|
|
|
_handleScoreUpdate: function WeaveSvc__handleScoreUpdate() {
|
|
|
|
const SCORE_UPDATE_DELAY = 3000;
|
2009-11-11 15:19:00 -08:00
|
|
|
Utils.delay(this._calculateScore, SCORE_UPDATE_DELAY, this, "_scoreTimer");
|
2009-11-09 09:57:58 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_calculateScore: function WeaveSvc_calculateScoreAndDoStuff() {
|
|
|
|
var engines = Engines.getEnabled();
|
|
|
|
for (let i = 0;i < engines.length;i++) {
|
2009-11-16 17:11:10 -08:00
|
|
|
this._log.trace(engines[i].name + ": score: " + engines[i].score);
|
2009-11-09 09:57:58 -08:00
|
|
|
this.globalScore += engines[i].score;
|
2009-11-11 15:19:00 -08:00
|
|
|
engines[i]._tracker.resetScore();
|
2009-11-09 09:57:58 -08:00
|
|
|
}
|
|
|
|
|
2009-11-16 17:11:10 -08:00
|
|
|
this._log.trace("Global score updated: " + this.globalScore);
|
2009-11-30 14:03:59 -08:00
|
|
|
this._checkSyncStatus();
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
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-21 17:13:41 -07:00
|
|
|
let res = new Resource(this.userAPI + 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-09-21 17:13:41 -07:00
|
|
|
return this.serverURL;
|
2009-08-26 15:32:46 -07:00
|
|
|
case 0:
|
2009-08-19 20:27:22 -07:00
|
|
|
case 200:
|
2009-09-29 18:33:41 -07:00
|
|
|
if (node == "null")
|
|
|
|
node = null;
|
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");
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.login = LOGIN_FAILED_NETWORK_ERROR;
|
2009-08-20 12:14:34 -07:00
|
|
|
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() {
|
2009-09-21 17:13:41 -07:00
|
|
|
// Make sure we didn't get some unexpected response for the cluster
|
2009-09-03 21:11:32 -07:00
|
|
|
let cluster = this._findCluster();
|
2009-09-29 18:33:41 -07:00
|
|
|
this._log.debug("cluster value = " + cluster);
|
2009-09-21 17:13:41 -07:00
|
|
|
if (cluster == null)
|
|
|
|
return false;
|
2009-08-19 20:27:22 -07:00
|
|
|
|
2009-09-21 17:13:41 -07:00
|
|
|
// Don't update stuff if we already have the right cluster
|
|
|
|
if (cluster == this.clusterURL)
|
|
|
|
return false;
|
2009-06-06 16:24:14 -07:00
|
|
|
|
2009-09-21 17:13:41 -07:00
|
|
|
this.clusterURL = cluster;
|
|
|
|
return true;
|
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-09-28 16:13:42 -07:00
|
|
|
this._notify("verify-login", "", function() {
|
2009-09-21 18:03:56 -07:00
|
|
|
// Make sure we have a cluster to verify against
|
2009-11-20 14:34:20 -08:00
|
|
|
// this is a little weird, if we don't get a node we pretend
|
2009-09-29 18:33:41 -07:00
|
|
|
// to succeed, since that probably means we just don't have storage
|
|
|
|
if (this.clusterURL == "" && !this._setCluster()) {
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.sync = NO_SYNC_NODE_FOUND;
|
2009-09-29 18:33:41 -07:00
|
|
|
Svc.Observer.notifyObservers(null, "weave:service:sync:delayed", "");
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-21 18:03:56 -07:00
|
|
|
|
2009-08-19 18:22:22 -07:00
|
|
|
try {
|
2009-09-28 16:13:42 -07:00
|
|
|
let test = new Resource(this.infoURL).get();
|
2009-08-26 15:32:46 -07:00
|
|
|
switch (test.status) {
|
2009-08-19 20:27:22 -07:00
|
|
|
case 200:
|
2009-09-28 16:13:42 -07:00
|
|
|
// The user is authenticated, so check the passphrase now
|
2009-09-03 21:11:32 -07:00
|
|
|
if (!this._verifyPassphrase()) {
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.login = LOGIN_FAILED_INVALID_PASSPHRASE;
|
2009-08-19 20:27:22 -07:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-28 16:13:42 -07:00
|
|
|
|
|
|
|
// Username/password and passphrase all verified
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.login = LOGIN_SUCCEEDED;
|
2009-08-19 20:27:22 -07:00
|
|
|
return true;
|
2009-09-28 16:13:42 -07:00
|
|
|
|
2009-08-19 20:27:22 -07:00
|
|
|
case 401:
|
2009-09-21 17:34:19 -07:00
|
|
|
case 404:
|
|
|
|
// Check that we're verifying with the correct cluster
|
2009-09-21 18:03:56 -07:00
|
|
|
if (this._setCluster())
|
2009-09-03 21:11:32 -07:00
|
|
|
return this._verifyLogin();
|
2009-08-19 20:27:22 -07:00
|
|
|
|
2009-09-21 17:34:19 -07:00
|
|
|
// We must have the right cluster, but the server doesn't expect us
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.login = LOGIN_FAILED_LOGIN_REJECTED;
|
2009-08-19 20:27:22 -07:00
|
|
|
return false;
|
2009-09-28 16:13:42 -07:00
|
|
|
|
2009-08-19 20:27:22 -07:00
|
|
|
default:
|
2009-09-28 16:13:42 -07:00
|
|
|
// Server didn't respond with something that we expected
|
|
|
|
this._checkServerError(test);
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.login = LOGIN_FAILED_SERVER_ERROR;
|
2009-09-28 16:13:42 -07:00
|
|
|
return false;
|
2009-08-19 18:22:22 -07:00
|
|
|
}
|
2009-09-28 16:13:42 -07:00
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
// Must have failed on some network issue
|
|
|
|
this._log.debug("verifyLogin failed: " + Utils.exceptionStr(ex));
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.login = LOGIN_FAILED_NETWORK_ERROR;
|
2009-09-28 16:13:42 -07:00
|
|
|
return false;
|
2009-08-19 18:22:22 -07:00
|
|
|
}
|
2009-09-28 16:13:42 -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-11-18 14:42:13 -08:00
|
|
|
// Don't allow empty/missing passphrase
|
|
|
|
if (!this.passphrase)
|
|
|
|
return false;
|
|
|
|
|
2009-07-23 17:12:32 -07:00
|
|
|
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-09-10 11:05:13 -07:00
|
|
|
let resp = new Resource(privkey.uri).put(privkey);
|
2009-08-26 15:32:46 -07:00
|
|
|
if (!resp.success)
|
|
|
|
throw resp;
|
|
|
|
|
2009-11-09 12:56:14 -08:00
|
|
|
// Save the new passphrase to the login manager for it to sync
|
2009-07-21 16:54:46 -07:00
|
|
|
this.passphrase = newphrase;
|
2009-11-09 12:56:14 -08:00
|
|
|
this.persistLogin();
|
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)
|
2009-09-28 16:28:38 -07:00
|
|
|
this._notify("changepwd", "", function() {
|
2009-09-21 17:13:41 -07:00
|
|
|
let url = this.userAPI + this.username + "/password";
|
2009-09-28 16:28:38 -07:00
|
|
|
try {
|
|
|
|
let resp = new Resource(url).post(newpass);
|
|
|
|
if (resp.status != 200) {
|
|
|
|
this._log.debug("Password change failed: " + resp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
// Must have failed on some network issue
|
|
|
|
this._log.debug("changePassword failed: " + Utils.exceptionStr(ex));
|
|
|
|
return false;
|
2009-07-21 20:59:02 -07:00
|
|
|
}
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-09-28 16:28:38 -07:00
|
|
|
// Save the new password for requests and login manager
|
2009-07-21 20:59:02 -07:00
|
|
|
this.password = newpass;
|
2009-09-28 16:28:38 -07:00
|
|
|
this.persistLogin();
|
2009-07-21 20:59:02 -07:00
|
|
|
return true;
|
2009-09-28 16:28:38 -07:00
|
|
|
})(),
|
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() {
|
2009-11-12 12:44:33 -08:00
|
|
|
/* Make remote commands ready so we have a list of clients beforehand */
|
|
|
|
this.prepCommand("logout", []);
|
|
|
|
let clientsBackup = Clients._store.clients;
|
|
|
|
|
|
|
|
/* Wipe */
|
|
|
|
this.wipeServer();
|
|
|
|
PubKeys.clearCache();
|
|
|
|
PrivKeys.clearCache();
|
|
|
|
|
|
|
|
/* Set remote commands before syncing */
|
|
|
|
Clients._store.clients = clientsBackup;
|
|
|
|
let username = this.username;
|
|
|
|
let password = this.password;
|
|
|
|
this.logout();
|
|
|
|
|
|
|
|
/* Set this so UI is updated on next run */
|
2009-07-21 14:31:16 -07:00
|
|
|
this.passphrase = newphrase;
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-11-12 12:44:33 -08:00
|
|
|
/* Login in sync: this also generates new keys */
|
|
|
|
this.login(username, password, newphrase);
|
2009-07-21 14:31:16 -07:00
|
|
|
this.sync(true);
|
|
|
|
return true;
|
|
|
|
}))(),
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-09-21 23:52:00 -07:00
|
|
|
requestPasswordReset: function WeaveSvc_requestPasswordReset(username) {
|
2009-09-28 13:34:56 -07:00
|
|
|
let res = new Resource(Svc.Prefs.get("pwChangeURL"));
|
2009-09-21 23:52:00 -07:00
|
|
|
res.authenticator = new NoOpAuthenticator();
|
|
|
|
res.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
|
|
let ret = res.post('uid=' + username);
|
|
|
|
if (ret.indexOf("Further instructions have been sent") >= 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2009-09-25 11:46:29 -07:00
|
|
|
_autoConnect: let (attempts = 0) function _autoConnect() {
|
|
|
|
// Can't autoconnect if we're missing these values
|
|
|
|
if (!this.username || !this.password || !this.passphrase)
|
|
|
|
return;
|
2009-08-19 20:27:22 -07:00
|
|
|
|
2009-09-25 11:46:29 -07:00
|
|
|
// Nothing more to do on a successful login
|
|
|
|
if (this.login())
|
|
|
|
return;
|
2009-08-19 20:27:22 -07:00
|
|
|
|
2009-09-25 11:46:29 -07:00
|
|
|
// Something failed, so try again some time later
|
|
|
|
let interval = this._calculateBackoff(++attempts, 60 * 1000);
|
2009-10-07 10:47:43 -07:00
|
|
|
this._log.debug("Autoconnect failed: " + Status.login + "; retry in " +
|
2009-09-24 22:51:38 -07:00
|
|
|
Math.ceil(interval / 1000) + " sec.");
|
|
|
|
Utils.delay(function() this._autoConnect(), interval, this, "_autoTimer");
|
2009-08-19 20:27:22 -07:00
|
|
|
},
|
|
|
|
|
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-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-15 18:54:05 -07:00
|
|
|
if (username)
|
2009-06-06 11:52:54 -07:00
|
|
|
this.username = username;
|
2009-09-15 18:54:05 -07:00
|
|
|
if (password)
|
2009-09-03 21:11:32 -07:00
|
|
|
this.password = password;
|
2009-09-15 18:54:05 -07:00
|
|
|
if (passphrase)
|
2009-09-03 21:11:32 -07:00
|
|
|
this.passphrase = passphrase;
|
2009-03-11 18:29:18 -07:00
|
|
|
|
|
|
|
if (!this.username) {
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.login = LOGIN_FAILED_NO_USERNAME;
|
2009-03-11 18:29:18 -07:00
|
|
|
throw "No username set, login failed";
|
|
|
|
}
|
|
|
|
if (!this.password) {
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.login = LOGIN_FAILED_NO_PASSWORD;
|
2009-03-11 18:29:18 -07:00
|
|
|
throw "No password given or found in password manager";
|
|
|
|
}
|
2009-09-10 22:57:36 -07:00
|
|
|
this._log.info("Logging in user " + this.username);
|
2009-03-11 18:29:18 -07:00
|
|
|
|
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
|
2009-10-07 10:47:43 -07:00
|
|
|
throw "Login failed: " + Status.login;
|
2009-03-11 18:29:18 -07:00
|
|
|
}
|
|
|
|
|
2009-09-24 22:51:38 -07:00
|
|
|
// No need to try automatically connecting after a successful login
|
|
|
|
if (this._autoTimer)
|
|
|
|
this._autoTimer.clear();
|
|
|
|
|
2009-03-11 18:29:18 -07:00
|
|
|
this._loggedIn = true;
|
2009-09-15 18:38:52 -07:00
|
|
|
// Try starting the sync timer now that we're logged in
|
2009-07-22 20:48:41 -07:00
|
|
|
this._checkSyncStatus();
|
2009-11-12 13:49:41 -08:00
|
|
|
Svc.Prefs.set("autoconnect", true);
|
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() {
|
2009-09-30 15:16:56 -07:00
|
|
|
// No need to do anything if we're already logged out
|
|
|
|
if (!this._loggedIn)
|
|
|
|
return;
|
|
|
|
|
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-11-12 13:49:41 -08:00
|
|
|
Svc.Prefs.set("autoconnect", false);
|
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-09-21 17:13:41 -07:00
|
|
|
let url = this.userAPI + 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-09-21 17:13:41 -07:00
|
|
|
let url = this.userAPI + 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);
|
2009-10-23 00:05:41 -07:00
|
|
|
return null;
|
2009-08-26 15:32:46 -07:00
|
|
|
}
|
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-11-12 12:44:33 -08:00
|
|
|
let reset = false;
|
|
|
|
|
2009-11-16 17:11:10 -08:00
|
|
|
this._log.trace("Fetching global metadata record");
|
2009-09-21 17:13:41 -07:00
|
|
|
let meta = Records.import(this.metaURL);
|
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-09-15 18:38:52 -07:00
|
|
|
this._checkServerError(Records.response);
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.sync = 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-10-07 10:47:43 -07:00
|
|
|
Status.sync = DESKTOP_VERSION_OUT_OF_DATE;
|
2009-06-06 11:53:23 -07:00
|
|
|
return false;
|
2009-03-18 13:22:40 -07:00
|
|
|
}
|
2009-11-12 12:44:33 -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-10-07 10:47:43 -07:00
|
|
|
Status.sync = 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-11-12 12:44:33 -08: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-11-12 12:44:33 -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-11-12 12:44:33 -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-12-02 17:57:13 -08:00
|
|
|
|
|
|
|
// XXX Bug 531005 Wait long enough to allow potentially another concurrent
|
|
|
|
// sync to finish generating the keypair and uploading them
|
|
|
|
Sync.sleep(15000);
|
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-09-15 18:38:52 -07:00
|
|
|
this._checkServerError(PubKeys.response);
|
|
|
|
this._checkServerError(PrivKeys.response);
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.sync = 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-10-07 10:47:43 -07:00
|
|
|
Status.sync = NO_KEYS_NO_KEYGEN;
|
2009-06-06 11:53:23 -07:00
|
|
|
return false;
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
|
|
|
|
2009-11-12 12:44:33 -08:00
|
|
|
if (!reset) {
|
|
|
|
this._log.warn("Calling freshStart from !reset case.");
|
|
|
|
this._freshStart();
|
|
|
|
this._log.info("Server data wiped to ensure consistency due to missing keys");
|
|
|
|
}
|
|
|
|
|
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-10-07 10:47:43 -07:00
|
|
|
Status.sync = KEYS_UPLOAD_FAIL;
|
2008-11-19 16:18:35 -08:00
|
|
|
this._log.error("Could not upload keys: " + Utils.exceptionStr(e));
|
|
|
|
}
|
|
|
|
} else {
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.sync = 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: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-10-07 10:47:43 -07:00
|
|
|
else if (Status.minimumNextSync > Date.now())
|
2009-09-15 18:38:52 -07:00
|
|
|
reason = kSyncBackoffNotMet;
|
2009-03-10 04:15:52 -07:00
|
|
|
|
2009-07-22 20:48:41 -07:00
|
|
|
return reason;
|
|
|
|
},
|
|
|
|
|
2009-09-24 19:04:06 -07:00
|
|
|
/**
|
|
|
|
* Remove any timers/observers that might trigger a sync
|
|
|
|
*/
|
|
|
|
_clearSyncTriggers: function _clearSyncTriggers() {
|
|
|
|
// Clear out any scheduled syncs
|
2009-09-24 22:51:38 -07:00
|
|
|
if (this._syncTimer)
|
|
|
|
this._syncTimer.clear();
|
2009-09-24 19:04:06 -07:00
|
|
|
|
|
|
|
// Clear out a sync that's just waiting for idle if we happen to have one
|
|
|
|
try {
|
2009-12-03 12:03:39 -08:00
|
|
|
Svc.Idle.removeIdleObserver(this, this._idleTime);
|
|
|
|
this._idleTime = 0;
|
2009-09-24 19:04:06 -07:00
|
|
|
}
|
|
|
|
catch(ex) {}
|
|
|
|
},
|
|
|
|
|
2009-07-22 20:48:41 -07:00
|
|
|
/**
|
|
|
|
* 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
|
2009-09-15 18:38:52 -07:00
|
|
|
// if we're in backoff, we'll schedule the next sync
|
|
|
|
let reason = this._checkSync();
|
|
|
|
if (reason && reason != kSyncBackoffNotMet) {
|
2009-09-24 19:04:06 -07:00
|
|
|
this._clearSyncTriggers();
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.service = STATUS_DISABLED;
|
2009-07-22 20:48:41 -07:00
|
|
|
return;
|
2009-03-10 04:15:52 -07:00
|
|
|
}
|
2009-07-22 20:48:41 -07:00
|
|
|
|
2009-11-30 14:03:59 -08:00
|
|
|
// Only set the wait time to 0 if we need to sync right away
|
|
|
|
let wait;
|
|
|
|
if (this.globalScore > this.syncThreshold) {
|
|
|
|
this._log.debug("Global Score threshold hit, triggering sync.");
|
|
|
|
wait = 0;
|
|
|
|
}
|
|
|
|
this._scheduleNextSync(wait);
|
2009-07-22 20:48:41 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call sync() on an idle timer
|
2009-08-18 20:03:11 -07:00
|
|
|
*
|
2009-12-03 12:03:39 -08:00
|
|
|
* delay is optional
|
2009-07-22 20:48:41 -07:00
|
|
|
*/
|
2009-12-03 12:03:39 -08:00
|
|
|
syncOnIdle: function WeaveSvc_syncOnIdle(delay) {
|
2009-11-30 13:35:20 -08:00
|
|
|
// No need to add a duplicate idle observer
|
2009-12-03 12:03:39 -08:00
|
|
|
if (this._idleTime)
|
2009-11-30 13:35:20 -08:00
|
|
|
return;
|
|
|
|
|
2009-12-03 12:03:39 -08:00
|
|
|
this._idleTime = delay || IDLE_TIME;
|
2009-07-22 20:48:41 -07:00
|
|
|
this._log.debug("Idle timer created for sync, will sync after " +
|
2009-12-03 12:03:39 -08:00
|
|
|
this._idleTime + " seconds of inactivity.");
|
|
|
|
Svc.Idle.addIdleObserver(this, this._idleTime);
|
2009-07-22 20:48:41 -07:00
|
|
|
},
|
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) {
|
2009-09-25 11:46:29 -07:00
|
|
|
// Figure out when to sync next if not given a interval to wait
|
|
|
|
if (interval == null) {
|
|
|
|
// Check if we had a pending sync from last time
|
2009-09-25 17:32:35 -07:00
|
|
|
if (this.nextSync != 0)
|
2009-09-25 11:46:29 -07:00
|
|
|
interval = this.nextSync - Date.now();
|
2009-09-25 17:32:35 -07:00
|
|
|
// Use the bigger of default sync interval and backoff
|
2009-11-20 14:34:20 -08:00
|
|
|
else
|
2009-10-07 10:47:43 -07:00
|
|
|
interval = Math.max(this.syncInterval, Status.backoffInterval);
|
2009-09-25 11:46:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the sync right away if we're already late
|
|
|
|
if (interval <= 0) {
|
|
|
|
this.syncOnIdle();
|
|
|
|
return;
|
|
|
|
}
|
2009-07-22 20:48:41 -07:00
|
|
|
|
2009-09-24 22:51:38 -07:00
|
|
|
this._log.debug("Next sync in " + Math.ceil(interval / 1000) + " sec.");
|
|
|
|
Utils.delay(function() this.syncOnIdle(), interval, this, "_syncTimer");
|
2009-09-25 11:46:29 -07:00
|
|
|
|
|
|
|
// Save the next sync time in-case sync is disabled (logout/offline/etc.)
|
|
|
|
this.nextSync = Date.now() + interval;
|
2009-07-22 20:48:41 -07:00
|
|
|
},
|
|
|
|
|
2009-09-15 18:38:52 -07:00
|
|
|
_syncErrors: 0,
|
2009-07-22 20:48:41 -07:00
|
|
|
/**
|
|
|
|
* Deal with sync errors appropriately
|
|
|
|
*/
|
|
|
|
_handleSyncError: function WeaveSvc__handleSyncError() {
|
2009-09-15 18:38:52 -07:00
|
|
|
this._syncErrors++;
|
2009-03-10 04:15:52 -07:00
|
|
|
|
2009-09-15 18:38:52 -07:00
|
|
|
// do nothing on the first couple of failures, if we're not in backoff due to 5xx errors
|
2009-10-07 10:47:43 -07:00
|
|
|
if (!Status.enforceBackoff) {
|
2009-09-15 18:38:52 -07:00
|
|
|
if (this._syncErrors < 3) {
|
|
|
|
this._scheduleNextSync();
|
|
|
|
return;
|
2009-08-19 20:27:22 -07:00
|
|
|
}
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.enforceBackoff = true;
|
2009-07-22 20:48:41 -07:00
|
|
|
}
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-09-15 18:38:52 -07:00
|
|
|
const MINIMUM_BACKOFF_INTERVAL = 15 * 60 * 1000; // 15 minutes
|
|
|
|
let interval = this._calculateBackoff(this._syncErrors, MINIMUM_BACKOFF_INTERVAL);
|
2009-07-22 20:48:41 -07:00
|
|
|
|
2009-09-15 18:38:52 -07:00
|
|
|
this._scheduleNextSync(interval);
|
2009-07-22 20:48:41 -07:00
|
|
|
|
2009-09-15 18:38:52 -07:00
|
|
|
let d = new Date(Date.now() + interval);
|
2009-07-22 20:48:41 -07:00
|
|
|
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-09-29 13:02:08 -07:00
|
|
|
sync: function sync()
|
2009-06-06 16:23:58 -07:00
|
|
|
this._catch(this._lock(this._notify("sync", "", function() {
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.resetSync();
|
2009-11-12 10:11:54 -08:00
|
|
|
if (Svc.Prefs.isSet("firstSync")) {
|
|
|
|
switch(Svc.Prefs.get("firstSync")) {
|
|
|
|
case "wipeClient":
|
|
|
|
this.wipeClient();
|
|
|
|
break;
|
|
|
|
case "wipeRemote":
|
2009-12-10 18:39:51 -08:00
|
|
|
this.wipeRemote(Engines.getAll().map(function(e) e.name));
|
2009-11-12 10:11:54 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this._scheduleNextSync();
|
2009-11-20 14:34:20 -08:00
|
|
|
return;
|
2009-11-12 10:11:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-29 18:33:41 -07:00
|
|
|
// if we don't have a node, get one. if that fails, retry in 10 minutes
|
|
|
|
if (this.clusterURL == "" && !this._setCluster()) {
|
|
|
|
this._scheduleNextSync(10 * 60 * 1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-29 13:02:08 -07:00
|
|
|
// Make sure we should sync or record why we shouldn't
|
2009-03-10 04:15:52 -07:00
|
|
|
let reason = this._checkSync();
|
2009-09-29 13:02:08 -07:00
|
|
|
if (reason) {
|
2009-03-24 19:23:53 -07:00
|
|
|
// this is a purposeful abort rather than a failure, so don't set
|
2009-09-15 18:38:52 -07:00
|
|
|
// any status bits
|
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-09-24 19:04:06 -07:00
|
|
|
// Clear out any potentially pending syncs now that we're syncing
|
|
|
|
this._clearSyncTriggers();
|
2009-09-25 11:46:29 -07:00
|
|
|
this.nextSync = 0;
|
2009-09-15 18:38:52 -07:00
|
|
|
|
2009-11-09 11:33:53 -08:00
|
|
|
this.globalScore = 0;
|
|
|
|
|
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-12-16 19:08:36 -08:00
|
|
|
// Ping the server with a special info request once a day
|
|
|
|
let infoURL = this.infoURL;
|
|
|
|
let now = Math.floor(Date.now() / 1000);
|
|
|
|
let lastPing = Svc.Prefs.get("lastPing", 0);
|
|
|
|
if (now - lastPing > 86400) { // 60 * 60 * 24
|
|
|
|
infoURL += "?";
|
|
|
|
Svc.Prefs.set("lastPing", now);
|
|
|
|
}
|
|
|
|
|
2009-09-03 21:30:40 -07:00
|
|
|
// Figure out what the last modified time is for each collection
|
2009-12-16 19:08:36 -08:00
|
|
|
let info = new Resource(infoURL).get();
|
2009-09-03 21:30:40 -07:00
|
|
|
if (!info.success)
|
|
|
|
throw "aborting sync, failed to get collections";
|
|
|
|
|
|
|
|
// Convert the response to an object and read out the modified times
|
2009-12-02 19:03:27 -08:00
|
|
|
for each (let engine in [Clients].concat(Engines.getAll()))
|
2009-09-10 11:04:36 -07:00
|
|
|
engine.lastModified = info.obj[engine.name] || 0;
|
2009-09-03 21:30:40 -07:00
|
|
|
|
2009-09-10 22:57:36 -07:00
|
|
|
this._log.trace("Refreshing client list");
|
2009-06-05 15:34:32 -07:00
|
|
|
Clients.sync();
|
2008-07-31 01:02:41 -07:00
|
|
|
|
2009-11-12 12:44:33 -08:00
|
|
|
// Process the incoming commands if we have any
|
|
|
|
if (Clients.getClients()[Clients.clientID].commands) {
|
|
|
|
try {
|
|
|
|
if (!(this.processCommands())) {
|
|
|
|
Status.sync = ABORT_SYNC_COMMAND;
|
|
|
|
throw "aborting sync, process commands said so";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repeat remoteSetup in-case the commands forced us to reset
|
|
|
|
if (!(this._remoteSetup()))
|
|
|
|
throw "aborting sync, remote setup failed after processing commands";
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
// Always immediately push back the local client (now without commands)
|
|
|
|
Clients.sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-25 15:05:09 -07:00
|
|
|
// Update the client mode now because it might change what we sync
|
|
|
|
this._updateClientMode();
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
try {
|
2009-10-08 13:51:22 -07:00
|
|
|
for each (let engine in Engines.getEnabled()) {
|
2009-03-10 04:07:24 -07:00
|
|
|
// If there's any problems with syncing the engine, report the failure
|
2009-10-07 10:47:43 -07:00
|
|
|
if (!(this._syncEngine(engine)) || Status.enforceBackoff) {
|
2009-03-10 04:07:24 -07:00
|
|
|
this._log.info("Aborting sync");
|
|
|
|
break;
|
2009-01-06 13:54:18 -08:00
|
|
|
}
|
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-10-27 16:15:20 -07:00
|
|
|
throw "Some engines did not sync correctly";
|
2009-03-10 04:07:24 -07:00
|
|
|
else {
|
|
|
|
Svc.Prefs.set("lastSync", new Date().toString());
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.sync = SYNC_SUCCEEDED;
|
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-11-25 17:08:51 -08:00
|
|
|
Svc.Prefs.reset("firstSync");
|
2008-07-08 17:44:00 -07:00
|
|
|
}
|
2009-06-06 16:23:58 -07:00
|
|
|
})))(),
|
2009-03-10 04:07:24 -07:00
|
|
|
|
2009-09-25 15:05:09 -07:00
|
|
|
/**
|
|
|
|
* Process the locally stored clients list to figure out what mode to be in
|
|
|
|
*/
|
|
|
|
_updateClientMode: function _updateClientMode() {
|
|
|
|
let numClients = 0;
|
|
|
|
let hasMobile = false;
|
|
|
|
|
|
|
|
// Check how many and what type of clients we have
|
|
|
|
for each (let {type} in Clients.getClients()) {
|
|
|
|
numClients++;
|
|
|
|
hasMobile = hasMobile || type == "mobile";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do if it's the same amount
|
|
|
|
if (this.numClients == numClients)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._log.debug("Client count: " + this.numClients + " -> " + numClients);
|
|
|
|
this.numClients = numClients;
|
|
|
|
|
|
|
|
let tabEngine = Engines.get("tabs");
|
|
|
|
if (numClients == 1) {
|
2009-09-25 15:41:27 -07:00
|
|
|
this.syncInterval = SINGLE_USER_SYNC;
|
2009-11-09 09:57:58 -08:00
|
|
|
this.syncThreshold = SINGLE_USER_THRESHOLD;
|
2009-09-25 15:05:09 -07:00
|
|
|
|
|
|
|
// Disable tabs sync for single client, but store the original value
|
|
|
|
Svc.Prefs.set("engine.tabs.backup", tabEngine.enabled);
|
|
|
|
tabEngine.enabled = false;
|
|
|
|
}
|
|
|
|
else {
|
2009-09-25 15:41:27 -07:00
|
|
|
this.syncInterval = hasMobile ? MULTI_MOBILE_SYNC : MULTI_DESKTOP_SYNC;
|
2009-11-09 09:57:58 -08:00
|
|
|
this.syncThreshold = hasMobile ? MULTI_MOBILE_THRESHOLD : MULTI_DESKTOP_THRESHOLD;
|
2009-09-25 15:05:09 -07:00
|
|
|
|
|
|
|
// Restore the original tab enabled value
|
|
|
|
tabEngine.enabled = Svc.Prefs.get("engine.tabs.backup", true);
|
|
|
|
Svc.Prefs.reset("engine.tabs.backup");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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-09-15 18:38:52 -07:00
|
|
|
this._checkServerError(e);
|
|
|
|
|
2009-10-27 16:15:20 -07:00
|
|
|
Status.engines = [engine.name, e.failureCode || ENGINE_UNKNOWN_FAIL];
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
this._syncError = true;
|
2009-08-31 17:17:20 -07:00
|
|
|
this._log.debug(Utils.exceptionStr(e));
|
|
|
|
return true;
|
2008-05-23 23:58:53 -07:00
|
|
|
}
|
2009-09-30 14:46:59 -07:00
|
|
|
finally {
|
|
|
|
// If this engine has more to fetch, remember that globally
|
|
|
|
if (engine.toFetch != null && engine.toFetch.length > 0)
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.partial = true;
|
2009-09-30 14:46:59 -07:00
|
|
|
}
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
|
|
|
|
2009-11-12 12:44:33 -08:00
|
|
|
_freshStart: function WeaveSvc__freshStart() {
|
2009-06-05 22:21:22 -07:00
|
|
|
this.resetClient();
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2009-12-02 17:57:13 -08:00
|
|
|
this._log.debug("Uploading new metadata record from freshStart");
|
2009-11-04 16:27:35 -08:00
|
|
|
let meta = new WBORecord(this.metaURL);
|
2009-02-19 04:09:55 -08:00
|
|
|
meta.payload.syncID = Clients.syncID;
|
2009-07-22 18:49:07 -07:00
|
|
|
this._updateRemoteVersion(meta);
|
2009-12-02 17:57:13 -08:00
|
|
|
|
|
|
|
// Wipe everything we know about except meta because we just uploaded it
|
|
|
|
let collections = [Clients].concat(Engines.getAll()).map(function(engine) {
|
|
|
|
return engine.name;
|
|
|
|
});
|
|
|
|
this.wipeServer(["crypto", "keys"].concat(collections));
|
2009-07-22 18:49:07 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_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-09-10 11:05:13 -07:00
|
|
|
let resp = new Resource(meta.uri).put(meta);
|
2009-08-26 15:32:46 -07:00
|
|
|
if (!resp.success)
|
|
|
|
throw resp;
|
2009-02-19 04:09:55 -08:00
|
|
|
},
|
2009-09-20 19:26:01 -07:00
|
|
|
|
|
|
|
|
2009-09-15 18:38:52 -07:00
|
|
|
/**
|
|
|
|
* Check to see if this is a failure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
_checkServerError: function WeaveSvc__checkServerError(resp) {
|
|
|
|
if (Utils.checkStatus(resp.status, null, [500, [502, 504]])) {
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.enforceBackoff = true;
|
2009-09-15 18:38:52 -07:00
|
|
|
if (resp.status == 503 && resp.headers["Retry-After"])
|
|
|
|
Observers.notify("weave:service:backoff:interval", parseInt(resp.headers["Retry-After"], 10));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
2009-10-07 10:47:43 -07:00
|
|
|
* Return a value for a backoff interval. Maximum is eight hours, unless
|
|
|
|
* Status.backoffInterval is higher.
|
2009-09-15 18:38:52 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
_calculateBackoff: function WeaveSvc__calculateBackoff(attempts, base_interval) {
|
|
|
|
const MAXIMUM_BACKOFF_INTERVAL = 8 * 60 * 60 * 1000; // 8 hours
|
|
|
|
let backoffInterval = attempts *
|
|
|
|
(Math.floor(Math.random() * base_interval) +
|
|
|
|
base_interval);
|
2009-10-07 10:47:43 -07:00
|
|
|
return Math.max(Math.min(backoffInterval, MAXIMUM_BACKOFF_INTERVAL), Status.backoffInterval);
|
2009-09-15 18:38:52 -07:00
|
|
|
},
|
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-09-10 11:04:36 -07:00
|
|
|
// Grab all the collections for the user and delete each one
|
|
|
|
let info = new Resource(this.infoURL).get();
|
|
|
|
for (let name in info.obj) {
|
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-09-21 17:13:41 -07:00
|
|
|
new Resource(this.storageURL + 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
|
|
|
|
2009-10-12 16:22:54 -07:00
|
|
|
// Fully wipe each engine if it's able to decrypt data
|
2009-03-20 00:13:16 -07:00
|
|
|
for each (let engine in engines)
|
2009-10-12 16:22:54 -07:00
|
|
|
if (engine._testDecrypt())
|
|
|
|
engine.wipeClient();
|
2009-10-12 16:45:40 -07:00
|
|
|
|
|
|
|
// Save the password/passphrase just in-case they aren't restored by sync
|
|
|
|
this.persistLogin();
|
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-12-10 18:39:51 -08:00
|
|
|
// Make sure stuff gets uploaded
|
|
|
|
Clients.resetSyncID();
|
|
|
|
|
2009-03-11 23:33:14 -07:00
|
|
|
// Clear out any server data
|
2009-12-10 18:39:51 -08:00
|
|
|
this.wipeServer(engines);
|
2009-11-12 12:44:33 -08:00
|
|
|
|
|
|
|
// Only wipe the engines provided
|
2009-12-10 18:39:51 -08:00
|
|
|
if (engines)
|
2009-11-12 12:44:33 -08:00
|
|
|
engines.forEach(function(e) this.prepCommand("wipeEngine", [e]), this);
|
|
|
|
// Tell the remote machines to wipe themselves
|
2009-12-10 18:39:51 -08:00
|
|
|
else
|
|
|
|
this.prepCommand("wipeAll", []);
|
|
|
|
|
|
|
|
// Make sure the changed clients get updated
|
|
|
|
Clients.sync();
|
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-11-12 12:44:33 -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"],
|
|
|
|
["logout", 0, "Log out client"],
|
|
|
|
].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;
|
|
|
|
}, {}),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the local client has any remote commands and perform them.
|
|
|
|
*
|
|
|
|
* @return False to abort sync
|
|
|
|
*/
|
|
|
|
processCommands: function WeaveSvc_processCommands()
|
|
|
|
this._notify("process-commands", "", function() {
|
|
|
|
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 + ")");
|
|
|
|
|
|
|
|
let engines = [args[0]];
|
|
|
|
switch (command) {
|
|
|
|
case "resetAll":
|
|
|
|
engines = null;
|
|
|
|
// Fallthrough
|
|
|
|
case "resetEngine":
|
|
|
|
this.resetClient(engines);
|
|
|
|
break;
|
|
|
|
case "wipeAll":
|
|
|
|
engines = null;
|
|
|
|
// Fallthrough
|
|
|
|
case "wipeEngine":
|
|
|
|
this.wipeClient(engines);
|
|
|
|
break;
|
|
|
|
case "logout":
|
|
|
|
this.logout();
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
this._log.debug("Received an unknown command: " + command);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})(),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 = JSON.stringify(args);
|
|
|
|
let notDupe = function(action) action.command != command ||
|
|
|
|
JSON.stringify(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);
|
|
|
|
}
|
|
|
|
},
|
2009-03-03 15:29:35 -08:00
|
|
|
};
|
2009-12-15 14:21:12 -08:00
|
|
|
|
|
|
|
// Load Weave on the first time this file is loaded
|
|
|
|
Weave.Service.onStartup();
|