[or-cvs] r13520: Clean up XPCOM hooking based on suggestions from timeless. (torbutton/trunk/src/components)

mikeperry at seul.org mikeperry at seul.org
Fri Feb 15 07:50:59 UTC 2008


Author: mikeperry
Date: 2008-02-15 02:50:59 -0500 (Fri, 15 Feb 2008)
New Revision: 13520

Modified:
   torbutton/trunk/src/components/crash-observer.js
   torbutton/trunk/src/components/ignore-history.js
Log:

Clean up XPCOM hooking based on suggestions from timeless.



Modified: torbutton/trunk/src/components/crash-observer.js
===================================================================
--- torbutton/trunk/src/components/crash-observer.js	2008-02-14 22:31:05 UTC (rev 13519)
+++ torbutton/trunk/src/components/crash-observer.js	2008-02-15 07:50:59 UTC (rev 13520)
@@ -36,6 +36,11 @@
   this._prefs = Components.classes["@mozilla.org/preferences-service;1"]
       .getService(Components.interfaces.nsIPrefBranch);
 
+  this.logger = Components.classes["@torproject.org/torbutton-logger;1"]
+      .getService(Components.interfaces.nsISupports).wrappedJSObject;
+  dump("New crash observer\n");
+  this.logger.log(3, "New StoreWrapper");
+
   this._store = function() {
     var store = kREAL_STORE.getService();
     for (var i = 0; i < kStoreInterfaces.length; i++) {
@@ -49,11 +54,16 @@
 {
   QueryInterface: function(iid) {
 
-    if (iid.equals(Components.interfaces.nsISupports) ||
-        iid.equals(Components.interfaces.nsIClassInfo)) {
-      return this.QueryInterface(iid);
+    if (iid.equals(Components.interfaces.nsISupports)) {
+        return this;
     }
 
+    if(iid.equals(Components.interfaces.nsIClassInfo)) {
+      var ret = this._store().QueryInterface(iid);
+      dump("classInfo: "+ret.classID);
+      return ret;
+    }
+
     try {
         var store = this._store().QueryInterface(iid);
         if (store) this.copyMethods(store);
@@ -68,24 +78,33 @@
   /* 
    * Copies methods from the true sessionstore object we are wrapping
    */
-  copyMethods: function(store) {
-    var mimic = function(obj, method) {
-      if(typeof(store[method]) == "function") {
-          obj[method] = function(a, b, c, d, e, f, g) {
-              return store[method](a, b, c, d, e, f, g);
-          };
+  copyMethods: function(wrapped) {
+    var mimic = function(newObj, method) {
+      if(typeof(wrapped[method]) == "function") {
+          // Code courtesy of timeless: 
+          // http://www.webwizardry.net/~timeless/windowStubs.js
+          var params = [];
+          params.length = wrapped[method].length;
+          var x = 0;
+          var call = method + "("+params.join().replace(/(?:)/g,function(){return "p"+(++x)})+")";
+          var fun = "function "+call+"{if (arguments.length < "+wrapped[method].length+") throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS; return wrapped."+method+".apply(wrapped, arguments);}";
+          // Already in scope
+          //var Components = this.Components;
+          newObj[method] = eval(fun);
       } else {
-          obj.__defineGetter__(method, function() { return store[method]; });
-          obj.__defineSetter__(method, function(val) { store[method] = val; });
+          newObj.__defineGetter__(method, function() { return wrapped[method]; });
+          newObj.__defineSetter__(method, function(val) { wrapped[method] = val; });
       }
     };
-    for (var method in store) {
+    for (var method in wrapped) {
       if(typeof(this[method]) == "undefined") mimic(this, method);
     }
   },
 
-  observe: function sss_observe(aSubject, aTopic, aData) {
+  observe: function(aSubject, aTopic, aData) {
     if(aTopic == "app-startup") {
+      dump("App startup\n");
+      this.logger.log(3, "Got app-startup");
       this._startup = true;
       var observerService = Cc["@mozilla.org/observer-service;1"].
           getService(Ci.nsIObserverService);
@@ -95,10 +114,12 @@
     this._store().observe(aSubject, aTopic, aData);
   },
 
-  doRestore: function sss_doRestore() {
+  doRestore: function() {
     var ret = false;
     // This is so lame. But the exposed API is braindead so it 
     // must be hacked around
+    dump("new doRestore\n");
+    this.logger.log(3, "Got doRestore");
     if((ret = this._store().doRestore()) && this._startup) {
         this._prefs.setBoolPref("extensions.torbutton.crashed", true);
     } 
@@ -151,6 +172,7 @@
                                   fileSpec, 
                                   location, 
                                   type);
+  dump("Registered crash observer\n");
 };
 
 StoreWrapperModule.getClassObject = function (compMgr, cid, iid)

Modified: torbutton/trunk/src/components/ignore-history.js
===================================================================
--- torbutton/trunk/src/components/ignore-history.js	2008-02-14 22:31:05 UTC (rev 13519)
+++ torbutton/trunk/src/components/ignore-history.js	2008-02-15 07:50:59 UTC (rev 13520)
@@ -13,7 +13,8 @@
 
 // Module specific constants
 const kMODULE_NAME = "Ignore History";
-const kMODULE_CONTRACTID = "@mozilla.org/browser/global-history;2";
+const kMODULE_CONTRACTID2 = "@mozilla.org/browser/global-history;2";
+const kMODULE_CONTRACTID3 = "@mozilla.org/browser/nav-history-service;1";
 const kMODULE_CID = Components.ID("bc666d45-a9a1-4096-9511-f6db6f686881");
 
 /* Mozilla defined interfaces for FF3.0 and 2.0 */
@@ -22,8 +23,14 @@
 
 // const kREAL_HISTORY = Components.classesByID[kREAL_HISTORY_CID];
 
-const kHistoryInterfaces = [ "nsIBrowserHistory", "nsIGlobalHistory2" ];
+const kHistoryInterfaces2 = [ "nsIBrowserHistory", "nsIGlobalHistory2" ];
 
+const kHistoryInterfaces3 = [ "nsIBrowserHistory", "nsIGlobalHistory2", 
+                             "nsIAutoCompleteSearch", "nsIGlobalHistory3",
+                             "nsIDownloadHistory", "nsIBrowserHistory",
+                             "nsIAutoCompleteSimpleResultListener",
+                             "nsINavHistoryService" ];
+
 const Cr = Components.results;
 
 function HistoryWrapper() {
@@ -32,10 +39,13 @@
       .getService(Components.interfaces.nsIXULAppInfo);
   var versionChecker = Components.classes["@mozilla.org/xpcom/version-comparator;1"]
       .getService(Components.interfaces.nsIVersionComparator);
+
   if(versionChecker.compare(appInfo.version, "3.0a1") >= 0) {
     this._real_history = Components.classesByID[kREAL_HISTORY_CID3];
+    this._interfaces = kHistoryInterfaces3;
   } else {
     this._real_history = Components.classesByID[kREAL_HISTORY_CID2];
+    this._interfaces = kHistoryInterfaces2;
   }
 
   this._prefs = Components.classes["@mozilla.org/preferences-service;1"]
@@ -43,8 +53,8 @@
 
   this._history = function() {
     var history = this._real_history.getService();
-    for (var i = 0; i < kHistoryInterfaces.length; i++) {
-      history.QueryInterface(Components.interfaces[kHistoryInterfaces[i]]);
+    for (var i = 0; i < this._interfaces.length; i++) {
+      history.QueryInterface(Components.interfaces[this._interfaces[i]]);
     }
     return history;
   };
@@ -53,7 +63,6 @@
 HistoryWrapper.prototype =
 {
   QueryInterface: function(iid) {
-
     if (iid.equals(Components.interfaces.nsISupports)) {
       return this;
     }
@@ -87,18 +96,25 @@
   /* 
    * Copies methods from the true history object we are wrapping
    */
-  copyMethods: function(history) {
-    var mimic = function(obj, method) {
-      if(typeof(history[method]) == "function") {
-          obj[method] = function(a, b, c, d, e, f, g) {
-              return history[method](a, b, c, d, e, f, g);
-          };
+  copyMethods: function(wrapped) {
+    var mimic = function(newObj, method) {
+      if(typeof(wrapped[method]) == "function") {
+          // Code courtesy of timeless: 
+          // http://www.webwizardry.net/~timeless/windowStubs.js
+          var params = [];
+          params.length = wrapped[method].length;
+          var x = 0;
+          var call = method + "("+params.join().replace(/(?:)/g,function(){return "p"+(++x)})+")";
+          var fun = "function "+call+"{if (arguments.length < "+wrapped[method].length+") throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS; return wrapped."+method+".apply(wrapped, arguments);}";
+          // already in scope
+          //var Components = this.Components;
+          newObj[method] = eval(fun);
       } else {
-          obj.__defineGetter__(method, function() { return history[method]; });
-          obj.__defineSetter__(method, function(val) { history[method] = val; });
+          newObj.__defineGetter__(method, function() { return wrapped[method]; });
+          newObj.__defineSetter__(method, function(val) { wrapped[method] = val; });
       }
     };
-    for (var method in history) {
+    for (var method in wrapped) {
       if(typeof(this[method]) == "undefined") mimic(this, method);
     }
   },
@@ -139,15 +155,15 @@
     Components.returnCode = Cr.NS_ERROR_NO_AGGREGATION;
     return null;
   }
-
-  // XXX: needs updating for FF3
+  /*
   if (!iid.equals(Components.interfaces.nsIGlobalHistory2) &&
       !iid.equals(Components.interfaces.nsIBrowserHistory) &&
     !iid.equals(Components.interfaces.nsISupports)) {
 
+    dump("Holla noint: "+iid.toString() +"\n");
     Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
     return null;
-  }
+  }*/
 
   if(!HistoryWrapperSingleton)
     HistoryWrapperSingleton = new HistoryWrapper();
@@ -170,10 +186,23 @@
   compMgr = compMgr.QueryInterface(nsIComponentRegistrar);
   compMgr.registerFactoryLocation(kMODULE_CID,
                                   kMODULE_NAME,
-                                  kMODULE_CONTRACTID,
+                                  kMODULE_CONTRACTID2,
                                   fileSpec, 
                                   location, 
                                   type);
+
+  var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
+      .getService(Components.interfaces.nsIXULAppInfo);
+  var versionChecker = Components.classes["@mozilla.org/xpcom/version-comparator;1"]
+      .getService(Components.interfaces.nsIVersionComparator);
+  if(versionChecker.compare(appInfo.version, "3.0a1") >= 0) {
+      compMgr.registerFactoryLocation(kMODULE_CID,
+              kMODULE_NAME,
+              kMODULE_CONTRACTID3,
+              fileSpec, 
+              location, 
+              type);
+  }
 };
 
 HistoryWrapperModule.getClassObject = function (compMgr, cid, iid)



More information about the tor-commits mailing list