Skip to content

Commit 47287e3

Browse files
authored
[MV3 Debug Extension] Extension sets the ide query parameter for the DevTools URI (#1943)
1 parent 1136d51 commit 47287e3

File tree

8 files changed

+184
-36
lines changed

8 files changed

+184
-36
lines changed

dwds/debug_extension_mv3/web/debug_session.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ Future<bool> _connectToDwds({
267267
..instanceId = debugInfo.appInstanceId
268268
..contextId = dartAppContextId
269269
..tabUrl = tabUrl
270-
..uriOnly = true));
270+
..uriOnly = true
271+
..isMv3Extension = true));
271272
return true;
272273
}
273274

@@ -358,7 +359,12 @@ void _openDevTools(String devToolsUri, {required int dartAppTabId}) async {
358359
final devToolsOpener = await fetchStorageObject<DevToolsOpener>(
359360
type: StorageObject.devToolsOpener);
360361
final devToolsTab = await createTab(
361-
devToolsUri,
362+
addQueryParameters(
363+
devToolsUri,
364+
queryParameters: {
365+
'ide': 'DebugExtension',
366+
},
367+
),
362368
inNewWindow: devToolsOpener?.newWindow ?? false,
363369
);
364370
debugSession.devToolsTabId = devToolsTab.id;

dwds/debug_extension_mv3/web/panel.dart

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import 'debug_session.dart';
1818
import 'logger.dart';
1919
import 'messaging.dart';
2020
import 'storage.dart';
21+
import 'utils.dart';
2122

2223
bool connecting = false;
23-
String devToolsBackgroundColor = darkColor;
24+
String backgroundColor = darkColor;
2425
bool isDartApp = true;
2526

2627
const bugLinkId = 'bugLink';
@@ -148,10 +149,10 @@ void _setColorThemeToMatchChromeDevTools() async {
148149
final chromeTheme = chrome.devtools.panels.themeName;
149150
final panelBody = document.getElementById(panelBodyId);
150151
if (chromeTheme == 'dark') {
151-
devToolsBackgroundColor = darkColor;
152+
backgroundColor = darkColor;
152153
_updateColorThemeForElement(panelBody, isDarkTheme: true);
153154
} else {
154-
devToolsBackgroundColor = lightColor;
155+
backgroundColor = lightColor;
155156
_updateColorThemeForElement(panelBody, isDarkTheme: false);
156157
}
157158
}
@@ -246,10 +247,16 @@ void _injectDevToolsIframe(String devToolsUri) {
246247
final panelBody = document.getElementById(panelBodyId);
247248
final panelType = panelBody?.getAttribute(panelAttribute) ?? 'debugger';
248249
final iframe = document.createElement('iframe');
249-
iframe.setAttribute(
250-
'src',
251-
'$devToolsUri&embed=true&page=$panelType&backgroundColor=$devToolsBackgroundColor',
250+
final iframeSrc = addQueryParameters(
251+
devToolsUri,
252+
queryParameters: {
253+
'ide': 'ChromeDevTools',
254+
'embed': 'true',
255+
'page': panelType,
256+
'backgroundColor': backgroundColor,
257+
},
252258
);
259+
iframe.setAttribute('src', iframeSrc);
253260
_hideWarningBanner();
254261
_updateElementVisibility(landingPageId, visible: false);
255262
_updateElementVisibility(loadingSpinnerId, visible: false);

dwds/debug_extension_mv3/web/utils.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,15 @@ bool isDevMode() {
4747
final extensionName = getProperty(extensionManifest, 'name') ?? '';
4848
return extensionName.contains('DEV');
4949
}
50+
51+
String addQueryParameters(
52+
String uri, {
53+
required Map<String, String> queryParameters,
54+
}) {
55+
final originalUri = Uri.parse(uri);
56+
final newUri = originalUri.replace(queryParameters: {
57+
...originalUri.queryParameters,
58+
...queryParameters,
59+
});
60+
return newUri.toString();
61+
}

dwds/lib/data/devtools_request.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,18 @@ abstract class DevToolsRequest
4040
/// correct `dartAppInstanceId` automatically.
4141
String? get tabUrl;
4242

43-
/// If this is a uri only request.
43+
/// Designates this as a request to send back the DevTools URI instead of
44+
/// opening DevTools in a new tab or window.
4445
///
45-
/// Only available on requests coming from dart debug extension.
46-
/// If true, DevTools should open in an embedded Chrome DevTools tab.
46+
/// Only available on requests coming from the Dart Debug Extension. Is `null`
47+
/// for local debug service.
4748
bool? get uriOnly;
49+
50+
/// Whether or not the MV3 Dart Debug Extension sent the request. Is `null`
51+
/// for local debug service.
52+
///
53+
/// Only available on requests coming from the Dart Debug Extension.
54+
bool? get isMv3Extension;
4855
}
4956

5057
/// A response to a [DevToolsRequest].

dwds/lib/data/devtools_request.g.dart

Lines changed: 28 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dwds/lib/src/handlers/dev_handler.dart

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -546,28 +546,40 @@ class DevHandler {
546546
extensionDebugConnections.add(DebugConnection(appServices));
547547
_servicesByAppId[appId] = appServices;
548548
}
549+
// If we don't have a DevTools instance, then are connecting to an IDE.
550+
// Therefore return early instead of opening DevTools:
551+
if (_devTools == null) return;
552+
549553
final encodedUri = await appServices.debugService.encodedUri;
550554

551555
appServices.dwdsStats.updateLoadTime(
552556
debuggerStart: debuggerStart, devToolsStart: DateTime.now());
553557

554-
if (_devTools != null) {
555-
// If we only want the URI, this means we are embedding Dart DevTools in
556-
// Chrome DevTools. Therefore return early.
557-
if (devToolsRequest.uriOnly ?? false) {
558-
final devToolsUri = _constructDevToolsUri(
559-
encodedUri,
560-
ideQueryParam: 'ChromeDevTools',
561-
);
562-
extensionDebugger.sendEvent('dwds.devtoolsUri', devToolsUri);
563-
return;
564-
}
565-
final devToolsUri = _constructDevToolsUri(
558+
// TODO(elliette): Remove handling requests from the MV2 extension after
559+
// MV3 release.
560+
// If we only want the URI, this means the Dart Debug Extension should
561+
// handle how to open it. Therefore return early before opening a new
562+
// tab or window:
563+
if (devToolsRequest.uriOnly ?? false) {
564+
// The MV3 extension is responsible for adding the IDE query
565+
// parameter to the DevTools URI.
566+
final devToolsUri = (devToolsRequest.isMv3Extension ?? false)
567+
? _constructDevToolsUri(encodedUri)
568+
: _constructDevToolsUri(
569+
encodedUri,
570+
ideQueryParam: 'ChromeDevTools',
571+
);
572+
return extensionDebugger.sendEvent('dwds.devtoolsUri', devToolsUri);
573+
}
574+
575+
// Otherwise, launch DevTools in a new tab / window:
576+
await _launchDevTools(
577+
extensionDebugger,
578+
_constructDevToolsUri(
566579
encodedUri,
567580
ideQueryParam: 'DebugExtension',
568-
);
569-
await _launchDevTools(extensionDebugger, devToolsUri);
570-
}
581+
),
582+
);
571583
});
572584
}
573585

dwds/lib/src/injected/client.js

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)