Skip to content

Commit c007560

Browse files
authored
[MV3 Debug Extension] Show warning when clicking on debug extension for non Dart app (#2015)
1 parent 1a010dc commit c007560

7 files changed

+67
-23
lines changed

dwds/debug_extension_mv3/web/debug_session.dart

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,9 @@ int? get latestAppBeingDebugged =>
106106

107107
Future<void> attachDebugger(int dartAppTabId,
108108
{required Trigger trigger}) async {
109-
// Check if a debugger is already attached:
110-
final existingDebuggerLocation = _debuggerLocation(dartAppTabId);
111-
if (existingDebuggerLocation != null) {
112-
return _showWarningNotification(
113-
'Already debugging in ${existingDebuggerLocation.displayName}.',
114-
);
115-
}
116-
// Determine if there are multiple apps in the tab:
117-
final multipleApps = await fetchStorageObject<String>(
118-
type: StorageObject.multipleAppsDetected,
119-
tabId: dartAppTabId,
120-
);
121-
if (multipleApps != null) {
122-
return _showWarningNotification(
123-
'Dart debugging is not supported in a multi-app environment.',
124-
);
125-
}
126-
// Verify that the user is authenticated:
127-
final isAuthenticated = await _authenticateUser(dartAppTabId);
128-
if (!isAuthenticated) return;
109+
// Validate that the tab can be debugged:
110+
final tabIsDebuggable = await _validateTabIsDebuggable(dartAppTabId);
111+
if (!tabIsDebuggable) return;
129112

130113
_tabIdToTrigger[dartAppTabId] = trigger;
131114
_registerDebugEventListeners();
@@ -177,6 +160,40 @@ Future<void> clearStaleDebugSession(int tabId) async {
177160
}
178161
}
179162

163+
Future<bool> _validateTabIsDebuggable(int dartAppTabId) async {
164+
// Check if a debugger is already attached:
165+
final existingDebuggerLocation = _debuggerLocation(dartAppTabId);
166+
if (existingDebuggerLocation != null) {
167+
await _showWarningNotification(
168+
'Already debugging in ${existingDebuggerLocation.displayName}.',
169+
);
170+
return false;
171+
}
172+
// Determine if this is a Dart app:
173+
final debugInfo = await fetchStorageObject<DebugInfo>(
174+
type: StorageObject.debugInfo,
175+
tabId: dartAppTabId,
176+
);
177+
if (debugInfo == null) {
178+
await _showWarningNotification('Not a Dart app.');
179+
return false;
180+
}
181+
// Determine if there are multiple apps in the tab:
182+
final multipleApps = await fetchStorageObject<String>(
183+
type: StorageObject.multipleAppsDetected,
184+
tabId: dartAppTabId,
185+
);
186+
if (multipleApps != null) {
187+
await _showWarningNotification(
188+
'Dart debugging is not supported in a multi-app environment.',
189+
);
190+
return false;
191+
}
192+
// Verify that the user is authenticated:
193+
final isAuthenticated = await _authenticateUser(dartAppTabId);
194+
return isAuthenticated;
195+
}
196+
180197
void _registerDebugEventListeners() {
181198
chrome.debugger.onEvent.addListener(allowInterop(_onDebuggerEvent));
182199
chrome.debugger.onDetach.addListener(allowInterop((source, _) async {
@@ -548,7 +565,7 @@ Future<bool> _authenticateUser(int tabId) async {
548565
);
549566
final authUrl = debugInfo?.authUrl;
550567
if (authUrl == null) {
551-
_showWarningNotification('Cannot authenticate user.');
568+
await _showWarningNotification('Cannot authenticate user.');
552569
return false;
553570
}
554571
final isAuthenticated = await _sendAuthRequest(authUrl);
@@ -582,7 +599,8 @@ Future<bool> _sendAuthRequest(String authUrl) async {
582599
return responseBody.contains('Dart Debug Authentication Success!');
583600
}
584601

585-
void _showWarningNotification(String message) {
602+
Future<bool> _showWarningNotification(String message) {
603+
final completer = Completer<bool>();
586604
chrome.notifications.create(
587605
/*notificationId*/ null,
588606
NotificationOptions(
@@ -591,8 +609,11 @@ void _showWarningNotification(String message) {
591609
iconUrl: 'static_assets/dart.png',
592610
type: 'basic',
593611
),
594-
/*callback*/ null,
612+
allowInterop((_) {
613+
completer.complete(true);
614+
}),
595615
);
616+
return completer.future;
596617
}
597618

598619
DebuggerLocation? _debuggerLocation(int dartAppTabId) {

dwds/test/puppeteer/extension_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,29 @@ void main() async {
373373
await devToolsTabTarget.onClose;
374374
});
375375

376+
test('Clicking extension icon for a non Dart app shows warning',
377+
() async {
378+
// Navigate to a page that doesn't contain a Dart app:
379+
final tab = await navigateToPage(browser,
380+
url: 'https://dart.dev', isNew: true);
381+
// Click on the Dart Debug Extension icon:
382+
await workerEvalDelay();
383+
await clickOnExtensionIcon(
384+
worker: worker,
385+
backgroundPage: backgroundPage,
386+
);
387+
// There should now be a warning notificiation:
388+
final chromeNotifications = await evaluate(
389+
_getNotifications(),
390+
worker: worker,
391+
backgroundPage: backgroundPage,
392+
);
393+
await workerEvalDelay();
394+
expect(chromeNotifications, isNotEmpty);
395+
// Close the tab:
396+
await tab.close();
397+
});
398+
376399
test('Refreshing the Dart app does not open a new Dart DevTools',
377400
() async {
378401
final appUrl = context.appUrl;
321 Bytes
Loading
6 Bytes
Loading
384 Bytes
Loading
384 Bytes
Loading
313 Bytes
Loading

0 commit comments

Comments
 (0)