Commit 193cd662 authored by Dan Ballard's avatar Dan Ballard Committed by Pier Angelo Vendrame
Browse files

fixup! TB 42247: Android helpers for the TorProvider

Bug 41188 pt2: strip out legacy TorController logic and shims
parent fcff9732
Loading
Loading
Loading
Loading
+18 −35
Original line number Diff line number Diff line
@@ -39,11 +39,8 @@ public class TorAndroidIntegration implements BundleEventListener {
  private static final String EVENT_TOR_STOP = "GeckoView:Tor:StopTor";
  private static final String EVENT_MEEK_START = "GeckoView:Tor:StartMeek";
  private static final String EVENT_MEEK_STOP = "GeckoView:Tor:StopMeek";
  private static final String EVENT_CONNECT_STATE_CHANGED = "GeckoView:Tor:ConnectStateChanged"; // deprecation path
  private static final String EVENT_CONNECT_STAGE_CHANGED = "GeckoView:Tor:ConnectStageChanged"; // replacement path
  private static final String EVENT_CONNECT_ERROR = "GeckoView:Tor:ConnectError";
  private static final String EVENT_CONNECT_STAGE_CHANGED = "GeckoView:Tor:ConnectStageChanged";
  private static final String EVENT_BOOTSTRAP_PROGRESS = "GeckoView:Tor:BootstrapProgress";
  private static final String EVENT_BOOTSTRAP_COMPLETE = "GeckoView:Tor:BootstrapComplete";
  private static final String EVENT_TOR_LOGS = "GeckoView:Tor:Logs";
  private static final String EVENT_SETTINGS_READY = "GeckoView:Tor:SettingsReady";
  private static final String EVENT_SETTINGS_CHANGED = "GeckoView:Tor:SettingsChanged";
@@ -59,6 +56,7 @@ public class TorAndroidIntegration implements BundleEventListener {
  private static final String EVENT_QUICKSTART_GET = "GeckoView:Tor:QuickstartGet";
  private static final String EVENT_QUICKSTART_SET = "GeckoView:Tor:QuickstartSet";
  private static final String EVENT_REGION_NAMES_GET = "GeckoView:Tor:RegionNamesGet";
  private static final String EVENT_SHOULD_SHOW_TOR_CONNECT = "GeckoView:Tor:ShouldShowTorConnect";

  private static final String CONTROL_PORT_FILE = "/control-ipc";
  private static final String SOCKS_FILE = "/socks-ipc";
@@ -121,11 +119,8 @@ public class TorAndroidIntegration implements BundleEventListener {
            EVENT_MEEK_STOP,
            EVENT_SETTINGS_READY,
            EVENT_SETTINGS_CHANGED,
            EVENT_CONNECT_STATE_CHANGED,
            EVENT_CONNECT_STAGE_CHANGED,
            EVENT_CONNECT_ERROR,
            EVENT_BOOTSTRAP_PROGRESS,
            EVENT_BOOTSTRAP_COMPLETE,
            EVENT_TOR_LOGS);
  }

@@ -154,35 +149,18 @@ public class TorAndroidIntegration implements BundleEventListener {
      } else {
        Log.w(TAG, "Ignoring a settings changed event that did not have the new settings.");
      }
    } else if (EVENT_CONNECT_STATE_CHANGED.equals(event)) {
      String state = message.getString("state");
      for (BootstrapStateChangeListener listener : mBootstrapStateListeners) {
        listener.onBootstrapStateChange(state);
      }
    } else if (EVENT_CONNECT_STAGE_CHANGED.equals(event)) {
      TorConnectStage stage = new TorConnectStage(message.getBundle("stage"));
      _lastKnownStage.setValue(stage);
      for (BootstrapStateChangeListener listener : mBootstrapStateListeners) {
        listener.onBootstrapStageChange(stage);
      }
    } else if (EVENT_CONNECT_ERROR.equals(event)) {
      String code = message.getString("code");
      String msg = message.getString("message");
      String phase = message.getString("phase");
      String reason = message.getString("reason");
      for (BootstrapStateChangeListener listener : mBootstrapStateListeners) {
        listener.onBootstrapError(code, msg, phase, reason);
      }
    } else if (EVENT_BOOTSTRAP_PROGRESS.equals(event)) {
      double progress = message.getDouble("progress");
      boolean hasWarnings = message.getBoolean("hasWarnings");
      for (BootstrapStateChangeListener listener : mBootstrapStateListeners) {
        listener.onBootstrapProgress(progress, hasWarnings);
      }
    } else if (EVENT_BOOTSTRAP_COMPLETE.equals(event)) {
      for (BootstrapStateChangeListener listener : mBootstrapStateListeners) {
        listener.onBootstrapComplete();
      }
    } else if (EVENT_TOR_LOGS.equals(event)) {
      String msg = message.getString("message");
      String type = message.getString("logType");
@@ -644,15 +622,9 @@ public class TorAndroidIntegration implements BundleEventListener {
  }

  public interface BootstrapStateChangeListener {
    void onBootstrapStateChange(String state); // depreaction path

    void onBootstrapStageChange(TorConnectStage stage); // new upgrade
    void onBootstrapStageChange(@NonNull TorConnectStage stage); // new upgrade

    void onBootstrapProgress(double progress, boolean hasWarnings);

    void onBootstrapComplete();

    void onBootstrapError(String code, String message, String phase, String reason);
  }

  public interface TorLogListener {
@@ -733,6 +705,17 @@ public class TorAndroidIntegration implements BundleEventListener {
    });
  }

  public interface ShouldShowTorConnectGetter {
    void onValue(Boolean shouldShowTorConnect);
  }

  public void shouldShowTorConnectGet(ShouldShowTorConnectGetter shouldShowTorConnectGetter) {
    EventDispatcher.getInstance().queryBoolean(EVENT_SHOULD_SHOW_TOR_CONNECT).then(shouldShowTorConnect -> {
      shouldShowTorConnectGetter.onValue(shouldShowTorConnect);
      return new GeckoResult<Void>();
    });
  }

  public @NonNull GeckoResult<Void> beginBootstrap() {
    return EventDispatcher.getInstance().queryVoid(EVENT_BOOTSTRAP_BEGIN);
  }
