fuchsia_compat.dart 3.18 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';

import 'package:fuchsia_remote_debug_protocol/fuchsia_remote_debug_protocol.dart';

9
import 'error.dart';
10 11 12 13 14 15 16 17 18 19 20 21 22

class _DummyPortForwarder implements PortForwarder {
  _DummyPortForwarder(this._port, this._remotePort);

  final int _port;
  final int _remotePort;

  @override
  int get port => _port;

  @override
  int get remotePort => _remotePort;

23 24 25
  @override
  String get openPortAddress => InternetAddress.loopbackIPv4.address;

26
  @override
27
  Future<void> stop() async { }
28 29 30 31 32
}

class _DummySshCommandRunner implements SshCommandRunner {
  _DummySshCommandRunner();

33 34 35
  void _log(String message) {
    driverLog('_DummySshCommandRunner', message);
  }
36 37

  @override
38
  String get sshConfigPath => '';
39 40 41 42 43

  @override
  String get address => InternetAddress.loopbackIPv4.address;

  @override
44
  String get interface => '';
45 46 47 48

  @override
  Future<List<String>> run(String command) async {
    try {
49 50 51
      final List<String> splitCommand = command.split(' ');
      final String exe = splitCommand[0];
      final List<String> args = splitCommand.skip(1).toList();
52 53 54 55 56 57 58
      // This needs to remain async in the event that this command attempts to
      // access something (like the hub) that requires interaction with this
      // process's event loop. A specific example is attempting to run `find`, a
      // synchronous command, on this own process's `out` directory. As `find`
      // will wait indefinitely for the `out` directory to be serviced, causing
      // a deadlock.
      final ProcessResult r = await Process.run(exe, args);
59
      return (r.stdout as String).split('\n');
60
    } on ProcessException catch (e) {
61
      _log("Error running '$command': $e");
62 63 64 65 66 67 68 69
    }
    return <String>[];
  }
}

Future<PortForwarder> _dummyPortForwardingFunction(
  String address,
  int remotePort, [
70 71
  String? interface,
  String? configFile,
72 73 74 75 76 77 78 79 80 81
]) async {
  return _DummyPortForwarder(remotePort, remotePort);
}

/// Utility class for creating connections to the Fuchsia Device.
///
/// If executed on a host (non-Fuchsia device), behaves the same as running
/// [FuchsiaRemoteConnection.connect] whereby the `FUCHSIA_REMOTE_URL` and
/// `FUCHSIA_SSH_CONFIG` variables must be set. If run on a Fuchsia device, will
/// connect locally without need for environment variables.
82
abstract final class FuchsiaCompat {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  static void _init() {
    fuchsiaPortForwardingFunction = _dummyPortForwardingFunction;
  }

  /// Restores state to normal if running on a Fuchsia device.
  ///
  /// Noop if running on the host machine.
  static void cleanup() {
    restoreFuchsiaPortForwardingFunction();
  }

  /// Creates a connection to the Fuchsia device's Dart VM's.
  ///
  /// See [FuchsiaRemoteConnection.connect] for more details.
  /// [FuchsiaCompat.cleanup] must be called when the connection is no longer in
  /// use. It is the caller's responsibility to call
  /// [FuchsiaRemoteConnection.stop].
  static Future<FuchsiaRemoteConnection> connect() async {
    FuchsiaCompat._init();
102 103
    return FuchsiaRemoteConnection.connectWithSshCommandRunner(
        _DummySshCommandRunner());
104 105
  }
}