From f2417e6c1cc9514c2568187e5b4082475872a304 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Tue, 20 Apr 2021 19:39:58 -0700 Subject: [PATCH] Launch Chrome natively on ARM macOS (#80758) --- .../flutter_tools/lib/src/web/chrome.dart | 15 ++- .../test/general.shard/web/chrome_test.dart | 115 ++++++++++++++++++ packages/flutter_tools/test/src/context.dart | 2 + 3 files changed, 130 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/web/chrome.dart b/packages/flutter_tools/lib/src/web/chrome.dart index 0edcb71d66..a17d6966cf 100644 --- a/packages/flutter_tools/lib/src/web/chrome.dart +++ b/packages/flutter_tools/lib/src/web/chrome.dart @@ -218,7 +218,7 @@ class ChromiumLauncher { url, ]; - final Process? process = await _spawnChromiumProcess(args); + final Process? process = await _spawnChromiumProcess(args, chromeExecutable); // When the process exits, copy the user settings back to the provided data-dir. if (process != null && cacheDir != null) { @@ -235,7 +235,18 @@ class ChromiumLauncher { ), skipCheck); } - Future _spawnChromiumProcess(List args) async { + Future _spawnChromiumProcess(List args, String chromeExecutable) async { + if (_operatingSystemUtils.hostPlatform == HostPlatform.darwin_arm) { + final ProcessResult result = _processManager.runSync(['file', chromeExecutable]); + // Check if ARM Chrome is installed. + // Mach-O 64-bit executable arm64 + if ((result.stdout as String).contains('arm64')) { + _logger.printTrace('Found ARM Chrome installation at $chromeExecutable, forcing native launch.'); + // If so, force Chrome to launch natively. + args.insertAll(0, ['/usr/bin/arch', '-arm64']); + } + } + // 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. diff --git a/packages/flutter_tools/test/general.shard/web/chrome_test.dart b/packages/flutter_tools/test/general.shard/web/chrome_test.dart index 2c88408e02..9760e0aa72 100644 --- a/packages/flutter_tools/test/general.shard/web/chrome_test.dart +++ b/packages/flutter_tools/test/general.shard/web/chrome_test.dart @@ -189,6 +189,121 @@ void main() { expect(logger.errorText, contains('Failed to restore Chrome preferences')); }); + testWithoutContext('can launch Chrome on x86_64 macOS', () async { + final OperatingSystemUtils macOSUtils = FakeOperatingSystemUtils(hostPlatform: HostPlatform.darwin_x64); + final ChromiumLauncher chromiumLauncher = ChromiumLauncher( + fileSystem: fileSystem, + platform: platform, + processManager: processManager, + operatingSystemUtils: macOSUtils, + browserFinder: findChromeExecutable, + logger: BufferLogger.test(), + ); + + processManager.addCommands([ + const FakeCommand( + command: [ + 'example_chrome', + '--user-data-dir=/.tmp_rand0/flutter_tools_chrome_device.rand0', + '--remote-debugging-port=12345', + ...kChromeArgs, + 'example_url', + ], + stderr: kDevtoolsStderr, + ) + ]); + + expect( + () async => chromiumLauncher.launch( + 'example_url', + skipCheck: true, + ), + returnsNormally, + ); + }); + + testWithoutContext('can launch x86_64 Chrome on ARM macOS', () async { + final OperatingSystemUtils macOSUtils = FakeOperatingSystemUtils(hostPlatform: HostPlatform.darwin_arm); + final ChromiumLauncher chromiumLauncher = ChromiumLauncher( + fileSystem: fileSystem, + platform: platform, + processManager: processManager, + operatingSystemUtils: macOSUtils, + browserFinder: findChromeExecutable, + logger: BufferLogger.test(), + ); + + processManager.addCommands([ + const FakeCommand( + command: [ + 'file', + 'example_chrome', + ], + stdout: 'Mach-O 64-bit executable x86_64', + ), + const FakeCommand( + command: [ + 'example_chrome', + '--user-data-dir=/.tmp_rand0/flutter_tools_chrome_device.rand0', + '--remote-debugging-port=12345', + ...kChromeArgs, + 'example_url', + ], + stderr: kDevtoolsStderr, + ) + ]); + + expect( + () async => chromiumLauncher.launch( + 'example_url', + skipCheck: true, + ), + returnsNormally, + ); + }); + + testWithoutContext('can launch ARM Chrome natively on ARM macOS when installed', () async { + final OperatingSystemUtils macOSUtils = FakeOperatingSystemUtils(hostPlatform: HostPlatform.darwin_arm); + final ChromiumLauncher chromiumLauncher = ChromiumLauncher( + fileSystem: fileSystem, + platform: platform, + processManager: processManager, + operatingSystemUtils: macOSUtils, + browserFinder: findChromeExecutable, + logger: BufferLogger.test(), + ); + + processManager.addCommands([ + const FakeCommand( + command: [ + 'file', + 'example_chrome', + ], + stdout: 'Mach-O 64-bit executable arm64', + ), + const FakeCommand( + command: [ + '/usr/bin/arch', + '-arm64', + 'example_chrome', + '--user-data-dir=/.tmp_rand0/flutter_tools_chrome_device.rand0', + '--remote-debugging-port=12345', + ...kChromeArgs, + 'example_url', + ], + stderr: kDevtoolsStderr, + ), + ]); + + expect( + () async => chromiumLauncher.launch( + 'example_url', + skipCheck: true, + ), + returnsNormally, + ); + }); + testWithoutContext('can launch chrome with a custom debug port', () async { processManager.addCommand(const FakeCommand( command: [ diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart index af9f9502c1..b57e6f5a4f 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -296,6 +296,8 @@ class MockSimControl extends Mock implements SimControl { } class FakeOperatingSystemUtils implements OperatingSystemUtils { + FakeOperatingSystemUtils({this.hostPlatform = HostPlatform.linux_x64}); + @override ProcessResult makeExecutable(File file) => null;