[tbb-commits] [tor-browser/tor-browser-78.11.0esr-10.5-1] fixup! Bug 27476: Implement about:torconnect captive portal within Tor Browser

sysrqb at torproject.org sysrqb at torproject.org
Thu Jun 10 20:13:01 UTC 2021


commit a4e2202fedb82e19d931cfaebf0dec9092a5480d
Author: Richard Pospesel <richard at torproject.org>
Date:   Tue Jun 8 18:08:00 2021 +0200

    fixup! Bug 27476: Implement about:torconnect captive portal within Tor Browser
---
 browser/components/torconnect/TorConnectParent.jsm | 14 ++++++
 .../torconnect/content/aboutTorConnect.css         |  4 ++
 .../torconnect/content/aboutTorConnect.js          | 50 ++++++++++++++++++++--
 .../torconnect/content/aboutTorConnect.xhtml       |  5 +++
 .../components/torpreferences/content/torPane.js   | 45 ++++++++++++++++---
 .../torpreferences/content/torPane.xhtml           | 10 +++--
 browser/modules/TorStrings.jsm                     |  3 ++
 toolkit/modules/AsyncPrefs.jsm                     |  2 +
 toolkit/modules/RemotePageAccessManager.jsm        |  7 +++
 9 files changed, 129 insertions(+), 11 deletions(-)

diff --git a/browser/components/torconnect/TorConnectParent.jsm b/browser/components/torconnect/TorConnectParent.jsm
index 03b258608b5c..f775507a744f 100644
--- a/browser/components/torconnect/TorConnectParent.jsm
+++ b/browser/components/torconnect/TorConnectParent.jsm
@@ -29,6 +29,10 @@ const gActiveTopics = [
   kTorLogHasWarnOrErrTopic,
 ];
 
+const gTorLauncherPrefs = {
+  quickstart: "extensions.torlauncher.quickstart",
+}
+
 class TorConnectParent extends JSWindowActorParent {
   constructor(...args) {
     super(...args);
@@ -47,6 +51,16 @@ class TorConnectParent extends JSWindowActorParent {
     for (const topic of gActiveTopics) {
       Services.obs.addObserver(this.gObserver, topic);
     }
+
+    this.quickstartObserver = {
+      observe(aSubject, aTopic, aData) {
+        if (aTopic === "nsPref:changed" &&
+            aData == gTorLauncherPrefs.quickstart) {
+          self.sendAsyncMessage("TorQuickstartPrefChanged", Services.prefs.getBoolPref(gTorLauncherPrefs.quickstart));
+        }
+      },
+    }
+    Services.prefs.addObserver(gTorLauncherPrefs.quickstart, this.quickstartObserver);
   }
 
   willDestroy() {
diff --git a/browser/components/torconnect/content/aboutTorConnect.css b/browser/components/torconnect/content/aboutTorConnect.css
index eb4277f2ce5e..2081d4f6c4b8 100644
--- a/browser/components/torconnect/content/aboutTorConnect.css
+++ b/browser/components/torconnect/content/aboutTorConnect.css
@@ -35,6 +35,10 @@
   width: 50%;
 }
 
+#quickstartCheckbox, #quickstartCheckboxLabel {
+  vertical-align: middle;
+}
+
 #copyLogButton {
   position: relative;
 }
diff --git a/browser/components/torconnect/content/aboutTorConnect.js b/browser/components/torconnect/content/aboutTorConnect.js
index ad398ccd86f9..5089bbc9363b 100644
--- a/browser/components/torconnect/content/aboutTorConnect.js
+++ b/browser/components/torconnect/content/aboutTorConnect.js
@@ -8,6 +8,12 @@ const kTorProcessDidNotStartTopic = "TorProcessDidNotStart";
 const kTorBootstrapStatusTopic = "TorBootstrapStatus";
 const kTorBootstrapErrorTopic = "TorBootstrapError";
 const kTorLogHasWarnOrErrTopic = "TorLogHasWarnOrErr";
