[or-cvs] r11638: Bug 503: Fix the session store issue with the bloodiest hack (in torbutton/trunk/src: chrome/content components)

mikeperry at seul.org mikeperry at seul.org
Tue Sep 25 18:21:24 UTC 2007


Author: mikeperry
Date: 2007-09-25 14:21:24 -0400 (Tue, 25 Sep 2007)
New Revision: 11638

Added:
   torbutton/trunk/src/components/session-blocker.js
Modified:
   torbutton/trunk/src/chrome/content/torbutton.js
   torbutton/trunk/src/components/cookie-jar-selector.js
   torbutton/trunk/src/components/cssblocker.js
   torbutton/trunk/src/components/ignore-history.js
Log:

Bug 503: Fix the session store issue with the bloodiest hack
imaginable. The SessionStore API has too many internal
functions and timers to hook the published IDL and actually
accomplish anything, so we're hooking nsIFileOutputStream and
refusing to do anything if the filename is sessionstore.js.

Also fix a few of our components to return null with error
code instead of throwing exception on QueryInterface failures.
(this seems to be the new way of doing things).



Modified: torbutton/trunk/src/chrome/content/torbutton.js
===================================================================
--- torbutton/trunk/src/chrome/content/torbutton.js	2007-09-25 16:25:11 UTC (rev 11637)
+++ torbutton/trunk/src/chrome/content/torbutton.js	2007-09-25 18:21:24 UTC (rev 11638)
@@ -77,7 +77,6 @@
             case "extensions.torbutton.block_nthwrite":
             case "extensions.torbutton.block_thwrite":
             case "extensions.torbutton.shutdown_method":
-            case "extensions.torbutton.disable_sessionstore":
             case "extensions.torbutton.spoof_english":
                 torbutton_log(1, "Got update message, updating status");
                 torbutton_update_status(
@@ -555,16 +554,6 @@
         torbutton_clear_history();
     }
 
