Skip to content
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

Cider passes an app ID instead of a workspace name to connect to the Dart Debug Extension #2272

Merged
merged 5 commits into from
Oct 30, 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
87 changes: 22 additions & 65 deletions dwds/debug_extension_mv3/web/cider_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ library cider_connection;
import 'dart:convert';
import 'dart:js_util';

import 'package:dwds/data/debug_info.dart';
import 'package:js/js.dart';

import 'chrome_api.dart';
import 'debug_session.dart';
import 'logger.dart';
import 'storage.dart';

/// Used to identify messages passed to/from Cider.
///
Expand All @@ -38,12 +36,9 @@ enum CiderMessageType {
/// The types must match those defined by ChromeExtensionErrorType in the
/// Cider extension.
enum CiderErrorType {
chromeError,
internalError,
invalidRequest,
multipleDartTabs,
noDartTab,
noWorkspace,
noAppId,
}

const _ciderPortName = 'cider';
Expand Down Expand Up @@ -115,36 +110,32 @@ Future<void> _handleMessageFromCider(dynamic message, Port _) async {
final messageBody = decoded['messageBody'] as String?;

if (messageType == CiderMessageType.startDebugRequest.name) {
await _startDebugging(workspaceName: messageBody);
await _startDebugging(appId: messageBody);
} else if (messageType == CiderMessageType.stopDebugRequest.name) {
await _stopDebugging(workspaceName: messageBody);
await _stopDebugging(appId: messageBody);
}
}

Future<void> _startDebugging({String? workspaceName}) async {
if (workspaceName == null) {
_sendNoWorkspaceError();
Future<void> _startDebugging({String? appId}) async {
if (appId == null) {
_sendNoAppIdError();
return;
}

final dartTab = await _findDartTabIdForWorkspace(workspaceName);
if (dartTab != null) {
// TODO(https://github.com/dart-lang/webdev/issues/2198): When debugging
// with Cider, disable debugging with DevTools.
await attachDebugger(dartTab, trigger: Trigger.cider);
}
final tabId = _tabId(appId);
// TODO(https://github.com/dart-lang/webdev/issues/2198): When debugging
// with Cider, disable debugging with DevTools.
await attachDebugger(tabId, trigger: Trigger.cider);
}

Future<void> _stopDebugging({String? workspaceName}) async {
if (workspaceName == null) {
_sendNoWorkspaceError();
Future<void> _stopDebugging({String? appId}) async {
if (appId == null) {
_sendNoAppIdError();
return;
}
final tabId = _tabId(appId);

final dartTab = await _findDartTabIdForWorkspace(workspaceName);
if (dartTab == null) return;
final successfullyDetached = await detachDebugger(
dartTab,
tabId,
type: TabType.dartApp,
reason: DetachReason.canceledByUser,
);
Expand All @@ -159,48 +150,14 @@ Future<void> _stopDebugging({String? workspaceName}) async {
}
}

void _sendNoWorkspaceError() {
sendErrorMessageToCider(
errorType: CiderErrorType.noWorkspace,
errorDetails: 'Cannot find a debuggable Dart tab without a workspace',
);
int _tabId(String appId) {
final tabId = appId.split('-').last;
return int.parse(tabId);
}

Future<int?> _findDartTabIdForWorkspace(String workspaceName) async {
final allTabsInfo = await fetchAllStorageObjectsOfType<DebugInfo>(
type: StorageObject.debugInfo,
void _sendNoAppIdError() {
sendErrorMessageToCider(
errorType: CiderErrorType.noAppId,
errorDetails: 'Cannot find a debuggable Dart tab without an app ID',
);
final dartTabIds = allTabsInfo
.where(
(debugInfo) => debugInfo.workspaceName == workspaceName,
)
.map(
(info) => info.tabId,
)
.toList();

if (dartTabIds.isEmpty) {
sendErrorMessageToCider(
errorType: CiderErrorType.noDartTab,
errorDetails: 'No debuggable Dart tabs found.',
);
return null;
}
if (dartTabIds.length > 1) {
sendErrorMessageToCider(
errorType: CiderErrorType.multipleDartTabs,
errorDetails: 'Too many debuggable Dart tabs found.',
);
return null;
}
final tabId = dartTabIds.first;
if (tabId == null) {
sendErrorMessageToCider(
errorType: CiderErrorType.chromeError,
errorDetails: 'Debuggable Dart tab is null.',
);
return null;
}

return tabId;
}
36 changes: 31 additions & 5 deletions dwds/debug_extension_mv3/web/debug_session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ Future<void> attachDebugger(
required Trigger trigger,
}) async {
// Validate that the tab can be debugged:
final tabIsDebuggable = await _validateTabIsDebuggable(dartAppTabId);
final tabIsDebuggable = await _validateTabIsDebuggable(
dartAppTabId,
forwardErrorsToCider: trigger == Trigger.cider,
);
if (!tabIsDebuggable) return;

_tabIdToTrigger[dartAppTabId] = trigger;
Expand Down Expand Up @@ -168,12 +171,16 @@ Future<void> clearStaleDebugSession(int tabId) async {
}
}

Future<bool> _validateTabIsDebuggable(int dartAppTabId) async {
Future<bool> _validateTabIsDebuggable(
int dartAppTabId, {
bool forwardErrorsToCider = false,
}) async {
// Check if a debugger is already attached:
final existingDebuggerLocation = _debuggerLocation(dartAppTabId);
if (existingDebuggerLocation != null) {
await _showWarningNotification(
await _showWarning(
'Already debugging in ${existingDebuggerLocation.displayName}.',
forwardToCider: forwardErrorsToCider,
);
return false;
}
Expand All @@ -183,7 +190,10 @@ Future<bool> _validateTabIsDebuggable(int dartAppTabId) async {
tabId: dartAppTabId,
);
if (debugInfo == null) {
await _showWarningNotification('Not a Dart app.');
await _showWarning(
'Not a Dart app.',
forwardToCider: forwardErrorsToCider,
);
return false;
}
// Determine if there are multiple apps in the tab:
Expand All @@ -192,8 +202,9 @@ Future<bool> _validateTabIsDebuggable(int dartAppTabId) async {
tabId: dartAppTabId,
);
if (multipleApps != null) {
await _showWarningNotification(
await _showWarning(
'Dart debugging is not supported in a multi-app environment.',
forwardToCider: forwardErrorsToCider,
);
return false;
}
Expand Down Expand Up @@ -692,6 +703,21 @@ Future<bool> _sendAuthRequest(String authUrl) async {
return responseBody.contains('Dart Debug Authentication Success!');
}

Future<bool> _showWarning(
String message, {
bool forwardToCider = false,
}) {
if (forwardToCider) {
sendErrorMessageToCider(
errorType: CiderErrorType.invalidRequest,
errorDetails: message,
);
return Future.value(true);
} else {
return _showWarningNotification(message);
}
}

Future<bool> _showWarningNotification(String message) {
final completer = Completer<bool>();
chrome.notifications.create(
Expand Down
Loading