Skip to content

Commit

Permalink
Send the Flutter inspector URL to Cider when requested (#2301)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliette authored Dec 11, 2023
1 parent 8375cd6 commit c26b8c8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
45 changes: 45 additions & 0 deletions dwds/debug_extension_mv3/web/cider_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import 'package:js/js.dart';
import 'chrome_api.dart';
import 'debug_session.dart';
import 'logger.dart';
import 'storage.dart';
import 'utils.dart';

/// Used to identify messages passed to/from Cider.
///
Expand All @@ -25,6 +27,8 @@ const _ciderDartMessageKey = 'CIDER_DART';
/// Cider extension.
enum CiderMessageType {
error,
inspectorUrlResponse,
inspectorUrlRequest,
startDebugResponse,
startDebugRequest,
stopDebugResponse,
Expand Down Expand Up @@ -113,6 +117,8 @@ Future<void> _handleMessageFromCider(dynamic message, Port _) async {
await _startDebugging(appId: messageBody);
} else if (messageType == CiderMessageType.stopDebugRequest.name) {
await _stopDebugging(appId: messageBody);
} else if (messageType == CiderMessageType.inspectorUrlRequest.name) {
await _sendInspectorUrl(appId: messageBody);
}
}

Expand Down Expand Up @@ -150,6 +156,45 @@ Future<void> _stopDebugging({String? appId}) async {
}
}

Future<void> _sendInspectorUrl({String? appId}) async {
if (appId == null) {
_sendNoAppIdError();
return;
}
final tabId = _tabId(appId);
final alreadyDebugging = isActiveDebugSession(tabId);
if (!alreadyDebugging) {
sendErrorMessageToCider(
errorType: CiderErrorType.invalidRequest,
errorDetails:
'Cannot send the inspector URL before the debugger has been attached.',
);
return;
}
final devToolsUri = await fetchStorageObject<String>(
type: StorageObject.devToolsUri,
tabId: tabId,
);
if (devToolsUri == null) {
sendErrorMessageToCider(
errorType: CiderErrorType.internalError,
errorDetails: 'Failed to fetch the DevTools URI for the inspector.',
);
return;
}
final inspectorUrl = addQueryParameters(
devToolsUri,
queryParameters: {
'embed': 'true',
'page': 'inspector',
},
);
sendMessageToCider(
messageType: CiderMessageType.inspectorUrlResponse,
messageBody: inspectorUrl,
);
}

int _tabId(String appId) {
final tabId = appId.split('-').last;
return int.parse(tabId);
Expand Down
9 changes: 8 additions & 1 deletion dwds/debug_extension_mv3/web/debug_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,14 @@ void _routeDwdsEvent(String eventData, SocketClient client, int tabId) {
tabId: tabId,
);
if (message.method == 'dwds.devtoolsUri') {
if (_tabIdToTrigger[tabId] != Trigger.cider) {
if (_tabIdToTrigger[tabId] == Trigger.cider) {
// Save the DevTools URI so that Cider can request it later:
setStorageObject(
type: StorageObject.devToolsUri,
value: message.params,
tabId: tabId,
);
} else {
_openDevTools(message.params, dartAppTabId: tabId);
}
}
Expand Down

0 comments on commit c26b8c8

Please sign in to comment.