[flutter_tools] cleanups to web runner functionality (#61178)

Skip unnecessary parsing of chrome URI. Ensure stack traces are initialized in web server. Disclaimer on web server that it does not support debugging and remove help message. Fix generated entrypoint to check for main(List<String> args)

- Fixes #59643
- Fixes #55084
- Fixes #60417
This commit is contained in:
Jonah Williams
2020-07-13 15:12:46 -07:00
committed by GitHub
parent a1097ea53f
commit e666ea8de4
10 changed files with 122 additions and 75 deletions

View File

@@ -66,19 +66,19 @@ define("main_module.bootstrap", ["$entrypoint", "dart_sdk"], function(app, dart_
window.\$dartLoader.rootDirectories = [];
if (window.\$requireLoader) {
window.\$requireLoader.getModuleLibraries = dart_sdk.dart.getModuleLibraries;
if (window.\$dartStackTraceUtility && !window.\$dartStackTraceUtility.ready) {
window.\$dartStackTraceUtility.ready = true;
let dart = dart_sdk.dart;
window.\$dartStackTraceUtility.setSourceMapProvider(function(url) {
var baseUrl = window.location.protocol + '//' + window.location.host;
url = url.replace(baseUrl + '/', '');
if (url == 'dart_sdk.js') {
return dart.getSourceMap('dart_sdk');
}
url = url.replace(".lib.js", "");
return dart.getSourceMap(url);
});
}
}
if (window.\$dartStackTraceUtility && !window.\$dartStackTraceUtility.ready) {
window.\$dartStackTraceUtility.ready = true;
let dart = dart_sdk.dart;
window.\$dartStackTraceUtility.setSourceMapProvider(function(url) {
var baseUrl = window.location.protocol + '//' + window.location.host;
url = url.replace(baseUrl + '/', '');
if (url == 'dart_sdk.js') {
return dart.getSourceMap('dart_sdk');
}
url = url.replace(".lib.js", "");
return dart.getSourceMap(url);
});
}
});
''';

View File

@@ -15,7 +15,6 @@ import '../base/logger.dart';
import '../base/os.dart';
import '../base/platform.dart';
import '../convert.dart';
import '../globals.dart' as globals;
/// An environment variable used to override the location of Google Chrome.
const String kChromeEnvironment = 'CHROME_EXECUTABLE';
@@ -105,14 +104,14 @@ class ChromiumLauncher {
@required Platform platform,
@required ProcessManager processManager,
@required OperatingSystemUtils operatingSystemUtils,
@required Logger logger,
@required BrowserFinder browserFinder,
@required Logger logger,
}) : _fileSystem = fileSystem,
_platform = platform,
_processManager = processManager,
_operatingSystemUtils = operatingSystemUtils,
_logger = logger,
_browserFinder = browserFinder,
_logger = logger,
_fileSystemUtils = FileSystemUtils(
fileSystem: fileSystem,
platform: platform,
@@ -122,9 +121,9 @@ class ChromiumLauncher {
final Platform _platform;
final ProcessManager _processManager;
final OperatingSystemUtils _operatingSystemUtils;
Logger _logger;
final BrowserFinder _browserFinder;
final FileSystemUtils _fileSystemUtils;
final Logger _logger;
bool get hasChromeInstance => _currentCompleter.isCompleted;
@@ -163,7 +162,6 @@ class ChromiumLauncher {
bool skipCheck = false,
Directory cacheDir,
}) async {
_logger ??= globals.logger;
if (_currentCompleter.isCompleted) {
throwToolExit('Only one instance of chrome can be started.');
}
@@ -207,13 +205,6 @@ class ChromiumLauncher {
final Process process = await _processManager.start(args);
// When the process exits, copy the user settings back to the provided data-dir.
if (cacheDir != null) {
unawaited(process.exitCode.whenComplete(() {
_cacheUserSessionInformation(userDataDir, cacheDir);
}));
}
process.stdout
.transform(utf8.decoder)
.transform(const LineSplitter())
@@ -221,7 +212,8 @@ class ChromiumLauncher {
_logger.printTrace('[CHROME]: $line');
});
// Wait until the DevTools are listening before trying to connect.
// Wait until the DevTools are listening before trying to connect. This is
// only required for flutter_test --platform=chrome and not flutter run.
await process.stderr
.transform(utf8.decoder)
.transform(const LineSplitter())
@@ -232,13 +224,18 @@ class ChromiumLauncher {
.firstWhere((String line) => line.startsWith('DevTools listening'), orElse: () {
return 'Failed to spawn stderr';
});
final Uri remoteDebuggerUri = await _getRemoteDebuggerUrl(Uri.parse('http://localhost:$port'));
// When the process exits, copy the user settings back to the provided data-dir.
if (cacheDir != null) {
unawaited(process.exitCode.whenComplete(() {
_cacheUserSessionInformation(userDataDir, cacheDir);
}));
}
return _connect(Chromium._(
port,
ChromeConnection('localhost', port),
url: url,
process: process,
remoteDebuggerUri: remoteDebuggerUri,
chromiumLauncher: this,
), skipCheck);
}
@@ -311,28 +308,6 @@ class ChromiumLauncher {
}
Future<Chromium> get connectedInstance => _currentCompleter.future;
/// Returns the full URL of the Chrome remote debugger for the main page.
///
/// This takes the [base] remote debugger URL (which points to a browser-wide
/// page) and uses its JSON API to find the resolved URL for debugging the host
/// page.
Future<Uri> _getRemoteDebuggerUrl(Uri base) async {
try {
final HttpClient client = HttpClient();
final HttpClientRequest request = await client.getUrl(base.resolve('/json/list'));
final HttpClientResponse response = await request.close();
final List<dynamic> jsonObject = await json.fuse(utf8).decoder.bind(response).single as List<dynamic>;
if (jsonObject == null || jsonObject.isEmpty) {
return base;
}
return base.resolve(jsonObject.first['devtoolsFrontendUrl'] as String);
} on Exception {
// If we fail to talk to the remote debugger protocol, give up and return
// the raw URL rather than crashing.
return base;
}
}
}
/// A class for managing an instance of a Chromium browser.
@@ -342,7 +317,6 @@ class Chromium {
this.chromeConnection, {
this.url,
Process process,
this.remoteDebuggerUri,
@required ChromiumLauncher chromiumLauncher,
}) : _process = process,
_chromiumLauncher = chromiumLauncher;
@@ -351,7 +325,6 @@ class Chromium {
final int debugPort;
final Process _process;
final ChromeConnection chromeConnection;
final Uri remoteDebuggerUri;
final ChromiumLauncher _chromiumLauncher;
Future<int> get onExit => _process.exitCode;

View File

@@ -305,10 +305,10 @@ class WebDevices extends PollingDeviceDiscovery {
chromiumLauncher: ChromiumLauncher(
browserFinder: findChromeExecutable,
fileSystem: fileSystem,
logger: logger,
platform: platform,
processManager: processManager,
operatingSystemUtils: operatingSystemUtils,
logger: logger,
),
);
if (platform.isWindows) {
@@ -316,10 +316,10 @@ class WebDevices extends PollingDeviceDiscovery {
chromiumLauncher: ChromiumLauncher(
browserFinder: findEdgeExecutable,
fileSystem: fileSystem,
logger: logger,
platform: platform,
processManager: processManager,
operatingSystemUtils: operatingSystemUtils,
logger: logger,
),
processManager: processManager,
logger: logger,
@@ -450,6 +450,10 @@ class WebServerDevice extends Device {
} else {
_logger.printStatus('$mainPath is being served at $url', emphasis: true);
}
_logger.printStatus(
'The web-server device does not support debugging. Consider using '
'the Chrome or Edge devices for an improved development workflow.'
);
_logger.sendEvent('app.webLaunchUrl', <String, dynamic>{'url': url, 'launched': false});
return LaunchResult.succeeded(observatoryUri: url != null ? Uri.parse(url): null);
}