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");
|
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,
|
|
|
|
|
2008-12-08 09:53:32 -08:00
|
|
|
// _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-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-12-09 12:26:14 -08:00
|
|
|
this._log = Log4Moz.repository.getLogger("Engine." + 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] != '/')
|
2008-12-23 13:51:30 -08:00
|
|
|
url += '/';
|
|
|
|
url += "0.3/user/";
|
2008-12-05 00:39:54 -08:00
|
|
|
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-28 19:59:44 -08:00
|
|
|
// Create a new record by querying the store, and add the engine metadata
|
|
|
|
_createRecord: function SyncEngine__createRecord(id) {
|
|
|
|
let record = this._store.createRecord(id);
|
2008-12-05 00:39:54 -08:00
|
|
|
record.uri = this.engineURL + id;
|
|
|
|
record.encryption = this.cryptoMetaURL;
|
2008-12-28 19:59:44 -08:00
|
|
|
return record;
|
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
|
|
|
if (a.depth != b.depth)
|
|
|
|
return false;
|
|
|
|
// note: sortindex ignored
|
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
|
|
|
_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: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-08 09:53:32 -08:00
|
|
|
let all = this._store.getAllIDs();
|
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
|
2008-12-08 09:53:32 -08:00
|
|
|
|
|
|
|
// XXX should have a heuristic like this, but then we need to be able to
|
|
|
|
// serialize each item by itself, something our stores can't currently do
|
|
|
|
//if (this._tracker.changedIDs.length >= 30)
|
2008-12-28 19:59:44 -08:00
|
|
|
//this._store.cacheItemsHint();
|
2008-12-08 09:53:32 -08:00
|
|
|
|
2008-12-05 00:39:54 -08:00
|
|
|
// 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) {
|
2008-12-28 19:59:44 -08:00
|
|
|
this.outgoing.push(this._createRecord(id));
|
2008-12-05 00:39:54 -08:00
|
|
|
}
|
2008-12-08 09:53:32 -08:00
|
|
|
|
2008-12-28 19:59:44 -08:00
|
|
|
//this._store.clearItemCacheHint();
|
2008-12-05 00:39:54 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Generate outgoing records
|
2008-12-19 11:48:09 -08:00
|
|
|
_processIncoming: function SyncEngine__processIncoming() {
|
2008-12-05 00:39:54 -08:00
|
|
|
let self = yield;
|
|
|
|
|
2008-12-19 11:48:09 -08:00
|
|
|
this._log.debug("Downloading & applying 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;
|
2008-12-19 11:48:09 -08:00
|
|
|
newitems.sort = "depthindex";
|
2008-11-19 16:20:25 -08:00
|
|
|
yield newitems.get(self.cb);
|
|
|
|
|
2008-12-26 21:49:17 -08:00
|
|
|
let mem = Cc["@mozilla.org/xpcom/memory-service;1"].getService(Ci.nsIMemory);
|
2008-12-19 11:48:09 -08:00
|
|
|
this._lastSyncTmp = 0;
|
2008-11-19 16:20:25 -08:00
|
|
|
let item;
|
2008-11-24 21:48:22 -08:00
|
|
|
while ((item = yield newitems.iter.next(self.cb))) {
|
2008-12-26 21:49:17 -08:00
|
|
|
if (mem.isLowMemory()) {
|
|
|
|
this._log.warn("Low memory, forcing GC");
|
|
|
|
Cu.forceGC();
|
|
|
|
if (mem.isLowMemory()) {
|
|
|
|
this._log.warn("Low memory, aborting sync!");
|
|
|
|
throw "Low memory";
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 11:48:09 -08:00
|
|
|
yield item.decrypt(self.cb, ID.get('WeaveCryptoID').password);
|
|
|
|
if (yield this._reconcile.async(this, self.cb, item))
|
|
|
|
yield this._applyIncoming.async(this, self.cb, item);
|
|
|
|
else
|
|
|
|
this._log.debug("Skipping reconciled incoming item");
|
2008-11-19 16:20:25 -08:00
|
|
|
}
|
2008-12-23 11:30:31 -08:00
|
|
|
if (typeof(this._lastSyncTmp) == "string")
|
|
|
|
this._lastSyncTmp = parseInt(this._lastSyncTmp);
|
2008-12-19 11:48:09 -08:00
|
|
|
if (this.lastSync < this._lastSyncTmp)
|
|
|
|
this.lastSync = this._lastSyncTmp;
|
2008-12-05 00:39:54 -08:00
|
|
|
|
2008-12-19 11:48:09 -08:00
|
|
|
// removes any holes caused by reconciliation above:
|
|
|
|
this._outgoing = this.outgoing.filter(function(n) n);
|
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.
|
2008-12-19 11:48:09 -08:00
|
|
|
_reconcile: function SyncEngine__reconcile(item) {
|
2008-12-05 00:39:54 -08:00
|
|
|
let self = yield;
|
2008-12-19 11:48:09 -08:00
|
|
|
let ret = true;
|
2008-11-24 08:04:14 -08:00
|
|
|
|
2008-12-19 11:48:09 -08:00
|
|
|
this._log.debug("Reconciling incoming item");
|
2008-12-05 03:28:17 -08:00
|
|
|
|
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 = [];
|
2008-12-19 11:48:09 -08:00
|
|
|
for (let o = 0; o < this.outgoing.length; o++) {
|
|
|
|
if (!this.outgoing[o])
|
|
|
|
continue; // skip previously removed items
|
|
|
|
if (item.id == this.outgoing[o].id) {
|
|
|
|
// Only consider it a conflict if there are actual differences
|
|
|
|
// otherwise, just ignore the outgoing record as well
|
|
|
|
if (!Utils.deepEquals(item.cleartext, this.outgoing[o].cleartext))
|
|
|
|
conflicts.push({in: item, out: this.outgoing[o]});
|
|
|
|
else
|
|
|
|
delete this.outgoing[o];
|
|
|
|
|
|
|
|
self.done(false);
|
|
|
|
return;
|
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-12-19 11:48:09 -08:00
|
|
|
for (let o = 0; o < this.outgoing.length; o++) {
|
|
|
|
if (!this.outgoing[o])
|
|
|
|
continue; // skip previously removed items
|
|
|
|
|
|
|
|
if (this._recordLike(item, this.outgoing[o])) {
|
|
|
|
// change refs in outgoing queue
|
|
|
|
yield this._changeRecordRefs.async(this, self.cb,
|
|
|
|
this.outgoing[o].id,
|
|
|
|
item.id);
|
|
|
|
// change actual id of item
|
|
|
|
this._store.changeItemID(this.outgoing[o].id,
|
|
|
|
item.id);
|
|
|
|
delete this.outgoing[o];
|
|
|
|
self.done(false);
|
|
|
|
return;
|
2008-11-26 07:25:28 -08:00
|
|
|
}
|
|
|
|
}
|
2008-12-19 11:48:09 -08:00
|
|
|
self.done(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) {
|
2008-12-05 00:39:54 -08:00
|
|
|
let self = yield;
|
2008-12-19 11:48:09 -08:00
|
|
|
this._log.debug("Applying incoming record");
|
2008-12-23 11:30:31 -08:00
|
|
|
this._log.trace("Incoming:\n" + item);
|
2008-12-19 11:48:09 -08:00
|
|
|
try {
|
|
|
|
yield this._store.applyIncoming(self.cb, 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) {
|
|
|
|
this._log.warn("Error while applying incoming record: " +
|
|
|
|
(e.message? e.message : e));
|
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-12-23 11:30:31 -08:00
|
|
|
|
2008-11-24 08:04:14 -08:00
|
|
|
if (this.outgoing.length) {
|
2008-12-23 11:30:31 -08:00
|
|
|
this._log.debug("Uploading client changes (" + this.outgoing.length + ")");
|
|
|
|
|
|
|
|
// collection we'll upload
|
2008-11-24 08:04:14 -08:00
|
|
|
let up = new Collection(this.engineURL);
|
2008-12-23 11:30:31 -08:00
|
|
|
|
|
|
|
// regen the store cache so we can get item depths
|
2008-12-28 19:59:44 -08:00
|
|
|
//this._store.cacheItemsHint();
|
2008-12-23 11:30:31 -08:00
|
|
|
let depth = {};
|
|
|
|
|
2008-11-24 08:04:14 -08:00
|
|
|
let out;
|
|
|
|
while ((out = this.outgoing.pop())) {
|
2008-12-23 11:30:31 -08:00
|
|
|
this._log.trace("Outgoing:\n" + out);
|
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);
|
2008-12-23 11:30:31 -08:00
|
|
|
this._store.wrapDepth(out.id, depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now add short depth-only records
|
|
|
|
this._log.trace(depth.length + "outgoing depth records");
|
|
|
|
for (let id in depth) {
|
|
|
|
up.pushDepthRecord({id: id, depth: depth[id]});
|
2008-11-24 08:04:14 -08:00
|
|
|
}
|
2008-12-28 19:59:44 -08:00
|
|
|
//this._store.clearItemCacheHint();
|
2008-12-23 11:30:31 -08:00
|
|
|
|
|
|
|
// do the upload
|
2008-11-24 08:04:14 -08:00
|
|
|
yield up.post(self.cb);
|
2008-12-23 11:30:31 -08:00
|
|
|
|
|
|
|
// save last modified date
|
|
|
|
let mod = up.data.modified;
|
|
|
|
if (typeof(mod) == "string")
|
|
|
|
mod = parseInt(mod);
|
|
|
|
if (mod > this.lastSync)
|
|
|
|
this.lastSync = mod;
|
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-19 11:48:09 -08:00
|
|
|
// Populate outgoing queue
|
2008-12-05 00:55:19 -08:00
|
|
|
yield this._generateOutgoing.async(this, self.cb);
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-12-19 11:48:09 -08:00
|
|
|
// Fetch incoming records and apply them
|
2008-12-05 00:55:19 -08:00
|
|
|
yield this._processIncoming.async(this, self.cb);
|
2008-03-19 15:17:04 -07:00
|
|
|
|
2008-12-19 11:48:09 -08:00
|
|
|
// Upload outgoing records
|
2008-12-05 00:55:19 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
};
|