Skip to content

Commit 0787d1e

Browse files
kenzieschmollCommit Queue
authored andcommitted
Use a more structured response type for ConnectedApp.getVmServices API
This CL: - changes the name of the `ConnectedApp.getVmServiceUris` service method to `ConnectedApp.getVmServices` since the response includes more than just the URI for each VM service object. - Adds a new response type `VmServicesResponse` - Enhances test coverage for existing response types Work towards #60540. Change-Id: Id4a1c05dde28c9b23975ad1d2ca68cb9e2bddb22 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/430545 Reviewed-by: Ben Konyi <[email protected]> Commit-Queue: Kenzie Davisson <[email protected]>
1 parent ebef427 commit 0787d1e

File tree

9 files changed

+332
-65
lines changed

9 files changed

+332
-65
lines changed

pkg/dtd/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
applications that DTD is aware of.
55
- Log exceptions from invalid `streamNotify` events.
66
- Added `getRegisteredServices` API.
7-
- Added a new response type `RegisteredServicesResponse`.
7+
- Added new response types `RegisteredServicesResponse` and
8+
`VmServicesResponse`.
89
- **Breaking Change**: Changed the `serviceName` parameter for the
910
`DartToolingDaemon.call` method to have type `String?` instead of `String.`
1011
- **Breaking Change**: When the `params` parameter for the

pkg/dtd/lib/src/constants.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension ConnectedAppServiceConstants on Never {
3939

4040
/// Service method name for the method that returns a list of VM service URIs
4141
/// for running applications in the context of a DTD instance.
42-
static const getVmServiceUris = 'getVmServiceUris';
42+
static const getVmServices = 'getVmServices';
4343

4444
/// Service method name for the method that registers a new VM service
4545
/// connection.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) 2025, 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+
part of 'response_types.dart';
6+
7+
/// A DTD response that contains information about a set of VM service
8+
/// connections.
9+
class VmServicesResponse {
10+
const VmServicesResponse({required this.vmServicesInfos});
11+
12+
factory VmServicesResponse.fromDTDResponse(DTDResponse response) {
13+
if (response.result[_kType] != type) {
14+
throw json_rpc.RpcException.invalidParams(
15+
'Expected $_kType param to be $type, got: ${response.result[_kType]}',
16+
);
17+
}
18+
return VmServicesResponse._fromDTDResponse(response);
19+
}
20+
21+
VmServicesResponse._fromDTDResponse(DTDResponse response)
22+
: vmServicesInfos = List<Map<String, Object?>>.from(
23+
(response.result[_kVmServices] as List).cast<Map<String, Object?>>(),
24+
).map(VmServiceInfo.fromJson).toList();
25+
26+
/// The key for the type parameter.
27+
static const String _kType = 'type';
28+
29+
/// The key for the VM services parameter.
30+
static const String _kVmServices = 'vmServices';
31+
32+
/// A list of VM services and their associated metadata.
33+
final List<VmServiceInfo> vmServicesInfos;
34+
35+
static String get type => 'VmServicesResponse';
36+
37+
Map<String, Object?> toJson() => <String, Object?>{
38+
_kType: type,
39+
_kVmServices: vmServicesInfos.map((info) => info.toJson()).toList(),
40+
};
41+
42+
@override
43+
String toString() => '[$type $_kVmServices: $vmServicesInfos]';
44+
}
45+
46+
/// Information about a VM service connection that is exposed via the DTD.
47+
class VmServiceInfo {
48+
const VmServiceInfo({
49+
required this.uri,
50+
this.exposedUri,
51+
this.name,
52+
});
53+
54+
/// Deserializes a [json] object to create a [VmServiceInfo] object.
55+
static VmServiceInfo fromJson(Map<String, Object?> json) => VmServiceInfo(
56+
uri: json[_kUri] as String,
57+
exposedUri: json[_kExposedUri] as String?,
58+
name: json[_kName] as String?,
59+
);
60+
61+
static const String _kUri = 'uri';
62+
static const String _kExposedUri = 'exposedUri';
63+
static const String _kName = 'name';
64+
65+
/// The URI for the VM service connection.
66+
final String uri;
67+
68+
/// The URI for the VM service connection that has been exposed to the
69+
/// user/client machine if the backend VM service is running in a different
70+
/// location (for example, an editor running in the user's browser with the
71+
/// backend on a remote server).
72+
///
73+
/// Code that runs on the user/client machine (such as DevTools and DevTools
74+
/// extensions) should prefer this URI (if provided) whereas code that also
75+
/// runs on the backend (such as the debug adapter) should always use [uri].
76+
///
77+
/// This value will be null or identical to [uri] in environments where
78+
/// there is no exposing to do (for example, an editor running locally on the
79+
/// same machine that the VM service is running).
80+
final String? exposedUri;
81+
82+
/// The human-readable name for this VM service connection as defined by tool
83+
/// or service that started it (e.g. 'Flutter - Pixel 5').
84+
///
85+
/// This is optional and may be null if the DTD client that registered the VM
86+
/// service did not provide a name.
87+
final String? name;
88+
89+
/// Serializes this [VmServiceInfo] object to JSON.
90+
Map<String, Object?> toJson() => <String, Object?>{
91+
_kUri: uri,
92+
if (exposedUri != null) _kExposedUri: exposedUri,
93+
if (name != null) _kName: name,
94+
};
95+
96+
@override
97+
String toString() {
98+
final sb = StringBuffer()..write(uri);
99+
if (exposedUri != null && exposedUri != uri) {
100+
sb.write(' (exposed: $exposedUri)');
101+
}
102+
if (name != null) {
103+
sb.write(' - $name');
104+
}
105+
return sb.toString();
106+
}
107+
}

pkg/dtd/lib/src/response_types/response_types.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc;
77

88
import '../dart_tooling_daemon.dart';
99

10+
part '_connected_app.dart';
1011
part '_file_system.dart';
1112

1213
/// A DTD response that indicates success.

pkg/dtd/lib/src/services/connected_app_service.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ extension ConnectedAppsExtension on DartToolingDaemon {
8888
);
8989
}
9090

