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-11-06 19:18:07 -08:00
|
|
|
const EXPORTED_SYMBOLS = ['Engines', 'NewEngine', '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-11-08 02:00:33 -08:00
|
|
|
Cu.import("resource://weave/base_records/wbo.js");
|
2008-11-19 16:20:25 -08:00
|
|
|
Cu.import("resource://weave/base_records/keys.js");
|
2008-11-08 02:00:33 -08:00
|
|
|
Cu.import("resource://weave/base_records/crypto.js");
|
2008-11-19 16:20:25 -08:00
|
|
|
Cu.import("resource://weave/base_records/collection.js");
|
2008-11-08 02:00:33 -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];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
function Engine() { /* subclasses should call this._init() */}
|
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-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-12-05 00:39:54 -08:00
|
|
|
get score() this._tracker.score,
|
|
|
|
|
|
|
|
// _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-12-05 00:39:54 -08:00
|
|
|
let tracker = new Tracker();
|
2008-07-25 01:06:23 -07:00
|
|
|
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 + ":";
|
2008-12-05 00:39:54 -08:00
|
|
|
|
|
|
|
this._tracker; // initialize tracker to load previously changed IDs
|
|
|
|
|
|
|
|
this._log.debug("Engine initialized");
|
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-11-06 19:18:07 -08:00
|
|
|
throw "_resetServer needs to be subclassed";
|
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";
|
|
|
|
},
|
|
|
|
|
|
|
|
_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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
function SyncEngine() { /* subclasses should call this._init() */ }
|
|
|
|
SyncEngine.prototype = {
|
2008-11-06 19:18:07 -08:00
|
|
|
__proto__: Engine.prototype,
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
get baseURL() {
|
|
|
|
let url = Utils.prefs.getCharPref("serverURL");
|
|
|
|
if (url && url[url.length-1] != '/')
|
|
|
|
url = url + '/';
|
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
|
|
|
get engineURL() {
|
|
|
|
return this.baseURL + ID.get('WeaveID').username + '/' + this.name + '/';
|
|
|
|
},
|
|
|
|
|
|
|
|
get cryptoMetaURL() {
|
|
|
|
return this.baseURL + ID.get('WeaveID').username + '/crypto/' + this.name;
|
2008-11-19 16:20:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
get lastSync() {
|
|
|
|
try {
|
|
|
|
return Utils.prefs.getCharPref(this.name + ".lastSync");
|
|
|
|
} catch (e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
},
|
2008-11-06 23:23:35 -08:00
|
|
|
set lastSync(value) {
|
|
|
|
Utils.prefs.setCharPref(this.name + ".lastSync", value);
|
|
|
|
},
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// XXX these two should perhaps just be a variable inside sync(), but we have
|
|
|
|
// one or two other methods that use it
|
|
|
|
|
2008-11-06 23:23:35 -08:00
|
|
|
get incoming() {
|
|
|
|
if (!this._incoming)
|
|
|
|
this._incoming = [];
|
|
|
|
return this._incoming;
|
|
|
|
},
|
|
|
|
|
|
|
|
get outgoing() {
|
|
|
|
if (!this._outgoing)
|
|
|
|
this._outgoing = [];
|
|
|
|
return this._outgoing;
|
|
|
|
},
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// Create a new record starting from an ID
|
|
|
|
// Calls _serializeItem to get the actual item, but sometimes needs to be
|
|
|
|
// overridden anyway (to alter parentid or other properties outside the payload)
|
|
|
|
_createRecord: function SyncEngine__newCryptoWrapper(id, encrypt) {
|
|
|
|
let self = yield;
|
2008-11-19 16:20:25 -08:00
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
let record = new CryptoWrapper();
|
|
|
|
record.uri = this.engineURL + id;
|
|
|
|
record.encryption = this.cryptoMetaURL;
|
|
|
|
record.cleartext = yield this._serializeItem.async(this, self.cb, id);
|
|
|
|
|
2008-12-05 03:36:27 -08:00
|
|
|
if (record.cleartext && record.cleartext.parentid)
|
2008-12-05 00:39:54 -08:00
|
|
|
record.parentid = record.cleartext.parentid;
|
|
|
|
|
|
|
|
if (encrypt || encrypt == undefined)
|
|
|
|
yield record.encrypt(self.cb, ID.get('WeaveCryptoID').password);
|
|
|
|
|
|
|
|
self.done(record);
|
2008-11-19 16:20:25 -08:00
|
|
|
},
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// Serialize an item. This will become the encrypted field in the payload of
|
|
|
|
// a record. Needs to be overridden in a subclass
|
|
|
|
_serializeItem: function SyncEngine__serializeItem(id) {
|
|
|
|
let self = yield;
|
|
|
|
self.done({});
|
2008-11-19 16:20:25 -08:00
|
|
|
},
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
_getAllIDs: function SyncEngine__getAllIDs() {
|
2008-11-06 19:18:07 -08:00
|
|
|
let self = yield;
|
2008-12-05 00:39:54 -08:00
|
|
|
self.done({});
|
|
|
|
},
|
2008-11-06 23:23:35 -08:00
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// Check if a record is "like" another one, even though the IDs are different,
|
|
|
|
// in that case, we'll change the ID of the local item to match
|
|
|
|
// Probably needs to be overridden in a subclass, to change which criteria
|
|
|
|
// make two records "the same one"
|
|
|
|
_recordLike: function SyncEngine__recordLike(a, b) {
|
|
|
|
if (a.parentid != b.parentid)
|
|
|
|
return false;
|
|
|
|
return Utils.deepEquals(a.cleartext, b.cleartext);
|
2008-11-19 16:20:25 -08:00
|
|
|
},
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
_changeRecordRefs: function SyncEngine__changeRecordRefs(oldID, newID) {
|
2008-11-19 16:20:25 -08:00
|
|
|
let self = yield;
|
2008-12-05 00:39:54 -08:00
|
|
|
for each (let rec in this.outgoing) {
|
|
|
|
if (rec.parentid == oldID)
|
|
|
|
rec.parentid = newID;
|
|
|
|
}
|
|
|
|
},
|
2008-11-06 23:23:35 -08:00
|
|
|
|
2008-12-05 00:55:19 -08:00
|
|
|
_changeItemID: function SyncEngine__changeItemID(oldID, newID) {
|
2008-12-05 00:39:54 -08:00
|
|
|
let self = yield;
|
|
|
|
throw "_changeRecordID must be overridden in a subclass";
|
2008-11-19 16:20:25 -08:00
|
|
|
},
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
_recDepth: function SyncEngine__recDepth(rec) {
|
2008-11-24 23:37:19 -08:00
|
|
|
// we've calculated depth for this record already
|
2008-11-24 23:12:00 -08:00
|
|
|
if (rec.depth)
|
|
|
|
return rec.depth;
|
2008-11-24 23:37:19 -08:00
|
|
|
|
|
|
|
// record has no parent
|
2008-11-24 23:12:00 -08:00
|
|
|
if (!rec.parentid)
|
|
|
|
return 0;
|
2008-11-24 23:37:19 -08:00
|
|
|
|
|
|
|
// search for the record's parent and calculate its depth, then add one
|
2008-11-24 23:12:00 -08:00
|
|
|
for each (let inc in this.incoming) {
|
|
|
|
if (inc.id == rec.parentid) {
|
|
|
|
rec.depth = this._recDepth(inc) + 1;
|
|
|
|
return rec.depth;
|
|
|
|
}
|
|
|
|
}
|
2008-11-24 23:37:19 -08:00
|
|
|
|
|
|
|
// we couldn't find the record's parent, so it's an orphan
|
2008-11-24 23:12:00 -08:00
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// Any setup that needs to happen at the beginning of each sync.
|
|
|
|
// Makes sure crypto records and keys are all set-up
|
|
|
|
_syncStartup: function SyncEngine__syncStartup() {
|
2008-11-26 07:25:28 -08:00
|
|
|
let self = yield;
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
this._log.debug("Ensuring server crypto records are there");
|
|
|
|
|
|
|
|
let meta = yield CryptoMetas.get(self.cb, this.cryptoMetaURL);
|
|
|
|
if (!meta) {
|
|
|
|
let cryptoSvc = Cc["@labs.mozilla.com/Weave/Crypto;1"].
|
|
|
|
getService(Ci.IWeaveCrypto);
|
|
|
|
let symkey = cryptoSvc.generateRandomKey();
|
|
|
|
let pubkey = yield PubKeys.getDefaultKey(self.cb);
|
|
|
|
meta = new CryptoMeta(this.cryptoMetaURL);
|
|
|
|
meta.generateIV();
|
|
|
|
yield meta.addUnwrappedKey(self.cb, pubkey, symkey);
|
|
|
|
yield meta.put(self.cb);
|
|
|
|
}
|
2008-12-05 00:58:25 -08:00
|
|
|
this._tracker.disable();
|
2008-11-26 07:25:28 -08:00
|
|
|
},
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// Generate outgoing records
|
|
|
|
_generateOutgoing: function SyncEngine__generateOutgoing() {
|
2008-11-19 16:20:25 -08:00
|
|
|
let self = yield;
|
|
|
|
|
2008-11-24 08:48:38 -08:00
|
|
|
this._log.debug("Calculating client changes");
|
2008-11-24 08:04:14 -08:00
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// first sync special case: upload all items
|
2008-12-06 00:08:54 -08:00
|
|
|
// note that we use a backdoor (of sorts) to the tracker and it
|
|
|
|
// won't save to disk this list
|
2008-11-19 16:20:25 -08:00
|
|
|
if (!this.lastSync) {
|
2008-12-05 00:39:54 -08:00
|
|
|
this._log.info("First sync, uploading all items");
|
2008-12-06 00:08:54 -08:00
|
|
|
|
|
|
|
// remove any old ones first
|
|
|
|
this._tracker.clearChangedIDs();
|
|
|
|
|
|
|
|
// now add all current ones
|
2008-12-05 00:39:54 -08:00
|
|
|
let all = yield this._getAllIDs.async(this, self.cb);
|
2008-12-06 00:08:54 -08:00
|
|
|
for (let id in all) {
|
|
|
|
this._tracker.changedIDs[id] = true;
|
2008-12-05 03:28:17 -08:00
|
|
|
}
|
2008-11-08 02:00:33 -08:00
|
|
|
}
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// generate queue from changed items list
|
|
|
|
// NOTE we want changed items -> outgoing -> server to be as atomic as
|
|
|
|
// possible, so we clear the changed IDs after we upload the changed records
|
|
|
|
// NOTE2 don't encrypt, we'll do that before uploading instead
|
|
|
|
for (let id in this._tracker.changedIDs) {
|
|
|
|
this.outgoing.push(yield this._createRecord.async(this, self.cb, id, false));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Generate outgoing records
|
|
|
|
_fetchIncoming: function SyncEngine__fetchIncoming() {
|
|
|
|
let self = yield;
|
|
|
|
|
2008-11-24 08:48:38 -08:00
|
|
|
this._log.debug("Downloading server changes");
|
2008-11-24 08:04:14 -08:00
|
|
|
|
2008-11-19 16:20:25 -08:00
|
|
|
let newitems = new Collection(this.engineURL);
|
|
|
|
newitems.modified = this.lastSync;
|
|
|
|
newitems.full = true;
|
|
|
|
yield newitems.get(self.cb);
|
|
|
|
|
|
|
|
let item;
|
2008-11-24 21:48:22 -08:00
|
|
|
while ((item = yield newitems.iter.next(self.cb))) {
|
2008-11-24 08:04:14 -08:00
|
|
|
this.incoming.push(item);
|
2008-11-19 16:20:25 -08:00
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Process incoming records to get them ready for reconciliation and applying later
|
|
|
|
// i.e., decrypt them, and sort them
|
|
|
|
_processIncoming: function SyncEngine__processIncoming() {
|
|
|
|
let self = yield;
|
|
|
|
|
|
|
|
this._log.debug("Decrypting and sorting incoming changes");
|
2008-11-08 02:00:33 -08:00
|
|
|
|
2008-11-24 23:12:00 -08:00
|
|
|
for each (let inc in this.incoming) {
|
2008-11-25 21:07:18 -08:00
|
|
|
yield inc.decrypt(self.cb, ID.get('WeaveCryptoID').password);
|
|
|
|
this._recDepth(inc); // note: doesn't need access to payload
|
2008-11-24 23:12:00 -08:00
|
|
|
}
|
|
|
|
this.incoming.sort(function(a, b) {
|
|
|
|
if ((typeof(a.depth) == "number" && typeof(b.depth) == "undefined") ||
|
|
|
|
(typeof(a.depth) == "number" && b.depth == null) ||
|
|
|
|
(a.depth > b.depth))
|
|
|
|
return 1;
|
|
|
|
if ((typeof(a.depth) == "undefined" && typeof(b.depth) == "number") ||
|
|
|
|
(a.depth == null && typeof(b.depth) == "number") ||
|
|
|
|
(a.depth < b.depth))
|
|
|
|
return -1;
|
2008-12-05 00:39:54 -08:00
|
|
|
if (a.cleartext && b.cleartext) {
|
|
|
|
if (a.cleartext.index > b.cleartext.index)
|
|
|
|
return 1;
|
|
|
|
if (a.cleartext.index < b.cleartext.index)
|
|
|
|
return -1;
|
|
|
|
}
|
2008-11-24 23:12:00 -08:00
|
|
|
return 0;
|
|
|
|
});
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Reconciliation has two steps:
|
|
|
|
// 1) Check for the same item (same ID) on both the incoming and outgoing
|
|
|
|
// queues. This means the same item was modified on this profile and another
|
|
|
|
// at the same time. In this case, this client wins (which really means, the
|
|
|
|
// last profile you sync wins).
|
|
|
|
// 2) Check if any incoming & outgoing items are actually the same, even
|
|
|
|
// though they have different IDs. This happens when the same item is added
|
|
|
|
// on two different machines at the same time. For example, when a profile
|
|
|
|
// is synced for the first time after having (manually or otherwise) imported
|
|
|
|
// bookmarks imported, every bookmark will match this condition.
|
|
|
|
// When two items with different IDs are "the same" we change the local ID to
|
|
|
|
// match the remote one.
|
|
|
|
_reconcile: function SyncEngine__reconcile() {
|
|
|
|
let self = yield;
|
2008-11-24 08:04:14 -08:00
|
|
|
|
2008-11-24 08:48:38 -08:00
|
|
|
this._log.debug("Reconciling server/client changes");
|
2008-11-24 08:04:14 -08:00
|
|
|
|
2008-12-05 03:28:17 -08:00
|
|
|
this._log.debug(this.incoming.length + " items coming in, " +
|
|
|
|
this.outgoing.length + " items going out");
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// Check for the same item (same ID) on both incoming & outgoing queues
|
2008-11-19 16:20:25 -08:00
|
|
|
let conflicts = [];
|
|
|
|
for (let i = 0; i < this.incoming.length; i++) {
|
2008-12-05 03:28:17 -08:00
|
|
|
for (let o = 0; o < this.outgoing.length; o++) {
|
|
|
|
if (this.incoming[i].id == this.outgoing[o].id) {
|
|
|
|
// Only consider it a conflict if there are actual differences
|
|
|
|
// otherwise, just remove the outgoing record as well
|
|
|
|
if (!Utils.deepEquals(this.incoming[i].cleartext,
|
|
|
|
this.outgoing[o].cleartext))
|
|
|
|
conflicts.push({in: this.incoming[i], out: this.outgoing[o]});
|
|
|
|
else
|
|
|
|
delete this.outgoing[o];
|
2008-11-19 16:20:25 -08:00
|
|
|
delete this.incoming[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-05 03:28:17 -08:00
|
|
|
this._outgoing = this.outgoing.filter(function(n) n); // removes any holes
|
2008-11-19 16:20:25 -08:00
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
this._incoming = this.incoming.filter(function(n) n); // removes any holes
|
2008-11-19 16:20:25 -08:00
|
|
|
if (conflicts.length)
|
2008-11-26 07:25:28 -08:00
|
|
|
this._log.debug("Conflicts found. Conflicting server changes discarded");
|
2008-11-19 16:20:25 -08:00
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// Check for items with different IDs which we think are the same one
|
2008-11-26 07:25:28 -08:00
|
|
|
for (let i = 0; i < this.incoming.length; i++) {
|
|
|
|
for (let o = 0; o < this.outgoing.length; o++) {
|
|
|
|
if (this._recordLike(this.incoming[i], this.outgoing[o])) {
|
|
|
|
// change refs in outgoing queue
|
|
|
|
yield this._changeRecordRefs.async(this, self.cb,
|
|
|
|
this.outgoing[o].id,
|
|
|
|
this.incoming[i].id);
|
|
|
|
// change actual id of item
|
2008-12-05 00:39:54 -08:00
|
|
|
yield this._changeItemID.async(this, self.cb,
|
|
|
|
this.outgoing[o].id,
|
|
|
|
this.incoming[i].id);
|
2008-11-26 07:25:28 -08:00
|
|
|
delete this.incoming[i];
|
|
|
|
delete this.outgoing[o];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
this._outgoing = this.outgoing.filter(function(n) n); // removes any holes
|
2008-11-26 07:25:28 -08:00
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
this._incoming = this.incoming.filter(function(n) n); // removes any holes
|
2008-12-05 03:28:17 -08:00
|
|
|
|
|
|
|
this._log.debug("Reconciliation complete");
|
|
|
|
this._log.debug(this.incoming.length + " items coming in, " +
|
|
|
|
this.outgoing.length + " items going out");
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
2008-11-24 08:04:14 -08:00
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// Apply incoming records
|
|
|
|
_applyIncoming: function SyncEngine__applyIncoming() {
|
|
|
|
let self = yield;
|
2008-11-26 07:25:28 -08:00
|
|
|
if (this.incoming.length) {
|
|
|
|
this._log.debug("Applying server changes");
|
|
|
|
let inc;
|
|
|
|
while ((inc = this.incoming.shift())) {
|
|
|
|
yield this._store.applyIncoming(self.cb, inc);
|
|
|
|
if (inc.modified > this.lastSync)
|
|
|
|
this.lastSync = inc.modified;
|
|
|
|
}
|
2008-11-19 16:20:25 -08:00
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
2008-11-06 23:23:35 -08:00
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// Upload outgoing records
|
|
|
|
_uploadOutgoing: function SyncEngine__uploadOutgoing() {
|
|
|
|
let self = yield;
|
2008-11-24 08:04:14 -08:00
|
|
|
if (this.outgoing.length) {
|
2008-11-26 07:25:28 -08:00
|
|
|
this._log.debug("Uploading client changes");
|
2008-11-24 08:04:14 -08:00
|
|
|
let up = new Collection(this.engineURL);
|
|
|
|
let out;
|
|
|
|
while ((out = this.outgoing.pop())) {
|
2008-12-05 00:39:54 -08:00
|
|
|
yield out.encrypt(self.cb, ID.get('WeaveCryptoID').password);
|
2008-11-24 08:04:14 -08:00
|
|
|
yield up.pushRecord(self.cb, out);
|
|
|
|
}
|
|
|
|
yield up.post(self.cb);
|
2008-11-24 21:48:22 -08:00
|
|
|
if (up.data.modified > this.lastSync)
|
|
|
|
this.lastSync = up.data.modified;
|
2008-11-08 02:00:33 -08:00
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
this._tracker.clearChangedIDs();
|
2008-07-25 17:02:43 -07:00
|
|
|
},
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// Any cleanup necessary.
|
|
|
|
// Save the current snapshot so as to calculate changes at next sync
|
2008-12-05 03:28:17 -08:00
|
|
|
_syncFinish: function SyncEngine__syncFinish(error) {
|
2008-11-06 19:18:07 -08:00
|
|
|
let self = yield;
|
2008-12-05 00:39:54 -08:00
|
|
|
this._log.debug("Finishing up sync");
|
|
|
|
this._tracker.resetScore();
|
2008-12-05 00:55:19 -08:00
|
|
|
this._tracker.enable();
|
2008-11-06 19:18:07 -08:00
|
|
|
},
|
|
|
|
|
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-12-05 00:55:19 -08:00
|
|
|
try {
|
|
|
|
yield this._syncStartup.async(this, self.cb);
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-12-05 00:55:19 -08:00
|
|
|
// Populate incoming and outgoing queues
|
|
|
|
yield this._generateOutgoing.async(this, self.cb);
|
|
|
|
yield this._fetchIncoming.async(this, self.cb);
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-12-05 00:55:19 -08:00
|
|
|
// Decrypt and sort incoming records, then reconcile
|
|
|
|
yield this._processIncoming.async(this, self.cb);
|
|
|
|
yield this._reconcile.async(this, self.cb);
|
2008-03-19 15:17:04 -07:00
|
|
|
|
2008-12-05 00:55:19 -08:00
|
|
|
// Apply incoming records, upload outgoing records
|
|
|
|
yield this._applyIncoming.async(this, self.cb);
|
|
|
|
yield this._uploadOutgoing.async(this, self.cb);
|
2008-12-05 03:28:17 -08:00
|
|
|
|
|
|
|
yield this._syncFinish.async(this, self.cb);
|
2008-12-05 00:55:19 -08:00
|
|
|
}
|
|
|
|
catch (e) {
|
2008-12-05 03:28:17 -08:00
|
|
|
this._log.warn("Sync failed");
|
2008-12-05 00:55:19 -08:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
finally {
|
2008-12-05 03:28:17 -08:00
|
|
|
this._tracker.enable();
|
2008-12-05 00:55:19 -08:00
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
2008-08-08 14:42:57 -07:00
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
_resetServer: function SyncEngine__resetServer() {
|
|
|
|
let self = yield;
|
|
|
|
let all = new Resource(this.engineURL);
|
|
|
|
yield all.delete(self.cb);
|
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-11-06 23:23:35 -08:00
|
|
|
__proto__: Engine.prototype,
|
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(),
|
|
|
|
|
2008-11-06 19:18:07 -08:00
|
|
|
get _remote() {
|
|
|
|
let remote = new RemoteStore(this);
|
|
|
|
this.__defineGetter__("_remote", function() remote);
|
|
|
|
return remote;
|
|
|
|
},
|
|
|
|
|
2008-08-08 14:42:57 -07:00
|
|
|
get _snapshot() {
|
|
|
|
let snap = new SnapshotStore(this.name);
|
|
|
|
this.__defineGetter__("_snapshot", function() snap);
|
|
|
|
return snap;
|
|
|
|
},
|
|
|
|
|
2008-11-06 19:18:07 -08:00
|
|
|
_resetServer: function SyncEngine__resetServer() {
|
|
|
|
let self = yield;
|
|
|
|
yield this._remote.wipe(self.cb);
|
|
|
|
},
|
|
|
|
|
2008-08-08 14:42:57 -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 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;
|
|
|
|
}
|
|
|
|
};
|