+const kTorQuickstartPrefChanged = "TorQuickstartPrefChanged";
+
+const TorLauncherPrefs = {
+  quickstart: "extensions.torlauncher.quickstart",
+  prompt_at_startup: "extensions.torlauncher.prompt_at_startup",
+}
 
 class AboutTorConnect {
   log(...args) {
@@ -34,6 +40,12 @@ class AboutTorConnect {
   get elemProgressMeter() {
     return this.getElem("progressBackground");
   }
+  get elemQuickstartCheckbox() {
+    return this.getElem("quickstartCheckbox");
+  }
+  get elemQuickstartLabel() {
+    return this.getElem("quickstartCheckboxLabel");
+  }
   get elemConnectButton() {
     return this.getElem("connectButton");
   }
@@ -82,7 +94,7 @@ class AboutTorConnect {
   setInitialUI() {
     this.setTitle(this.torStrings.torConnect.torConnect);
     this.elemProgressDesc.textContent =
-      this.torStrings.settings.torPreferencesDescription;
+      this.torStrings.settings.quickstartDescription;
     this.showElem(this.elemConnectButton);
     this.showElem(this.elemAdvancedButton);
     this.hideElem(this.elemCopyLogButton);
@@ -223,12 +235,19 @@ class AboutTorConnect {
     document.title = title;
   }
 
-  initButtons() {
+  async initElements() {
     this.elemAdvancedButton.textContent = this.torStrings.torConnect.torConfigure;
     this.elemAdvancedButton.addEventListener("click", () => {
       RPMSendAsyncMessage("OpenTorAdvancedPreferences");
     });
 
+    this.elemQuickstartLabel.textContent = this.torStrings.settings.quickstartCheckbox;
+    this.elemQuickstartCheckbox.addEventListener("change", () => {
+      const quickstart = this.elemQuickstartCheckbox.checked;
+      RPMSetBoolPref(TorLauncherPrefs.quickstart, quickstart);
+    });
+    this.elemQuickstartCheckbox.checked = await RPMGetBoolPref(TorLauncherPrefs.quickstart);
+
     this.elemConnectButton.textContent =
       this.torStrings.torConnect.torConnectButton;
     this.elemConnectButton.addEventListener("click", () => {
@@ -277,6 +296,21 @@ class AboutTorConnect {
     RPMAddMessageListener(kTorBootstrapStatusTopic, ({ data }) => {
       this.updateBootstrapProgress(data);
     });
+    RPMAddMessageListener(kTorQuickstartPrefChanged, ({ data }) => {
+      // update checkbox with latest quickstart pref value
+      this.elemQuickstartCheckbox.checked = data;
+    })
+  }
+
+  initKeyboardShortcuts() {
+    document.onkeydown = (evt) => {
+      // unfortunately it looks like we still haven't standardized keycodes to
+      // integers, so we must resort to a string compare here :(
+      // see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code for relevant documentation
+      if (evt.code === "Escape") {
+        this.stopTorBootstrap();
+      }
+    };
   }
 
   async init() {
@@ -285,13 +319,23 @@ class AboutTorConnect {
       "dir",
       await RPMSendQuery("GetDirection")
     );
-    this.initButtons();
+    this.initElements();
     this.initObservers();
+    this.initKeyboardShortcuts();
     this.state = AboutTorConnect.STATE_INITIAL;
 
     // Request the most recent bootstrap status info so that a
     // TorBootstrapStatus notification is generated as soon as possible.
     RPMSendAsyncMessage("TorRetrieveBootstrapStatus");
+
+    // quickstart is the user set pref for starting tor automatically
+    // prompt_at_startup will be set to false after successful bootstrap, and true on error
+    // by tor-launcher, so we want to keep the connect screen up when prompt_at_startup is true
+    ///  even if quickstart is enabled so user can potentially resolve errors on next launch
+    if (await RPMGetBoolPref(TorLauncherPrefs.quickstart) &&
+       !await RPMGetBoolPref(TorLauncherPrefs.prompt_at_startup)) {
+      this.connect();
+    }
   }
 }
 
diff --git a/browser/components/torconnect/content/aboutTorConnect.xhtml b/browser/components/torconnect/content/aboutTorConnect.xhtml
index e0f813f62d67..d12c896c8959 100644
--- a/browser/components/torconnect/content/aboutTorConnect.xhtml
+++ b/browser/components/torconnect/content/aboutTorConnect.xhtml
@@ -28,6 +28,11 @@
           </div>
         </div>
 
+        <div id="quickstartContainer">
+          <input id="quickstartCheckbox" type="checkbox" />
+          <label id="quickstartCheckboxLabel" for="quickstartCheckbox"/>
+        </div>
+
         <div id="connectButtonContainer" class="button-container">
           <button id="advancedButton" hidden="true"></button>
           <button id="copyLogButton" hidden="true">
diff --git a/browser/components/torpreferences/content/torPane.js b/browser/components/torpreferences/content/torPane.js
index 66213ceb7789..63db876a7ccd 100644
--- a/browser/components/torpreferences/content/torPane.js
+++ b/browser/components/torpreferences/content/torPane.js
@@ -53,6 +53,10 @@ const { parsePort, parseBridgeStrings, parsePortList } = ChromeUtils.import(
   "chrome://browser/content/torpreferences/parseFunctions.jsm"
 );
 
+const TorLauncherPrefs = {
+  quickstart: "extensions.torlauncher.quickstart",
+}
+
 /*
   Tor Pane
 
@@ -71,8 +75,12 @@ const gTorPane = (function() {
     },
     torPreferences: {
       header: "h1#torPreferences-header",
-      description: "span#torPreferences-description",
-      learnMore: "label#torPreferences-learnMore",
+    },
+    quickstart: {
+      header: "h2#torPreferences-quickstart-header",
+      description: "span#torPreferences-quickstart-description",
+      learnMore: "label#torPreferences-quickstart-learnMore",
+      enableQuickstartCheckbox: "checkbox#torPreferences-quickstart-toggle",
     },
     bridges: {
       header: "h2#torPreferences-bridges-header",
@@ -122,6 +130,7 @@ const gTorPane = (function() {
     _messageBox: null,
     _messageBoxMessage: null,
     _messageBoxButton: null,
+    _enableQuickstartCheckbox: null,
     _useBridgeCheckbox: null,
     _bridgeSelectionRadiogroup: null,
     _builtinBridgeOption: null,
@@ -210,11 +219,15 @@ const gTorPane = (function() {
       // Heading
       prefpane.querySelector(selectors.torPreferences.header).innerText =
         TorStrings.settings.torPreferencesHeading;
-      prefpane.querySelector(selectors.torPreferences.description).textContent =
-        TorStrings.settings.torPreferencesDescription;
+
+      // Quickstart
+      prefpane.querySelector(selectors.quickstart.header).innerText =
+        TorStrings.settings.quickstartHeading;
+      prefpane.querySelector(selectors.quickstart.description).textContent =
+        TorStrings.settings.quickstartDescription;
       {
         let learnMore = prefpane.querySelector(
-          selectors.torPreferences.learnMore
+          selectors.quickstart.learnMore
         );
         learnMore.setAttribute("value", TorStrings.settings.learnMore);
         learnMore.setAttribute(
@@ -222,6 +235,19 @@ const gTorPane = (function() {
           TorStrings.settings.learnMoreTorBrowserURL
         );
       }
+      this._enableQuickstartCheckbox = prefpane.querySelector(
+        selectors.quickstart.enableQuickstartCheckbox
+      );
+      this._enableQuickstartCheckbox.setAttribute(
+        "label",
+        TorStrings.settings.quickstartCheckbox
+      );
+      this._enableQuickstartCheckbox.addEventListener("command", e => {
+        const checked = this._enableQuickstartCheckbox.checked;
+        Services.prefs.setBoolPref(TorLauncherPrefs.quickstart, checked);
+      });
+      this._enableQuickstartCheckbox.checked = Services.prefs.getBoolPref(TorLauncherPrefs.quickstart);
+      Services.prefs.addObserver(TorLauncherPrefs.quickstart, this);
 
       // Bridge setup
       prefpane.querySelector(selectors.bridges.header).innerText =
@@ -583,6 +609,15 @@ const gTorPane = (function() {
     // Callbacks
     //
 
+    // callback for when the quickstart pref changes
+    observe(subject, topic, data) {
+      if (topic != "nsPref:changed") return;
+      if (data === TorLauncherPrefs.quickstart) {
+        this._enableQuickstartCheckbox.checked =
+          Services.prefs.getBoolPref(TorLauncherPrefs.quickstart);
+      }
+    },
+
     // callback when using bridges toggled
     onToggleBridge(enabled) {
       this._useBridgeCheckbox.checked = enabled;
diff --git a/browser/components/torpreferences/content/torPane.xhtml b/browser/components/torpreferences/content/torPane.xhtml
index 88f82c37a3c9..996325e08bf7 100644
--- a/browser/components/torpreferences/content/torPane.xhtml
+++ b/browser/components/torpreferences/content/torPane.xhtml
@@ -33,12 +33,16 @@
   <html:h1 id="torPreferences-header"/>
 </hbox>
 
-<groupbox data-category="paneTor"
+<!-- Quickstart -->
+<groupbox id="torPreferences-quickstart-group"
+          data-category="paneTor"
           hidden="true">
+  <html:h2 id="torPreferences-quickstart-header"/>
   <description flex="1">
-    <html:span id="torPreferences-description" class="tail-with-learn-more"/>
-    <label id="torPreferences-learnMore" class="learnMore text-link" is="text-link"/>
+    <html:span id="torPreferences-quickstart-description"/>
+    <label id="torPreferences-quickstart-learnMore" class="learnMore text-link" is="text-link"/>
   </description>
+  <checkbox id="torPreferences-quickstart-toggle"/>
 </groupbox>
 
 <!-- Bridges -->
diff --git a/browser/modules/TorStrings.jsm b/browser/modules/TorStrings.jsm
index 407a220278c4..e7597bbfa74f 100644
--- a/browser/modules/TorStrings.jsm
+++ b/browser/modules/TorStrings.jsm
@@ -257,6 +257,9 @@ var TorStrings = {
         "Tor Browser routes your traffic over the Tor Network, run by thousands of volunteers around the world."
       ),
       learnMore: getString("torPreferences.learnMore", "Learn More"),
+      quickstartHeading: getString("torPreferences.quickstart", "Quickstart"),
+      quickstartDescription: getString("torPreferences.quickstartDescription", "Tor Browser routes your traffic over the Tor Network, run by thousands of volunteers around the world. Quickstart allows Tor Browser to connect automatically."),
+      quickstartCheckbox : getString("torPreferences.quickstartCheckbox", "Always connect automatically"),
       bridgesHeading: getString("torPreferences.bridges", "Bridges"),
       bridgesDescription: getString(
         "torPreferences.bridgesDescription",
diff --git a/toolkit/modules/AsyncPrefs.jsm b/toolkit/modules/AsyncPrefs.jsm
index aca86556cd5e..b81ff5e22b9b 100644
--- a/toolkit/modules/AsyncPrefs.jsm
+++ b/toolkit/modules/AsyncPrefs.jsm
@@ -18,6 +18,8 @@ const kAllowedPrefs = new Set([
   "testing.allowed-prefs.some-char-pref",
   "testing.allowed-prefs.some-int-pref",
 
+  "extensions.torlauncher.quickstart",
+
   "narrate.rate",
   "narrate.voice",
 
diff --git a/toolkit/modules/RemotePageAccessManager.jsm b/toolkit/modules/RemotePageAccessManager.jsm
index 8a6c0911a060..ee21aa7a750f 100644
--- a/toolkit/modules/RemotePageAccessManager.jsm
+++ b/toolkit/modules/RemotePageAccessManager.jsm
@@ -198,6 +198,13 @@ let RemotePageAccessManager = {
         "TorIsNetworkDisabled",
         "TorLogHasWarnOrErr",
       ],
+      RPMGetBoolPref: [
+        "extensions.torlauncher.quickstart",
+        "extensions.torlauncher.prompt_at_startup",
+      ],
+      RPMSetBoolPref: [
+        "extensions.torlauncher.quickstart",
+      ],
     },
   },
 





More information about the tbb-commits mailing list