|
| 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 | +import 'package:json_rpc_2/json_rpc_2.dart'; |
| 6 | + |
| 7 | +import 'constants.dart'; |
| 8 | +import 'dart_tooling_daemon.dart'; |
| 9 | +import 'response_types.dart'; |
| 10 | +import 'rpc_error_codes.dart'; |
| 11 | + |
| 12 | +/// Extension methods on the [DartToolingDaemon] that call the ConnectedApps |
| 13 | +/// service. |
| 14 | +extension ConnectedAppsExtension on DartToolingDaemon { |
| 15 | + /// Registers a new VM service connection at [uri]. |
| 16 | + /// |
| 17 | + /// This is a privileged RPC that requires a [secret] to be called |
| 18 | + /// successfully. This [secret] is generated by DTD at startup and provided to |
| 19 | + /// the spawner of DTD only so that it is the only DTD client that can call |
| 20 | + /// protected methods. If [secret] is invalid, an [RpcException] with error |
| 21 | + /// code [RpcErrorCodes.kPermissionDenied] will be thrown. |
| 22 | + /// |
| 23 | + /// [exposedUri] is the URI for the VM service connection that has been |
| 24 | + /// exposed to the user/client machine if the backend VM service is running in |
| 25 | + /// a different location (for example, an editor running in the user's browser |
| 26 | + /// with the backend on a remote server). |
| 27 | + /// |
| 28 | + /// Code that runs on the user/client machine (such as DevTools and DevTools |
| 29 | + /// extensions) should prefer this URI (if provided) whereas code that also |
| 30 | + /// runs on the backend (such as the debug adapter) should always use [uri]. |
| 31 | + /// |
| 32 | + /// [exposedUri] will be null or identical to [uri] in environments where |
| 33 | + /// there is no exposing to do (for example, an editor running locally on the |
| 34 | + /// same machine that the VM service is running). |
| 35 | + /// |
| 36 | + /// [name] is the human-readable name for this VM service connection as |
| 37 | + /// defined by the DTD client calling this method (e.g. 'Flutter - Pixel 5'). |
| 38 | + /// This is optional and may be null if the DTD client registering the VM |
| 39 | + /// service does not have any useful naming information to provide. |
| 40 | + /// |
| 41 | + /// If a VM service connection cannot be established for [uri], an |
| 42 | + /// [RpcException] with error code [RpcErrorCodes.kConnectionFailed] will be |
| 43 | + /// thrown. |
| 44 | + /// |
| 45 | + /// When this method is called, a |
| 46 | + /// [ConnectedAppServiceConstants.vmServiceRegistered] event will be sent over |
| 47 | + /// the [ConnectedAppServiceConstants.serviceName] stream. |
| 48 | + Future<Success> registerVmService({ |
| 49 | + required String uri, |
| 50 | + required String secret, |
| 51 | + String? exposedUri, |
| 52 | + String? name, |
| 53 | + }) { |
| 54 | + return _callOnConnectedAppService<Success>( |
| 55 | + ConnectedAppServiceConstants.registerVmService, |
| 56 | + params: { |
| 57 | + EventParameters.uri: uri, |
| 58 | + EventParameters.secret: secret, |
| 59 | + if (exposedUri != null) EventParameters.exposedUri: exposedUri, |
| 60 | + if (name != null) EventParameters.name: name, |
| 61 | + }, |
| 62 | + parseResponse: Success.fromDTDResponse, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /// Unregisters the VM service connection at [uri], if it exists. |
| 67 | + /// |
| 68 | + /// If the VM service at [uri] was not already registered, this method does |
| 69 | + /// nothing. |
| 70 | + /// |
| 71 | + /// This is a privileged RPC that requires a [secret] to be called |
| 72 | + /// successfully. This [secret] is generated by DTD at startup and provided to |
| 73 | + /// the spawner of DTD only so that it is the only DTD client that can call |
| 74 | + /// protected methods. If [secret] is invalid, an [RpcException] with error |
| 75 | + /// code [RpcErrorCodes.kPermissionDenied] will be thrown. |
| 76 | + /// |
| 77 | + /// When this method is called, a |
| 78 | + /// [ConnectedAppServiceConstants.vmServiceUnregistered] event will be sent |
| 79 | + /// over the [ConnectedAppServiceConstants.serviceName] stream. |
| 80 | + Future<Success> unregisterVmService({ |
| 81 | + required String uri, |
| 82 | + required String secret, |
| 83 | + }) { |
| 84 | + return _callOnConnectedAppService<Success>( |
| 85 | + ConnectedAppServiceConstants.unregisterVmService, |
| 86 | + params: {EventParameters.uri: uri, EventParameters.secret: secret}, |
| 87 | + parseResponse: Success.fromDTDResponse, |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 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, |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /// The stream of VM service update events. |
| 101 | + /// |
| 102 | + /// Events sent over this stream will be of kind 'VmServiceRegistered' |
| 103 | + /// or 'VmServiceUnregistered' and will contain the VM service URI they are |
| 104 | + /// associated with. |
| 105 | + /// |
| 106 | + /// A listener added to this [Stream] should be immediately followed by a |
| 107 | + /// call to `streamListen(kConnectedAppServiceName)`, which will allow the |
| 108 | + /// listener to start receiving events on this stream. Any events received |
| 109 | + /// before the listener is added and `streamListen` is called will be dropeed. |
| 110 | + Stream<DTDEvent> onVmServiceUpdate() { |
| 111 | + return onEvent(ConnectedAppServiceConstants.serviceName); |
| 112 | + } |
| 113 | + |
| 114 | + Future<T> _callOnConnectedAppService<T>( |
| 115 | + String methodName, { |
| 116 | + Map<String, Object> params = const {}, |
| 117 | + required T Function(DTDResponse) parseResponse, |
| 118 | + }) async { |
| 119 | + final response = await call( |
| 120 | + ConnectedAppServiceConstants.serviceName, |
| 121 | + methodName, |
| 122 | + params: params, |
| 123 | + ); |
| 124 | + return parseResponse(response); |
| 125 | + } |
| 126 | +} |
0 commit comments