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>
|
|
|
|
*
|
|
|
|
* 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-05-21 18:09:19 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['SyncCore', 'BookmarksSyncCore', 'HistorySyncCore',
|
2008-05-22 15:36:44 -07:00
|
|
|
'CookieSyncCore', 'PasswordSyncCore', 'FormSyncCore'];
|
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-03-07 01:56:36 -08:00
|
|
|
Cu.import("resource://weave/async.js");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
Function.prototype.async = Async.sugar;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SyncCore objects
|
|
|
|
* Sync cores deal with diff creation and conflict resolution.
|
|
|
|
* Tree data structures where all nodes have GUIDs only need to be
|
|
|
|
* subclassed for each data type to implement commandLike and
|
|
|
|
* itemExists.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function SyncCore() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
SyncCore.prototype = {
|
|
|
|
_logName: "Sync",
|
|
|
|
|
|
|
|
_init: function SC__init() {
|
|
|
|
this._log = Log4Moz.Service.getLogger("Service." + this._logName);
|
|
|
|
},
|
|
|
|
|
2008-05-23 11:05:42 -07:00
|
|
|
// FIXME: this won't work for deep objects, or objects with optional
|
|
|
|
// properties
|
2007-12-10 21:38:53 -08:00
|
|
|
_getEdits: function SC__getEdits(a, b) {
|
|
|
|
let ret = {numProps: 0, props: {}};
|
|
|
|
for (prop in a) {
|
2008-02-13 14:30:44 -08:00
|
|
|
if (!Utils.deepEquals(a[prop], b[prop])) {
|
2007-12-10 21:38:53 -08:00
|
|
|
ret.numProps++;
|
|
|
|
ret.props[prop] = b[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
|
|
|
|
_nodeParents: function SC__nodeParents(GUID, tree) {
|
|
|
|
return this._nodeParentsInt(GUID, tree, []);
|
|
|
|
},
|
|
|
|
|
|
|
|
_nodeParentsInt: function SC__nodeParentsInt(GUID, tree, parents) {
|
|
|
|
if (!tree[GUID] || !tree[GUID].parentGUID)
|
|
|
|
return parents;
|
|
|
|
parents.push(tree[GUID].parentGUID);
|
|
|
|
return this._nodeParentsInt(tree[GUID].parentGUID, tree, parents);
|
|
|
|
},
|
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
_detectUpdates: function SC__detectUpdates(a, b) {
|
|
|
|
let self = yield;
|
|
|
|
let listener = new Utils.EventListener(self.cb);
|
2007-12-10 21:38:53 -08:00
|
|
|
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
|
|
|
|
let cmds = [];
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (let GUID in a) {
|
|
|
|
|
|
|
|
timer.initWithCallback(listener, 0, timer.TYPE_ONE_SHOT);
|
|
|
|
yield; // Yield to main loop
|
|
|
|
|
|
|
|
if (GUID in b) {
|
|
|
|
let edits = this._getEdits(a[GUID], b[GUID]);
|
|
|
|
if (edits.numProps == 0) // no changes - skip
|
|
|
|
continue;
|
|
|
|
let parents = this._nodeParents(GUID, b);
|
|
|
|
cmds.push({action: "edit", GUID: GUID,
|
|
|
|
depth: parents.length, parents: parents,
|
|
|
|
data: edits.props});
|
|
|
|
} else {
|
|
|
|
let parents = this._nodeParents(GUID, a); // ???
|
|
|
|
cmds.push({action: "remove", GUID: GUID,
|
|
|
|
depth: parents.length, parents: parents});
|
|
|
|
}
|
|
|
|
}
|
2008-03-07 01:56:36 -08:00
|
|
|
|
2008-05-23 11:05:42 -07:00
|
|
|
for (GUID in b) {
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
timer.initWithCallback(listener, 0, timer.TYPE_ONE_SHOT);
|
|
|
|
yield; // Yield to main loop
|
|
|
|
|
|
|
|
if (GUID in a)
|
|
|
|
continue;
|
|
|
|
let parents = this._nodeParents(GUID, b);
|
|
|
|
cmds.push({action: "create", GUID: GUID,
|
|
|
|
depth: parents.length, parents: parents,
|
|
|
|
data: b[GUID]});
|
|
|
|
}
|
|
|
|
cmds.sort(function(a, b) {
|
|
|
|
if (a.depth > b.depth)
|
|
|
|
return 1;
|
|
|
|
if (a.depth < b.depth)
|
|
|
|
return -1;
|
|
|
|
if (a.index > b.index)
|
|
|
|
return -1;
|
|
|
|
if (a.index < b.index)
|
|
|
|
return 1;
|
|
|
|
return 0; // should never happen, but not a big deal if it does
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (e) {
|
2008-03-07 01:56:36 -08:00
|
|
|
throw e;
|
2007-12-10 21:38:53 -08:00
|
|
|
|
|
|
|
} finally {
|
|
|
|
timer = null;
|
2008-03-07 01:56:36 -08:00
|
|
|
self.done(cmds);
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_commandLike: function SC__commandLike(a, b) {
|
|
|
|
this._log.error("commandLike needs to be subclassed");
|
|
|
|
|
|
|
|
// Check that neither command is null, and verify that the GUIDs
|
|
|
|
// are different (otherwise we need to check for edits)
|
|
|
|
if (!a || !b || a.GUID == b.GUID)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that all other properties are the same
|
|
|
|
// FIXME: could be optimized...
|
|
|
|
for (let key in a) {
|
2008-02-13 14:30:44 -08:00
|
|
|
if (key != "GUID" && !Utils.deepEquals(a[key], b[key]))
|
2007-12-10 21:38:53 -08:00
|
|
|
return false;
|
|
|
|
}
|
2008-05-23 11:05:42 -07:00
|
|
|
for (key in b) {
|
2008-02-13 14:30:44 -08:00
|
|
|
if (key != "GUID" && !Utils.deepEquals(a[key], b[key]))
|
2007-12-10 21:38:53 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
// When we change the GUID of a local item (because we detect it as
|
|
|
|
// being the same item as a remote one), we need to fix any other
|
|
|
|
// local items that have it as their parent
|
|
|
|
_fixParents: function SC__fixParents(list, oldGUID, newGUID) {
|
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
if (!list[i])
|
|
|
|
continue;
|
2008-03-30 03:36:25 -07:00
|
|
|
if (list[i].data && list[i].data.parentGUID == oldGUID)
|
2007-12-10 21:38:53 -08:00
|
|
|
list[i].data.parentGUID = newGUID;
|
|
|
|
for (let j = 0; j < list[i].parents.length; j++) {
|
|
|
|
if (list[i].parents[j] == oldGUID)
|
|
|
|
list[i].parents[j] = newGUID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_conflicts: function SC__conflicts(a, b) {
|
2008-02-13 14:30:44 -08:00
|
|
|
if ((a.GUID == b.GUID) && !Utils.deepEquals(a, b))
|
2007-12-10 21:38:53 -08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
_getPropagations: function SC__getPropagations(commands, conflicts, propagations) {
|
|
|
|
for (let i = 0; i < commands.length; i++) {
|
|
|
|
let alsoConflicts = function(elt) {
|
|
|
|
return (elt.action == "create" || elt.action == "remove") &&
|
|
|
|
commands[i].parents.indexOf(elt.GUID) >= 0;
|
|
|
|
};
|
|
|
|
if (conflicts.some(alsoConflicts))
|
|
|
|
conflicts.push(commands[i]);
|
|
|
|
|
|
|
|
let cmdConflicts = function(elt) {
|
|
|
|
return elt.GUID == commands[i].GUID;
|
|
|
|
};
|
|
|
|
if (!conflicts.some(cmdConflicts))
|
|
|
|
propagations.push(commands[i]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_itemExists: function SC__itemExists(GUID) {
|
|
|
|
this._log.error("itemExists needs to be subclassed");
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2008-03-07 01:56:36 -08:00
|
|
|
_reconcile: function SC__reconcile(listA, listB) {
|
|
|
|
let self = yield;
|
|
|
|
let listener = new Utils.EventListener(self.cb);
|
2007-12-10 21:38:53 -08:00
|
|
|
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
|
|
|
|
let propagations = [[], []];
|
|
|
|
let conflicts = [[], []];
|
|
|
|
let ret = {propagations: propagations, conflicts: conflicts};
|
|
|
|
this._log.debug("Reconciling " + listA.length +
|
2008-06-02 15:24:52 -07:00
|
|
|
" against " + listB.length + " commands");
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-30 03:36:25 -07:00
|
|
|
let guidChanges = [];
|
|
|
|
for (let i = 0; i < listA.length; i++) {
|
|
|
|
let a = listA[i];
|
|
|
|
timer.initWithCallback(listener, 0, timer.TYPE_ONE_SHOT);
|
|
|
|
yield; // Yield to main loop
|
|
|
|
|
|
|
|
//this._log.debug("comparing " + i + ", listB length: " + listB.length);
|
|
|
|
|
|
|
|
let skip = false;
|
|
|
|
listB = listB.filter(function(b) {
|
|
|
|
// fast path for when we already found a matching command
|
|
|
|
if (skip)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (Utils.deepEquals(a, b)) {
|
|
|
|
delete listA[i]; // a
|
|
|
|
skip = true;
|
|
|
|
return false; // b
|
|
|
|
|
|
|
|
} else if (this._commandLike(a, b)) {
|
|
|
|
this._fixParents(listA, a.GUID, b.GUID);
|
|
|
|
guidChanges.push({action: "edit",
|
|
|
|
GUID: a.GUID,
|
|
|
|
data: {GUID: b.GUID}});
|
|
|
|
delete listA[i]; // a
|
|
|
|
skip = true;
|
|
|
|
return false; // b, but we add it back from guidChanges
|
|
|
|
}
|
|
|
|
|
|
|
|
// watch out for create commands with GUIDs that already exist
|
|
|
|
if (b.action == "create" && this._itemExists(b.GUID)) {
|
|
|
|
this._log.error("Remote command has GUID that already exists " +
|
|
|
|
"locally. Dropping command.");
|
|
|
|
return false; // delete b
|
|
|
|
}
|
|
|
|
return true; // keep b
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
listA = listA.filter(function(elt) { return elt });
|
|
|
|
listB = guidChanges.concat(listB);
|
|
|
|
|
|
|
|
for (let i = 0; i < listA.length; i++) {
|
|
|
|
for (let j = 0; j < listB.length; j++) {
|
|
|
|
|
2007-12-10 21:38:53 -08:00
|
|
|
timer.initWithCallback(listener, 0, timer.TYPE_ONE_SHOT);
|
|
|
|
yield; // Yield to main loop
|
|
|
|
|
2008-03-30 03:36:25 -07:00
|
|
|
if (this._conflicts(listA[i], listB[j]) ||
|
|
|
|
this._conflicts(listB[j], listA[i])) {
|
|
|
|
if (!conflicts[0].some(
|
|
|
|
function(elt) { return elt.GUID == listA[i].GUID }))
|
|
|
|
conflicts[0].push(listA[i]);
|
|
|
|
if (!conflicts[1].some(
|
|
|
|
function(elt) { return elt.GUID == listB[j].GUID }))
|
|
|
|
conflicts[1].push(listB[j]);
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
|
|
|
}
|
2008-03-30 03:36:25 -07:00
|
|
|
}
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-30 03:36:25 -07:00
|
|
|
this._getPropagations(listA, conflicts[0], propagations[1]);
|
2007-12-10 21:38:53 -08:00
|
|
|
|
2008-03-30 03:36:25 -07:00
|
|
|
timer.initWithCallback(listener, 0, timer.TYPE_ONE_SHOT);
|
|
|
|
yield; // Yield to main loop
|
|
|
|
|
|
|
|
this._getPropagations(listB, conflicts[1], propagations[0]);
|
|
|
|
ret = {propagations: propagations, conflicts: conflicts};
|
|
|
|
|
|
|
|
timer = null;
|
|
|
|
self.done(ret);
|
2007-12-10 21:38:53 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
// Public methods
|
|
|
|
|
|
|
|
detectUpdates: function SC_detectUpdates(onComplete, a, b) {
|
|
|
|
return this._detectUpdates.async(this, onComplete, a, b);
|
|
|
|
},
|
|
|
|
|
|
|
|
reconcile: function SC_reconcile(onComplete, listA, listB) {
|
|
|
|
return this._reconcile.async(this, onComplete, listA, listB);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function BookmarksSyncCore() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
BookmarksSyncCore.prototype = {
|
|
|
|
_logName: "BMSync",
|
|
|
|
|
|
|
|
__bms: null,
|
|
|
|
get _bms() {
|
|
|
|
if (!this.__bms)
|
|
|
|
this.__bms = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
|
|
|
|
getService(Ci.nsINavBookmarksService);
|
|
|
|
return this.__bms;
|
|
|
|
},
|
|
|
|
|
|
|
|
_itemExists: function BSC__itemExists(GUID) {
|
|
|
|
return this._bms.getItemIdForGUID(GUID) >= 0;
|
|
|
|
},
|
|
|
|
|
2008-03-19 17:31:00 -07:00
|
|
|
_getEdits: function BSC__getEdits(a, b) {
|
|
|
|
// NOTE: we do not increment ret.numProps, as that would cause
|
|
|
|
// edit commands to always get generated
|
|
|
|
let ret = SyncCore.prototype._getEdits.call(this, a, b);
|
|
|
|
ret.props.type = a.type;
|
|
|
|
return ret;
|
|
|
|
},
|
|
|
|
|
|
|
|
// compares properties
|
|
|
|
// returns true if the property is not set in either object
|
|
|
|
// returns true if the property is set and equal in both objects
|
|
|
|
// returns false otherwise
|
|
|
|
_comp: function BSC__comp(a, b, prop) {
|
|
|
|
return (!a.data[prop] && !b.data[prop]) ||
|
|
|
|
(a.data[prop] && b.data[prop] && (a.data[prop] == b.data[prop]));
|
|
|
|
},
|
|
|
|
|
|
|
|
_commandLike: function BSC__commandLike(a, b) {
|
2007-12-10 21:38:53 -08:00
|
|
|
// Check that neither command is null, that their actions, types,
|
|
|
|
// and parents are the same, and that they don't have the same
|
|
|
|
// GUID.
|
2008-03-30 03:36:25 -07:00
|
|
|
// * Items with the same GUID do not qualify for 'likeness' because
|
|
|
|
// we already consider them to be the same object, and therefore
|
|
|
|
// we need to process any edits.
|
|
|
|
// * Remove or edit commands don't qualify for likeness either,
|
|
|
|
// since remove or edit commands with different GUIDs are
|
|
|
|
// guaranteed to refer to two different items
|
|
|
|
// * The parent GUID check works because reconcile() fixes up the
|
|
|
|
// parent GUIDs as it runs, and the command list is sorted by
|
|
|
|
// depth
|
2007-12-10 21:38:53 -08:00
|
|
|
if (!a || !b ||
|
2008-03-20 14:59:20 -07:00
|
|
|
a.action != b.action ||
|
2008-03-30 03:36:25 -07:00
|
|
|
a.action != "create" ||
|
2008-03-20 14:59:20 -07:00
|
|
|
a.data.type != b.data.type ||
|
|
|
|
a.data.parentGUID != b.data.parentGUID ||
|
|
|
|
a.GUID == b.GUID)
|
2007-12-10 21:38:53 -08:00
|
|
|
return false;
|
|
|
|
|
2008-05-23 17:50:24 -07:00
|
|
|
// Bookmarks and folders are allowed to be in a different index as long as
|
|
|
|
// they are in the same folder. Separators must be at
|
2007-12-10 21:38:53 -08:00
|
|
|
// the same index to qualify for 'likeness'.
|
|
|
|
switch (a.data.type) {
|
|
|
|
case "bookmark":
|
2008-03-29 00:00:16 -07:00
|
|
|
if (this._comp(a, b, 'URI') &&
|
|
|
|
this._comp(a, b, 'title'))
|
2007-12-10 21:38:53 -08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
case "query":
|
2008-03-29 00:00:16 -07:00
|
|
|
if (this._comp(a, b, 'URI') &&
|
|
|
|
this._comp(a, b, 'title'))
|
2007-12-10 21:38:53 -08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
case "microsummary":
|
2008-03-29 00:00:16 -07:00
|
|
|
if (this._comp(a, b, 'URI') &&
|
|
|
|
this._comp(a, b, 'generatorURI'))
|
2007-12-10 21:38:53 -08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
case "folder":
|
2008-05-23 17:50:24 -07:00
|
|
|
if (this._comp(a, b, 'title'))
|
2007-12-10 21:38:53 -08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
case "livemark":
|
2008-03-29 00:00:16 -07:00
|
|
|
if (this._comp(a, b, 'title') &&
|
|
|
|
this._comp(a, b, 'siteURI') &&
|
|
|
|
this._comp(a, b, 'feedURI'))
|
2007-12-10 21:38:53 -08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
case "separator":
|
2008-03-19 17:31:00 -07:00
|
|
|
if (this._comp(a, b, 'index'))
|
2007-12-10 21:38:53 -08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
default:
|
2008-03-05 00:00:56 -08:00
|
|
|
let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
|
|
|
|
this._log.error("commandLike: Unknown item type: " + json.encode(a));
|
2007-12-10 21:38:53 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
BookmarksSyncCore.prototype.__proto__ = new SyncCore();
|
|
|
|
|
2007-12-14 18:07:25 -08:00
|
|
|
function HistorySyncCore() {
|
|
|
|
this._init();
|
2007-12-10 21:38:53 -08:00
|
|
|
}
|
2007-12-14 18:07:25 -08:00
|
|
|
HistorySyncCore.prototype = {
|
|
|
|
_logName: "HistSync",
|
|
|
|
|
2008-06-02 15:24:52 -07:00
|
|
|
_itemExists: function HSC__itemExists(GUID) {
|
2007-12-14 18:07:25 -08:00
|
|
|
// we don't care about already-existing items; just try to re-add them
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2008-06-02 15:24:52 -07:00
|
|
|
_commandLike: function HSC_commandLike(a, b) {
|
2007-12-14 18:07:25 -08:00
|
|
|
// History commands never qualify for likeness. We will always
|
|
|
|
// take the union of all client/server items. We use the URL as
|
|
|
|
// the GUID, so the same sites will map to the same item (same
|
|
|
|
// GUID), without our intervention.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
HistorySyncCore.prototype.__proto__ = new SyncCore();
|
2008-03-11 10:08:38 -07:00
|
|
|
|
|
|
|
|
2008-04-01 17:51:10 -07:00
|
|
|
function CookieSyncCore() {
|
2008-03-11 10:08:38 -07:00
|
|
|
this._init();
|
|
|
|
}
|
2008-04-01 17:51:10 -07:00
|
|
|
CookieSyncCore.prototype = {
|
2008-03-11 10:08:38 -07:00
|
|
|
_logName: "CookieSync",
|
|
|
|
|
|
|
|
__cookieManager: null,
|
|
|
|
get _cookieManager() {
|
|
|
|
if (!this.__cookieManager)
|
|
|
|
this.__cookieManager = Cc["@mozilla.org/cookiemanager;1"].
|
|
|
|
getService(Ci.nsICookieManager2);
|
2008-04-04 12:08:04 -07:00
|
|
|
/* need the 2nd revision of the ICookieManager interface
|
|
|
|
because it supports add() and the 1st one doesn't. */
|
2008-05-23 11:05:42 -07:00
|
|
|
return this.__cookieManager;
|
2008-03-11 10:08:38 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
_itemExists: function CSC__itemExists(GUID) {
|
2008-04-03 14:30:34 -07:00
|
|
|
/* true if a cookie with the given GUID exists.
|
|
|
|
The GUID that we are passed should correspond to the keys
|
|
|
|
that we define in the JSON returned by CookieStore.wrap()
|
|
|
|
That is, it will be a string of the form
|
|
|
|
"host:path:name". */
|
2008-05-23 11:05:42 -07:00
|
|
|
|
2008-04-03 14:30:34 -07:00
|
|
|
/* TODO verify that colons can't normally appear in any of
|
|
|
|
the fields -- if they did it then we can't rely on .split(":")
|
|
|
|
to parse correctly.*/
|
2008-05-23 11:05:42 -07:00
|
|
|
|
2008-04-03 14:26:06 -07:00
|
|
|
let cookieArray = GUID.split( ":" );
|
|
|
|
let cookieHost = cookieArray[0];
|
|
|
|
let cookiePath = cookieArray[1];
|
|
|
|
let cookieName = cookieArray[2];
|
|
|
|
|
2008-04-03 14:30:34 -07:00
|
|
|
/* alternate implementation would be to instantiate a cookie from
|
2008-05-23 11:05:42 -07:00
|
|
|
cookieHost, cookiePath, and cookieName, then call
|
2008-04-03 14:30:34 -07:00
|
|
|
cookieManager.cookieExists(). Maybe that would have better
|
|
|
|
performance? This implementation seems pretty slow.*/
|
2008-04-03 14:26:06 -07:00
|
|
|
let enumerator = this._cookieManager.enumerator;
|
|
|
|
while (enumerator.hasMoreElements())
|
|
|
|
{
|
|
|
|
let aCookie = enumerator.getNext();
|
|
|
|
if (aCookie.host == cookieHost &&
|
|
|
|
aCookie.path == cookiePath &&
|
|
|
|
aCookie.name == cookieName ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2008-04-03 14:30:34 -07:00
|
|
|
/* Note: We can't just call cookieManager.cookieExists() with a generic
|
|
|
|
javascript object with .host, .path, and .name attributes attatched.
|
|
|
|
cookieExists is implemented in C and does a hard static_cast to an
|
2008-05-23 11:05:42 -07:00
|
|
|
nsCookie object, so duck typing doesn't work (and in fact makes
|
2008-04-03 14:30:34 -07:00
|
|
|
Firefox hard-crash as the static_cast returns null and is not checked.)
|
|
|
|
*/
|
2008-03-11 10:08:38 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_commandLike: function CSC_commandLike(a, b) {
|
2008-04-04 12:08:04 -07:00
|
|
|
/* Method required to be overridden.
|
|
|
|
a and b each have a .data and a .GUID
|
|
|
|
If this function returns true, an editCommand will be
|
|
|
|
generated to try to resolve the thing.
|
|
|
|
but are a and b objects of the type in the Store or
|
|
|
|
are they "commands"?? */
|
2008-04-03 14:26:06 -07:00
|
|
|
return false;
|
2008-03-11 10:08:38 -07:00
|
|
|
}
|
|
|
|
};
|
2008-04-01 17:51:10 -07:00
|
|
|
CookieSyncCore.prototype.__proto__ = new SyncCore();
|
2008-05-21 18:09:19 -07:00
|
|
|
|
|
|
|
|
|
|
|
function PasswordSyncCore() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
PasswordSyncCore.prototype = {
|
|
|
|
_logName: "PasswordSync",
|
|
|
|
|
|
|
|
__loginManager : null,
|
|
|
|
get _loginManager() {
|
|
|
|
if (!this.__loginManager)
|
|
|
|
this.__loginManager = Cc["@mozilla.org/login-manager;1"].
|
|
|
|
getService(Ci.nsILoginManager);
|
|
|
|
return this.__loginManager;
|
|
|
|
},
|
|
|
|
|
|
|
|
_itemExists: function PSC__itemExists(GUID) {
|
|
|
|
var found = false;
|
|
|
|
var logins = this._loginManager.getAllLogins({});
|
|
|
|
|
|
|
|
// XXX It would be more efficient to compute all the hashes in one shot,
|
|
|
|
// cache the results, and check the cache here. That would need to happen
|
|
|
|
// once per sync -- not sure how to invalidate cache after current sync?
|
|
|
|
for (var i = 0; i < logins.length && !found; i++) {
|
|
|
|
var hash = this._hashLoginInfo(logins[i]);
|
|
|
|
if (hash == GUID)
|
|
|
|
found = true;;
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
},
|
|
|
|
|
|
|
|
_commandLike: function PSC_commandLike(a, b) {
|
|
|
|
// Not used.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
PasswordSyncCore.prototype.__proto__ = new SyncCore();
|
2008-05-22 15:36:44 -07:00
|
|
|
|
|
|
|
function FormSyncCore() {
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
FormSyncCore.prototype = {
|
|
|
|
_logName: "FormSync",
|
|
|
|
|
|
|
|
__formDB: null,
|
|
|
|
get _formDB() {
|
|
|
|
if (!this.__formDB) {
|
|
|
|
var file = Cc["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Ci.nsIProperties).
|
|
|
|
get("ProfD", Ci.nsIFile);
|
|
|
|
file.append("formhistory.sqlite");
|
|
|
|
var stor = Cc["@mozilla.org/storage/service;1"].
|
|
|
|
getService(Ci.mozIStorageService);
|
|
|
|
this.__formDB = stor.openDatabase(file);
|
|
|
|
}
|
|
|
|
return this.__formDB;
|
|
|
|
},
|
|
|
|
|
|
|
|
_itemExists: function FSC__itemExists(GUID) {
|
|
|
|
var found = false;
|
|
|
|
var stmnt = this._formDB.createStatement("SELECT * FROM moz_formhistory");
|
|
|
|
|
|
|
|
/* Same performance restrictions as PasswordSyncCore apply here:
|
|
|
|
caching required */
|
|
|
|
while (stmnt.executeStep()) {
|
|
|
|
var nam = stmnt.getUTF8String(1);
|
|
|
|
var val = stmnt.getUTF8String(2);
|
|
|
|
var key = Utils.sha1(nam + val);
|
2008-05-23 11:05:42 -07:00
|
|
|
|
2008-05-22 15:36:44 -07:00
|
|
|
if (key == GUID)
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
},
|
|
|
|
|
|
|
|
_commandLike: function FSC_commandLike(a, b) {
|
|
|
|
/* Not required as GUIDs for similar data sets will be the same */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
FormSyncCore.prototype.__proto__ = new SyncCore();
|