-
Notifications
You must be signed in to change notification settings - Fork 84
[MV3] Dart Debug Extension supports cross-extension communication with AngularDart DevTools #1866
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
92a66ce
Forward messages to external extensions
elliette d622817
Wip
elliette 18e567d
forgot to save
elliette 9201c2d
Add cross-extension communication with AngularDart DevTools
elliette 306702d
Clean up
elliette 307cf5b
Fix analyze error
elliette 8e5d7ba
Resolve merge conflicts
elliette e418205
Respond to PR comments
elliette 7c1e8cb
Add a couple comments
elliette 3a57763
Add TODO
elliette File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
dwds/debug_extension_mv3/web/cross_extension_communication.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
@JS() | ||
library cross_extension_communication; | ||
|
||
import 'package:js/js.dart'; | ||
|
||
import 'chrome_api.dart'; | ||
import 'data_types.dart'; | ||
import 'debug_session.dart'; | ||
import 'logger.dart'; | ||
import 'storage.dart'; | ||
import 'web_api.dart'; | ||
|
||
// The only extension allowed to communicate with this extension is the | ||
// AngularDart DevTools extension. | ||
// | ||
// This ID is used to send messages to AngularDart DevTools, while the | ||
// externally_connectable field in the manifest.json allows AngularDart DevTools | ||
// to send messages to this extension. | ||
const _angularDartDevToolsId = 'nbkbficgbembimioedhceniahniffgpl'; | ||
|
||
// A set of events to forward to the AngularDart DevTools extension. | ||
final _eventsForAngularDartDevTools = { | ||
'Overlay.inspectNodeRequested', | ||
'dwds.encodedUri', | ||
}; | ||
|
||
void handleMessagesFromAngularDartDevTools( | ||
dynamic jsRequest, MessageSender sender, Function sendResponse) async { | ||
if (jsRequest == null) return; | ||
final message = jsRequest as ExternalExtensionMessage; | ||
if (message.name == 'chrome.debugger.sendCommand') { | ||
_forwardCommandToChromeDebugger(message, sendResponse); | ||
} else if (message.name == 'dwds.encodedUri') { | ||
_respondWithEncodedUri(message.tabId, sendResponse); | ||
} else if (message.name == 'dwds.startDebugging') { | ||
attachDebugger(message.tabId, trigger: Trigger.angularDartDevTools); | ||
sendResponse(true); | ||
} else { | ||
sendResponse( | ||
ErrorResponse()..error = 'Unknown message name: ${message.name}'); | ||
} | ||
} | ||
|
||
void maybeForwardMessageToAngularDartDevTools( | ||
{required String method, required dynamic params, required int tabId}) { | ||
if (!_eventsForAngularDartDevTools.contains(method)) return; | ||
|
||
final message = method.startsWith('dwds') | ||
? _dwdsEventMessage(method: method, params: params, tabId: tabId) | ||
: _debugEventMessage(method: method, params: params, tabId: tabId); | ||
|
||
_forwardMessageToAngularDartDevTools(message); | ||
} | ||
|
||
void _forwardCommandToChromeDebugger( | ||
ExternalExtensionMessage message, Function sendResponse) { | ||
try { | ||
final options = message.options as SendCommandOptions; | ||
chrome.debugger.sendCommand( | ||
Debuggee(tabId: message.tabId), | ||
options.method, | ||
options.commandParams, | ||
allowInterop( | ||
([result]) => _respondWithChromeResult(result, sendResponse)), | ||
); | ||
} catch (e) { | ||
sendResponse(ErrorResponse()..error = '$e'); | ||
} | ||
} | ||
|
||
void _respondWithChromeResult(Object? chromeResult, Function sendResponse) { | ||
// No result indicates that an error occurred. | ||
if (chromeResult == null) { | ||
sendResponse(ErrorResponse() | ||
..error = JSON.stringify( | ||
chrome.runtime.lastError ?? 'Unknown error.', | ||
)); | ||
} else { | ||
sendResponse(chromeResult); | ||
} | ||
} | ||
|
||
void _respondWithEncodedUri(int tabId, Function sendResponse) async { | ||
final encodedUri = await fetchStorageObject<EncodedUri>( | ||
type: StorageObject.encodedUri, tabId: tabId); | ||
sendResponse(encodedUri ?? ''); | ||
} | ||
|
||
void _forwardMessageToAngularDartDevTools(ExternalExtensionMessage message) { | ||
chrome.runtime.sendMessage( | ||
_angularDartDevToolsId, | ||
message, | ||
/* options */ null, | ||
allowInterop(([result]) => _checkForErrors(result, message.name)), | ||
); | ||
} | ||
|
||
void _checkForErrors(Object? chromeResult, String messageName) { | ||
// No result indicates that an error occurred. | ||
if (chromeResult == null) { | ||
final errorMessage = chrome.runtime.lastError?.message ?? 'Unknown error.'; | ||
debugWarn('Error forwarding $messageName: $errorMessage'); | ||
} | ||
} | ||
|
||
ExternalExtensionMessage _debugEventMessage({ | ||
required String method, | ||
required dynamic params, | ||
required int tabId, | ||
}) => | ||
ExternalExtensionMessage( | ||
name: 'chrome.debugger.event', | ||
tabId: tabId, | ||
options: DebugEvent(method: method, params: params), | ||
); | ||
|
||
ExternalExtensionMessage _dwdsEventMessage({ | ||
required String method, | ||
required dynamic params, | ||
required int tabId, | ||
}) => | ||
ExternalExtensionMessage( | ||
name: method, | ||
tabId: tabId, | ||
options: params, | ||
); | ||
|
||
// This message is used for cross-extension communication between this extension | ||
// and the AngularDart DevTools extension. | ||
@JS() | ||
@anonymous | ||
class ExternalExtensionMessage { | ||
external int get tabId; | ||
external String get name; | ||
external dynamic get options; | ||
external factory ExternalExtensionMessage( | ||
{required int tabId, required String name, required dynamic options}); | ||
} | ||
|
||
@JS() | ||
@anonymous | ||
class DebugEvent { | ||
external factory DebugEvent({String method, Object? params}); | ||
} | ||
|
||
@JS() | ||
@anonymous | ||
class SendCommandOptions { | ||
external String get method; | ||
external Object get commandParams; | ||
} | ||
|
||
@JS() | ||
@anonymous | ||
class ErrorResponse { | ||
external set error(String error); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a comment indicating that we only allow the ACX extension to communicate with this extension due to the manifest config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, added one here and in
cross_extension_communication