2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = ["Service"];
|
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-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;
|
|
|
|
|
2011-04-19 12:35:04 -07:00
|
|
|
const CRYPTO_COLLECTION = "crypto";
|
|
|
|
const KEYS_WBO = "keys";
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-08-29 14:43:39 -07:00
|
|
|
Cu.import("resource://services-common/log4moz.js");
|
|
|
|
Cu.import("resource://services-common/preferences.js");
|
2012-08-13 16:51:58 -07:00
|
|
|
Cu.import("resource://services-common/utils.js");
|
2010-06-16 14:30:08 -07:00
|
|
|
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/identity.js");
|
2012-08-29 14:43:39 -07:00
|
|
|
Cu.import("resource://services-sync/policies.js");
|
|
|
|
Cu.import("resource://services-sync/record.js");
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/resource.js");
|
2011-07-14 12:11:42 -07:00
|
|
|
Cu.import("resource://services-sync/rest.js");
|
2012-08-14 11:34:20 -07:00
|
|
|
Cu.import("resource://services-sync/stages/cluster.js");
|
2012-08-14 11:34:14 -07:00
|
|
|
Cu.import("resource://services-sync/stages/enginesync.js");
|
2012-08-29 14:43:39 -07:00
|
|
|
Cu.import("resource://services-sync/status.js");
|
|
|
|
Cu.import("resource://services-sync/userapi.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2012-08-29 14:43:40 -07:00
|
|
|
const ENGINE_MODULES = {
|
|
|
|
Addons: "addons.js",
|
|
|
|
Apps: "apps.js",
|
|
|
|
Bookmarks: "bookmarks.js",
|
|
|
|
Form: "forms.js",
|
|
|
|
History: "history.js",
|
|
|
|
Password: "passwords.js",
|
|
|
|
Prefs: "prefs.js",
|
|
|
|
Tab: "tabs.js",
|
|
|
|
};
|
|
|
|
|
2011-07-14 12:11:42 -07:00
|
|
|
const STORAGE_INFO_TYPES = [INFO_COLLECTIONS,
|
|
|
|
INFO_COLLECTION_USAGE,
|
|
|
|
INFO_COLLECTION_COUNTS,
|
|
|
|
INFO_QUOTA];
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
function Sync11Service() {
|
2009-06-05 22:21:14 -07:00
|
|
|
this._notify = Utils.notify("weave:service:");
|
2009-02-22 00:04:58 -08:00
|
|
|
}
|
2012-09-14 16:02:33 -07:00
|
|
|
Sync11Service.prototype = {
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2009-06-06 11:52:49 -07:00
|
|
|
_lock: Utils.lock,
|
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
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
userBaseURL: null,
|
|
|
|
infoURL: null,
|
|
|
|
storageURL: null,
|
|
|
|
metaURL: null,
|
|
|
|
cryptoKeyURL: null,
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2012-08-29 14:43:40 -07:00
|
|
|
get enabledEngineNames() {
|
|
|
|
return [e.name for each (e in this.engineManager.getEnabled())];
|
|
|
|
},
|
|
|
|
|
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) {
|
2012-11-12 09:48:26 -08:00
|
|
|
if (!value.endsWith("/")) {
|
|
|
|
value += "/";
|
|
|
|
}
|
|
|
|
|
2009-09-22 01:00:43 -07:00
|
|
|
// 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;
|
2011-04-08 14:52:08 -07:00
|
|
|
return misc + MISC_API_VERSION + "/";
|
2009-09-21 17:13:41 -07:00
|
|
|
},
|
2009-08-25 17:15:36 -07:00
|
|
|
|
2012-08-29 14:43:39 -07:00
|
|
|
/**
|
|
|
|
* The URI of the User API service.
|
|
|
|
*
|
|
|
|
* This is the base URI of the service as applicable to all users up to
|
|
|
|
* and including the server version path component, complete with trailing
|
|
|
|
* forward slash.
|
|
|
|
*/
|
|
|
|
get userAPIURI() {
|
|
|
|
// Append to the serverURL if it's a relative fragment.
|
|
|
|
let url = Svc.Prefs.get("userURL");
|
|
|
|
if (!url.contains(":")) {
|
|
|
|
url = this.serverURL + url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url + USER_API_VERSION + "/";
|
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
|
|
|
},
|
|
|
|
|
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
|
|
|
|
2009-02-11 22:08:56 -08:00
|
|
|
get locked() { return this._locked; },
|
2012-08-08 16:35:37 -07:00
|
|
|
lock: function lock() {
|
2008-11-03 14:59:45 -08:00
|
|
|
if (this._locked)
|
|
|
|
return false;
|
|
|
|
this._locked = true;
|
|
|
|
return true;
|
|
|
|
},
|
2012-08-08 16:35:37 -07:00
|
|
|
unlock: function unlock() {
|
2008-11-03 14:59:45 -08:00
|
|
|
this._locked = false;
|
2008-05-23 12:08:03 -07:00
|
|
|
},
|
|
|
|
|
2011-01-10 21:51:29 -08:00
|
|
|
// A specialized variant of Utils.catch.
|
|
|
|
// This provides a more informative error message when we're already syncing:
|
|
|
|
// see Bug 616568.
|
|
|
|
_catch: function _catch(func) {
|
|
|
|
function lockExceptions(ex) {
|
|
|
|
if (Utils.isLockException(ex)) {
|
|
|
|
// This only happens if we're syncing already.
|
|
|
|
this._log.info("Cannot start sync: already syncing?");
|
|
|
|
}
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2011-01-10 21:51:29 -08:00
|
|
|
return Utils.catch.call(this, func, lockExceptions);
|
|
|
|
},
|
|
|
|
|
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
|
2012-09-14 16:02:33 -07:00
|
|
|
if (this.clusterURL == "" || this.identity.username == "")
|
2009-09-21 18:03:56 -07:00
|
|
|
return;
|
|
|
|
|
2011-04-08 14:52:08 -07:00
|
|
|
let storageAPI = this.clusterURL + SYNC_API_VERSION + "/";
|
2012-09-14 16:02:33 -07:00
|
|
|
this.userBaseURL = storageAPI + this.identity.username + "/";
|
2010-09-11 09:39:21 -07:00
|
|
|
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";
|
2011-04-19 12:35:04 -07:00
|
|
|
this.cryptoKeysURL = this.storageURL + CRYPTO_COLLECTION + "/" + KEYS_WBO;
|
2008-12-01 18:43:43 -08:00
|
|
|
},
|
|
|
|
|
2012-08-08 16:35:37 -07:00
|
|
|
_checkCrypto: function _checkCrypto() {
|
2009-02-10 00:56:42 -08:00
|
|
|
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;
|
|
|
|
},
|
2011-04-19 12:33:47 -07:00
|
|
|
|
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.
|
2011-04-19 12:33:47 -07:00
|
|
|
*
|
2010-12-09 23:06:44 -08:00
|
|
|
* 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.
|
2011-04-19 12:33:47 -07:00
|
|
|
*
|
2010-12-09 23:06:44 -08:00
|
|
|
* If our key collection changed, we signal to the engine (via our return
|
|
|
|
* value) that it should retry decryption.
|
2011-04-19 12:33:47 -07:00
|
|
|
*
|
2010-12-09 23:06:44 -08:00
|
|
|
* 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.
|
2011-04-19 12:33:47 -07:00
|
|
|
*
|
2010-12-09 23:06:44 -08:00
|
|
|
* 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,
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
/*
|
|
|
|
* Returns whether to try again.
|
|
|
|
*/
|
|
|
|
handleHMACEvent: function handleHMACEvent() {
|
|
|
|
let now = Date.now();
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
// 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;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
this._log.info("Bad HMAC event detected. Attempting recovery " +
|
|
|
|
"or signaling to other clients.");
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
// Set the last handled time so that we don't act again.
|
|
|
|
this.lastHMACEvent = now;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
// Fetch keys.
|
2011-04-19 12:35:04 -07:00
|
|
|
let cryptoKeys = new CryptoWrapper(CRYPTO_COLLECTION, KEYS_WBO);
|
2010-12-09 23:06:44 -08:00
|
|
|
try {
|
2012-09-14 16:02:32 -07:00
|
|
|
let cryptoResp = cryptoKeys.fetch(this.resource(this.cryptoKeysURL)).response;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
// Save out the ciphertext for when we reupload. If there's a bug in
|
2012-09-14 16:02:33 -07:00
|
|
|
// CollectionKeyManager, this will prevent us from uploading junk.
|
2010-12-09 23:06:44 -08:00
|
|
|
let cipherText = cryptoKeys.ciphertext;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
if (!cryptoResp.success) {
|
|
|
|
this._log.warn("Failed to download keys.");
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
let keysChanged = this.handleFetchedKeys(this.identity.syncKeyBundle,
|
2010-12-09 23:06:44 -08:00
|
|
|
cryptoKeys, true);
|
|
|
|
if (keysChanged) {
|
|
|
|
// Did they change? If so, carry on.
|
|
|
|
this._log.info("Suggesting retry.");
|
|
|
|
return true; // Try again.
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
// If not, reupload them and continue the current sync.
|
|
|
|
cryptoKeys.ciphertext = cipherText;
|
|
|
|
cryptoKeys.cleartext = null;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-09-14 16:02:32 -07:00
|
|
|
let uploadResp = cryptoKeys.upload(this.resource(this.cryptoKeysURL));
|
2010-12-09 23:06:44 -08:00
|
|
|
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.");
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
return false; // Don't try again: same keys.
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Got exception \"" + ex + "\" fetching and handling " +
|
|
|
|
"crypto keys. Will try again later.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
handleFetchedKeys: function handleFetchedKeys(syncKey, cryptoKeys, skipReset) {
|
|
|
|
// Don't want to wipe if we're just starting up!
|
2012-09-14 16:02:33 -07:00
|
|
|
let wasBlank = this.collectionKeys.isClear;
|
|
|
|
let keysChanged = this.collectionKeys.updateContents(syncKey, cryptoKeys);
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
if (keysChanged && !wasBlank) {
|
|
|
|
this._log.debug("Keys changed: " + JSON.stringify(keysChanged));
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
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;
|
2011-04-19 12:33:47 -07:00
|
|
|
},
|
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();
|
2012-08-29 14:43:40 -07:00
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
// Status is instantiated before us and is the first to grab an instance of
|
|
|
|
// the IdentityManager. We use that instance because IdentityManager really
|
|
|
|
// needs to be a singleton. Ideally, the longer-lived object would spawn
|
|
|
|
// this service instance.
|
|
|
|
if (!Status || !Status._authManager) {
|
|
|
|
throw new Error("Status or Status._authManager not initialized.");
|
|
|
|
}
|
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status = Status;
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity = Status._authManager;
|
2012-09-14 16:02:33 -07:00
|
|
|
this.collectionKeys = new CollectionKeyManager();
|
|
|
|
|
2012-08-29 14:43:41 -07:00
|
|
|
this.errorHandler = new ErrorHandler(this);
|
2011-08-26 14:01:35 -07:00
|
|
|
|
|
|
|
this._log = Log4Moz.repository.getLogger("Sync.Service");
|
|
|
|
this._log.level =
|
|
|
|
Log4Moz.Level[Svc.Prefs.get("log.logger.service.main")];
|
|
|
|
|
2010-04-01 15:16:19 -07:00
|
|
|
this._log.info("Loading Weave " + WEAVE_VERSION);
|
2009-09-25 08:13:12 -07:00
|
|
|
|
2012-08-14 11:34:20 -07:00
|
|
|
this._clusterManager = new ClusterManager(this);
|
2012-09-14 16:02:32 -07:00
|
|
|
this.recordManager = new RecordManager(this);
|
2012-08-14 11:34:20 -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-09-07 09:44:01 -07:00
|
|
|
Svc.Prefs.observe("engine.", this);
|
2008-11-06 17:37:17 -08:00
|
|
|
|
2012-08-29 14:43:41 -07:00
|
|
|
this.scheduler = new SyncScheduler(this);
|
2011-06-27 06:22:49 -07:00
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
if (!this.enabled) {
|
|
|
|
this._log.info("Firefox Sync disabled.");
|
|
|
|
}
|
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();
|
2012-03-22 15:49:50 -07:00
|
|
|
if (status != STATUS_DISABLED && status != CLIENT_NOT_CONFIGURED) {
|
2010-08-06 08:30:58 -07:00
|
|
|
Svc.Obs.notify("weave:engine:start-tracking");
|
2012-03-22 15:49:50 -07:00
|
|
|
}
|
2010-08-06 08:30:58 -07:00
|
|
|
|
2010-06-10 17:04:49 -07:00
|
|
|
// Send an event now that Weave service is ready. We don't do this
|
2011-02-17 13:41:13 -08:00
|
|
|
// synchronously so that observers can import this module before
|
|
|
|
// registering an observer.
|
2012-08-08 16:35:37 -07:00
|
|
|
Utils.nextTick(function onNextTick() {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.ready = true;
|
2013-01-30 07:05:12 -08:00
|
|
|
|
|
|
|
// UI code uses the flag on the XPCOM service so it doesn't have
|
|
|
|
// to load a bunch of modules.
|
|
|
|
let xps = Cc["@mozilla.org/weave/service;1"]
|
|
|
|
.getService(Ci.nsISupports)
|
|
|
|
.wrappedJSObject;
|
|
|
|
xps.ready = true;
|
|
|
|
|
2011-02-17 13:41:13 -08:00
|
|
|
Svc.Obs.notify("weave:service:ready");
|
2012-09-14 16:02:33 -07:00
|
|
|
}.bind(this));
|
2008-11-06 17:37:17 -08:00
|
|
|
},
|
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
_checkSetup: function _checkSetup() {
|
|
|
|
if (!this.enabled) {
|
2012-09-14 16:02:33 -07:00
|
|
|
return this.status.service = STATUS_DISABLED;
|
2012-03-22 15:49:50 -07:00
|
|
|
}
|
2012-09-14 16:02:33 -07:00
|
|
|
return this.status.checkSetup();
|
2010-04-09 10:20:41 -07:00
|
|
|
},
|
|
|
|
|
2010-06-16 14:30:13 -07:00
|
|
|
_migratePrefs: function _migratePrefs() {
|
2011-06-13 11:39:51 -07:00
|
|
|
// Migrate old debugLog prefs.
|
2011-06-14 01:41:36 -07:00
|
|
|
let logLevel = Svc.Prefs.get("log.appender.debugLog");
|
2011-06-13 11:39:51 -07:00
|
|
|
if (logLevel) {
|
2011-06-14 01:41:36 -07:00
|
|
|
Svc.Prefs.set("log.appender.file.level", logLevel);
|
|
|
|
Svc.Prefs.reset("log.appender.debugLog");
|
2011-06-13 11:39:51 -07:00
|
|
|
}
|
|
|
|
if (Svc.Prefs.get("log.appender.debugLog.enabled")) {
|
2011-06-14 01:41:36 -07:00
|
|
|
Svc.Prefs.set("log.appender.file.logOnSuccess", true);
|
|
|
|
Svc.Prefs.reset("log.appender.debugLog.enabled");
|
2011-06-13 11:39:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate old extensions.weave.* prefs if we haven't already tried.
|
2010-06-16 14:30:13 -07:00
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
2009-04-08 12:39:14 -07:00
|
|
|
/**
|
|
|
|
* Register the built-in engines for certain applications
|
|
|
|
*/
|
2012-08-08 16:35:37 -07:00
|
|
|
_registerEngines: function _registerEngines() {
|
2012-08-29 14:43:41 -07:00
|
|
|
this.engineManager = new EngineManager(this);
|
2012-08-29 14:43:40 -07:00
|
|
|
|
2009-04-08 12:39:14 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-29 14:43:41 -07:00
|
|
|
this.clientsEngine = new ClientEngine(this);
|
2012-08-29 14:43:40 -07:00
|
|
|
|
2012-08-29 14:43:40 -07:00
|
|
|
for (let name of engines) {
|
|
|
|
if (!name in ENGINE_MODULES) {
|
|
|
|
this._log.info("Do not know about engine: " + name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ns = {};
|
|
|
|
try {
|
|
|
|
Cu.import("resource://services-sync/engines/" + ENGINE_MODULES[name], ns);
|
|
|
|
|
|
|
|
let engineName = name + "Engine";
|
|
|
|
if (!(engineName in ns)) {
|
|
|
|
this._log.warn("Could not find exported engine instance: " + engineName);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-08-29 14:43:41 -07:00
|
|
|
this.engineManager.register(ns[engineName], this);
|
2012-08-29 14:43:40 -07:00
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Could not register engine " + name + ": " +
|
|
|
|
CommonUtils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
}
|
2009-04-08 12:39:14 -07:00
|
|
|
},
|
|
|
|
|
2008-06-23 16:10:31 -07:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference]),
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
|
2012-08-08 16:35:37 -07:00
|
|
|
observe: function 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;
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-14 16:02:32 -07:00
|
|
|
/**
|
|
|
|
* Obtain a Resource instance with authentication credentials.
|
|
|
|
*/
|
|
|
|
resource: function resource(url) {
|
|
|
|
let res = new Resource(url);
|
2012-09-14 16:02:33 -07:00
|
|
|
res.authenticator = this.identity.getResourceAuthenticator();
|
2012-09-14 16:02:32 -07:00
|
|
|
|
|
|
|
return res;
|
|
|
|
},
|
|
|
|
|
2012-09-14 16:02:32 -07:00
|
|
|
/**
|
|
|
|
* Obtain a SyncStorageRequest instance with authentication credentials.
|
|
|
|
*/
|
|
|
|
getStorageRequest: function getStorageRequest(url) {
|
|
|
|
let request = new SyncStorageRequest(url);
|
2012-09-14 16:02:33 -07:00
|
|
|
request.authenticator = this.identity.getRESTRequestAuthenticator();
|
2012-09-14 16:02:32 -07:00
|
|
|
|
|
|
|
return request;
|
|
|
|
},
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
/**
|
|
|
|
* Perform the info fetch as part of a login or key fetch.
|
|
|
|
*/
|
2011-01-31 20:55:48 -08:00
|
|
|
_fetchInfo: function _fetchInfo(url) {
|
2010-11-29 16:41:17 -08:00
|
|
|
let infoURL = url || this.infoURL;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-20 12:47:51 -08:00
|
|
|
this._log.trace("In _fetchInfo: " + infoURL);
|
2011-01-31 20:55:48 -08:00
|
|
|
let info;
|
|
|
|
try {
|
2012-09-14 16:02:32 -07:00
|
|
|
info = this.resource(infoURL).get();
|
2011-01-31 20:55:48 -08:00
|
|
|
} catch (ex) {
|
2012-08-29 14:43:40 -07:00
|
|
|
this.errorHandler.checkServerError(ex);
|
2011-01-31 20:55:48 -08:00
|
|
|
throw ex;
|
|
|
|
}
|
2010-11-29 16:41:17 -08:00
|
|
|
if (!info.success) {
|
2012-08-29 14:43:40 -07:00
|
|
|
this.errorHandler.checkServerError(info);
|
2010-11-29 16:41:17 -08:00
|
|
|
throw "aborting sync, failed to get collections";
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
},
|
|
|
|
|
|
|
|
verifyAndFetchSymmetricKeys: function verifyAndFetchSymmetricKeys(infoResponse) {
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.debug("Fetching and verifying -- or generating -- symmetric keys.");
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Don't allow empty/missing passphrase.
|
|
|
|
// Furthermore, we assume that our sync key is already upgraded,
|
|
|
|
// and fail if that assumption is invalidated.
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
if (!this.identity.syncKey) {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_NO_PASSPHRASE;
|
|
|
|
this.status.sync = CREDENTIALS_CHANGED;
|
2010-11-29 16:41:17 -08:00
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
let syncKeyBundle = this.identity.syncKeyBundle;
|
2012-05-03 14:25:33 -07:00
|
|
|
if (!syncKeyBundle) {
|
|
|
|
this._log.error("Sync Key Bundle not set. Invalid Sync Key?");
|
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_INVALID_PASSPHRASE;
|
|
|
|
this.status.sync = CREDENTIALS_CHANGED;
|
2010-11-29 16:41:17 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!infoResponse)
|
|
|
|
infoResponse = this._fetchInfo(); // Will throw an exception on failure.
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// 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.");
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_SERVER_ERROR;
|
2012-08-29 14:43:40 -07:00
|
|
|
this.errorHandler.checkServerError(infoResponse);
|
2010-11-29 16:41:17 -08:00
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
let infoCollections = infoResponse.obj;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.info("Testing info/collections: " + JSON.stringify(infoCollections));
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
if (this.collectionKeys.updateNeeded(infoCollections)) {
|
|
|
|
this._log.info("collection keys reports that a key update is needed.");
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Don't always set to CREDENTIALS_CHANGED -- we will probably take care of this.
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Fetch storage/crypto/keys.
|
|
|
|
let cryptoKeys;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2011-04-19 12:35:04 -07:00
|
|
|
if (infoCollections && (CRYPTO_COLLECTION in infoCollections)) {
|
2010-11-29 16:41:17 -08:00
|
|
|
try {
|
2011-04-19 12:35:04 -07:00
|
|
|
cryptoKeys = new CryptoWrapper(CRYPTO_COLLECTION, KEYS_WBO);
|
2012-09-14 16:02:32 -07:00
|
|
|
let cryptoResp = cryptoKeys.fetch(this.resource(this.cryptoKeysURL)).response;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
if (cryptoResp.success) {
|
2012-03-22 15:49:50 -07:00
|
|
|
let keysChanged = this.handleFetchedKeys(syncKeyBundle, cryptoKeys);
|
2010-11-29 16:41:17 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (cryptoResp.status == 404) {
|
2012-09-14 16:02:33 -07:00
|
|
|
// On failure, ask to generate new keys and upload them.
|
2010-11-29 16:41:17 -08:00
|
|
|
// 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.
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_SERVER_ERROR;
|
2012-08-29 14:43:40 -07:00
|
|
|
this.errorHandler.checkServerError(cryptoResp);
|
2011-09-06 09:17:26 -07:00
|
|
|
this._log.warn("Got status " + cryptoResp.status + " fetching crypto keys.");
|
2010-11-29 16:41:17 -08:00
|
|
|
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?
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// One kind of exception: HMAC failure.
|
2010-12-09 10:32:03 -08:00
|
|
|
if (Utils.isHMACMismatch(ex)) {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_INVALID_PASSPHRASE;
|
|
|
|
this.status.sync = CREDENTIALS_CHANGED;
|
2010-11-29 16:41:17 -08:00
|
|
|
}
|
2010-12-10 16:32:16 -08:00
|
|
|
else {
|
|
|
|
// In the absence of further disambiguation or more precise
|
|
|
|
// failure constants, just report failure.
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED;
|
2010-12-10 16:32:16 -08:00
|
|
|
}
|
2010-11-29 16:41:17 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._log.info("... 'crypto' is not a reported collection. Generating new keys.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cryptoKeys) {
|
2011-04-19 12:35:04 -07:00
|
|
|
this._log.info("No keys! Generating new ones.");
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2011-04-19 12:35:04 -07:00
|
|
|
// Better make some and upload them, and wipe the server to ensure
|
|
|
|
// consistency. This is all achieved via _freshStart.
|
|
|
|
// If _freshStart fails to clear the server or upload keys, it will
|
|
|
|
// throw.
|
|
|
|
this._freshStart();
|
2010-11-29 16:41:17 -08:00
|
|
|
return true;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Last-ditch case.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// No update needed: we're good!
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2011-10-27 22:25:00 -07:00
|
|
|
} catch (ex) {
|
2010-11-29 16:41:17 -08:00
|
|
|
// This means no keys are present, or there's a network error.
|
2011-10-27 22:25:00 -07:00
|
|
|
this._log.debug("Failed to fetch and verify keys: "
|
|
|
|
+ Utils.exceptionStr(ex));
|
2012-08-29 14:43:40 -07:00
|
|
|
this.errorHandler.checkServerError(ex);
|
2010-11-29 16:41:17 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
verifyLogin: function verifyLogin() {
|
2012-09-14 16:02:33 -07:00
|
|
|
if (!this.identity.username) {
|
2012-08-13 16:51:58 -07:00
|
|
|
this._log.warn("No username in verifyLogin.");
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_NO_USERNAME;
|
2012-08-13 16:51:58 -07:00
|
|
|
return false;
|
|
|
|
}
|
2011-01-31 20:55:48 -08:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
// Unlock master password, or return.
|
|
|
|
// Attaching auth credentials to a request requires access to
|
|
|
|
// passwords, which means that Resource.get can throw MP-related
|
|
|
|
// exceptions!
|
|
|
|
// Try to fetch the passphrase first, while we still have control.
|
|
|
|
try {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity.syncKey;
|
2012-08-13 16:51:58 -07:00
|
|
|
} catch (ex) {
|
|
|
|
this._log.debug("Fetching passphrase threw " + ex +
|
|
|
|
"; assuming master password locked.");
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = MASTER_PASSWORD_LOCKED;
|
2012-08-13 16:51:58 -07:00
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
try {
|
|
|
|
// Make sure we have a cluster to verify against.
|
|
|
|
// This is a little weird, if we don't get a node we pretend
|
|
|
|
// to succeed, since that probably means we just don't have storage.
|
2012-08-14 11:34:20 -07:00
|
|
|
if (this.clusterURL == "" && !this._clusterManager.setCluster()) {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.sync = NO_SYNC_NODE_FOUND;
|
2012-08-13 16:51:58 -07:00
|
|
|
Svc.Obs.notify("weave:service:sync:delayed");
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
// Fetch collection info on every startup.
|
2012-09-14 16:02:32 -07:00
|
|
|
let test = this.resource(this.infoURL).get();
|
2010-11-29 16:41:17 -08:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
switch (test.status) {
|
|
|
|
case 200:
|
|
|
|
// The user is authenticated.
|
|
|
|
|
|
|
|
// We have no way of verifying the passphrase right now,
|
|
|
|
// so wait until remoteSetup to do so.
|
|
|
|
// Just make the most trivial checks.
|
2012-09-14 16:02:33 -07:00
|
|
|
if (!this.identity.syncKey) {
|
2012-08-13 16:51:58 -07:00
|
|
|
this._log.warn("No passphrase in verifyLogin.");
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_NO_PASSPHRASE;
|
2012-08-13 16:51:58 -07:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-14 13:03:39 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
// Go ahead and do remote setup, so that we can determine
|
|
|
|
// conclusively that our passphrase is correct.
|
|
|
|
if (this._remoteSetup()) {
|
|
|
|
// Username/password verified.
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_SUCCEEDED;
|
2012-08-13 16:51:58 -07:00
|
|
|
return true;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
this._log.warn("Remote setup failed.");
|
|
|
|
// Remote setup must have failed.
|
|
|
|
return false;
|
2009-09-28 16:13:42 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
case 401:
|
|
|
|
this._log.warn("401: login failed.");
|
|
|
|
// Fall through to the 404 case.
|
2012-03-22 15:49:50 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
case 404:
|
|
|
|
// Check that we're verifying with the correct cluster
|
2012-08-14 11:34:20 -07:00
|
|
|
if (this._clusterManager.setCluster()) {
|
2012-08-13 16:51:58 -07:00
|
|
|
return this.verifyLogin();
|
|
|
|
}
|
2009-08-19 20:27:22 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
// We must have the right cluster, but the server doesn't expect us
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_LOGIN_REJECTED;
|
2012-08-13 16:51:58 -07:00
|
|
|
return false;
|
2009-09-28 16:13:42 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
default:
|
|
|
|
// Server didn't respond with something that we expected
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_SERVER_ERROR;
|
2012-08-29 14:43:40 -07:00
|
|
|
this.errorHandler.checkServerError(test);
|
2012-08-13 16:51:58 -07:00
|
|
|
return false;
|
2009-08-19 18:22:22 -07:00
|
|
|
}
|
2012-08-13 16:51:58 -07:00
|
|
|
} catch (ex) {
|
|
|
|
// Must have failed on some network issue
|
|
|
|
this._log.debug("verifyLogin failed: " + Utils.exceptionStr(ex));
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_NETWORK_ERROR;
|
2012-08-29 14:43:40 -07:00
|
|
|
this.errorHandler.checkServerError(ex);
|
2012-08-13 16:51:58 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
2008-06-30 16:50:19 -07:00
|
|
|
|
2012-08-08 16:35:37 -07:00
|
|
|
generateNewSymmetricKeys: function generateNewSymmetricKeys() {
|
2011-04-19 12:35:04 -07:00
|
|
|
this._log.info("Generating new keys WBO...");
|
2012-09-14 16:02:33 -07:00
|
|
|
let wbo = this.collectionKeys.generateNewKeysWBO();
|
2011-04-19 12:35:04 -07:00
|
|
|
this._log.info("Encrypting new key bundle.");
|
2012-09-14 16:02:33 -07:00
|
|
|
wbo.encrypt(this.identity.syncKeyBundle);
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.info("Uploading...");
|
2012-09-14 16:02:32 -07:00
|
|
|
let uploadRes = wbo.upload(this.resource(this.cryptoKeysURL));
|
2011-04-19 12:35:04 -07:00
|
|
|
if (uploadRes.status != 200) {
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.warn("Got status " + uploadRes.status + " uploading new keys. What to do? Throw!");
|
2012-08-29 14:43:40 -07:00
|
|
|
this.errorHandler.checkServerError(uploadRes);
|
2010-11-29 16:41:17 -08:00
|
|
|
throw new Error("Unable to upload symmetric keys.");
|
|
|
|
}
|
2011-04-19 12:35:04 -07:00
|
|
|
this._log.info("Got status " + uploadRes.status + " uploading keys.");
|
|
|
|
let serverModified = uploadRes.obj; // Modified timestamp according to server.
|
2011-04-19 12:56:46 -07:00
|
|
|
this._log.debug("Server reports crypto modified: " + serverModified);
|
2011-04-19 12:35:04 -07:00
|
|
|
|
|
|
|
// Now verify that info/collections shows them!
|
|
|
|
this._log.debug("Verifying server collection records.");
|
|
|
|
let info = this._fetchInfo();
|
|
|
|
this._log.debug("info/collections is: " + info);
|
|
|
|
|
|
|
|
if (info.status != 200) {
|
|
|
|
this._log.warn("Non-200 info/collections response. Aborting.");
|
|
|
|
throw new Error("Unable to upload symmetric keys.");
|
|
|
|
}
|
|
|
|
|
|
|
|
info = info.obj;
|
2011-04-19 13:18:15 -07:00
|
|
|
if (!(CRYPTO_COLLECTION in info)) {
|
2011-04-19 12:35:04 -07:00
|
|
|
this._log.error("Consistency failure: info/collections excludes " +
|
|
|
|
"crypto after successful upload.");
|
|
|
|
throw new Error("Symmetric key upload failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't check against local modified: clock drift.
|
2011-04-19 13:18:15 -07:00
|
|
|
if (info[CRYPTO_COLLECTION] < serverModified) {
|
2011-04-19 12:35:04 -07:00
|
|
|
this._log.error("Consistency failure: info/collections crypto entry " +
|
|
|
|
"is stale after successful upload.");
|
|
|
|
throw new Error("Symmetric key upload failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't matter if the timestamp is ahead.
|
|
|
|
|
|
|
|
// Download and install them.
|
|
|
|
let cryptoKeys = new CryptoWrapper(CRYPTO_COLLECTION, KEYS_WBO);
|
2012-09-14 16:02:32 -07:00
|
|
|
let cryptoResp = cryptoKeys.fetch(this.resource(this.cryptoKeysURL)).response;
|
2011-04-19 12:35:04 -07:00
|
|
|
if (cryptoResp.status != 200) {
|
|
|
|
this._log.warn("Failed to download keys.");
|
|
|
|
throw new Error("Symmetric key download failed.");
|
|
|
|
}
|
2012-09-14 16:02:33 -07:00
|
|
|
let keysChanged = this.handleFetchedKeys(this.identity.syncKeyBundle,
|
2011-04-19 12:35:04 -07:00
|
|
|
cryptoKeys, true);
|
|
|
|
if (keysChanged) {
|
|
|
|
this._log.info("Downloaded keys differed, as expected.");
|
|
|
|
}
|
2010-11-29 16:41:17 -08:00
|
|
|
},
|
2008-07-11 17:40:52 -07:00
|
|
|
|
2012-08-29 14:43:39 -07:00
|
|
|
changePassword: function changePassword(newPassword) {
|
|
|
|
let client = new UserAPI10Client(this.userAPIURI);
|
|
|
|
let cb = Async.makeSpinningCallback();
|
2012-09-14 16:02:33 -07:00
|
|
|
client.changePassword(this.identity.username,
|
|
|
|
this.identity.basicPassword, newPassword, cb);
|
2012-08-29 14:43:39 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
try {
|
2012-08-29 14:43:39 -07:00
|
|
|
cb.wait();
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.debug("Password change failed: " +
|
|
|
|
CommonUtils.exceptionStr(ex));
|
2012-08-13 16:51:58 -07:00
|
|
|
return false;
|
|
|
|
}
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
// Save the new password for requests and login manager.
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity.basicPassword = newPassword;
|
2012-08-13 16:51:58 -07:00
|
|
|
this.persistLogin();
|
|
|
|
return true;
|
2012-08-08 16:35:37 -07:00
|
|
|
},
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2012-08-08 16:35:37 -07:00
|
|
|
changePassphrase: function changePassphrase(newphrase) {
|
2012-08-13 16:51:58 -07:00
|
|
|
return this._catch(function doChangePasphrase() {
|
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. */
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity.syncKey = newphrase;
|
2010-05-04 13:56:12 -07:00
|
|
|
this.persistLogin();
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2010-12-10 16:53:40 -08:00
|
|
|
/* We need to re-encrypt everything, so reset. */
|
|
|
|
this.resetClient();
|
2012-09-14 16:02:33 -07:00
|
|
|
this.collectionKeys.clear();
|
2010-12-10 16:53:40 -08:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
/* Login and sync. This also generates new keys. */
|
2011-06-06 07:49:13 -07:00
|
|
|
this.sync();
|
2012-08-13 16:51:58 -07:00
|
|
|
|
|
|
|
Svc.Obs.notify("weave:service:change-passphrase", true);
|
|
|
|
|
2009-07-21 14:31:16 -07:00
|
|
|
return true;
|
2012-08-13 16:51:58 -07:00
|
|
|
})();
|
2012-08-08 16:35:37 -07:00
|
|
|
},
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
startOver: function startOver() {
|
2011-10-27 22:25:01 -07:00
|
|
|
this._log.trace("Invoking Service.startOver.");
|
2011-06-06 11:29:45 -07:00
|
|
|
Svc.Obs.notify("weave:engine:stop-tracking");
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.resetSync();
|
2011-06-06 11:29:45 -07:00
|
|
|
|
|
|
|
// We want let UI consumers of the following notification know as soon as
|
|
|
|
// possible, so let's fake for the CLIENT_NOT_CONFIGURED status for now
|
|
|
|
// by emptying the passphrase (we still need the password).
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity.syncKey = null;
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_NO_PASSPHRASE;
|
2011-06-06 11:29:45 -07:00
|
|
|
this.logout();
|
|
|
|
Svc.Obs.notify("weave:service:start-over");
|
|
|
|
|
2011-04-13 15:34:58 -07:00
|
|
|
// Deletion doesn't make sense if we aren't set up yet!
|
|
|
|
if (this.clusterURL != "") {
|
|
|
|
// Clear client-specific data from the server, including disabled engines.
|
2012-08-29 14:43:40 -07:00
|
|
|
for each (let engine in [this.clientsEngine].concat(this.engineManager.getAll())) {
|
2011-04-13 15:34:58 -07:00
|
|
|
try {
|
|
|
|
engine.removeClientData();
|
|
|
|
} catch(ex) {
|
|
|
|
this._log.warn("Deleting client data for " + engine.name + " failed:"
|
|
|
|
+ Utils.exceptionStr(ex));
|
|
|
|
}
|
2011-04-08 14:51:55 -07:00
|
|
|
}
|
2011-04-13 15:34:58 -07:00
|
|
|
} else {
|
|
|
|
this._log.debug("Skipping client data removal: no cluster URL.");
|
2011-04-08 14:51:55 -07:00
|
|
|
}
|
|
|
|
|
2010-12-10 16:53:40 -08:00
|
|
|
// Reset all engines and clear keys.
|
2010-04-01 13:43:09 -07:00
|
|
|
this.resetClient();
|
2012-09-14 16:02:33 -07:00
|
|
|
this.collectionKeys.clear();
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.resetBackoff();
|
2011-04-19 12:33:47 -07:00
|
|
|
|
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;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-05-07 13:16:37 -07:00
|
|
|
Svc.Prefs.set("lastversion", WEAVE_VERSION);
|
2012-03-22 15:49:50 -07:00
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity.deleteSyncCredentials();
|
2010-04-01 13:43:09 -07:00
|
|
|
},
|
|
|
|
|
2009-09-03 21:11:32 -07:00
|
|
|
persistLogin: function persistLogin() {
|
|
|
|
try {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity.persistCredentials(true);
|
2012-03-22 15:49:50 -07:00
|
|
|
} catch (ex) {
|
|
|
|
this._log.info("Unable to persist credentials: " + ex);
|
2009-09-03 21:11:32 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-08-08 16:35:37 -07:00
|
|
|
login: function login(username, password, passphrase) {
|
|
|
|
function onNotify() {
|
2008-12-27 12:15:45 -08:00
|
|
|
this._loggedIn = false;
|
2011-05-19 18:08:07 -07:00
|
|
|
if (Services.io.offline) {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.login = LOGIN_FAILED_NETWORK_ERROR;
|
2009-08-19 20:27:22 -07:00
|
|
|
throw "Application is offline, login should not be called";
|
2011-02-03 10:23:39 -08:00
|
|
|
}
|
2008-03-30 08:40:23 -07:00
|
|
|
|
2010-08-09 16:46:54 -07:00
|
|
|
let initialStatus = this._checkSetup();
|
2012-03-22 15:49:50 -07:00
|
|
|
if (username) {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity.username = username;
|
2012-03-22 15:49:50 -07:00
|
|
|
}
|
|
|
|
if (password) {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity.basicPassword = password;
|
2012-03-22 15:49:50 -07:00
|
|
|
}
|
|
|
|
if (passphrase) {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity.syncKey = passphrase;
|
2012-03-22 15:49:50 -07:00
|
|
|
}
|
2009-03-11 18:29:18 -07:00
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
if (this._checkSetup() == CLIENT_NOT_CONFIGURED) {
|
|
|
|
throw "Aborting login, client not configured.";
|
|
|
|
}
|
2010-04-09 10:20:41 -07:00
|
|
|
|
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
|
2012-03-22 15:49:50 -07:00
|
|
|
&& (username || password || passphrase)) {
|
2010-08-09 16:46:54 -07:00
|
|
|
Svc.Obs.notify("weave:service:setup-complete");
|
2012-03-22 15:49:50 -07:00
|
|
|
}
|
2010-08-09 16:46:54 -07:00
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
this._log.info("Logging in user " + this.identity.username);
|
2012-03-22 15:49:50 -07:00
|
|
|
this._updateCachedURLs();
|
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.
|
2012-09-14 16:02:33 -07:00
|
|
|
throw "Login failed: " + this.status.login;
|
2009-03-11 18:29:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this._loggedIn = true;
|
2009-03-12 13:01:04 -07:00
|
|
|
|
2009-06-06 11:52:54 -07:00
|
|
|
return true;
|
2012-08-08 16:35:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
let notifier = this._notify("login", "", onNotify.bind(this));
|
|
|
|
return this._catch(this._lock("service.js: login", notifier))();
|
|
|
|
},
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2012-03-22 15:49:50 -07:00
|
|
|
logout: function 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-04-20 18:56:44 -07:00
|
|
|
Svc.Obs.notify("weave:service:logout:finish");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2010-09-13 08:17:37 -07:00
|
|
|
checkAccount: function checkAccount(account) {
|
2012-08-29 14:43:39 -07:00
|
|
|
let client = new UserAPI10Client(this.userAPIURI);
|
|
|
|
let cb = Async.makeSpinningCallback();
|
|
|
|
|
2012-09-14 16:02:33 -07:00
|
|
|
let username = this.identity.usernameFromAccount(account);
|
2012-08-29 14:43:39 -07:00
|
|
|
client.usernameExists(username, cb);
|
2009-08-18 20:03:11 -07:00
|
|
|
|
2009-08-20 10:20:01 -07:00
|
|
|
try {
|
2012-08-29 14:43:39 -07:00
|
|
|
let exists = cb.wait();
|
|
|
|
return exists ? "notAvailable" : "available";
|
|
|
|
} catch (ex) {
|
|
|
|
// TODO fix API convention.
|
2012-08-29 14:43:40 -07:00
|
|
|
return this.errorHandler.errorStr(ex);
|
2009-08-20 10:20:01 -07:00
|
|
|
}
|
2009-08-18 20:03:11 -07:00
|
|
|
},
|
|
|
|
|
2011-03-25 00:57:23 -07:00
|
|
|
createAccount: function createAccount(email, password,
|
|
|
|
captchaChallenge, captchaResponse) {
|
2012-08-29 14:43:39 -07:00
|
|
|
let client = new UserAPI10Client(this.userAPIURI);
|
2010-06-17 08:47:13 -07:00
|
|
|
|
|
|
|
// Hint to server to allow scripted user creation or otherwise
|
|
|
|
// ignore captcha.
|
2012-08-29 14:43:39 -07:00
|
|
|
if (Svc.Prefs.isSet("admin-secret")) {
|
|
|
|
client.adminSecret = Svc.Prefs.get("admin-secret", "");
|
|
|
|
}
|
2009-06-06 16:24:17 -07:00
|
|
|
|
2012-08-29 14:43:39 -07:00
|
|
|
let cb = Async.makeSpinningCallback();
|
2009-08-26 16:09:48 -07:00
|
|
|
|
2012-08-29 14:43:39 -07:00
|
|
|
client.createAccount(email, password, captchaChallenge, captchaResponse,
|
|
|
|
cb);
|
2009-08-26 15:32:46 -07:00
|
|
|
|
2012-08-29 14:43:39 -07:00
|
|
|
try {
|
|
|
|
cb.wait();
|
|
|
|
return null;
|
|
|
|
} catch (ex) {
|
2012-08-29 14:43:40 -07:00
|
|
|
return this.errorHandler.errorStr(ex.body);
|
2012-08-29 14:43:39 -07:00
|
|
|
}
|
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).
|
2012-08-08 16:35:37 -07:00
|
|
|
_remoteSetup: function _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");
|
2012-09-14 16:02:32 -07:00
|
|
|
let meta = this.recordManager.get(this.metaURL);
|
2011-04-19 12:33:47 -07:00
|
|
|
|
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)) {
|
2011-04-19 12:33:47 -07:00
|
|
|
|
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));
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-09-14 16:02:32 -07:00
|
|
|
this.recordManager.del(this.metaURL);
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-06 14:22:09 -08:00
|
|
|
// ... fetch the current record from the server, and COPY THE FLAGS.
|
2012-09-14 16:02:32 -07:00
|
|
|
let newMeta = this.recordManager.get(this.metaURL);
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-09-14 16:02:32 -07:00
|
|
|
if (!this.recordManager.response.success || !newMeta) {
|
2010-12-09 19:37:02 -08:00
|
|
|
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;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-12-09 19:37:02 -08:00
|
|
|
newMeta.isNew = true;
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2012-09-14 16:02:32 -07:00
|
|
|
this.recordManager.set(this.metaURL, newMeta);
|
2012-09-14 16:02:32 -07:00
|
|
|
if (!newMeta.upload(this.resource(this.metaURL)).success) {
|
2010-12-09 19:37:02 -08:00
|
|
|
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;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
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)) {
|
2011-04-19 12:33:47 -07:00
|
|
|
|
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
|
2012-09-14 16:02:32 -07:00
|
|
|
let status = this.recordManager.response.status;
|
2009-02-23 16:27:41 -08:00
|
|
|
if (status != 200 && status != 404) {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.sync = METARECORD_DOWNLOAD_FAIL;
|
2012-09-14 16:02:32 -07:00
|
|
|
this.errorHandler.checkServerError(this.recordManager.response);
|
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;
|
2011-04-19 12:33:47 -07: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) {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.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) {
|
2011-04-19 12:33:47 -07:00
|
|
|
|
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();
|
2012-09-14 16:02:33 -07:00
|
|
|
this.collectionKeys.clear();
|
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;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
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()) {
|
2012-09-14 16:02:33 -07:00
|
|
|
this.status.sync = CREDENTIALS_CHANGED;
|
2010-03-31 18:58:07 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-12-20 20:01:34 -08:00
|
|
|
/**
|
|
|
|
* Return whether we should attempt login at the start of a sync.
|
2011-04-19 12:33:47 -07:00
|
|
|
*
|
|
|
|
* Note that this function has strong ties to _checkSync: callers
|
2010-12-20 20:01:34 -08:00
|
|
|
* of this function should typically use _checkSync to verify that
|
|
|
|
* any necessary login took place.
|
|
|
|
*/
|
|
|
|
_shouldLogin: function _shouldLogin() {
|
|
|
|
return this.enabled &&
|
2011-05-19 18:08:07 -07:00
|
|
|
!Services.io.offline &&
|
2010-12-20 20:01:34 -08:00
|
|
|
!this.isLoggedIn;
|
|
|
|
},
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2009-03-10 04:15:52 -07:00
|
|
|
/**
|
2009-07-22 20:48:41 -07:00
|
|
|
* Determine if a sync should run.
|
2011-04-19 12:33:47 -07:00
|
|
|
*
|
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
|
|
|
|
*/
|
2012-08-08 16:35:37 -07:00
|
|
|
_checkSync: function _checkSync(ignore) {
|
2009-03-10 04:15:52 -07:00
|
|
|
let reason = "";
|
|
|
|
if (!this.enabled)
|
|
|
|
reason = kSyncWeaveDisabled;
|
2011-05-19 18:08:07 -07:00
|
|
|
else if (Services.io.offline)
|
2009-03-10 04:30:36 -07:00
|
|
|
reason = kSyncNetworkOffline;
|
2012-09-14 16:02:33 -07:00
|
|
|
else if (this.status.minimumNextSync > Date.now())
|
2009-09-15 18:38:52 -07:00
|
|
|
reason = kSyncBackoffNotMet;
|
2012-09-14 16:02:33 -07:00
|
|
|
else if ((this.status.login == MASTER_PASSWORD_LOCKED) &&
|
2011-01-10 21:51:29 -08:00
|
|
|
Utils.mpLocked())
|
|
|
|
reason = kSyncMasterPasswordLocked;
|
2010-04-08 14:03:10 -07:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2010-12-16 12:00:57 -08:00
|
|
|
sync: function sync() {
|
2011-02-25 14:42:59 -08:00
|
|
|
let dateStr = new Date().toLocaleFormat(LOG_DATE_FORMAT);
|
2011-07-21 13:53:04 -07:00
|
|
|
this._log.debug("User-Agent: " + SyncStorageRequest.prototype.userAgent);
|
2011-02-25 14:42:59 -08:00
|
|
|
this._log.info("Starting sync at " + dateStr);
|
2011-01-10 21:51:29 -08:00
|
|
|
this._catch(function () {
|
2010-12-20 20:01:34 -08:00
|
|
|
// Make sure we're logged in.
|
|
|
|
if (this._shouldLogin()) {
|
2011-01-10 21:51:29 -08:00
|
|
|
this._log.debug("In sync: should login.");
|
|
|
|
if (!this.login()) {
|
|
|
|
this._log.debug("Not syncing: login returned false.");
|
|
|
|
return;
|
|
|
|
}
|
2010-12-20 20:01:34 -08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._log.trace("In sync: no need to login.");
|
|
|
|
}
|
|
|
|
return this._lockedSync.apply(this, arguments);
|
2011-01-10 21:51:29 -08:00
|
|
|
})();
|
2010-12-16 12:00:57 -08:00
|
|
|
},
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2009-03-10 04:07:24 -07:00
|
|
|
/**
|
|
|
|
* Sync up engines with the server.
|
|
|
|
*/
|
2012-08-14 11:34:14 -07:00
|
|
|
_lockedSync: function _lockedSync() {
|
|
|
|
return this._lock("service.js: sync",
|
|
|
|
this._notify("sync", "", function onNotify() {
|
|
|
|
|
|
|
|
let synchronizer = new EngineSynchronizer(this);
|
|
|
|
let cb = Async.makeSpinningCallback();
|
|
|
|
synchronizer.onComplete = cb;
|
|
|
|
|
|
|
|
synchronizer.sync();
|
|
|
|
// wait() throws if the first argument is truthy, which is exactly what
|
|
|
|
// we want.
|
|
|
|
let result = cb.wait();
|
|
|
|
}))();
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
/**
|
|
|
|
* If we have a passphrase, rather than a 25-alphadigit sync key,
|
|
|
|
* use the provided sync ID to bootstrap it using PBKDF2.
|
2011-04-19 12:33:47 -07:00
|
|
|
*
|
2010-11-29 16:41:17 -08:00
|
|
|
* Store the new 'passphrase' back into the identity manager.
|
2011-04-19 12:33:47 -07:00
|
|
|
*
|
2010-11-29 16:41:17 -08:00
|
|
|
* 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) {
|
2012-09-14 16:02:33 -07:00
|
|
|
let p = this.identity.syncKey;
|
2012-03-22 15:49:50 -07:00
|
|
|
|
|
|
|
if (!p) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Check whether it's already a key that we generated.
|
2012-03-22 15:49:50 -07:00
|
|
|
if (Utils.isPassphrase(p)) {
|
|
|
|
this._log.info("Sync key is up-to-date: no need to upgrade.");
|
2010-11-29 16:41:17 -08:00
|
|
|
return true;
|
2012-03-22 15:49:50 -07:00
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Otherwise, let's upgrade it.
|
|
|
|
// N.B., we persist the sync key without testing it first...
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
let s = btoa(syncID); // It's what WeaveCrypto expects. *sigh*
|
|
|
|
let k = Utils.derivePresentableKeyFromPassphrase(p, s, PBKDF2_KEY_BYTES); // Base 32.
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
if (!k) {
|
|
|
|
this._log.error("No key resulted from derivePresentableKeyFromPassphrase. Failing upgrade.");
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.info("Upgrading sync key...");
|
2012-09-14 16:02:33 -07:00
|
|
|
this.identity.syncKey = k;
|
2010-11-29 16:41:17 -08:00
|
|
|
this._log.info("Saving upgraded sync key...");
|
|
|
|
this.persistLogin();
|
|
|
|
this._log.info("Done saving.");
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2012-08-08 16:35:37 -07:00
|
|
|
_freshStart: function _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();
|
2012-09-14 16:02:33 -07:00
|
|
|
this.collectionKeys.clear();
|
2010-11-29 16:41:17 -08:00
|
|
|
this.upgradeSyncKey(this.syncID);
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2011-10-27 22:25:00 -07:00
|
|
|
// Wipe the server.
|
|
|
|
let wipeTimestamp = this.wipeServer();
|
|
|
|
|
|
|
|
// Upload a new meta/global record.
|
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));
|
2012-09-14 16:02:32 -07:00
|
|
|
let res = this.resource(this.metaURL);
|
2011-10-27 22:25:00 -07:00
|
|
|
// It would be good to set the X-If-Unmodified-Since header to `timestamp`
|
|
|
|
// for this PUT to ensure at least some level of transactionality.
|
|
|
|
// Unfortunately, the servers don't support it after a wipe right now
|
|
|
|
// (bug 693893), so we're going to defer this until bug 692700.
|
|
|
|
let resp = res.put(meta);
|
|
|
|
if (!resp.success) {
|
|
|
|
// If we got into a race condition, we'll abort the sync this way, too.
|
|
|
|
// That's fine. We'll just wait till the next sync. The client that we're
|
|
|
|
// racing is probably busy uploading stuff right now anyway.
|
2010-03-16 16:31:55 -07:00
|
|
|
throw resp;
|
2011-10-27 22:25:00 -07:00
|
|
|
}
|
2012-09-14 16:02:32 -07:00
|
|
|
this.recordManager.set(this.metaURL, meta);
|
2009-12-02 17:57:13 -08:00
|
|
|
|
|
|
|
// Wipe everything we know about except meta because we just uploaded it
|
2012-08-29 14:43:40 -07:00
|
|
|
let engines = [this.clientsEngine].concat(this.engineManager.getAll());
|
2012-08-29 14:43:40 -07:00
|
|
|
let collections = [engine.name for each (engine in engines)];
|
2011-04-19 12:33:47 -07:00
|
|
|
|
2011-04-19 12:35:04 -07:00
|
|
|
// Generate, upload, and download new keys. Do this last so we don't wipe
|
|
|
|
// them...
|
2010-11-29 16:41:17 -08:00
|
|
|
this.generateNewSymmetricKeys();
|
2009-07-22 18:49:07 -07: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]
|
2011-10-27 22:25:00 -07:00
|
|
|
* Array of collections to wipe. If not given, all collections are
|
|
|
|
* wiped by issuing a DELETE request for `storageURL`.
|
2011-04-19 12:33:47 -07:00
|
|
|
*
|
2011-10-27 22:25:00 -07:00
|
|
|
* @return the server's timestamp of the (last) DELETE.
|
2009-02-26 22:14:32 -08:00
|
|
|
*/
|
2012-08-13 16:51:58 -07:00
|
|
|
wipeServer: function wipeServer(collections) {
|
|
|
|
let response;
|
|
|
|
if (!collections) {
|
|
|
|
// Strip the trailing slash.
|
2012-09-14 16:02:32 -07:00
|
|
|
let res = this.resource(this.storageURL.slice(0, -1));
|
2012-08-13 16:51:58 -07:00
|
|
|
res.setHeader("X-Confirm-Delete", "1");
|
|
|
|
try {
|
|
|
|
response = res.delete();
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.debug("Failed to wipe server: " + CommonUtils.exceptionStr(ex));
|
|
|
|
throw ex;
|
2010-04-07 17:06:37 -07:00
|
|
|
}
|
2012-08-13 16:51:58 -07:00
|
|
|
if (response.status != 200 && response.status != 404) {
|
|
|
|
this._log.debug("Aborting wipeServer. Server responded with " +
|
|
|
|
response.status + " response for " + this.storageURL);
|
|
|
|
throw response;
|
|
|
|
}
|
|
|
|
return response.headers["x-weave-timestamp"];
|
|
|
|
}
|
|
|
|
|
|
|
|
let timestamp;
|
|
|
|
for (let name of collections) {
|
|
|
|
let url = this.storageURL + name;
|
|
|
|
try {
|
2012-09-14 16:02:32 -07:00
|
|
|
response = this.resource(url).delete();
|
2012-08-13 16:51:58 -07:00
|
|
|
} catch (ex) {
|
|
|
|
this._log.debug("Failed to wipe '" + name + "' collection: " +
|
|
|
|
Utils.exceptionStr(ex));
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.status != 200 && response.status != 404) {
|
|
|
|
this._log.debug("Aborting wipeServer. Server responded with " +
|
|
|
|
response.status + " response for " + url);
|
|
|
|
throw response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ("x-weave-timestamp" in response.headers) {
|
|
|
|
timestamp = response.headers["x-weave-timestamp"];
|
2009-02-11 23:54:20 -08:00
|
|
|
}
|
2012-08-13 16:51:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return timestamp;
|
|
|
|
},
|
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
|
|
|
*/
|
2012-08-13 16:51:58 -07:00
|
|
|
wipeClient: function wipeClient(engines) {
|
|
|
|
// If we don't have any engines, reset the service and wipe all engines
|
|
|
|
if (!engines) {
|
|
|
|
// Clear out any service data
|
|
|
|
this.resetService();
|
2009-03-20 00:13:16 -07:00
|
|
|
|
2012-08-29 14:43:40 -07:00
|
|
|
engines = [this.clientsEngine].concat(this.engineManager.getAll());
|
2012-08-13 16:51:58 -07:00
|
|
|
}
|
|
|
|
// Convert the array of names into engines
|
|
|
|
else {
|
2012-08-29 14:43:40 -07:00
|
|
|
engines = this.engineManager.get(engines);
|
2012-08-13 16:51:58 -07:00
|
|
|
}
|
2009-03-11 23:33:14 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
// Fully wipe each engine if it's able to decrypt data
|
|
|
|
for each (let engine in engines) {
|
|
|
|
if (engine.canDecrypt()) {
|
|
|
|
engine.wipeClient();
|
|
|
|
}
|
|
|
|
}
|
2009-10-12 16:45:40 -07:00
|
|
|
|
2012-08-13 16:51:58 -07:00
|
|
|
// Save the password/passphrase just in-case they aren't restored by sync
|
|
|
|
this.persistLogin();
|
|
|
|
},
|
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
|
|
|
*/
|
2011-10-27 22:25:01 -07:00
|
|
|
wipeRemote: function wipeRemote(engines) {
|
|
|
|
try {
|
2011-08-04 16:19:02 -07: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
|
|
|
|
2011-08-04 16:19:02 -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
|
|
|
|
2011-08-04 16:19:02 -07:00
|
|
|
// Only wipe the engines provided.
|
|
|
|
if (engines) {
|
2012-08-29 14:43:40 -07:00
|
|
|
engines.forEach(function(e) this.clientsEngine.sendCommand("wipeEngine", [e]), this);
|
2011-08-04 16:19:02 -07:00
|
|
|
}
|
|
|
|
// Tell the remote machines to wipe themselves.
|
|
|
|
else {
|
2012-08-29 14:43:40 -07:00
|
|
|
this.clientsEngine.sendCommand("wipeAll", []);
|
2011-08-04 16:19:02 -07:00
|
|
|
}
|
2009-12-10 18:39:51 -08:00
|
|
|
|
2011-08-04 16:19:02 -07:00
|
|
|
// Make sure the changed clients get updated.
|
2012-08-29 14:43:40 -07:00
|
|
|
this.clientsEngine.sync();
|
2011-10-27 22:25:01 -07:00
|
|
|
} catch (ex) {
|
2012-08-29 14:43:40 -07:00
|
|
|
this.errorHandler.checkServerError(ex);
|
2011-10-27 22:25:01 -07:00
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
},
|
2009-03-11 23:33:14 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset local service information like logs, sync times, caches.
|
|
|
|
*/
|
2012-08-13 16:51:58 -07:00
|
|
|
resetService: function resetService() {
|
|
|
|
this._catch(function reset() {
|
2010-12-15 14:36:53 -08:00
|
|
|
this._log.info("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 = "";
|
2012-09-14 16:02:32 -07:00
|
|
|
this.recordManager.clearCache();
|
2012-08-13 16:51:58 -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
|
|
|
*/
|
2012-08-13 16:51:58 -07:00
|
|
|
resetClient: function resetClient(engines) {
|
|
|
|
this._catch(function doResetClient() {
|
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
|
|
|
|
2012-08-29 14:43:40 -07:00
|
|
|
engines = [this.clientsEngine].concat(this.engineManager.getAll());
|
2009-03-20 00:13:16 -07:00
|
|
|
}
|
|
|
|
// Convert the array of names into engines
|
2012-08-13 16:51:58 -07:00
|
|
|
else {
|
2012-08-29 14:43:40 -07:00
|
|
|
engines = this.engineManager.get(engines);
|
2012-08-13 16:51:58 -07:00
|
|
|
}
|
2009-02-19 04:09:55 -08:00
|
|
|
|
2009-02-26 22:36:14 -08:00
|
|
|
// Have each engine drop any temporary meta data
|
2012-08-13 16:51:58 -07:00
|
|
|
for each (let engine in engines) {
|
2009-06-05 15:34:34 -07:00
|
|
|
engine.resetClient();
|
2012-08-13 16:51:58 -07:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
},
|
2009-11-12 12:44:33 -08:00
|
|
|
|
2011-08-04 19:02:49 -07:00
|
|
|
/**
|
|
|
|
* Fetch storage info from the server.
|
2011-08-04 16:19:02 -07:00
|
|
|
*
|
2011-07-14 12:11:42 -07:00
|
|
|
* @param type
|
|
|
|
* String specifying what info to fetch from the server. Must be one
|
|
|
|
* of the INFO_* values. See Sync Storage Server API spec for details.
|
|
|
|
* @param callback
|
|
|
|
* Callback function with signature (error, data) where `data' is
|
|
|
|
* the return value from the server already parsed as JSON.
|
2011-08-04 16:19:02 -07:00
|
|
|
*
|
2011-07-14 12:11:42 -07:00
|
|
|
* @return RESTRequest instance representing the request, allowing callers
|
|
|
|
* to cancel the request.
|
|
|
|
*/
|
|
|
|
getStorageInfo: function getStorageInfo(type, callback) {
|
|
|
|
if (STORAGE_INFO_TYPES.indexOf(type) == -1) {
|
|
|
|
throw "Invalid value for 'type': " + type;
|
|
|
|
}
|
2010-09-11 09:39:21 -07:00
|
|
|
|
2011-07-14 12:11:42 -07:00
|
|
|
let info_type = "info/" + type;
|
|
|
|
this._log.trace("Retrieving '" + info_type + "'...");
|
|
|
|
let url = this.userBaseURL + info_type;
|
2012-09-14 16:02:32 -07:00
|
|
|
return this.getStorageRequest(url).get(function onComplete(error) {
|
2011-07-14 12:11:42 -07:00
|
|
|
// Note: 'this' is the request.
|
|
|
|
if (error) {
|
|
|
|
this._log.debug("Failed to retrieve '" + info_type + "': " +
|
|
|
|
Utils.exceptionStr(error));
|
|
|
|
return callback(error);
|
|
|
|
}
|
|
|
|
if (this.response.status != 200) {
|
|
|
|
this._log.debug("Failed to retrieve '" + info_type +
|
|
|
|
"': server responded with HTTP" +
|
|
|
|
this.response.status);
|
|
|
|
return callback(this.response);
|
|
|
|
}
|
2010-09-11 09:39:21 -07:00
|
|
|
|
2011-07-14 12:11:42 -07:00
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
result = JSON.parse(this.response.body);
|
|
|
|
} catch (ex) {
|
|
|
|
this._log.debug("Server returned invalid JSON for '" + info_type +
|
|
|
|
"': " + this.response.body);
|
|
|
|
return callback(ex);
|
|
|
|
}
|
|
|
|
this._log.trace("Successfully retrieved '" + info_type + "'.");
|
|
|
|
return callback(null, result);
|
|
|
|
});
|
2012-08-29 14:43:41 -07:00
|
|
|
},
|
2009-03-03 15:29:35 -08:00
|
|
|
};
|
2012-08-29 14:43:41 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.Service = new Sync11Service();
|
2012-08-29 14:43:41 -07:00
|
|
|
Service.onStartup();
|