[tbb-commits] [torbutton/master] Bug 21999: Fix display of language prompt for TBB/ESR52

gk at torproject.org gk at torproject.org
Tue Jul 11 12:58:59 UTC 2017


commit 90c35ef999de8b9ee09876f5772a2a728d2bafc1
Author: Arthur Edelstein <arthuredelstein at gmail.com>
Date:   Fri Jun 16 00:24:29 2017 -0700

    Bug 21999: Fix display of language prompt for TBB/ESR52
---
 src/chrome/content/torbutton.js | 116 ++++++++++++++++++----------------------
 1 file changed, 52 insertions(+), 64 deletions(-)

diff --git a/src/chrome/content/torbutton.js b/src/chrome/content/torbutton.js
index 3999bd4..b14585b 100644
--- a/src/chrome/content/torbutton.js
+++ b/src/chrome/content/torbutton.js
@@ -12,7 +12,7 @@ let { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
 let { showDialog } = Cu.import("resource://torbutton/modules/utils.js", {});
 let { unescapeTorString } = Cu.import("resource://torbutton/modules/utils.js", {});
 let SecurityPrefs = Cu.import("resource://torbutton/modules/security-prefs.js", {});
-let { bindPrefAndInit } = Cu.import("resource://torbutton/modules/utils.js", {});
+let { bindPrefAndInit, observe } = Cu.import("resource://torbutton/modules/utils.js", {});
 
 const k_tb_last_browser_version_pref = "extensions.torbutton.lastBrowserVersion";
 const k_tb_browser_update_needed_pref = "extensions.torbutton.updateNeeded";
@@ -1997,6 +1997,8 @@ function torbutton_is_windowed(wind) {
     return true;
 }
 
+let stopLanguagePromptObserver;
+
 // Bug 1506 P3: This is needed pretty much only for the version check
 // and the window resizing. See comments for individual functions for
 // details
@@ -2031,11 +2033,12 @@ function torbutton_new_window(event)
     }
 
     // If the default language is not English and we have not already asked,
-    // add a web progress listener that will show a "request English language
-    // web pages?" prompt the first time an http or https page is opened.
+    // add an http-on-modify-request observer that will show a "request English
+    // language web pages?" prompt the first time a content http or https page
+    // is opened.
     if (torbutton_should_prompt_for_language_preference()) {
-      progress.addProgressListener(torbutton_langPromptListener,
-                                   Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
+      stopLanguagePromptObserver = observe("http-on-modify-request",
+                                           torbutton_http_connection_observed);
     }
 
     // Check the version on every new window. We're already pinging check in these cases.    
@@ -2210,68 +2213,53 @@ var torbutton_resizelistener =
   onSecurityChange: function() {}
 };
 
-var torbutton_langPromptListener =
-{
-  QueryInterface: function(aIID)
-  {
-   if (aIID.equals(Ci.nsIWebProgressListener) ||
-       aIID.equals(Ci.nsISupportsWeakReference) ||
-       aIID.equals(Ci.nsISupports))
-     return this;
-   throw Cr.NS_NOINTERFACE;
-  },
-
-  onLocationChange: function(aProgress, aRequest, aURI) {},
+function torbutton_http_connection_observed(aRequest, aData) {
+  // If we are loading an HTTP page from content, show the
+  // "request English language web pages?" prompt.
+  try {
+    let httpChannel = aRequest.QueryInterface(Ci.nsIHttpChannel);
+    if (!aRequest.URI.schemeIs("http") && !aRequest.URI.schemeIs("https")) {
+      return;
+    }
+    if (!httpChannel) return;
+    let notificationCallbacks = httpChannel.notificationCallbacks;
+    if (!notificationCallbacks) return;
+    let loadContext = notificationCallbacks.getInterface(Ci.nsILoadContext);
+    if (!loadContext) return;
+    if (!loadContext.isContent) return;
+    // The above QI did not throw, the scheme is http[s], and we know the
+    // load context is content, so we must have a true HTTP request from content.
+    // Stop the observer and display the prompt if another window has
+    // not already done so.
+    stopLanguagePromptObserver();
 
-  onStateChange: function(aProgress, aRequest, aFlag, aStatus) {
-    if (aFlag & Ci.nsIWebProgressListener.STATE_START) {
-      // If we are loading an HTTP page, show the
-      // "request English language web pages?" prompt.
-      try {
-        let httpChannel = aRequest.QueryInterface(Ci.nsIHttpChannel);
-
-        // The above QI did not throw, so we must have an HTTP request.
-        // Remove this listener and display the prompt if another window has
-        // not already done so.
-        let progress = Cc["@mozilla.org/docloaderservice;1"]
-                         .getService(Ci.nsIWebProgress);
-        progress.removeProgressListener(torbutton_langPromptListener,
-                            Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
-
-        if (torbutton_should_prompt_for_language_preference()) {
-          if (torbutton_is_homepage_url(aRequest.URI)) {
-            // If the homepage is being loaded, display the prompt after a
-            // delay to avoid a problem where a blank prompt is displayed.
-            // In this case, the homepage will be loaded using the current
-            // spoof English setting, which is OK.
-            setTimeout(function() {
-              if (torbutton_should_prompt_for_language_preference())
-                torbutton_prompt_for_language_preference();
-            }, 2000);
-          } else {
-            // No delay is needed. Display the prompt and fix up the
-            // Accept-Language header before allowing the load to continue.
+    if (torbutton_should_prompt_for_language_preference()) {
+      if (torbutton_is_homepage_url(aRequest.URI)) {
+        // If the homepage is being loaded, display the prompt after a
+        // delay to avoid a problem where a blank prompt is displayed.
+        // In this case, the homepage will be loaded using the current
+        // spoof English setting, which is OK.
+        setTimeout(function() {
+          if (torbutton_should_prompt_for_language_preference())
             torbutton_prompt_for_language_preference();
-
-            // The Accept-Language header for this request was set when the
-            // channel was created. Reset it to match the value that will be
-            // used for future requests.
-            let val = torbutton_get_current_accept_language_value(aRequest.URI);
-            if (val)
-              httpChannel.setRequestHeader("Accept-Language", val, false);
-          }
-        }
-      } catch (e) {}
+        }, 2000);
+      } else {
+        // No delay is needed. Display the prompt and fix up the
+        // Accept-Language header before allowing the load to continue.
+        torbutton_prompt_for_language_preference();
+
+        // The Accept-Language header for this request was set when the
+        // channel was created. Reset it to match the value that will be
+        // used for future requests.
+        let val = torbutton_get_current_accept_language_value(aRequest.URI);
+        if (val)
+          httpChannel.setRequestHeader("Accept-Language", val, false);
+      }
     }
-  },
-
-  onProgressChange: function(aProgress, aRequest, curSelfProgress,
-                             maxSelfProgress, curTotalProgress,
-                             maxTotalProgress) {},
-  onStatusChange: function(aProgress, aRequest, stat, message) {},
-  onSecurityChange: function() {}
-};
-
+  } catch (e) {
+    torbutton_log(3, e.message);
+  }
+}
 
 // aURI should be an http or https nsIURI object.
 function torbutton_get_current_accept_language_value(aURI)





More information about the tbb-commits mailing list