web_device.dart 4.42 KB
Newer Older
1 2 3 4
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:meta/meta.dart';

7 8 9
import '../application_package.dart';
import '../base/file_system.dart';
import '../base/io.dart';
10
import '../base/platform.dart';
11
import '../base/process_manager.dart';
12 13
import '../build_info.dart';
import '../device.dart';
14
import '../features.dart';
15
import '../project.dart';
16
import 'chrome.dart';
17
import 'workflow.dart';
18 19

class WebApplicationPackage extends ApplicationPackage {
20
  WebApplicationPackage(this.flutterProject) : super(id: flutterProject.manifest.appName);
21

22
  final FlutterProject flutterProject;
23 24

  @override
25
  String get name => flutterProject.manifest.appName;
26 27

  /// The location of the web source assets.
28
  Directory get webSourcePath => flutterProject.directory.childDirectory('web');
29 30
}

31 32 33
class ChromeDevice extends Device {
  ChromeDevice() : super(
      'chrome',
34 35 36 37
      category: Category.web,
      platformType: PlatformType.web,
      ephemeral: false,
  );
38

39 40
  // TODO(jonahwilliams): this is technically false, but requires some refactoring
  // to allow hot mode restart only devices.
41
  @override
42
  bool get supportsHotReload => true;
43 44

  @override
45
  bool get supportsHotRestart => true;
46 47 48 49 50

  @override
  bool get supportsStartPaused => true;

  @override
51
  bool get supportsFlutterExit => true;
52 53 54 55 56

  @override
  bool get supportsScreenshot => false;

  @override
57
  void clearLogs() { }
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

  @override
  DeviceLogReader getLogReader({ApplicationPackage app}) {
    return NoOpDeviceLogReader(app.name);
  }

  @override
  Future<bool> installApp(ApplicationPackage app) async => true;

  @override
  Future<bool> isAppInstalled(ApplicationPackage app) async => true;

  @override
  Future<bool> isLatestBuildInstalled(ApplicationPackage app) async => true;

  @override
  Future<bool> get isLocalEmulator async => false;

76 77 78
  @override
  Future<String> get emulatorId async => null;

79
  @override
80
  bool isSupported() =>  featureFlags.isWebEnabled && canFindChrome();
81 82

  @override
83
  String get name => 'Chrome';
84 85 86 87 88

  @override
  DevicePortForwarder get portForwarder => const NoOpDevicePortForwarder();

  @override
89
  Future<String> get sdkNameAndVersion async {
90 91 92
    if (!isSupported()) {
      return 'unknown';
    }
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    // See https://bugs.chromium.org/p/chromium/issues/detail?id=158372
    String version = 'unknown';
    if (platform.isWindows) {
      final ProcessResult result = await processManager.run(<String>[
        r'reg', 'query', 'HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon', '/v', 'version'
      ]);
      if (result.exitCode == 0) {
        final List<String> parts = result.stdout.split(RegExp(r'\s+'));
        if (parts.length > 2) {
          version = 'Google Chrome ' + parts[parts.length - 2];
        }
      }
    } else {
      final String chrome = findChromeExecutable();
      final ProcessResult result = await processManager.run(<String>[
        chrome,
        '--version',
      ]);
      if (result.exitCode == 0) {
        version = result.stdout;
      }
114
    }
115
    return version;
116
  }
117 118 119 120 121 122 123 124 125 126 127

  @override
  Future<LaunchResult> startApp(
    covariant WebApplicationPackage package, {
    String mainPath,
    String route,
    DebuggingOptions debuggingOptions,
    Map<String, Object> platformArgs,
    bool prebuiltApplication = false,
    bool ipv6 = false,
  }) async {
128 129
    // See [ResidentWebRunner.run] in flutter_tools/lib/src/resident_web_runner.dart
    // for the web initialization and server logic.
130 131 132 133 134 135 136 137 138
    return LaunchResult.succeeded(observatoryUri: null);
  }

  @override
  Future<bool> stopApp(ApplicationPackage app) async {
    return true;
  }

  @override
139
  Future<TargetPlatform> get targetPlatform async => TargetPlatform.web_javascript;
140 141 142 143

  @override
  Future<bool> uninstallApp(ApplicationPackage app) async => true;

144 145 146 147
  @override
  bool isSupportedForProject(FlutterProject flutterProject) {
    return flutterProject.web.existsSync();
  }
148 149 150
}

class WebDevices extends PollingDeviceDiscovery {
151
  WebDevices() : super('chrome');
152

153
  final ChromeDevice _webDevice = ChromeDevice();
154 155

  @override
156
  bool get canListAnything => featureFlags.isWebEnabled;
157 158 159 160 161 162 163 164 165

  @override
  Future<List<Device>> pollingGetDevices() async {
    return <Device>[
      _webDevice,
    ];
  }

  @override
166
  bool get supportsPlatform =>  featureFlags.isWebEnabled;
167
}
168 169 170 171 172

@visibleForTesting
String parseVersionForWindows(String input) {
  return input.split(RegExp('\w')).last;
}