@@ -47,31 +47,43 @@ final _log = Logger('devtools_server_client');
4747bool get isDevToolsServerAvailable =>
4848 kIsWeb && storage is ServerConnectionStorage ;
4949
50- const _debugDevToolsServerFlag = 'debug_devtools_server' ;
50+ const _debugDevToolsServerEnvironmentVariable = String .fromEnvironment (
51+ 'debug_devtools_server' ,
52+ );
53+
54+ /// Whether DevTools was run using the `dt run` command, which runs DevTools in
55+ /// debug mode using `flutter run` and connects it to an instance of the
56+ /// DevTools server.
57+ bool get usingDebugDevToolsServer =>
58+ _debugDevToolsServerEnvironmentVariable.isNotEmpty && ! kReleaseMode;
5159
5260String get devToolsServerUriAsString {
53- const debugDevToolsServerUriAsString = String .fromEnvironment (
54- _debugDevToolsServerFlag,
55- );
5661 // Ensure we only use the debug DevTools server URI in non-release
5762 // builds. By running `dt run`, an instance of DevTools run with `flutter run`
5863 // can be connected to the DevTools server on a different port.
59- return debugDevToolsServerUriAsString.isNotEmpty && ! kReleaseMode
60- ? debugDevToolsServerUriAsString
64+ return usingDebugDevToolsServer
65+ ? _debugDevToolsServerEnvironmentVariable
6166 : Uri .base .toString ();
6267}
6368
69+ /// Helper to build a request URI to the DevTools server, which may not be on
70+ /// the same origin as the DevTools app window.
71+ Uri buildDevToolsServerRequestUri (String url) {
72+ // [_debugDevToolsServerEnvironmentVariable] will be the empty string if the
73+ // [_debugDevToolsServerFlag] environment variable declaration was not set
74+ // using `--dart-define`.
75+ const baseUri = _debugDevToolsServerEnvironmentVariable;
76+ return Uri .parse (path.join (baseUri, url));
77+ }
78+
6479/// Helper to catch any server request which could fail.
6580///
6681/// Returns HttpRequest or null (if server failure).
6782Future <Response ?> request (String url) async {
6883 Response ? response;
6984
7085 try {
71- // This will be the empty string if this environment declaration was not
72- // set using `--dart-define`.
73- const baseUri = String .fromEnvironment (_debugDevToolsServerFlag);
74- response = await post (Uri .parse (path.join (baseUri, url)));
86+ response = await post (buildDevToolsServerRequestUri (url));
7587 } catch (_) {}
7688
7789 return response;
0 commit comments