Commit 6877b274 authored by clairehurst's avatar clairehurst 🌱
Browse files

fixup! TB 42247: Android helpers for the TorProvider

parent 4f30e0a4
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -52,6 +52,8 @@ public class TorIntegrationAndroid implements BundleEventListener {
  private static final String EVENT_BOOTSTRAP_BEGIN_AUTO = "GeckoView:Tor:BootstrapBeginAuto";
  private static final String EVENT_BOOTSTRAP_CANCEL = "GeckoView:Tor:BootstrapCancel";
  private static final String EVENT_BOOTSTRAP_GET_STATE = "GeckoView:Tor:BootstrapGetState";
  private static final String EVENT_QUICKSTART_GET = "GeckoView:Tor:QuickstartGet";
  private static final String EVENT_QUICKSTART_SET = "GeckoView:Tor:QuickstartSet";

  private static final String CONTROL_PORT_FILE = "/control-ipc";
  private static final String SOCKS_FILE = "/socks-ipc";
@@ -682,6 +684,23 @@ public class TorIntegrationAndroid implements BundleEventListener {
    return EventDispatcher.getInstance().queryVoid(EVENT_SETTINGS_SET, bundle);
  }

  public interface QuickstartGetter {
    void onValue(boolean enabled);
  }

  public void quickstartGet(QuickstartGetter quickstartGetter) {
    EventDispatcher.getInstance().queryBoolean(EVENT_QUICKSTART_GET).then(enabled -> {
      quickstartGetter.onValue(Boolean.TRUE.equals(enabled));
      return new GeckoResult<Void>();
    });
  }

  public @NonNull GeckoResult<Void> quickstartSet(boolean enabled) {
    final GeckoBundle bundle = new GeckoBundle(1);
    bundle.putBoolean("enabled", enabled);
    return EventDispatcher.getInstance().queryVoid(EVENT_QUICKSTART_SET, bundle);
  }

  public @NonNull GeckoResult<Void> beginBootstrap() {
    return EventDispatcher.getInstance().queryVoid(EVENT_BOOTSTRAP_BEGIN);
  }
+0 −9
Original line number Diff line number Diff line
@@ -101,8 +101,6 @@ public class TorSettings {

  public boolean enabled = true;

  public boolean quickstart = false;

  // bridges section
  public boolean bridgesEnabled = false;
  public BridgeSource bridgesSource = BridgeSource.Invalid;
@@ -125,7 +123,6 @@ public class TorSettings {

  public TorSettings(GeckoBundle bundle) {
    try {
      GeckoBundle qs = bundle.getBundle("quickstart");
      GeckoBundle bridges = bundle.getBundle("bridges");
      GeckoBundle proxy = bundle.getBundle("proxy");
      GeckoBundle firewall = bundle.getBundle("firewall");
@@ -135,8 +132,6 @@ public class TorSettings {
      bridgesBuiltinType = BridgeBuiltinType.fromString(bridges.getString("builtin_type"));
      bridgeBridgeStrings = bridges.getStringArray("bridge_strings");

      quickstart = qs.getBoolean("enabled");

      firewallEnabled = firewall.getBoolean("enabled");
      firewallAllowedPorts = firewall.getIntArray("allowed_ports");

@@ -156,7 +151,6 @@ public class TorSettings {
  public GeckoBundle asGeckoBundle() {
    GeckoBundle bundle = new GeckoBundle();

    GeckoBundle qs = new GeckoBundle();
    GeckoBundle bridges = new GeckoBundle();
    GeckoBundle proxy = new GeckoBundle();
    GeckoBundle firewall = new GeckoBundle();
@@ -166,8 +160,6 @@ public class TorSettings {
    bridges.putString("builtin_type", bridgesBuiltinType.toString());
    bridges.putStringArray("bridge_strings", bridgeBridgeStrings);

    qs.putBoolean("enabled", quickstart);

    firewall.putBoolean("enabled", firewallEnabled);
    firewall.putIntArray("allowed_ports", firewallAllowedPorts);

@@ -178,7 +170,6 @@ public class TorSettings {
    proxy.putInt("port", proxyPort);
    proxy.putInt("type", proxyType.toInt());

    bundle.putBundle("quickstart", qs);
    bundle.putBundle("bridges", bridges);
    bundle.putBundle("proxy", proxy);
    bundle.putBundle("firewall", firewall);
+0 −3
Original line number Diff line number Diff line
@@ -24,9 +24,6 @@ public class TorLegacyAndroidSettings {
    // always true, tor is enabled in TB
    settings.enabled = true;

    // firefox-android disconnected quick start a while ago so it's untracked
    settings.quickstart = false;

    settings.bridgesEnabled = Prefs.bridgesEnabled();

    // tor-android-service CustomTorInstaller.java
+25 −45
Original line number Diff line number Diff line
@@ -41,12 +41,19 @@ const ListenedEvents = Object.freeze({
  bootstrapBeginAuto: "GeckoView:Tor:BootstrapBeginAuto",
  bootstrapCancel: "GeckoView:Tor:BootstrapCancel",
  bootstrapGetState: "GeckoView:Tor:BootstrapGetState",
  quickstartGet: "GeckoView:Tor:QuickstartGet",
  quickstartSet: "GeckoView:Tor:QuickstartSet",
});

class TorAndroidIntegrationImpl {
  #initialized = false;

  async init() {
  /**
   * Register our listeners.
   * We want this function to block GeckoView initialization, so it should not be
   * async. Any async task should be moved to #deferredInit, instead.
   */
  init() {
    if (this.#initialized) {
      logger.warn("Something tried to initilize us again.");
      return;
@@ -74,6 +81,14 @@ class TorAndroidIntegrationImpl {
    // by TorProviderBuilder.init.
    lazy.TorProviderBuilder.firstWindowLoaded();

    this.#deferredInit();
  }

  /**
   * Perform our init tasks that should not block the initialization of
   * GeckoView. This function will not be awaited, so errors can only be logged.
   */
  async #deferredInit() {
    try {
      await lazy.TorSettings.init();
      await lazy.TorConnect.init();
@@ -82,24 +97,6 @@ class TorAndroidIntegrationImpl {
    }
  }

  /**
   * Combine the current TorSettings settings with the TorConnect settings.
   *
   * @returns {object} The TorSettings in an object, which also has a
   *   `quickstart.enabled` property.
   */
  // This is added for backward compatibility with TorSettings.getSettings prior
  // to tor-browser#41921, when it used to control the quickstart setting.
  // TODO: Have android separate out the request for TorConnect.quickstart. In
  // principle, this would allow android tor connect UI to be loaded before
  // TorSettings has initialized (the SettingsReady signal), similar to desktop.
  // See tor-browser#43408.
  #getAllSettings() {
    const settings = lazy.TorSettings.getSettings();
    settings.quickstart = { enabled: lazy.TorConnect.quickstart };
    return settings;
  }

  observe(subj, topic) {
    switch (topic) {
      // TODO: Replace with StageChange.
@@ -142,7 +139,7 @@ class TorAndroidIntegrationImpl {
      case lazy.TorSettingsTopics.Ready:
        lazy.EventDispatcher.instance.sendRequest({
          type: EmittedEvents.settingsReady,
          settings: this.#getAllSettings(),
          settings: lazy.TorSettings.getSettings(),
        });
        break;
      case lazy.TorSettingsTopics.SettingsChanged:
@@ -151,20 +148,7 @@ class TorAndroidIntegrationImpl {
        lazy.EventDispatcher.instance.sendRequest({
          type: EmittedEvents.settingsChanged,
          changes: subj.wrappedJSObject.changes ?? [],
          settings: this.#getAllSettings(),
        });
        break;
      case lazy.TorConnectTopics.QuickstartChange:
        // We also include the TorSettings, and a `changes` Array similar to
        // SettingsChanged signal. This is for backward compatibility with
        // TorSettings.getSettings prior to tor-browser#41921, when it used to
        // control the quickstart setting.
        // TODO: Have android separate out the request for TorConnect.quickstart.
        // See tor-browser#43408.
        lazy.EventDispatcher.instance.sendRequest({
          type: EmittedEvents.settingsChanged,
          changes: ["quickstart.enabled"],
          settings: this.#getAllSettings(),
          settings: lazy.TorSettings.getSettings(),
        });
        break;
    }
@@ -175,19 +159,9 @@ class TorAndroidIntegrationImpl {
    try {
      switch (event) {
        case ListenedEvents.settingsGet:
          callback?.onSuccess(this.#getAllSettings());
          callback?.onSuccess(lazy.TorSettings.getSettings());
          return;
        case ListenedEvents.settingsSet:
          // TODO: Set quickstart via a separate event. See tor-browser#43408.
          // NOTE: Currently this may trigger GeckoView:Tor:SettingsChanged
          // twice: once for quickstart.enabled, and again for the other
          // settings.
          if (
            "quickstart" in data.settings &&
            "enabled" in data.settings.quickstart
          ) {
            lazy.TorConnect.quickstart = data.settings.quickstart.enabled;
          }
          // TODO: Handle setting throw? This can throw if data.settings is
          // incorrectly formatted, but more like it can throw when the settings
          // fail to be passed onto the TorProvider. tor-browser#43405.
@@ -211,6 +185,12 @@ class TorAndroidIntegrationImpl {
          return;
        // TODO: Expose TorConnect.startAgain() to allow users to begin
        // from the start again.
        case ListenedEvents.quickstartGet:
          callback?.onSuccess(lazy.TorConnect.quickstart);
          return;
        case ListenedEvents.quickstartSet:
          lazy.TorConnect.quickstart = data.enabled;
          break;
      }
      callback?.onSuccess();
    } catch (e) {