Unverified Commit 32440ce1 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Migrate tools chrome to null safety (#79801)

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