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-08-08 14:42:57 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['Engines', 'Engine', 'SyncEngine', 'BlobEngine'];
|
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-11-03 15:00:38 -08:00
|
|
|
Cu.import("resource://weave/resource.js");
|
2008-07-29 18:38:58 -07:00
|
|
|
Cu.import("resource://weave/clientData.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;
|
|
|
|
},
|
2008-07-09 17:17:24 -07:00
|
|
|
getEnabled: function EngMgr_getEnabled() {
|
|
|
|
let ret = [];
|
|
|
|
for (key in this._engines) {
|
|
|
|
if(this._engines[key].enabled)
|
|
|
|
ret.push(this._engines[key]);
|
2008-07-21 18:23:51 -07:00
|
|
|
}
|
2008-07-09 17:17:24 -07:00
|
|
|
return ret;
|
|
|
|
},
|
2008-04-14 18:53:35 -07:00
|
|
|
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"; },
|
|
|
|
|
2008-07-09 17:17:24 -07:00
|
|
|
// "Default";
|
|
|
|
get displayName() { throw "displayName property must be overriden in subclasses"; },
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
// "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-05-28 20:11:39 -07:00
|
|
|
get _remote() {
|
2008-07-25 01:06:23 -07:00
|
|
|
let remote = new RemoteStore(this);
|
|
|
|
this.__defineGetter__("_remote", function() remote);
|
|
|
|
return remote;
|
2008-05-28 20:11:39 -07:00
|
|
|
},
|
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
|
|
|
get _os() {
|
2008-07-25 01:06:23 -07:00
|
|
|
let os = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
|
|
|
this.__defineGetter__("_os", function() os);
|
|
|
|
return os;
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-03-05 00:00:56 -08:00
|
|
|
get _json() {
|
2008-07-25 01:06:23 -07:00
|
|
|
let json = Cc["@mozilla.org/dom/json;1"].
|
|
|
|
createInstance(Ci.nsIJSON);
|
|
|
|
this.__defineGetter__("_json", function() json);
|
|
|
|
return json;
|
2008-03-05 00:00:56 -08:00
|
|
|
},
|
|
|
|
|
2008-05-22 15:58:29 -07:00
|
|
|
// _core, _store and _tracker need to be overridden in subclasses
|
2007-12-14 18:07:25 -08:00
|
|
|
get _store() {
|
2008-07-25 01:06:23 -07:00
|
|
|
let store = new Store();
|
|
|
|
this.__defineGetter__("_store", function() store);
|
|
|
|
return store;
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
2008-05-23 11:05:42 -07:00
|
|
|
|
2008-06-27 20:16:43 -07:00
|
|
|
get _core() {
|
2008-07-25 01:06:23 -07:00
|
|
|
let core = new SyncCore(this._store);
|
|
|
|
this.__defineGetter__("_core", function() core);
|
|
|
|
return core;
|
2008-06-27 20:16:43 -07:00
|
|
|
},
|
|
|
|
|
2008-05-22 15:58:29 -07:00
|
|
|
get _tracker() {
|
2008-07-25 01:06:23 -07:00
|
|
|
let tracker = new tracker();
|
|
|
|
this.__defineGetter__("_tracker", function() tracker);
|
|
|
|
return tracker;
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-06-14 01:07:06 -07:00
|
|
|
get engineId() {
|
2008-06-03 15:14:27 -07:00
|
|
|
let id = ID.get('Engine:' + this.name);
|
2008-06-23 16:23:57 -07:00
|
|
|
if (!id) {
|
|
|
|
// Copy the service login from WeaveID
|
|
|
|
let masterID = ID.get('WeaveID');
|
|
|
|
|
|
|
|
id = new Identity(this.logName, masterID.username, masterID.password);
|
2008-06-01 19:10:11 -07:00
|
|
|
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() {
|
2008-07-25 17:02:43 -07:00
|
|
|
let levelPref = "log.logger.service.engine." + this.name;
|
|
|
|
let level = "Debug";
|
|
|
|
try { level = Utils.prefs.getCharPref(levelPref); }
|
|
|
|
catch (e) { /* ignore unset prefs */ }
|
|
|
|
|
2008-11-03 15:00:38 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger("Service." + this.logName);
|
2008-07-25 17:02:43 -07:00
|
|
|
this._log.level = Log4Moz.Level[level];
|
2007-12-14 18:07:25 -08:00
|
|
|
this._osPrefix = "weave:" + this.name + ":";
|
2007-12-10 21:38:53 -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-06-17 02:03:02 -07:00
|
|
|
yield this._remote.wipe(self.cb);
|
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._store.wipe();
|
|
|
|
this._log.debug("Client reset completed successfully");
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-07-25 01:06:23 -07:00
|
|
|
_sync: function Engine__sync() {
|
|
|
|
let self = yield;
|
|
|
|
throw "_sync needs to be subclassed";
|
|
|
|
},
|
|
|
|
|
|
|
|
_initialUpload: function Engine__initialUpload() {
|
|
|
|
let self = yield;
|
2008-07-25 17:02:43 -07:00
|
|
|
throw "_initialUpload needs to be subclassed";
|
2008-07-25 01:06:23 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_share: function Engine__share(guid, username) {
|
|
|
|
let self = yield;
|
|
|
|
/* 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. */
|
|
|
|
self.done();
|
|
|
|
},
|
|
|
|
|
|
|
|
_stopSharing: function Engine__stopSharing(guid, username) {
|
|
|
|
let self = yield;
|
|
|
|
/* This should be overridden by the engine subclass for each datatype.
|
|
|
|
Stop sharing the data node identified by guid with the user identified
|
|
|
|
by username.*/
|
|
|
|
self.done();
|
|
|
|
},
|
|
|
|
|
|
|
|
sync: function Engine_sync(onComplete) {
|
|
|
|
return this._sync.async(this, onComplete);
|
|
|
|
},
|
|
|
|
|
|
|
|
share: function Engine_share(onComplete, guid, username) {
|
|
|
|
return this._share.async(this, onComplete, guid, username);
|
|
|
|
},
|
|
|
|
|
|
|
|
stopSharing: function Engine_share(onComplete, guid, username) {
|
|
|
|
return this._stopSharing.async(this, onComplete, guid, username);
|
|
|
|
},
|
|
|
|
|
|
|
|
resetServer: function Engimne_resetServer(onComplete) {
|
2008-07-30 21:58:13 -07:00
|
|
|
this._notify("reset-server", "", this._resetServer).async(this, onComplete);
|
2008-07-25 01:06:23 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
resetClient: function Engine_resetClient(onComplete) {
|
2008-07-30 21:58:13 -07:00
|
|
|
this._notify("reset-client", "", this._resetClient).async(this, onComplete);
|
2008-07-25 01:06:23 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function SyncEngine() {}
|
|
|
|
SyncEngine.prototype = {
|
|
|
|
__proto__: new Engine(),
|
|
|
|
|
2008-07-30 22:52:49 -07:00
|
|
|
get _snapshot() {
|
2008-07-25 01:06:23 -07:00
|
|
|
let snap = new SnapshotStore(this.name);
|
2008-07-30 22:52:49 -07:00
|
|
|
this.__defineGetter__("_snapshot", function() snap);
|
2008-07-25 01:06:23 -07:00
|
|
|
return snap;
|
2008-07-25 17:02:43 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_resetClient: function SyncEngine__resetClient() {
|
|
|
|
let self = yield;
|
|
|
|
this._log.debug("Resetting client state");
|
|
|
|
this._snapshot.wipe();
|
|
|
|
this._store.wipe();
|
|
|
|
this._log.debug("Client reset completed successfully");
|
|
|
|
},
|
|
|
|
|
|
|
|
_initialUpload: function Engine__initialUpload() {
|
|
|
|
let self = yield;
|
|
|
|
this._log.info("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
|
|
|
|
yield this._remote.initialize(self.cb, this._snapshot);
|
|
|
|
this._snapshot.save();
|
|
|
|
},
|
2008-07-25 01:06:23 -07:00
|
|
|
|
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-07-25 17:02:43 -07:00
|
|
|
_sync: function SyncEngine__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");
|
2008-07-09 17:17:24 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:sync:engine:start", this.displayName);
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-07-25 17:02:43 -07:00
|
|
|
this._snapshot.load();
|
|
|
|
|
2008-06-15 08:21:44 -07:00
|
|
|
try {
|
2008-07-31 00:20:55 -07:00
|
|
|
this._remote.status.data; // FIXME - otherwise we get an error...
|
2008-07-25 01:06:23 -07:00
|
|
|
yield this._remote.openSession(self.cb, this._snapshot);
|
2008-07-31 00:20:55 -07:00
|
|
|
|
2008-06-25 04:43:07 -07:00
|
|
|
} catch (e if e.status == 404) {
|
2008-06-15 08:21:44 -07:00
|
|
|
yield this._initialUpload.async(this, self.cb);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
// 1) Fetch server deltas
|
2008-07-09 17:17:24 -07:00
|
|
|
|
|
|
|
this._os.notifyObservers(null, "weave:service:sync:status", "status.downloading-deltas");
|
2008-08-08 14:42:57 -07:00
|
|
|
let serverSnap = yield this._remote.wrap(self.cb);
|
|
|
|
let serverUpdates = yield this._core.detectUpdates(self.cb,
|
|
|
|
this._snapshot.data, serverSnap);
|
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-07-09 17:17:24 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:sync:status", "status.calculating-differences");
|
2008-08-08 14:42:57 -07:00
|
|
|
let localSnap = new SnapshotStore();
|
|
|
|
localSnap.data = this._store.wrap();
|
|
|
|
this._core.detectUpdates(self.cb, this._snapshot.data, localSnap.data);
|
2008-03-19 15:17:04 -07:00
|
|
|
let localUpdates = yield;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
this._log.trace("local json:\n" + localSnap.serialize());
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.trace("Local updates: " + this._serializeCommands(localUpdates));
|
2008-08-08 14:42:57 -07:00
|
|
|
this._log.trace("Server updates: " + this._serializeCommands(serverUpdates));
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
if (serverUpdates.length == 0 && localUpdates.length == 0) {
|
2008-07-09 17:17:24 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:sync:status", "status.no-changes-required");
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.info("Sync complete: no changes needed on client or server");
|
2008-08-08 14:42:57 -07:00
|
|
|
this._snapshot.version = this._remote.status.data.maxVersion;
|
|
|
|
this._snapshot.save();
|
2008-03-19 15:17:04 -07:00
|
|
|
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.
|
|
|
|
|
2008-07-09 17:17:24 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:sync:status", "status.reconciling-updates");
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.info("Reconciling client/server updates");
|
2008-08-08 14:42:57 -07:00
|
|
|
let ret = yield this._core.reconcile(self.cb, localUpdates, serverUpdates);
|
2008-03-19 15:17:04 -07:00
|
|
|
|
|
|
|
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)) {
|
2008-07-09 17:17:24 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:sync:status", "status.no-changes-required");
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.info("Sync complete: no changes needed on client or server");
|
2008-08-08 14:42:57 -07:00
|
|
|
this._snapshot.data = localSnap.data;
|
2008-06-16 09:04:23 -07:00
|
|
|
this._snapshot.version = this._remote.status.data.maxVersion;
|
2008-03-19 15:17:04 -07:00
|
|
|
this._snapshot.save();
|
|
|
|
self.done(true);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
if (clientConflicts.length || serverConflicts.length)
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.warn("Conflicts found! Discarding server changes");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
// 3.1) Apply server changes to local store
|
2008-07-09 17:17:24 -07:00
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
if (clientChanges.length) {
|
|
|
|
this._log.info("Applying changes locally");
|
2008-07-09 17:17:24 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:sync:status", "status.applying-changes");
|
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
// apply to real store
|
|
|
|
yield this._store.applyCommands.async(this._store, self.cb, clientChanges);
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
// get the current state
|
|
|
|
let newSnap = new SnapshotStore();
|
|
|
|
newSnap.data = this._store.wrap();
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
// apply to the snapshot we got in step 1, and compare with current state
|
|
|
|
yield localSnap.applyCommands.async(localSnap, self.cb, clientChanges);
|
|
|
|
let diff = yield this._core.detectUpdates(self.cb,
|
|
|
|
localSnap.data, newSnap.data);
|
2008-03-19 15:17:04 -07:00
|
|
|
if (diff.length != 0) {
|
|
|
|
this._log.warn("Commands did not apply correctly");
|
2008-06-27 14:21:54 -07:00
|
|
|
this._log.trace("Diff from snapshot+commands -> " +
|
2008-03-19 15:17:04 -07:00
|
|
|
"new snapshot after commands:\n" +
|
|
|
|
this._serializeCommands(diff));
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2008-08-08 14:42:57 -07:00
|
|
|
|
|
|
|
// update the local snap to the current state, we'll use it below
|
|
|
|
localSnap.data = newSnap.data;
|
|
|
|
localSnap.version = this._remote.status.data.maxVersion;
|
2008-03-19 15:17:04 -07:00
|
|
|
}
|
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-08-08 14:42:57 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:sync:status",
|
|
|
|
"status.calculating-differences");
|
|
|
|
let serverDelta = yield this._core.detectUpdates(self.cb,
|
|
|
|
serverSnap, localSnap.data);
|
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);
|
2008-06-27 14:21:54 -07:00
|
|
|
this._log.trace("Actual changes for server: " +
|
2008-03-19 15:17:04 -07:00
|
|
|
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");
|
2008-08-08 14:42:57 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:sync:status",
|
|
|
|
"status.uploading-deltas");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
yield this._remote.appendDelta(self.cb, localSnap, serverDelta,
|
|
|
|
{maxVersion: this._snapshot.version,
|
|
|
|
deltasEncryption: Crypto.defaultAlgorithm});
|
|
|
|
localSnap.version = this._remote.status.data.maxVersion;
|
2008-06-10 19:12:04 -07:00
|
|
|
|
|
|
|
this._log.info("Successfully updated deltas and status on server");
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2008-03-19 15:17:04 -07:00
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
this._snapshot.data = localSnap.data;
|
|
|
|
this._snapshot.version = localSnap.version;
|
|
|
|
this._snapshot.save();
|
|
|
|
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.info("Sync complete");
|
|
|
|
self.done(true);
|
2008-07-25 01:06:23 -07:00
|
|
|
}
|
|
|
|
};
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
function BlobEngine() {
|
2008-07-25 17:02:43 -07:00
|
|
|
// subclasses should call _init
|
|
|
|
// we don't call it here because it requires serverPrefix to be set
|
|
|
|
}
|
2008-08-08 14:42:57 -07:00
|
|
|
BlobEngine.prototype = {
|
2008-07-25 01:06:23 -07:00
|
|
|
__proto__: new Engine(),
|
2008-06-14 01:07:06 -07:00
|
|
|
|
2008-07-25 17:02:43 -07:00
|
|
|
get _profileID() {
|
2008-07-29 18:38:58 -07:00
|
|
|
return ClientData.GUID;
|
2008-07-25 17:02:43 -07:00
|
|
|
},
|
2008-07-25 01:06:23 -07:00
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
_init: function BlobEngine__init() {
|
2008-07-25 17:02:43 -07:00
|
|
|
// FIXME meep?
|
|
|
|
this.__proto__.__proto__.__proto__.__proto__._init.call(this);
|
2008-07-29 11:03:06 -07:00
|
|
|
this._keys = new Keychain(this.serverPrefix);
|
2008-07-25 17:02:43 -07:00
|
|
|
this._file = new Resource(this.serverPrefix + "data");
|
|
|
|
this._file.pushFilter(new JsonFilter());
|
2008-07-29 11:03:06 -07:00
|
|
|
this._file.pushFilter(new CryptoFilter(this.engineId));
|
|
|
|
},
|
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
_initialUpload: function BlobEngine__initialUpload() {
|
2008-07-29 11:03:06 -07:00
|
|
|
let self = yield;
|
2008-08-08 14:42:57 -07:00
|
|
|
this._log.info("Initial upload to server");
|
2008-07-29 12:04:41 -07:00
|
|
|
yield this._keys.initialize(self.cb, this.engineId);
|
2008-07-29 11:03:06 -07:00
|
|
|
this._file.data = {};
|
|
|
|
yield this._merge.async(this, self.cb);
|
2008-07-29 17:35:10 -07:00
|
|
|
yield this._file.put(self.cb);
|
2008-07-25 17:02:43 -07:00
|
|
|
},
|
2008-07-25 01:06:23 -07:00
|
|
|
|
2008-07-25 17:02:43 -07:00
|
|
|
// NOTE: Assumes this._file has latest server data
|
|
|
|
// this method is separate from _sync so it's easy to override in subclasses
|
2008-08-08 14:42:57 -07:00
|
|
|
_merge: function BlobEngine__merge() {
|
2008-07-25 17:02:43 -07:00
|
|
|
let self = yield;
|
|
|
|
this._file.data[this._profileID] = this._store.wrap();
|
|
|
|
},
|
2008-07-25 01:06:23 -07:00
|
|
|
|
2008-07-25 17:02:43 -07:00
|
|
|
// This engine is very simple:
|
|
|
|
// 1) Get the latest server data
|
|
|
|
// 2) Merge with our local data store
|
|
|
|
// 3) Upload new merged data
|
|
|
|
// NOTE: a version file will be needed in the future to optimize the case
|
|
|
|
// where there are no changes
|
2008-08-08 14:42:57 -07:00
|
|
|
_sync: function BlobEngine__sync() {
|
2008-07-25 17:02:43 -07:00
|
|
|
let self = yield;
|
2008-07-25 01:06:23 -07:00
|
|
|
|
2008-07-25 17:02:43 -07:00
|
|
|
this._log.info("Beginning sync");
|
2008-07-30 21:58:13 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:sync:engine:start", this.name);
|
2008-07-25 01:06:23 -07:00
|
|
|
|
2008-11-03 15:00:38 -08:00
|
|
|
// FIXME
|
|
|
|
//if (!(yield DAV.MKCOL(this.serverPrefix, self.cb)))
|
|
|
|
// throw "Could not create remote folder";
|
2008-07-25 01:06:23 -07:00
|
|
|
|
2008-07-25 17:02:43 -07:00
|
|
|
try {
|
2008-07-29 12:04:41 -07:00
|
|
|
if ("none" != Utils.prefs.getCharPref("encryption"))
|
|
|
|
yield this._keys.getKeyAndIV(self.cb, this.engineId);
|
2008-07-25 17:02:43 -07:00
|
|
|
yield this._file.get(self.cb);
|
2008-07-29 11:03:06 -07:00
|
|
|
yield this._merge.async(this, self.cb);
|
2008-07-29 17:35:10 -07:00
|
|
|
yield this._file.put(self.cb);
|
2008-07-29 12:04:41 -07:00
|
|
|
|
2008-07-25 17:02:43 -07:00
|
|
|
} catch (e if e.status == 404) {
|
2008-07-29 12:04:41 -07:00
|
|
|
yield this._initialUpload.async(this, self.cb);
|
2008-07-25 01:06:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this._log.info("Sync complete");
|
2008-08-08 14:42:57 -07:00
|
|
|
this._os.notifyObservers(null, "weave:service:sync:engine:success", this.name);
|
2008-07-25 01:06:23 -07:00
|
|
|
self.done(true);
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2008-07-30 21:58:13 -07:00
|
|
|
};
|
2008-08-08 14:42:57 -07:00
|
|
|
|
|
|
|
function HeuristicEngine() {
|
|
|
|
}
|
|
|
|
HeuristicEngine.prototype = {
|
|
|
|
__proto__: new Engine(),
|
|
|
|
|
|
|
|
get _snapshot() {
|
|
|
|
let snap = new SnapshotStore(this.name);
|
|
|
|
this.__defineGetter__("_snapshot", function() snap);
|
|
|
|
return snap;
|
|
|
|
},
|
|
|
|
|
|
|
|
_resetClient: function SyncEngine__resetClient() {
|
|
|
|
let self = yield;
|
|
|
|
this._log.debug("Resetting client state");
|
|
|
|
this._snapshot.wipe();
|
|
|
|
this._store.wipe();
|
|
|
|
this._log.debug("Client reset completed successfully");
|
|
|
|
},
|
|
|
|
|
|
|
|
_initialUpload: function HeuristicEngine__initialUpload() {
|
|
|
|
let self = yield;
|
|
|
|
this._log.info("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
|
|
|
|
yield this._remote.initialize(self.cb, this._snapshot);
|
|
|
|
this._snapshot.save();
|
|
|
|
},
|
|
|
|
|
|
|
|
_sync: function HeuristicEngine__sync() {
|
|
|
|
let self = yield;
|
|
|
|
}
|
|
|
|
};
|