Verified Commit c946e894 authored by Pier Angelo Vendrame's avatar Pier Angelo Vendrame 🎃
Browse files

fixup! Bug 42247: Android helpers for the TorProvider

Switch from a gate based on the update channel to a gate based on a
pref.
Also, draft the JS part for TorSettings events.
parent 7518949f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ ChromeUtils.defineESModuleGetters(lazy, {
  PdfJs: "resource://pdf.js/PdfJs.sys.mjs",
  Preferences: "resource://gre/modules/Preferences.sys.mjs",
  RFPHelper: "resource://gre/modules/RFPHelper.sys.mjs",
  TorAndroidIntegration: "resource://gre/modules/TorAndroidIntegration.sys.mjs",
  TorDomainIsolator: "resource://gre/modules/TorDomainIsolator.sys.mjs",
});

@@ -259,6 +260,7 @@ class GeckoViewStartup {
          "GeckoView:SetLocale",
        ]);

        lazy.TorAndroidIntegration.init();
        lazy.TorDomainIsolator.init();

        Services.obs.addObserver(this, "browser-idle-startup-tasks-finished");
+16 −0
Original line number Diff line number Diff line
@@ -476,6 +476,11 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
      getSettings().mSecurityLevel.set(level);
      return this;
    }

    public @NonNull Builder useNewBootstrap(final boolean flag) {
      getSettings().mUseNewBootstrap.set(flag);
      return this;
    }
  }

  private GeckoRuntime mRuntime;
@@ -527,6 +532,8 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
  /* package */ final Pref<Integer> mSpoofEnglish = new Pref<>("privacy.spoof_english", 0);
  /* package */ final Pref<Integer> mSecurityLevel =
      new Pref<>("browser.security_level.security_slider", 4);
  /* package */ final Pref<Boolean> mUseNewBootstrap =
      new Pref<>("browser.tor_android.use_new_bootstrap", false);

  /* package */ int mPreferredColorScheme = COLOR_SCHEME_SYSTEM;

@@ -1319,6 +1326,15 @@ public final class GeckoRuntimeSettings extends RuntimeSettings {
    return this;
  }

  public boolean getUseNewBootstrap() {
    return mUseNewBootstrap.get();
  }

  public @NonNull GeckoRuntimeSettings setUseNewBootstrap(final boolean flag) {
    mUseNewBootstrap.commit(flag);
    return this;
  }

  @Override // Parcelable
  public void writeToParcel(final Parcel out, final int flags) {
    super.writeToParcel(out, flags);
+108 −0
Original line number Diff line number Diff line
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { ConsoleAPI } from "resource://gre/modules/Console.sys.mjs";

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  EventDispatcher: "resource://gre/modules/Messaging.sys.mjs",
  TorConnect: "resource://gre/modules/TorConnect.sys.mjs",
  TorProviderBuilder: "resource://gre/modules/TorProviderBuilder.sys.mjs",
  TorSettings: "resource://gre/modules/TorSettings.sys.mjs",
});

const Prefs = Object.freeze({
  useNewBootstrap: "browser.tor_android.use_new_bootstrap",
  logLevel: "browser.tor_android.log_level",
});

const logger = new ConsoleAPI({
  maxLogLevel: "info",
  maxLogLevelPref: Prefs.logLevel,
  prefix: "TorAndroidIntegration",
});

const ListenedEvents = Object.freeze({
  settingsGet: "GeckoView:Tor:SettingsGet",
  settingsSet: "GeckoView:Tor:SettingsSet",
  settingsApply: "GeckoView:Tor:SettingsApply",
  settingsSave: "GeckoView:Tor:SettingsSave",
});

class TorAndroidIntegrationImpl {
  #initialized = false;

  init() {
    lazy.EventDispatcher.instance.registerListener(
      this,
      Object.values(ListenedEvents)
    );

    this.#bootstrapMethodReset();
    Services.prefs.addObserver(Prefs.useNewBootstrap, this);
  }

  async #initNewBootstrap() {
    if (this.#initialized) {
      return;
    }
    this.#initialized = true;

    lazy.TorProviderBuilder.init().finally(() => {
      lazy.TorProviderBuilder.firstWindowLoaded();
    });
    try {
      await lazy.TorSettings.init();
      await lazy.TorConnect.init();
    } catch (e) {
      logger.error("Cannot initialize TorSettings or TorConnect", e);
    }
  }

  observe(subj, topic, data) {
    switch (topic) {
      case "nsPref:changed":
        if (data === Prefs.useNewBootstrap) {
          this.#bootstrapMethodReset();
        }
        break;
    }
  }

  async onEvent(event, data, callback) {
    logger.debug(`Received event ${event}`, data);
    try {
      switch (event) {
        case settingsGet:
          callback?.onSuccess(lazy.TorSettings.getSettings());
          return;
        case settingsSet:
          // This does not throw, so we do not have any way to report the error!
          lazy.TorSettings.setSettings(data);
          break;
        case settingsApply:
          await lazy.TorSettings.applySettings();
          break;
        case settingsSave:
          await lazy.TorSettings.saveSettings();
          break;
      }
      callback?.onSuccess();
    } catch (e) {
      logger.error();
      callback?.sendError(e);
    }
  }

  #bootstrapMethodReset() {
    if (Services.prefs.getBoolPref(Prefs.useNewBootstrap, false)) {
      this.#initNewBootstrap();
    } else {
      Services.prefs.clearUserPref("network.proxy.socks");
      Services.prefs.clearUserPref("network.proxy.socks_port");
    }
  }
}

export const TorAndroidIntegration = new TorAndroidIntegrationImpl();
+1 −0
Original line number Diff line number Diff line
@@ -213,6 +213,7 @@ EXTRA_JS_MODULES += [
    "Sqlite.sys.mjs",
    "SubDialog.sys.mjs",
    "Timer.sys.mjs",
    "TorAndroidIntegration.sys.mjs",
    "TorConnect.sys.mjs",
    "TorSettings.sys.mjs",
    "TorStrings.sys.mjs",