Verified Commit 7f2e764f authored by henry's avatar henry Committed by ma1
Browse files

fixup! TB 31286: Implementation of bridge, proxy, and firewall settings in...

fixup! TB 31286: Implementation of bridge, proxy, and firewall settings in about:preferences#connection

TB 43462: Make the internet status in `about:preferences` update
automatically without any user input.

We listen to the status via TorConnect.
parent 09cac933
Loading
Loading
Loading
Loading
+19 −84
Original line number Diff line number Diff line
@@ -22,13 +22,9 @@ const { TorProviderBuilder, TorProviderTopics } = ChromeUtils.importESModule(
  "resource://gre/modules/TorProviderBuilder.sys.mjs"
);

const { TorConnect, TorConnectTopics, TorConnectStage, TorCensorshipLevel } =
const { InternetStatus, TorConnect, TorConnectTopics, TorConnectStage } =
  ChromeUtils.importESModule("resource://gre/modules/TorConnect.sys.mjs");

const { MoatRPC } = ChromeUtils.importESModule(
  "resource://gre/modules/Moat.sys.mjs"
);

const { QRCode } = ChromeUtils.importESModule(
  "resource://gre/modules/QRCode.sys.mjs"
);
@@ -2375,12 +2371,6 @@ const gNetworkStatus = {
    this._internetResultEl = this._internetAreaEl.querySelector(
      ".network-status-result"
    );
    this._internetTestButton = document.getElementById(
      "network-status-internet-test-button"
    );
    this._internetTestButton.addEventListener("click", () => {
      this._startInternetTest();
    });

    this._torAreaEl = document.getElementById("network-status-tor-area");
    this._torResultEl = this._torAreaEl.querySelector(".network-status-result");
@@ -2391,10 +2381,11 @@ const gNetworkStatus = {
      TorConnect.openTorConnect({ beginBootstrapping: "soft" });
    });

    this._updateInternetStatus("unknown");
    this._updateInternetStatus();
    this._updateTorConnectionStatus();

    Services.obs.addObserver(this, TorConnectTopics.StageChange);
    Services.obs.addObserver(this, TorConnectTopics.InternetStatusChange);
  },

  /**
@@ -2402,98 +2393,42 @@ const gNetworkStatus = {
   */
  uninit() {
    Services.obs.removeObserver(this, TorConnectTopics.StageChange);
    Services.obs.removeObserver(this, TorConnectTopics.InternetStatusChange);
  },

  observe(subject, topic) {
    switch (topic) {
      // triggered when tor connect state changes and we may
      // need to update the messagebox
      case TorConnectTopics.StageChange: {
      case TorConnectTopics.StageChange:
        this._updateTorConnectionStatus();
        break;
      }
    }
  },

  /**
   * Whether the test should be disabled.
   *
   * @type {boolean}
   */
  _internetTestDisabled: false,
  /**
   * Start the internet test.
   */
  async _startInternetTest() {
    if (this._internetTestDisabled) {
      return;
    }
    this._internetTestDisabled = true;
    // We use "aria-disabled" rather than the "disabled" attribute so that the
    // button can remain focusable during the test.
    // TODO: Replace with moz-button when it handles this for us. See
    // tor-browser#43275.
    this._internetTestButton.setAttribute("aria-disabled", "true");
    this._internetTestButton.classList.add("spoof-button-disabled");
    this._internetTestButton.tabIndex = -1;
    try {
      this._updateInternetStatus("testing");
      const mrpc = new MoatRPC();
      let status = null;
      try {
        await mrpc.init();
        status = await mrpc.testInternetConnection();
      } catch (err) {
        log.error("Error while checking the Internet connection", err);
      } finally {
        mrpc.uninit();
      }
      if (status) {
        this._updateInternetStatus(status.successful ? "online" : "offline");
      } else {
        this._updateInternetStatus("unknown");
      }
    } finally {
      this._internetTestButton.removeAttribute("aria-disabled");
      this._internetTestButton.classList.remove("spoof-button-disabled");
      this._internetTestButton.tabIndex = 0;
      this._internetTestDisabled = false;
      case TorConnectTopics.InternetStatusChange:
        this._updateInternetStatus();
        break;
    }
  },

  /**
   * Update the shown internet status.
   *
   * @param {string} stateName - The name of the state to show.
   */
  _updateInternetStatus(stateName) {
  _updateInternetStatus() {
    let l10nId;
    switch (stateName) {
      case "testing":
        l10nId = "tor-connection-internet-status-testing";
        break;
      case "offline":
    let isOffline = false;
    switch (TorConnect.internetStatus) {
      case InternetStatus.Offline:
        l10nId = "tor-connection-internet-status-offline";
        isOffline = true;
        break;
      case "online":
      case InternetStatus.Online:
        l10nId = "tor-connection-internet-status-online";
        break;
      default:
        l10nId = "tor-connection-internet-status-unknown";
        break;
    }
    if (l10nId) {
    this._internetResultEl.setAttribute("data-l10n-id", l10nId);
    } else {
      this._internetResultEl.removeAttribute("data-l10n-id");
      this._internetResultEl.textContent = "";
    }

    this._internetAreaEl.classList.toggle(
      "status-loading",
      stateName === "testing"
    );
    this._internetAreaEl.classList.toggle(
      "status-offline",
      stateName === "offline"
    );
    this._internetAreaEl.classList.toggle("status-offline", isOffline);
  },

  /**
+11 −23
Original line number Diff line number Diff line
@@ -31,28 +31,16 @@
      aria-labelledby="network-status-internet-area-label"
    >
      <html:img alt="" class="network-status-icon" />
      <!-- Use an aria-live area to announce "Internet: Online" or
         - "Internet: Offline". We only expect this to change when triggered by
         - the user clicking the "Test" button, so shouldn't occur unexpectedly.
         -->
      <html:div
        class="network-status-live-area"
        aria-live="polite"
        aria-atomic="true"
      >
      <!-- NOTE: We do not wrap the label and status ("Internet: Offline", etc)
         - in an aria-live area because it may be too noisey and may not be
         - important to the user. -->
      <html:span
        id="network-status-internet-area-label"
        class="network-status-label"
        data-l10n-id="tor-connection-internet-status-label"
      ></html:span>
        <img alt="" class="network-status-loading-icon tor-loading-icon" />
      <html:span class="network-status-result"></html:span>
    </html:div>
      <html:button
        id="network-status-internet-test-button"
        data-l10n-id="tor-connection-internet-status-test-button"
      ></html:button>
    </html:div>
    <html:div
      id="network-status-tor-area"
      class="network-status-area"
@@ -60,8 +48,8 @@
      aria-labelledby="network-status-tor-area-label"
    >
      <html:img alt="" class="network-status-icon" />
      <!-- NOTE: Unlike #network-status-internet-area, we do not wrap the label
         - and status ("Tor network: Not connected", etc) in an aria-live area.
      <!-- NOTE: We do not wrap the label and status
         - ("Tor network: Not connected", etc) in an aria-live area.
         - This is not likely to change whilst this page has focus.
         - Moreover, the status is already present in the torconnect status bar
         - in the window tab bar. -->
+1 −13
Original line number Diff line number Diff line
@@ -90,24 +90,12 @@ button.spoof-button-disabled {
  }
}

.network-status-live-area {
  display: contents;
}

.network-status-label {
  font-weight: bold;
  margin-inline-end: 0.75em;
}

.network-status-loading-icon {
  margin-inline-end: 0.5em;
}

#network-status-internet-area:not(.status-loading) .network-status-loading-icon {
  display: none;
}

.network-status-result:not(:empty) {
.network-status-result {
  margin-inline-end: 0.75em;
}