diff --git a/dwds/debug_extension_mv3/web/debug_session.dart b/dwds/debug_extension_mv3/web/debug_session.dart index 47ac4a813..4ceac2a4e 100644 --- a/dwds/debug_extension_mv3/web/debug_session.dart +++ b/dwds/debug_extension_mv3/web/debug_session.dart @@ -106,26 +106,9 @@ int? get latestAppBeingDebugged => Future attachDebugger(int dartAppTabId, {required Trigger trigger}) async { - // Check if a debugger is already attached: - final existingDebuggerLocation = _debuggerLocation(dartAppTabId); - if (existingDebuggerLocation != null) { - return _showWarningNotification( - 'Already debugging in ${existingDebuggerLocation.displayName}.', - ); - } - // Determine if there are multiple apps in the tab: - final multipleApps = await fetchStorageObject( - type: StorageObject.multipleAppsDetected, - tabId: dartAppTabId, - ); - if (multipleApps != null) { - return _showWarningNotification( - 'Dart debugging is not supported in a multi-app environment.', - ); - } - // Verify that the user is authenticated: - final isAuthenticated = await _authenticateUser(dartAppTabId); - if (!isAuthenticated) return; + // Validate that the tab can be debugged: + final tabIsDebuggable = await _validateTabIsDebuggable(dartAppTabId); + if (!tabIsDebuggable) return; _tabIdToTrigger[dartAppTabId] = trigger; _registerDebugEventListeners(); @@ -177,6 +160,40 @@ Future clearStaleDebugSession(int tabId) async { } } +Future _validateTabIsDebuggable(int dartAppTabId) async { + // Check if a debugger is already attached: + final existingDebuggerLocation = _debuggerLocation(dartAppTabId); + if (existingDebuggerLocation != null) { + await _showWarningNotification( + 'Already debugging in ${existingDebuggerLocation.displayName}.', + ); + return false; + } + // Determine if this is a Dart app: + final debugInfo = await fetchStorageObject( + type: StorageObject.debugInfo, + tabId: dartAppTabId, + ); + if (debugInfo == null) { + await _showWarningNotification('Not a Dart app.'); + return false; + } + // Determine if there are multiple apps in the tab: + final multipleApps = await fetchStorageObject( + type: StorageObject.multipleAppsDetected, + tabId: dartAppTabId, + ); + if (multipleApps != null) { + await _showWarningNotification( + 'Dart debugging is not supported in a multi-app environment.', + ); + return false; + } + // Verify that the user is authenticated: + final isAuthenticated = await _authenticateUser(dartAppTabId); + return isAuthenticated; +} + void _registerDebugEventListeners() { chrome.debugger.onEvent.addListener(allowInterop(_onDebuggerEvent)); chrome.debugger.onDetach.addListener(allowInterop((source, _) async { @@ -548,7 +565,7 @@ Future _authenticateUser(int tabId) async { ); final authUrl = debugInfo?.authUrl; if (authUrl == null) { - _showWarningNotification('Cannot authenticate user.'); + await _showWarningNotification('Cannot authenticate user.'); return false; } final isAuthenticated = await _sendAuthRequest(authUrl); @@ -582,7 +599,8 @@ Future _sendAuthRequest(String authUrl) async { return responseBody.contains('Dart Debug Authentication Success!'); } -void _showWarningNotification(String message) { +Future _showWarningNotification(String message) { + final completer = Completer(); chrome.notifications.create( /*notificationId*/ null, NotificationOptions( @@ -591,8 +609,11 @@ void _showWarningNotification(String message) { iconUrl: 'static_assets/dart.png', type: 'basic', ), - /*callback*/ null, + allowInterop((_) { + completer.complete(true); + }), ); + return completer.future; } DebuggerLocation? _debuggerLocation(int dartAppTabId) { diff --git a/dwds/test/puppeteer/extension_test.dart b/dwds/test/puppeteer/extension_test.dart index b68a3d882..9a48c0f90 100644 --- a/dwds/test/puppeteer/extension_test.dart +++ b/dwds/test/puppeteer/extension_test.dart @@ -373,6 +373,29 @@ void main() async { await devToolsTabTarget.onClose; }); + test('Clicking extension icon for a non Dart app shows warning', + () async { + // Navigate to a page that doesn't contain a Dart app: + final tab = await navigateToPage(browser, + url: 'https://dart.dev', isNew: true); + // Click on the Dart Debug Extension icon: + await workerEvalDelay(); + await clickOnExtensionIcon( + worker: worker, + backgroundPage: backgroundPage, + ); + // There should now be a warning notificiation: + final chromeNotifications = await evaluate( + _getNotifications(), + worker: worker, + backgroundPage: backgroundPage, + ); + await workerEvalDelay(); + expect(chromeNotifications, isNotEmpty); + // Close the tab: + await tab.close(); + }); + test('Refreshing the Dart app does not open a new Dart DevTools', () async { final appUrl = context.appUrl; diff --git a/dwds/test/puppeteer/test_images/chromeDevTools_externalBuild.png b/dwds/test/puppeteer/test_images/chromeDevTools_externalBuild.png index 6372516fe..0de722768 100644 Binary files a/dwds/test/puppeteer/test_images/chromeDevTools_externalBuild.png and b/dwds/test/puppeteer/test_images/chromeDevTools_externalBuild.png differ diff --git a/dwds/test/puppeteer/test_images/debuggerPanelDisconnected_flutterApp.png b/dwds/test/puppeteer/test_images/debuggerPanelDisconnected_flutterApp.png index c8040bfe3..96ac4caa6 100644 Binary files a/dwds/test/puppeteer/test_images/debuggerPanelDisconnected_flutterApp.png and b/dwds/test/puppeteer/test_images/debuggerPanelDisconnected_flutterApp.png differ diff --git a/dwds/test/puppeteer/test_images/debuggerPanelLandingPage_dartApp.png b/dwds/test/puppeteer/test_images/debuggerPanelLandingPage_dartApp.png index b9184f2e4..090a1c74b 100644 Binary files a/dwds/test/puppeteer/test_images/debuggerPanelLandingPage_dartApp.png and b/dwds/test/puppeteer/test_images/debuggerPanelLandingPage_dartApp.png differ diff --git a/dwds/test/puppeteer/test_images/debuggerPanelLandingPage_flutterApp.png b/dwds/test/puppeteer/test_images/debuggerPanelLandingPage_flutterApp.png index b9184f2e4..090a1c74b 100644 Binary files a/dwds/test/puppeteer/test_images/debuggerPanelLandingPage_flutterApp.png and b/dwds/test/puppeteer/test_images/debuggerPanelLandingPage_flutterApp.png differ diff --git a/dwds/test/puppeteer/test_images/inspectorPanelLandingPage_flutterApp.png b/dwds/test/puppeteer/test_images/inspectorPanelLandingPage_flutterApp.png index 6e4385451..39d8b8916 100644 Binary files a/dwds/test/puppeteer/test_images/inspectorPanelLandingPage_flutterApp.png and b/dwds/test/puppeteer/test_images/inspectorPanelLandingPage_flutterApp.png differ