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 ***** */
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
const EXPORTED_SYMBOLS = ['Engines', 'Engine', 'SyncEngine'];
|
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");
|
2009-02-22 00:04:58 -08:00
|
|
|
Cu.import("resource://weave/ext/Observers.js");
|
2009-07-14 14:01:26 -07:00
|
|
|
Cu.import("resource://weave/ext/Sync.js");
|
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-11-03 15:00:38 -08:00
|
|
|
Cu.import("resource://weave/resource.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");
|
2008-05-22 15:58:29 -07:00
|
|
|
Cu.import("resource://weave/trackers.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-04-14 18:53:35 -07:00
|
|
|
// Singleton service, holds registered engines
|
|
|
|
|
|
|
|
Utils.lazy(this, 'Engines', EngineManagerSvc);
|
|
|
|
|
|
|
|
function EngineManagerSvc() {
|
|
|
|
this._engines = {};
|
2009-03-20 00:13:16 -07:00
|
|
|
this._log = Log4Moz.repository.getLogger("Service.Engines");
|
|
|
|
this._log.level = Log4Moz.Level[Svc.Prefs.get(
|
|
|
|
"log.logger.service.engines", "Debug")];
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2008-04-14 18:53:35 -07:00
|
|
|
EngineManagerSvc.prototype = {
|
|
|
|
get: function EngMgr_get(name) {
|
2009-03-20 00:13:16 -07:00
|
|
|
// Return an array of engines if we have an array of names
|
2009-04-07 14:45:29 -07:00
|
|
|
if (Utils.isArray(name)) {
|
2009-03-20 00:13:16 -07:00
|
|
|
let engines = [];
|
|
|
|
name.forEach(function(name) {
|
|
|
|
let engine = this.get(name);
|
|
|
|
if (engine)
|
|
|
|
engines.push(engine);
|
|
|
|
}, this);
|
|
|
|
return engines;
|
|
|
|
}
|
|
|
|
|
|
|
|
let engine = this._engines[name];
|
|
|
|
if (!engine)
|
|
|
|
this._log.debug("Could not get engine: " + name);
|
|
|
|
return engine;
|
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;
|
|
|
|
},
|
2009-04-07 14:45:41 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register an Engine to the service. Alternatively, give an array of engine
|
|
|
|
* objects to register.
|
|
|
|
*
|
|
|
|
* @param engineObject
|
|
|
|
* Engine object used to get an instance of the engine
|
|
|
|
* @return The engine object if anything failed
|
|
|
|
*/
|
|
|
|
register: function EngMgr_register(engineObject) {
|
|
|
|
if (Utils.isArray(engineObject))
|
|
|
|
return engineObject.map(this.register, this);
|
|
|
|
|
|
|
|
try {
|
2009-04-08 12:39:14 -07:00
|
|
|
let name = engineObject.prototype.name;
|
|
|
|
if (name in this._engines)
|
|
|
|
this._log.error("Engine '" + name + "' is already registered!");
|
|
|
|
else
|
|
|
|
this._engines[name] = new engineObject();
|
2009-04-07 14:45:41 -07:00
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
let mesg = ex.message ? ex.message : ex;
|
|
|
|
let name = engineObject || "";
|
|
|
|
name = name.prototype || "";
|
|
|
|
name = name.name || "";
|
|
|
|
|
|
|
|
let out = "Could not initialize engine '" + name + "': " + mesg;
|
|
|
|
dump(out);
|
|
|
|
this._log.error(out);
|
|
|
|
|
|
|
|
return engineObject;
|
|
|
|
}
|
2008-04-14 18:53:35 -07:00
|
|
|
},
|
|
|
|
unregister: function EngMgr_unregister(val) {
|
|
|
|
let name = val;
|
|
|
|
if (val instanceof Engine)
|
|
|
|
name = val.name;
|
|
|
|
delete this._engines[name];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
function Engine() { this._init(); }
|
2007-12-14 18:07:25 -08:00
|
|
|
Engine.prototype = {
|
2009-01-06 13:54:18 -08:00
|
|
|
name: "engine",
|
|
|
|
displayName: "Boring Engine",
|
|
|
|
logName: "Engine",
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
// _storeObj, and _trackerObj should to be overridden in subclasses
|
2008-07-09 17:17:24 -07:00
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
_storeObj: Store,
|
|
|
|
_trackerObj: Tracker,
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2009-05-14 09:38:15 -07:00
|
|
|
get enabled() Svc.Prefs.get("engine." + this.name, null),
|
|
|
|
set enabled(val) Svc.Prefs.set("engine." + this.name, !!val),
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
get score() this._tracker.score,
|
2008-04-15 17:21:34 -07:00
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
get _store() {
|
2009-03-02 16:15:48 -08:00
|
|
|
if (!this.__store)
|
|
|
|
this.__store = new this._storeObj();
|
|
|
|
return this.__store;
|
2007-12-14 18:07:25 -08:00
|
|
|
},
|
2008-05-23 11:05:42 -07:00
|
|
|
|
2008-05-22 15:58:29 -07:00
|
|
|
get _tracker() {
|
2009-03-02 18:55:26 -08:00
|
|
|
if (!this.__tracker)
|
|
|
|
this.__tracker = new this._trackerObj();
|
|
|
|
return this.__tracker;
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
2008-04-15 17:21:34 -07:00
|
|
|
_init: function Engine__init() {
|
2009-06-05 15:34:23 -07:00
|
|
|
this._notify = Utils.notify("weave:engine:");
|
2008-12-09 12:26:14 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger("Engine." + this.logName);
|
2009-07-14 18:34:03 -07:00
|
|
|
let level = Svc.Prefs.get("log.logger.engine." + this.name, "Debug");
|
2008-07-25 17:02:43 -07:00
|
|
|
this._log.level = Log4Moz.Level[level];
|
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
|
|
|
},
|
|
|
|
|
2009-06-05 15:34:32 -07:00
|
|
|
sync: function Engine_sync() {
|
2009-01-06 13:54:18 -08:00
|
|
|
if (!this._sync)
|
|
|
|
throw "engine does not implement _sync method";
|
2009-09-03 20:11:36 -07:00
|
|
|
|
|
|
|
let times = {};
|
|
|
|
let wrapped = {};
|
|
|
|
// Find functions in any point of the prototype chain
|
|
|
|
for (let _name in this) {
|
|
|
|
let name = _name;
|
|
|
|
|
|
|
|
// Ignore certain constructors/functions
|
|
|
|
if (name.search(/^_(.+Obj|notify)$/) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Only track functions but skip the constructors
|
|
|
|
if (typeof this[name] == "function") {
|
|
|
|
times[name] = [];
|
|
|
|
wrapped[name] = this[name];
|
|
|
|
|
|
|
|
// Wrap the original function with a start/stop timer
|
|
|
|
this[name] = function() {
|
|
|
|
let start = Date.now();
|
|
|
|
try {
|
|
|
|
return wrapped[name].apply(this, arguments);
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
times[name].push(Date.now() - start);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this._notify("sync", this.name, this._sync)();
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
// Restore original unwrapped functionality
|
|
|
|
for (let [name, func] in Iterator(wrapped))
|
|
|
|
this[name] = func;
|
|
|
|
|
|
|
|
let stats = {};
|
|
|
|
for (let [name, time] in Iterator(times)) {
|
|
|
|
// Figure out stats on the times unless there's nothing
|
|
|
|
let num = time.length;
|
|
|
|
if (num == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Track the min/max/sum of the values
|
|
|
|
let stat = {
|
|
|
|
num: num,
|
|
|
|
sum: 0
|
|
|
|
};
|
|
|
|
time.forEach(function(val) {
|
|
|
|
if (val < stat.min || stat.min == null)
|
|
|
|
stat.min = val;
|
|
|
|
if (val > stat.max || stat.max == null)
|
|
|
|
stat.max = val;
|
|
|
|
stat.sum += val;
|
|
|
|
});
|
|
|
|
|
|
|
|
stat.avg = Number((stat.sum / num).toFixed(2));
|
|
|
|
stats[name] = stat;
|
|
|
|
}
|
|
|
|
|
|
|
|
stats.toString = function() {
|
|
|
|
let sums = [];
|
|
|
|
for (let [name, stat] in Iterator(this))
|
|
|
|
if (stat.sum != null)
|
|
|
|
sums.push(name.replace(/^_/, "") + " " + stat.sum);
|
|
|
|
|
|
|
|
return "Total (ms): " + sums.sort().join(", ");
|
|
|
|
};
|
|
|
|
|
|
|
|
this._log.info(stats);
|
|
|
|
}
|
2008-07-25 01:06:23 -07:00
|
|
|
},
|
|
|
|
|
2009-06-05 15:34:37 -07:00
|
|
|
wipeServer: function Engine_wipeServer() {
|
2009-01-06 13:54:18 -08:00
|
|
|
if (!this._wipeServer)
|
|
|
|
throw "engine does not implement _wipeServer method";
|
2009-06-05 15:34:37 -07:00
|
|
|
this._notify("wipe-server", this.name, this._wipeServer)();
|
2008-07-25 01:06:23 -07:00
|
|
|
},
|
|
|
|
|
2009-02-26 22:36:14 -08:00
|
|
|
/**
|
|
|
|
* Get rid of any local meta-data
|
|
|
|
*/
|
2009-06-05 15:34:34 -07:00
|
|
|
resetClient: function Engine_resetClient() {
|
2009-02-26 22:36:14 -08:00
|
|
|
if (!this._resetClient)
|
|
|
|
throw "engine does not implement _resetClient method";
|
|
|
|
|
2009-06-05 15:34:34 -07:00
|
|
|
this._notify("reset-client", this.name, this._resetClient)();
|
2009-02-26 22:36:14 -08:00
|
|
|
},
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
_wipeClient: function Engine__wipeClient() {
|
2009-06-05 15:34:34 -07:00
|
|
|
this.resetClient();
|
2009-01-06 13:54:18 -08:00
|
|
|
this._log.debug("Deleting all local data");
|
|
|
|
this._store.wipe();
|
2008-07-25 01:06:23 -07:00
|
|
|
},
|
2009-02-26 22:36:14 -08:00
|
|
|
|
2009-06-05 15:34:35 -07:00
|
|
|
wipeClient: function Engine_wipeClient() {
|
|
|
|
this._notify("wipe-client", this.name, this._wipeClient)();
|
2008-07-25 01:06:23 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
function SyncEngine() { this._init(); }
|
2008-12-05 00:39:54 -08:00
|
|
|
SyncEngine.prototype = {
|
2008-11-06 19:18:07 -08:00
|
|
|
__proto__: Engine.prototype,
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
_recordObj: CryptoWrapper,
|
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
get baseURL() {
|
2009-02-10 00:57:16 -08:00
|
|
|
let url = Svc.Prefs.get("clusterURL");
|
|
|
|
if (!url)
|
|
|
|
return null;
|
|
|
|
if (url[url.length-1] != '/')
|
2008-12-23 13:51:30 -08:00
|
|
|
url += '/';
|
2009-08-25 17:06:13 -07:00
|
|
|
url += "0.5/";
|
2008-12-05 00:39:54 -08:00
|
|
|
return url;
|
|
|
|
},
|
|
|
|
|
|
|
|
get engineURL() {
|
2009-08-25 17:06:13 -07:00
|
|
|
return this.baseURL + ID.get('WeaveID').username +
|
|
|
|
'/storage/' + this.name + '/';
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
get cryptoMetaURL() {
|
2009-08-25 17:06:13 -07:00
|
|
|
return this.baseURL + ID.get('WeaveID').username +
|
|
|
|
'/storage/crypto/' + this.name;
|
2008-11-19 16:20:25 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
get lastSync() {
|
2009-06-16 17:22:59 -07:00
|
|
|
return parseFloat(Svc.Prefs.get(this.name + ".lastSync", "0"));
|
2008-11-19 16:20:25 -08:00
|
|
|
},
|
2008-11-06 23:23:35 -08:00
|
|
|
set lastSync(value) {
|
2009-06-16 17:22:59 -07:00
|
|
|
// Reset the pref in-case it's a number instead of a string
|
2009-02-23 19:33:40 -08:00
|
|
|
Svc.Prefs.reset(this.name + ".lastSync");
|
2009-06-16 17:22:59 -07:00
|
|
|
// Store the value as a string to keep floating point precision
|
|
|
|
Svc.Prefs.set(this.name + ".lastSync", value.toString());
|
2009-02-17 13:20:02 -08:00
|
|
|
},
|
|
|
|
resetLastSync: function SyncEngine_resetLastSync() {
|
|
|
|
this._log.debug("Resetting " + this.name + " last sync time");
|
|
|
|
Svc.Prefs.reset(this.name + ".lastSync");
|
2009-06-16 17:22:59 -07:00
|
|
|
Svc.Prefs.set(this.name + ".lastSync", "0");
|
2008-11-06 23:23:35 -08:00
|
|
|
},
|
|
|
|
|
2008-12-28 19:59:44 -08:00
|
|
|
// Create a new record by querying the store, and add the engine metadata
|
|
|
|
_createRecord: function SyncEngine__createRecord(id) {
|
2009-02-19 00:36:55 -08:00
|
|
|
return this._store.createRecord(id, this.cryptoMetaURL);
|
2008-11-19 16:20:25 -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;
|
2008-12-23 11:30:31 -08:00
|
|
|
// note: sortindex ignored
|
2009-04-03 10:38:47 -07:00
|
|
|
if (a.deleted || b.deleted)
|
2009-01-07 13:05:05 -08:00
|
|
|
return false;
|
2008-12-05 00:39:54 -08:00
|
|
|
return Utils.deepEquals(a.cleartext, b.cleartext);
|
2008-11-19 16:20:25 -08:00
|
|
|
},
|
|
|
|
|
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() {
|
|
|
|
this._log.debug("Ensuring server crypto records are there");
|
|
|
|
|
2009-06-04 16:50:57 -07:00
|
|
|
let meta = CryptoMetas.get(this.cryptoMetaURL);
|
2008-12-05 00:39:54 -08:00
|
|
|
if (!meta) {
|
2009-01-27 13:35:10 -08:00
|
|
|
let symkey = Svc.Crypto.generateRandomKey();
|
2009-06-04 17:04:51 -07:00
|
|
|
let pubkey = PubKeys.getDefaultKey();
|
2008-12-05 00:39:54 -08:00
|
|
|
meta = new CryptoMeta(this.cryptoMetaURL);
|
|
|
|
meta.generateIV();
|
2009-06-04 17:36:34 -07:00
|
|
|
meta.addUnwrappedKey(pubkey, symkey);
|
2009-01-27 13:35:10 -08:00
|
|
|
let res = new Resource(meta.uri);
|
2009-08-26 15:32:46 -07:00
|
|
|
let resp = res.put(meta.serialize());
|
|
|
|
if (!resp.success)
|
|
|
|
throw resp;
|
2009-07-16 23:13:58 -07:00
|
|
|
|
|
|
|
// Cache the cryto meta that we just put on the server
|
|
|
|
CryptoMetas.set(meta.uri, meta);
|
2008-12-05 00:39:54 -08:00
|
|
|
}
|
2008-11-24 08:04:14 -08:00
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// first sync special case: upload all items
|
2009-01-02 13:51:38 -08:00
|
|
|
// NOTE: we use a backdoor (of sorts) to the tracker so it
|
|
|
|
// won't save to disk this list over and over
|
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
|
|
|
this._tracker.clearChangedIDs();
|
2009-01-02 13:51:38 -08:00
|
|
|
[i for (i in this._store.getAllIDs())]
|
|
|
|
.forEach(function(id) this._tracker.changedIDs[id] = true, this);
|
2008-11-08 02:00:33 -08:00
|
|
|
}
|
|
|
|
|
2009-01-02 13:51:38 -08:00
|
|
|
let outnum = [i for (i in this._tracker.changedIDs)].length;
|
|
|
|
this._log.info(outnum + " outgoing items pre-reconciliation");
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Generate outgoing records
|
2008-12-19 11:48:09 -08:00
|
|
|
_processIncoming: function SyncEngine__processIncoming() {
|
2009-09-03 21:30:40 -07:00
|
|
|
// Only bother getting data from the server if there's new things
|
|
|
|
if (this.lastModified <= this.lastSync) {
|
|
|
|
this._log.debug("Nothing new from the server to process");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-19 11:48:09 -08:00
|
|
|
this._log.debug("Downloading & applying server changes");
|
2008-11-24 08:04:14 -08:00
|
|
|
|
2008-12-30 23:52:20 -08:00
|
|
|
// enable cache, and keep only the first few items. Otherwise (when
|
|
|
|
// we have more outgoing items than can fit in the cache), we will
|
|
|
|
// keep rotating items in and out, perpetually getting cache misses
|
|
|
|
this._store.cache.enabled = true;
|
|
|
|
this._store.cache.fifo = false; // filo
|
|
|
|
this._store.cache.clear();
|
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
let newitems = new Collection(this.engineURL, this._recordObj);
|
2009-02-10 00:57:16 -08:00
|
|
|
newitems.newer = this.lastSync;
|
2008-11-19 16:20:25 -08:00
|
|
|
newitems.full = true;
|
2009-08-15 00:56:27 -07:00
|
|
|
newitems.sort = "index";
|
2008-11-19 16:20:25 -08:00
|
|
|
|
2009-01-14 22:01:04 -08:00
|
|
|
let count = {applied: 0, reconciled: 0};
|
2008-12-30 23:52:20 -08:00
|
|
|
this._lastSyncTmp = 0;
|
2009-02-26 17:31:06 -08:00
|
|
|
|
2009-07-22 16:38:34 -07:00
|
|
|
newitems.recordHandler = Utils.bind2(this, function(item) {
|
2009-01-27 13:35:10 -08:00
|
|
|
try {
|
2009-07-14 16:51:04 -07:00
|
|
|
item.decrypt(ID.get("WeaveCryptoID"));
|
2009-06-04 14:18:04 -07:00
|
|
|
if (this._reconcile(item)) {
|
2009-02-26 17:31:06 -08:00
|
|
|
count.applied++;
|
2009-06-05 00:36:11 -07:00
|
|
|
this._applyIncoming(item);
|
2009-02-26 17:31:06 -08:00
|
|
|
} else {
|
|
|
|
count.reconciled++;
|
|
|
|
this._log.trace("Skipping reconciled incoming item " + item.id);
|
|
|
|
if (this._lastSyncTmp < item.modified)
|
|
|
|
this._lastSyncTmp = item.modified;
|
|
|
|
}
|
2009-01-27 13:35:10 -08:00
|
|
|
} catch (e) {
|
2009-02-26 17:31:06 -08:00
|
|
|
this._log.error("Could not process incoming record: " +
|
2009-01-27 13:35:10 -08:00
|
|
|
Utils.exceptionStr(e));
|
|
|
|
}
|
2009-07-14 14:01:26 -07:00
|
|
|
Sync.sleep(0);
|
2009-07-22 16:38:34 -07:00
|
|
|
});
|
|
|
|
|
2009-08-26 15:32:46 -07:00
|
|
|
let resp = newitems.get();
|
|
|
|
if (!resp.success)
|
|
|
|
throw resp;
|
2009-07-22 16:38:34 -07:00
|
|
|
|
2008-12-19 11:48:09 -08:00
|
|
|
if (this.lastSync < this._lastSyncTmp)
|
|
|
|
this.lastSync = this._lastSyncTmp;
|
2008-12-30 23:52:20 -08:00
|
|
|
|
2009-01-14 22:01:04 -08:00
|
|
|
this._log.info("Applied " + count.applied + " records, reconciled " +
|
|
|
|
count.reconciled + " records");
|
|
|
|
|
2009-01-02 13:51:38 -08:00
|
|
|
// try to free some memory
|
|
|
|
this._store.cache.clear();
|
|
|
|
Cu.forceGC();
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
|
|
|
|
2009-07-28 10:06:02 -07:00
|
|
|
/**
|
|
|
|
* Find a GUID that is like the incoming item
|
|
|
|
*
|
|
|
|
* @return GUID of the similar record; falsy otherwise
|
|
|
|
*/
|
|
|
|
_findLikeId: function SyncEngine__findLikeId(item) {
|
|
|
|
// By default, only look in the outgoing queue for similar records
|
|
|
|
for (let id in this._tracker.changedIDs)
|
|
|
|
if (this._recordLike(item, this._createRecord(id)))
|
|
|
|
return id;
|
|
|
|
},
|
|
|
|
|
2009-01-02 15:51:35 -08:00
|
|
|
_isEqual: function SyncEngine__isEqual(item) {
|
|
|
|
let local = this._createRecord(item.id);
|
2009-08-20 17:00:15 -07:00
|
|
|
if (this._log.level <= Log4Moz.Level.Trace)
|
|
|
|
this._log.trace("Local record: " + local);
|
2009-01-02 15:51:35 -08:00
|
|
|
if (item.parentid == local.parentid &&
|
2009-01-02 17:27:45 -08:00
|
|
|
item.sortindex == local.sortindex &&
|
2009-05-15 11:18:16 -07:00
|
|
|
item.deleted == local.deleted &&
|
2009-01-02 15:51:35 -08:00
|
|
|
Utils.deepEquals(item.cleartext, local.cleartext)) {
|
2009-01-02 17:35:47 -08:00
|
|
|
this._log.trace("Local record is the same");
|
2009-01-02 15:51:35 -08:00
|
|
|
return true;
|
|
|
|
} else {
|
2009-01-02 17:35:47 -08:00
|
|
|
this._log.trace("Local record is different");
|
2009-01-02 15:51:35 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-01-02 13:51:38 -08:00
|
|
|
// Reconciliation has three steps:
|
2008-12-05 00:39:54 -08:00
|
|
|
// 1) Check for the same item (same ID) on both the incoming and outgoing
|
2009-01-02 13:51:38 -08:00
|
|
|
// 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 the incoming item's ID exists locally. In that case it's an
|
|
|
|
// update and we should not try a similarity check (step 3)
|
|
|
|
// 3) 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. It's also the common
|
|
|
|
// case when syncing for the first time two machines that already have the
|
|
|
|
// same bookmarks. In this case we change the IDs to match.
|
2008-12-19 11:48:09 -08:00
|
|
|
_reconcile: function SyncEngine__reconcile(item) {
|
2009-08-25 16:15:05 -07:00
|
|
|
if (this._log.level <= Log4Moz.Level.Trace)
|
|
|
|
this._log.trace("Incoming: " + item);
|
|
|
|
|
2009-01-02 15:51:35 -08:00
|
|
|
// Step 1: Check for conflicts
|
|
|
|
// If same as local record, do not upload
|
2009-03-03 01:29:35 -08:00
|
|
|
this._log.trace("Reconcile step 1");
|
2008-12-29 23:28:17 -08:00
|
|
|
if (item.id in this._tracker.changedIDs) {
|
2009-01-02 15:51:35 -08:00
|
|
|
if (this._isEqual(item))
|
2008-12-29 23:28:17 -08:00
|
|
|
this._tracker.removeChangedID(item.id);
|
2009-06-04 14:18:04 -07:00
|
|
|
return false;
|
2008-12-29 23:28:17 -08:00
|
|
|
}
|
|
|
|
|
2009-01-02 15:51:35 -08:00
|
|
|
// Step 2: Check for updates
|
|
|
|
// If different from local record, apply server update
|
2009-03-03 01:29:35 -08:00
|
|
|
this._log.trace("Reconcile step 2");
|
2009-06-04 14:18:04 -07:00
|
|
|
if (this._store.itemExists(item.id))
|
|
|
|
return !this._isEqual(item);
|
2008-11-19 16:20:25 -08:00
|
|
|
|
2009-01-08 21:33:37 -08:00
|
|
|
// If the incoming item has been deleted, skip step 3
|
2009-03-03 01:29:35 -08:00
|
|
|
this._log.trace("Reconcile step 2.5");
|
2009-06-04 14:18:04 -07:00
|
|
|
if (item.deleted)
|
|
|
|
return true;
|
2009-01-08 21:33:37 -08:00
|
|
|
|
2009-01-02 15:51:35 -08:00
|
|
|
// Step 3: Check for similar items
|
2009-03-03 01:29:35 -08:00
|
|
|
this._log.trace("Reconcile step 3");
|
2009-07-28 10:06:02 -07:00
|
|
|
let likeId = this._findLikeId(item);
|
|
|
|
if (likeId) {
|
|
|
|
// Change the local item GUID to the incoming one
|
|
|
|
this._store.changeItemID(likeId, item.id);
|
|
|
|
|
|
|
|
// Remove outgoing changes of the original id any any that were just made
|
|
|
|
this._tracker.removeChangedID(likeId);
|
|
|
|
this._tracker.removeChangedID(item.id);
|
|
|
|
|
|
|
|
this._store.cache.clear(); // because parentid refs will be wrong
|
|
|
|
return false;
|
2008-11-26 07:25:28 -08:00
|
|
|
}
|
2009-06-04 14:18:04 -07:00
|
|
|
|
|
|
|
return true;
|
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
|
2008-12-19 11:48:09 -08:00
|
|
|
_applyIncoming: function SyncEngine__applyIncoming(item) {
|
|
|
|
try {
|
2009-01-14 22:01:04 -08:00
|
|
|
this._tracker.ignoreAll = true;
|
2009-06-05 00:36:11 -07:00
|
|
|
this._store.applyIncoming(item);
|
2008-12-23 11:30:31 -08:00
|
|
|
if (this._lastSyncTmp < item.modified)
|
2008-12-19 11:48:09 -08:00
|
|
|
this._lastSyncTmp = item.modified;
|
|
|
|
} catch (e) {
|
2009-08-16 12:39:23 -07:00
|
|
|
this._log.warn("Error while applying record: " + Utils.stackTrace(e));
|
2009-01-13 14:43:21 -08:00
|
|
|
} finally {
|
2009-01-14 22:01:04 -08:00
|
|
|
this._tracker.ignoreAll = false;
|
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() {
|
2009-01-02 13:51:38 -08:00
|
|
|
let outnum = [i for (i in this._tracker.changedIDs)].length;
|
|
|
|
this._log.debug("Preparing " + outnum + " outgoing records");
|
|
|
|
if (outnum) {
|
2008-12-23 11:30:31 -08:00
|
|
|
// collection we'll upload
|
2008-11-24 08:04:14 -08:00
|
|
|
let up = new Collection(this.engineURL);
|
2009-08-13 18:50:54 -07:00
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
// Upload what we've got so far in the collection
|
|
|
|
let doUpload = Utils.bind2(this, function(desc) {
|
|
|
|
this._log.info("Uploading " + desc + " of " + outnum + " records");
|
2009-08-26 15:32:46 -07:00
|
|
|
let resp = up.post();
|
|
|
|
if (!resp.success)
|
|
|
|
throw resp;
|
2009-08-25 18:04:46 -07:00
|
|
|
|
|
|
|
// Record the modified time of the upload
|
2009-08-26 16:09:48 -07:00
|
|
|
let modified = resp.headers["X-Weave-Timestamp"];
|
2009-08-25 18:04:46 -07:00
|
|
|
if (modified > this.lastSync)
|
|
|
|
this.lastSync = modified;
|
|
|
|
|
2009-08-13 18:50:54 -07:00
|
|
|
up.clearRecords();
|
|
|
|
});
|
2008-12-23 11:30:31 -08:00
|
|
|
|
2008-12-30 23:52:20 -08:00
|
|
|
// don't cache the outgoing items, we won't need them later
|
|
|
|
this._store.cache.enabled = false;
|
|
|
|
|
|
|
|
for (let id in this._tracker.changedIDs) {
|
|
|
|
let out = this._createRecord(id);
|
2009-08-20 17:00:15 -07:00
|
|
|
if (this._log.level <= Log4Moz.Level.Trace)
|
|
|
|
this._log.trace("Outgoing: " + out);
|
2009-07-22 16:21:33 -07:00
|
|
|
|
2009-07-14 16:51:04 -07:00
|
|
|
out.encrypt(ID.get("WeaveCryptoID"));
|
2009-03-31 12:09:38 -07:00
|
|
|
up.pushData(JSON.parse(out.serialize())); // FIXME: inefficient
|
2009-07-22 16:21:33 -07:00
|
|
|
|
2009-08-13 18:50:54 -07:00
|
|
|
// Partial upload
|
|
|
|
if ((++count % MAX_UPLOAD_RECORDS) == 0)
|
|
|
|
doUpload((count - MAX_UPLOAD_RECORDS) + " - " + count + " out");
|
2009-07-22 16:21:33 -07:00
|
|
|
|
2009-07-14 14:01:26 -07:00
|
|
|
Sync.sleep(0);
|
2008-12-23 11:30:31 -08:00
|
|
|
}
|
|
|
|
|
2009-08-13 18:50:54 -07:00
|
|
|
// Final upload
|
|
|
|
if (count % MAX_UPLOAD_RECORDS > 0)
|
|
|
|
doUpload(count >= MAX_UPLOAD_RECORDS ? "last batch" : "all");
|
2008-12-30 23:52:20 -08:00
|
|
|
|
2009-08-13 18:50:54 -07:00
|
|
|
this._store.cache.enabled = true;
|
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
|
2009-06-04 23:48:27 -07:00
|
|
|
_syncFinish: function SyncEngine__syncFinish() {
|
2009-07-22 21:40:18 -07:00
|
|
|
this._log.trace("Finishing up sync");
|
2008-12-05 00:39:54 -08:00
|
|
|
this._tracker.resetScore();
|
2008-11-06 19:18:07 -08:00
|
|
|
},
|
|
|
|
|
2008-07-25 17:02:43 -07:00
|
|
|
_sync: function SyncEngine__sync() {
|
2008-12-05 00:55:19 -08:00
|
|
|
try {
|
2009-06-04 23:48:27 -07:00
|
|
|
this._syncStartup();
|
2009-02-22 00:04:58 -08:00
|
|
|
Observers.notify("weave:engine:sync:status", "process-incoming");
|
2009-06-05 00:39:35 -07:00
|
|
|
this._processIncoming();
|
2009-02-22 00:04:58 -08:00
|
|
|
Observers.notify("weave:engine:sync:status", "upload-outgoing");
|
2009-06-04 23:48:27 -07:00
|
|
|
this._uploadOutgoing();
|
|
|
|
this._syncFinish();
|
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;
|
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
2008-08-08 14:42:57 -07:00
|
|
|
|
2009-01-06 13:54:18 -08:00
|
|
|
_wipeServer: function SyncEngine__wipeServer() {
|
2009-06-05 15:34:37 -07:00
|
|
|
new Resource(this.engineURL).delete();
|
|
|
|
new Resource(this.cryptoMetaURL).delete();
|
2009-02-26 22:36:14 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
_resetClient: function SyncEngine__resetClient() {
|
|
|
|
this.resetLastSync();
|
2008-07-25 01:06:23 -07:00
|
|
|
}
|
|
|
|
};
|