Skip to content

Commit

Permalink
feat(tools): Arbitrary browser flags (closes #65575) (#104935)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 authored Jun 24, 2022
1 parent 08779f0 commit 47f54ac
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 34 deletions.
1 change: 1 addition & 0 deletions packages/flutter_tools/lib/src/commands/drive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ class DriveCommand extends RunCommandBase {
packageConfig,
chromeBinary: stringArgDeprecated('chrome-binary'),
headless: boolArgDeprecated('headless'),
webBrowserFlags: stringsArg(FlutterOptions.kWebBrowserFlag),
browserDimension: stringArgDeprecated('browser-dimension')!.split(','),
browserName: stringArgDeprecated('browser-name'),
driverPort: stringArgDeprecated('driver-port') != null
Expand Down
11 changes: 8 additions & 3 deletions packages/flutter_tools/lib/src/commands/run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,12 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
@protected
Future<DebuggingOptions> createDebuggingOptions(bool webMode) async {
final BuildInfo buildInfo = await getBuildInfo();
final int? browserDebugPort = featureFlags.isWebEnabled && argResults!.wasParsed('web-browser-debug-port')
final int? webBrowserDebugPort = featureFlags.isWebEnabled && argResults!.wasParsed('web-browser-debug-port')
? int.parse(stringArgDeprecated('web-browser-debug-port')!)
: null;
final List<String> webBrowserFlags = featureFlags.isWebEnabled
? stringsArg(FlutterOptions.kWebBrowserFlag)
: const <String>[];
if (buildInfo.mode.isRelease) {
return DebuggingOptions.disabled(
buildInfo,
Expand All @@ -216,7 +219,8 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
webUseSseForInjectedClient: featureFlags.isWebEnabled && stringArgDeprecated('web-server-debug-injected-client-protocol') == 'sse',
webEnableExposeUrl: featureFlags.isWebEnabled && boolArgDeprecated('web-allow-expose-url'),
webRunHeadless: featureFlags.isWebEnabled && boolArgDeprecated('web-run-headless'),
webBrowserDebugPort: browserDebugPort,
webBrowserDebugPort: webBrowserDebugPort,
webBrowserFlags: webBrowserFlags,
enableImpeller: enableImpeller,
uninstallFirst: uninstallFirst,
);
Expand Down Expand Up @@ -253,7 +257,8 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment
webUseSseForInjectedClient: featureFlags.isWebEnabled && stringArgDeprecated('web-server-debug-injected-client-protocol') == 'sse',
webEnableExposeUrl: featureFlags.isWebEnabled && boolArgDeprecated('web-allow-expose-url'),
webRunHeadless: featureFlags.isWebEnabled && boolArgDeprecated('web-run-headless'),
webBrowserDebugPort: browserDebugPort,
webBrowserDebugPort: webBrowserDebugPort,
webBrowserFlags: webBrowserFlags,
webEnableExpressionEvaluation: featureFlags.isWebEnabled && boolArgDeprecated('web-enable-expression-evaluation'),
webLaunchUrl: featureFlags.isWebEnabled ? stringArgDeprecated('web-launch-url') : null,
vmserviceOutFile: stringArgDeprecated('vmservice-out-file'),
Expand Down
8 changes: 8 additions & 0 deletions packages/flutter_tools/lib/src/device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ class DebuggingOptions {
this.webUseSseForInjectedClient = true,
this.webRunHeadless = false,
this.webBrowserDebugPort,
this.webBrowserFlags = const <String>[],
this.webEnableExpressionEvaluation = false,
this.webLaunchUrl,
this.vmserviceOutFile,
Expand All @@ -805,6 +806,7 @@ class DebuggingOptions {
this.webUseSseForInjectedClient = true,
this.webRunHeadless = false,
this.webBrowserDebugPort,
this.webBrowserFlags = const <String>[],
this.webLaunchUrl,
this.cacheSkSL = false,
this.traceAllowlist,
Expand Down Expand Up @@ -871,6 +873,7 @@ class DebuggingOptions {
required this.webUseSseForInjectedClient,
required this.webRunHeadless,
required this.webBrowserDebugPort,
required this.webBrowserFlags,
required this.webEnableExpressionEvaluation,
required this.webLaunchUrl,
required this.vmserviceOutFile,
Expand Down Expand Up @@ -930,6 +933,9 @@ class DebuggingOptions {
/// The port the browser should use for its debugging protocol.
final int? webBrowserDebugPort;

/// Arbitrary browser flags.
final List<String> webBrowserFlags;

/// Enable expression evaluation for web target.
final bool webEnableExpressionEvaluation;

Expand Down Expand Up @@ -983,6 +989,7 @@ class DebuggingOptions {
'webUseSseForInjectedClient': webUseSseForInjectedClient,
'webRunHeadless': webRunHeadless,
'webBrowserDebugPort': webBrowserDebugPort,
'webBrowserFlags': webBrowserFlags,
'webEnableExpressionEvaluation': webEnableExpressionEvaluation,
'webLaunchUrl': webLaunchUrl,
'vmserviceOutFile': vmserviceOutFile,
Expand Down Expand Up @@ -1027,6 +1034,7 @@ class DebuggingOptions {
webUseSseForInjectedClient: (json['webUseSseForInjectedClient'] as bool?)!,
webRunHeadless: (json['webRunHeadless'] as bool?)!,
webBrowserDebugPort: json['webBrowserDebugPort'] as int?,
webBrowserFlags: ((json['webBrowserFlags'] as List<dynamic>?)?.cast<String>())!,
webEnableExpressionEvaluation: (json['webEnableExpressionEvaluation'] as bool?)!,
webLaunchUrl: json['webLaunchUrl'] as String?,
vmserviceOutFile: json['vmserviceOutFile'] as String?,
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter_tools/lib/src/drive/drive_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ abstract class DriverService {
String? browserName,
bool? androidEmulator,
int? driverPort,
List<String> webBrowserFlags,
List<String>? browserDimension,
String? profileMemory,
});
Expand Down Expand Up @@ -254,6 +255,7 @@ class FlutterDriverService extends DriverService {
String? browserName,
bool? androidEmulator,
int? driverPort,
List<String> webBrowserFlags = const <String>[],
List<String>? browserDimension,
String? profileMemory,
}) async {
Expand Down
26 changes: 21 additions & 5 deletions packages/flutter_tools/lib/src/drive/web_driver_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class WebDriverService extends DriverService {
String? browserName,
bool? androidEmulator,
int? driverPort,
List<String> webBrowserFlags = const <String>[],
List<String>? browserDimension,
String? profileMemory,
}) async {
Expand All @@ -144,7 +145,12 @@ class WebDriverService extends DriverService {
try {
webDriver = await async_io.createDriver(
uri: Uri.parse('http://localhost:$driverPort/'),
desired: getDesiredCapabilities(browser, headless, chromeBinary),
desired: getDesiredCapabilities(
browser,
headless,
webBrowserFlags: webBrowserFlags,
chromeBinary: chromeBinary,
),
);
} on SocketException catch (error) {
_logger.printTrace('$error');
Expand Down Expand Up @@ -234,10 +240,15 @@ enum Browser {
safari,
}

/// Returns desired capabilities for given [browser], [headless] and
/// [chromeBinary].
/// Returns desired capabilities for given [browser], [headless], [chromeBinary]
/// and [webBrowserFlags].
@visibleForTesting
Map<String, dynamic> getDesiredCapabilities(Browser browser, bool? headless, [String? chromeBinary]) {
Map<String, dynamic> getDesiredCapabilities(
Browser browser,
bool? headless, {
List<String> webBrowserFlags = const <String>[],
String? chromeBinary,
}) {
switch (browser) {
case Browser.chrome:
return <String, dynamic>{
Expand All @@ -262,6 +273,7 @@ Map<String, dynamic> getDesiredCapabilities(Browser browser, bool? headless, [St
'--no-sandbox',
'--no-first-run',
if (headless!) '--headless',
...webBrowserFlags,
],
'perfLoggingPrefs': <String, String>{
'traceCategories':
Expand All @@ -278,6 +290,7 @@ Map<String, dynamic> getDesiredCapabilities(Browser browser, bool? headless, [St
'moz:firefoxOptions' : <String, dynamic>{
'args': <String>[
if (headless!) '-headless',
...webBrowserFlags,
],
'prefs': <String, dynamic>{
'dom.file.createInChild': true,
Expand Down Expand Up @@ -313,7 +326,10 @@ Map<String, dynamic> getDesiredCapabilities(Browser browser, bool? headless, [St
'platformName': 'android',
'goog:chromeOptions': <String, dynamic>{
'androidPackage': 'com.android.chrome',
'args': <String>['--disable-fullscreen'],
'args': <String>[
'--disable-fullscreen',
...webBrowserFlags,
],
},
};
}
Expand Down
10 changes: 10 additions & 0 deletions packages/flutter_tools/lib/src/runner/flutter_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class FlutterOptions {
static const String kAssumeInitializeFromDillUpToDate = 'assume-initialize-from-dill-up-to-date';
static const String kFatalWarnings = 'fatal-warnings';
static const String kUseApplicationBinary = 'use-application-binary';
static const String kWebBrowserFlag = 'web-browser-flag';
}

/// flutter command categories for usage.
Expand Down Expand Up @@ -270,6 +271,15 @@ abstract class FlutterCommand extends Command<void> {
help: 'The URL to provide to the browser. Defaults to an HTTP URL with the host '
'name of "--web-hostname", the port of "--web-port", and the path set to "/".',
);
argParser.addMultiOption(
FlutterOptions.kWebBrowserFlag,
help: 'Additional flag to pass to a browser instance at startup.\n'
'Chrome: https://www.chromium.org/developers/how-tos/run-chromium-with-flags/\n'
'Firefox: https://wiki.mozilla.org/Firefox/CommandLineOptions\n'
'Multiple flags can be passed by repeating "--${FlutterOptions.kWebBrowserFlag}" multiple times.',
valueHelp: '--foo=bar',
hide: !verboseHelp,
);
}

void usesTargetOption() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@ class BrowserManager {
///
/// The browser will start in headless mode if [headless] is true.
///
/// Add arbitrary browser flags via [webBrowserFlags].
///
/// The [settings] indicate how to invoke this browser's executable.
///
/// Returns the browser manager, or throws an [ApplicationException] if a
Expand All @@ -670,8 +672,13 @@ class BrowserManager {
Future<WebSocketChannel> future, {
bool debug = false,
bool headless = true,
List<String> webBrowserFlags = const <String>[],
}) async {
final Chromium chrome = await chromiumLauncher.launch(url.toString(), headless: headless);
final Chromium chrome = await chromiumLauncher.launch(
url.toString(),
headless: headless,
webBrowserFlags: webBrowserFlags,
);
final Completer<BrowserManager> completer = Completer<BrowserManager>();

unawaited(chrome.onExit.then((int? browserExitCode) {
Expand Down
4 changes: 4 additions & 0 deletions packages/flutter_tools/lib/src/web/chrome.dart
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,14 @@ class ChromiumLauncher {
/// port is picked automatically.
///
/// [skipCheck] does not attempt to make a devtools connection before returning.
///
/// [webBrowserFlags] add arbitrary browser flags.
Future<Chromium> launch(String url, {
bool headless = false,
int? debugPort,
bool skipCheck = false,
Directory? cacheDir,
List<String> webBrowserFlags = const <String>[],
}) async {
if (currentCompleter.isCompleted) {
throwToolExit('Only one instance of chrome can be started.');
Expand Down Expand Up @@ -215,6 +218,7 @@ class ChromiumLauncher {
'--no-sandbox',
'--window-size=2400,1800',
],
...webBrowserFlags,
url,
];

Expand Down
1 change: 1 addition & 0 deletions packages/flutter_tools/lib/src/web/web_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ abstract class ChromiumDevice extends Device {
.childDirectory('chrome-device'),
headless: debuggingOptions.webRunHeadless,
debugPort: debuggingOptions.webBrowserDebugPort,
webBrowserFlags: debuggingOptions.webBrowserFlags,
);
}
_logger.sendEvent('app.webLaunchUrl', <String, Object>{'url': url, 'launched': launchChrome});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ class FailingFakeDriverService extends Fake implements DriverService {
String browserName,
bool androidEmulator,
int driverPort,
List<String> webBrowserFlags,
List<String> browserDimension,
String profileMemory,
}) async => 1;
Expand Down
Loading

0 comments on commit 47f54ac

Please sign in to comment.