Skip to content

Commit 50534a1

Browse files
authored
Cider passes an app ID instead of a workspace name to connect to the Dart Debug Extension (#2272)
1 parent b3d6ef1 commit 50534a1

File tree

2 files changed

+53
-70
lines changed

2 files changed

+53
-70
lines changed

dwds/debug_extension_mv3/web/cider_connection.dart

Lines changed: 22 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ library cider_connection;
88
import 'dart:convert';
99
import 'dart:js_util';
1010

11-
import 'package:dwds/data/debug_info.dart';
1211
import 'package:js/js.dart';
1312

1413
import 'chrome_api.dart';
1514
import 'debug_session.dart';
1615
import 'logger.dart';
17-
import 'storage.dart';
1816

1917
/// Used to identify messages passed to/from Cider.
2018
///
@@ -38,12 +36,9 @@ enum CiderMessageType {
3836
/// The types must match those defined by ChromeExtensionErrorType in the
3937
/// Cider extension.
4038
enum CiderErrorType {
41-
chromeError,
4239
internalError,
4340
invalidRequest,
44-
multipleDartTabs,
45-
noDartTab,
46-
noWorkspace,
41+
noAppId,
4742
}
4843

4944
const _ciderPortName = 'cider';
@@ -115,36 +110,32 @@ Future<void> _handleMessageFromCider(dynamic message, Port _) async {
115110
final messageBody = decoded['messageBody'] as String?;
116111

117112
if (messageType == CiderMessageType.startDebugRequest.name) {
118-
await _startDebugging(workspaceName: messageBody);
113+
await _startDebugging(appId: messageBody);
119114
} else if (messageType == CiderMessageType.stopDebugRequest.name) {
120-
await _stopDebugging(workspaceName: messageBody);
115+
await _stopDebugging(appId: messageBody);
121116
}
122117
}
123118

124-
Future<void> _startDebugging({String? workspaceName}) async {
125-
if (workspaceName == null) {
126-
_sendNoWorkspaceError();
119+
Future<void> _startDebugging({String? appId}) async {
120+
if (appId == null) {
121+
_sendNoAppIdError();
127122
return;
128123
}
129-
130-
final dartTab = await _findDartTabIdForWorkspace(workspaceName);
131-
if (dartTab != null) {
132-
// TODO(https://github.com/dart-lang/webdev/issues/2198): When debugging
133-
// with Cider, disable debugging with DevTools.
134-
await attachDebugger(dartTab, trigger: Trigger.cider);
135-
}
124+
final tabId = _tabId(appId);
125+
// TODO(https://github.com/dart-lang/webdev/issues/2198): When debugging
126+
// with Cider, disable debugging with DevTools.
127+
await attachDebugger(tabId, trigger: Trigger.cider);
136128
}
137129

138-
Future<void> _stopDebugging({String? workspaceName}) async {
139-
if (workspaceName == null) {
140-
_sendNoWorkspaceError();
130+
Future<void> _stopDebugging({String? appId}) async {
131+
if (appId == null) {
132+
_sendNoAppIdError();
141133
return;
142134
}
135+
final tabId = _tabId(appId);
143136

144-
final dartTab = await _findDartTabIdForWorkspace(workspaceName);
145-
if (dartTab == null) return;
146137
final successfullyDetached = await detachDebugger(
147-
dartTab,
138+
tabId,
148139
type: TabType.dartApp,
149140
reason: DetachReason.canceledByUser,
150141
);
@@ -159,48 +150,14 @@ Future<void> _stopDebugging({String? workspaceName}) async {
159150
}
160151
}
161152

162-
void _sendNoWorkspaceError() {
163-
sendErrorMessageToCider(
164-
errorType: CiderErrorType.noWorkspace,
165-
errorDetails: 'Cannot find a debuggable Dart tab without a workspace',
166-
);
153+
int _tabId(String appId) {
154+
final tabId = appId.split('-').last;
155+
return int.parse(tabId);
167156
}
168157

169-
Future<int?> _findDartTabIdForWorkspace(String workspaceName) async {
170-
final allTabsInfo = await fetchAllStorageObjectsOfType<DebugInfo>(
171-
type: StorageObject.debugInfo,
158+
void _sendNoAppIdError() {
159+
sendErrorMessageToCider(
160+
errorType: CiderErrorType.noAppId,
161+
errorDetails: 'Cannot find a debuggable Dart tab without an app ID',
172162
);
173-
final dartTabIds = allTabsInfo
174-
.where(
175-
(debugInfo) => debugInfo.workspaceName == workspaceName,
176-
)
177-
.map(
178-
(info) => info.tabId,
179-
)
180-
.toList();
181-
182-
if (dartTabIds.isEmpty) {
183-
sendErrorMessageToCider(
184-
errorType: CiderErrorType.noDartTab,
185-
errorDetails: 'No debuggable Dart tabs found.',
186-
);
187-
return null;
188-
}
189-
if (dartTabIds.length > 1) {
190-
sendErrorMessageToCider(
191-
errorType: CiderErrorType.multipleDartTabs,
192-
errorDetails: 'Too many debuggable Dart tabs found.',
193-
);
194-
return null;
195-
}
196-
final tabId = dartTabIds.first;
197-
if (tabId == null) {
198-
sendErrorMessageToCider(
199-
errorType: CiderErrorType.chromeError,
200-
errorDetails: 'Debuggable Dart tab is null.',
201-
);
202-
return null;
203-
}
204-
205-
return tabId;
206163
}

dwds/debug_extension_mv3/web/debug_session.dart

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ Future<void> attachDebugger(
111111
required Trigger trigger,
112112
}) async {
113113
// Validate that the tab can be debugged:
114-
final tabIsDebuggable = await _validateTabIsDebuggable(dartAppTabId);
114+
final tabIsDebuggable = await _validateTabIsDebuggable(
115+
dartAppTabId,
116+
forwardErrorsToCider: trigger == Trigger.cider,
117+
);
115118
if (!tabIsDebuggable) return;
116119

117120
_tabIdToTrigger[dartAppTabId] = trigger;
@@ -168,12 +171,16 @@ Future<void> clearStaleDebugSession(int tabId) async {
168171
}
169172
}
170173

171-
Future<bool> _validateTabIsDebuggable(int dartAppTabId) async {
174+
Future<bool> _validateTabIsDebuggable(
175+
int dartAppTabId, {
176+
bool forwardErrorsToCider = false,
177+
}) async {
172178
// Check if a debugger is already attached:
173179
final existingDebuggerLocation = _debuggerLocation(dartAppTabId);
174180
if (existingDebuggerLocation != null) {
175-
await _showWarningNotification(
181+
await _showWarning(
176182
'Already debugging in ${existingDebuggerLocation.displayName}.',
183+
forwardToCider: forwardErrorsToCider,
177184
);
178185
return false;
179186
}
@@ -183,7 +190,10 @@ Future<bool> _validateTabIsDebuggable(int dartAppTabId) async {
183190
tabId: dartAppTabId,
184191
);
185192
if (debugInfo == null) {
186-
await _showWarningNotification('Not a Dart app.');
193+
await _showWarning(
194+
'Not a Dart app.',
195+
forwardToCider: forwardErrorsToCider,
196+
);
187197
return false;
188198
}
189199
// Determine if there are multiple apps in the tab:
@@ -192,8 +202,9 @@ Future<bool> _validateTabIsDebuggable(int dartAppTabId) async {
192202
tabId: dartAppTabId,
193203
);
194204
if (multipleApps != null) {
195-
await _showWarningNotification(
205+
await _showWarning(
196206
'Dart debugging is not supported in a multi-app environment.',
207+
forwardToCider: forwardErrorsToCider,
197208
);
198209
return false;
199210
}
@@ -692,6 +703,21 @@ Future<bool> _sendAuthRequest(String authUrl) async {
692703
return responseBody.contains('Dart Debug Authentication Success!');
693704
}
694705

706+
Future<bool> _showWarning(
707+
String message, {
708+
bool forwardToCider = false,
709+
}) {
710+
if (forwardToCider) {
711+
sendErrorMessageToCider(
712+
errorType: CiderErrorType.invalidRequest,
713+
errorDetails: message,
714+
);
715+
return Future.value(true);
716+
} else {
717+
return _showWarningNotification(message);
718+
}
719+
}
720+
695721
Future<bool> _showWarningNotification(String message) {
696722
final completer = Completer<bool>();
697723
chrome.notifications.create(

0 commit comments

Comments
 (0)