-    // FIXME:
-    // http://lxr.mozilla.org/seamonkey/source/browser/components/sessionstore/nsISessionStore.idl
-    // or just make a pref to always disable store
-    // http://wiki.mozilla.org/Session_Restore
-    // http://kb.mozillazine.org/Browser.sessionstore.privacy_level
-    // http://kb.mozillazine.org/About:config_entries
-
-    m_tb_prefs.setBoolPref("browser.sessionstore.enabled", 
-            !torprefs.getBoolPref("disable_sessionstore"));
-
     if(mode) {
         if(torprefs.getBoolPref('block_thwrite')) {
             m_tb_prefs.setIntPref("browser.download.manager.retention", 0);

Modified: torbutton/trunk/src/components/cookie-jar-selector.js
===================================================================
--- torbutton/trunk/src/components/cookie-jar-selector.js	2007-09-25 16:25:11 UTC (rev 11637)
+++ torbutton/trunk/src/components/cookie-jar-selector.js	2007-09-25 18:21:24 UTC (rev 11638)
@@ -14,6 +14,8 @@
 const kMODULE_CONTRACTID = "@stanford.edu/cookie-jar-selector;1";
 const kMODULE_CID = Components.ID("e6204253-b690-4159-bfe8-d4eedab6b3be");
 
+const Cr = Components.results;
+
 function CookieJarSelector() {
   var Cc = Components.classes;
   var Ci = Components.interfaces;
@@ -101,8 +103,10 @@
   QueryInterface: function(iid)
   {
     if (!iid.equals(nsIClassInfo) &&
-        !iid.equals(nsISupports))
-      throw Components.results.NS_ERROR_NO_INTERFACE;
+        !iid.equals(nsISupports)) {
+      Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
+      return null;
+    }
     return this;
   },
 
@@ -130,13 +134,15 @@
 
 CookieJarSelectorFactory.createInstance = function (outer, iid)
 {
-  if (outer != null)
-    throw Components.results.NS_ERROR_NO_AGGREGATION;
-
+  if (outer != null) {
+    Components.returnCode = Cr.NS_ERROR_NO_AGGREGATION;
+    return null;
+  }
   if (!iid.equals(nsIClassInfo) &&
-      !iid.equals(nsISupports))
-    throw Components.results.NS_ERROR_NO_INTERFACE;
-
+      !iid.equals(nsISupports)) {
+    Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
+    return null;
+  }
   return new CookieJarSelector();
 }
 
@@ -156,13 +162,12 @@
 
 CookieJarSelectorModule.getClassObject = function (compMgr, cid, iid)
 {
-  if (!cid.equals(kMODULE_CID))
-    throw Components.results.NS_ERROR_NO_INTERFACE;
+  if (cid.equals(kMODULE_CID))
+    return CookieJarSelectorFactory;
 
-  if (!iid.equals(Components.interfaces.nsIFactory))
-    throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-    
-  return CookieJarSelectorFactory;
+
+  Components.returnCode = Cr.NS_ERROR_NOT_REGISTERED;
+  return null;
 }
 
 CookieJarSelectorModule.canUnload = function (compMgr)

Modified: torbutton/trunk/src/components/cssblocker.js
===================================================================
--- torbutton/trunk/src/components/cssblocker.js	2007-09-25 16:25:11 UTC (rev 11637)
+++ torbutton/trunk/src/components/cssblocker.js	2007-09-25 18:21:24 UTC (rev 11638)
@@ -28,6 +28,7 @@
 const ok = Components.interfaces.nsIContentPolicy.ACCEPT;
 const block = Components.interfaces.nsIContentPolicy.REJECT_REQUEST;
 const CPolicy = Components.interfaces.nsIContentPolicy;
+const Cr = Components.results;
 
 // Retrieves the window object for a node or returns null if it isn't possible
 function getWindow(node) {
@@ -51,7 +52,8 @@
 				iid.equals(Components.interfaces.nsIFactory))
 			return this;
 
-		throw Components.results.NS_ERROR_NO_INTERFACE;
+       Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
+       return null;
 	}
 };
 var array = Components.classes['@mozilla.org/supports-array;1'].createInstance(Components.interfaces.nsISupportsArray);
@@ -202,12 +204,16 @@
 const factory = {
 	// nsIFactory interface implementation
 	createInstance: function(outer, iid) {
-		if (outer != null)
-			throw Components.results.NS_ERROR_NO_AGGREGATION;
+		if (outer != null) {
+           Components.returnCode = Cr.NS_ERROR_NO_AGGREGATION;
+           return null;
+       }
 
         if (!iid.equals(Components.interfaces.nsIContentPolicy) &&
-                !iid.equals(Components.interfaces.nsISupports))
-            throw Components.results.NS_ERROR_NO_INTERFACE;
+                !iid.equals(Components.interfaces.nsISupports)) {
+            Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;          
+            return null;
+        }
 
         policy.init();
 		return policy;
@@ -216,6 +222,7 @@
 	// nsISupports interface implementation
 	QueryInterface: function(iid) {
 		if (iid.equals(Components.interfaces.nsISupports) ||
+				iid.equals(Components.interfaces.nsIModule) ||
 				iid.equals(Components.interfaces.nsIFactory))
 			return this;
 
@@ -224,7 +231,8 @@
 			dump("CSS Blocker: factory.QI to an unknown interface: " + iid + "\n");
         */
 
-		throw Components.results.NS_ERROR_NO_INTERFACE;
+        Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;          
+        return null;   
 	}
 };
 
