|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +@JS() |
| 6 | +library cider_connection; |
| 7 | + |
| 8 | +import 'dart:convert'; |
| 9 | + |
| 10 | +import 'package:dwds/data/debug_info.dart'; |
| 11 | +import 'package:js/js.dart'; |
| 12 | + |
| 13 | +import 'chrome_api.dart'; |
| 14 | +import 'debug_session.dart'; |
| 15 | +import 'logger.dart'; |
| 16 | +import 'storage.dart'; |
| 17 | + |
| 18 | +/// Defines the message types that can be passed to/from Cider. |
| 19 | +/// |
| 20 | +/// The types must match those defined by ChromeExtensionMessageType in the |
| 21 | +/// Cider extension. |
| 22 | +enum CiderMessageType { |
| 23 | + error, |
| 24 | + startDebugResponse, |
| 25 | + startDebugRequest, |
| 26 | + stopDebugResponse, |
| 27 | + stopDebugRequest, |
| 28 | +} |
| 29 | + |
| 30 | +/// Defines the error types that can be sent to Cider. |
| 31 | +/// |
| 32 | +/// The types must match those defined by ChromeExtensionErrorType in the |
| 33 | +/// Cider extension. |
| 34 | +enum CiderErrorType { |
| 35 | + chromeError, |
| 36 | + internalError, |
| 37 | + invalidRequest, |
| 38 | + multipleDartTabs, |
| 39 | + noDartTab, |
| 40 | + noWorkspace, |
| 41 | +} |
| 42 | + |
| 43 | +const _ciderPortName = 'cider'; |
| 44 | +Port? _ciderPort; |
| 45 | + |
| 46 | +/// Handles a connect request from Cider. |
| 47 | +/// |
| 48 | +/// The only site allowed to connect with this extension is Cider. The allowed |
| 49 | +/// URIs for Cider are set in the externally_connectable field in the manifest. |
| 50 | +void handleCiderConnectRequest(Port port) { |
| 51 | + if (port.name == _ciderPortName) { |
| 52 | + _ciderPort = port; |
| 53 | + |
| 54 | + port.onMessage.addListener( |
| 55 | + allowInterop(_handleMessageFromCider), |
| 56 | + ); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// Sends a message to the Cider-connected port. |
| 61 | +void sendMessageToCider({ |
| 62 | + required CiderMessageType messageType, |
| 63 | + String? messageBody, |
| 64 | +}) { |
| 65 | + if (_ciderPort == null) return; |
| 66 | + final message = jsonEncode({ |
| 67 | + 'messageType': messageType.name, |
| 68 | + 'messageBody': messageBody, |
| 69 | + }); |
| 70 | + _ciderPort!.postMessage(message); |
| 71 | +} |
| 72 | + |
| 73 | +/// Sends an error message to the Cider-connected port. |
| 74 | +void sendErrorMessageToCider({ |
| 75 | + required CiderErrorType errorType, |
| 76 | + String? errorDetails, |
| 77 | +}) { |
| 78 | + debugWarn('CiderError.${errorType.name} $errorDetails'); |
| 79 | + if (_ciderPort == null) return; |
| 80 | + final message = jsonEncode({ |
| 81 | + 'messageType': CiderMessageType.error.name, |
| 82 | + 'errorType': errorType.name, |
| 83 | + 'messageBody': errorDetails, |
| 84 | + }); |
| 85 | + _ciderPort!.postMessage(message); |
| 86 | +} |
| 87 | + |
| 88 | +Future<void> _handleMessageFromCider(dynamic message, Port _) async { |
| 89 | + if (message is! String) { |
| 90 | + sendErrorMessageToCider( |
| 91 | + errorType: CiderErrorType.invalidRequest, |
| 92 | + errorDetails: 'Expected request to be a string: $message', |
| 93 | + ); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + final decoded = jsonDecode(message) as Map<String, dynamic>; |
| 98 | + final messageType = decoded['messageType'] as String?; |
| 99 | + final messageBody = decoded['messageBody'] as String?; |
| 100 | + |
| 101 | + if (messageType == CiderMessageType.startDebugRequest.name) { |
| 102 | + await _startDebugging(workspaceName: messageBody); |
| 103 | + } else if (messageType == CiderMessageType.stopDebugRequest.name) { |
| 104 | + await _stopDebugging(workspaceName: messageBody); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +Future<void> _startDebugging({String? workspaceName}) async { |
| 109 | + if (workspaceName == null) { |
| 110 | + _sendNoWorkspaceError(); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + final dartTab = await _findDartTabIdForWorkspace(workspaceName); |
| 115 | + if (dartTab != null) { |
| 116 | + // TODO(https://github.com/dart-lang/webdev/issues/2198): When debugging |
| 117 | + // with Cider, disable debugging with DevTools. |
| 118 | + await attachDebugger(dartTab, trigger: Trigger.cider); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +Future<void> _stopDebugging({String? workspaceName}) async { |
| 123 | + if (workspaceName == null) { |
| 124 | + _sendNoWorkspaceError(); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + final dartTab = await _findDartTabIdForWorkspace(workspaceName); |
| 129 | + if (dartTab == null) return; |
| 130 | + final successfullyDetached = await detachDebugger( |
| 131 | + dartTab, |
| 132 | + type: TabType.dartApp, |
| 133 | + reason: DetachReason.canceledByUser, |
| 134 | + ); |
| 135 | + |
| 136 | + if (successfullyDetached) { |
| 137 | + sendMessageToCider(messageType: CiderMessageType.stopDebugResponse); |
| 138 | + } else { |
| 139 | + sendErrorMessageToCider( |
| 140 | + errorType: CiderErrorType.internalError, |
| 141 | + errorDetails: 'Unable to detach debugger.', |
| 142 | + ); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +void _sendNoWorkspaceError() { |
| 147 | + sendErrorMessageToCider( |
| 148 | + errorType: CiderErrorType.noWorkspace, |
| 149 | + errorDetails: 'Cannot find a debuggable Dart tab without a workspace', |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +Future<int?> _findDartTabIdForWorkspace(String workspaceName) async { |
| 154 | + final allTabsInfo = await fetchAllStorageObjectsOfType<DebugInfo>( |
| 155 | + type: StorageObject.debugInfo, |
| 156 | + ); |
| 157 | + final dartTabIds = allTabsInfo |
| 158 | + .where( |
| 159 | + (debugInfo) => debugInfo.workspaceName == workspaceName, |
| 160 | + ) |
| 161 | + .map( |
| 162 | + (info) => info.tabId, |
| 163 | + ) |
| 164 | + .toList(); |
| 165 | + |
| 166 | + if (dartTabIds.isEmpty) { |
| 167 | + sendErrorMessageToCider( |
| 168 | + errorType: CiderErrorType.noDartTab, |
| 169 | + errorDetails: 'No debuggable Dart tabs found.', |
| 170 | + ); |
| 171 | + return null; |
| 172 | + } |
| 173 | + if (dartTabIds.length > 1) { |
| 174 | + sendErrorMessageToCider( |
| 175 | + errorType: CiderErrorType.noDartTab, |
| 176 | + errorDetails: 'Too many debuggable Dart tabs found.', |
| 177 | + ); |
| 178 | + return null; |
| 179 | + } |
| 180 | + final tabId = dartTabIds.first; |
| 181 | + if (tabId == null) { |
| 182 | + sendErrorMessageToCider( |
| 183 | + errorType: CiderErrorType.chromeError, |
| 184 | + errorDetails: 'Debuggable Dart tab is null.', |
| 185 | + ); |
| 186 | + return null; |
| 187 | + } |
| 188 | + |
| 189 | + return tabId; |
| 190 | +} |
0 commit comments