Skip to content

Cider expects messages from a Chrome extension to be a JS object, not a string #2249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dwds/debug_extension_mv3/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mv3_extension
publish_to: none
version: 1.36.0
version: 1.37.0
homepage: https://github.com/dart-lang/webdev
description: >-
A Chrome extension for Dart debugging.
Expand Down
28 changes: 22 additions & 6 deletions dwds/debug_extension_mv3/web/cider_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
library cider_connection;

import 'dart:convert';
import 'dart:js_util';

import 'package:dwds/data/debug_info.dart';
import 'package:js/js.dart';
Expand All @@ -15,6 +16,11 @@ import 'debug_session.dart';
import 'logger.dart';
import 'storage.dart';

/// Used to identify messages passed to/from Cider.
///
/// This must match the key defined in the Cider extension.
const _ciderDartMessageKey = 'CIDER_DART';

/// Defines the message types that can be passed to/from Cider.
///
/// The types must match those defined by ChromeExtensionMessageType in the
Expand Down Expand Up @@ -67,7 +73,7 @@ void sendMessageToCider({
'messageType': messageType.name,
'messageBody': messageBody,
});
_ciderPort!.postMessage(message);
_sendMessageToCider(message);
}

/// Sends an error message to the Cider-connected port.
Expand All @@ -82,19 +88,29 @@ void sendErrorMessageToCider({
'errorType': errorType.name,
'messageBody': errorDetails,
});
_ciderPort!.postMessage(message);
_sendMessageToCider(message);
}

void _sendMessageToCider(String json) {
final message = {
'key': _ciderDartMessageKey,
'json': json,
};
_ciderPort!.postMessage(jsify(message));
}

Future<void> _handleMessageFromCider(dynamic message, Port _) async {
if (message is! String) {
final key = getProperty(message, 'key');
final json = getProperty(message, 'json');
if (key != _ciderDartMessageKey || json is! String) {
sendErrorMessageToCider(
errorType: CiderErrorType.invalidRequest,
errorDetails: 'Expected request to be a string: $message',
errorDetails: 'Invalid message format: $message',
);
return;
}

final decoded = jsonDecode(message) as Map<String, dynamic>;
final decoded = jsonDecode(json) as Map<String, dynamic>;
final messageType = decoded['messageType'] as String?;
final messageBody = decoded['messageBody'] as String?;

Expand Down Expand Up @@ -172,7 +188,7 @@ Future<int?> _findDartTabIdForWorkspace(String workspaceName) async {
}
if (dartTabIds.length > 1) {
sendErrorMessageToCider(
errorType: CiderErrorType.noDartTab,
errorType: CiderErrorType.multipleDartTabs,
errorDetails: 'Too many debuggable Dart tabs found.',
);
return null;
Expand Down
2 changes: 1 addition & 1 deletion dwds/debug_extension_mv3/web/manifest_mv2.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Dart Debug Extension",
"version": "1.36",
"version": "1.37",
"manifest_version": 2,
"devtools_page": "static_assets/devtools.html",
"browser_action": {
Expand Down
2 changes: 1 addition & 1 deletion dwds/debug_extension_mv3/web/manifest_mv3.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Dart Debug Extension",
"version": "1.36",
"version": "1.37",
"manifest_version": 3,
"devtools_page": "static_assets/devtools.html",
"action": {
Expand Down