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>
|
2010-11-29 16:41:17 -08:00
|
|
|
* Richard Newman <rnewman@mozilla.com>
|
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 ***** */
|
|
|
|
|
2010-08-25 15:49:44 -07:00
|
|
|
// 'Weave' continues to be exported for backwards compatibility.
|
|
|
|
const EXPORTED_SYMBOLS = ["Service", "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
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// How long a key to generate from an old passphrase.
|
|
|
|
const PBKDF2_KEY_BYTES = 16;
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/auth.js");
|
|
|
|
Cu.import("resource://services-sync/base_records/crypto.js");
|
|
|
|
Cu.import("resource://services-sync/base_records/wbo.js");
|
|
|
|
Cu.import("resource://services-sync/constants.js");
|
|
|
|
Cu.import("resource://services-sync/engines.js");
|
|
|
|
Cu.import("resource://services-sync/engines/clients.js");
|
|
|
|
Cu.import("resource://services-sync/ext/Sync.js");
|
2010-08-25 15:49:44 -07:00
|
|
|
Cu.import("resource://services-sync/ext/Preferences.js");
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/identity.js");
|
|
|
|
Cu.import("resource://services-sync/log4moz.js");
|
|
|
|
Cu.import("resource://services-sync/resource.js");
|
|
|
|
Cu.import("resource://services-sync/status.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2010-08-25 15:49:44 -07:00
|
|
|
Cu.import("resource://services-sync/main.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2010-08-25 15:49:44 -07:00
|
|
|
Utils.lazy(this, '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,
|
2010-06-04 09:05:55 -07:00
|
|
|
_locked: false,
|
2008-06-12 12:36:58 -07:00
|
|
|
_loggedIn: false,
|
2009-03-24 19:23:53 -07:00
|
|
|
|
2010-09-13 08:17:37 -07:00
|
|
|
get account() Svc.Prefs.get("account", this.username),
|
|
|
|
set account(value) {
|
|
|
|
if (value) {
|
|
|
|
value = value.toLowerCase();
|
|
|
|
Svc.Prefs.set("account", value);
|
|
|
|
} else {
|
|
|
|
Svc.Prefs.reset("account");
|
|
|
|
}
|
|
|
|
this.username = this._usernameFromAccount(value);
|
|
|
|
},
|
|
|
|
|
|
|
|
_usernameFromAccount: function _usernameFromAccount(value) {
|
|
|
|
// If we encounter characters not allowed by the API (as found for
|
|
|
|
// instance in an email address), hash the value.
|
|
|
|
if (value && value.match(/[^A-Z0-9._-]/i))
|
|
|
|
return Utils.sha1Base32(value.toLowerCase()).toLowerCase();
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2010-04-26 11:37:12 -07:00
|
|
|
get password() ID.get("WeaveID").password,
|
|
|
|
set password(value) ID.get("WeaveID").password = value,
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
get passphrase() ID.get("WeaveCryptoID").keyStr,
|
|
|
|
set passphrase(value) ID.get("WeaveCryptoID").keyStr = value,
|
|
|
|
|
|
|
|
get syncKeyBundle() ID.get("WeaveCryptoID"),
|
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;
|
2010-04-06 12:07:34 -07:00
|
|
|
return misc + "1.0/";
|
2009-09-21 17:13:41 -07:00
|
|
|
},
|
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;
|
2010-04-06 12:07:34 -07:00
|
|
|
return user + "1.0/";
|
2009-09-21 17:13:41 -07:00
|
|
|
},
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2010-04-08 14:03:10 -07:00
|
|
|
get pwResetURL() {
|
|
|
|
return this.serverURL + "weave-password-reset";
|
|
|
|
},
|
|
|
|
|
2010-05-21 14:41:19 -07:00
|
|
|
get updatedURL() {
|
|
|
|
return WEAVE_CHANNEL == "dev" ? UPDATED_DEV_URL : UPDATED_REL_URL;
|
|
|
|
},
|
|
|
|
|
2010-03-25 09:52:45 -07:00
|
|
|
get syncID() {
|
|
|
|
// Generate a random syncID id we don't have one
|
|
|
|
let syncID = Svc.Prefs.get("client.syncID", "");
|
|
|
|
return syncID == "" ? this.syncID = Utils.makeGUID() : syncID;
|
|
|
|
},
|
|
|
|
set syncID(value) {
|
|
|
|
Svc.Prefs.set("client.syncID", value);
|
|
|
|
},
|
|
|
|
|
2009-02-11 22:08:56 -08:00
|
|
|
get isLoggedIn() { return this._loggedIn; },
|
2008-07-09 17:17:24 -07:00
|
|
|
|
2010-04-29 08:50:46 -07:00
|
|
|
// nextSync and nextHeartbeat are in milliseconds, but prefs can't hold that much
|
2009-09-25 11:46:29 -07:00
|
|
|
get nextSync() Svc.Prefs.get("nextSync", 0) * 1000,
|
|
|
|
set nextSync(value) Svc.Prefs.set("nextSync", Math.floor(value / 1000)),
|
2010-04-29 08:50:46 -07:00
|
|
|
get nextHeartbeat() Svc.Prefs.get("nextHeartbeat", 0) * 1000,
|
|
|
|
set nextHeartbeat(value) Svc.Prefs.set("nextHeartbeat", Math.floor(value / 1000)),
|
2009-09-25 11:46:29 -07:00
|
|
|
|
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") + "/";
|
2010-09-11 09:39:21 -07:00
|
|
|
this.userBaseURL = storageAPI + this.username + "/";
|
|
|
|
this._log.debug("Caching URLs under storage user base: " + this.userBaseURL);
|
2009-09-21 17:13:41 -07:00
|
|
|
|
|
|
|
// Generate and cache various URLs under the storage API for this user
|
2010-09-11 09:39:21 -07:00
|
|
|
this.infoURL = this.userBaseURL + "info/collections";
|
|
|
|
this.storageURL = this.userBaseURL + "storage/";
|
2009-09-21 17:13:41 -07:00
|
|
|
this.metaURL = this.storageURL + "meta/global";
|
2010-11-29 16:41:17 -08:00
|
|
|
this.cryptoKeysURL = this.storageURL + "crypto/keys";
|
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;
|
|
|
|
},
|
2010-12-09 23:06:44 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Here is a disgusting yet reasonable way of handling HMAC errors deep in
|
|
|
|
* the guts of Sync. The astute reader will note that this is a hacky way of
|
|
|
|
* implementing something like continuable conditions.
|
|
|
|
*
|
|
|
|
* A handler function is glued to each engine. If the engine discovers an
|
|
|
|
* HMAC failure, we fetch keys from the server and update our keys, just as
|
|
|
|
* we would on startup.
|
|
|
|
*
|
|
|
|
* If our key collection changed, we signal to the engine (via our return
|
|
|
|
* value) that it should retry decryption.
|
|
|
|
*
|
|
|
|
* If our key collection did not change, it means that we already had the
|
|
|
|
* correct keys... and thus a different client has the wrong ones. Reupload
|
|
|
|
* the bundle that we fetched, which will bump the modified time on the
|
|
|
|
* server and (we hope) prompt a broken client to fix itself.
|
|
|
|
*
|
|
|
|
* We keep track of the time at which we last applied this reasoning, because
|
|
|
|
* thrashing doesn't solve anything. We keep a reasonable interval between
|
|
|
|
* these remedial actions.
|
|
|
|
*/
|
|
|
|
lastHMACEvent: 0,
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns whether to try again.
|
|
|
|
*/
|
|
|
|
handleHMACEvent: function handleHMACEvent() {
|
|
|
|
let now = Date.now();
|
|
|
|
|
|
|
|
// Leave a sizable delay between HMAC recovery attempts. This gives us
|
|
|
|
// time for another client to fix themselves if we touch the record.
|
|
|
|
if ((now - this.lastHMACEvent) < HMAC_EVENT_INTERVAL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this._log.info("Bad HMAC event detected. Attempting recovery " +
|
|
|
|
"or signaling to other clients.");
|
|
|
|
|
|
|
|
// Set the last handled time so that we don't act again.
|
|
|
|
this.lastHMACEvent = now;
|
|
|
|
|
|
|
|
// Fetch keys.
|
|
|
|
let cryptoKeys = new CryptoWrapper("crypto", "keys");
|
|
|
|
try {
|
|
|
|
let cryptoResp = cryptoKeys.fetch(this.cryptoKeysURL).response;
|
|
|
|
|
|
|
|
// Save out the ciphertext for when we reupload. If there's a bug in
|
|
|
|
// CollectionKeys, this will prevent us from uploading junk.
|
|
|
|
let cipherText = cryptoKeys.ciphertext;
|
|
|
|
|
|
|
|
if (!cryptoResp.success) {
|
|
|
|
this._log.warn("Failed to download keys.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let keysChanged = this.handleFetchedKeys(this.syncKeyBundle,
|
|
|
|
cryptoKeys, true);
|
|
|
|
if (keysChanged) {
|
|
|
|
// Did they change? If so, carry on.
|
|
|
|
this._log.info("Suggesting retry.");
|
|
|
|
return true; // Try again.
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not, reupload them and continue the current sync.
|
|
|
|
cryptoKeys.ciphertext = cipherText;
|
|
|
|
cryptoKeys.cleartext = null;
|
|
|
|
|
|
|
|
let uploadResp = cryptoKeys.upload(this.cryptoKeysURL);
|
|
|
|
if (uploadResp.success)
|
|
|
|
this._log.info("Successfully re-uploaded keys. Continuing sync.");
|
|
|
|
else
|
|
|
|
this._log.warn("Got error response re-uploading keys. " +
|
|
|
|
"Continuing sync; let's try again later.");
|
|
|
|
|
|
|
|
return false; // Don't try again: same keys.
|
|
|
|
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Got exception \"" + ex + "\" fetching and handling " +
|
|
|
|
"crypto keys. Will try again later.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleFetchedKeys: function handleFetchedKeys(syncKey, cryptoKeys, skipReset) {
|
|
|
|
// Don't want to wipe if we're just starting up!
|
|
|
|
// This is largely relevant because we don't persist
|
|
|
|
// CollectionKeys yet: Bug 610913.
|
|
|
|
let wasBlank = CollectionKeys.isClear;
|
|
|
|
let keysChanged = CollectionKeys.updateContents(syncKey, cryptoKeys);
|
|
|
|
|
|
|
|
if (keysChanged && !wasBlank) {
|
|
|
|
this._log.debug("Keys changed: " + JSON.stringify(keysChanged));
|
|
|
|
|
|
|
|
if (!skipReset) {
|
|
|
|
this._log.info("Resetting client to reflect key change.");
|
|
|
|
|
|
|
|
if (keysChanged.length) {
|
|
|
|
// Collection keys only. Reset individual engines.
|
|
|
|
this.resetClient(keysChanged);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Default key changed: wipe it all.
|
|
|
|
this.resetClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.info("Downloaded new keys, client reset. Proceeding.");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2009-02-10 00:56:42 -08:00
|
|
|
|
2009-09-25 08:13:12 -07:00
|
|
|
/**
|
|
|
|
* Prepare to initialize the rest of Weave after waiting a little bit
|
|
|
|
*/
|
|
|
|
onStartup: function onStartup() {
|
2010-06-16 14:30:13 -07:00
|
|
|
this._migratePrefs();
|
2009-09-25 08:13:12 -07:00
|
|
|
this._initLogs();
|
2010-04-01 15:16:19 -07:00
|
|
|
this._log.info("Loading Weave " + WEAVE_VERSION);
|
2009-09-25 08:13:12 -07:00
|
|
|
|
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;
|
2010-08-12 20:31:00 -07:00
|
|
|
this._log.info("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
|
|
|
|
2010-08-06 08:30:58 -07:00
|
|
|
Svc.Obs.add("weave:service:setup-complete", this);
|
2010-04-20 18:56:44 -07:00
|
|
|
Svc.Obs.add("network:offline-status-changed", this);
|
|
|
|
Svc.Obs.add("weave:service:sync:finish", this);
|
|
|
|
Svc.Obs.add("weave:service:sync:error", this);
|
|
|
|
Svc.Obs.add("weave:service:backoff:interval", this);
|
|
|
|
Svc.Obs.add("weave:engine:score:updated", this);
|
2010-06-03 18:37:07 -07:00
|
|
|
Svc.Obs.add("weave:resource:status:401", this);
|
2010-09-07 09:44:01 -07:00
|
|
|
Svc.Prefs.observe("engine.", this);
|
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)
|
2010-08-25 15:49:45 -07:00
|
|
|
let id = ID.get("WeaveID");
|
|
|
|
if (!id)
|
|
|
|
id = ID.set("WeaveID", new Identity(PWDMGR_PASSWORD_REALM, this.username));
|
|
|
|
Auth.defaultAuthenticator = new BasicAuthenticator(id);
|
2008-11-06 17:37:17 -08:00
|
|
|
|
2010-08-25 15:49:45 -07:00
|
|
|
if (!ID.get("WeaveCryptoID"))
|
|
|
|
ID.set("WeaveCryptoID",
|
2010-11-29 16:41:17 -08:00
|
|
|
new SyncKeyBundle(PWDMGR_PASSPHRASE_REALM, this.username));
|
2008-11-06 17:37:17 -08:00
|
|
|
|
2009-09-21 17:13:41 -07:00
|
|
|
this._updateCachedURLs();
|
2008-11-06 17:37:17 -08:00
|
|
|
|
2010-08-06 08:30:58 -07:00
|
|
|
let status = this._checkSetup();
|
|
|
|
if (status != STATUS_DISABLED && status != CLIENT_NOT_CONFIGURED)
|
|
|
|
Svc.Obs.notify("weave:engine:start-tracking");
|
|
|
|
|
2010-06-10 17:04:49 -07:00
|
|
|
// Applications can specify this preference if they want autoconnect
|
|
|
|
// to happen after a fixed delay.
|
|
|
|
let delay = Svc.Prefs.get("autoconnectDelay");
|
|
|
|
if (delay) {
|
|
|
|
this.delayedAutoConnect(delay);
|
2010-04-01 15:16:19 -07:00
|
|
|
}
|
2010-06-10 17:04:49 -07:00
|
|
|
|
|
|
|
// Send an event now that Weave service is ready. We don't do this
|
|
|
|
// synchronously so that observers will definitely have access to the
|
|
|
|
// 'Weave' namespace.
|
|
|
|
Utils.delay(function() Svc.Obs.notify("weave:service:ready"), 0);
|
2008-11-06 17:37:17 -08:00
|
|
|
},
|
|
|
|
|
2010-04-09 10:20:41 -07:00
|
|
|
_checkSetup: function WeaveSvc__checkSetup() {
|
2010-08-25 15:49:45 -07:00
|
|
|
if (!this.enabled)
|
|
|
|
return Status.service = STATUS_DISABLED;
|
|
|
|
return Status.checkSetup();
|
2010-04-09 10:20:41 -07:00
|
|
|
},
|
|
|
|
|
2010-06-16 14:30:13 -07:00
|
|
|
_migratePrefs: function _migratePrefs() {
|
|
|
|
// No need to re-migrate
|
|
|
|
if (Svc.Prefs.get("migrated", false))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Grab the list of old pref names
|
|
|
|
let oldPrefBranch = "extensions.weave.";
|
|
|
|
let oldPrefNames = Cc["@mozilla.org/preferences-service;1"].
|
|
|
|
getService(Ci.nsIPrefService).
|
|
|
|
getBranch(oldPrefBranch).
|
|
|
|
getChildList("", {});
|
|
|
|
|
|
|
|
// Map each old pref to the current pref branch
|
|
|
|
let oldPref = new Preferences(oldPrefBranch);
|
|
|
|
for each (let pref in oldPrefNames)
|
|
|
|
Svc.Prefs.set(pref, oldPref.get(pref));
|
|
|
|
|
|
|
|
// Remove all the old prefs and remember that we've migrated
|
|
|
|
oldPref.resetBranch("");
|
|
|
|
Svc.Prefs.set("migrated", true);
|
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
|
2010-10-11 09:46:53 -07:00
|
|
|
let enabled = Svc.Prefs.get("log.appender.debugLog.enabled", false);
|
|
|
|
if (enabled) {
|
|
|
|
let verbose = Svc.Directory.get("ProfD", Ci.nsIFile);
|
|
|
|
verbose.QueryInterface(Ci.nsILocalFile);
|
|
|
|
verbose.append("weave");
|
|
|
|
verbose.append("logs");
|
|
|
|
verbose.append("verbose-log.txt");
|
|
|
|
if (!verbose.exists())
|
|
|
|
verbose.create(verbose.NORMAL_FILE_TYPE, PERMS_FILE);
|
|
|
|
|
|
|
|
let maxSize = 65536; // 64 * 1024 (64KB)
|
|
|
|
this._debugApp = new Log4Moz.RotatingFileAppender(verbose, formatter, maxSize);
|
|
|
|
this._debugApp.level = Log4Moz.Level[Svc.Prefs.get("log.appender.debugLog")];
|
|
|
|
root.addAppender(this._debugApp);
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-03-28 19:36:11 -07:00
|
|
|
clearLogs: function WeaveSvc_clearLogs() {
|
2010-10-11 09:46:53 -07:00
|
|
|
if (this._debugApp)
|
|
|
|
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 = [];
|
2010-06-09 17:22:03 -07:00
|
|
|
// Applications can provide this preference (comma-separated list)
|
|
|
|
// to specify which engines should be registered on startup.
|
|
|
|
let pref = Svc.Prefs.get("registerEngines");
|
|
|
|
if (pref) {
|
|
|
|
engines = pref.split(",");
|
2009-04-08 12:39:14 -07:00
|
|
|
}
|
|
|
|
|
2010-06-16 15:11:40 -07:00
|
|
|
// Grab the actual engines and register them
|
2009-04-08 12:39:14 -07:00
|
|
|
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) {
|
2010-08-06 08:30:58 -07:00
|
|
|
case "weave:service:setup-complete":
|
|
|
|
let status = this._checkSetup();
|
|
|
|
if (status != STATUS_DISABLED && status != CLIENT_NOT_CONFIGURED)
|
|
|
|
Svc.Obs.notify("weave:engine:start-tracking");
|
|
|
|
break;
|
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;
|
2009-07-22 20:48:41 -07:00
|
|
|
case "weave:service:sync:error":
|
|
|
|
this._handleSyncError();
|
2010-03-31 18:58:07 -07:00
|
|
|
if (Status.sync == CREDENTIALS_CHANGED) {
|
|
|
|
this.logout();
|
|
|
|
Utils.delay(function() this.login(), 0, this);
|
|
|
|
}
|
2009-07-22 20:48:41 -07:00
|
|
|
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":
|
2010-03-25 14:24:41 -07:00
|
|
|
let interval = (data + Math.random() * data * 0.25) * 1000; // 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;
|
2010-06-03 18:37:07 -07:00
|
|
|
case "weave:resource:status:401":
|
|
|
|
this._handleResource401(subject);
|
|
|
|
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;
|
2010-09-07 09:44:01 -07:00
|
|
|
case "nsPref:changed":
|
|
|
|
if (this._ignorePrefObserver)
|
|
|
|
return;
|
|
|
|
let engine = data.slice((PREFS_BRANCH + "engine.").length);
|
|
|
|
this._handleEngineStatusChanged(engine);
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2010-09-07 09:44:01 -07:00
|
|
|
_handleEngineStatusChanged: function handleEngineDisabled(engine) {
|
|
|
|
this._log.trace("Status for " + engine + " engine changed.");
|
|
|
|
if (Svc.Prefs.get("engineStatusChanged." + engine, false)) {
|
|
|
|
// The enabled status being changed back to what it was before.
|
|
|
|
Svc.Prefs.reset("engineStatusChanged." + engine);
|
|
|
|
} else {
|
|
|
|
// Remember that the engine status changed locally until the next sync.
|
|
|
|
Svc.Prefs.set("engineStatusChanged." + engine, true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-06-03 18:37:07 -07:00
|
|
|
_handleResource401: function _handleResource401(request) {
|
|
|
|
// Only handle 401s that are hitting the current cluster
|
|
|
|
let spec = request.resource.spec;
|
|
|
|
let cluster = this.clusterURL;
|
|
|
|
if (spec.indexOf(cluster) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Nothing to do if the cluster isn't changing
|
|
|
|
if (!this._setCluster())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Replace the old cluster with the new one to retry the request
|
|
|
|
request.newUri = this.clusterURL + spec.slice(cluster.length);
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
2010-01-25 10:30:31 -08:00
|
|
|
let fail;
|
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) {
|
2010-01-25 10:30:31 -08:00
|
|
|
case 400:
|
|
|
|
Status.login = LOGIN_FAILED_LOGIN_REJECTED;
|
|
|
|
fail = "Find cluster denied: " + this._errorStr(node);
|
|
|
|
break;
|
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:
|
2010-06-09 11:07:54 -07:00
|
|
|
fail = "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;
|
2010-01-25 10:30:31 -08:00
|
|
|
fail = e;
|
2009-08-20 12:14:34 -07:00
|
|
|
}
|
2010-01-25 10:30:31 -08:00
|
|
|
throw fail;
|
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
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
/**
|
|
|
|
* Perform the info fetch as part of a login or key fetch.
|
|
|
|
*/
|
|
|
|
_fetchInfo: function _fetchInfo(url, logout) {
|
|
|
|
let infoURL = url || this.infoURL;
|
|
|
|
|
|
|
|
let info = new Resource(infoURL).get();
|
|
|
|
if (!info.success) {
|
|
|
|
if (info.status == 401) {
|
|
|
|
if (logout) {
|
|
|
|
this.logout();
|
|
|
|
Status.login = LOGIN_FAILED_LOGIN_REJECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw "aborting sync, failed to get collections";
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
},
|
|
|
|
|
|
|
|
verifyAndFetchSymmetricKeys: function verifyAndFetchSymmetricKeys(infoResponse) {
|
|
|
|
|
|
|
|
this._log.debug("Fetching and verifying -- or generating -- symmetric keys.");
|
|
|
|
|
|
|
|
// Don't allow empty/missing passphrase.
|
|
|
|
// Furthermore, we assume that our sync key is already upgraded,
|
|
|
|
// and fail if that assumption is invalidated.
|
|
|
|
|
|
|
|
let syncKey = this.syncKeyBundle;
|
|
|
|
if (!syncKey) {
|
|
|
|
this._log.error("No sync key: cannot fetch symmetric keys.");
|
|
|
|
Status.login = LOGIN_FAILED_NO_PASSPHRASE;
|
|
|
|
Status.sync = CREDENTIALS_CHANGED; // For want of a better option.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not sure this validation is necessary now.
|
|
|
|
if (!Utils.isPassphrase(syncKey.keyStr)) {
|
|
|
|
this._log.warn("Sync key input is invalid: cannot fetch symmetric keys.");
|
|
|
|
Status.login = LOGIN_FAILED_INVALID_PASSPHRASE;
|
|
|
|
Status.sync = CREDENTIALS_CHANGED;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!infoResponse)
|
|
|
|
infoResponse = this._fetchInfo(); // Will throw an exception on failure.
|
|
|
|
|
|
|
|
// This only applies when the server is already at version 4.
|
|
|
|
if (infoResponse.status != 200) {
|
|
|
|
this._log.warn("info/collections returned non-200 response. Failing key fetch.");
|
|
|
|
Status.login = LOGIN_FAILED_SERVER_ERROR;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let infoCollections = infoResponse.obj;
|
|
|
|
|
|
|
|
this._log.info("Testing info/collections: " + JSON.stringify(infoCollections));
|
|
|
|
|
|
|
|
if (CollectionKeys.updateNeeded(infoCollections)) {
|
|
|
|
this._log.info("CollectionKeys reports that a key update is needed.");
|
|
|
|
|
|
|
|
// Don't always set to CREDENTIALS_CHANGED -- we will probably take care of this.
|
|
|
|
|
|
|
|
// Fetch storage/crypto/keys.
|
|
|
|
let cryptoKeys;
|
|
|
|
|
|
|
|
if (infoCollections && ('crypto' in infoCollections)) {
|
|
|
|
try {
|
|
|
|
cryptoKeys = new CryptoWrapper("crypto", "keys");
|
|
|
|
let cryptoResp = cryptoKeys.fetch(this.cryptoKeysURL).response;
|
|
|
|
|
|
|
|
if (cryptoResp.success) {
|
2010-12-09 23:06:44 -08:00
|
|
|
let keysChanged = this.handleFetchedKeys(syncKey, cryptoKeys);
|
2010-11-29 16:41:17 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (cryptoResp.status == 404) {
|
|
|
|
// On failure, ask CollectionKeys to generate new keys and upload them.
|
|
|
|
// Fall through to the behavior below.
|
|
|
|
this._log.warn("Got 404 for crypto/keys, but 'crypto' in info/collections. Regenerating.");
|
|
|
|
cryptoKeys = null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Some other problem.
|
|
|
|
this._log.warn("Got status " + cryptoResp.status + " fetching crypto keys.");
|
|
|
|
Status.login = LOGIN_FAILED_SERVER_ERROR;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
this._log.warn("Got exception \"" + ex + "\" fetching cryptoKeys.");
|
|
|
|
// TODO: Um, what exceptions might we get here? Should we re-throw any?
|
|
|
|
|
|
|
|
// One kind of exception: HMAC failure.
|
2010-12-09 10:32:03 -08:00
|
|
|
if (Utils.isHMACMismatch(ex)) {
|
2010-11-29 16:41:17 -08:00
|
|
|
Status.login = LOGIN_FAILED_INVALID_PASSPHRASE;
|
|
|
|
Status.sync = CREDENTIALS_CHANGED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// Assume that every other failure is network-related.
|
|
|
|
Status.login = LOGIN_FAILED_NETWORK_ERROR;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._log.info("... 'crypto' is not a reported collection. Generating new keys.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cryptoKeys) {
|
|
|
|
// Must have got a 404, or no reported collection.
|
|
|
|
// Better make some and upload them.
|
|
|
|
this.generateNewSymmetricKeys();
|
2010-12-09 10:32:03 -08:00
|
|
|
|
|
|
|
// Oh, and reset the client so we reupload, too.
|
|
|
|
this.resetClient();
|
2010-11-29 16:41:17 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last-ditch case.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No update needed: we're good!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
// This means no keys are present, or there's a network error.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-06-14 13:03:39 -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;
|
2010-04-20 18:56:44 -07:00
|
|
|
Svc.Obs.notify("weave:service:sync:delayed");
|
2009-09-29 18:33:41 -07:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-21 18:03:56 -07:00
|
|
|
|
2010-06-14 13:03:39 -07:00
|
|
|
if (!this.username) {
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.warn("No username in verifyLogin.");
|
2010-06-14 13:03:39 -07:00
|
|
|
Status.login = LOGIN_FAILED_NO_USERNAME;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-19 18:22:22 -07:00
|
|
|
try {
|
2010-11-29 16:41:17 -08:00
|
|
|
// Fetch collection info on every startup.
|
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:
|
2010-06-14 13:03:39 -07:00
|
|
|
// The user is authenticated.
|
2010-11-29 16:41:17 -08:00
|
|
|
|
|
|
|
// We have no way of verifying the passphrase right now,
|
|
|
|
// so wait until remoteSetup to do so.
|
|
|
|
// Just make the most trivial checks.
|
2010-06-14 13:03:39 -07:00
|
|
|
if (!this.passphrase) {
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.warn("No passphrase in verifyLogin.");
|
2010-06-14 13:03:39 -07:00
|
|
|
Status.login = LOGIN_FAILED_NO_PASSPHRASE;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Go ahead and do remote setup, so that we can determine
|
|
|
|
// conclusively that our passphrase is correct.
|
|
|
|
if (this._remoteSetup()) {
|
2009-09-28 16:13:42 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Username/password verified.
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.login = LOGIN_SUCCEEDED;
|
2009-08-19 20:27:22 -07:00
|
|
|
return true;
|
2010-11-29 16:41:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
this._log.warn("Remote setup failed.");
|
|
|
|
// Remote setup must have failed.
|
|
|
|
return false;
|
2009-09-28 16:13:42 -07:00
|
|
|
|
2009-08-19 20:27:22 -07:00
|
|
|
case 401:
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.warn("401: login failed.");
|
2010-07-19 15:28:54 -07:00
|
|
|
// Login failed. If the password contains non-ASCII characters,
|
|
|
|
// perhaps the server password is an old low-byte only one?
|
|
|
|
let id = ID.get('WeaveID');
|
|
|
|
if (id.password != id.passwordUTF8) {
|
|
|
|
let res = new Resource(this.infoURL);
|
|
|
|
let auth = new BrokenBasicAuthenticator(id);
|
|
|
|
res.authenticator = auth;
|
|
|
|
test = res.get();
|
|
|
|
if (test.status == 200) {
|
|
|
|
this._log.debug("Non-ASCII password detected. "
|
|
|
|
+ "Changing to UTF-8 version.");
|
|
|
|
// Let's change the password on the server to the UTF8 version.
|
|
|
|
let url = this.userAPI + this.username + "/password";
|
|
|
|
res = new Resource(url);
|
|
|
|
res.authenticator = auth;
|
|
|
|
res.post(id.passwordUTF8);
|
|
|
|
return this.verifyLogin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Yes, we want to fall through to the 404 case.
|
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())
|
2010-06-14 13:03:39 -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
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
generateNewSymmetricKeys:
|
|
|
|
function WeaveSvc_generateNewSymmetricKeys() {
|
|
|
|
this._log.info("Generating new keys....");
|
|
|
|
CollectionKeys.generateNewKeys();
|
|
|
|
let wbo = CollectionKeys.asWBO("crypto", "keys");
|
|
|
|
this._log.info("Encrypting new key bundle. Modified time is " + wbo.modified);
|
|
|
|
wbo.encrypt(this.syncKeyBundle);
|
|
|
|
|
|
|
|
this._log.info("Uploading...");
|
|
|
|
let uploadRes = wbo.upload(this.cryptoKeysURL);
|
|
|
|
if (uploadRes.status >= 400) {
|
|
|
|
this._log.warn("Got status " + uploadRes.status + " uploading new keys. What to do? Throw!");
|
|
|
|
throw new Error("Unable to upload symmetric keys.");
|
|
|
|
}
|
|
|
|
this._log.info("Got status " + uploadRes.status);
|
|
|
|
},
|
2008-07-11 17:40:52 -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 {
|
2010-07-19 15:28:54 -07:00
|
|
|
let resp = new Resource(url).post(Utils.encodeUTF8(newpass));
|
2009-09-28 16:28:38 -07:00
|
|
|
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
|
|
|
|
2010-11-29 16:41:17 -08: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
|
|
|
|
2010-03-31 18:58:07 -07:00
|
|
|
changePassphrase: function WeaveSvc_changePassphrase(newphrase)
|
|
|
|
this._catch(this._notify("changepph", "", function() {
|
2010-11-29 16:41:17 -08:00
|
|
|
/* Wipe. */
|
2009-11-12 12:44:33 -08:00
|
|
|
this.wipeServer();
|
|
|
|
|
|
|
|
this.logout();
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
/* Set this so UI is updated on next run. */
|
2009-07-21 14:31:16 -07:00
|
|
|
this.passphrase = newphrase;
|
2010-05-04 13:56:12 -07:00
|
|
|
this.persistLogin();
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
/* Login and sync. This also generates new keys. */
|
2010-03-31 18:58:07 -07:00
|
|
|
this.login();
|
2009-07-21 14:31:16 -07:00
|
|
|
this.sync(true);
|
|
|
|
return true;
|
|
|
|
}))(),
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2010-04-01 13:43:09 -07:00
|
|
|
startOver: function() {
|
2010-04-02 16:30:09 -07:00
|
|
|
// Set a username error so the status message shows "set up..."
|
2010-04-21 17:40:42 -07:00
|
|
|
Status.login = LOGIN_FAILED_NO_USERNAME;
|
2010-04-01 13:43:09 -07:00
|
|
|
this.logout();
|
2010-11-29 16:41:17 -08:00
|
|
|
// Reset all engines.
|
2010-04-01 13:43:09 -07:00
|
|
|
this.resetClient();
|
2010-11-29 16:41:17 -08:00
|
|
|
// Reset Weave prefs.
|
2010-09-07 09:44:01 -07:00
|
|
|
this._ignorePrefObserver = true;
|
2010-04-01 13:43:09 -07:00
|
|
|
Svc.Prefs.resetBranch("");
|
2010-09-07 09:44:01 -07:00
|
|
|
this._ignorePrefObserver = false;
|
2010-11-29 16:41:17 -08:00
|
|
|
|
2010-12-07 16:30:06 -08:00
|
|
|
// Clear keys.
|
|
|
|
CollectionKeys.clear();
|
|
|
|
|
2010-05-07 13:16:37 -07:00
|
|
|
Svc.Prefs.set("lastversion", WEAVE_VERSION);
|
2010-04-01 13:43:09 -07:00
|
|
|
// Find weave logins and remove them.
|
2010-05-07 15:39:58 -07:00
|
|
|
this.password = "";
|
|
|
|
this.passphrase = "";
|
2010-04-01 13:43:09 -07:00
|
|
|
Svc.Login.findLogins({}, PWDMGR_HOST, "", "").map(function(login) {
|
|
|
|
Svc.Login.removeLogin(login);
|
|
|
|
});
|
2010-04-21 17:40:42 -07:00
|
|
|
Svc.Obs.notify("weave:service:start-over");
|
2010-08-06 08:30:58 -07:00
|
|
|
Svc.Obs.notify("weave:engine:stop-tracking");
|
2010-04-01 13:43:09 -07:00
|
|
|
},
|
|
|
|
|
2010-06-10 17:04:49 -07:00
|
|
|
delayedAutoConnect: function delayedAutoConnect(delay) {
|
|
|
|
if (this._loggedIn)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (this._checkSetup() == STATUS_OK && Svc.Prefs.get("autoconnect")) {
|
|
|
|
Utils.delay(this._autoConnect, delay * 1000, this, "_autoTimer");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-09-25 11:46:29 -07:00
|
|
|
_autoConnect: let (attempts = 0) function _autoConnect() {
|
2010-04-08 14:03:10 -07:00
|
|
|
let reason =
|
|
|
|
Utils.mpLocked() ? "master password still locked"
|
|
|
|
: this._checkSync([kSyncNotLoggedIn, kFirstSyncChoiceNotMade]);
|
2010-01-07 16:13:44 -08:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Can't autoconnect if we're missing these values.
|
2010-01-07 16:13:44 -08:00
|
|
|
if (!reason) {
|
|
|
|
if (!this.username || !this.password || !this.passphrase)
|
|
|
|
return;
|
2009-08-19 20:27:22 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Nothing more to do on a successful login.
|
2010-01-07 16:13:44 -08:00
|
|
|
if (this.login())
|
|
|
|
return;
|
|
|
|
}
|
2009-08-19 20:27:22 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Something failed, so try again some time later.
|
2009-09-25 11:46:29 -07:00
|
|
|
let interval = this._calculateBackoff(++attempts, 60 * 1000);
|
2010-01-07 16:13:44 -08:00
|
|
|
this._log.debug("Autoconnect failed: " + (reason || Status.login) +
|
|
|
|
"; retry in " + Math.ceil(interval / 1000) + " sec.");
|
2009-09-24 22:51:38 -07:00
|
|
|
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() {
|
2010-11-29 16:41:17 -08:00
|
|
|
// Canceled master password prompt can prevent these from succeeding.
|
2009-09-03 21:11:32 -07:00
|
|
|
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)
|
2010-11-29 16:41:17 -08:00
|
|
|
this._catch(this._lock("service.js: login",
|
|
|
|
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
|
|
|
|
2010-08-09 16:46:54 -07:00
|
|
|
let initialStatus = this._checkSetup();
|
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
|
|
|
|
2010-04-09 10:20:41 -07:00
|
|
|
if (this._checkSetup() == CLIENT_NOT_CONFIGURED)
|
|
|
|
throw "aborting login, client not configured";
|
|
|
|
|
2010-08-09 16:46:54 -07:00
|
|
|
// Calling login() with parameters when the client was
|
|
|
|
// previously not configured means setup was completed.
|
|
|
|
if (initialStatus == CLIENT_NOT_CONFIGURED
|
|
|
|
&& (username || password || passphrase))
|
|
|
|
Svc.Obs.notify("weave:service:setup-complete");
|
|
|
|
|
2009-09-10 22:57:36 -07:00
|
|
|
this._log.info("Logging in user " + this.username);
|
2009-03-11 18:29:18 -07:00
|
|
|
|
2010-06-14 13:03:39 -07:00
|
|
|
if (!this.verifyLogin()) {
|
2010-04-09 10:20:41 -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
|
|
|
}
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// No need to try automatically connecting after a successful login.
|
2009-09-24 22:51:38 -07:00
|
|
|
if (this._autoTimer)
|
|
|
|
this._autoTimer.clear();
|
|
|
|
|
2009-03-11 18:29:18 -07:00
|
|
|
this._loggedIn = true;
|
2010-11-29 16:41:17 -08: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() {
|
2010-11-29 16:41:17 -08:00
|
|
|
// No need to do anything if we're already logged out.
|
2009-09-30 15:16:56 -07:00
|
|
|
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;
|
2009-03-10 04:15:52 -07:00
|
|
|
|
2010-11-29 16:41:17 -08: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
|
|
|
|
2010-04-20 18:56:44 -07:00
|
|
|
Svc.Obs.notify("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";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-09-13 08:17:37 -07:00
|
|
|
checkAccount: function checkAccount(account) {
|
|
|
|
let username = this._usernameFromAccount(account);
|
2010-09-15 03:54:33 -07:00
|
|
|
return this.checkUsername(username);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Backwards compat with the Firefox UI. Fold into checkAccount() once
|
|
|
|
// bug 595066 has landed.
|
|
|
|
checkUsername: function 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();
|
2010-05-06 21:02:00 -07:00
|
|
|
if (data.status == 200) {
|
|
|
|
if (data == "0")
|
|
|
|
return "available";
|
|
|
|
else if (data == "1")
|
|
|
|
return "notAvailable";
|
|
|
|
}
|
|
|
|
|
2009-08-20 10:20:01 -07:00
|
|
|
}
|
|
|
|
catch(ex) {}
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Convert to the error string, or default to generic on exception.
|
2009-08-18 20:03:11 -07:00
|
|
|
return this._errorStr(data);
|
|
|
|
},
|
|
|
|
|
2010-09-15 03:54:33 -07:00
|
|
|
createAccount: function createAccount() {
|
|
|
|
// Backwards compat with the Firefox UI. Change to signature to
|
|
|
|
// (email, password, captchaChallenge, captchaResponse) once
|
|
|
|
// bug 595066 has landed.
|
|
|
|
let username, email, password, captchaChallenge, captchaResponse;
|
|
|
|
if (arguments.length == 4) {
|
|
|
|
[email, password, captchaChallenge, captchaResponse] = arguments;
|
|
|
|
username = this._usernameFromAccount(email);
|
|
|
|
} else {
|
|
|
|
[username, password, email, captchaChallenge, captchaResponse] = arguments;
|
|
|
|
}
|
|
|
|
|
2009-08-25 17:06:13 -07:00
|
|
|
let payload = JSON.stringify({
|
2010-07-19 15:28:54 -07:00
|
|
|
"password": Utils.encodeUTF8(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);
|
2010-08-25 15:49:44 -07:00
|
|
|
res.authenticator = new NoOpAuthenticator();
|
2010-06-17 08:47:13 -07:00
|
|
|
|
|
|
|
// Hint to server to allow scripted user creation or otherwise
|
|
|
|
// ignore captcha.
|
2010-05-04 13:55:34 -07:00
|
|
|
if (Svc.Prefs.isSet("admin-secret"))
|
|
|
|
res.setHeader("X-Weave-Secret", Svc.Prefs.get("admin-secret", ""));
|
2009-06-06 16:24:17 -07:00
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Stuff we need to do after login, before we can really do
|
|
|
|
// anything (e.g. key setup).
|
|
|
|
_remoteSetup: function WeaveSvc__remoteSetup(infoResponse) {
|
2009-11-12 12:44:33 -08:00
|
|
|
let reset = false;
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.debug("Fetching global metadata record");
|
2010-08-23 06:20:39 -07:00
|
|
|
let meta = Records.get(this.metaURL);
|
2010-12-06 14:22:09 -08:00
|
|
|
|
|
|
|
// Checking modified time of the meta record.
|
|
|
|
if (infoResponse &&
|
|
|
|
(infoResponse.obj.meta != this.metaModified) &&
|
2010-12-09 19:37:02 -08:00
|
|
|
(!meta || !meta.isNew)) {
|
2010-12-06 14:22:09 -08:00
|
|
|
|
|
|
|
// Delete the cached meta record...
|
|
|
|
this._log.debug("Clearing cached meta record. metaModified is " +
|
|
|
|
JSON.stringify(this.metaModified) + ", setting to " +
|
2010-12-06 15:09:14 -08:00
|
|
|
JSON.stringify(infoResponse.obj.meta));
|
2010-12-09 19:37:02 -08:00
|
|
|
|
2010-12-06 14:22:09 -08:00
|
|
|
Records.del(this.metaURL);
|
|
|
|
|
|
|
|
// ... fetch the current record from the server, and COPY THE FLAGS.
|
|
|
|
let newMeta = Records.get(this.metaURL);
|
2010-12-09 19:37:02 -08:00
|
|
|
|
|
|
|
if (!Records.response.success || !newMeta) {
|
|
|
|
this._log.debug("No meta/global record on the server. Creating one.");
|
|
|
|
newMeta = new WBORecord("meta", "global");
|
|
|
|
newMeta.payload.syncID = this.syncID;
|
|
|
|
newMeta.payload.storageVersion = STORAGE_VERSION;
|
|
|
|
|
|
|
|
newMeta.isNew = true;
|
|
|
|
|
|
|
|
Records.set(this.metaURL, newMeta);
|
|
|
|
if (!newMeta.upload(this.metaURL).success) {
|
|
|
|
this._log.warn("Unable to upload new meta/global. Failing remote setup.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If newMeta, then it stands to reason that meta != null.
|
|
|
|
newMeta.isNew = meta.isNew;
|
|
|
|
newMeta.changed = meta.changed;
|
|
|
|
}
|
|
|
|
|
2010-12-06 14:22:09 -08:00
|
|
|
// Switch in the new meta object and record the new time.
|
|
|
|
meta = newMeta;
|
2010-12-06 15:09:14 -08:00
|
|
|
this.metaModified = infoResponse.obj.meta;
|
2010-12-06 14:22:09 -08:00
|
|
|
}
|
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
|
|
|
|
2010-03-16 16:31:55 -07:00
|
|
|
this._log.debug(["Weave Version:", WEAVE_VERSION, "Local Storage:",
|
|
|
|
STORAGE_VERSION, "Remote Storage:", remoteVersion].join(" "));
|
2009-03-02 14:15:46 -08:00
|
|
|
|
2010-03-16 16:31:55 -07:00
|
|
|
// Check for cases that require a fresh start. When comparing remoteVersion,
|
|
|
|
// we need to convert it to a number as older clients used it as a string.
|
2009-02-23 12:36:55 -08:00
|
|
|
if (!meta || !meta.payload.storageVersion || !meta.payload.syncID ||
|
2010-03-16 16:31:55 -07:00
|
|
|
STORAGE_VERSION > parseFloat(remoteVersion)) {
|
2010-12-06 14:22:09 -08:00
|
|
|
|
|
|
|
this._log.info("One of: no meta, no meta storageVersion, or no meta syncID. Fresh start needed.");
|
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-03-03 11:57:30 -08:00
|
|
|
|
2009-11-12 12:44:33 -08:00
|
|
|
reset = true;
|
2010-11-29 16:41:17 -08:00
|
|
|
|
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)
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.info("Metadata record not found, server was wiped to ensure " +
|
2009-02-23 16:56:23 -08:00
|
|
|
"consistency.");
|
|
|
|
else // 200
|
2010-03-16 16:31:55 -07:00
|
|
|
this._log.info("Wiped server; incompatible metadata: " + remoteVersion);
|
2009-02-23 12:36:55 -08:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
return true;
|
2010-03-16 16:31:55 -07:00
|
|
|
}
|
|
|
|
else if (remoteVersion > STORAGE_VERSION) {
|
2009-10-07 10:47:43 -07:00
|
|
|
Status.sync = VERSION_OUT_OF_DATE;
|
2010-03-16 16:31:55 -07:00
|
|
|
this._log.warn("Upgrade required to access newer storage version.");
|
2009-06-06 11:53:23 -07:00
|
|
|
return false;
|
2010-03-25 09:52:45 -07:00
|
|
|
}
|
|
|
|
else if (meta.payload.syncID != this.syncID) {
|
2010-12-06 14:22:09 -08:00
|
|
|
|
|
|
|
this._log.info("Sync IDs differ. Local is " + this.syncID + ", remote is " + meta.payload.syncID);
|
2010-04-01 15:54:53 -07:00
|
|
|
this.resetClient();
|
2010-03-25 09:52:45 -07:00
|
|
|
this.syncID = meta.payload.syncID;
|
|
|
|
this._log.debug("Clear cached values and take syncId: " + this.syncID);
|
2009-12-02 17:57:13 -08:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
if (!this.upgradeSyncKey(meta.payload.syncID)) {
|
|
|
|
this._log.warn("Failed to upgrade sync key. Failing remote setup.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.verifyAndFetchSymmetricKeys(infoResponse)) {
|
|
|
|
this._log.warn("Failed to fetch symmetric keys. Failing remote setup.");
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-02 16:38:05 -07:00
|
|
|
|
2010-03-31 18:58:07 -07:00
|
|
|
// bug 545725 - re-verify creds and fail sanely
|
2010-06-14 13:03:39 -07:00
|
|
|
if (!this.verifyLogin()) {
|
2010-03-31 18:58:07 -07:00
|
|
|
Status.sync = CREDENTIALS_CHANGED;
|
|
|
|
this._log.info("Credentials have changed, aborting sync and forcing re-login.");
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-11 23:54:20 -08:00
|
|
|
|
2010-12-01 14:59:33 -08:00
|
|
|
return true;
|
2008-11-19 16:18:35 -08:00
|
|
|
}
|
2010-11-29 16:41:17 -08:00
|
|
|
else {
|
|
|
|
if (!this.upgradeSyncKey(meta.payload.syncID)) {
|
|
|
|
this._log.warn("Failed to upgrade sync key. Failing remote setup.");
|
2009-06-06 11:53:23 -07:00
|
|
|
return false;
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
if (!this.verifyAndFetchSymmetricKeys(infoResponse)) {
|
|
|
|
this._log.warn("Failed to fetch symmetric keys. Failing remote setup.");
|
2009-06-06 11:53:23 -07:00
|
|
|
return false;
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
|
|
|
|
2010-12-01 14:59:33 -08:00
|
|
|
return true;
|
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.
|
2010-04-08 14:03:10 -07:00
|
|
|
*
|
|
|
|
* @param ignore [optional]
|
|
|
|
* array of reasons to ignore when checking
|
2009-03-10 04:15:52 -07:00
|
|
|
*
|
|
|
|
* @return Reason for not syncing; not-truthy if sync should run
|
|
|
|
*/
|
2010-04-08 14:03:10 -07:00
|
|
|
_checkSync: function WeaveSvc__checkSync(ignore) {
|
2009-03-10 04:15:52 -07:00
|
|
|
let reason = "";
|
|
|
|
if (!this.enabled)
|
|
|
|
reason = kSyncWeaveDisabled;
|
2009-03-10 04:30:36 -07:00
|
|
|
else if (Svc.IO.offline)
|
|
|
|
reason = kSyncNetworkOffline;
|
2009-10-07 10:47:43 -07:00
|
|
|
else if (Status.minimumNextSync > Date.now())
|
2009-09-15 18:38:52 -07:00
|
|
|
reason = kSyncBackoffNotMet;
|
2010-04-08 14:03:10 -07:00
|
|
|
else if (!this._loggedIn)
|
|
|
|
reason = kSyncNotLoggedIn;
|
|
|
|
else if (Svc.Prefs.get("firstSync") == "notReady")
|
|
|
|
reason = kFirstSyncChoiceNotMade;
|
|
|
|
|
|
|
|
if (ignore && ignore.indexOf(reason) != -1)
|
|
|
|
return "";
|
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();
|
2010-03-25 14:24:41 -07:00
|
|
|
if (this._heartbeatTimer)
|
|
|
|
this._heartbeatTimer.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
|
2010-04-08 14:03:10 -07:00
|
|
|
if (this._checkSync([kSyncBackoffNotMet])) {
|
2009-09-24 19:04:06 -07:00
|
|
|
this._clearSyncTriggers();
|
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) {
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.debug("Syncing as soon as we're idle.");
|
2009-09-25 11:46:29 -07:00
|
|
|
this.syncOnIdle();
|
|
|
|
return;
|
|
|
|
}
|
2009-07-22 20:48:41 -07:00
|
|
|
|
2009-12-17 18:51:55 -08:00
|
|
|
this._log.trace("Next sync in " + Math.ceil(interval / 1000) + " sec.");
|
2009-09-24 22:51:38 -07:00
|
|
|
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;
|
2010-03-25 14:24:41 -07:00
|
|
|
|
|
|
|
// if we're a single client, set up a heartbeat to detect new clients sooner
|
|
|
|
if (this.numClients == 1)
|
|
|
|
this._scheduleHeartbeat();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hits info/collections on the server to see if there are new clients.
|
2010-04-02 16:38:05 -07:00
|
|
|
* This is only called when the account has one active client, and if more
|
2010-03-25 14:24:41 -07:00
|
|
|
* are found will trigger a sync to change client sync frequency and update data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
_doHeartbeat: function WeaveSvc__doHeartbeat() {
|
|
|
|
if (this._heartbeatTimer)
|
|
|
|
this._heartbeatTimer.clear();
|
|
|
|
|
2010-04-29 08:50:46 -07:00
|
|
|
this.nextHeartbeat = 0;
|
2010-03-25 14:24:41 -07:00
|
|
|
let info = null;
|
|
|
|
try {
|
|
|
|
info = new Resource(this.infoURL).get();
|
|
|
|
if (info && info.success) {
|
|
|
|
// if clients.lastModified doesn't match what the server has,
|
|
|
|
// we have another client in play
|
2010-04-02 16:38:05 -07:00
|
|
|
this._log.trace("Remote timestamp:" + info.obj["clients"] +
|
2010-03-25 14:24:41 -07:00
|
|
|
" Local timestamp: " + Clients.lastSync);
|
|
|
|
if (info.obj["clients"] > Clients.lastSync) {
|
|
|
|
this._log.debug("New clients detected, triggering a full sync");
|
|
|
|
this.syncOnIdle();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._checkServerError(info);
|
|
|
|
this._log.debug("Heartbeat failed. HTTP Error: " + info.status);
|
|
|
|
}
|
|
|
|
} catch(ex) {
|
|
|
|
// if something throws unexpectedly, not a big deal
|
|
|
|
this._log.debug("Heartbeat failed unexpectedly: " + ex);
|
2010-04-02 16:38:05 -07:00
|
|
|
}
|
2010-03-25 14:24:41 -07:00
|
|
|
|
|
|
|
// no joy, schedule the next heartbeat
|
|
|
|
this._scheduleHeartbeat();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up a heartbeat ping to check for new clients. This is not a critical
|
2010-04-02 16:38:05 -07:00
|
|
|
* behaviour for the client, so if we hit server/network issues, we'll just drop
|
2010-03-25 14:24:41 -07:00
|
|
|
* this until the next sync.
|
|
|
|
*/
|
|
|
|
_scheduleHeartbeat: function WeaveSvc__scheduleNextHeartbeat() {
|
2010-04-29 08:50:46 -07:00
|
|
|
if (this._heartbeatTimer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let now = Date.now();
|
|
|
|
if (this.nextHeartbeat && this.nextHeartbeat < now) {
|
|
|
|
this._doHeartbeat();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the next sync is in less than an hour, don't bother
|
2010-03-25 14:24:41 -07:00
|
|
|
let interval = MULTI_DESKTOP_SYNC;
|
|
|
|
if (this.nextSync < Date.now() + interval ||
|
|
|
|
Status.enforceBackoff)
|
|
|
|
return;
|
|
|
|
|
2010-04-29 08:50:46 -07:00
|
|
|
if (this.nextHeartbeat)
|
|
|
|
interval = this.nextHeartbeat - now;
|
|
|
|
else
|
|
|
|
this.nextHeartbeat = now + interval;
|
|
|
|
|
2010-04-02 16:38:05 -07:00
|
|
|
this._log.trace("Setting up heartbeat, next ping in " +
|
2010-03-25 14:24:41 -07:00
|
|
|
Math.ceil(interval / 1000) + " sec.");
|
|
|
|
Utils.delay(function() this._doHeartbeat(), interval, this, "_heartbeatTimer");
|
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()
|
2010-11-29 16:41:17 -08:00
|
|
|
this._catch(this._lock("service.js: sync",
|
|
|
|
this._notify("sync", "", function() {
|
|
|
|
|
|
|
|
this._log.info("In sync().");
|
2009-11-12 10:11:54 -08:00
|
|
|
|
2010-05-03 14:01:08 -07:00
|
|
|
let syncStartTime = Date.now();
|
|
|
|
|
2010-04-05 20:53:31 -07:00
|
|
|
Status.resetSync();
|
2009-09-29 18:33:41 -07:00
|
|
|
|
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
|
|
|
|
2010-04-05 20:53:31 -07:00
|
|
|
// if we don't have a node, get one. if that fails, retry in 10 minutes
|
|
|
|
if (this.clusterURL == "" && !this._setCluster()) {
|
2010-07-15 17:02:06 -07:00
|
|
|
Status.sync = NO_SYNC_NODE_FOUND;
|
2010-04-05 20:53:31 -07:00
|
|
|
this._scheduleNextSync(10 * 60 * 1000);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
2010-04-29 08:50:46 -07:00
|
|
|
this.nextHeartbeat = 0;
|
2009-09-15 18:38:52 -07:00
|
|
|
|
2010-04-02 16:38:05 -07:00
|
|
|
// reset backoff info, if the server tells us to continue backing off,
|
2010-03-25 14:24:41 -07:00
|
|
|
// we'll handle that later
|
|
|
|
Status.resetBackoff();
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Ping the server with a special info request once a day.
|
2009-12-16 19:08:36 -08:00
|
|
|
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
|
2009-12-22 13:46:34 -08:00
|
|
|
infoURL += "?v=" + WEAVE_VERSION;
|
2009-12-16 19:08:36 -08:00
|
|
|
Svc.Prefs.set("lastPing", now);
|
|
|
|
}
|
|
|
|
|
2009-09-03 21:30:40 -07:00
|
|
|
// Figure out what the last modified time is for each collection
|
2010-11-29 16:41:17 -08:00
|
|
|
let info = this._fetchInfo(infoURL, true);
|
2010-08-05 07:52:17 -07:00
|
|
|
this.globalScore = 0;
|
2009-09-03 21:30:40 -07:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
if (!(this._remoteSetup(info)))
|
2010-05-05 17:16:17 -07:00
|
|
|
throw "aborting sync, remote setup failed";
|
|
|
|
|
|
|
|
// Make sure we have an up-to-date list of clients before sending commands
|
2009-09-10 22:57:36 -07:00
|
|
|
this._log.trace("Refreshing client list");
|
2010-09-11 09:39:21 -07:00
|
|
|
this._syncEngine(Clients);
|
2008-07-31 01:02:41 -07:00
|
|
|
|
2010-05-05 17:16:17 -07:00
|
|
|
// Wipe data in the desired direction if necessary
|
|
|
|
switch (Svc.Prefs.get("firstSync")) {
|
|
|
|
case "resetClient":
|
|
|
|
this.resetClient(Engines.getEnabled().map(function(e) e.name));
|
|
|
|
break;
|
|
|
|
case "wipeClient":
|
|
|
|
this.wipeClient(Engines.getEnabled().map(function(e) e.name));
|
|
|
|
break;
|
|
|
|
case "wipeRemote":
|
|
|
|
this.wipeRemote(Engines.getEnabled().map(function(e) e.name));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-11-12 12:44:33 -08:00
|
|
|
// Process the incoming commands if we have any
|
2010-03-16 16:39:08 -07:00
|
|
|
if (Clients.localCommands) {
|
2009-11-12 12:44:33 -08:00
|
|
|
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
|
2010-11-29 16:41:17 -08:00
|
|
|
if (!(this._remoteSetup(info)))
|
2009-11-12 12:44:33 -08:00
|
|
|
throw "aborting sync, remote setup failed after processing commands";
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
// Always immediately push back the local client (now without commands)
|
2010-09-11 09:39:21 -07:00
|
|
|
this._syncEngine(Clients);
|
2009-11-12 12:44:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-07 09:44:01 -07:00
|
|
|
// Update the client mode and engines because it might change what we sync.
|
2009-09-25 15:05:09 -07:00
|
|
|
this._updateClientMode();
|
2010-09-07 09:44:01 -07:00
|
|
|
this._updateEnabledEngines();
|
2009-09-25 15:05:09 -07:00
|
|
|
|
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
|
|
|
|
2010-03-25 10:05:21 -07:00
|
|
|
// Upload meta/global if any engines changed anything
|
|
|
|
let meta = Records.get(this.metaURL);
|
2010-09-13 08:17:37 -07:00
|
|
|
if (meta.isNew || meta.changed) {
|
2010-11-29 16:41:17 -08:00
|
|
|
new Resource(this.metaURL).put(meta);
|
2010-09-13 08:17:37 -07:00
|
|
|
delete meta.isNew;
|
|
|
|
delete meta.changed;
|
|
|
|
}
|
2010-03-25 10:05:21 -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;
|
2010-05-03 14:01:08 -07:00
|
|
|
let syncTime = ((Date.now() - syncStartTime) / 1000).toFixed(2);
|
|
|
|
this._log.info("Sync completed successfully in " + syncTime + " secs.");
|
2009-03-10 04:07:24 -07:00
|
|
|
}
|
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() {
|
|
|
|
// Nothing to do if it's the same amount
|
2010-03-16 16:39:08 -07:00
|
|
|
let {numClients, hasMobile} = Clients.stats;
|
2009-09-25 15:05:09 -07:00
|
|
|
if (this.numClients == numClients)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._log.debug("Client count: " + this.numClients + " -> " + numClients);
|
|
|
|
this.numClients = numClients;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-09-07 09:44:01 -07:00
|
|
|
_updateEnabledEngines: function _updateEnabledEngines() {
|
2010-12-07 16:18:22 -08:00
|
|
|
this._log.info("Updating enabled engines: " + this.numClients + " clients.");
|
2010-09-07 09:44:01 -07:00
|
|
|
let meta = Records.get(this.metaURL);
|
2010-09-13 08:17:37 -07:00
|
|
|
if (meta.isNew || !meta.payload.engines)
|
2010-09-07 09:44:01 -07:00
|
|
|
return;
|
2010-12-06 17:23:38 -08:00
|
|
|
|
|
|
|
// If we're the only client, and no engines are marked as enabled,
|
|
|
|
// thumb our noses at the server data: it can't be right.
|
|
|
|
// Belt-and-suspenders approach to Bug 615926.
|
|
|
|
if ((this.numClients <= 1) &&
|
|
|
|
([e for (e in meta.payload.engines) if (e != "clients")].length == 0)) {
|
|
|
|
this._log.info("One client and no enabled engines: not touching local engine status.");
|
|
|
|
return;
|
|
|
|
}
|
2010-09-07 09:44:01 -07:00
|
|
|
|
|
|
|
this._ignorePrefObserver = true;
|
|
|
|
|
|
|
|
let enabled = [eng.name for each (eng in Engines.getEnabled())];
|
|
|
|
for (let engineName in meta.payload.engines) {
|
|
|
|
let index = enabled.indexOf(engineName);
|
|
|
|
if (index != -1) {
|
|
|
|
// The engine is enabled locally. Nothing to do.
|
|
|
|
enabled.splice(index, 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let engine = Engines.get(engineName);
|
|
|
|
if (!engine) {
|
|
|
|
// The engine doesn't exist locally. Nothing to do.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-09-08 12:09:40 -07:00
|
|
|
if (Svc.Prefs.get("engineStatusChanged." + engine.prefName, false)) {
|
2010-09-07 09:44:01 -07:00
|
|
|
// The engine was disabled locally. Wipe server data and
|
|
|
|
// disable it everywhere.
|
|
|
|
this._log.trace("Wiping data for " + engineName + " engine.");
|
|
|
|
engine.wipeServer();
|
|
|
|
delete meta.payload.engines[engineName];
|
|
|
|
meta.changed = true;
|
|
|
|
} else {
|
|
|
|
// The engine was enabled remotely. Enable it locally.
|
|
|
|
this._log.trace(engineName + " engine was enabled remotely.");
|
|
|
|
engine.enabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any remaining engines were either enabled locally or disabled remotely.
|
|
|
|
for each (engineName in enabled) {
|
2010-09-08 12:09:40 -07:00
|
|
|
let engine = Engines.get(engineName);
|
|
|
|
if (Svc.Prefs.get("engineStatusChanged." + engine.prefName, false)) {
|
2010-09-07 09:44:01 -07:00
|
|
|
this._log.trace("The " + engineName + " engine was enabled locally.");
|
|
|
|
} else {
|
|
|
|
this._log.trace("The " + engineName + " engine was disabled remotely.");
|
2010-09-08 12:09:40 -07:00
|
|
|
engine.enabled = false;
|
2010-09-07 09:44:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-08 12:09:40 -07:00
|
|
|
Svc.Prefs.resetBranch("engineStatusChanged.");
|
2010-09-07 09:44:01 -07:00
|
|
|
this._ignorePrefObserver = false;
|
|
|
|
},
|
|
|
|
|
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;
|
2010-11-23 21:21:31 -08:00
|
|
|
this._log.debug(engine.name + " failed: " + Utils.exceptionStr(e));
|
2009-08-31 17:17:20 -07:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
/**
|
|
|
|
* Silently fixes case issues.
|
|
|
|
*/
|
|
|
|
syncKeyNeedsUpgrade: function syncKeyNeedsUpgrade() {
|
|
|
|
let p = this.passphrase;
|
|
|
|
|
|
|
|
// Check whether it's already a key that we generated.
|
|
|
|
if (Utils.isPassphrase(p)) {
|
|
|
|
this._log.info("Sync key is up-to-date: no need to upgrade.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we have a passphrase, rather than a 25-alphadigit sync key,
|
|
|
|
* use the provided sync ID to bootstrap it using PBKDF2.
|
|
|
|
*
|
|
|
|
* Store the new 'passphrase' back into the identity manager.
|
|
|
|
*
|
|
|
|
* We can check this as often as we want, because once it's done the
|
|
|
|
* check will no longer succeed. It only matters that it happens after
|
|
|
|
* we decide to bump the server storage version.
|
|
|
|
*/
|
|
|
|
upgradeSyncKey: function upgradeSyncKey(syncID) {
|
|
|
|
let p = this.passphrase;
|
|
|
|
|
|
|
|
// Check whether it's already a key that we generated.
|
|
|
|
if (!this.syncKeyNeedsUpgrade(p))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Otherwise, let's upgrade it.
|
|
|
|
// N.B., we persist the sync key without testing it first...
|
|
|
|
|
|
|
|
let s = btoa(syncID); // It's what WeaveCrypto expects. *sigh*
|
|
|
|
let k = Utils.derivePresentableKeyFromPassphrase(p, s, PBKDF2_KEY_BYTES); // Base 32.
|
|
|
|
|
|
|
|
if (!k) {
|
|
|
|
this._log.error("No key resulted from derivePresentableKeyFromPassphrase. Failing upgrade.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._log.info("Upgrading sync key...");
|
|
|
|
this.passphrase = k;
|
|
|
|
this._log.info("Saving upgraded sync key...");
|
|
|
|
this.persistLogin();
|
|
|
|
this._log.info("Done saving.");
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2009-11-12 12:44:33 -08:00
|
|
|
_freshStart: function WeaveSvc__freshStart() {
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.info("Fresh start. Resetting client and considering key upgrade.");
|
2009-06-05 22:21:22 -07:00
|
|
|
this.resetClient();
|
2010-11-29 16:41:17 -08:00
|
|
|
this.upgradeSyncKey(this.syncID);
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
let meta = new WBORecord("meta", "global");
|
2010-03-25 09:52:45 -07:00
|
|
|
meta.payload.syncID = this.syncID;
|
2010-03-16 16:31:55 -07:00
|
|
|
meta.payload.storageVersion = STORAGE_VERSION;
|
2010-09-13 08:17:37 -07:00
|
|
|
meta.isNew = true;
|
2010-03-16 16:31:55 -07:00
|
|
|
|
|
|
|
this._log.debug("New metadata record: " + JSON.stringify(meta.payload));
|
2010-11-29 16:41:17 -08:00
|
|
|
let resp = new Resource(this.metaURL).put(meta);
|
2010-03-16 16:31:55 -07:00
|
|
|
if (!resp.success)
|
|
|
|
throw resp;
|
2010-11-29 16:41:17 -08:00
|
|
|
Records.set(this.metaURL, 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;
|
|
|
|
});
|
2010-11-29 16:41:17 -08:00
|
|
|
this.wipeServer(collections);
|
|
|
|
|
|
|
|
// Generate and upload new keys. Do this last so we don't wipe them...
|
|
|
|
this.generateNewSymmetricKeys();
|
2009-07-22 18:49:07 -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;
|
2010-06-01 15:15:53 -07:00
|
|
|
if (resp.status == 503 && resp.headers["retry-after"])
|
2010-09-11 09:39:21 -07:00
|
|
|
Svc.Obs.notify("weave:service:backoff:interval",
|
|
|
|
parseInt(resp.headers["retry-after"], 10));
|
2009-09-15 18:38:52 -07:00
|
|
|
}
|
2010-09-11 09:39:21 -07:00
|
|
|
if (resp.status == 400 && resp == RESPONSE_OVER_QUOTA)
|
|
|
|
Status.sync = OVER_QUOTA;
|
2009-09-15 18:38:52 -07:00
|
|
|
},
|
|
|
|
/**
|
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
|
|
|
/**
|
2010-04-07 17:06:37 -07:00
|
|
|
* Wipe user data from the server.
|
2009-03-11 23:33:14 -07:00
|
|
|
*
|
2010-04-07 17:06:37 -07:00
|
|
|
* @param collections [optional]
|
|
|
|
* Array of collections to wipe. If not given, all collections are wiped.
|
2010-11-29 16:41:33 -08:00
|
|
|
*
|
|
|
|
* @param includeKeys [optional]
|
|
|
|
* If true, keys/pubkey and keys/privkey are deleted from the server.
|
|
|
|
* This is false by default, which will cause the usual upgrade paths
|
|
|
|
* to leave those keys on the server. This is to solve Bug 614737: old
|
|
|
|
* clients check for keys *before* checking storage versions.
|
|
|
|
*
|
|
|
|
* Note that this parameter only has an effect if `collections` is not
|
|
|
|
* passed. If you explicitly pass a list of collections, they will be
|
|
|
|
* processed regardless of the value of `includeKeys`.
|
2009-02-26 22:14:32 -08:00
|
|
|
*/
|
2010-11-29 16:41:33 -08:00
|
|
|
wipeServer: function wipeServer(collections, includeKeyPairs)
|
2010-08-05 09:24:45 -07:00
|
|
|
this._notify("wipe-server", "", function() {
|
2010-04-07 17:06:37 -07:00
|
|
|
if (!collections) {
|
|
|
|
collections = [];
|
|
|
|
let info = new Resource(this.infoURL).get();
|
2010-11-29 16:41:33 -08:00
|
|
|
for (let name in info.obj) {
|
|
|
|
if (includeKeyPairs || (name != "keys"))
|
|
|
|
collections.push(name);
|
|
|
|
}
|
2010-04-07 17:06:37 -07:00
|
|
|
}
|
|
|
|
for each (let name in collections) {
|
2010-08-05 09:24:45 -07:00
|
|
|
let url = this.storageURL + name;
|
|
|
|
let response = new Resource(url).delete();
|
|
|
|
if (response.status != 200 && response.status != 404) {
|
|
|
|
throw "Aborting wipeServer. Server responded with "
|
|
|
|
+ response.status + " response for " + url;
|
2009-02-26 22:14:32 -08:00
|
|
|
}
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
2010-08-05 09:24:45 -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)
|
2010-10-05 11:32:56 -07:00
|
|
|
if (engine.canDecrypt())
|
2009-10-12 16:22:54 -07:00
|
|
|
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
|
2010-05-05 17:16:17 -07:00
|
|
|
this.resetClient(engines);
|
2009-12-10 18:39:51 -08:00
|
|
|
|
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
|
2010-03-25 09:52:45 -07:00
|
|
|
this.syncID = "";
|
2009-02-26 22:36:14 -08:00
|
|
|
Svc.Prefs.reset("lastSync");
|
2010-11-29 16:41:17 -08:00
|
|
|
Records.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-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() {
|
|
|
|
// Immediately clear out the commands as we've got them locally
|
2010-03-16 16:39:08 -07:00
|
|
|
let commands = Clients.localCommands;
|
|
|
|
Clients.clearCommands();
|
2009-11-12 12:44:33 -08:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2010-03-16 16:39:08 -07:00
|
|
|
// Send the command to all remote clients
|
|
|
|
this._log.debug("Sending clients: " + [command, args, commandData.desc]);
|
|
|
|
Clients.sendCommand(command, args);
|
2009-11-12 12:44:33 -08:00
|
|
|
},
|
2010-09-11 09:39:21 -07:00
|
|
|
|
|
|
|
_getInfo: function _getInfo(what)
|
|
|
|
this._catch(this._notify(what, "", function() {
|
|
|
|
let url = this.userBaseURL + "info/" + what;
|
|
|
|
let response = new Resource(url).get();
|
|
|
|
if (response.status != 200)
|
|
|
|
return null;
|
|
|
|
return response.obj;
|
|
|
|
}))(),
|
|
|
|
|
|
|
|
getCollectionUsage: function getCollectionUsage()
|
|
|
|
this._getInfo("collection_usage"),
|
|
|
|
|
|
|
|
getQuota: function getQuota() this._getInfo("quota")
|
|
|
|
|
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
|
2010-08-25 15:49:44 -07:00
|
|
|
Service.onStartup();
|