91-
/// Returns a list of VM service URIs for running applications in the context
92-
/// of this DTD instance.
93-
Future<StringListResponse> getVmServiceUris() {
94-
return _callOnConnectedAppService<StringListResponse>(
95-
ConnectedAppServiceConstants.getVmServiceUris,
96-
parseResponse: StringListResponse.fromDTDResponse,
91+
/// Returns a response containing information about the set of VM service
92+
/// connections this DTD instance is aware of.
93+
Future<VmServicesResponse> getVmServices() {
94+
return _callOnConnectedAppService<VmServicesResponse>(
95+
ConnectedAppServiceConstants.getVmServices,
96+
parseResponse: VmServicesResponse.fromDTDResponse,
9797
);
9898
}
9999

pkg/dtd/test/connected_app_service_test.dart

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ void main() {
5555
expect(response.type, 'Success');
5656
expect(response.value, null);
5757
expect(
58-
(await client.getVmServiceUris()).value,
59-
[testApp.vmServiceUri],
58+
(await client.getVmServices())
59+
.vmServicesInfos
60+
.map((s) => s.toJson()),
61+
[
62+
{'uri': testApp.vmServiceUri},
63+
],
6064
);
6165

6266
response = await client.registerVmService(
@@ -66,8 +70,12 @@ void main() {
6670
expect(response.type, 'Success');
6771
expect(response.value, null);
6872
expect(
69-
(await client.getVmServiceUris()).value,
70-
[testApp.vmServiceUri],
73+
(await client.getVmServices())
74+
.vmServicesInfos
75+
.map((s) => s.toJson()),
76+
[
77+
{'uri': testApp.vmServiceUri},
78+
],
7179
);
7280
});
7381

@@ -100,8 +108,12 @@ void main() {
100108
expect(response.type, 'Success');
101109
expect(response.value, null);
102110
expect(
103-
(await client.getVmServiceUris()).value,
104-
[testApp.vmServiceUri],
111+
(await client.getVmServices())
112+
.vmServicesInfos
113+
.map((s) => s.toJson()),
114+
[
115+
{'uri': testApp.vmServiceUri},
116+
],
105117
);
106118

107119
response = await client.unregisterVmService(
@@ -110,7 +122,7 @@ void main() {
110122
);
111123
expect(response.type, 'Success');
112124
expect(response.value, null);
113-
expect((await client.getVmServiceUris()).value, <String>[]);
125+
expect((await client.getVmServices()).vmServicesInfos, isEmpty);
114126
});
115127

116128
test('succeeds for URI that is not in the registry', () async {
@@ -161,13 +173,14 @@ void main() {
161173
});
162174
});
163175

164-
test('getVmServiceUris', () async {
176+
test('getVmServices', () async {
165177
await startDtd();
166178

167179
final testApp1 = DartCliAppProcess();
168180
await testApp1.start();
169181
var response = await client.registerVmService(
170182
uri: testApp1.vmServiceUri,
183+
name: 'app 1',
171184
secret: dtdSecret!,
172185
);
173186
expect(response.type, 'Success');
@@ -177,13 +190,20 @@ void main() {
177190
await testApp2.start();
178191
response = await client.registerVmService(
179192
uri: testApp2.vmServiceUri,
193+
exposedUri: testApp2.vmServiceUri,
180194
secret: dtdSecret!,
181195
);
182196
expect(response.type, 'Success');
183197
expect(response.value, null);
184198

185-
final uris = await client.getVmServiceUris();
186-
expect(uris.value, [testApp1.vmServiceUri, testApp2.vmServiceUri]);
199+
final servicesResponse = await client.getVmServices();
200+
expect(
201+
servicesResponse.vmServicesInfos.map((s) => s.toJson()),
202+
[
203+
{'uri': testApp1.vmServiceUri, 'name': 'app 1'},
204+
{'uri': testApp2.vmServiceUri, 'exposedUri': testApp2.vmServiceUri},
205+
],
206+
);
187207
});
188208

189209
group('sends stream updates', () {

pkg/dtd/test/dart_tooling_daemon_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void main() {
153153
'getRegisteredServices',
154154
'ConnectedApp.registerVmService',
155155
'ConnectedApp.unregisterVmService',
156-
'ConnectedApp.getVmServiceUris',
156+
'ConnectedApp.getVmServices',
157157
'FileSystem.readFileAsString',
158158
'FileSystem.writeFileAsString',
159159
'FileSystem.listDirectoryContents',

0 commit comments

Comments
 (0)