@@ -751,21 +734,21 @@ public class TorAndroidIntegration implements BundleEventListener {
    return EventDispatcher.getInstance().queryVoid(EVENT_BOOTSTRAP_CANCEL);
  }

  public void registerBootstrapStateChangeListener(BootstrapStateChangeListener listener) {
  public synchronized void registerBootstrapStateChangeListener(BootstrapStateChangeListener listener) {
    mBootstrapStateListeners.add(listener);
  }

  public void unregisterBootstrapStateChangeListener(BootstrapStateChangeListener listener) {
  public synchronized void unregisterBootstrapStateChangeListener(BootstrapStateChangeListener listener) {
    mBootstrapStateListeners.remove(listener);
  }

  private final HashSet<BootstrapStateChangeListener> mBootstrapStateListeners = new HashSet<>();

  public void registerLogListener(TorLogListener listener) {
  public synchronized void registerLogListener(TorLogListener listener) {
    mLogListeners.add(listener);
  }

  public void unregisterLogListener(TorLogListener listener) {
  public synchronized void unregisterLogListener(TorLogListener listener) {
    mLogListeners.remove(listener);
  }

+24 −0
Original line number Diff line number Diff line
package org.mozilla.geckoview;

import org.mozilla.gecko.util.GeckoBundle;

public class TorConnectError {
    public String code;
    public String message;
    public String phase;
    public String reason;

    public TorConnectError(GeckoBundle bundle) {
        code = bundle.getString("code");
        message = bundle.getString("message");
        phase = bundle.getString("phase");
        reason = bundle.getString("reason");
    }

    public TorConnectError(String code, String message, String phase, String reason) {
        this.code = code;
        this.message = message;
        this.phase = phase;
        this.reason = reason;
    }
}
+2 −16
Original line number Diff line number Diff line
@@ -5,24 +5,10 @@ import org.mozilla.gecko.util.GeckoBundle;
// Class to receive ConnectStage object from TorConnect.sys.mjs ~ln677
public class TorConnectStage {

    public class Error {
        public String code;
        public String message;
        public String phase;
        public String reason;

        public Error(GeckoBundle bundle) {
            code = bundle.getString("code");
            message = bundle.getString("message");
            phase = bundle.getString("phase");
            reason = bundle.getString("reason");
        }
    }

    public TorConnectStageName name;
    // The TorConnectStage prior to this bootstrap attempt. Only set during the "Bootstrapping" stage.
    public TorConnectStageName bootstrapTrigger;
    public Error error;
    public TorConnectError error;
    public String defaultRegion;
    public Boolean potentiallyBlocked;
    public Boolean tryAgain;
@@ -37,7 +23,7 @@ public class TorConnectStage {
        potentiallyBlocked = bundle.getBoolean("potentiallyBlocked");
        tryAgain = bundle.getBoolean("tryAgain");
        if (bundle.getBundle("error") != null) {
            error = new Error(bundle.getBundle("error"));
            error = new TorConnectError(bundle.getBundle("error"));
        }
        bootstrappingStatus = new TorBootstrappingStatus(bundle.getBundle("bootstrappingStatus"));
    }
+4 −11
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ const EmittedEvents = Object.freeze({
  settingsChanged: "GeckoView:Tor:SettingsChanged",
  connectStateChanged: "GeckoView:Tor:ConnectStateChanged", // deprecation path
  connectStageChanged: "GeckoView:Tor:ConnectStageChanged", // new replacement path
  connectError: "GeckoView:Tor:ConnectError",
  bootstrapProgress: "GeckoView:Tor:BootstrapProgress",
  bootstrapComplete: "GeckoView:Tor:BootstrapComplete",
  torLogs: "GeckoView:Tor:Logs",
@@ -46,6 +45,7 @@ const ListenedEvents = Object.freeze({
  quickstartGet: "GeckoView:Tor:QuickstartGet",
  quickstartSet: "GeckoView:Tor:QuickstartSet",
  regionNamesGet: "GeckoView:Tor:RegionNamesGet",
  shouldShowTorConnectGet: "GeckoView:Tor:ShouldShowTorConnect",
});

class TorAndroidIntegrationImpl {
@@ -131,16 +131,6 @@ class TorAndroidIntegrationImpl {
          type: EmittedEvents.bootstrapComplete,
        });
        break;
      // TODO: Replace with StageChange stage.error.
      case lazy.TorConnectTopics.Error:
        lazy.EventDispatcher.instance.sendRequest({
          type: EmittedEvents.connectError,
          code: subj.wrappedJSObject.code ?? "",
          message: subj.wrappedJSObject.message ?? "",
          phase: subj.wrappedJSObject.cause?.phase ?? "",
          reason: subj.wrappedJSObject.cause?.reason ?? "",
        });
        break;
      case lazy.TorProviderTopics.TorLog:
        lazy.EventDispatcher.instance.sendRequest({
          type: EmittedEvents.torLogs,
@@ -208,6 +198,9 @@ class TorAndroidIntegrationImpl {
        case ListenedEvents.regionNamesGet:
          callback?.onSuccess(lazy.TorConnect.getRegionNames());
          return;
        case ListenedEvents.shouldShowTorConnectGet:
          callback?.onSuccess(lazy.TorConnect.shouldShowTorConnect());
          return;
      }
      callback?.onSuccess();
    } catch (e) {