[tbb-commits] [torbutton/master] Bug 30888: move torbutton_util.js to modules/utils.js

gk at torproject.org gk at torproject.org
Mon Nov 25 14:44:16 UTC 2019


commit ed3fafebab28d005e22208aa921e5f5c7c54153f
Author: Alex Catarineu <acat at torproject.org>
Date:   Mon Jun 17 19:16:47 2019 +0200

    Bug 30888: move torbutton_util.js to modules/utils.js
---
 chrome/content/preferences.xhtml      |  1 -
 chrome/content/tor-circuit-display.js |  9 +---
 chrome/content/torbutton.js           | 32 +++++++-------
 chrome/content/torbutton_util.js      | 79 -----------------------------------
 components/external-app-blocker.js    | 16 +++----
 modules/utils.js                      | 54 +++++++++++++++++++++++-
 6 files changed, 80 insertions(+), 111 deletions(-)

diff --git a/chrome/content/preferences.xhtml b/chrome/content/preferences.xhtml
index 6f662f5f..eafbf01e 100644
--- a/chrome/content/preferences.xhtml
+++ b/chrome/content/preferences.xhtml
@@ -6,7 +6,6 @@
     <title>&torbutton.prefs.security_settings;</title>
     <link type="text/css" rel="stylesheet" charset="UTF-8" href="chrome://torbutton/skin/preferences.css"/>
     <link type="text/css" rel="stylesheet" charset="UTF-8" href="chrome://torbutton/skin/preferences-mobile.css"/>
-    <script type="text/javascript" src="torbutton_util.js"></script>
     <script type="text/javascript" src="preferences-mobile.js"></script>
     <style>
     </style>
diff --git a/chrome/content/tor-circuit-display.js b/chrome/content/tor-circuit-display.js
index 1e58b2b4..19b55d18 100644
--- a/chrome/content/tor-circuit-display.js
+++ b/chrome/content/tor-circuit-display.js
@@ -32,7 +32,7 @@ const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 let { controller } = ChromeUtils.import("resource://torbutton/modules/tor-control-port.js", {});
 
 // Utility functions
-let { bindPrefAndInit, observe, getLocale, getDomainForBrowser } = ChromeUtils.import("resource://torbutton/modules/utils.js", {});
+let { bindPrefAndInit, observe, getLocale, getDomainForBrowser, torbutton_get_property_string } = ChromeUtils.import("resource://torbutton/modules/utils.js", {});
 
 // Make the TorButton logger available.
 let logger = Cc["@torproject.org/torbutton-logger;1"]
