Update form sync engine to work with Weave0.3/FF3.5

This commit is contained in:
Anant Narayanan 2009-04-06 19:05:16 +02:00
Родитель 20376d2bdc
Коммит 21b13a7d8b
2 изменённых файлов: 196 добавлений и 118 удалений

Просмотреть файл

@ -40,13 +40,15 @@ const Cc = Components.classes;
const Ci = Components.interfaces; const Ci = Components.interfaces;
const Cu = Components.utils; const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://weave/log4moz.js"); Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/async.js"); Cu.import("resource://weave/async.js");
Cu.import("resource://weave/util.js"); Cu.import("resource://weave/util.js");
Cu.import("resource://weave/engines.js"); Cu.import("resource://weave/engines.js");
Cu.import("resource://weave/syncCores.js");
Cu.import("resource://weave/stores.js"); Cu.import("resource://weave/stores.js");
Cu.import("resource://weave/trackers.js"); Cu.import("resource://weave/trackers.js");
Cu.import("resource://weave/type_records/forms.js");
Function.prototype.async = Async.sugar; Function.prototype.async = Async.sugar;
@ -55,23 +57,34 @@ function FormEngine() {
} }
FormEngine.prototype = { FormEngine.prototype = {
__proto__: SyncEngine.prototype, __proto__: SyncEngine.prototype,
get _super() SyncEngine.prototype, name: "forms",
displayname: "Forms",
get name() "forms", logName: "Forms",
get displayName() "Forms", _storeObj: FormStore,
get logName() "Forms", _trackerObj: FormTracker,
get serverPrefix() "user-data/forms/", _recordObj: FormRec,
get _store() { _syncStartup: function FormEngine__syncStartup() {
let store = new FormStore(); let self = yield;
this.__defineGetter__("_store", function() store); this._store.cacheFormItems();
return store; yield SyncEngine.prototype._syncStartup.async(this, self.cb);
}, },
get _tracker() { /* Wipe cache when sync finishes */
let tracker = new FormsTracker(); _syncFinish: function FormEngine__syncFinish() {
this.__defineGetter__("_tracker", function() tracker); let self = yield;
return tracker; this._store.clearFormCache();
yield SyncEngine.prototype._syncFinish.async(this, self.cb);
},
_recordLike: function SyncEngine__recordLike(a, b) {
if (a.cleartext == null || b.cleartext == null)
return false;
if (a.cleartext.name == b.cleartext.name &&
a.cleartext.value == b.cleartext.value) {
return true;
}
return false;
} }
}; };
@ -82,7 +95,7 @@ function FormStore() {
FormStore.prototype = { FormStore.prototype = {
__proto__: Store.prototype, __proto__: Store.prototype,
_logName: "FormStore", _logName: "FormStore",
_lookup: null, _formItems: null,
get _formDB() { get _formDB() {
let file = Cc["@mozilla.org/file/directory_service;1"]. let file = Cc["@mozilla.org/file/directory_service;1"].
@ -109,33 +122,19 @@ FormStore.prototype = {
this.__defineGetter__("_formStatement", function() stmnt); this.__defineGetter__("_formStatement", function() stmnt);
return stmnt; return stmnt;
}, },
create: function FormStore_create(record) { cacheFormItems: function FormStore_cacheFormItems() {
this._log.debug("Got create record: " + record.id); this._log.debug("Caching all form items");
this._formHistory.addEntry(record.cleartext.name, record.cleartext.value); this._formItems = this.getAllIDs();
}, },
remove: function FormStore_remove(record) { clearFormCache: function FormStore_clearFormCache() {
this._log.debug("Got remove record: " + record.id); this._log.debug("Clearing form cache");
this._formItems = null;
if (record.id in this._lookup) {
let data = this._lookup[record.id];
} else {
this._log.warn("Invalid GUID found, ignoring remove request.");
return;
}
this._formHistory.removeEntry(data.name, data.value);
delete this._lookup[record.id];
}, },
update: function FormStore__editCommand(record) { getAllIDs: function FormStore_getAllIDs() {
this._log.debug("Got update record: " + record.id); let items = {};
this._log.warn("Ignoring update request");
},
wrap: function FormStore_wrap() {
this._lookup = {};
let stmnt = this._formStatement; let stmnt = this._formStatement;
while (stmnt.executeStep()) { while (stmnt.executeStep()) {
@ -143,23 +142,56 @@ FormStore.prototype = {
let val = stmnt.getUTF8String(2); let val = stmnt.getUTF8String(2);
let key = Utils.sha1(nam + val); let key = Utils.sha1(nam + val);
this._lookup[key] = { name: nam, value: val }; items[key] = { name: nam, value: val };
} }
stmnt.reset(); stmnt.reset();
return items;
},
changeItemID: function FormStore_changeItemID(oldID, newID) {
this._log.warn("FormStore IDs are data-dependent, cannot change!");
},
itemExists: function FormStore_itemExists(id) {
return (id in this._formItems);
},
createRecord: function FormStore_createRecord(guid, cryptoMetaURL) {
let record = new FormRec();
record.id = guid;
return this._lookup; if (guid in this._formItems) {
let item = this._formItems[guid];
record.encryption = cryptoMetaURL;
record.name = item.name;
record.value = item.value;
} else {
record.deleted = true;
}
return record;
},
create: function FormStore_create(record) {
this._log.debug("Adding form record for " + record.name);
this._formHistory.addEntry(record.name, record.value);
}, },
wrapItem: function FormStore_wrapItem(id) { remove: function FormStore_remove(record) {
if (!this._lookup) this._log.debug("Removing form record: " + record.id);
this._lookup = this.wrap();
return this._lookup[id]; if (record.id in this._formItems) {
let item = this._formItems[record.id];
this._formHistory.removeEntry(item.name, item.value);
return;
}
this._log.warn("Invalid GUID found, ignoring remove request.");
}, },
getAllIDs: function FormStore_getAllIDs() { update: function FormStore_update(record) {
if (!this._lookup) this._log.warn("Ignoring form record update request!");
this._lookup = this.wrap();
return this._lookup;
}, },
wipe: function FormStore_wipe() { wipe: function FormStore_wipe() {
@ -167,79 +199,50 @@ FormStore.prototype = {
} }
}; };
function FormsTracker() { function FormTracker() {
this._init(); this._init();
} }
FormsTracker.prototype = { FormTracker.prototype = {
__proto__: Tracker.prototype, __proto__: Tracker.prototype,
_logName: "FormsTracker", _logName: "FormTracker",
file: "form",
get _formDB() {
let file = Cc["@mozilla.org/file/directory_service;1"]. QueryInterface: XPCOMUtils.generateQI([Ci.nsIFormSubmitObserver]),
getService(Ci.nsIProperties).
get("ProfD", Ci.nsIFile); __observerService: null,
get _observerService() {
file.append("formhistory.sqlite"); if (!this.__observerService)
let stor = Cc["@mozilla.org/storage/service;1"]. this.__observerService = Cc["@mozilla.org/observer-service;1"].
getService(Ci.mozIStorageService); getService(Ci.nsIObserverService);
return this.__observerService;
let formDB = stor.openDatabase(file);
this.__defineGetter__("_formDB", function() formDB);
return formDB;
}, },
get _formStatement() { _init: function FormTracker__init() {
let stmnt = this._formDB.
createStatement("SELECT COUNT(fieldname) FROM moz_formhistory");
this.__defineGetter__("_formStatement", function() stmnt);
return stmnt;
},
/* nsIFormSubmitObserver is not available in JS.
* To calculate scores, we instead just count the changes in
* the database since the last time we were asked.
*
* FIXME!: Buggy, because changes in a row doesn't result in
* an increment of our score. A possible fix is to do a
* SELECT for each fieldname and compare those instead of the
* whole row count.
*
* Each change is worth 2 points. At some point, we may
* want to differentiate between search-history rows and other
* form items, and assign different scores.
*/
_rowCount: 0,
get score() {
let stmnt = this._formStatement;
stmnt.executeStep();
let count = stmnt.getInt32(0);
this._score = Math.abs(this._rowCount - count) * 2;
stmnt.reset();
if (this._score >= 100)
return 100;
else
return this._score;
},
resetScore: function FormsTracker_resetScore() {
let stmnt = this._formStatement;
stmnt.executeStep();
this._rowCount = stmnt.getInt32(0);
this._score = 0;
stmnt.reset();
},
_init: function FormsTracker__init() {
this.__proto__.__proto__._init.call(this); this.__proto__.__proto__._init.call(this);
this._log.trace("FormTracker initializing!");
let stmnt = this._formStatement; this._observerService.addObserver(this, "earlyformsubmit", false);
stmnt.executeStep(); },
/* 10 points per form element */
notify: function FormTracker_notify(formElement, aWindow, actionURI) {
if (this.ignoreAll)
return;
this._log.trace("Form submission notification for " + actionURI.spec);
this._rowCount = stmnt.getInt32(0); /* Get number of elements in form, add points and changedIDs */
stmnt.reset(); let len = formElement.length;
let elements = formElement.elements;
for (let i = 0; i < len; i++) {
let element = elements.item(i);
let inputElement = element.QueryInterface(Ci.nsIDOMHTMLInputElement);
if (inputElement && inputElement.type == "text") {
this._log.trace("Logging form element: " + inputElement.name + "::" +
inputElement.value);
this.addChangedID(Utils.sha1(inputElement.name + inputElement.value));
this._score += 10;
}
}
} }
}; };

Просмотреть файл

@ -0,0 +1,75 @@
/* ***** 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 Weave.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Anant Narayanan <anant@kix.in>
*
* 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 ***** */
const EXPORTED_SYMBOLS = ['FormRec'];
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
Cu.import("resource://weave/log4moz.js");
Cu.import("resource://weave/util.js");
Cu.import("resource://weave/async.js");
Cu.import("resource://weave/base_records/wbo.js");
Cu.import("resource://weave/base_records/crypto.js");
Cu.import("resource://weave/base_records/keys.js");
Function.prototype.async = Async.sugar;
function FormRec(uri) {
this._FormRec_init(uri);
}
FormRec.prototype = {
__proto__: CryptoWrapper.prototype,
_logName: "Record.Form",
_FormRec_init: function FormRec_init(uri) {
this._CryptoWrap_init(uri);
this.cleartext = {
};
},
get name() this.cleartext.name,
set name(p) {
this.cleartext.name = p;
},
get value() this.cleartext.value,
set value(p) {
this.cleartext.value = p;
}
};