Commit f767af19 authored by henry's avatar henry Committed by henry
Browse files

fixup! TB 40933: Add tor-launcher functionality

TB 45161: Drop the restart prompt.
parent 5b194faf
Loading
Loading
Loading
Loading
+0 −82
Original line number Diff line number Diff line
@@ -15,8 +15,6 @@ ChromeUtils.defineESModuleGetters(lazy, {
    "moz-src:///toolkit/components/tor-launcher/TorProviderBuilder.sys.mjs",
});

const kPropBundleURI = "chrome://torbutton/locale/torlauncher.properties";
const kPropNamePrefix = "torlauncher.";
const kIPCDirPrefName = "extensions.torlauncher.tmp_ipc_dir";

/**
@@ -374,86 +372,6 @@ export const TorLauncherUtil = {
    return !re.test(path);
  },

  // Returns true if user confirms; false if not.
  showConfirm(aParentWindow, aMsg, aDefaultButtonLabel, aCancelButtonLabel) {
    if (!aParentWindow) {
      aParentWindow = Services.wm.getMostRecentWindow("navigator:browser");
    }

    const ps = Services.prompt;
    const title = this.getLocalizedString("error_title");
    const btnFlags =
      ps.BUTTON_POS_0 * ps.BUTTON_TITLE_IS_STRING +
      ps.BUTTON_POS_0_DEFAULT +
      ps.BUTTON_POS_1 * ps.BUTTON_TITLE_IS_STRING;

    const notUsed = { value: false };
    const btnIndex = ps.confirmEx(
      aParentWindow,
      title,
      aMsg,
      btnFlags,
      aDefaultButtonLabel,
      aCancelButtonLabel,
      null,
      null,
      notUsed
    );
    return btnIndex === 0;
  },

  /**
   * Ask the user whether they desire to restart tor.
   *
   * @param {boolean} initError If we could connect to the control port at
   * least once and we are showing this prompt because the tor process exited
   * suddenly, we will display a different message
   * @returns {boolean} true if the user asked to restart tor
   */
  showRestartPrompt(initError) {
    let s;
    if (initError) {
      const key = "tor_exited_during_startup";
      s = this.getLocalizedString(key);
    } else {
      // tor exited suddenly, so configuration should be okay
      s =
        this.getLocalizedString("tor_exited") +
        "\n\n" +
        this.getLocalizedString("tor_exited2");
    }
    const defaultBtnLabel = this.getLocalizedString("restart_tor");
    let cancelBtnLabel = "OK";
    try {
      const kSysBundleURI = "chrome://global/locale/commonDialogs.properties";
      const sysBundle = Services.strings.createBundle(kSysBundleURI);
      cancelBtnLabel = sysBundle.GetStringFromName(cancelBtnLabel);
    } catch (e) {
      console.warn("Could not localize the cancel button", e);
    }
    return this.showConfirm(null, s, defaultBtnLabel, cancelBtnLabel);
  },

  _stringBundle: null,

  // Localized Strings
  // TODO: Switch to fluent also these ones.

  // "torlauncher." is prepended to aStringName.
  getLocalizedString(aStringName) {
    if (!aStringName) {
      return aStringName;
    }
    if (!this._stringBundle) {
      this._stringBundle = Services.strings.createBundle(kPropBundleURI);
    }
    try {
      const key = kPropNamePrefix + aStringName;
      return this._stringBundle.GetStringFromName(key);
    } catch (e) {}
    return aStringName;
  },

  /**
   * Determine what kind of SOCKS port has been requested for this session or
   * the browser has been configured for.
+0 −83
Original line number Diff line number Diff line
@@ -4,8 +4,6 @@

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  TorLauncherUtil:
    "moz-src:///toolkit/components/tor-launcher/TorLauncherUtil.sys.mjs",
  TorProvider: "moz-src:///toolkit/components/tor-launcher/TorProvider.sys.mjs",
  TorProviderMock:
    "moz-src:///toolkit/components/tor-launcher/TorProviderMock.sys.mjs",
@@ -279,8 +277,6 @@ export class TorProviderBuilder {
      TorProviderTopics.ProviderStateChanged,
      provider.state
    );

    this.#promptProviderState(false);
  }

  /**
@@ -400,85 +396,6 @@ export class TorProviderBuilder {
    this.#replaceProvider();
  }

  // TODO: Remove firstWindowLoaded, #uiReady, #prompting, #promptProviderState
  // and use TorConnect instead. tor-browser#43570.
  /**
   * Check if the provider has been succesfully initialized when the first
   * browser window is shown.
   * This is a workaround we need because ideally we would like the tor process
   * to start as soon as possible, to avoid delays in the about:torconnect page,
   * but we should modify TorConnect and about:torconnect to handle this case
   * there with a better UX.
   */
  static firstWindowLoaded() {
    this.#promptProviderState(true);
  }

  /**
   * Tell whether the browser UI is ready.
   * We ignore any errors until it is because we cannot show them.
   *
   * @type {boolean}
   */
  static #uiReady = false;

  /**
   * Whether we are prompting the user for a restart of the provider.
   *
   * @type {boolean}
   */
  static #prompting = false;

  /**
   * Prompt the user to restart the provider, if this is necessary.
   *
   * @param {boolean} uiReady - Whether this is being called for the first time
   *   when the UI is ready.
   */
  static async #promptProviderState(uiReady) {
    if (uiReady) {
      this.#uiReady = true;
    }
    if (this.#providerData.provider.state === TorProviderState.Running) {
      // Nothing to wait for.
      return;
    }
    if (!this.#uiReady) {
      lazy.logger.warn(
        "Seen exit, but not doing anything because the UI is not ready yet."
      );
      return;
    }
    if (this.#prompting) {
      // Already prompting, so don't duplicate.
      return;
    }

    this.#prompting = true;
    let waitForInit = uiReady;
    let retry = true;
    try {
      while (retry) {
        if (waitForInit) {
          try {
            await this.#providerData.initPromise;
          } catch {}
        }
        if (
          this.#providerData.provider.state === TorProviderState.Stopped &&
          lazy.TorLauncherUtil.showRestartPrompt(uiReady)
        ) {
          waitForInit = true;
          this.replace();
        } else {
          retry = false;
        }
      }
    } finally {
      this.#prompting = false;
    }
  }

  /**
   * Return the provider chosen by the user.
   * This function checks the TOR_PROVIDER environment variable and if it is a