[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [torbutton/maint-1.2] Implement Tor livemark blocking for FF3.5+.



Author: Mike Perry <mikeperry-git@xxxxxxxxxx>
Date: Mon, 14 Dec 2009 14:36:40 -0800
Subject: Implement Tor livemark blocking for FF3.5+.
Commit: fc9088ae401ed88e0bbf7759fd4303157ecf019b

Also add pref UI for disabling livemarks.
---
 src/chrome/content/preferences.js       |    2 +
 src/chrome/content/preferences.xul      |    3 +
 src/chrome/content/torbutton.js         |   28 +++++-
 src/chrome/locale/en/torbutton.dtd      |    4 +
 src/components/block-livemarks.js       |  163 +++++++++++++++++++++++++++++++
 src/defaults/preferences/preferences.js |    2 +-
 6 files changed, 197 insertions(+), 5 deletions(-)
 create mode 100644 src/components/block-livemarks.js

diff --git a/src/chrome/content/preferences.js b/src/chrome/content/preferences.js
index b87d5eb..c19eb39 100644
--- a/src/chrome/content/preferences.js
+++ b/src/chrome/content/preferences.js
@@ -257,6 +257,7 @@ function torbutton_prefs_init(doc) {
     doc.getElementById('torbutton_blockNonTorForms').checked = o_torprefs.getBoolPref('block_ntforms');
     doc.getElementById('torbutton_isolateContent').checked = o_torprefs.getBoolPref('isolate_content');
     doc.getElementById('torbutton_noSearch').checked = o_torprefs.getBoolPref('no_search');
+    doc.getElementById('torbutton_disableLivemarks').checked = o_torprefs.getBoolPref('disable_livemarks');
     doc.getElementById('torbutton_closeTor').checked = o_torprefs.getBoolPref('close_tor');
     doc.getElementById('torbutton_closeNonTor').checked = o_torprefs.getBoolPref('close_nontor');
     doc.getElementById('torbutton_noUpdates').checked = o_torprefs.getBoolPref('no_updates');
@@ -424,6 +425,7 @@ function torbutton_prefs_save(doc) {
     o_torprefs.setBoolPref('block_tforms', doc.getElementById('torbutton_blockTorForms').checked);
     o_torprefs.setBoolPref('block_ntforms', doc.getElementById('torbutton_blockNonTorForms').checked);
     o_torprefs.setBoolPref('no_search', doc.getElementById('torbutton_noSearch').checked);
+    o_torprefs.setBoolPref('disable_livemarks', doc.getElementById('torbutton_disableLivemarks').checked);
     o_torprefs.setBoolPref('close_tor', doc.getElementById('torbutton_closeTor').checked);
     o_torprefs.setBoolPref('close_nontor', doc.getElementById('torbutton_closeNonTor').checked);
     o_torprefs.setBoolPref('no_updates', doc.getElementById('torbutton_noUpdates').checked);
diff --git a/src/chrome/content/preferences.xul b/src/chrome/content/preferences.xul
index 840a534..f8c3f06 100644
--- a/src/chrome/content/preferences.xul
+++ b/src/chrome/content/preferences.xul
@@ -177,6 +177,9 @@
                   oncommand="torbutton_prefs_set_field_attributes(document)"/>
         <checkbox id="torbutton_noSearch" label="&torbutton.prefs.no_search;" 
                   oncommand="torbutton_prefs_set_field_attributes(document)"/>
+        <checkbox id="torbutton_disableLivemarks"
+                  label="&torbutton.prefs.disable_livemarks; &torbutton.prefs.dtd_recommended;" 
+                  oncommand="torbutton_prefs_set_field_attributes(document)"/>
         <checkbox id="torbutton_blockTorFileNet" label="&torbutton.prefs.block_tor_file_net;" 
                   oncommand="torbutton_prefs_set_field_attributes(document)"/>
         <checkbox id="torbutton_blockNonTorFileNet" label="&torbutton.prefs.block_nontor_file_net;" 
diff --git a/src/chrome/content/torbutton.js b/src/chrome/content/torbutton.js
index 0b9ee16..1159c4d 100644
--- a/src/chrome/content/torbutton.js
+++ b/src/chrome/content/torbutton.js
@@ -279,6 +279,7 @@ var torbutton_unique_pref_observer =
                 }
                 break;
 
+            case "extensions.torbutton.disable_livemarks":
             case "extensions.torbutton.spoof_english":
                 torbutton_log(1, "Got update message, updating status");
                 torbutton_update_status(
@@ -1296,14 +1297,23 @@ function torbutton_update_status(mode, force_update) {
                 "full_page_plugins", m_tb_plugin_string, false, changed);
     }
 
+    if(m_tb_ff35) {
+      var livemarks = Cc["@mozilla.org/browser/livemark-service;2"].
+                        getService(Ci.nsILivemarkService);
+      if (mode &&
+           m_tb_prefs.getBoolPref("extensions.torbutton.disable_livemarks")) {
+        livemarks.stopUpdateLivemarks();
+        torbutton_log(3, "Disabled livemarks");
+      } else {
+        livemarks.start();
+        torbutton_log(3, "Enabled livemarks");
+      }
+    }
+
     // No need to clear cookies if just updating prefs
     if(!changed && force_update)
         return;
 
-    // XXX: Pref for this? Hrmm.. It's broken anyways
-    torbutton_setIntPref("browser.bookmarks.livemark_refresh_seconds",
-            "livemark_refresh", 31536000, mode, changed);
-
     torbutton_set_timezone(mode, false);
 
     // This call also has to be here for 3rd party proxy changers.
@@ -2942,6 +2952,16 @@ function torbutton_do_startup()
         }
         var tor_enabled = torbutton_check_status();
 
+        if(m_tb_ff35 &&
+           m_tb_prefs.getBoolPref("extensions.torbutton.disable_livemarks")) {
+          var livemarks = Cc["@mozilla.org/browser/livemark-service;2"].
+                            getService(Ci.nsILivemarkService);
+          if (tor_enabled) {
+            livemarks.stopUpdateLivemarks();
+            torbutton_log(3, "Disabled livemarks");
+          }
+        }
+
         torbutton_set_timezone(tor_enabled, true);
 
         // FIXME: This is probably better done by reimplementing the 
diff --git a/src/chrome/locale/en/torbutton.dtd b/src/chrome/locale/en/torbutton.dtd
index 9b18387..7183cca 100644
--- a/src/chrome/locale/en/torbutton.dtd
+++ b/src/chrome/locale/en/torbutton.dtd
@@ -93,3 +93,7 @@
 <!ENTITY torbutton.prefs.socks_vfive "SOCKS v5">
 <!ENTITY torbutton.prefs.no_proxies_on "No Proxies for: ">
 <!ENTITY torbutton.prefs.no_proxy_warning "Warning: Avoid using any hostnames above">
+<!ENTITY torbutton.prefs.disable_livemarks "Disable Livemark updates during Tor usage">
+<!ENTITY torbutton.prefs.dtd_recommended "(recommended)">
+<!ENTITY torbutton.prefs.dtd_optional "(optional)">
+<!ENTITY torbutton.prefs.dtd_crucial "(crucial)">
diff --git a/src/components/block-livemarks.js b/src/components/block-livemarks.js
new file mode 100644
index 0000000..d9986dc
--- /dev/null
+++ b/src/components/block-livemarks.js
@@ -0,0 +1,163 @@
+/*************************************************************************
+ * Block Livemarks (JavaScript XPCOM component)
+ *
+ * The livemark service start() method is run on a 5 second delay timer
+ * after startup. The safest way to block this is to hook the component.
+ *************************************************************************/
+
+// Module specific constants
+const kMODULE_NAME = "Block Tor Livemarks";
+const kMODULE_CONTRACTID = "@mozilla.org/browser/livemark-service;2";
+const kMODULE_CID = Components.ID("24892630-d5f3-4af8-9662-e1d6479c5290");
+
+/* Mozilla defined interfaces for FF3.x */
+const kREAL_CID = "{dca61eb5-c7cd-4df1-b0fb-d0722baba251}";
+
+const kImplementedInterfaces = [ "nsIFactory",
+                                 "nsILivemarkService",
+                                 "nsINavBookmarkObserver" ];
+const Cr = Components.results;
+
+function LivemarkWrapper() {
+  this.logger = Components.classes["@torproject.org/torbutton-logger;1"]
+      .getService(Components.interfaces.nsISupports).wrappedJSObject;
+  this.logger.log(3, "Component Load 6: New LivemarkWrapper "+kMODULE_CONTRACTID);
+
+  // assuming we're running under Firefox
+  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);
+
+  this._real_service = Components.classesByID[kREAL_CID];
+  this._interfaces = kImplementedInterfaces;
+
+  this._prefs = Components.classes["@mozilla.org/preferences-service;1"]
+      .getService(Components.interfaces.nsIPrefBranch);
+
+  this._service = function() {
+    var service = this._real_service.getService();
+    for (var i = 0; i < this._interfaces.length; i++) {
+      service.QueryInterface(Components.interfaces[this._interfaces[i]]);
+    }
+    return service;
+  };
+
+  this.copyMethods(this._service());
+}
+
+LivemarkWrapper.prototype =
+{
+  QueryInterface: function(iid) {
+    if (iid.equals(Components.interfaces.nsISupports)) {
+      return this;
+    }
+
+    var service = this._service().QueryInterface(iid);
+    this.copyMethods(service);
+    return this;
+  },
+
+  /*
+   * Copies methods from the true service object we are wrapping
+   */
+  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;
+          if(params.length) call = "("+params.join().replace(/(?:)/g,function(){return "p"+(++x)})+")";
+          else call = "()";
+          var fun = "(function "+call+"{if (arguments.length < "+wrapped[method].length+") return Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS; try { return wrapped."+method+".apply(wrapped, arguments);} catch(e) { return e.result; }})";
+          newObj[method] = eval(fun);
+       } else {
+          newObj.__defineGetter__(method, function() { return wrapped[method]; });
+          newObj.__defineSetter__(method, function(val) { wrapped[method] = val; });
+      }
+    };
+    for (var method in wrapped) {
+      if(typeof(this[method]) == "undefined") mimic(this, method);
+    }
+  },
+
+  /* Because start runs on a delayed timer at startup, we must hook it block
+   * it */
+  start: function() {
+    if(this._prefs.getBoolPref("extensions.torbutton.disable_livemarks")
+            && this._prefs.getBoolPref("extensions.torbutton.tor_enabled")) {
+      this.logger.log(3, "Blocked livemarks start from component");
+      return;
+    } else {
+      return this._service().start();
+    }
+  }
+};
+
+var LivemarkWrapperSingleton = null;
+var LivemarkWrapperFactory = new Object();
+
+LivemarkWrapperFactory.createInstance = function (outer, iid)
+{
+  if (outer != null) {
+    Components.returnCode = Cr.NS_ERROR_NO_AGGREGATION;
+    return null;
+  }
+
+  if(!LivemarkWrapperSingleton)
+    LivemarkWrapperSingleton = new LivemarkWrapper();
+
+  return LivemarkWrapperSingleton;
+};
+
+
+/**
+ * JS XPCOM component registration goop:
+ *
+ * Everything below is boring boilerplate and can probably be ignored.
+ */
+
+var LivemarkWrapperModule = new Object();
+
+LivemarkWrapperModule.registerSelf =
+function (compMgr, fileSpec, location, type) {
+  var nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar;
+  compMgr = compMgr.QueryInterface(nsIComponentRegistrar);
+
+  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.5a1") >= 0) {
+      compMgr.registerFactoryLocation(kMODULE_CID,
+              kMODULE_NAME,
+              kMODULE_CONTRACTID,
+              fileSpec,
+              location,
+              type);
+  }
+};
+
+LivemarkWrapperModule.getClassObject = function (compMgr, cid, iid)
+{
+  if (cid.equals(kMODULE_CID))
+    return LivemarkWrapperFactory;
+
+  Components.returnCode = Cr.NS_ERROR_NOT_REGISTERED;
+  return null;
+};
+
+LivemarkWrapperModule.canUnload = function (compMgr)
+{
+  return true;
+};
+
+function NSGetModule(compMgr, fileSpec)
+{
+  return LivemarkWrapperModule;
+}
+
diff --git a/src/defaults/preferences/preferences.js b/src/defaults/preferences/preferences.js
index a4480b6..1a96ed4 100644
--- a/src/defaults/preferences/preferences.js
+++ b/src/defaults/preferences/preferences.js
@@ -67,7 +67,6 @@ pref("extensions.torbutton.saved.expire_history", 9);
 pref("extensions.torbutton.saved.download_retention", 2);
 pref("extensions.torbutton.saved.formfill", true);
 pref("extensions.torbutton.saved.remember_signons", true);
-pref("extensions.torbutton.saved.livemark_refresh", 0);
 pref("extensions.torbutton.saved.sendSecureXSiteReferrer", true);
 pref("extensions.torbutton.saved.sendRefererHeader", 2);
 pref("extensions.torbutton.saved.dom_storage", true);
@@ -152,6 +151,7 @@ pref("extensions.torbutton.tor_memory_jar",false);
 pref("extensions.torbutton.nontor_memory_jar",false);
 pref("extensions.torbutton.tz_string","");
 pref("extensions.torbutton.launch_warning",true);
+pref("extensions.torbutton.disable_livemarks",true);
 
 // Google Captcha prefs
 // FIXME: NID cookie?
-- 
1.5.6.5