mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Migrate tools chrome to null safety (#79801)
This commit is contained in:
parent
e6fbdb6316
commit
32440ce165
@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// @dart = 2.8
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
@ -56,7 +54,7 @@ typedef BrowserFinder = String Function(Platform, FileSystem);
|
||||
/// Does not verify whether the executable exists.
|
||||
String findChromeExecutable(Platform platform, FileSystem fileSystem) {
|
||||
if (platform.environment.containsKey(kChromeEnvironment)) {
|
||||
return platform.environment[kChromeEnvironment];
|
||||
return platform.environment[kChromeEnvironment]!;
|
||||
}
|
||||
if (platform.isLinux) {
|
||||
return kLinuxExecutable;
|
||||
@ -67,9 +65,12 @@ String findChromeExecutable(Platform platform, FileSystem fileSystem) {
|
||||
if (platform.isWindows) {
|
||||
/// The possible locations where the chrome executable can be located on windows.
|
||||
final List<String> kWindowsPrefixes = <String>[
|
||||
platform.environment['LOCALAPPDATA'],
|
||||
platform.environment['PROGRAMFILES'],
|
||||
platform.environment['PROGRAMFILES(X86)'],
|
||||
if (platform.environment.containsKey('LOCALAPPDATA'))
|
||||
platform.environment['LOCALAPPDATA']!,
|
||||
if (platform.environment.containsKey('PROGRAMFILES'))
|
||||
platform.environment['PROGRAMFILES']!,
|
||||
if (platform.environment.containsKey('PROGRAMFILES(X86)'))
|
||||
platform.environment['PROGRAMFILES(X86)']!,
|
||||
];
|
||||
final String windowsPrefix = kWindowsPrefixes.firstWhere((String prefix) {
|
||||
if (prefix == null) {
|
||||
@ -88,14 +89,17 @@ String findChromeExecutable(Platform platform, FileSystem fileSystem) {
|
||||
/// Does not verify whether the executable exists.
|
||||
String findEdgeExecutable(Platform platform, FileSystem fileSystem) {
|
||||
if (platform.environment.containsKey(kEdgeEnvironment)) {
|
||||
return platform.environment[kEdgeEnvironment];
|
||||
return platform.environment[kEdgeEnvironment]!;
|
||||
}
|
||||
if (platform.isWindows) {
|
||||
/// The possible locations where the Edge executable can be located on windows.
|
||||
final List<String> kWindowsPrefixes = <String>[
|
||||
platform.environment['LOCALAPPDATA'],
|
||||
platform.environment['PROGRAMFILES'],
|
||||
platform.environment['PROGRAMFILES(X86)'],
|
||||
if (platform.environment.containsKey('LOCALAPPDATA'))
|
||||
platform.environment['LOCALAPPDATA']!,
|
||||
if (platform.environment.containsKey('PROGRAMFILES'))
|
||||
platform.environment['PROGRAMFILES']!,
|
||||
if (platform.environment.containsKey('PROGRAMFILES(X86)'))
|
||||
platform.environment['PROGRAMFILES(X86)']!,
|
||||
];
|
||||
final String windowsPrefix = kWindowsPrefixes.firstWhere((String prefix) {
|
||||
if (prefix == null) {
|
||||
@ -113,12 +117,12 @@ String findEdgeExecutable(Platform platform, FileSystem fileSystem) {
|
||||
/// A launcher for Chromium browsers with devtools configured.
|
||||
class ChromiumLauncher {
|
||||
ChromiumLauncher({
|
||||
@required FileSystem fileSystem,
|
||||
@required Platform platform,
|
||||
@required ProcessManager processManager,
|
||||
@required OperatingSystemUtils operatingSystemUtils,
|
||||
@required BrowserFinder browserFinder,
|
||||
@required Logger logger,
|
||||
required FileSystem fileSystem,
|
||||
required Platform platform,
|
||||
required ProcessManager processManager,
|
||||
required OperatingSystemUtils operatingSystemUtils,
|
||||
required BrowserFinder browserFinder,
|
||||
required Logger logger,
|
||||
}) : _fileSystem = fileSystem,
|
||||
_platform = platform,
|
||||
_processManager = processManager,
|
||||
@ -162,9 +166,9 @@ class ChromiumLauncher {
|
||||
/// [skipCheck] does not attempt to make a devtools connection before returning.
|
||||
Future<Chromium> launch(String url, {
|
||||
bool headless = false,
|
||||
int debugPort,
|
||||
int? debugPort,
|
||||
bool skipCheck = false,
|
||||
Directory cacheDir,
|
||||
Directory? cacheDir,
|
||||
}) async {
|
||||
if (currentCompleter.isCompleted) {
|
||||
throwToolExit('Only one instance of chrome can be started.');
|
||||
@ -214,10 +218,10 @@ class ChromiumLauncher {
|
||||
url,
|
||||
];
|
||||
|
||||
final Process process = await _spawnChromiumProcess(args);
|
||||
final Process? process = await _spawnChromiumProcess(args);
|
||||
|
||||
// When the process exits, copy the user settings back to the provided data-dir.
|
||||
if (cacheDir != null) {
|
||||
if (process != null && cacheDir != null) {
|
||||
unawaited(process.exitCode.whenComplete(() {
|
||||
_cacheUserSessionInformation(userDataDir, cacheDir);
|
||||
}));
|
||||
@ -231,7 +235,7 @@ class ChromiumLauncher {
|
||||
), skipCheck);
|
||||
}
|
||||
|
||||
Future<Process> _spawnChromiumProcess(List<String> args) async {
|
||||
Future<Process?> _spawnChromiumProcess(List<String> args) async {
|
||||
// Keep attempting to launch the browser until one of:
|
||||
// - Chrome launched successfully, in which case we just return from the loop.
|
||||
// - The tool detected an unretriable Chrome error, in which case we throw ToolExit.
|
||||
@ -264,7 +268,8 @@ class ChromiumLauncher {
|
||||
'Encountered glibc bug https://sourceware.org/bugzilla/show_bug.cgi?id=19329. '
|
||||
'Will try launching browser again.',
|
||||
);
|
||||
return null;
|
||||
// Return value unused.
|
||||
return '';
|
||||
}
|
||||
_logger.printTrace('Failed to launch browser. Command used to launch it: ${args.join(' ')}');
|
||||
throw ToolExit(
|
||||
@ -283,7 +288,8 @@ class ChromiumLauncher {
|
||||
// launching more processes.
|
||||
unawaited(process.exitCode.timeout(const Duration(seconds: 1), onTimeout: () {
|
||||
process.kill();
|
||||
return null;
|
||||
// sigterm
|
||||
return 15;
|
||||
}));
|
||||
}
|
||||
}
|
||||
@ -302,7 +308,7 @@ class ChromiumLauncher {
|
||||
/// Note: more detailed docs of the Chrome user preferences store exists here:
|
||||
/// https://www.chromium.org/developers/design-documents/preferences.
|
||||
void _cacheUserSessionInformation(Directory userDataDir, Directory cacheDir) {
|
||||
final Directory targetChromeDefault = _fileSystem.directory(_fileSystem.path.join(cacheDir?.path ?? '', _chromeDefaultPath));
|
||||
final Directory targetChromeDefault = _fileSystem.directory(_fileSystem.path.join(cacheDir.path, _chromeDefaultPath));
|
||||
final Directory sourceChromeDefault = _fileSystem.directory(_fileSystem.path.join(userDataDir.path, _chromeDefaultPath));
|
||||
if (sourceChromeDefault.existsSync()) {
|
||||
targetChromeDefault.createSync(recursive: true);
|
||||
@ -315,7 +321,7 @@ class ChromiumLauncher {
|
||||
}
|
||||
}
|
||||
|
||||
final File targetPreferencesFile = _fileSystem.file(_fileSystem.path.join(cacheDir?.path ?? '', _preferencesPath));
|
||||
final File targetPreferencesFile = _fileSystem.file(_fileSystem.path.join(cacheDir.path, _preferencesPath));
|
||||
final File sourcePreferencesFile = _fileSystem.file(_fileSystem.path.join(userDataDir.path, _preferencesPath));
|
||||
|
||||
if (sourcePreferencesFile.existsSync()) {
|
||||
@ -330,7 +336,7 @@ class ChromiumLauncher {
|
||||
/// Restore Chrome user information from a per-project cache into Chrome's
|
||||
/// user data directory.
|
||||
void _restoreUserSessionInformation(Directory cacheDir, Directory userDataDir) {
|
||||
final Directory sourceChromeDefault = _fileSystem.directory(_fileSystem.path.join(cacheDir.path ?? '', _chromeDefaultPath));
|
||||
final Directory sourceChromeDefault = _fileSystem.directory(_fileSystem.path.join(cacheDir.path, _chromeDefaultPath));
|
||||
final Directory targetChromeDefault = _fileSystem.directory(_fileSystem.path.join(userDataDir.path, _chromeDefaultPath));
|
||||
try {
|
||||
if (sourceChromeDefault.existsSync()) {
|
||||
@ -367,18 +373,18 @@ class Chromium {
|
||||
this.debugPort,
|
||||
this.chromeConnection, {
|
||||
this.url,
|
||||
Process process,
|
||||
@required ChromiumLauncher chromiumLauncher,
|
||||
Process? process,
|
||||
required ChromiumLauncher chromiumLauncher,
|
||||
}) : _process = process,
|
||||
_chromiumLauncher = chromiumLauncher;
|
||||
|
||||
final String url;
|
||||
final String? url;
|
||||
final int debugPort;
|
||||
final Process _process;
|
||||
final Process? _process;
|
||||
final ChromeConnection chromeConnection;
|
||||
final ChromiumLauncher _chromiumLauncher;
|
||||
|
||||
Future<int> get onExit => _process.exitCode;
|
||||
Future<int?> get onExit async => _process?.exitCode;
|
||||
|
||||
Future<void> close() async {
|
||||
if (_chromiumLauncher.hasChromeInstance) {
|
||||
|
Loading…
Reference in New Issue
Block a user