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>
|
2011-01-18 16:23:25 -08:00
|
|
|
* Anant Narayanan <anant@kix.in>
|
2010-11-11 11:00:35 -08:00
|
|
|
* Philipp von Weitershausen <philipp@weitershausen.de>
|
2010-11-29 16:41:17 -08:00
|
|
|
* Richard Newman <rnewman@mozilla.com>
|
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 ***** */
|
|
|
|
|
2011-01-18 16:23:25 -08:00
|
|
|
const EXPORTED_SYMBOLS = ['Engines', 'Engine', 'SyncEngine',
|
|
|
|
'Tracker', 'Store'];
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2011-05-30 18:52:23 -07:00
|
|
|
Cu.import("resource://services-sync/async.js");
|
2011-01-18 16:23:30 -08:00
|
|
|
Cu.import("resource://services-sync/record.js");
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/constants.js");
|
|
|
|
Cu.import("resource://services-sync/ext/Observers.js");
|
|
|
|
Cu.import("resource://services-sync/identity.js");
|
|
|
|
Cu.import("resource://services-sync/log4moz.js");
|
|
|
|
Cu.import("resource://services-sync/resource.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2008-11-08 02:00:33 -08:00
|
|
|
|
2010-12-09 23:06:44 -08:00
|
|
|
Cu.import("resource://services-sync/main.js"); // So we can get to Service for callbacks.
|
|
|
|
|
2011-01-18 16:23:25 -08:00
|
|
|
/*
|
|
|
|
* Trackers are associated with a single engine and deal with
|
|
|
|
* listening for changes to their particular data type.
|
|
|
|
*
|
|
|
|
* There are two things they keep track of:
|
|
|
|
* 1) A score, indicating how urgently the engine wants to sync
|
|
|
|
* 2) A list of IDs for all the changed items that need to be synced
|
|
|
|
* and updating their 'score', indicating how urgently they
|
|
|
|
* want to sync.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function Tracker(name) {
|
|
|
|
name = name || "Unnamed";
|
|
|
|
this.name = this.file = name.toLowerCase();
|
|
|
|
|
2011-06-13 11:42:18 -07:00
|
|
|
this._log = Log4Moz.repository.getLogger("Sync.Tracker." + name);
|
2011-01-18 16:23:25 -08:00
|
|
|
let level = Svc.Prefs.get("log.logger.engine." + this.name, "Debug");
|
|
|
|
this._log.level = Log4Moz.Level[level];
|
|
|
|
|
|
|
|
this._score = 0;
|
|
|
|
this._ignored = [];
|
|
|
|
this.ignoreAll = false;
|
|
|
|
this.changedIDs = {};
|
|
|
|
this.loadChangedIDs();
|
|
|
|
}
|
|
|
|
Tracker.prototype = {
|
|
|
|
/*
|
|
|
|
* Score can be called as often as desired to decide which engines to sync
|
|
|
|
*
|
|
|
|
* Valid values for score:
|
|
|
|
* -1: Do not sync unless the user specifically requests it (almost disabled)
|
|
|
|
* 0: Nothing has changed
|
|
|
|
* 100: Please sync me ASAP!
|
|
|
|
*
|
|
|
|
* Setting it to other values should (but doesn't currently) throw an exception
|
|
|
|
*/
|
|
|
|
get score() {
|
|
|
|
return this._score;
|
|
|
|
},
|
|
|
|
|
|
|
|
set score(value) {
|
|
|
|
this._score = value;
|
|
|
|
Observers.notify("weave:engine:score:updated", this.name);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Should be called by service everytime a sync has been done for an engine
|
|
|
|
resetScore: function T_resetScore() {
|
|
|
|
this._score = 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
saveChangedIDs: function T_saveChangedIDs() {
|
2011-06-06 12:27:36 -07:00
|
|
|
Utils.namedTimer(function() {
|
2011-01-18 16:23:25 -08:00
|
|
|
Utils.jsonSave("changes/" + this.file, this, this.changedIDs);
|
|
|
|
}, 1000, this, "_lazySave");
|
|
|
|
},
|
|
|
|
|
|
|
|
loadChangedIDs: function T_loadChangedIDs() {
|
|
|
|
Utils.jsonLoad("changes/" + this.file, this, function(json) {
|
2011-01-26 21:43:24 -08:00
|
|
|
if (json) {
|
|
|
|
this.changedIDs = json;
|
|
|
|
}
|
2011-01-18 16:23:25 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// ignore/unignore specific IDs. Useful for ignoring items that are
|
|
|
|
// being processed, or that shouldn't be synced.
|
|
|
|
// But note: not persisted to disk
|
|
|
|
|
|
|
|
ignoreID: function T_ignoreID(id) {
|
|
|
|
this.unignoreID(id);
|
|
|
|
this._ignored.push(id);
|
|
|
|
},
|
|
|
|
|
|
|
|
unignoreID: function T_unignoreID(id) {
|
|
|
|
let index = this._ignored.indexOf(id);
|
|
|
|
if (index != -1)
|
|
|
|
this._ignored.splice(index, 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
addChangedID: function addChangedID(id, when) {
|
|
|
|
if (!id) {
|
|
|
|
this._log.warn("Attempted to add undefined ID to tracker");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this.ignoreAll || (id in this._ignored))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Default to the current time in seconds if no time is provided
|
|
|
|
if (when == null)
|
|
|
|
when = Math.floor(Date.now() / 1000);
|
|
|
|
|
|
|
|
// Add/update the entry if we have a newer time
|
|
|
|
if ((this.changedIDs[id] || -Infinity) < when) {
|
|
|
|
this._log.trace("Adding changed ID: " + [id, when]);
|
|
|
|
this.changedIDs[id] = when;
|
|
|
|
this.saveChangedIDs();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
removeChangedID: function T_removeChangedID(id) {
|
|
|
|
if (!id) {
|
|
|
|
this._log.warn("Attempted to remove undefined ID to tracker");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this.ignoreAll || (id in this._ignored))
|
|
|
|
return false;
|
|
|
|
if (this.changedIDs[id] != null) {
|
|
|
|
this._log.trace("Removing changed ID " + id);
|
|
|
|
delete this.changedIDs[id];
|
|
|
|
this.saveChangedIDs();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
clearChangedIDs: function T_clearChangedIDs() {
|
|
|
|
this._log.trace("Clearing changed ID list");
|
|
|
|
this.changedIDs = {};
|
|
|
|
this.saveChangedIDs();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Data Stores
|
|
|
|
* These can wrap, serialize items and apply commands
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Store(name) {
|
|
|
|
name = name || "Unnamed";
|
|
|
|
this.name = name.toLowerCase();
|
|
|
|
|
2011-06-13 11:42:18 -07:00
|
|
|
this._log = Log4Moz.repository.getLogger("Sync.Store." + name);
|
2011-01-18 16:23:25 -08:00
|
|
|
let level = Svc.Prefs.get("log.logger.engine." + this.name, "Debug");
|
|
|
|
this._log.level = Log4Moz.Level[level];
|
2011-03-07 13:07:59 -08:00
|
|
|
|
2011-05-19 18:08:35 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "_timer", function() {
|
2011-03-07 13:07:59 -08:00
|
|
|
return Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
});
|
2011-01-18 16:23:25 -08:00
|
|
|
}
|
|
|
|
Store.prototype = {
|
2011-01-28 09:39:12 -08:00
|
|
|
|
2011-03-07 13:07:59 -08:00
|
|
|
_sleep: function _sleep(delay) {
|
2011-05-30 18:52:23 -07:00
|
|
|
let cb = Async.makeSyncCallback();
|
2011-06-06 12:27:36 -07:00
|
|
|
this._timer.initWithCallback(cb, delay, Ci.nsITimer.TYPE_ONE_SHOT);
|
2011-05-30 18:52:23 -07:00
|
|
|
Async.waitForSyncCallback(cb);
|
2011-03-07 13:07:59 -08:00
|
|
|
},
|
|
|
|
|
2011-01-28 09:39:12 -08:00
|
|
|
applyIncomingBatch: function applyIncomingBatch(records) {
|
|
|
|
let failed = [];
|
2011-03-07 13:07:59 -08:00
|
|
|
for each (let record in records) {
|
2011-01-28 09:39:12 -08:00
|
|
|
try {
|
|
|
|
this.applyIncoming(record);
|
2011-06-15 00:03:32 -07:00
|
|
|
} catch (ex if (ex.code == Engine.prototype.eEngineAbortApplyIncoming)) {
|
|
|
|
// This kind of exception should have a 'cause' attribute, which is an
|
|
|
|
// originating exception.
|
|
|
|
// ex.cause will carry its stack with it when rethrown.
|
|
|
|
throw ex.cause;
|
2011-01-28 09:39:12 -08:00
|
|
|
} catch (ex) {
|
|
|
|
this._log.warn("Failed to apply incoming record " + record.id);
|
|
|
|
this._log.warn("Encountered exception: " + Utils.exceptionStr(ex));
|
|
|
|
failed.push(record.id);
|
|
|
|
}
|
2011-03-07 13:07:59 -08:00
|
|
|
};
|
2011-01-28 09:39:12 -08:00
|
|
|
return failed;
|
|
|
|
},
|
|
|
|
|
2011-01-18 16:23:25 -08:00
|
|
|
applyIncoming: function Store_applyIncoming(record) {
|
|
|
|
if (record.deleted)
|
|
|
|
this.remove(record);
|
|
|
|
else if (!this.itemExists(record.id))
|
|
|
|
this.create(record);
|
|
|
|
else
|
|
|
|
this.update(record);
|
|
|
|
},
|
|
|
|
|
|
|
|
// override these in derived objects
|
|
|
|
|
|
|
|
create: function Store_create(record) {
|
|
|
|
throw "override create in a subclass";
|
|
|
|
},
|
|
|
|
|
|
|
|
remove: function Store_remove(record) {
|
|
|
|
throw "override remove in a subclass";
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function Store_update(record) {
|
|
|
|
throw "override update in a subclass";
|
|
|
|
},
|
|
|
|
|
|
|
|
itemExists: function Store_itemExists(id) {
|
|
|
|
throw "override itemExists in a subclass";
|
|
|
|
},
|
|
|
|
|
|
|
|
createRecord: function Store_createRecord(id, collection) {
|
|
|
|
throw "override createRecord in a subclass";
|
|
|
|
},
|
|
|
|
|
|
|
|
changeItemID: function Store_changeItemID(oldID, newID) {
|
|
|
|
throw "override changeItemID in a subclass";
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllIDs: function Store_getAllIDs() {
|
|
|
|
throw "override getAllIDs in a subclass";
|
|
|
|
},
|
|
|
|
|
|
|
|
wipe: function Store_wipe() {
|
|
|
|
throw "override wipe in a subclass";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-04-14 18:53:35 -07:00
|
|
|
// Singleton service, holds registered engines
|
|
|
|
|
2011-05-19 18:08:35 -07:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "Engines", function() {
|
|
|
|
return new EngineManagerSvc();
|
|
|
|
});
|
2008-04-14 18:53:35 -07:00
|
|
|
|
|
|
|
function EngineManagerSvc() {
|
|
|
|
this._engines = {};
|
2011-06-13 11:42:18 -07:00
|
|
|
this._log = Log4Moz.repository.getLogger("Sync.EngineManager");
|
2009-03-20 00:13:16 -07:00
|
|
|
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];
|
2010-11-29 16:41:17 -08:00
|
|
|
if (!engine) {
|
2009-03-20 00:13:16 -07:00
|
|
|
this._log.debug("Could not get engine: " + name);
|
2010-11-29 16:41:17 -08:00
|
|
|
if (Object.keys)
|
|
|
|
this._log.debug("Engines are: " + JSON.stringify(Object.keys(this._engines)));
|
|
|
|
}
|
2009-03-20 00:13:16 -07:00
|
|
|
return engine;
|
2008-04-14 18:53:35 -07:00
|
|
|
},
|
|
|
|
getAll: function EngMgr_getAll() {
|
2009-10-08 13:51:22 -07:00
|
|
|
return [engine for ([name, engine] in Iterator(Engines._engines))];
|
2008-04-14 18:53:35 -07:00
|
|
|
},
|
2008-07-09 17:17:24 -07:00
|
|
|
getEnabled: function EngMgr_getEnabled() {
|
2009-10-08 13:51:22 -07:00
|
|
|
return this.getAll().filter(function(engine) engine.enabled);
|
2008-07-09 17:17:24 -07:00
|
|
|
},
|
2010-12-09 23:06:44 -08:00
|
|
|
|
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 {
|
2010-02-11 15:29:15 -08:00
|
|
|
let engine = new engineObject();
|
|
|
|
let name = engine.name;
|
2009-04-08 12:39:14 -07:00
|
|
|
if (name in this._engines)
|
|
|
|
this._log.error("Engine '" + name + "' is already registered!");
|
|
|
|
else
|
2010-02-11 15:29:15 -08:00
|
|
|
this._engines[name] = engine;
|
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;
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-02-11 15:29:15 -08:00
|
|
|
function Engine(name) {
|
|
|
|
this.Name = name || "Unnamed";
|
|
|
|
this.name = name.toLowerCase();
|
|
|
|
|
2010-02-11 15:25:31 -08:00
|
|
|
this._notify = Utils.notify("weave:engine:");
|
2011-06-13 11:42:18 -07:00
|
|
|
this._log = Log4Moz.repository.getLogger("Sync.Engine." + this.Name);
|
2010-02-11 15:25:31 -08:00
|
|
|
let level = Svc.Prefs.get("log.logger.engine." + this.name, "Debug");
|
|
|
|
this._log.level = Log4Moz.Level[level];
|
|
|
|
|
|
|
|
this._tracker; // initialize tracker to load previously changed IDs
|
|
|
|
this._log.debug("Engine initialized");
|
|
|
|
}
|
2007-12-14 18:07:25 -08:00
|
|
|
Engine.prototype = {
|
2009-01-06 13:54:18 -08:00
|
|
|
// _storeObj, and _trackerObj should to be overridden in subclasses
|
|
|
|
_storeObj: Store,
|
|
|
|
_trackerObj: Tracker,
|
2007-12-14 18:07:25 -08:00
|
|
|
|
2011-06-15 00:03:32 -07:00
|
|
|
// Local 'constant'.
|
|
|
|
// Signal to the engine that processing further records is pointless.
|
|
|
|
eEngineAbortApplyIncoming: "error.engine.abort.applyincoming",
|
|
|
|
|
2009-11-30 12:15:18 -08:00
|
|
|
get prefName() this.name,
|
2010-06-01 15:06:16 -07:00
|
|
|
get enabled() Svc.Prefs.get("engine." + this.prefName, false),
|
2009-11-30 12:15:18 -08:00
|
|
|
set enabled(val) Svc.Prefs.set("engine." + this.prefName, !!val),
|
2009-05-14 09:38:15 -07:00
|
|
|
|
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() {
|
2010-02-11 15:29:15 -08:00
|
|
|
let store = new this._storeObj(this.Name);
|
|
|
|
this.__defineGetter__("_store", function() store);
|
|
|
|
return 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() {
|
2010-02-11 15:29:15 -08:00
|
|
|
let tracker = new this._trackerObj(this.Name);
|
|
|
|
this.__defineGetter__("_tracker", function() tracker);
|
|
|
|
return tracker;
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
2009-11-20 14:34:20 -08:00
|
|
|
|
2009-06-05 15:34:32 -07:00
|
|
|
sync: function Engine_sync() {
|
2010-04-29 16:42:21 -07:00
|
|
|
if (!this.enabled)
|
|
|
|
return;
|
|
|
|
|
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
|
|
|
|
2011-03-02 15:27:16 -08:00
|
|
|
this._notify("sync", this.name, this._sync)();
|
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");
|
2010-04-07 17:52:22 -07:00
|
|
|
this._tracker.ignoreAll = true;
|
2009-01-06 13:54:18 -08:00
|
|
|
this._store.wipe();
|
2010-04-07 17:52:22 -07:00
|
|
|
this._tracker.ignoreAll = false;
|
2010-05-03 14:39:32 -07:00
|
|
|
this._tracker.clearChangedIDs();
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-02-11 15:29:15 -08:00
|
|
|
function SyncEngine(name) {
|
|
|
|
Engine.call(this, name || "SyncEngine");
|
2011-01-26 21:34:31 -08:00
|
|
|
this.loadToFetch();
|
2011-05-27 17:32:13 -07:00
|
|
|
this.loadPreviousFailed();
|
2010-02-11 15:25:31 -08:00
|
|
|
}
|
2011-03-20 16:10:40 -07:00
|
|
|
|
|
|
|
// Enumeration to define approaches to handling bad records.
|
|
|
|
// Attached to the constructor to allow use as a kind of static enumeration.
|
|
|
|
SyncEngine.kRecoveryStrategy = {
|
|
|
|
ignore: "ignore",
|
|
|
|
retry: "retry",
|
|
|
|
error: "error"
|
|
|
|
};
|
|
|
|
|
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,
|
2010-03-25 10:05:21 -07:00
|
|
|
version: 1,
|
2011-04-06 17:18:22 -07:00
|
|
|
|
|
|
|
// How many records to pull in a single sync. This is primarily to avoid very
|
|
|
|
// long first syncs against profiles with many history records.
|
2011-01-28 09:39:12 -08:00
|
|
|
downloadLimit: null,
|
2011-04-06 17:18:22 -07:00
|
|
|
|
|
|
|
// How many records to pull at one time when specifying IDs. This is to avoid
|
|
|
|
// URI length limitations.
|
|
|
|
guidFetchBatchSize: DEFAULT_GUID_FETCH_BATCH_SIZE,
|
|
|
|
mobileGUIDFetchBatchSize: DEFAULT_MOBILE_GUID_FETCH_BATCH_SIZE,
|
|
|
|
|
|
|
|
// How many records to process in a single batch.
|
2011-01-28 09:39:12 -08:00
|
|
|
applyIncomingBatchSize: DEFAULT_STORE_BATCH_SIZE,
|
2009-01-06 13:54:18 -08:00
|
|
|
|
2011-04-08 14:52:08 -07:00
|
|
|
get storageURL() Svc.Prefs.get("clusterURL") + SYNC_API_VERSION +
|
2009-11-10 15:24:31 -08:00
|
|
|
"/" + ID.get("WeaveID").username + "/storage/",
|
2008-12-05 00:39:54 -08:00
|
|
|
|
2009-09-21 17:13:41 -07:00
|
|
|
get engineURL() this.storageURL + this.name,
|
2008-12-05 00:39:54 -08:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
get cryptoKeysURL() this.storageURL + "crypto/keys",
|
2008-11-19 16:20:25 -08:00
|
|
|
|
2010-03-25 10:05:21 -07:00
|
|
|
get metaURL() this.storageURL + "meta/global",
|
|
|
|
|
|
|
|
get syncID() {
|
|
|
|
// Generate a random syncID if we don't have one
|
|
|
|
let syncID = Svc.Prefs.get(this.name + ".syncID", "");
|
|
|
|
return syncID == "" ? this.syncID = Utils.makeGUID() : syncID;
|
|
|
|
},
|
|
|
|
set syncID(value) {
|
|
|
|
Svc.Prefs.set(this.name + ".syncID", value);
|
|
|
|
},
|
|
|
|
|
2010-11-11 11:00:35 -08:00
|
|
|
/*
|
|
|
|
* lastSync is a timestamp in server time.
|
|
|
|
*/
|
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");
|
2010-11-11 11:00:35 -08:00
|
|
|
this.lastSyncLocal = 0;
|
|
|
|
},
|
|
|
|
|
2011-01-26 21:34:31 -08:00
|
|
|
get toFetch() this._toFetch,
|
|
|
|
set toFetch(val) {
|
2011-05-27 17:32:13 -07:00
|
|
|
// Coerce the array to a string for more efficient comparison.
|
|
|
|
if (val + "" == this._toFetch) {
|
|
|
|
return;
|
|
|
|
}
|
2011-01-26 21:34:31 -08:00
|
|
|
this._toFetch = val;
|
2011-06-06 12:27:36 -07:00
|
|
|
Utils.namedTimer(function () {
|
2011-01-26 21:34:31 -08:00
|
|
|
Utils.jsonSave("toFetch/" + this.name, this, val);
|
|
|
|
}, 0, this, "_toFetchDelay");
|
|
|
|
},
|
|
|
|
|
|
|
|
loadToFetch: function loadToFetch() {
|
|
|
|
// Initialize to empty if there's no file
|
|
|
|
this._toFetch = [];
|
|
|
|
Utils.jsonLoad("toFetch/" + this.name, this, function(toFetch) {
|
2011-01-26 21:43:24 -08:00
|
|
|
if (toFetch) {
|
|
|
|
this._toFetch = toFetch;
|
|
|
|
}
|
2011-01-26 21:34:31 -08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-05-27 17:32:13 -07:00
|
|
|
get previousFailed() this._previousFailed,
|
|
|
|
set previousFailed(val) {
|
|
|
|
// Coerce the array to a string for more efficient comparison.
|
|
|
|
if (val + "" == this._previousFailed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._previousFailed = val;
|
2011-06-06 12:27:36 -07:00
|
|
|
Utils.namedTimer(function () {
|
2011-05-27 17:32:13 -07:00
|
|
|
Utils.jsonSave("failed/" + this.name, this, val);
|
|
|
|
}, 0, this, "_previousFailedDelay");
|
|
|
|
},
|
|
|
|
|
|
|
|
loadPreviousFailed: function loadPreviousFailed() {
|
|
|
|
// Initialize to empty if there's no file
|
|
|
|
this._previousFailed = [];
|
|
|
|
Utils.jsonLoad("failed/" + this.name, this, function(previousFailed) {
|
|
|
|
if (previousFailed) {
|
|
|
|
this._previousFailed = previousFailed;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2010-11-11 11:00:35 -08:00
|
|
|
/*
|
|
|
|
* lastSyncLocal is a timestamp in local time.
|
|
|
|
*/
|
|
|
|
get lastSyncLocal() {
|
|
|
|
return parseInt(Svc.Prefs.get(this.name + ".lastSyncLocal", "0"), 10);
|
|
|
|
},
|
|
|
|
set lastSyncLocal(value) {
|
|
|
|
// Store as a string because pref can only store C longs as numbers.
|
|
|
|
Svc.Prefs.set(this.name + ".lastSyncLocal", value.toString());
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a mapping of IDs -> changed timestamp. Engine implementations
|
|
|
|
* can override this method to bypass the tracker for certain or all
|
|
|
|
* changed items.
|
|
|
|
*/
|
|
|
|
getChangedIDs: function getChangedIDs() {
|
|
|
|
return this._tracker.changedIDs;
|
2008-11-06 23:23:35 -08:00
|
|
|
},
|
|
|
|
|
2010-03-05 14:46:48 -08:00
|
|
|
// Create a new record using the store and add in crypto fields
|
2008-12-28 19:59:44 -08:00
|
|
|
_createRecord: function SyncEngine__createRecord(id) {
|
2010-11-29 16:41:17 -08:00
|
|
|
let record = this._store.createRecord(id, this.name);
|
2010-03-05 14:46:48 -08:00
|
|
|
record.id = id;
|
2010-11-29 16:41:17 -08:00
|
|
|
record.collection = this.name;
|
2010-03-05 14:46:48 -08:00
|
|
|
return record;
|
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.
|
|
|
|
_syncStartup: function SyncEngine__syncStartup() {
|
2010-05-21 12:15:58 -07:00
|
|
|
|
2010-03-25 10:05:21 -07:00
|
|
|
// Determine if we need to wipe on outdated versions
|
|
|
|
let metaGlobal = Records.get(this.metaURL);
|
|
|
|
let engines = metaGlobal.payload.engines || {};
|
|
|
|
let engineData = engines[this.name] || {};
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
let needsWipe = false;
|
|
|
|
|
2010-03-25 10:05:21 -07:00
|
|
|
// Assume missing versions are 0 and wipe the server
|
|
|
|
if ((engineData.version || 0) < this.version) {
|
|
|
|
this._log.debug("Old engine data: " + [engineData.version, this.version]);
|
|
|
|
|
|
|
|
// Prepare to clear the server and upload everything
|
2010-11-29 16:41:17 -08:00
|
|
|
needsWipe = true;
|
2010-03-25 10:05:21 -07:00
|
|
|
this.syncID = "";
|
|
|
|
|
|
|
|
// Set the newer version and newly generated syncID
|
|
|
|
engineData.version = this.version;
|
|
|
|
engineData.syncID = this.syncID;
|
|
|
|
|
|
|
|
// Put the new data back into meta/global and mark for upload
|
|
|
|
engines[this.name] = engineData;
|
|
|
|
metaGlobal.payload.engines = engines;
|
|
|
|
metaGlobal.changed = true;
|
|
|
|
}
|
|
|
|
// Don't sync this engine if the server has newer data
|
|
|
|
else if (engineData.version > this.version) {
|
|
|
|
let error = new String("New data: " + [engineData.version, this.version]);
|
|
|
|
error.failureCode = VERSION_OUT_OF_DATE;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
// Changes to syncID mean we'll need to upload everything
|
|
|
|
else if (engineData.syncID != this.syncID) {
|
|
|
|
this._log.debug("Engine syncIDs: " + [engineData.syncID, this.syncID]);
|
|
|
|
this.syncID = engineData.syncID;
|
|
|
|
this._resetClient();
|
|
|
|
};
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Delete any existing data and reupload on bad version or missing meta.
|
|
|
|
// No crypto component here...? We could regenerate per-collection keys...
|
|
|
|
if (needsWipe) {
|
2010-09-07 09:44:01 -07:00
|
|
|
this.wipeServer(true);
|
2008-12-05 00:39:54 -08:00
|
|
|
}
|
2008-11-24 08:04:14 -08:00
|
|
|
|
2010-11-23 21:21:31 -08:00
|
|
|
// Save objects that need to be uploaded in this._modified. We also save
|
|
|
|
// the timestamp of this fetch in this.lastSyncLocal. As we successfully
|
|
|
|
// upload objects we remove them from this._modified. If an error occurs
|
|
|
|
// or any objects fail to upload, they will remain in this._modified. At
|
|
|
|
// the end of a sync, or after an error, we add all objects remaining in
|
|
|
|
// this._modified to the tracker.
|
|
|
|
this.lastSyncLocal = Date.now();
|
2010-11-11 11:00:35 -08:00
|
|
|
if (this.lastSync) {
|
|
|
|
this._modified = this.getChangedIDs();
|
|
|
|
} else {
|
|
|
|
// Mark all items to be uploaded, but treat them as changed from long ago
|
2009-12-17 18:51:55 -08:00
|
|
|
this._log.debug("First sync, uploading all items");
|
2010-11-11 11:00:35 -08:00
|
|
|
this._modified = {};
|
2010-04-01 15:54:53 -07:00
|
|
|
for (let id in this._store.getAllIDs())
|
2010-11-11 11:00:35 -08:00
|
|
|
this._modified[id] = 0;
|
2008-11-08 02:00:33 -08:00
|
|
|
}
|
2010-11-23 21:21:31 -08:00
|
|
|
// Clear the tracker now. If the sync fails we'll add the ones we failed
|
|
|
|
// to upload back.
|
|
|
|
this._tracker.clearChangedIDs();
|
|
|
|
|
|
|
|
// Array of just the IDs from this._modified. This is what we iterate over
|
|
|
|
// so we can modify this._modified during the iteration.
|
|
|
|
this._modifiedIDs = [id for (id in this._modified)];
|
|
|
|
this._log.info(this._modifiedIDs.length +
|
|
|
|
" outgoing items pre-reconciliation");
|
2009-08-31 17:30:34 -07:00
|
|
|
|
|
|
|
// Keep track of what to delete at the end of sync
|
|
|
|
this._delete = {};
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
|
|
|
|
2010-11-09 13:51:19 -08:00
|
|
|
// Process incoming records
|
2008-12-19 11:48:09 -08:00
|
|
|
_processIncoming: function SyncEngine__processIncoming() {
|
2009-12-17 18:51:55 -08:00
|
|
|
this._log.trace("Downloading & applying server changes");
|
2008-11-24 08:04:14 -08:00
|
|
|
|
2010-06-01 15:07:50 -07:00
|
|
|
// Figure out how many total items to fetch this sync; do less on mobile.
|
2010-11-09 13:51:19 -08:00
|
|
|
let batchSize = Infinity;
|
2009-01-06 13:54:18 -08:00
|
|
|
let newitems = new Collection(this.engineURL, this._recordObj);
|
2011-04-06 17:18:22 -07:00
|
|
|
let isMobile = (Svc.Prefs.get("client.type") == "mobile");
|
|
|
|
|
|
|
|
if (isMobile) {
|
2010-11-09 13:51:19 -08:00
|
|
|
batchSize = MOBILE_BATCH_SIZE;
|
2010-07-14 15:18:24 -07:00
|
|
|
}
|
2009-02-10 00:57:16 -08:00
|
|
|
newitems.newer = this.lastSync;
|
2008-11-19 16:20:25 -08:00
|
|
|
newitems.full = true;
|
2010-11-09 13:51:19 -08:00
|
|
|
newitems.limit = batchSize;
|
2011-05-27 17:32:13 -07:00
|
|
|
|
|
|
|
// applied => number of items that should be applied.
|
|
|
|
// failed => number of items that failed in this sync.
|
|
|
|
// newFailed => number of items that failed for the first time in this sync.
|
|
|
|
// reconciled => number of items that were reconciled.
|
|
|
|
let count = {applied: 0, failed: 0, newFailed: 0, reconciled: 0};
|
2009-09-10 23:11:33 -07:00
|
|
|
let handled = [];
|
2011-01-28 09:39:12 -08:00
|
|
|
let applyBatch = [];
|
|
|
|
let failed = [];
|
2011-05-27 17:32:13 -07:00
|
|
|
let failedInPreviousSync = this.previousFailed;
|
|
|
|
let fetchBatch = Utils.arrayUnion(this.toFetch, failedInPreviousSync);
|
|
|
|
// Reset previousFailed for each sync since previously failed items may not fail again.
|
|
|
|
this.previousFailed = [];
|
2011-01-28 09:39:12 -08:00
|
|
|
|
2011-06-15 00:03:32 -07:00
|
|
|
// Used (via exceptions) to allow the record handler/reconciliation/etc.
|
|
|
|
// methods to signal that they would like processing of incoming records to
|
|
|
|
// cease.
|
|
|
|
let aborting = undefined;
|
|
|
|
|
2011-01-28 09:39:12 -08:00
|
|
|
function doApplyBatch() {
|
|
|
|
this._tracker.ignoreAll = true;
|
2011-06-15 00:03:32 -07:00
|
|
|
try {
|
|
|
|
failed = failed.concat(this._store.applyIncomingBatch(applyBatch));
|
|
|
|
} catch (ex) {
|
|
|
|
// Catch any error that escapes from applyIncomingBatch. At present
|
|
|
|
// those will all be abort events.
|
|
|
|
this._log.warn("Got exception " + Utils.exceptionStr(ex) +
|
|
|
|
", aborting processIncoming.");
|
|
|
|
aborting = ex;
|
|
|
|
}
|
2011-01-28 09:39:12 -08:00
|
|
|
this._tracker.ignoreAll = false;
|
|
|
|
applyBatch = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function doApplyBatchAndPersistFailed() {
|
|
|
|
// Apply remaining batch.
|
|
|
|
if (applyBatch.length) {
|
|
|
|
doApplyBatch.call(this);
|
|
|
|
}
|
|
|
|
// Persist failed items so we refetch them.
|
|
|
|
if (failed.length) {
|
2011-05-27 17:32:13 -07:00
|
|
|
this.previousFailed = Utils.arrayUnion(failed, this.previousFailed);
|
2011-01-28 09:39:12 -08:00
|
|
|
count.failed += failed.length;
|
|
|
|
this._log.debug("Records that failed to apply: " + failed);
|
|
|
|
failed = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-02 15:27:16 -08:00
|
|
|
// Not binding this method to 'this' for performance reasons. It gets
|
|
|
|
// called for every incoming record.
|
|
|
|
let self = this;
|
|
|
|
newitems.recordHandler = function(item) {
|
2011-06-15 00:03:32 -07:00
|
|
|
if (aborting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-12 15:18:43 -08:00
|
|
|
// Grab a later last modified if possible
|
2011-03-02 15:27:16 -08:00
|
|
|
if (self.lastModified == null || item.modified > self.lastModified)
|
|
|
|
self.lastModified = item.modified;
|
2009-11-12 15:18:43 -08:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
// Track the collection for the WBO.
|
2011-03-02 15:27:16 -08:00
|
|
|
item.collection = self.name;
|
2010-11-29 16:41:17 -08:00
|
|
|
|
2009-09-10 23:11:33 -07:00
|
|
|
// Remember which records were processed
|
|
|
|
handled.push(item.id);
|
|
|
|
|
2009-01-27 13:35:10 -08:00
|
|
|
try {
|
2010-12-09 23:06:44 -08:00
|
|
|
try {
|
|
|
|
item.decrypt();
|
2011-03-20 16:10:40 -07:00
|
|
|
} catch (ex if Utils.isHMACMismatch(ex)) {
|
|
|
|
let strategy = self.handleHMACMismatch(item, true);
|
|
|
|
if (strategy == SyncEngine.kRecoveryStrategy.retry) {
|
|
|
|
// You only get one retry.
|
|
|
|
try {
|
|
|
|
// Try decrypting again, typically because we've got new keys.
|
|
|
|
self._log.info("Trying decrypt again...");
|
|
|
|
item.decrypt();
|
|
|
|
strategy = null;
|
|
|
|
} catch (ex if Utils.isHMACMismatch(ex)) {
|
|
|
|
strategy = self.handleHMACMismatch(item, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (strategy) {
|
|
|
|
case null:
|
|
|
|
// Retry succeeded! No further handling.
|
|
|
|
break;
|
|
|
|
case SyncEngine.kRecoveryStrategy.retry:
|
|
|
|
self._log.debug("Ignoring second retry suggestion.");
|
|
|
|
// Fall through to error case.
|
|
|
|
case SyncEngine.kRecoveryStrategy.error:
|
|
|
|
self._log.warn("Error decrypting record: " + Utils.exceptionStr(ex));
|
|
|
|
failed.push(item.id);
|
|
|
|
return;
|
|
|
|
case SyncEngine.kRecoveryStrategy.ignore:
|
|
|
|
self._log.debug("Ignoring record " + item.id +
|
|
|
|
" with bad HMAC: already handled.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-01-28 09:39:12 -08:00
|
|
|
} catch (ex) {
|
2011-03-02 15:27:16 -08:00
|
|
|
self._log.warn("Error decrypting record: " + Utils.exceptionStr(ex));
|
2011-01-28 09:39:12 -08:00
|
|
|
failed.push(item.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let shouldApply;
|
|
|
|
try {
|
2011-03-02 15:27:16 -08:00
|
|
|
shouldApply = self._reconcile(item);
|
2011-06-15 00:03:32 -07:00
|
|
|
} catch (ex if (ex.code == Engine.prototype.eEngineAbortApplyIncoming)) {
|
|
|
|
self._log.warn("Reconciliation failed: aborting incoming processing.");
|
|
|
|
failed.push(item.id);
|
|
|
|
aborting = ex.cause;
|
2011-01-28 09:39:12 -08:00
|
|
|
} catch (ex) {
|
2011-03-02 15:27:16 -08:00
|
|
|
self._log.warn("Failed to reconcile incoming record " + item.id);
|
|
|
|
self._log.warn("Encountered exception: " + Utils.exceptionStr(ex));
|
2011-01-28 09:39:12 -08:00
|
|
|
failed.push(item.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldApply) {
|
|
|
|
count.applied++;
|
|
|
|
applyBatch.push(item);
|
|
|
|
} else {
|
|
|
|
count.reconciled++;
|
2011-03-02 15:27:16 -08:00
|
|
|
self._log.trace("Skipping reconciled incoming item " + item.id);
|
2011-01-28 09:39:12 -08:00
|
|
|
}
|
|
|
|
|
2011-03-02 15:27:16 -08:00
|
|
|
if (applyBatch.length == self.applyIncomingBatchSize) {
|
|
|
|
doApplyBatch.call(self);
|
2009-09-10 12:41:38 -07:00
|
|
|
}
|
2011-03-07 13:07:59 -08:00
|
|
|
self._store._sleep(0);
|
2011-03-02 15:27:16 -08:00
|
|
|
};
|
2009-07-22 16:38:34 -07:00
|
|
|
|
2009-09-10 23:11:33 -07:00
|
|
|
// Only bother getting data from the server if there's new things
|
2009-11-12 15:18:43 -08:00
|
|
|
if (this.lastModified == null || this.lastModified > this.lastSync) {
|
2009-09-10 23:11:33 -07:00
|
|
|
let resp = newitems.get();
|
2011-01-28 09:39:12 -08:00
|
|
|
doApplyBatchAndPersistFailed.call(this);
|
2009-09-15 18:38:52 -07:00
|
|
|
if (!resp.success) {
|
|
|
|
resp.failureCode = ENGINE_DOWNLOAD_FAIL;
|
2009-09-10 23:11:33 -07:00
|
|
|
throw resp;
|
2009-09-15 18:38:52 -07:00
|
|
|
}
|
2011-06-15 00:03:32 -07:00
|
|
|
|
|
|
|
if (aborting) {
|
|
|
|
throw aborting;
|
|
|
|
}
|
2009-09-10 23:11:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 13:51:19 -08:00
|
|
|
// Mobile: check if we got the maximum that we requested; get the rest if so.
|
2009-09-10 23:11:33 -07:00
|
|
|
if (handled.length == newitems.limit) {
|
|
|
|
let guidColl = new Collection(this.engineURL);
|
2010-12-06 17:25:35 -08:00
|
|
|
|
|
|
|
// Sort and limit so that on mobile we only get the last X records.
|
|
|
|
guidColl.limit = this.downloadLimit;
|
2009-09-10 23:11:33 -07:00
|
|
|
guidColl.newer = this.lastSync;
|
2011-01-26 21:34:31 -08:00
|
|
|
|
2010-12-06 17:25:35 -08:00
|
|
|
// index: Orders by the sortindex descending (highest weight first).
|
|
|
|
guidColl.sort = "index";
|
2009-09-10 23:11:33 -07:00
|
|
|
|
|
|
|
let guids = guidColl.get();
|
|
|
|
if (!guids.success)
|
|
|
|
throw guids;
|
|
|
|
|
|
|
|
// Figure out which guids weren't just fetched then remove any guids that
|
|
|
|
// were already waiting and prepend the new ones
|
|
|
|
let extra = Utils.arraySub(guids.obj, handled);
|
2011-01-28 09:39:12 -08:00
|
|
|
if (extra.length > 0) {
|
|
|
|
fetchBatch = Utils.arrayUnion(extra, fetchBatch);
|
|
|
|
this.toFetch = Utils.arrayUnion(extra, this.toFetch);
|
|
|
|
}
|
2011-01-26 21:34:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fast-foward the lastSync timestamp since we have stored the
|
|
|
|
// remaining items in toFetch.
|
|
|
|
if (this.lastSync < this.lastModified) {
|
|
|
|
this.lastSync = this.lastModified;
|
2009-09-10 23:11:33 -07:00
|
|
|
}
|
|
|
|
|
2011-04-06 17:18:22 -07:00
|
|
|
// Process any backlog of GUIDs.
|
|
|
|
// At this point we impose an upper limit on the number of items to fetch
|
|
|
|
// in a single request, even for desktop, to avoid hitting URI limits.
|
|
|
|
batchSize = isMobile ? this.mobileGUIDFetchBatchSize :
|
|
|
|
this.guidFetchBatchSize;
|
|
|
|
|
2011-06-15 00:03:32 -07:00
|
|
|
while (fetchBatch.length && !aborting) {
|
2009-09-10 23:11:33 -07:00
|
|
|
// Reuse the original query, but get rid of the restricting params
|
2011-01-26 21:34:31 -08:00
|
|
|
// and batch remaining records.
|
2009-09-10 23:11:33 -07:00
|
|
|
newitems.limit = 0;
|
|
|
|
newitems.newer = 0;
|
2011-01-28 09:39:12 -08:00
|
|
|
newitems.ids = fetchBatch.slice(0, batchSize);
|
2009-09-10 23:11:33 -07:00
|
|
|
|
2009-09-11 08:24:42 -07:00
|
|
|
// Reuse the existing record handler set earlier
|
2009-09-10 23:11:33 -07:00
|
|
|
let resp = newitems.get();
|
2009-09-15 18:38:52 -07:00
|
|
|
if (!resp.success) {
|
|
|
|
resp.failureCode = ENGINE_DOWNLOAD_FAIL;
|
2009-09-10 23:11:33 -07:00
|
|
|
throw resp;
|
2009-09-15 18:38:52 -07:00
|
|
|
}
|
2009-07-22 16:38:34 -07:00
|
|
|
|
2011-01-28 09:39:12 -08:00
|
|
|
// This batch was successfully applied. Not using
|
|
|
|
// doApplyBatchAndPersistFailed() here to avoid writing toFetch twice.
|
|
|
|
fetchBatch = fetchBatch.slice(batchSize);
|
2011-05-27 17:32:13 -07:00
|
|
|
this.toFetch = Utils.arraySub(this.toFetch, newitems.ids);
|
|
|
|
this.previousFailed = Utils.arrayUnion(this.previousFailed, failed);
|
|
|
|
if (failed.length) {
|
|
|
|
count.failed += failed.length;
|
|
|
|
this._log.debug("Records that failed to apply: " + failed);
|
|
|
|
}
|
2011-01-28 09:39:12 -08:00
|
|
|
failed = [];
|
2011-06-15 00:03:32 -07:00
|
|
|
|
|
|
|
if (aborting) {
|
|
|
|
throw aborting;
|
|
|
|
}
|
|
|
|
|
2011-01-26 21:34:31 -08:00
|
|
|
if (this.lastSync < this.lastModified) {
|
|
|
|
this.lastSync = this.lastModified;
|
|
|
|
}
|
|
|
|
}
|
2008-12-30 23:52:20 -08:00
|
|
|
|
2011-01-28 09:39:12 -08:00
|
|
|
// Apply remaining items.
|
|
|
|
doApplyBatchAndPersistFailed.call(this);
|
|
|
|
|
2011-05-27 17:32:13 -07:00
|
|
|
count.newFailed = Utils.arraySub(this.previousFailed, failedInPreviousSync).length;
|
|
|
|
if (count.newFailed) {
|
2011-01-28 09:39:12 -08:00
|
|
|
// Notify observers if records failed to apply. Pass the count object
|
|
|
|
// along so that they can make an informed decision on what to do.
|
|
|
|
Observers.notify("weave:engine:sync:apply-failed", count, this.name);
|
|
|
|
}
|
|
|
|
this._log.info(["Records:",
|
|
|
|
count.applied, "applied,",
|
|
|
|
count.failed, "failed to apply,",
|
2011-05-27 17:32:13 -07:00
|
|
|
count.newFailed, "newly failed to apply,",
|
2011-01-28 09:39:12 -08:00
|
|
|
count.reconciled, "reconciled."].join(" "));
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
|
|
|
|
2009-07-28 10:06:02 -07:00
|
|
|
/**
|
2009-08-31 17:27:30 -07:00
|
|
|
* Find a GUID of an item that is a duplicate of the incoming item but happens
|
|
|
|
* to have a different GUID
|
2009-07-28 10:06:02 -07:00
|
|
|
*
|
2009-08-31 17:27:30 -07:00
|
|
|
* @return GUID of the similar item; falsy otherwise
|
2009-07-28 10:06:02 -07:00
|
|
|
*/
|
2009-08-31 17:27:30 -07:00
|
|
|
_findDupe: function _findDupe(item) {
|
|
|
|
// By default, assume there's no dupe items for the engine
|
2009-07-28 10:06:02 -07:00
|
|
|
},
|
|
|
|
|
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);
|
2010-03-16 16:31:56 -07:00
|
|
|
if (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-08-31 17:51:26 -07:00
|
|
|
_deleteId: function _deleteId(id) {
|
|
|
|
this._tracker.removeChangedID(id);
|
|
|
|
|
|
|
|
// Remember this id to delete at the end of sync
|
|
|
|
if (this._delete.ids == null)
|
|
|
|
this._delete.ids = [id];
|
|
|
|
else
|
|
|
|
this._delete.ids.push(id);
|
|
|
|
},
|
|
|
|
|
2009-09-08 23:33:15 -07:00
|
|
|
_handleDupe: function _handleDupe(item, dupeId) {
|
2010-04-01 15:29:16 -07:00
|
|
|
// Prefer shorter guids; for ties, just do an ASCII compare
|
|
|
|
let preferLocal = dupeId.length < item.id.length ||
|
|
|
|
(dupeId.length == item.id.length && dupeId < item.id);
|
|
|
|
|
|
|
|
if (preferLocal) {
|
|
|
|
this._log.trace("Preferring local id: " + [dupeId, item.id]);
|
2009-09-08 23:33:15 -07:00
|
|
|
this._deleteId(item.id);
|
|
|
|
item.id = dupeId;
|
2010-04-01 15:54:53 -07:00
|
|
|
this._tracker.addChangedID(dupeId, 0);
|
2009-09-08 23:33:15 -07:00
|
|
|
}
|
|
|
|
else {
|
2010-04-01 15:29:16 -07:00
|
|
|
this._log.trace("Switching local id to incoming: " + [item.id, dupeId]);
|
2009-09-08 23:33:15 -07:00
|
|
|
this._store.changeItemID(dupeId, item.id);
|
|
|
|
this._deleteId(dupeId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-06-01 15:07:50 -07:00
|
|
|
// Reconcile incoming and existing records. Return true if server
|
|
|
|
// data should be applied.
|
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);
|
|
|
|
|
2010-04-01 15:54:53 -07:00
|
|
|
this._log.trace("Reconcile step 1: Check for conflicts");
|
2010-11-11 11:00:35 -08:00
|
|
|
if (item.id in this._modified) {
|
2010-04-01 15:54:53 -07:00
|
|
|
// If the incoming and local changes are the same, skip
|
|
|
|
if (this._isEqual(item)) {
|
2010-11-23 21:21:31 -08:00
|
|
|
delete this._modified[item.id];
|
2010-04-01 15:54:53 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Records differ so figure out which to take
|
2011-04-25 13:17:26 -07:00
|
|
|
let recordAge = AsyncResource.serverTime - item.modified;
|
2010-11-11 11:00:35 -08:00
|
|
|
let localAge = Date.now() / 1000 - this._modified[item.id];
|
2010-04-01 15:54:53 -07:00
|
|
|
this._log.trace("Record age vs local age: " + [recordAge, localAge]);
|
|
|
|
|
|
|
|
// Apply the record if the record is newer (server wins)
|
|
|
|
return recordAge < localAge;
|
2008-12-29 23:28:17 -08:00
|
|
|
}
|
|
|
|
|
2010-04-01 15:54:53 -07:00
|
|
|
this._log.trace("Reconcile step 2: Check for updates");
|
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
|
|
|
|
2010-04-01 15:54:53 -07:00
|
|
|
this._log.trace("Reconcile step 2.5: Don't dupe deletes");
|
2009-06-04 14:18:04 -07:00
|
|
|
if (item.deleted)
|
|
|
|
return true;
|
2009-01-08 21:33:37 -08:00
|
|
|
|
2010-04-01 15:54:53 -07:00
|
|
|
this._log.trace("Reconcile step 3: Find dupes");
|
2009-08-31 17:27:30 -07:00
|
|
|
let dupeId = this._findDupe(item);
|
2009-09-08 23:33:15 -07:00
|
|
|
if (dupeId)
|
|
|
|
this._handleDupe(item, dupeId);
|
2009-06-04 14:18:04 -07:00
|
|
|
|
2009-08-31 17:27:30 -07:00
|
|
|
// Apply the incoming item (now that the dupe is the right id)
|
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
|
|
|
// Upload outgoing records
|
|
|
|
_uploadOutgoing: function SyncEngine__uploadOutgoing() {
|
2011-03-02 15:27:16 -08:00
|
|
|
this._log.trace("Uploading local changes to server.");
|
2010-11-23 21:21:31 -08:00
|
|
|
if (this._modifiedIDs.length) {
|
|
|
|
this._log.trace("Preparing " + this._modifiedIDs.length +
|
|
|
|
" outgoing records");
|
2009-09-10 22:57:36 -07:00
|
|
|
|
2008-12-23 11:30:31 -08:00
|
|
|
// collection we'll upload
|
2009-11-10 15:52:40 -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) {
|
2010-11-23 21:21:31 -08:00
|
|
|
this._log.info("Uploading " + desc + " of " +
|
|
|
|
this._modifiedIDs.length + " records");
|
2009-08-26 15:32:46 -07:00
|
|
|
let resp = up.post();
|
2009-09-15 18:38:52 -07:00
|
|
|
if (!resp.success) {
|
|
|
|
this._log.debug("Uploading records failed: " + resp);
|
|
|
|
resp.failureCode = ENGINE_UPLOAD_FAIL;
|
2009-08-26 15:32:46 -07:00
|
|
|
throw resp;
|
2009-09-15 18:38:52 -07:00
|
|
|
}
|
2009-08-25 18:04:46 -07:00
|
|
|
|
2010-11-11 11:00:35 -08:00
|
|
|
// Update server timestamp from the upload.
|
2010-06-01 15:15:53 -07:00
|
|
|
let modified = resp.headers["x-weave-timestamp"];
|
2009-08-25 18:04:46 -07:00
|
|
|
if (modified > this.lastSync)
|
|
|
|
this.lastSync = modified;
|
|
|
|
|
2010-11-23 21:21:31 -08:00
|
|
|
let failed_ids = [id for (id in resp.obj.failed)];
|
2010-07-31 04:28:00 -07:00
|
|
|
if (failed_ids.length)
|
|
|
|
this._log.debug("Records that will be uploaded again because "
|
|
|
|
+ "the server couldn't store them: "
|
|
|
|
+ failed_ids.join(", "));
|
|
|
|
|
2010-11-23 21:21:31 -08:00
|
|
|
// Clear successfully uploaded objects.
|
|
|
|
for each (let id in resp.obj.success) {
|
|
|
|
delete this._modified[id];
|
|
|
|
}
|
|
|
|
|
2009-08-13 18:50:54 -07:00
|
|
|
up.clearRecords();
|
|
|
|
});
|
2008-12-23 11:30:31 -08:00
|
|
|
|
2010-11-23 21:21:31 -08:00
|
|
|
for each (let id in this._modifiedIDs) {
|
2010-01-06 09:59:05 -08:00
|
|
|
try {
|
|
|
|
let out = this._createRecord(id);
|
|
|
|
if (this._log.level <= Log4Moz.Level.Trace)
|
|
|
|
this._log.trace("Outgoing: " + out);
|
2009-07-22 16:21:33 -07:00
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
out.encrypt();
|
2010-01-06 09:59:05 -08:00
|
|
|
up.pushData(out);
|
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
this._log.warn("Error creating record: " + Utils.exceptionStr(ex));
|
|
|
|
}
|
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
|
|
|
|
2011-03-07 13:07:59 -08:00
|
|
|
this._store._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-11-08 02:00:33 -08:00
|
|
|
}
|
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();
|
2009-08-31 17:30:34 -07:00
|
|
|
|
2009-10-12 18:11:31 -07:00
|
|
|
let doDelete = Utils.bind2(this, function(key, val) {
|
|
|
|
let coll = new Collection(this.engineURL, this._recordObj);
|
|
|
|
coll[key] = val;
|
|
|
|
coll.delete();
|
|
|
|
});
|
|
|
|
|
2009-08-31 17:30:34 -07:00
|
|
|
for (let [key, val] in Iterator(this._delete)) {
|
|
|
|
// Remove the key for future uses
|
|
|
|
delete this._delete[key];
|
|
|
|
|
2009-10-12 18:11:31 -07:00
|
|
|
// Send a simple delete for the property
|
|
|
|
if (key != "ids" || val.length <= 100)
|
|
|
|
doDelete(key, val);
|
|
|
|
else {
|
|
|
|
// For many ids, split into chunks of at most 100
|
|
|
|
while (val.length > 0) {
|
|
|
|
doDelete(key, val.slice(0, 100));
|
|
|
|
val = val.slice(100);
|
|
|
|
}
|
|
|
|
}
|
2009-08-31 17:30:34 -07:00
|
|
|
}
|
2008-11-06 19:18:07 -08:00
|
|
|
},
|
|
|
|
|
2010-11-23 21:21:31 -08:00
|
|
|
_syncCleanup: function _syncCleanup() {
|
|
|
|
if (!this._modified)
|
2010-11-11 11:00:35 -08:00
|
|
|
return;
|
|
|
|
|
2010-11-23 21:21:31 -08:00
|
|
|
// Mark failed WBOs as changed again so they are reuploaded next time.
|
|
|
|
for (let [id, when] in Iterator(this._modified)) {
|
2010-11-11 11:00:35 -08:00
|
|
|
this._tracker.addChangedID(id, when);
|
|
|
|
}
|
2010-11-23 21:21:31 -08:00
|
|
|
delete this._modified;
|
|
|
|
delete this._modifiedIDs;
|
2010-11-11 11:00:35 -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();
|
2010-11-23 21:21:31 -08:00
|
|
|
} finally {
|
|
|
|
this._syncCleanup();
|
2008-12-05 00:55:19 -08:00
|
|
|
}
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
2008-08-08 14:42:57 -07:00
|
|
|
|
2010-10-05 11:32:56 -07:00
|
|
|
canDecrypt: function canDecrypt() {
|
2009-10-12 16:22:54 -07:00
|
|
|
// Report failure even if there's nothing to decrypt
|
|
|
|
let canDecrypt = false;
|
|
|
|
|
|
|
|
// Fetch the most recently uploaded record and try to decrypt it
|
|
|
|
let test = new Collection(this.engineURL, this._recordObj);
|
|
|
|
test.limit = 1;
|
|
|
|
test.sort = "newest";
|
|
|
|
test.full = true;
|
|
|
|
test.recordHandler = function(record) {
|
2010-11-29 16:41:17 -08:00
|
|
|
record.decrypt();
|
2009-10-12 16:22:54 -07:00
|
|
|
canDecrypt = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Any failure fetching/decrypting will just result in false
|
|
|
|
try {
|
|
|
|
this._log.trace("Trying to decrypt a record from the server..");
|
|
|
|
test.get();
|
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
this._log.debug("Failed test decrypt: " + Utils.exceptionStr(ex));
|
|
|
|
}
|
|
|
|
|
|
|
|
return canDecrypt;
|
|
|
|
},
|
|
|
|
|
2009-02-26 22:36:14 -08:00
|
|
|
_resetClient: function SyncEngine__resetClient() {
|
|
|
|
this.resetLastSync();
|
2011-05-27 17:32:13 -07:00
|
|
|
this.previousFailed = [];
|
2011-01-26 21:34:31 -08:00
|
|
|
this.toFetch = [];
|
2010-09-07 09:44:01 -07:00
|
|
|
},
|
|
|
|
|
2010-11-29 16:41:17 -08:00
|
|
|
wipeServer: function wipeServer() {
|
2010-09-07 09:44:01 -07:00
|
|
|
new Resource(this.engineURL).delete();
|
|
|
|
this._resetClient();
|
2010-12-09 23:06:44 -08:00
|
|
|
},
|
2011-03-20 16:10:40 -07:00
|
|
|
|
2011-04-08 14:51:55 -07:00
|
|
|
removeClientData: function removeClientData() {
|
|
|
|
// Implement this method in engines that store client specific data
|
|
|
|
// on the server.
|
|
|
|
},
|
|
|
|
|
2011-03-20 16:10:40 -07:00
|
|
|
/*
|
|
|
|
* Decide on (and partially effect) an error-handling strategy.
|
|
|
|
*
|
|
|
|
* Asks the Service to respond to an HMAC error, which might result in keys
|
|
|
|
* being downloaded. That call returns true if an action which might allow a
|
|
|
|
* retry to occur.
|
|
|
|
*
|
|
|
|
* If `mayRetry` is truthy, and the Service suggests a retry,
|
|
|
|
* handleHMACMismatch returns kRecoveryStrategy.retry. Otherwise, it returns
|
|
|
|
* kRecoveryStrategy.error.
|
|
|
|
*
|
|
|
|
* Subclasses of SyncEngine can override this method to allow for different
|
|
|
|
* behavior -- e.g., to delete and ignore erroneous entries.
|
|
|
|
*
|
|
|
|
* All return values will be part of the kRecoveryStrategy enumeration.
|
|
|
|
*/
|
|
|
|
handleHMACMismatch: function handleHMACMismatch(item, mayRetry) {
|
|
|
|
// By default we either try again, or bail out noisily.
|
|
|
|
return (Weave.Service.handleHMACEvent() && mayRetry) ?
|
|
|
|
SyncEngine.kRecoveryStrategy.retry :
|
|
|
|
SyncEngine.kRecoveryStrategy.error;
|
2008-07-25 01:06:23 -07:00
|
|
|
}
|
|
|
|
};
|