@@ -204,15 +204,10 @@ let collectBrowserCredentials = function () {
 
 // ## User interface
 
-// __torbuttonBundle__.
-// Bundle of localized strings for torbutton UI.
-let torbuttonBundle = Services.strings.createBundle(
-                        "chrome://torbutton/locale/torbutton.properties");
-
 // __uiString__.
 // Read the localized strings for this UI.
 let uiString = function (shortName) {
-  return torbuttonBundle.GetStringFromName("torbutton.circuit_display." + shortName);
+  return torbutton_get_property_string("torbutton.circuit_display." + shortName);
 };
 
 // __localizedCountryNameFromCode(countryCode)__.
diff --git a/chrome/content/torbutton.js b/chrome/content/torbutton.js
index e75584cf..0ca37789 100644
--- a/chrome/content/torbutton.js
+++ b/chrome/content/torbutton.js
@@ -1,3 +1,9 @@
+// window globals
+var torbutton_init;
+var torbutton_new_circuit;
+var torbutton_new_identity;
+
+(() => {
 // Bug 1506 P1-P5: This is the main Torbutton overlay file. Much needs to be
 // preserved here, but in an ideal world, most of this code should perhaps be
 // moved into an XPCOM service, and much can also be tossed. See also
@@ -7,18 +13,20 @@
 // TODO: Double-check there are no strange exploits to defeat:
 //       http://kb.mozillazine.org/Links_to_local_pages_don%27t_work
 
-/* global torbutton_log, gBrowser, torbutton_safelog, CustomizableUI,
-   createTorCircuitDisplay, torbutton_get_property_string, gFindBarInitialized,
-   gFindBar, OpenBrowserWindow, PrivateBrowsingUtils, torbutton_get_stringbundle,
+/* global gBrowser, CustomizableUI,
+   createTorCircuitDisplay, gFindBarInitialized,
+   gFindBar, OpenBrowserWindow, PrivateBrowsingUtils,
    Services, AppConstants
  */
 
 let {
-  showDialog,
   show_torbrowser_manual,
   unescapeTorString,
   bindPrefAndInit,
   getDomainForBrowser,
+  torbutton_safelog,
+  torbutton_log,
+  torbutton_get_property_string,
 } = ChromeUtils.import("resource://torbutton/modules/utils.js", {});
 let SecurityPrefs = ChromeUtils.import("resource://torbutton/modules/security-prefs.js", {});
 let { configureControlPortModule } = Cu.import("resource://torbutton/modules/tor-control-port.js", {});
@@ -32,15 +40,10 @@ var m_tb_prefs = Services.prefs;
 
 // status
 var m_tb_wasinited = false;
-var m_tb_plugin_string = false;
 var m_tb_is_main_window = false;
-var m_tb_hidden_browser = false;
 
 var m_tb_confirming_plugins = false;
 
-var m_tb_window_height = window.outerHeight;
-var m_tb_window_width = window.outerWidth;
-
 var m_tb_control_ipc_file = null;    // Set if using IPC (UNIX domain socket).
 var m_tb_control_port = null;        // Set if using TCP.
 var m_tb_control_host = null;        // Set if using TCP.
@@ -232,7 +235,7 @@ function torbutton_is_mobile() {
 // Bug 1506 P2-P4: This code sets some version variables that are irrelevant.
 // It does read out some important environment variables, though. It is
 // called once per browser window.. This might belong in a component.
-function torbutton_init() {
+torbutton_init = function() {
     torbutton_log(3, 'called init()');
 
     SecurityPrefs.initialize();
@@ -746,7 +749,7 @@ function torbutton_send_ctrl_cmd(command) {
 }
 
 // Bug 1506 P4: Needed for New IP Address
-function torbutton_new_circuit() {
+torbutton_new_circuit = function() {
   let firstPartyDomain = getDomainForBrowser(gBrowser.selectedBrowser);
 
   let domainIsolator = Cc["@torproject.org/domain-isolator;1"]
@@ -758,7 +761,7 @@ function torbutton_new_circuit() {
 }
 
 // Bug 1506 P4: Needed for New Identity.
-async function torbutton_new_identity() {
+torbutton_new_identity = async function() {
   try {
     // Make sure that we can only click once on New Identiy to avoid race
     // conditions leading to failures (see bug 11783 for an example).
@@ -1720,9 +1723,6 @@ function torbutton_new_window(event)
       return;
     }
 
-    m_tb_window_height = window.outerHeight;
-    m_tb_window_width = window.outerWidth;
-
     if (!m_tb_wasinited) {
         torbutton_init();
     }
@@ -1906,5 +1906,5 @@ function torbutton_init_user_manual_links() {
     torbutton_abouttor_message_handler.updateAllOpenPages();
   });
 }
-
+})();
 //vim:set ts=4
diff --git a/chrome/content/torbutton_util.js b/chrome/content/torbutton_util.js
deleted file mode 100644
index e6446cde..00000000
--- a/chrome/content/torbutton_util.js
+++ /dev/null
@@ -1,79 +0,0 @@
-// Bug 1506 P1-P3: These utility functions might be useful, but
-// you probably just want to rewrite them or use the underlying
-// code directly. I don't see any of them as essential for 1506,
-// really.
-
-var m_tb_torlog = Cc["@torproject.org/torbutton-logger;1"]
-.getService(Ci.nsISupports).wrappedJSObject;
-
-var m_tb_string_bundle = torbutton_get_stringbundle();
-
-function torbutton_safelog(nLevel, sMsg, scrub) {
-    m_tb_torlog.safe_log(nLevel, sMsg, scrub);
-    return true;
-}
-
-function torbutton_log(nLevel, sMsg) {
-    m_tb_torlog.log(nLevel, sMsg);
-
-    // So we can use it in boolean expressions to determine where the 
-    // short-circuit is..
-    return true; 
-}
-
-// get a preferences branch object
-// FIXME: this is lame.
-function torbutton_get_prefbranch(branch_name) {
-    var o_prefs = false;
-    var o_branch = false;
-
-    torbutton_log(1, "called get_prefbranch()");
-    o_prefs = Services.prefs;
-    if (!o_prefs) {
-        torbutton_log(5, "Failed to get preferences-service!");
-        return false;
-    }
-
-    o_branch = o_prefs.getBranch(branch_name);
-    if (!o_branch)
-    {
-        torbutton_log(5, "Failed to get prefs branch!");
-        return false;
-    }
-
-    return o_branch;
-}
-
-// load localization strings
-function torbutton_get_stringbundle()
-{
-    var o_stringbundle = false;
-
-    try {
-        var oBundle = Services.strings;
-        o_stringbundle = oBundle.createBundle("chrome://torbutton/locale/torbutton.properties");
-    } catch(err) {
-        o_stringbundle = false;
-    }
-    if (!o_stringbundle) {
-        torbutton_log(5, 'ERROR (init): failed to find torbutton-bundle');
-    }
-
-    return o_stringbundle;
-}
-
-function torbutton_get_property_string(propertyname)
-{
-    try { 
-        if (!m_tb_string_bundle) {
-            m_tb_string_bundle = torbutton_get_stringbundle();
-        }
-
-        return m_tb_string_bundle.GetStringFromName(propertyname);
-    } catch(e) {
-        torbutton_log(4, "Unlocalized string "+propertyname);
-    }
-
-    return propertyname;
-}
-
diff --git a/components/external-app-blocker.js b/components/external-app-blocker.js
index afd5c002..9d5cbd79 100644
--- a/components/external-app-blocker.js
+++ b/components/external-app-blocker.js
@@ -16,6 +16,8 @@ const { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm
 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 const { PromptUtils } = ChromeUtils.import("resource://gre/modules/SharedPromptUtils.jsm");
 
+let { torbutton_get_property_string } = ChromeUtils.import("resource://torbutton/modules/utils.js", {});
+
 // Module specific constants
 const kMODULE_NAME = "Torbutton External App Handler";
 const kCONTRACT_ID = "@torproject.org/torbutton-extAppBlocker;1";
@@ -82,13 +84,13 @@ ExternalAppBlocker.prototype =
       parentWin = Services.wm.getMostRecentWindow("navigator:browser");
     }
 
-    let title = parentWin.torbutton_get_property_string("torbutton.popup.external.title");
-    let app = parentWin.torbutton_get_property_string("torbutton.popup.external.app");
-    let note = parentWin.torbutton_get_property_string("torbutton.popup.external.note");
-    let suggest = parentWin.torbutton_get_property_string("torbutton.popup.external.suggest");
-    let launch = parentWin.torbutton_get_property_string("torbutton.popup.launch");
-    let cancel = parentWin.torbutton_get_property_string("torbutton.popup.cancel");
-    let dontask = parentWin.torbutton_get_property_string("torbutton.popup.dontask");
+    let title = torbutton_get_property_string("torbutton.popup.external.title");
+    let app = torbutton_get_property_string("torbutton.popup.external.app");
+    let note = torbutton_get_property_string("torbutton.popup.external.note");
+    let suggest = torbutton_get_property_string("torbutton.popup.external.suggest");
+    let launch = torbutton_get_property_string("torbutton.popup.launch");
+    let cancel = torbutton_get_property_string("torbutton.popup.cancel");
+    let dontask = torbutton_get_property_string("torbutton.popup.dontask");
 
     let args = {
       promptType:       "confirmEx",
diff --git a/modules/utils.js b/modules/utils.js
index 0caf475f..b726342b 100644
--- a/modules/utils.js
+++ b/modules/utils.js
@@ -239,6 +239,58 @@ var getDomainForBrowser = (browser) => {
   return fpd;
 };
 
+var m_tb_torlog = Cc["@torproject.org/torbutton-logger;1"]
+.getService(Ci.nsISupports).wrappedJSObject;
+
+var m_tb_string_bundle = torbutton_get_stringbundle();
+
+function torbutton_safelog(nLevel, sMsg, scrub) {
+    m_tb_torlog.safe_log(nLevel, sMsg, scrub);
+    return true;
+}
+
+function torbutton_log(nLevel, sMsg) {
+    m_tb_torlog.log(nLevel, sMsg);
+
+    // So we can use it in boolean expressions to determine where the
+    // short-circuit is..
+    return true;
+}
+
+// load localization strings
+function torbutton_get_stringbundle()
+{
+    var o_stringbundle = false;
+
+    try {
+        var oBundle = Services.strings;
+        o_stringbundle = oBundle.createBundle("chrome://torbutton/locale/torbutton.properties");
+    } catch(err) {
+        o_stringbundle = false;
+    }
+    if (!o_stringbundle) {
+        torbutton_log(5, 'ERROR (init): failed to find torbutton-bundle');
+    }
+
+    return o_stringbundle;
+}
+
+function torbutton_get_property_string(propertyname)
+{
+    try {
+        if (!m_tb_string_bundle) {
+            m_tb_string_bundle = torbutton_get_stringbundle();
+        }
+
+        return m_tb_string_bundle.GetStringFromName(propertyname);
+    } catch(e) {
+        torbutton_log(4, "Unlocalized string "+propertyname);
+    }
+
+    return propertyname;
+}
+
 // Export utility functions for external use.
 let EXPORTED_SYMBOLS = ["bindPref", "bindPrefAndInit", "getEnv", "getLocale", "getDomainForBrowser",
-                        "getPrefValue", "observe", "showDialog", "show_torbrowser_manual", "unescapeTorString"];
+                        "getPrefValue", "observe", "showDialog", "show_torbrowser_manual", "unescapeTorString",
+                        "torbutton_safelog", "torbutton_log", "torbutton_get_property_string"];



More information about the tbb-commits mailing list