diff --git a/dwds/debug_extension_mv3/web/cider_connection.dart b/dwds/debug_extension_mv3/web/cider_connection.dart index 54a78e659..f0e67bb51 100644 --- a/dwds/debug_extension_mv3/web/cider_connection.dart +++ b/dwds/debug_extension_mv3/web/cider_connection.dart @@ -8,13 +8,11 @@ library cider_connection; import 'dart:convert'; import 'dart:js_util'; -import 'package:dwds/data/debug_info.dart'; import 'package:js/js.dart'; import 'chrome_api.dart'; import 'debug_session.dart'; import 'logger.dart'; -import 'storage.dart'; /// Used to identify messages passed to/from Cider. /// @@ -38,12 +36,9 @@ enum CiderMessageType { /// The types must match those defined by ChromeExtensionErrorType in the /// Cider extension. enum CiderErrorType { - chromeError, internalError, invalidRequest, - multipleDartTabs, - noDartTab, - noWorkspace, + noAppId, } const _ciderPortName = 'cider'; @@ -115,36 +110,32 @@ Future _handleMessageFromCider(dynamic message, Port _) async { final messageBody = decoded['messageBody'] as String?; if (messageType == CiderMessageType.startDebugRequest.name) { - await _startDebugging(workspaceName: messageBody); + await _startDebugging(appId: messageBody); } else if (messageType == CiderMessageType.stopDebugRequest.name) { - await _stopDebugging(workspaceName: messageBody); + await _stopDebugging(appId: messageBody); } } -Future _startDebugging({String? workspaceName}) async { - if (workspaceName == null) { - _sendNoWorkspaceError(); +Future _startDebugging({String? appId}) async { + if (appId == null) { + _sendNoAppIdError(); return; } - - final dartTab = await _findDartTabIdForWorkspace(workspaceName); - if (dartTab != null) { - // TODO(https://github.com/dart-lang/webdev/issues/2198): When debugging - // with Cider, disable debugging with DevTools. - await attachDebugger(dartTab, trigger: Trigger.cider); - } + final tabId = _tabId(appId); + // TODO(https://github.com/dart-lang/webdev/issues/2198): When debugging + // with Cider, disable debugging with DevTools. + await attachDebugger(tabId, trigger: Trigger.cider); } -Future _stopDebugging({String? workspaceName}) async { - if (workspaceName == null) { - _sendNoWorkspaceError(); +Future _stopDebugging({String? appId}) async { + if (appId == null) { + _sendNoAppIdError(); return; } + final tabId = _tabId(appId); - final dartTab = await _findDartTabIdForWorkspace(workspaceName); - if (dartTab == null) return; final successfullyDetached = await detachDebugger( - dartTab, + tabId, type: TabType.dartApp, reason: DetachReason.canceledByUser, ); @@ -159,48 +150,14 @@ Future _stopDebugging({String? workspaceName}) async { } } -void _sendNoWorkspaceError() { - sendErrorMessageToCider( - errorType: CiderErrorType.noWorkspace, - errorDetails: 'Cannot find a debuggable Dart tab without a workspace', - ); +int _tabId(String appId) { + final tabId = appId.split('-').last; + return int.parse(tabId); } -Future _findDartTabIdForWorkspace(String workspaceName) async { - final allTabsInfo = await fetchAllStorageObjectsOfType( - type: StorageObject.debugInfo, +void _sendNoAppIdError() { + sendErrorMessageToCider( + errorType: CiderErrorType.noAppId, + errorDetails: 'Cannot find a debuggable Dart tab without an app ID', ); - final dartTabIds = allTabsInfo - .where( - (debugInfo) => debugInfo.workspaceName == workspaceName, - ) - .map( - (info) => info.tabId, - ) - .toList(); - - if (dartTabIds.isEmpty) { - sendErrorMessageToCider( - errorType: CiderErrorType.noDartTab, - errorDetails: 'No debuggable Dart tabs found.', - ); - return null; - } - if (dartTabIds.length > 1) { - sendErrorMessageToCider( - errorType: CiderErrorType.multipleDartTabs, - errorDetails: 'Too many debuggable Dart tabs found.', - ); - return null; - } - final tabId = dartTabIds.first; - if (tabId == null) { - sendErrorMessageToCider( - errorType: CiderErrorType.chromeError, - errorDetails: 'Debuggable Dart tab is null.', - ); - return null; - } - - return tabId; } diff --git a/dwds/debug_extension_mv3/web/debug_session.dart b/dwds/debug_extension_mv3/web/debug_session.dart index 268556c40..fd9e24446 100644 --- a/dwds/debug_extension_mv3/web/debug_session.dart +++ b/dwds/debug_extension_mv3/web/debug_session.dart @@ -111,7 +111,10 @@ Future attachDebugger( required Trigger trigger, }) async { // Validate that the tab can be debugged: - final tabIsDebuggable = await _validateTabIsDebuggable(dartAppTabId); + final tabIsDebuggable = await _validateTabIsDebuggable( + dartAppTabId, + forwardErrorsToCider: trigger == Trigger.cider, + ); if (!tabIsDebuggable) return; _tabIdToTrigger[dartAppTabId] = trigger; @@ -168,12 +171,16 @@ Future clearStaleDebugSession(int tabId) async { } } -Future _validateTabIsDebuggable(int dartAppTabId) async { +Future _validateTabIsDebuggable( + int dartAppTabId, { + bool forwardErrorsToCider = false, +}) async { // Check if a debugger is already attached: final existingDebuggerLocation = _debuggerLocation(dartAppTabId); if (existingDebuggerLocation != null) { - await _showWarningNotification( + await _showWarning( 'Already debugging in ${existingDebuggerLocation.displayName}.', + forwardToCider: forwardErrorsToCider, ); return false; } @@ -183,7 +190,10 @@ Future _validateTabIsDebuggable(int dartAppTabId) async { tabId: dartAppTabId, ); if (debugInfo == null) { - await _showWarningNotification('Not a Dart app.'); + await _showWarning( + 'Not a Dart app.', + forwardToCider: forwardErrorsToCider, + ); return false; } // Determine if there are multiple apps in the tab: @@ -192,8 +202,9 @@ Future _validateTabIsDebuggable(int dartAppTabId) async { tabId: dartAppTabId, ); if (multipleApps != null) { - await _showWarningNotification( + await _showWarning( 'Dart debugging is not supported in a multi-app environment.', + forwardToCider: forwardErrorsToCider, ); return false; } @@ -692,6 +703,21 @@ Future _sendAuthRequest(String authUrl) async { return responseBody.contains('Dart Debug Authentication Success!'); } +Future _showWarning( + String message, { + bool forwardToCider = false, +}) { + if (forwardToCider) { + sendErrorMessageToCider( + errorType: CiderErrorType.invalidRequest, + errorDetails: message, + ); + return Future.value(true); + } else { + return _showWarningNotification(message); + } +} + Future _showWarningNotification(String message) { final completer = Completer(); chrome.notifications.create(