Skip to content

Commit

Permalink
Merge branch 'master' into issue-2287
Browse files Browse the repository at this point in the history
  • Loading branch information
elliette authored Jan 2, 2024
2 parents 7cf56fd + ebcd034 commit 8e4583c
Show file tree
Hide file tree
Showing 39 changed files with 1,686 additions and 1,067 deletions.
7 changes: 5 additions & 2 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## 23.0.0-wip
## 23.1.0-wip

## 23.0.0
- Restructure `LoadStrategy` to provide build settings. - [#2270](https://github.com/dart-lang/webdev/pull/2270)
- Add `FrontendServerLegacyStrategyProvider` and update bootstrap generation logic for `LegacyStrategy` - [#2285](https://github.com/dart-lang/webdev/pull/2285)
- Tolerate failures to detect a dart execution context. - [#2286](https://github.com/dart-lang/webdev/pull/2286)
- Tolerate failures to detect a Dart execution context. - [#2286](https://github.com/dart-lang/webdev/pull/2286)
- Fix a null cast error when debugging a `Class` from VS Code. - [#2303](https://github.com/dart-lang/webdev/pull/2303)

## 22.1.0
- Update `package:vm_service` constraint to `^13.0.0`. - [#2265](https://github.com/dart-lang/webdev/pull/2265)
Expand Down
4 changes: 3 additions & 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.38.0
version: 2.1.1
homepage: https://github.com/dart-lang/webdev
description: >-
A Chrome extension for Dart debugging.
Expand All @@ -14,12 +14,14 @@ dependencies:
js: ^0.6.1+1

dev_dependencies:
args: ^2.3.1
build: ^2.0.0
build_runner: ^2.4.0
built_collection: ^5.0.0
built_value_generator: ^8.3.0
build_web_compilers: ^4.0.4
dwds: ^16.0.0
path: ^1.8.1
sse: ^4.1.2
web_socket_channel: ^2.2.0

Expand Down
16 changes: 8 additions & 8 deletions dwds/debug_extension_mv3/tool/build_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

// Run from the extension root directory:
// - For dev: dart run tool/build_extension.dart
// - For prod: dart run tool/build_extension.dart prod
// - For MV3: dart run tool/build_extension.dart --mv3
// - For prod: dart run tool/build_extension.dart --prod
// - For MV2: dart run tool/build_extension.dart --mv2

import 'dart:convert';
import 'dart:io';
Expand All @@ -19,26 +19,26 @@ import 'package:args/args.dart';
import 'package:path/path.dart' as p;

const _prodFlag = 'prod';
const _mv3Flag = 'mv3';
const _mv2Flag = 'mv2';

void main(List<String> arguments) async {
final parser = ArgParser()
..addFlag(_prodFlag, negatable: true, defaultsTo: false)
..addFlag(_mv3Flag, negatable: true, defaultsTo: false);
..addFlag(_mv2Flag, negatable: true, defaultsTo: false);
final argResults = parser.parse(arguments);

exitCode = await run(
isProd: argResults[_prodFlag] as bool,
isMV3: argResults[_mv3Flag] as bool,
isMV2: argResults[_mv2Flag] as bool,
);
if (exitCode != 0) {
_logWarning('Run terminated unexpectedly with exit code: $exitCode');
}
}

Future<int> run({required bool isProd, required bool isMV3}) async {
Future<int> run({required bool isProd, required bool isMV2}) async {
_logInfo(
'Building ${isMV3 ? 'MV3' : 'MV2'} extension for ${isProd ? 'prod' : 'dev'}',
'Building ${isMV2 ? 'MV2' : 'MV3'} extension for ${isProd ? 'prod' : 'dev'}',
);
_logInfo('Compiling extension with dart2js to /compiled directory');
final compileStep = await Process.start(
Expand All @@ -50,7 +50,7 @@ Future<int> run({required bool isProd, required bool isMV3}) async {
if (compileExitCode != 0) {
return compileExitCode;
}
final manifestFileName = isMV3 ? 'manifest_mv3' : 'manifest_mv2';
final manifestFileName = isMV2 ? 'manifest_mv2' : 'manifest_mv3';
_logInfo('Copying manifest.json to /compiled directory');
try {
File(p.join('web', '$manifestFileName.json')).copySync(
Expand Down
50 changes: 32 additions & 18 deletions dwds/debug_extension_mv3/web/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ void _registerListeners() {

chrome.commands.onCommand
.addListener(allowInterop(_maybeSendCopyAppIdRequest));

// Detect clicks on the Dart Debug Extension icon.
onExtensionIconClicked(
allowInterop(
(Tab tab) => attachDebugger(
tab.id,
trigger: Trigger.extensionIcon,
),
),
);
}

Future<void> _handleRuntimeMessages(
Expand Down Expand Up @@ -140,6 +130,20 @@ Future<void> _handleRuntimeMessages(
},
);

interceptMessage<DebugStateChange>(
message: jsRequest,
expectedType: MessageType.debugStateChange,
expectedSender: Script.popup,
expectedRecipient: Script.background,
messageHandler: (DebugStateChange debugStateChange) {
final newState = debugStateChange.newState;
final tabId = debugStateChange.tabId;
if (newState == DebugStateChange.startDebugging) {
attachDebugger(tabId, trigger: Trigger.extensionIcon);
}
},
);

interceptMessage<String>(
message: jsRequest,
expectedType: MessageType.multipleAppsDetected,
Expand All @@ -158,7 +162,7 @@ Future<void> _handleRuntimeMessages(
value: multipleAppsDetected,
tabId: dartTab.id,
);
_setWarningIcon();
_setWarningIcon(dartTab.id);
},
);

Expand Down Expand Up @@ -186,7 +190,7 @@ Future<void> _detectNavigationAwayFromDartApp(
final debugInfo = await _fetchDebugInfo(navigationInfo.tabId);
if (debugInfo == null) return;
if (debugInfo.tabUrl != navigationInfo.url) {
_setDefaultIcon();
_setDefaultIcon(navigationInfo.tabId);
await clearStaleDebugSession(tabId);
await removeStorageObject(type: StorageObject.debugInfo, tabId: tabId);
await detachDebugger(
Expand Down Expand Up @@ -245,28 +249,38 @@ Future<bool> _maybeSendCopyAppIdRequest(String command, [Tab? tab]) async {
Future<void> _updateIcon(int activeTabId) async {
final debugInfo = await _fetchDebugInfo(activeTabId);
if (debugInfo == null) {
_setDefaultIcon();
_setDefaultIcon(activeTabId);
return;
}
final multipleApps = await fetchStorageObject<String>(
type: StorageObject.multipleAppsDetected,
tabId: activeTabId,
);
multipleApps == null ? _setDebuggableIcon() : _setWarningIcon();
multipleApps == null
? _setDebuggableIcon(activeTabId)
: _setWarningIcon(activeTabId);
}

void _setDebuggableIcon() {
void _setDebuggableIcon(int tabId) {
setExtensionIcon(IconInfo(path: 'static_assets/dart.png'));
setExtensionPopup(
PopupDetails(popup: 'static_assets/popup.html', tabId: tabId),
);
}

void _setWarningIcon() {
setExtensionIcon(IconInfo(path: 'static_assets/dart_warning.png'));
void _setWarningIcon(int tabId) {
setExtensionPopup(
PopupDetails(popup: 'static_assets/popup.html', tabId: tabId),
);
}

void _setDefaultIcon() {
void _setDefaultIcon(int tabId) {
final iconPath =
isDevMode ? 'static_assets/dart_dev.png' : 'static_assets/dart_grey.png';
setExtensionIcon(IconInfo(path: iconPath));
setExtensionPopup(
PopupDetails(popup: '', tabId: tabId),
);
}

Future<DebugInfo?> _fetchDebugInfo(int tabId) {
Expand Down
50 changes: 50 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 @@ -24,7 +26,10 @@ const _ciderDartMessageKey = 'CIDER_DART';
/// The types must match those defined by ChromeExtensionMessageType in the
/// Cider extension.
enum CiderMessageType {
connected,
error,
inspectorUrlResponse,
inspectorUrlRequest,
startDebugResponse,
startDebugRequest,
stopDebugResponse,
Expand All @@ -50,11 +55,14 @@ Port? _ciderPort;
/// URIs for Cider are set in the externally_connectable field in the manifest.
void handleCiderConnectRequest(Port port) {
if (port.name == _ciderPortName) {
debugLog('Received connect request from Cider', verbose: true);
_ciderPort = port;

port.onMessage.addListener(
allowInterop(_handleMessageFromCider),
);

sendMessageToCider(messageType: CiderMessageType.connected);
}
}

Expand Down Expand Up @@ -113,6 +121,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 All @@ -124,6 +134,7 @@ Future<void> _startDebugging({String? appId}) async {
final tabId = _tabId(appId);
// TODO(https://github.com/dart-lang/webdev/issues/2198): When debugging
// with Cider, disable debugging with DevTools.
debugLog('Attach debugger to Cider', verbose: true);
await attachDebugger(tabId, trigger: Trigger.cider);
}

Expand All @@ -150,6 +161,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
21 changes: 12 additions & 9 deletions dwds/debug_extension_mv3/web/debug_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import 'cider_connection.dart';
import 'cross_extension_communication.dart';
import 'data_serializers.dart';
import 'data_types.dart';
import 'lifeline_ports.dart';
import 'logger.dart';
import 'messaging.dart';
import 'storage.dart';
Expand Down Expand Up @@ -116,7 +115,7 @@ Future<void> attachDebugger(
forwardErrorsToCider: trigger == Trigger.cider,
);
if (!tabIsDebuggable) return;

debugLog('Attaching to tab $dartAppTabId', verbose: true);
_tabIdToTrigger[dartAppTabId] = trigger;
_registerDebugEventListeners();
chrome.debugger.attach(
Expand Down Expand Up @@ -358,6 +357,7 @@ Future<bool> _connectToDwds({
? WebSocketClient(WebSocketChannel.connect(uri))
: SseSocketClient(SseClient(uri.toString(), debugKey: 'DebugExtension'));
final trigger = _tabIdToTrigger[dartAppTabId];
debugLog('Connecting to DWDS...', verbose: true);
final debugSession = _DebugSession(
client: client,
appTabId: dartAppTabId,
Expand All @@ -381,8 +381,6 @@ Future<bool> _connectToDwds({
cancelOnError: true,
);
_debugSessions.add(debugSession);
// Create a connection with the lifeline port to keep the debug session alive:
await maybeCreateLifelinePort(dartAppTabId);
// Send a DevtoolsRequest to the event stream:
final tabUrl = await _getTabUrl(dartAppTabId);
debugSession.sendEvent(
Expand All @@ -409,11 +407,19 @@ 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);
}
}
if (message.method == 'dwds.debugUri') {
debugLog('Sending debug URI to Cider ${message.params}', verbose: true);
sendMessageToCider(
messageType: CiderMessageType.startDebugResponse,
messageBody: message.params,
Expand Down Expand Up @@ -592,10 +598,7 @@ void _removeDebugSession(_DebugSession debugSession) {
debugSession.sendEvent(event);
debugSession.close();
final removed = _debugSessions.remove(debugSession);
if (removed) {
// Maybe remove the corresponding lifeline connection:
maybeRemoveLifelinePort(debugSession.appTabId);
} else {
if (!removed) {
debugWarn('Could not remove debug session.');
}
}
Expand Down
27 changes: 0 additions & 27 deletions dwds/debug_extension_mv3/web/lifeline_connection.dart

This file was deleted.

Loading

0 comments on commit 8e4583c

Please sign in to comment.