@@ -256,13 +264,11 @@
 	},
 
 	getClassObject: function(compMgr, cid, iid) {
-		if (!cid.equals(CSSB_CID))
-			throw Components.results.NS_ERROR_NO_INTERFACE;
+		if (cid.equals(CSSB_CID))
+            return factory;
 
-		if (!iid.equals(Components.interfaces.nsIFactory))
-			throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-
-		return factory;
+        Components.returnCode = Cr.NS_ERROR_NOT_REGISTERED;
+        return null;
 	},
 
 	canUnload: function(compMgr) {

Modified: torbutton/trunk/src/components/ignore-history.js
===================================================================
--- torbutton/trunk/src/components/ignore-history.js	2007-09-25 16:25:11 UTC (rev 11637)
+++ torbutton/trunk/src/components/ignore-history.js	2007-09-25 18:21:24 UTC (rev 11638)
@@ -21,6 +21,8 @@
 const kREAL_HISTORY = Components.classesByID[kREAL_HISTORY_CID];
 const kHistoryInterfaces = [ "nsIBrowserHistory", "nsIGlobalHistory2" ];
 
+const Cr = Components.results;
+
 function HistoryWrapper() {
   this._prefs = Components.classes["@mozilla.org/preferences-service;1"]
       .getService(Components.interfaces.nsIPrefBranch);
@@ -113,14 +115,19 @@
 
 HistoryWrapperFactory.createInstance = function (outer, iid)
 {
-  if (outer != null)
-    throw Components.results.NS_ERROR_NO_AGGREGATION;
+  if (outer != null) {
+    Components.returnCode = Cr.NS_ERROR_NO_AGGREGATION;
+    return null;
+  }
 
   if (!iid.equals(Components.interfaces.nsIGlobalHistory2) &&
       !iid.equals(Components.interfaces.nsIBrowserHistory) &&
-      !iid.equals(Components.interfaces.nsISupports))
-    throw Components.results.NS_ERROR_NO_INTERFACE;
+    !iid.equals(Components.interfaces.nsISupports)) {
 
+    Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
+    return null;
+  }
+
   if(!HistoryWrapperSingleton)
     HistoryWrapperSingleton = new HistoryWrapper();
 
@@ -150,13 +157,11 @@
 
 HistoryWrapperModule.getClassObject = function (compMgr, cid, iid)
 {
-  if (!cid.equals(kMODULE_CID))
-    throw Components.results.NS_ERROR_NO_INTERFACE;
+  if (cid.equals(kMODULE_CID))
+    return HistoryWrapperFactory;
 
-  if (!iid.equals(Components.interfaces.nsIFactory))
-    throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-    
-  return HistoryWrapperFactory;
+  Components.returnCode = Cr.NS_ERROR_NOT_REGISTERED;
+  return null;
 };
 
 HistoryWrapperModule.canUnload = function (compMgr)

Added: torbutton/trunk/src/components/session-blocker.js
===================================================================
--- torbutton/trunk/src/components/session-blocker.js	                        (rev 0)
+++ torbutton/trunk/src/components/session-blocker.js	2007-09-25 18:21:24 UTC (rev 11638)
@@ -0,0 +1,163 @@
+/*************************************************************************
+ * Session Blocker (JavaScript XPCOM component)
+ * Disables reading and writing session state for tor loaded tabs. 
+ * This component is implemented as a wrapper around the true sessionsaver 
+ * object that sometimes refuses to save state.
+ *
+ *************************************************************************/
+
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cr = Components.results;
+
+// Module specific constants
+const kMODULE_NAME = "Session Blocking File Output Stream";
+const kMODULE_CONTRACTID = "@mozilla.org/network/safe-file-output-stream;1";
+const kMODULE_CID = Components.ID("9215354b-1787-4aef-9946-780f046c75a8");
+
+/* Mozilla defined interfaces */
+const kREAL_STORE_CID = "{a181af0d-68b8-4308-94db-d4f859058215}";
+const kREAL_STORE = Components.classesByID[kREAL_STORE_CID];
+const kStoreInterfaces = [
+    "nsIFileOutputStream",
+    "nsIOutputStream",
+    "nsISafeOutputStream",
+    "nsISeekableStream",
+    "nsISupports"];
+
+function StoreWrapper() {
+  this._prefs = Components.classes["@mozilla.org/preferences-service;1"]
+      .getService(Components.interfaces.nsIPrefBranch);
+
+  this._store = function() {
+    var store = kREAL_STORE.getService();
+    for (var i = 0; i < kStoreInterfaces.length; i++) {
+      store.QueryInterface(Components.interfaces[kStoreInterfaces[i]]);
+    }
+    return store;
+  };
+}
+
+StoreWrapper.prototype =
+{
+  _log: function(str) {
+    // TODO: This could be done better/unified with the main log system..
+    if(this._loglevel <= 2) {
+      dump(str);
+    } 
+  },
+
+  QueryInterface: function(iid) {
+
+    if (iid.equals(Components.interfaces.nsISupports) ||
+        iid.equals(Components.interfaces.nsISecurityCheckedComponent) ||
+        iid.equals(Components.interfaces.nsIClassInfo)) {
+      return this.QueryInterface(iid);
+    }
+
+    var store = this._store().QueryInterface(iid);
+    if (store) this.copyMethods(store);
+    return this;
+  },
+
+  /* 
+   * Copies methods from the true sessionstore object we are wrapping
+   */
+  copyMethods: function(store) {
+    var mimic = function(obj, method) {
+      obj[method] = function(a, b, c, d, e, f, g) {
+        if(this._passthrough) {
+          return 0;
+        }
+        return store[method](a, b, c, d, e, f, g);
+      };
+    };
+    for (var method in store) {
+      if(typeof(this[method]) == "undefined") mimic(this, method);
+    }
+  },
+
+  init: function(file, ioFlags , perm , behaviorFlags ) {
+    this._prefs = Components.classes["@mozilla.org/preferences-service;1"]
+      .getService(Components.interfaces.nsIPrefBranch);
+    this._loglevel = this._prefs.getIntPref("extensions.torbutton.loglevel");
+    try {
+      if(this._prefs.getBoolPref("extensions.torbutton.disable_sessionstore") &&
+          file.leafName == "sessionstore.js" || file.leafName == "sessionstore.bak") {
+        this._passthrough = true;
+        return;
+      }
+    } catch (e) { // permission denied to access filename 
+    }
+    
+    this._passthrough = false;
+    this._store().init(file,ioFlags,perm,behaviorFlags);
+  },
+};
+ 
+const StoreWrapperFactory = {
+
+  createInstance: function(aOuter, aIID) {
+    if (aOuter != null) {
+      Components.returnCode = Cr.NS_ERROR_NO_AGGREGATION;
+      return null;
+    }
+    
+    return (new StoreWrapper()).QueryInterface(aIID);
+  },
+  
+  lockFactory: function(aLock) { },
+  
+  QueryInterface: function(aIID) {
+    if (!aIID.equals(Ci.nsISupports) && !aIID.equals(Ci.nsIModule) &&
+        !aIID.equals(Ci.nsIFactory)) {
+      dump("Bad QI: "+aIID.toString());
+      Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
+      return null;
+    }
+    
+    return this;
+  }
+};
+
+
+
+/**
+ * JS XPCOM component registration goop:
+ *
+ * Everything below is boring boilerplate and can probably be ignored.
+ */
+
+var StoreWrapperModule = new Object();
+
+StoreWrapperModule.registerSelf = 
+function (compMgr, fileSpec, location, type){
+  var nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar;
+  compMgr = compMgr.QueryInterface(nsIComponentRegistrar);
+  compMgr.registerFactoryLocation(kMODULE_CID,
+                                  kMODULE_NAME,
+                                  kMODULE_CONTRACTID,
+                                  fileSpec, 
+                                  location, 
+                                  type);
+};
+
+StoreWrapperModule.getClassObject = function (compMgr, cid, iid)
+{
+  if (cid.equals(kMODULE_CID)) {
+    return StoreWrapperFactory;
+  }
+  Components.returnCode = Cr.NS_ERROR_NOT_REGISTERED;
+  return null;
+};
+
+StoreWrapperModule.canUnload = function (compMgr)
+{
+  return true;
+};
+
+function NSGetModule(compMgr, fileSpec)
+{
+  return StoreWrapperModule;
+}
+



More information about the tor-commits mailing list