Skip to content

Commit c26b8c8

Browse files
authored
Send the Flutter inspector URL to Cider when requested (#2301)
1 parent 8375cd6 commit c26b8c8

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

dwds/debug_extension_mv3/web/cider_connection.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import 'package:js/js.dart';
1313
import 'chrome_api.dart';
1414
import 'debug_session.dart';
1515
import 'logger.dart';
16+
import 'storage.dart';
17+
import 'utils.dart';
1618

1719
/// Used to identify messages passed to/from Cider.
1820
///
@@ -25,6 +27,8 @@ const _ciderDartMessageKey = 'CIDER_DART';
2527
/// Cider extension.
2628
enum CiderMessageType {
2729
error,
30+
inspectorUrlResponse,
31+
inspectorUrlRequest,
2832
startDebugResponse,
2933
startDebugRequest,
3034
stopDebugResponse,
@@ -113,6 +117,8 @@ Future<void> _handleMessageFromCider(dynamic message, Port _) async {
113117
await _startDebugging(appId: messageBody);
114118
} else if (messageType == CiderMessageType.stopDebugRequest.name) {
115119
await _stopDebugging(appId: messageBody);
120+
} else if (messageType == CiderMessageType.inspectorUrlRequest.name) {
121+
await _sendInspectorUrl(appId: messageBody);
116122
}
117123
}
118124

@@ -150,6 +156,45 @@ Future<void> _stopDebugging({String? appId}) async {
150156
}
151157
}
152158

159+
Future<void> _sendInspectorUrl({String? appId}) async {
160+
if (appId == null) {
161+
_sendNoAppIdError();
162+
return;
163+
}
164+
final tabId = _tabId(appId);
165+
final alreadyDebugging = isActiveDebugSession(tabId);
166+
if (!alreadyDebugging) {
167+
sendErrorMessageToCider(
168+
errorType: CiderErrorType.invalidRequest,
169+
errorDetails:
170+
'Cannot send the inspector URL before the debugger has been attached.',
171+
);
172+
return;
173+
}
174+
final devToolsUri = await fetchStorageObject<String>(
175+
type: StorageObject.devToolsUri,
176+
tabId: tabId,
177+
);
178+
if (devToolsUri == null) {
179+
sendErrorMessageToCider(
180+
errorType: CiderErrorType.internalError,
181+
errorDetails: 'Failed to fetch the DevTools URI for the inspector.',
182+
);
183+
return;
184+
}
185+
final inspectorUrl = addQueryParameters(
186+
devToolsUri,
187+
queryParameters: {
188+
'embed': 'true',
189+
'page': 'inspector',
190+
},
191+
);
192+
sendMessageToCider(
193+
messageType: CiderMessageType.inspectorUrlResponse,
194+
messageBody: inspectorUrl,
195+
);
196+
}
197+
153198
int _tabId(String appId) {
154199
final tabId = appId.split('-').last;
155200
return int.parse(tabId);

dwds/debug_extension_mv3/web/debug_session.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,14 @@ void _routeDwdsEvent(String eventData, SocketClient client, int tabId) {
406406
tabId: tabId,
407407
);
408408
if (message.method == 'dwds.devtoolsUri') {
409-
if (_tabIdToTrigger[tabId] != Trigger.cider) {
409+
if (_tabIdToTrigger[tabId] == Trigger.cider) {
410+
// Save the DevTools URI so that Cider can request it later:
411+
setStorageObject(
412+
type: StorageObject.devToolsUri,
413+
value: message.params,
414+
tabId: tabId,
415+
);
416+
} else {
410417
_openDevTools(message.params, dartAppTabId: tabId);
411418
}
412419
}

0 commit comments

Comments
 (0)