diff --git a/services/sync/modules/engines.js b/services/sync/modules/engines.js index f9abdf565a46..2fa95e4d042c 100644 --- a/services/sync/modules/engines.js +++ b/services/sync/modules/engines.js @@ -1085,6 +1085,13 @@ FormEngine.prototype = { if (!this.__store) this.__store = new FormStore(); return this.__store; + }, + + __tracker: null, + get _tracker() { + if (!this.__tracker) + this.__tracker = new FormsTracker(); + return this.__tracker; } }; FormEngine.prototype.__proto__ = new Engine(); \ No newline at end of file diff --git a/services/sync/modules/trackers.js b/services/sync/modules/trackers.js index 9c7fb1ffe966..cb2af47e7ef6 100644 --- a/services/sync/modules/trackers.js +++ b/services/sync/modules/trackers.js @@ -34,7 +34,8 @@ * * ***** END LICENSE BLOCK ***** */ -const EXPORTED_SYMBOLS = ['Tracker', 'BookmarksTracker', 'HistoryTracker']; +const EXPORTED_SYMBOLS = ['Tracker', 'BookmarksTracker', 'HistoryTracker', + 'FormsTracker']; const Cc = Components.classes; const Ci = Components.interfaces; @@ -186,3 +187,72 @@ Function.prototype.async = Async.sugar; } } HistoryTracker.prototype.__proto__ = new Tracker(); + + function FormsTracker() { + this._init(); + } + FormsTracker.prototype = { + _logName: "FormsTracker", + + __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; + } + + /* 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() { + var stmnt = this._formDB.createStatement("SELECT COUNT(fieldname) FROM moz_formhistory"); + stmnt.executeStep(); + var count = stmnt.getInt32(0); + stmnt.reset(); + + this._score = abs(this._rowCount - count) * 2; + + if (this._score >= 100) + return 100; + else + return this._score; + }, + + resetScore: function FormsTracker_resetScore() { + var stmnt = this._formDB.createStatement("SELECT COUNT(fieldname) FROM moz_formhistory"); + stmnt.executeStep(); + this._rowCount = stmnt.getInt32(0); + stmnt.reset(); + + super.resetScore(); + }, + + _init: function FormsTracker__init() { + super._init(); + + var stmnt = this._formDB.createStatement("SELECT COUNT(fieldname) FROM moz_formhistory"); + stmnt.executeStep(); + this._rowCount = stmnt.getInt32(0); + stmnt.reset(); + } + } + FormsTracker.prototype.__proto__ = new Tracker();