mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-30 00:01:50 +00:00
Bug 556361 - New searches from the searchbar don't get synced [r=mconnor]
Add a component that notifies when satchel methods are getting called. The notifications come as "form-notifier" with JSON data of the function name, arguments, and type (before vs after).
This commit is contained in:
parent
0bc6e62a98
commit
1891e47f71
51
services/sync/FormNotifier.js
Normal file
51
services/sync/FormNotifier.js
Normal file
@ -0,0 +1,51 @@
|
||||
const Cc = Components.classes;
|
||||
const Ci = Components.interfaces;
|
||||
const Cu = Components.utils;
|
||||
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
||||
|
||||
function FormNotifier() {
|
||||
let baseForm = Components.classesByID["{a2059c0e-5a58-4c55-ab7c-26f0557546ef}"].
|
||||
getService(Ci.nsIFormHistory2)
|
||||
let obs = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
|
||||
for (let keyval in Iterator(baseForm)) {
|
||||
// Make a local copy of these values
|
||||
let [key, val] = keyval;
|
||||
|
||||
// Don't overwrite something we already have
|
||||
if (key in this)
|
||||
continue;
|
||||
|
||||
// Make a getter to grab non-functions
|
||||
if (typeof val != "function") {
|
||||
this.__defineGetter__(key, function() baseForm[key]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Wrap the function with notifications
|
||||
this[key] = function() {
|
||||
let args = Array.slice(arguments);
|
||||
let notify = function(type) {
|
||||
obs.notifyObservers(null, "form-notifier", JSON.stringify({
|
||||
args: args,
|
||||
func: key,
|
||||
type: type
|
||||
}));
|
||||
};
|
||||
|
||||
notify("before");
|
||||
val.apply(this, arguments);
|
||||
notify("after");
|
||||
};
|
||||
}
|
||||
}
|
||||
FormNotifier.prototype = {
|
||||
classDescription: "Form Notifier Wrapper",
|
||||
contractID: "@mozilla.org/satchel/form-history;1",
|
||||
classID: Components.ID("{be5a097b-6ee6-4c6a-8eca-6bce87d570e9}"),
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFormHistory2]),
|
||||
};
|
||||
|
||||
let components = [FormNotifier];
|
||||
function NSGetModule(compMgr, fileSpec) XPCOMUtils.generateModule(components);
|
@ -197,14 +197,39 @@ FormStore.prototype = {
|
||||
|
||||
function FormTracker(name) {
|
||||
Tracker.call(this, name);
|
||||
Svc.Obs.add("form-notifier", this);
|
||||
Svc.Observer.addObserver(this, "earlyformsubmit", false);
|
||||
}
|
||||
FormTracker.prototype = {
|
||||
__proto__: Tracker.prototype,
|
||||
|
||||
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFormSubmitObserver]),
|
||||
QueryInterface: XPCOMUtils.generateQI([
|
||||
Ci.nsIFormSubmitObserver,
|
||||
Ci.nsIObserver]),
|
||||
|
||||
trackEntry: function trackEntry(name, value) {
|
||||
this.addChangedID(Utils.sha1(name + value));
|
||||
this.score += 10;
|
||||
},
|
||||
|
||||
observe: function observe(subject, topic, data) {
|
||||
let name, value;
|
||||
|
||||
// Figure out if it's a function that we care about tracking
|
||||
let formCall = JSON.parse(data);
|
||||
let func = formCall.func;
|
||||
if ((func == "addEntry" && formCall.type == "after") ||
|
||||
(func == "removeEntry" && formCall.type == "before"))
|
||||
[name, value] = formCall.args;
|
||||
|
||||
// Skip if there's nothing of interest
|
||||
if (name == null || value == null)
|
||||
return;
|
||||
|
||||
this._log.trace("Logging form action: " + [func, name, value]);
|
||||
this.trackEntry(name, value);
|
||||
},
|
||||
|
||||
/* 10 points per form element */
|
||||
notify: function FormTracker_notify(formElement, aWindow, actionURI) {
|
||||
if (this.ignoreAll)
|
||||
return;
|
||||
@ -268,8 +293,7 @@ FormTracker.prototype = {
|
||||
}
|
||||
|
||||
this._log.trace("Logging form element: " + name + " :: " + el.value);
|
||||
this.addChangedID(Utils.sha1(name + el.value));
|
||||
this.score += 10;
|
||||
this.trackEntry(name, el.value);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user