Commit 1ad717fe authored by henry's avatar henry
Browse files

fixup! TB 40933: Add tor-launcher functionality

TB 41921: Ensure shouldStartAndOwnTor is constant per-session.

TorConnect and TorSettings `enabled` properties depend on this value,
and shouldn't change per session.
parent 06cbcd5d
Loading
Loading
Loading
Loading
+36 −12
Original line number Diff line number Diff line
@@ -17,8 +17,6 @@ const kPropBundleURI = "chrome://torbutton/locale/torlauncher.properties";
const kPropNamePrefix = "torlauncher.";
const kIPCDirPrefName = "extensions.torlauncher.tmp_ipc_dir";

let gStringBundle = null;

/**
 * This class allows to lookup for the paths of the various files that are
 * needed or can be used with the tor daemon, such as its configuration, the
@@ -332,7 +330,7 @@ class TorFile {
  }
}

export const TorLauncherUtil = Object.freeze({
export const TorLauncherUtil = {
  get isAndroid() {
    return Services.appinfo.OS === "Android";
  },
@@ -417,6 +415,8 @@ export const TorLauncherUtil = Object.freeze({
    return this.showConfirm(null, s, defaultBtnLabel, cancelBtnLabel);
  },

  _stringBundle: null,

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

@@ -425,6 +425,9 @@ export const TorLauncherUtil = Object.freeze({
    if (!aStringName) {
      return aStringName;
    }
    if (!this._stringBundle) {
      this._stringBundle = Services.strings.createBundle(kPropBundleURI);
    }
    try {
      const key = kPropNamePrefix + aStringName;
      return this._stringBundle.GetStringFromName(key);
@@ -587,7 +590,12 @@ export const TorLauncherUtil = Object.freeze({
    Services.prefs.savePrefFile(null);
  },

  get shouldStartAndOwnTor() {
  /**
   * Determine the current value for whether we should start and own Tor.
   *
   * @returns {boolean} Whether we should start and own Tor.
   */
  _getShouldStartAndOwnTor() {
    const kPrefStartTor = "extensions.torlauncher.start_tor";
    try {
      const kBrowserToolboxPort = "MOZ_BROWSER_TOOLBOX_PORT";
@@ -610,6 +618,29 @@ export const TorLauncherUtil = Object.freeze({
    return Services.prefs.getBoolPref(kPrefStartTor, true);
  },

  /**
   * Cached value for shouldStartAndOwnTor, or `null` if not yet initialised.
   *
   * @type {boolean}
   */
  _shouldStartAndOwnTor: null,

  /**
   * Whether we should start and own Tor.
   *
   * The value should be constant per-session.
   *
   * @type {boolean}
   */
  get shouldStartAndOwnTor() {
    // Do not want this value to change within the same session, so always used
    // the cached valued if it is available.
    if (this._shouldStartAndOwnTor === null) {
      this._shouldStartAndOwnTor = this._getShouldStartAndOwnTor();
    }
    return this._shouldStartAndOwnTor;
  },

  get shouldShowNetworkSettings() {
    try {
      const kEnvForceShowNetConfig = "TOR_FORCE_NET_CONFIG";
@@ -668,11 +699,4 @@ export const TorLauncherUtil = Object.freeze({
      console.warn("Could not remove the IPC directory", e);
    }
  },

  get _stringBundle() {
    if (!gStringBundle) {
      gStringBundle = Services.strings.createBundle(kPropBundleURI);
    }
    return gStringBundle;
  },
});
};