|
| 1 | +// Copyright 2023 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc_2; |
| 8 | +import 'package:stream_channel/stream_channel.dart'; |
| 9 | +import 'package:web_socket_channel/web_socket_channel.dart'; |
| 10 | + |
| 11 | +import '../../shared/config_specific/post_message/post_message.dart'; |
| 12 | + |
| 13 | +/// An API for interacting with Dart tooling. |
| 14 | +class DartApi { |
| 15 | + DartApi.rpc(this._rpc) : vsCode = VsCodeApi(_rpc) { |
| 16 | + unawaited(_rpc.listen()); |
| 17 | + } |
| 18 | + |
| 19 | + /// Connects the API using 'postMessage'. This is only available when running |
| 20 | + /// on web and embedded inside VS Code. |
| 21 | + factory DartApi.postMessage() { |
| 22 | + final postMessageController = StreamController(); |
| 23 | + postMessageController.stream.listen((message) => postMessage(message, '*')); |
| 24 | + final channel = StreamChannel( |
| 25 | + onPostMessage.map((event) => event.data), |
| 26 | + postMessageController, |
| 27 | + ); |
| 28 | + return DartApi.rpc(json_rpc_2.Peer.withoutJson(channel)); |
| 29 | + } |
| 30 | + |
| 31 | + /// Connects the API over the provided WebSocket. |
| 32 | + factory DartApi.webSocket(WebSocketChannel socket) { |
| 33 | + return DartApi.rpc(json_rpc_2.Peer(socket.cast<String>())); |
| 34 | + } |
| 35 | + |
| 36 | + final json_rpc_2.Peer _rpc; |
| 37 | + |
| 38 | + /// Access to APIs related to VS Code, such as executing VS Code commands or |
| 39 | + /// interacting with the Dart/Flutter extensions. |
| 40 | + final VsCodeApi vsCode; |
| 41 | + |
| 42 | + void dispose() { |
| 43 | + unawaited(_rpc.close()); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Base class for the different APIs that may be available. |
| 48 | +abstract base class ToolApi { |
| 49 | + ToolApi(this.rpc); |
| 50 | + |
| 51 | + final json_rpc_2.Peer rpc; |
| 52 | + |
| 53 | + String get apiName; |
| 54 | + |
| 55 | + /// Checks whether this API is available. |
| 56 | + /// |
| 57 | + /// Calls to any other API should only be made if and when this [Future] |
| 58 | + /// completes with `true`. |
| 59 | + late final Future<bool> isAvailable = |
| 60 | + _sendRequest<bool>('checkAvailable').catchError((_) => false); |
| 61 | + |
| 62 | + Future<T> _sendRequest<T>(String method, [Object? parameters]) async { |
| 63 | + return (await rpc.sendRequest('$apiName.$method', parameters)) as T; |
| 64 | + } |
| 65 | + |
| 66 | + /// Listens for an event '[apiName].[name]' that has a Map for parameters. |
| 67 | + Stream<Map<String, Object?>> events(String name) { |
| 68 | + final streamController = StreamController<Map<String, Object?>>.broadcast(); |
| 69 | + rpc.registerMethod('$apiName.$name', (json_rpc_2.Parameters parameters) { |
| 70 | + streamController.add(parameters.asMap.cast<String, Object?>()); |
| 71 | + }); |
| 72 | + return streamController.stream; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +final class VsCodeApi extends ToolApi { |
| 77 | + // TODO(dantup): Consider code-generation because Dart-Code and DevTools will |
| 78 | + // both have implementations of this API (although in Dart + TypeScript). |
| 79 | + VsCodeApi(super.rpc); |
| 80 | + |
| 81 | + @override |
| 82 | + final apiName = 'vsCode'; |
| 83 | + |
| 84 | + late final Stream<VsCodeDevicesEvent> devicesChanged = |
| 85 | + events('devicesChanged').map(VsCodeDevicesEvent.fromJson); |
| 86 | + |
| 87 | + Future<Object?> executeCommand(String command, [List<Object?>? arguments]) { |
| 88 | + return _sendRequest( |
| 89 | + 'executeCommand', |
| 90 | + {'command': command, 'arguments': arguments}, |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + Future<void> selectDevice(String id) { |
| 95 | + return _sendRequest( |
| 96 | + 'selectDevice', |
| 97 | + {'id': id}, |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +class VsCodeDevice { |
| 103 | + VsCodeDevice({required this.id}); |
| 104 | + |
| 105 | + VsCodeDevice.fromJson(Map<String, Object?> json) |
| 106 | + : this(id: json['id'] as String); |
| 107 | + |
| 108 | + final String id; |
| 109 | + |
| 110 | + Map<String, Object?> toJson() => {'id': id}; |
| 111 | +} |
| 112 | + |
| 113 | +class VsCodeDevicesEvent { |
| 114 | + VsCodeDevicesEvent({required this.selectedDeviceId, required this.devices}); |
| 115 | + |
| 116 | + VsCodeDevicesEvent.fromJson(Map<String, Object?> json) |
| 117 | + : this( |
| 118 | + selectedDeviceId: json['selectedDeviceId'] as String?, |
| 119 | + devices: (json['devices'] as List) |
| 120 | + .cast<Map<String, Object?>>() |
| 121 | + .map(VsCodeDevice.fromJson) |
| 122 | + .toList(), |
| 123 | + ); |
| 124 | + |
| 125 | + final String? selectedDeviceId; |
| 126 | + final List<VsCodeDevice> devices; |
| 127 | + |
| 128 | + Map<String, Object?> toJson() => { |
| 129 | + 'selectedDeviceId': selectedDeviceId, |
| 130 | + 'devices': devices.map((device) => device.toJson()).toList(), |
| 131 | + }; |
| 132 | +} |
0 commit comments