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-03 11:32:59 -07:00
|
|
|
* Myk Melez <myk@mozilla.org>
|
2007-12-10 21:38:53 -08:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2008-06-03 14:45:53 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['Engines',
|
|
|
|
'Engine'];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2007-12-10 21:38:53 -08:00
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
Cu.import("resource://weave/constants.js");
|
|
|
|
Cu.import("resource://weave/util.js");
|
2008-05-28 20:11:39 -07:00
|
|
|
Cu.import("resource://weave/wrap.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
Cu.import("resource://weave/crypto.js");
|
2008-03-31 07:20:09 -07:00
|
|
|
Cu.import("resource://weave/dav.js");
|
2008-05-28 20:11:39 -07:00
|
|
|
Cu.import("resource://weave/remote.js");
|
2008-03-19 15:17:04 -07:00
|
|
|
Cu.import("resource://weave/identity.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
Cu.import("resource://weave/stores.js");
|
|
|
|
Cu.import("resource://weave/syncCores.js");
|
2008-05-22 15:58:29 -07:00
|
|
|
Cu.import("resource://weave/trackers.js");
|
2008-03-07 01:56:36 -08:00
|
|
|
Cu.import("resource://weave/async.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
Function.prototype.async = Async.sugar;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-04-14 18:53:35 -07:00
|
|
|
// Singleton service, holds registered engines
|
|
|
|
|
|
|
|
Utils.lazy(this, 'Engines', EngineManagerSvc);
|
|
|
|
|
|
|
|
function EngineManagerSvc() {
|
|
|
|
this._engines = {};
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2008-04-14 18:53:35 -07:00
|
|
|
EngineManagerSvc.prototype = {
|
|
|
|
get: function EngMgr_get(name) {
|
2008-05-23 11:05:42 -07:00
|
|
|
return this._engines[name];
|
2008-04-14 18:53:35 -07:00
|
|
|
},
|
|
|
|
getAll: function EngMgr_getAll() {
|
|
|
|
let ret = [];
|
|
|
|
for (key in this._engines) {
|
|
|
|
ret.push(this._engines[key]);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
register: function EngMgr_register(engine) {
|
|
|
|
this._engines[engine.name] = engine;
|
|
|
|
},
|
|
|
|
unregister: function EngMgr_unregister(val) {
|
|
|
|
let name = val;
|
|
|
|
if (val instanceof Engine)
|
|
|
|
name = val.name;
|
|
|
|
delete this._engines[name];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function Engine() {}
|
2007-12-14 18:07:25 -08:00
|
|
|
Engine.prototype = {
|
2008-05-28 20:11:39 -07:00
|
|
|
_notify: Wrap.notify,
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
// "default-engine";
|
|
|
|
get name() { throw "name property must be overridden in subclasses"; },
|
|
|
|
|
|
|
|
// "DefaultEngine";
|
|
|
|
get logName() { throw "logName property must be overridden in subclasses"; },
|
|
|
|
|
|
|
|
// "user-data/default-engine/";
|
|
|
|
get serverPrefix() { throw "serverPrefix property must be overridden in subclasses"; },
|
|
|
|
|
2008-06-11 00:03:28 -07:00
|
|
|
get snapshot() this._snapshot,
|
|
|
|
|
2008-05-28 20:11:39 -07:00
|
|
|
get _remote() {
|
|
|
|
if (!this.__remote)
|
2008-06-01 19:10:11 -07:00
|
|
|
this.__remote = new RemoteStore(this.serverPrefix, 'Engine:' + this.name);
|
2008-05-28 20:11:39 -07:00
|
|
|
return this.__remote;
|
|
|
|
},
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-04-15 17:21:34 -07:00
|
|
|
get enabled() {
|
|
|
|
return Utils.prefs.getBoolPref("engine." + this.name);
|
|
|
|
},
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
__os: null,
|
|
|
|
get _os() {
|
|
|
|
if (!this.__os)
|
|
|
|
this.__os = Cc["@mozilla.org/observer-service;1"]
|
|
|
|
.getService(Ci.nsIObserverService);
|
|
|
|
return this.__os;
|
|
|
|
},
|
|
|
|
|
2008-03-05 00:00:56 -08:00
|
|
|
__json: null,
|
|
|
|
get _json() {
|
|
|
|
if (!this.__json)
|
|
|
|
this.__json = Cc["@mozilla.org/dom/json;1"].
|
|
|
|
createInstance(Ci.nsIJSON);
|
|
|
|
return this.__json;
|
|
|
|
},
|
|
|
|
|
2008-05-22 15:58:29 -07:00
|
|
|
// _core, _store and _tracker need to be overridden in subclasses
|
2007-12-10 21:38:53 -08:00
|
|
|
__core: null,
|
|
|
|
get _core() {
|
|
|
|
if (!this.__core)
|
2007-12-14 18:07:25 -08:00
|
|
|
this.__core = new SyncCore();
|
2007-12-10 21:38:53 -08:00
|
|
|
return this.__core;
|
|
|
|
},
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
__store: null,
|
|
|
|
get _store() {
|
|
|
|
if (!this.__store)
|
|
|
|
this.__store = new Store();
|
|
|
|
return this.__store;
|
|
|
|
},
|
2008-05-23 11:05:42 -07:00
|
|
|
|
2008-05-22 15:58:29 -07:00
|
|
|
__tracker: null,
|
|
|
|
get _tracker() {
|
|
|
|
if (!this.__tracker)
|
|
|
|
this.__tracker = new Tracker();
|
|
|
|
return this.__tracker;
|
|
|
|
},
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
__snapshot: null,
|
|
|
|
get _snapshot() {
|
|
|
|
if (!this.__snapshot)
|
2007-12-14 18:07:25 -08:00
|
|
|
this.__snapshot = new SnapshotStore(this.name);
|
2007-12-10 21:38:53 -08:00
|
|
|
return this.__snapshot;
|
|
|
|
},
|
|
|
|
set _snapshot(value) {
|
|
|
|
this.__snapshot = value;
|
|
|
|
},
|
|
|
|
|
2008-04-15 17:21:34 -07:00
|
|
|
get _pbeId() {
|
|
|
|
let id = ID.get('Engine:PBE:' + this.name);
|
|
|
|
if (!id)
|
|
|
|
id = ID.get('Engine:PBE:default');
|
|
|
|
if (!id)
|
|
|
|
throw "No identity found for engine PBE!";
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
2008-05-23 19:47:25 -07:00
|
|
|
get _engineId() {
|
2008-06-03 15:14:27 -07:00
|
|
|
let id = ID.get('Engine:' + this.name);
|
2008-06-01 19:10:11 -07:00
|
|
|
if (!id ||
|
|
|
|
id.username != this._pbeId.username || id.realm != this._pbeId.realm) {
|
2008-05-23 19:47:25 -07:00
|
|
|
let password = null;
|
2008-06-01 19:10:11 -07:00
|
|
|
if (id)
|
|
|
|
password = id.password;
|
|
|
|
id = new Identity(this._pbeId.realm + ' - ' + this.logName,
|
|
|
|
this._pbeId.username, password);
|
|
|
|
ID.set('Engine:' + this.name, id);
|
2008-05-23 19:47:25 -07:00
|
|
|
}
|
2008-06-01 19:10:11 -07:00
|
|
|
return id;
|
2008-05-23 19:47:25 -07:00
|
|
|
},
|
|
|
|
|
2008-04-15 17:21:34 -07:00
|
|
|
_init: function Engine__init() {
|
2007-12-14 18:07:25 -08:00
|
|
|
this._log = Log4Moz.Service.getLogger("Service." + this.logName);
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.level =
|
|
|
|
Log4Moz.Level[Utils.prefs.getCharPref("log.logger.service.engine")];
|
2007-12-14 18:07:25 -08:00
|
|
|
this._osPrefix = "weave:" + this.name + ":";
|
2007-12-10 21:38:53 -08:00
|
|
|
this._snapshot.load();
|
|
|
|
},
|
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
_getSymKey: function Engine__getSymKey() {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
|
|
|
|
2008-05-23 19:57:38 -07:00
|
|
|
if ("none" == Utils.prefs.getCharPref("encryption"))
|
|
|
|
return;
|
|
|
|
|
2008-06-05 00:17:32 -07:00
|
|
|
this._remote.keys.getKey(self.cb, this._pbeId);
|
2008-03-19 15:17:04 -07:00
|
|
|
let symkey = yield;
|
|
|
|
this._engineId.setTempPassword(symkey);
|
|
|
|
|
2008-05-23 19:57:38 -07:00
|
|
|
self.done();
|
2008-03-07 01:56:36 -08:00
|
|
|
},
|
|
|
|
|
2008-03-05 00:00:56 -08:00
|
|
|
_serializeCommands: function Engine__serializeCommands(commands) {
|
|
|
|
let json = this._json.encode(commands);
|
|
|
|
//json = json.replace(/ {action/g, "\n {action");
|
|
|
|
return json;
|
|
|
|
},
|
2008-03-31 07:20:09 -07:00
|
|
|
|
2008-03-05 00:00:56 -08:00
|
|
|
_serializeConflicts: function Engine__serializeConflicts(conflicts) {
|
|
|
|
let json = this._json.encode(conflicts);
|
|
|
|
//json = json.replace(/ {action/g, "\n {action");
|
|
|
|
return json;
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
_resetServer: function Engine__resetServer() {
|
|
|
|
let self = yield;
|
2008-05-28 20:11:39 -07:00
|
|
|
this._log.debug("Resetting server data");
|
|
|
|
this._remote.status.delete(self.cb);
|
|
|
|
yield;
|
|
|
|
this._remote.keys.delete(self.cb);
|
|
|
|
yield;
|
|
|
|
this._remote.snapshot.delete(self.cb);
|
|
|
|
yield;
|
|
|
|
this._remote.deltas.delete(self.cb);
|
|
|
|
yield;
|
|
|
|
this._log.debug("Server files deleted");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
_resetClient: function Engine__resetClient() {
|
|
|
|
let self = yield;
|
2008-05-28 20:11:39 -07:00
|
|
|
this._log.debug("Resetting client state");
|
|
|
|
this._snapshot.wipe();
|
|
|
|
this._store.wipe();
|
|
|
|
this._log.debug("Client reset completed successfully");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// original
|
|
|
|
// / \
|
|
|
|
// A / \ B
|
|
|
|
// / \
|
|
|
|
// client --C-> server
|
|
|
|
// \ /
|
|
|
|
// D \ / C
|
|
|
|
// \ /
|
|
|
|
// final
|
|
|
|
|
|
|
|
// If we have a saved snapshot, original == snapshot. Otherwise,
|
|
|
|
// it's the empty set {}.
|
|
|
|
|
|
|
|
// C is really the diff between server -> final, so if we determine
|
|
|
|
// D we can calculate C from that. In the case where A and B have
|
|
|
|
// no conflicts, C == A and D == B.
|
|
|
|
|
|
|
|
// Sync flow:
|
|
|
|
// 1) Fetch server deltas
|
|
|
|
// 1.1) Construct current server status from snapshot + server deltas
|
|
|
|
// 1.2) Generate single delta from snapshot -> current server status ("B")
|
|
|
|
// 2) Generate local deltas from snapshot -> current client status ("A")
|
|
|
|
// 3) Reconcile client/server deltas and generate new deltas for them.
|
|
|
|
// Reconciliation won't generate C directly, we will simply diff
|
|
|
|
// server->final after step 3.1.
|
|
|
|
// 3.1) Apply local delta with server changes ("D")
|
|
|
|
// 3.2) Append server delta to the delta file and upload ("C")
|
|
|
|
|
2008-06-02 15:24:52 -07:00
|
|
|
_sync: function Engine__sync() {
|
2008-03-07 01:56:36 -08:00
|
|
|
let self = yield;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.info("Beginning sync");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-06-10 19:12:04 -07:00
|
|
|
this._remote.initSession(self.cb);
|
|
|
|
yield;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-06-10 19:12:04 -07:00
|
|
|
this._log.info("Local snapshot version: " + this._snapshot.version);
|
|
|
|
this._log.info("Server maxVersion: " + this._remote.status.data.maxVersion);
|
|
|
|
this._log.debug("Server snapVersion: " + this._remote.status.data.snapVersion);
|
2008-06-05 00:17:32 -07:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
// 1) Fetch server deltas
|
|
|
|
this._getServerData.async(this, self.cb);
|
|
|
|
let server = yield;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
// 2) Generate local deltas from snapshot -> current client status
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
let localJson = new SnapshotStore();
|
|
|
|
localJson.data = this._store.wrap();
|
|
|
|
this._core.detectUpdates(self.cb, this._snapshot.data, localJson.data);
|
|
|
|
let localUpdates = yield;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.trace("local json:\n" + localJson.serialize());
|
|
|
|
this._log.trace("Local updates: " + this._serializeCommands(localUpdates));
|
|
|
|
this._log.trace("Server updates: " + this._serializeCommands(server.updates));
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
if (server.updates.length == 0 && localUpdates.length == 0) {
|
|
|
|
this._snapshot.version = server.maxVersion;
|
|
|
|
this._log.info("Sync complete: no changes needed on client or server");
|
|
|
|
self.done(true);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
// 3) Reconcile client/server deltas and generate new deltas for them.
|
|
|
|
|
|
|
|
this._log.info("Reconciling client/server updates");
|
|
|
|
this._core.reconcile(self.cb, localUpdates, server.updates);
|
|
|
|
ret = yield;
|
|
|
|
|
|
|
|
let clientChanges = ret.propagations[0];
|
|
|
|
let serverChanges = ret.propagations[1];
|
|
|
|
let clientConflicts = ret.conflicts[0];
|
|
|
|
let serverConflicts = ret.conflicts[1];
|
|
|
|
|
|
|
|
this._log.info("Changes for client: " + clientChanges.length);
|
|
|
|
this._log.info("Predicted changes for server: " + serverChanges.length);
|
|
|
|
this._log.info("Client conflicts: " + clientConflicts.length);
|
|
|
|
this._log.info("Server conflicts: " + serverConflicts.length);
|
|
|
|
this._log.trace("Changes for client: " + this._serializeCommands(clientChanges));
|
|
|
|
this._log.trace("Predicted changes for server: " + this._serializeCommands(serverChanges));
|
|
|
|
this._log.trace("Client conflicts: " + this._serializeConflicts(clientConflicts));
|
|
|
|
this._log.trace("Server conflicts: " + this._serializeConflicts(serverConflicts));
|
|
|
|
|
|
|
|
if (!(clientChanges.length || serverChanges.length ||
|
|
|
|
clientConflicts.length || serverConflicts.length)) {
|
|
|
|
this._log.info("Sync complete: no changes needed on client or server");
|
|
|
|
this._snapshot.data = localJson.data;
|
|
|
|
this._snapshot.version = server.maxVersion;
|
|
|
|
this._snapshot.save();
|
|
|
|
self.done(true);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
if (clientConflicts.length || serverConflicts.length) {
|
|
|
|
this._log.warn("Conflicts found! Discarding server changes");
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
let savedSnap = Utils.deepCopy(this._snapshot.data);
|
|
|
|
let savedVersion = this._snapshot.version;
|
|
|
|
let newSnapshot;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
// 3.1) Apply server changes to local store
|
|
|
|
if (clientChanges.length) {
|
|
|
|
this._log.info("Applying changes locally");
|
|
|
|
// Note that we need to need to apply client changes to the
|
|
|
|
// current tree, not the saved snapshot
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-26 23:11:15 -07:00
|
|
|
localJson.applyCommands.async(localJson, self.cb, clientChanges);
|
|
|
|
yield;
|
2008-03-19 15:17:04 -07:00
|
|
|
this._snapshot.data = localJson.data;
|
|
|
|
this._snapshot.version = server.maxVersion;
|
2008-03-26 23:11:15 -07:00
|
|
|
this._store.applyCommands.async(this._store, self.cb, clientChanges);
|
|
|
|
yield;
|
2007-12-10 21:38:53 -08:00
|
|
|
newSnapshot = this._store.wrap();
|
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
this._core.detectUpdates(self.cb, this._snapshot.data, newSnapshot);
|
|
|
|
let diff = yield;
|
|
|
|
if (diff.length != 0) {
|
|
|
|
this._log.warn("Commands did not apply correctly");
|
|
|
|
this._log.debug("Diff from snapshot+commands -> " +
|
|
|
|
"new snapshot after commands:\n" +
|
|
|
|
this._serializeCommands(diff));
|
|
|
|
// FIXME: do we really want to revert the snapshot here?
|
|
|
|
this._snapshot.data = Utils.deepCopy(savedSnap);
|
|
|
|
this._snapshot.version = savedVersion;
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2008-03-19 15:17:04 -07:00
|
|
|
this._snapshot.save();
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
// 3.2) Append server delta to the delta file and upload
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
// Generate a new diff, from the current server snapshot to the
|
|
|
|
// current client snapshot. In the case where there are no
|
|
|
|
// conflicts, it should be the same as what the resolver returned
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
newSnapshot = this._store.wrap();
|
|
|
|
this._core.detectUpdates(self.cb, server.snapshot, newSnapshot);
|
|
|
|
let serverDelta = yield;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
// Log an error if not the same
|
|
|
|
if (!(serverConflicts.length ||
|
|
|
|
Utils.deepEquals(serverChanges, serverDelta)))
|
|
|
|
this._log.warn("Predicted server changes differ from " +
|
|
|
|
"actual server->client diff (can be ignored in many cases)");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.info("Actual changes for server: " + serverDelta.length);
|
|
|
|
this._log.debug("Actual changes for server: " +
|
|
|
|
this._serializeCommands(serverDelta));
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
if (serverDelta.length) {
|
|
|
|
this._log.info("Uploading changes to server");
|
|
|
|
this._snapshot.data = newSnapshot;
|
|
|
|
this._snapshot.version = ++server.maxVersion;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-06-10 19:12:04 -07:00
|
|
|
/*
|
|
|
|
if (server.formatVersion != ENGINE_STORAGE_FORMAT_VERSION) {
|
2008-03-19 15:17:04 -07:00
|
|
|
this._fullUpload.async(this, self.cb);
|
|
|
|
let status = yield;
|
|
|
|
if (!status)
|
|
|
|
this._log.error("Could not upload files to server"); // eep?
|
2008-06-10 19:12:04 -07:00
|
|
|
*/
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-06-10 19:12:04 -07:00
|
|
|
this._remote.appendDelta(self.cb, serverDelta);
|
|
|
|
yield;
|
|
|
|
|
|
|
|
let c = 0;
|
|
|
|
for (GUID in this._snapshot.data)
|
|
|
|
c++;
|
|
|
|
|
|
|
|
this._remote.status.data.maxVersion = this._snapshot.version;
|
|
|
|
this._remote.status.data.snapEncryption = Crypto.defaultAlgorithm;
|
|
|
|
this._remote.status.data.itemCount = c;
|
|
|
|
this._remote.status.put(self.cb, this._remote.status.data);
|
2008-06-11 17:44:08 -07:00
|
|
|
yield;
|
2008-06-10 19:12:04 -07:00
|
|
|
|
|
|
|
this._log.info("Successfully updated deltas and status on server");
|
|
|
|
this._snapshot.save();
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2008-03-19 15:17:04 -07:00
|
|
|
|
|
|
|
this._log.info("Sync complete");
|
|
|
|
self.done(true);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
/* Get the deltas/combined updates from the server
|
|
|
|
* Returns:
|
|
|
|
* status:
|
|
|
|
* -1: error
|
|
|
|
* 0: ok
|
|
|
|
* These fields may be null when status is -1:
|
|
|
|
* formatVersion:
|
|
|
|
* version of the data format itself. For compatibility checks.
|
|
|
|
* maxVersion:
|
|
|
|
* the latest version on the server
|
|
|
|
* snapVersion:
|
|
|
|
* the version of the current snapshot on the server (deltas not applied)
|
|
|
|
* snapEncryption:
|
|
|
|
* encryption algorithm currently used on the server-stored snapshot
|
|
|
|
* deltasEncryption:
|
|
|
|
* encryption algorithm currently used on the server-stored deltas
|
|
|
|
* snapshot:
|
|
|
|
* full snapshot of the latest server version (deltas applied)
|
|
|
|
* deltas:
|
|
|
|
* all of the individual deltas on the server
|
|
|
|
* updates:
|
|
|
|
* the relevant deltas (from our snapshot version to current),
|
|
|
|
* combined into a single set.
|
|
|
|
*/
|
2008-03-07 01:56:36 -08:00
|
|
|
_getServerData: function BmkEngine__getServerData() {
|
|
|
|
let self = yield;
|
2008-05-30 20:43:55 -07:00
|
|
|
let status;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-05-28 20:11:39 -07:00
|
|
|
try {
|
|
|
|
this._log.debug("Getting status file from server");
|
|
|
|
this._remote.status.get(self.cb);
|
2008-05-30 20:43:55 -07:00
|
|
|
status = yield;
|
2008-05-28 20:11:39 -07:00
|
|
|
this._log.info("Got status file from server");
|
2008-03-19 15:17:04 -07:00
|
|
|
|
2008-05-28 20:11:39 -07:00
|
|
|
} catch (e if e.message.status == 404) {
|
2008-03-07 01:56:36 -08:00
|
|
|
this._log.info("Server has no status file, Initial upload to server");
|
|
|
|
|
|
|
|
this._snapshot.data = this._store.wrap();
|
|
|
|
this._snapshot.version = 0;
|
|
|
|
this._snapshot.GUID = null; // in case there are other snapshots out there
|
|
|
|
|
|
|
|
this._fullUpload.async(this, self.cb);
|
|
|
|
let uploadStatus = yield;
|
|
|
|
if (!uploadStatus)
|
2008-05-28 20:11:39 -07:00
|
|
|
throw "Initial upload failed";
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
this._log.info("Initial upload to server successful");
|
|
|
|
this._snapshot.save();
|
|
|
|
|
2008-05-28 20:11:39 -07:00
|
|
|
self.done({status: 0,
|
|
|
|
formatVersion: ENGINE_STORAGE_FORMAT_VERSION,
|
|
|
|
maxVersion: this._snapshot.version,
|
|
|
|
snapVersion: this._snapshot.version,
|
|
|
|
snapEncryption: Crypto.defaultAlgorithm,
|
|
|
|
deltasEncryption: Crypto.defaultAlgorithm,
|
|
|
|
snapshot: Utils.deepCopy(this._snapshot.data),
|
|
|
|
deltas: [],
|
|
|
|
updates: []});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ret = {status: -1,
|
|
|
|
formatVersion: null, maxVersion: null, snapVersion: null,
|
|
|
|
snapEncryption: null, deltasEncryption: null,
|
|
|
|
snapshot: null, deltas: null, updates: null};
|
|
|
|
let deltas, allDeltas;
|
|
|
|
let snap = new SnapshotStore();
|
|
|
|
|
|
|
|
// Bail out if the server has a newer format version than we can parse
|
|
|
|
if (status.formatVersion > ENGINE_STORAGE_FORMAT_VERSION) {
|
|
|
|
this._log.error("Server uses storage format v" + status.formatVersion +
|
|
|
|
", this client understands up to v" + ENGINE_STORAGE_FORMAT_VERSION);
|
|
|
|
throw "Incompatible server format for engine data";
|
|
|
|
}
|
|
|
|
|
|
|
|
this._getSymKey.async(this, self.cb);
|
|
|
|
yield;
|
|
|
|
|
|
|
|
if (status.formatVersion == 0) {
|
|
|
|
ret.snapEncryption = status.snapEncryption = "none";
|
|
|
|
ret.deltasEncryption = status.deltasEncryption = "none";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status.GUID != this._snapshot.GUID) {
|
|
|
|
this._log.info("Remote/local sync GUIDs do not match. " +
|
|
|
|
"Forcing initial sync.");
|
|
|
|
this._log.debug("Remote: " + status.GUID);
|
|
|
|
this._log.debug("Local: " + this._snapshot.GUID);
|
|
|
|
this._store.resetGUIDs();
|
|
|
|
this._snapshot.data = {};
|
|
|
|
this._snapshot.version = -1;
|
|
|
|
this._snapshot.GUID = status.GUID;
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2008-03-07 01:56:36 -08:00
|
|
|
|
2008-05-28 20:11:39 -07:00
|
|
|
if (this._snapshot.version < status.snapVersion) {
|
|
|
|
this._log.trace("Local snapshot version < server snapVersion");
|
|
|
|
|
|
|
|
if (this._snapshot.version >= 0)
|
|
|
|
this._log.info("Local snapshot is out of date");
|
|
|
|
|
|
|
|
this._log.info("Downloading server snapshot");
|
2008-06-05 00:17:32 -07:00
|
|
|
this._remote.snapshot.get(self.cb);
|
2008-05-30 20:43:55 -07:00
|
|
|
snap.data = yield;
|
2008-05-28 20:11:39 -07:00
|
|
|
|
|
|
|
this._log.info("Downloading server deltas");
|
2008-06-05 00:17:32 -07:00
|
|
|
this._remote.deltas.get(self.cb);
|
2008-05-30 20:43:55 -07:00
|
|
|
allDeltas = yield;
|
|
|
|
deltas = allDeltas;
|
2008-05-28 20:11:39 -07:00
|
|
|
|
|
|
|
} else if (this._snapshot.version >= status.snapVersion &&
|
|
|
|
this._snapshot.version < status.maxVersion) {
|
|
|
|
this._log.trace("Server snapVersion <= local snapshot version < server maxVersion");
|
|
|
|
snap.data = Utils.deepCopy(this._snapshot.data);
|
|
|
|
|
|
|
|
this._log.info("Downloading server deltas");
|
2008-06-05 00:17:32 -07:00
|
|
|
this._remote.deltas.get(self.cb);
|
2008-05-30 20:43:55 -07:00
|
|
|
allDeltas = yield;
|
2008-05-28 20:11:39 -07:00
|
|
|
deltas = allDeltas.slice(this._snapshot.version - status.snapVersion);
|
|
|
|
|
|
|
|
} else if (this._snapshot.version == status.maxVersion) {
|
|
|
|
this._log.trace("Local snapshot version == server maxVersion");
|
|
|
|
snap.data = Utils.deepCopy(this._snapshot.data);
|
|
|
|
|
|
|
|
// FIXME: could optimize this case by caching deltas file
|
|
|
|
this._log.info("Downloading server deltas");
|
2008-06-05 00:17:32 -07:00
|
|
|
this._remote.deltas.get(self.cb);
|
2008-05-30 20:43:55 -07:00
|
|
|
allDeltas = yield;
|
2008-05-28 20:11:39 -07:00
|
|
|
deltas = [];
|
|
|
|
|
|
|
|
} else { // this._snapshot.version > status.maxVersion
|
|
|
|
this._log.error("Server snapshot is older than local snapshot");
|
|
|
|
throw "Server snapshot is older than local snapshot";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (var i = 0; i < deltas.length; i++) {
|
|
|
|
snap.applyCommands.async(snap, self.cb, deltas[i]);
|
|
|
|
yield;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this._log.error("Error applying remote deltas to saved snapshot");
|
|
|
|
this._log.error("Clearing local snapshot; next sync will merge");
|
|
|
|
this._log.debug("Exception: " + Utils.exceptionStr(e));
|
|
|
|
this._log.trace("Stack:\n" + Utils.stackTrace(e));
|
|
|
|
this._snapshot.wipe();
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.status = 0;
|
|
|
|
ret.formatVersion = status.formatVersion;
|
|
|
|
ret.maxVersion = status.maxVersion;
|
|
|
|
ret.snapVersion = status.snapVersion;
|
|
|
|
ret.snapEncryption = status.snapEncryption;
|
|
|
|
ret.deltasEncryption = status.deltasEncryption;
|
|
|
|
ret.snapshot = snap.data;
|
|
|
|
ret.deltas = allDeltas;
|
|
|
|
this._core.detectUpdates(self.cb, this._snapshot.data, snap.data);
|
|
|
|
ret.updates = yield;
|
|
|
|
|
2008-06-03 15:14:27 -07:00
|
|
|
self.done(ret);
|
2008-03-07 01:56:36 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_fullUpload: function Engine__fullUpload() {
|
|
|
|
let self = yield;
|
|
|
|
let ret = false;
|
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
Crypto.PBEkeygen.async(Crypto, self.cb);
|
|
|
|
let symkey = yield;
|
|
|
|
this._engineId.setTempPassword(symkey);
|
|
|
|
if (!this._engineId.password)
|
|
|
|
throw "Could not generate a symmetric encryption key";
|
2008-05-23 11:05:42 -07:00
|
|
|
|
2008-05-21 16:28:23 -07:00
|
|
|
let enckey = this._engineId.password;
|
|
|
|
if ("none" != Utils.prefs.getCharPref("encryption")) {
|
|
|
|
Crypto.RSAencrypt.async(Crypto, self.cb,
|
|
|
|
this._engineId.password, this._pbeId);
|
|
|
|
enckey = yield;
|
|
|
|
}
|
2008-05-23 11:05:42 -07:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
if (!enckey)
|
|
|
|
throw "Could not encrypt symmetric encryption key";
|
|
|
|
|
|
|
|
let keys = {ring: {}};
|
2008-06-05 00:17:32 -07:00
|
|
|
keys.ring[this._engineId.username] = enckey;
|
2008-05-30 20:43:55 -07:00
|
|
|
this._remote.keys.put(self.cb, keys);
|
2008-05-28 20:11:39 -07:00
|
|
|
yield;
|
2008-03-19 15:17:04 -07:00
|
|
|
|
2008-05-30 20:43:55 -07:00
|
|
|
this._remote.snapshot.put(self.cb, this._snapshot.wrap());
|
2008-05-28 20:11:39 -07:00
|
|
|
yield;
|
2008-05-30 20:43:55 -07:00
|
|
|
this._remote.deltas.put(self.cb, []);
|
2008-05-28 20:11:39 -07:00
|
|
|
yield;
|
2008-03-07 01:56:36 -08:00
|
|
|
|
|
|
|
let c = 0;
|
|
|
|
for (GUID in this._snapshot.data)
|
|
|
|
c++;
|
|
|
|
|
2008-05-28 20:11:39 -07:00
|
|
|
this._remote.status.put(self.cb,
|
2008-05-30 20:43:55 -07:00
|
|
|
{GUID: this._snapshot.GUID,
|
|
|
|
formatVersion: ENGINE_STORAGE_FORMAT_VERSION,
|
|
|
|
snapVersion: this._snapshot.version,
|
|
|
|
maxVersion: this._snapshot.version,
|
|
|
|
snapEncryption: Crypto.defaultAlgorithm,
|
|
|
|
deltasEncryption: "none",
|
|
|
|
itemCount: c});
|
2008-05-28 20:11:39 -07:00
|
|
|
yield;
|
2008-03-07 01:56:36 -08:00
|
|
|
|
|
|
|
this._log.info("Full upload to server successful");
|
|
|
|
ret = true;
|
2008-05-23 11:05:42 -07:00
|
|
|
self.done(ret);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-06 17:33:44 -07:00
|
|
|
_share: function Engine__share(guid, username) {
|
|
|
|
/* This should be overridden by the engine subclass for each datatype.
|
|
|
|
Implementation should share the data node identified by guid,
|
|
|
|
and all its children, if any, with the user identified by username. */
|
|
|
|
return;
|
2008-03-28 03:25:51 -07:00
|
|
|
},
|
|
|
|
|
2008-06-06 17:33:44 -07:00
|
|
|
// TODO need a "stop sharing" function.
|
2008-03-25 23:01:34 -07:00
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
sync: function Engine_sync(onComplete) {
|
2007-12-10 21:38:53 -08:00
|
|
|
return this._sync.async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
2008-06-06 17:33:44 -07:00
|
|
|
share: function Engine_share(onComplete, guid, username) {
|
|
|
|
return this._share.async(this, onComplete, guid, username);
|
2008-03-25 23:01:34 -07:00
|
|
|
},
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
resetServer: function Engine_resetServer(onComplete) {
|
2008-05-28 20:11:39 -07:00
|
|
|
this._notify("reset-server", this._resetServer).async(this, onComplete);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
resetClient: function Engine_resetClient(onComplete) {
|
2008-05-28 20:11:39 -07:00
|
|
|
this._notify("reset-client", this._resetClient).async(this, onComplete);
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
|
|
|
};
|