|
4 | 4 |
|
5 | 5 | import 'dart:async'; |
6 | 6 |
|
| 7 | +import 'package:devtools_app_shared/service.dart'; |
7 | 8 | import 'package:devtools_app_shared/utils.dart'; |
8 | 9 | import 'package:devtools_shared/devtools_shared.dart'; |
9 | 10 | import 'package:dtd/dtd.dart'; |
10 | 11 | import 'package:flutter/foundation.dart'; |
11 | 12 | import 'package:json_rpc_2/json_rpc_2.dart'; |
| 13 | +import 'package:logging/logging.dart'; |
12 | 14 |
|
13 | 15 | import '../analytics/constants.dart'; |
14 | 16 | import '../framework/app_error_handling.dart'; |
15 | 17 | import 'api_classes.dart'; |
16 | 18 |
|
| 19 | +final _log = Logger('editor_client'); |
| 20 | + |
17 | 21 | /// A client wrapper that connects to an editor over DTD. |
18 | 22 | /// |
19 | 23 | /// Changes made to the editor services/events should be considered carefully to |
20 | 24 | /// ensure they are not breaking changes to already-shipped editors. |
21 | 25 | class EditorClient extends DisposableController |
22 | 26 | with AutoDisposeControllerMixin { |
23 | | - EditorClient(this._dtd) { |
| 27 | + EditorClient(this._dtdManager) { |
24 | 28 | unawaited(initialized); // Trigger async initialization. |
25 | 29 | } |
26 | 30 |
|
27 | | - final DartToolingDaemon _dtd; |
| 31 | + final DTDManager _dtdManager; |
28 | 32 | late final initialized = _initialize(); |
29 | 33 |
|
| 34 | + DartToolingDaemon get _dtd => _dtdManager.connection.value!; |
| 35 | + |
30 | 36 | String get gaId => EditorSidebar.id; |
31 | 37 |
|
32 | 38 | Future<void> _initialize() async { |
33 | 39 | autoDisposeStreamSubscription( |
34 | | - _dtd.onEvent(CoreDtdServiceConstants.servicesStreamId).listen((data) { |
35 | | - final kind = data.kind; |
36 | | - if (kind != CoreDtdServiceConstants.serviceRegisteredKind && |
37 | | - kind != CoreDtdServiceConstants.serviceUnregisteredKind) { |
38 | | - return; |
39 | | - } |
40 | | - |
| 40 | + _dtdManager.serviceRegistrationBroadcastStream.listen((data) { |
41 | 41 | final service = data.data[DtdParameters.service] as String?; |
42 | | - if (service == null || |
43 | | - (service != editorServiceName && service != lspServiceName)) { |
44 | | - return; |
45 | | - } |
46 | | - |
| 42 | + if (service == null) return; |
47 | 43 | final isRegistered = |
48 | | - kind == CoreDtdServiceConstants.serviceRegisteredKind; |
| 44 | + data.kind == CoreDtdServiceConstants.serviceRegisteredKind; |
49 | 45 | final method = data.data[DtdParameters.method] as String; |
50 | 46 | final capabilities = |
51 | 47 | data.data[DtdParameters.capabilities] as Map<String, Object?>?; |
52 | | - final lspMethod = LspMethod.fromMethodName(method); |
53 | | - if (lspMethod != null) { |
54 | | - lspMethod.isRegistered = isRegistered; |
55 | | - if (lspMethod == LspMethod.editableArguments) { |
56 | | - // Update the notifier so that the Property Editor is aware that the |
57 | | - // editableArguments API is registered. |
58 | | - _editableArgumentsApiIsRegistered.value = isRegistered; |
59 | | - } |
60 | | - } else if (method == EditorMethod.getDevices.name) { |
61 | | - _supportsGetDevices = isRegistered; |
62 | | - } else if (method == EditorMethod.getDebugSessions.name) { |
63 | | - _supportsGetDebugSessions = isRegistered; |
64 | | - } else if (method == EditorMethod.selectDevice.name) { |
65 | | - _supportsSelectDevice = isRegistered; |
66 | | - } else if (method == EditorMethod.hotReload.name) { |
67 | | - _supportsHotReload = isRegistered; |
68 | | - } else if (method == EditorMethod.hotRestart.name) { |
69 | | - _supportsHotRestart = isRegistered; |
70 | | - } else if (method == EditorMethod.openDevToolsPage.name) { |
71 | | - _supportsOpenDevToolsPage = isRegistered; |
72 | | - _supportsOpenDevToolsForceExternal = |
73 | | - capabilities?[Field.supportsForceExternal] == true; |
74 | | - } else { |
75 | | - return; |
76 | | - } |
77 | 48 |
|
78 | | - final info = isRegistered |
79 | | - ? ServiceRegistered( |
80 | | - service: service, |
81 | | - method: method, |
82 | | - capabilities: capabilities, |
83 | | - ) |
84 | | - : ServiceUnregistered(service: service, method: method); |
85 | | - _editorServiceChangedController.add(info); |
| 49 | + _handleServiceRegistration( |
| 50 | + service: service, |
| 51 | + method: method, |
| 52 | + capabilities: capabilities, |
| 53 | + isRegistered: isRegistered, |
| 54 | + ); |
86 | 55 | }), |
87 | 56 | ); |
88 | 57 |
|
@@ -126,15 +95,75 @@ class EditorClient extends DisposableController |
126 | 95 | } |
127 | 96 | }), |
128 | 97 | ); |
129 | | - await [ |
130 | | - _dtd.streamListen(CoreDtdServiceConstants.servicesStreamId), |
131 | | - _dtd.streamListen(editorStreamName).catchError((_) { |
132 | | - // Because we currently call streamListen in two places (here and |
133 | | - // ThemeManager) this can fail. It doesn't matter if this happens, |
134 | | - // however we should refactor this code to better support using the DTD |
135 | | - // connection in multiple places without them having to coordinate. |
136 | | - }), |
137 | | - ].wait; |
| 98 | + |
| 99 | + await _dtd.streamListen(editorStreamName).catchError((_) { |
| 100 | + // Because we currently call streamListen in two places (here and |
| 101 | + // ThemeManager) this can fail. It doesn't matter if this happens, |
| 102 | + // however we should refactor this code to better support using the DTD |
| 103 | + // connection in multiple places without them having to coordinate. |
| 104 | + }); |
| 105 | + |
| 106 | + // Check if any client services have already been registered against DTD. |
| 107 | + try { |
| 108 | + final response = await _dtd.getRegisteredServices(); |
| 109 | + for (final service in response.clientServices) { |
| 110 | + for (final method in service.methods.values) { |
| 111 | + _handleServiceRegistration( |
| 112 | + service: service.name, |
| 113 | + method: method.name, |
| 114 | + capabilities: method.capabilities, |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | + } catch (e) { |
| 119 | + _log.warning('Failed to fetch registered services: $e'); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + void _handleServiceRegistration({ |
| 124 | + required String service, |
| 125 | + required String method, |
| 126 | + Map<String, Object?>? capabilities, |
| 127 | + bool isRegistered = true, |
| 128 | + }) { |
| 129 | + if (service != editorServiceName && service != lspServiceName) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + final lspMethod = LspMethod.fromMethodName(method); |
| 134 | + if (lspMethod != null) { |
| 135 | + lspMethod.isRegistered = isRegistered; |
| 136 | + if (lspMethod == LspMethod.editableArguments) { |
| 137 | + // Update the notifier so that the Property Editor is aware that the |
| 138 | + // editableArguments API is registered. |
| 139 | + _editableArgumentsApiIsRegistered.value = isRegistered; |
| 140 | + } |
| 141 | + } else if (method == EditorMethod.getDevices.name) { |
| 142 | + _supportsGetDevices = isRegistered; |
| 143 | + } else if (method == EditorMethod.getDebugSessions.name) { |
| 144 | + _supportsGetDebugSessions = isRegistered; |
| 145 | + } else if (method == EditorMethod.selectDevice.name) { |
| 146 | + _supportsSelectDevice = isRegistered; |
| 147 | + } else if (method == EditorMethod.hotReload.name) { |
| 148 | + _supportsHotReload = isRegistered; |
| 149 | + } else if (method == EditorMethod.hotRestart.name) { |
| 150 | + _supportsHotRestart = isRegistered; |
| 151 | + } else if (method == EditorMethod.openDevToolsPage.name) { |
| 152 | + _supportsOpenDevToolsPage = isRegistered; |
| 153 | + _supportsOpenDevToolsForceExternal = |
| 154 | + capabilities?[Field.supportsForceExternal] == true; |
| 155 | + } else { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + final info = isRegistered |
| 160 | + ? ServiceRegistered( |
| 161 | + service: service, |
| 162 | + method: method, |
| 163 | + capabilities: capabilities, |
| 164 | + ) |
| 165 | + : ServiceUnregistered(service: service, method: method); |
| 166 | + _editorServiceChangedController.add(info); |
138 | 167 | } |
139 | 168 |
|
140 | 169 | /// Close the connection to DTD. |
|
0 commit comments