Unverified Commit f2417e6c authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Launch Chrome natively on ARM macOS (#80758)

parent b5d521f9
......@@ -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<Process?> _spawnChromiumProcess(List<String> args) async {
Future<Process?> _spawnChromiumProcess(List<String> args, String chromeExecutable) async {
if (_operatingSystemUtils.hostPlatform == HostPlatform.darwin_arm) {
final ProcessResult result = _processManager.runSync(<String>['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, <String>['/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.
......
......@@ -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(<FakeCommand>[
const FakeCommand(
command: <String>[
'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(<FakeCommand>[
const FakeCommand(
command: <String>[
'file',
'example_chrome',
],
stdout: 'Mach-O 64-bit executable x86_64',
),
const FakeCommand(
command: <String>[
'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(<FakeCommand>[
const FakeCommand(
command: <String>[
'file',
'example_chrome',
],
stdout: 'Mach-O 64-bit executable arm64',
),
const FakeCommand(
command: <String>[
'/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: <String>[
......
......@@ -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;
......
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