gecko/services/sync/FormNotifier.js
Edward Lee 5833da7aa7 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).
2010-04-01 15:21:54 -07:00

52 lines
1.5 KiB
JavaScript

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);