Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 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'; |
Zachary Anderson | a036980 | 2017-04-14 21:18:48 -0700 | [diff] [blame] | 6 | import 'dart:math' as math; |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 7 | |
John McCutchan | 24ab837 | 2016-09-15 13:18:32 -0700 | [diff] [blame] | 8 | import 'package:json_rpc_2/error_code.dart' as rpc_error_code; |
Devon Carew | 6737712 | 2017-01-12 14:03:04 -0800 | [diff] [blame] | 9 | import 'package:json_rpc_2/json_rpc_2.dart' as rpc; |
Alexandre Ardhuin | 2de61a0 | 2017-03-31 18:34:13 +0200 | [diff] [blame] | 10 | import 'package:meta/meta.dart' show required; |
Jason Simmons | 37d7875 | 2016-12-09 11:38:38 -0800 | [diff] [blame] | 11 | import 'package:stream_channel/stream_channel.dart'; |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 12 | import 'package:web_socket_channel/io.dart'; |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 13 | import 'package:web_socket_channel/web_socket_channel.dart'; |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 14 | |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 15 | import 'base/common.dart'; |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 16 | import 'base/context.dart'; |
Yegor | 85473d0 | 2018-04-19 18:29:49 -0700 | [diff] [blame] | 17 | import 'base/io.dart' as io; |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 18 | import 'base/utils.dart'; |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 19 | import 'convert.dart' show base64, utf8; |
Jonah Williams | 831163f | 2019-11-26 14:49:56 -0800 | [diff] [blame] | 20 | import 'device.dart'; |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 21 | import 'globals.dart' as globals; |
Kenzie Schmoll | d50d9c5 | 2019-09-05 09:50:36 -0700 | [diff] [blame] | 22 | import 'version.dart'; |
Ian Hickson | d7fb51a | 2016-08-02 16:52:57 -0700 | [diff] [blame] | 23 | |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 24 | /// Override `WebSocketConnector` in [context] to use a different constructor |
| 25 | /// for [WebSocket]s (used by tests). |
Jonah Williams | 301eaa8 | 2019-04-15 08:59:28 -0700 | [diff] [blame] | 26 | typedef WebSocketConnector = Future<io.WebSocket> Function(String url, {io.CompressionOptions compression}); |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 27 | |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 28 | /// A function that opens a two-way communication channel to the specified [uri]. |
Jonah Williams | 301eaa8 | 2019-04-15 08:59:28 -0700 | [diff] [blame] | 29 | typedef _OpenChannel = Future<StreamChannel<String>> Function(Uri uri, {io.CompressionOptions compression}); |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 30 | |
Todd Volkert | a68c979 | 2017-02-23 10:05:00 -0800 | [diff] [blame] | 31 | _OpenChannel _openChannel = _defaultOpenChannel; |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 32 | |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 33 | /// A function that reacts to the invocation of the 'reloadSources' service. |
| 34 | /// |
| 35 | /// The VM Service Protocol allows clients to register custom services that |
| 36 | /// can be invoked by other clients through the service protocol itself. |
| 37 | /// |
| 38 | /// Clients like Observatory use external 'reloadSources' services, |
| 39 | /// when available, instead of the VM internal one. This allows these clients to |
| 40 | /// invoke Flutter HotReload when connected to a Flutter Application started in |
| 41 | /// hot mode. |
| 42 | /// |
| 43 | /// See: https://github.com/dart-lang/sdk/issues/30023 |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 44 | typedef ReloadSources = Future<void> Function( |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 45 | String isolateId, { |
| 46 | bool force, |
| 47 | bool pause, |
| 48 | }); |
| 49 | |
Kenzie Schmoll | 33bfa6a | 2019-01-17 08:28:54 -0800 | [diff] [blame] | 50 | typedef Restart = Future<void> Function({ bool pause }); |
| 51 | |
Alexandre Ardhuin | a07d371 | 2018-09-14 21:06:19 +0200 | [diff] [blame] | 52 | typedef CompileExpression = Future<String> Function( |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 53 | String isolateId, |
| 54 | String expression, |
| 55 | List<String> definitions, |
| 56 | List<String> typeDefinitions, |
| 57 | String libraryUri, |
| 58 | String klass, |
| 59 | bool isStatic, |
| 60 | ); |
| 61 | |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 62 | typedef ReloadMethod = Future<void> Function({ |
| 63 | String classId, |
| 64 | String libraryId, |
| 65 | }); |
| 66 | |
Jonah Williams | 301eaa8 | 2019-04-15 08:59:28 -0700 | [diff] [blame] | 67 | Future<StreamChannel<String>> _defaultOpenChannel(Uri uri, {io.CompressionOptions compression = io.CompressionOptions.compressionDefault}) async { |
Yegor | 85473d0 | 2018-04-19 18:29:49 -0700 | [diff] [blame] | 68 | Duration delay = const Duration(milliseconds: 100); |
| 69 | int attempts = 0; |
| 70 | io.WebSocket socket; |
Yegor | ee735c4 | 2018-04-20 14:45:50 -0700 | [diff] [blame] | 71 | |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 72 | Future<void> handleError(dynamic e) async { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 73 | globals.printTrace('Exception attempting to connect to Observatory: $e'); |
| 74 | globals.printTrace('This was attempt #$attempts. Will retry in $delay.'); |
Yegor | ee735c4 | 2018-04-20 14:45:50 -0700 | [diff] [blame] | 75 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 76 | if (attempts == 10) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 77 | globals.printStatus('This is taking longer than expected...'); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 78 | } |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 79 | |
Yegor | ee735c4 | 2018-04-20 14:45:50 -0700 | [diff] [blame] | 80 | // Delay next attempt. |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 81 | await Future<void>.delayed(delay); |
Yegor | ee735c4 | 2018-04-20 14:45:50 -0700 | [diff] [blame] | 82 | |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 83 | // Back off exponentially, up to 1600ms per attempt. |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 84 | if (delay < const Duration(seconds: 1)) { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 85 | delay *= 2; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 86 | } |
Yegor | ee735c4 | 2018-04-20 14:45:50 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Jonah Williams | 0acd3e6 | 2019-04-25 15:51:08 -0700 | [diff] [blame] | 89 | final WebSocketConnector constructor = context.get<WebSocketConnector>() ?? io.WebSocket.connect; |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 90 | while (socket == null) { |
Yegor | 85473d0 | 2018-04-19 18:29:49 -0700 | [diff] [blame] | 91 | attempts += 1; |
| 92 | try { |
Jonah Williams | 301eaa8 | 2019-04-15 08:59:28 -0700 | [diff] [blame] | 93 | socket = await constructor(uri.toString(), compression: compression); |
Yegor | ee735c4 | 2018-04-20 14:45:50 -0700 | [diff] [blame] | 94 | } on io.WebSocketException catch (e) { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 95 | await handleError(e); |
Yegor | ee735c4 | 2018-04-20 14:45:50 -0700 | [diff] [blame] | 96 | } on io.SocketException catch (e) { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 97 | await handleError(e); |
Yegor | 85473d0 | 2018-04-19 18:29:49 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 100 | return IOWebSocketChannel(socket).cast<String>(); |
Yegor | 85473d0 | 2018-04-19 18:29:49 -0700 | [diff] [blame] | 101 | } |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 102 | |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 103 | /// Override `VMServiceConnector` in [context] to return a different VMService |
| 104 | /// from [VMService.connect] (used by tests). |
Jonah Williams | 831163f | 2019-11-26 14:49:56 -0800 | [diff] [blame] | 105 | typedef VMServiceConnector = Future<VMService> Function(Uri httpUri, { |
| 106 | ReloadSources reloadSources, |
| 107 | Restart restart, |
| 108 | CompileExpression compileExpression, |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 109 | ReloadMethod reloadMethod, |
Jonah Williams | 831163f | 2019-11-26 14:49:56 -0800 | [diff] [blame] | 110 | io.CompressionOptions compression, |
| 111 | Device device, |
| 112 | }); |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 113 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 114 | /// A connection to the Dart VM Service. |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 115 | // TODO(mklim): Test this, https://github.com/flutter/flutter/issues/23031 |
John McCutchan | cab7c8d | 2016-08-11 13:14:13 -0700 | [diff] [blame] | 116 | class VMService { |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 117 | VMService( |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 118 | this._peer, |
| 119 | this.httpAddress, |
| 120 | this.wsAddress, |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 121 | ReloadSources reloadSources, |
Kenzie Schmoll | 33bfa6a | 2019-01-17 08:28:54 -0800 | [diff] [blame] | 122 | Restart restart, |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 123 | CompileExpression compileExpression, |
Jonah Williams | 831163f | 2019-11-26 14:49:56 -0800 | [diff] [blame] | 124 | Device device, |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 125 | ReloadMethod reloadMethod, |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 126 | ) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 127 | _vm = VM._empty(this); |
Alexandre Ardhuin | 2166ea5 | 2017-03-15 23:09:58 +0100 | [diff] [blame] | 128 | _peer.listen().catchError(_connectionError.completeError); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 129 | |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 130 | _peer.registerMethod('streamNotify', (rpc.Parameters event) { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 131 | _handleStreamNotify(event.asMap.cast<String, dynamic>()); |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 132 | }); |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 133 | |
| 134 | if (reloadSources != null) { |
| 135 | _peer.registerMethod('reloadSources', (rpc.Parameters params) async { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 136 | final String isolateId = params['isolateId'].value as String; |
| 137 | final bool force = params.asMap['force'] as bool ?? false; |
| 138 | final bool pause = params.asMap['pause'] as bool ?? false; |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 139 | |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 140 | if (isolateId.isEmpty) { |
Alexandre Ardhuin | f15c887 | 2020-02-11 20:58:27 +0100 | [diff] [blame] | 141 | throw rpc.RpcException.invalidParams("Invalid 'isolateId': $isolateId"); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 142 | } |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 143 | try { |
| 144 | await reloadSources(isolateId, force: force, pause: pause); |
| 145 | return <String, String>{'type': 'Success'}; |
| 146 | } on rpc.RpcException { |
| 147 | rethrow; |
Zachary Anderson | 9de7787 | 2020-02-27 22:46:23 -0800 | [diff] [blame^] | 148 | } on Exception catch (e, st) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 149 | throw rpc.RpcException(rpc_error_code.SERVER_ERROR, |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 150 | 'Error during Sources Reload: $e\n$st'); |
| 151 | } |
| 152 | }); |
| 153 | |
Ben Konyi | 8d81c30 | 2019-07-08 15:09:17 -0700 | [diff] [blame] | 154 | _peer.sendNotification('registerService', <String, String>{ |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 155 | 'service': 'reloadSources', |
Kenzie Schmoll | 33bfa6a | 2019-01-17 08:28:54 -0800 | [diff] [blame] | 156 | 'alias': 'Flutter Tools', |
| 157 | }); |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 158 | |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | if (reloadMethod != null) { |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 162 | // Register a special method for hot UI. while this is implemented |
| 163 | // currently in the same way as hot reload, it leaves the tool free |
| 164 | // to change to a more efficient implementation in the future. |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 165 | // |
| 166 | // `library` should be the file URI of the updated code. |
| 167 | // `class` should be the name of the Widget subclass to be marked dirty. For example, |
| 168 | // if the build method of a StatelessWidget is updated, this is the name of class. |
| 169 | // If the build method of a StatefulWidget is updated, then this is the name |
| 170 | // of the Widget class that created the State object. |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 171 | _peer.registerMethod('reloadMethod', (rpc.Parameters params) async { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 172 | final String libraryId = params['library'].value as String; |
| 173 | final String classId = params['class'].value as String; |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 174 | |
| 175 | if (libraryId.isEmpty) { |
Alexandre Ardhuin | f15c887 | 2020-02-11 20:58:27 +0100 | [diff] [blame] | 176 | throw rpc.RpcException.invalidParams("Invalid 'libraryId': $libraryId"); |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 177 | } |
| 178 | if (classId.isEmpty) { |
Alexandre Ardhuin | f15c887 | 2020-02-11 20:58:27 +0100 | [diff] [blame] | 179 | throw rpc.RpcException.invalidParams("Invalid 'classId': $classId"); |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 180 | } |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 181 | |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 182 | globals.printTrace('reloadMethod not yet supported, falling back to hot reload'); |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 183 | |
| 184 | try { |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 185 | await reloadMethod( |
| 186 | libraryId: libraryId, |
| 187 | classId: classId, |
| 188 | ); |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 189 | return <String, String>{'type': 'Success'}; |
| 190 | } on rpc.RpcException { |
| 191 | rethrow; |
Zachary Anderson | 9de7787 | 2020-02-27 22:46:23 -0800 | [diff] [blame^] | 192 | } on Exception catch (e, st) { |
Jonah Williams | 331d19f | 2019-10-29 13:21:06 -0700 | [diff] [blame] | 193 | throw rpc.RpcException(rpc_error_code.SERVER_ERROR, |
| 194 | 'Error during Sources Reload: $e\n$st'); |
| 195 | } |
| 196 | }); |
| 197 | _peer.sendNotification('registerService', <String, String>{ |
| 198 | 'service': 'reloadMethod', |
| 199 | 'alias': 'Flutter Tools', |
| 200 | }); |
Kenzie Schmoll | 33bfa6a | 2019-01-17 08:28:54 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (restart != null) { |
| 204 | _peer.registerMethod('hotRestart', (rpc.Parameters params) async { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 205 | final bool pause = params.asMap['pause'] as bool ?? false; |
Kenzie Schmoll | 33bfa6a | 2019-01-17 08:28:54 -0800 | [diff] [blame] | 206 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 207 | if (pause is! bool) { |
Alexandre Ardhuin | f15c887 | 2020-02-11 20:58:27 +0100 | [diff] [blame] | 208 | throw rpc.RpcException.invalidParams("Invalid 'pause': $pause"); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 209 | } |
Kenzie Schmoll | 33bfa6a | 2019-01-17 08:28:54 -0800 | [diff] [blame] | 210 | |
| 211 | try { |
| 212 | await restart(pause: pause); |
| 213 | return <String, String>{'type': 'Success'}; |
| 214 | } on rpc.RpcException { |
| 215 | rethrow; |
Zachary Anderson | 9de7787 | 2020-02-27 22:46:23 -0800 | [diff] [blame^] | 216 | } on Exception catch (e, st) { |
Kenzie Schmoll | 33bfa6a | 2019-01-17 08:28:54 -0800 | [diff] [blame] | 217 | throw rpc.RpcException(rpc_error_code.SERVER_ERROR, |
| 218 | 'Error during Hot Restart: $e\n$st'); |
| 219 | } |
| 220 | }); |
| 221 | |
Ben Konyi | 8d81c30 | 2019-07-08 15:09:17 -0700 | [diff] [blame] | 222 | _peer.sendNotification('registerService', <String, String>{ |
Kenzie Schmoll | 33bfa6a | 2019-01-17 08:28:54 -0800 | [diff] [blame] | 223 | 'service': 'hotRestart', |
| 224 | 'alias': 'Flutter Tools', |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 225 | }); |
| 226 | } |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 227 | |
Kenzie Schmoll | d50d9c5 | 2019-09-05 09:50:36 -0700 | [diff] [blame] | 228 | _peer.registerMethod('flutterVersion', (rpc.Parameters params) async { |
Mehmet Fidanboylu | 0c5ae7d | 2020-02-03 20:43:02 -0800 | [diff] [blame] | 229 | final FlutterVersion version = context.get<FlutterVersion>() ?? FlutterVersion(); |
Kenzie Schmoll | d50d9c5 | 2019-09-05 09:50:36 -0700 | [diff] [blame] | 230 | final Map<String, Object> versionJson = version.toJson(); |
| 231 | versionJson['frameworkRevisionShort'] = version.frameworkRevisionShort; |
| 232 | versionJson['engineRevisionShort'] = version.engineRevisionShort; |
| 233 | return versionJson; |
| 234 | }); |
| 235 | |
| 236 | _peer.sendNotification('registerService', <String, String>{ |
| 237 | 'service': 'flutterVersion', |
| 238 | 'alias': 'Flutter Tools', |
| 239 | }); |
| 240 | |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 241 | if (compileExpression != null) { |
| 242 | _peer.registerMethod('compileExpression', (rpc.Parameters params) async { |
| 243 | final String isolateId = params['isolateId'].asString; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 244 | if (isolateId is! String || isolateId.isEmpty) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 245 | throw rpc.RpcException.invalidParams( |
Alexandre Ardhuin | f15c887 | 2020-02-11 20:58:27 +0100 | [diff] [blame] | 246 | "Invalid 'isolateId': $isolateId"); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 247 | } |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 248 | final String expression = params['expression'].asString; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 249 | if (expression is! String || expression.isEmpty) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 250 | throw rpc.RpcException.invalidParams( |
Alexandre Ardhuin | f15c887 | 2020-02-11 20:58:27 +0100 | [diff] [blame] | 251 | "Invalid 'expression': $expression"); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 252 | } |
Alexander Aprelev | 7ebf272 | 2018-07-12 14:59:22 -0700 | [diff] [blame] | 253 | final List<String> definitions = |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 254 | List<String>.from(params['definitions'].asList); |
Alexander Aprelev | 7ebf272 | 2018-07-12 14:59:22 -0700 | [diff] [blame] | 255 | final List<String> typeDefinitions = |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 256 | List<String>.from(params['typeDefinitions'].asList); |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 257 | final String libraryUri = params['libraryUri'].asString; |
Alexander Aprelev | ed2376a | 2018-06-27 15:51:23 -0700 | [diff] [blame] | 258 | final String klass = params['klass'].exists ? params['klass'].asString : null; |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 259 | final bool isStatic = params['isStatic'].asBoolOr(false); |
| 260 | |
| 261 | try { |
| 262 | final String kernelBytesBase64 = await compileExpression(isolateId, |
| 263 | expression, definitions, typeDefinitions, libraryUri, klass, |
| 264 | isStatic); |
| 265 | return <String, dynamic>{'type': 'Success', |
| 266 | 'result': <String, dynamic> {'kernelBytes': kernelBytesBase64}}; |
| 267 | } on rpc.RpcException { |
| 268 | rethrow; |
Zachary Anderson | 9de7787 | 2020-02-27 22:46:23 -0800 | [diff] [blame^] | 269 | } on Exception catch (e, st) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 270 | throw rpc.RpcException(rpc_error_code.SERVER_ERROR, |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 271 | 'Error during expression compilation: $e\n$st'); |
| 272 | } |
| 273 | }); |
| 274 | |
Ben Konyi | 8d81c30 | 2019-07-08 15:09:17 -0700 | [diff] [blame] | 275 | _peer.sendNotification('registerService', <String, String>{ |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 276 | 'service': 'compileExpression', |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 277 | 'alias': 'Flutter Tools', |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 278 | }); |
| 279 | } |
Jonah Williams | 831163f | 2019-11-26 14:49:56 -0800 | [diff] [blame] | 280 | if (device != null) { |
| 281 | _peer.registerMethod('flutterMemoryInfo', (rpc.Parameters params) async { |
| 282 | final MemoryInfo result = await device.queryMemoryInfo(); |
| 283 | return result.toJson(); |
| 284 | }); |
| 285 | _peer.sendNotification('registerService', <String, String>{ |
| 286 | 'service': 'flutterMemoryInfo', |
| 287 | 'alias': 'Flutter Tools', |
| 288 | }); |
| 289 | } |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Dan Field | 1db5d66 | 2019-04-25 08:27:00 -0700 | [diff] [blame] | 292 | static void _unhandledError(dynamic error, dynamic stack) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 293 | globals.logger.printTrace('Error in internal implementation of JSON RPC.\n$error\n$stack'); |
Dan Field | 1db5d66 | 2019-04-25 08:27:00 -0700 | [diff] [blame] | 294 | assert(false); |
| 295 | } |
| 296 | |
Yegor | 25e6cc6 | 2016-12-12 10:58:21 -0800 | [diff] [blame] | 297 | /// Connect to a Dart VM Service at [httpUri]. |
| 298 | /// |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 299 | /// If the [reloadSources] parameter is not null, the 'reloadSources' service |
| 300 | /// will be registered. The VM Service Protocol allows clients to register |
| 301 | /// custom services that can be invoked by other clients through the service |
| 302 | /// protocol itself. |
| 303 | /// |
| 304 | /// See: https://github.com/dart-lang/sdk/commit/df8bf384eb815cf38450cb50a0f4b62230fba217 |
Yegor | d4830fc | 2017-11-08 10:43:47 -0800 | [diff] [blame] | 305 | static Future<VMService> connect( |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 306 | Uri httpUri, { |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 307 | ReloadSources reloadSources, |
| 308 | Restart restart, |
| 309 | CompileExpression compileExpression, |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 310 | ReloadMethod reloadMethod, |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 311 | io.CompressionOptions compression = io.CompressionOptions.compressionDefault, |
Jonah Williams | 831163f | 2019-11-26 14:49:56 -0800 | [diff] [blame] | 312 | Device device, |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 313 | }) async { |
| 314 | final VMServiceConnector connector = context.get<VMServiceConnector>() ?? VMService._connect; |
Jonah Williams | 831163f | 2019-11-26 14:49:56 -0800 | [diff] [blame] | 315 | return connector(httpUri, |
| 316 | reloadSources: reloadSources, |
| 317 | restart: restart, |
| 318 | compileExpression: compileExpression, |
| 319 | compression: compression, |
| 320 | device: device, |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 321 | reloadMethod: reloadMethod, |
Jonah Williams | 831163f | 2019-11-26 14:49:56 -0800 | [diff] [blame] | 322 | ); |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static Future<VMService> _connect( |
| 326 | Uri httpUri, { |
Carlo Bernaschina | 16037e3 | 2017-07-17 15:13:24 -0700 | [diff] [blame] | 327 | ReloadSources reloadSources, |
Kenzie Schmoll | 33bfa6a | 2019-01-17 08:28:54 -0800 | [diff] [blame] | 328 | Restart restart, |
Alexander Aprelev | f11c8d9 | 2018-06-05 13:22:13 -0700 | [diff] [blame] | 329 | CompileExpression compileExpression, |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 330 | ReloadMethod reloadMethod, |
Jonah Williams | 301eaa8 | 2019-04-15 08:59:28 -0700 | [diff] [blame] | 331 | io.CompressionOptions compression = io.CompressionOptions.compressionDefault, |
Jonah Williams | 831163f | 2019-11-26 14:49:56 -0800 | [diff] [blame] | 332 | Device device, |
Yegor | d4830fc | 2017-11-08 10:43:47 -0800 | [diff] [blame] | 333 | }) async { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 334 | final Uri wsUri = httpUri.replace(scheme: 'ws', path: globals.fs.path.join(httpUri.path, 'ws')); |
Jonah Williams | 301eaa8 | 2019-04-15 08:59:28 -0700 | [diff] [blame] | 335 | final StreamChannel<String> channel = await _openChannel(wsUri, compression: compression); |
Dan Field | 1db5d66 | 2019-04-25 08:27:00 -0700 | [diff] [blame] | 336 | final rpc.Peer peer = rpc.Peer.withoutJson(jsonDocument.bind(channel), onUnhandledError: _unhandledError); |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 337 | final VMService service = VMService( |
| 338 | peer, |
| 339 | httpUri, |
| 340 | wsUri, |
| 341 | reloadSources, |
| 342 | restart, |
| 343 | compileExpression, |
| 344 | device, |
| 345 | reloadMethod, |
| 346 | ); |
Yegor | d4830fc | 2017-11-08 10:43:47 -0800 | [diff] [blame] | 347 | // This call is to ensure we are able to establish a connection instead of |
| 348 | // keeping on trucking and failing farther down the process. |
| 349 | await service._sendRequest('getVersion', const <String, dynamic>{}); |
| 350 | return service; |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 351 | } |
Yegor | 25e6cc6 | 2016-12-12 10:58:21 -0800 | [diff] [blame] | 352 | |
John McCutchan | 487f28f | 2016-08-08 12:42:31 -0700 | [diff] [blame] | 353 | final Uri httpAddress; |
Dan Rubel | a9584e1 | 2016-11-30 20:29:04 -0500 | [diff] [blame] | 354 | final Uri wsAddress; |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 355 | final rpc.Peer _peer; |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 356 | final Completer<Map<String, dynamic>> _connectionError = Completer<Map<String, dynamic>>(); |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 357 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 358 | VM _vm; |
| 359 | /// The singleton [VM] object. Owns [Isolate] and [FlutterView] objects. |
| 360 | VM get vm => _vm; |
Devon Carew | ec75177 | 2016-05-26 15:26:14 -0700 | [diff] [blame] | 361 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 362 | final Map<String, StreamController<ServiceEvent>> _eventControllers = |
| 363 | <String, StreamController<ServiceEvent>>{}; |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 364 | |
Phil Quitslund | 802eca2 | 2019-03-06 11:05:16 -0800 | [diff] [blame] | 365 | final Set<String> _listeningFor = <String>{}; |
Devon Carew | ec75177 | 2016-05-26 15:26:14 -0700 | [diff] [blame] | 366 | |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 367 | /// Whether our connection to the VM service has been closed; |
| 368 | bool get isClosed => _peer.isClosed; |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 369 | |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 370 | Future<void> get done async { |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 371 | await _peer.done; |
| 372 | } |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 373 | |
| 374 | // Events |
Alexander Aprelev | 09ca3c1 | 2018-04-18 01:21:33 -0700 | [diff] [blame] | 375 | Future<Stream<ServiceEvent>> get onDebugEvent => onEvent('Debug'); |
| 376 | Future<Stream<ServiceEvent>> get onExtensionEvent => onEvent('Extension'); |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 377 | // IsolateStart, IsolateRunnable, IsolateExit, IsolateUpdate, ServiceExtensionAdded |
Alexander Aprelev | 09ca3c1 | 2018-04-18 01:21:33 -0700 | [diff] [blame] | 378 | Future<Stream<ServiceEvent>> get onIsolateEvent => onEvent('Isolate'); |
| 379 | Future<Stream<ServiceEvent>> get onTimelineEvent => onEvent('Timeline'); |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 380 | Future<Stream<ServiceEvent>> get onStdoutEvent => onEvent('Stdout'); // WriteEvent |
| 381 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 382 | // TODO(johnmccutchan): Add FlutterView events. |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 383 | |
Todd Volkert | 3a0aabc | 2018-04-18 09:24:52 -0700 | [diff] [blame] | 384 | /// Returns a stream of VM service events. |
| 385 | /// |
| 386 | /// This purposely returns a `Future<Stream<T>>` rather than a `Stream<T>` |
| 387 | /// because it first registers with the VM to receive events on the stream, |
| 388 | /// and only once the VM has acknowledged that the stream has started will |
| 389 | /// we return the associated stream. Any attempt to streamline this API into |
| 390 | /// returning `Stream<T>` should take that into account to avoid race |
| 391 | /// conditions. |
Alexander Aprelev | 09ca3c1 | 2018-04-18 01:21:33 -0700 | [diff] [blame] | 392 | Future<Stream<ServiceEvent>> onEvent(String streamId) async { |
| 393 | await _streamListen(streamId); |
Devon Carew | ec75177 | 2016-05-26 15:26:14 -0700 | [diff] [blame] | 394 | return _getEventController(streamId).stream; |
| 395 | } |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 396 | |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 397 | Future<Map<String, dynamic>> _sendRequest( |
| 398 | String method, |
| 399 | Map<String, dynamic> params, |
| 400 | ) { |
Alexandre Ardhuin | f62afdc | 2018-10-01 21:29:08 +0200 | [diff] [blame] | 401 | return Future.any<Map<String, dynamic>>(<Future<Map<String, dynamic>>>[ |
Alexandre Ardhuin | f11c341 | 2019-09-27 10:46:45 +0200 | [diff] [blame] | 402 | _peer.sendRequest(method, params).then<Map<String, dynamic>>(castStringKeyedMap), |
| 403 | _connectionError.future, |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 404 | ]); |
| 405 | } |
| 406 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 407 | StreamController<ServiceEvent> _getEventController(String eventName) { |
| 408 | StreamController<ServiceEvent> controller = _eventControllers[eventName]; |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 409 | if (controller == null) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 410 | controller = StreamController<ServiceEvent>.broadcast(); |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 411 | _eventControllers[eventName] = controller; |
| 412 | } |
| 413 | return controller; |
| 414 | } |
| 415 | |
| 416 | void _handleStreamNotify(Map<String, dynamic> data) { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 417 | final String streamId = data['streamId'] as String; |
| 418 | final Map<String, dynamic> eventData = castStringKeyedMap(data['event']); |
| 419 | final Map<String, dynamic> eventIsolate = castStringKeyedMap(eventData['isolate']); |
Devon Carew | 990dae8 | 2017-07-27 11:47:33 -0700 | [diff] [blame] | 420 | |
| 421 | // Log event information. |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 422 | globals.printTrace('Notification from VM: $data'); |
Devon Carew | 990dae8 | 2017-07-27 11:47:33 -0700 | [diff] [blame] | 423 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 424 | ServiceEvent event; |
| 425 | if (eventIsolate != null) { |
| 426 | // getFromMap creates the Isolate if necessary. |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 427 | final Isolate isolate = vm.getFromMap(eventIsolate) as Isolate; |
| 428 | event = ServiceObject._fromMap(isolate, eventData) as ServiceEvent; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 429 | if (event.kind == ServiceEvent.kIsolateExit) { |
| 430 | vm._isolateCache.remove(isolate.id); |
| 431 | vm._buildIsolateList(); |
| 432 | } else if (event.kind == ServiceEvent.kIsolateRunnable) { |
| 433 | // Force reload once the isolate becomes runnable so that we |
| 434 | // update the root library. |
| 435 | isolate.reload(); |
| 436 | } |
| 437 | } else { |
| 438 | // The event doesn't have an isolate, so it is owned by the VM. |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 439 | event = ServiceObject._fromMap(vm, eventData) as ServiceEvent; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 440 | } |
| 441 | _getEventController(streamId).add(event); |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 444 | Future<void> _streamListen(String streamId) async { |
Devon Carew | ec75177 | 2016-05-26 15:26:14 -0700 | [diff] [blame] | 445 | if (!_listeningFor.contains(streamId)) { |
| 446 | _listeningFor.add(streamId); |
Jonah Williams | 8d6dc62 | 2019-08-15 12:13:12 -0700 | [diff] [blame] | 447 | await _sendRequest('streamListen', <String, dynamic>{'streamId': streamId}); |
Devon Carew | ec75177 | 2016-05-26 15:26:14 -0700 | [diff] [blame] | 448 | } |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 449 | } |
| 450 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 451 | /// Reloads the VM. |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 452 | Future<void> getVM() async => await vm.reload(); |
Zachary Anderson | 9a068c6 | 2017-03-16 14:35:53 -0700 | [diff] [blame] | 453 | |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 454 | Future<void> refreshViews({ bool waitForViews = false }) => vm.refreshViews(waitForViews: waitForViews); |
Martin Kustermann | d294757 | 2020-02-18 21:39:37 +0100 | [diff] [blame] | 455 | |
| 456 | Future<void> close() async => await _peer.close(); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /// An error that is thrown when constructing/updating a service object. |
| 460 | class VMServiceObjectLoadError { |
| 461 | VMServiceObjectLoadError(this.message, this.map); |
| 462 | final String message; |
| 463 | final Map<String, dynamic> map; |
| 464 | } |
| 465 | |
| 466 | bool _isServiceMap(Map<String, dynamic> m) { |
| 467 | return (m != null) && (m['type'] != null); |
| 468 | } |
| 469 | bool _hasRef(String type) => (type != null) && type.startsWith('@'); |
Alexandre Ardhuin | 2e80bf1 | 2018-02-06 09:00:11 +0100 | [diff] [blame] | 470 | String _stripRef(String type) => _hasRef(type) ? type.substring(1) : type; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 471 | |
| 472 | /// Given a raw response from the service protocol and a [ServiceObjectOwner], |
| 473 | /// recursively walk the response and replace values that are service maps with |
| 474 | /// actual [ServiceObject]s. During the upgrade the owner is given a chance |
| 475 | /// to return a cached / canonicalized object. |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 476 | void _upgradeCollection( |
| 477 | dynamic collection, |
| 478 | ServiceObjectOwner owner, |
| 479 | ) { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 480 | if (collection is ServiceMap) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 481 | return; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 482 | } |
Jacob Richman | 53fc96d | 2017-02-06 08:55:09 -0800 | [diff] [blame] | 483 | if (collection is Map<String, dynamic>) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 484 | _upgradeMap(collection, owner); |
| 485 | } else if (collection is List) { |
| 486 | _upgradeList(collection, owner); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | void _upgradeMap(Map<String, dynamic> map, ServiceObjectOwner owner) { |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 491 | map.forEach((String k, Object v) { |
Jacob Richman | 53fc96d | 2017-02-06 08:55:09 -0800 | [diff] [blame] | 492 | if ((v is Map<String, dynamic>) && _isServiceMap(v)) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 493 | map[k] = owner.getFromMap(v); |
| 494 | } else if (v is List) { |
| 495 | _upgradeList(v, owner); |
Jacob Richman | 53fc96d | 2017-02-06 08:55:09 -0800 | [diff] [blame] | 496 | } else if (v is Map<String, dynamic>) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 497 | _upgradeMap(v, owner); |
| 498 | } |
| 499 | }); |
| 500 | } |
| 501 | |
| 502 | void _upgradeList(List<dynamic> list, ServiceObjectOwner owner) { |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 503 | for (int i = 0; i < list.length; i += 1) { |
| 504 | final Object v = list[i]; |
Jacob Richman | 53fc96d | 2017-02-06 08:55:09 -0800 | [diff] [blame] | 505 | if ((v is Map<String, dynamic>) && _isServiceMap(v)) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 506 | list[i] = owner.getFromMap(v); |
| 507 | } else if (v is List) { |
| 508 | _upgradeList(v, owner); |
Jacob Richman | 53fc96d | 2017-02-06 08:55:09 -0800 | [diff] [blame] | 509 | } else if (v is Map<String, dynamic>) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 510 | _upgradeMap(v, owner); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /// Base class of all objects received over the service protocol. |
| 516 | abstract class ServiceObject { |
| 517 | ServiceObject._empty(this._owner); |
| 518 | |
| 519 | /// Factory constructor given a [ServiceObjectOwner] and a service map, |
| 520 | /// upgrade the map into a proper [ServiceObject]. This function always |
| 521 | /// returns a new instance and does not interact with caches. |
Alexandre Ardhuin | bfa1d25 | 2019-03-23 00:02:21 +0100 | [diff] [blame] | 522 | factory ServiceObject._fromMap( |
| 523 | ServiceObjectOwner owner, |
| 524 | Map<String, dynamic> map, |
| 525 | ) { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 526 | if (map == null) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 527 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 528 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 529 | |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 530 | if (!_isServiceMap(map)) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 531 | throw VMServiceObjectLoadError('Expected a service map', map); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 532 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 533 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 534 | final String type = _stripRef(map['type'] as String); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 535 | |
| 536 | ServiceObject serviceObject; |
| 537 | switch (type) { |
| 538 | case 'Event': |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 539 | serviceObject = ServiceEvent._empty(owner); |
Alexandre Ardhuin | b873162 | 2019-09-24 21:03:37 +0200 | [diff] [blame] | 540 | break; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 541 | case 'FlutterView': |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 542 | serviceObject = FlutterView._empty(owner.vm); |
Alexandre Ardhuin | b873162 | 2019-09-24 21:03:37 +0200 | [diff] [blame] | 543 | break; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 544 | case 'Isolate': |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 545 | serviceObject = Isolate._empty(owner.vm); |
Alexandre Ardhuin | b873162 | 2019-09-24 21:03:37 +0200 | [diff] [blame] | 546 | break; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 547 | } |
Alexandre Ardhuin | 8bcf302 | 2017-04-07 21:41:17 +0200 | [diff] [blame] | 548 | // If we don't have a model object for this service object type, as a |
| 549 | // fallback return a ServiceMap object. |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 550 | serviceObject ??= ServiceMap._empty(owner); |
Devon Carew | 990dae8 | 2017-07-27 11:47:33 -0700 | [diff] [blame] | 551 | // We have now constructed an empty service object, call update to populate it. |
Alexander Aprelev | 0f3aa50 | 2018-02-09 10:33:24 -0800 | [diff] [blame] | 552 | serviceObject.updateFromMap(map); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 553 | return serviceObject; |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 554 | } |
| 555 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 556 | final ServiceObjectOwner _owner; |
| 557 | ServiceObjectOwner get owner => _owner; |
| 558 | |
| 559 | /// The id of this object. |
| 560 | String get id => _id; |
| 561 | String _id; |
| 562 | |
| 563 | /// The user-level type of this object. |
| 564 | String get type => _type; |
| 565 | String _type; |
| 566 | |
| 567 | /// The vm-level type of this object. Usually the same as [type]. |
| 568 | String get vmType => _vmType; |
| 569 | String _vmType; |
| 570 | |
| 571 | /// Is it safe to cache this object? |
| 572 | bool _canCache = false; |
| 573 | bool get canCache => _canCache; |
| 574 | |
| 575 | /// Has this object been fully loaded? |
| 576 | bool get loaded => _loaded; |
| 577 | bool _loaded = false; |
| 578 | |
| 579 | /// Is this object immutable after it is [loaded]? |
| 580 | bool get immutable => false; |
| 581 | |
| 582 | String get name => _name; |
| 583 | String _name; |
| 584 | |
| 585 | String get vmName => _vmName; |
| 586 | String _vmName; |
| 587 | |
| 588 | /// If this is not already loaded, load it. Otherwise reload. |
| 589 | Future<ServiceObject> load() async { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 590 | if (loaded) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 591 | return this; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 592 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 593 | return reload(); |
| 594 | } |
| 595 | |
| 596 | /// Fetch this object from vmService and return the response directly. |
| 597 | Future<Map<String, dynamic>> _fetchDirect() { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 598 | final Map<String, dynamic> params = <String, dynamic>{ |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 599 | 'objectId': id, |
| 600 | }; |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 601 | return _owner.isolate.invokeRpcRaw('getObject', params: params); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | Future<ServiceObject> _inProgressReload; |
| 605 | /// Reload the service object (if possible). |
| 606 | Future<ServiceObject> reload() async { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 607 | final bool hasId = (id != null) && (id != ''); |
| 608 | final bool isVM = this is VM; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 609 | // We should always reload the VM. |
| 610 | // We can't reload objects without an id. |
| 611 | // We shouldn't reload an immutable and already loaded object. |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 612 | if (!isVM && (!hasId || (immutable && loaded))) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 613 | return this; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 614 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 615 | |
| 616 | if (_inProgressReload == null) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 617 | final Completer<ServiceObject> completer = Completer<ServiceObject>(); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 618 | _inProgressReload = completer.future; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 619 | try { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 620 | final Map<String, dynamic> response = await _fetchDirect(); |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 621 | if (_stripRef(response['type'] as String) == 'Sentinel') { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 622 | // An object may have been collected. |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 623 | completer.complete(ServiceObject._fromMap(owner, response)); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 624 | } else { |
Alexander Aprelev | 0f3aa50 | 2018-02-09 10:33:24 -0800 | [diff] [blame] | 625 | updateFromMap(response); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 626 | completer.complete(this); |
| 627 | } |
Zachary Anderson | 9de7787 | 2020-02-27 22:46:23 -0800 | [diff] [blame^] | 628 | // Catches all exceptions to propagate to the completer. |
| 629 | } catch (e, st) { // ignore: avoid_catches_without_on_clauses |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 630 | completer.completeError(e, st); |
| 631 | } |
| 632 | _inProgressReload = null; |
Jason Simmons | 494270f | 2018-02-20 09:45:32 -0800 | [diff] [blame] | 633 | return await completer.future; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 636 | return await _inProgressReload; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /// Update [this] using [map] as a source. [map] can be a service reference. |
Alexander Aprelev | 0f3aa50 | 2018-02-09 10:33:24 -0800 | [diff] [blame] | 640 | void updateFromMap(Map<String, dynamic> map) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 641 | // Don't allow the type to change on an object update. |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 642 | final bool mapIsRef = _hasRef(map['type'] as String); |
| 643 | final String mapType = _stripRef(map['type'] as String); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 644 | |
| 645 | if ((_type != null) && (_type != mapType)) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 646 | throw VMServiceObjectLoadError('ServiceObject types must not change', |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 647 | map); |
| 648 | } |
| 649 | _type = mapType; |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 650 | _vmType = map.containsKey('_vmType') ? _stripRef(map['_vmType'] as String) : _type; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 651 | |
| 652 | _canCache = map['fixedId'] == true; |
| 653 | if ((_id != null) && (_id != map['id']) && _canCache) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 654 | throw VMServiceObjectLoadError('ServiceObject id changed', map); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 655 | } |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 656 | _id = map['id'] as String; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 657 | |
| 658 | // Copy name properties. |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 659 | _name = map['name'] as String; |
| 660 | _vmName = map.containsKey('_vmName') ? map['_vmName'] as String : _name; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 661 | |
| 662 | // We have now updated all common properties, let the subclasses update |
| 663 | // their specific properties. |
| 664 | _update(map, mapIsRef); |
| 665 | } |
| 666 | |
| 667 | /// Implemented by subclasses to populate their model. |
| 668 | void _update(Map<String, dynamic> map, bool mapIsRef); |
| 669 | } |
| 670 | |
| 671 | class ServiceEvent extends ServiceObject { |
Alexandre Ardhuin | 2ea1d81 | 2018-10-04 07:28:07 +0200 | [diff] [blame] | 672 | ServiceEvent._empty(ServiceObjectOwner owner) : super._empty(owner); |
| 673 | |
| 674 | String _kind; |
| 675 | String get kind => _kind; |
| 676 | DateTime _timestamp; |
| 677 | DateTime get timestamp => _timestamp; |
| 678 | String _extensionKind; |
| 679 | String get extensionKind => _extensionKind; |
| 680 | Map<String, dynamic> _extensionData; |
| 681 | Map<String, dynamic> get extensionData => _extensionData; |
| 682 | List<Map<String, dynamic>> _timelineEvents; |
| 683 | List<Map<String, dynamic>> get timelineEvents => _timelineEvents; |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 684 | String _message; |
| 685 | String get message => _message; |
Alexandre Ardhuin | 2ea1d81 | 2018-10-04 07:28:07 +0200 | [diff] [blame] | 686 | |
| 687 | // The possible 'kind' values. |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 688 | static const String kVMUpdate = 'VMUpdate'; |
| 689 | static const String kIsolateStart = 'IsolateStart'; |
| 690 | static const String kIsolateRunnable = 'IsolateRunnable'; |
| 691 | static const String kIsolateExit = 'IsolateExit'; |
| 692 | static const String kIsolateUpdate = 'IsolateUpdate'; |
| 693 | static const String kIsolateReload = 'IsolateReload'; |
| 694 | static const String kIsolateSpawn = 'IsolateSpawn'; |
| 695 | static const String kServiceExtensionAdded = 'ServiceExtensionAdded'; |
| 696 | static const String kPauseStart = 'PauseStart'; |
| 697 | static const String kPauseExit = 'PauseExit'; |
| 698 | static const String kPauseBreakpoint = 'PauseBreakpoint'; |
| 699 | static const String kPauseInterrupted = 'PauseInterrupted'; |
| 700 | static const String kPauseException = 'PauseException'; |
John McCutchan | 9112823 | 2016-10-20 07:30:24 -0700 | [diff] [blame] | 701 | static const String kPausePostRequest = 'PausePostRequest'; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 702 | static const String kNone = 'None'; |
| 703 | static const String kResume = 'Resume'; |
| 704 | static const String kBreakpointAdded = 'BreakpointAdded'; |
| 705 | static const String kBreakpointResolved = 'BreakpointResolved'; |
| 706 | static const String kBreakpointRemoved = 'BreakpointRemoved'; |
| 707 | static const String kGraph = '_Graph'; |
| 708 | static const String kGC = 'GC'; |
| 709 | static const String kInspect = 'Inspect'; |
| 710 | static const String kDebuggerSettingsUpdate = '_DebuggerSettingsUpdate'; |
| 711 | static const String kConnectionClosed = 'ConnectionClosed'; |
| 712 | static const String kLogging = '_Logging'; |
| 713 | static const String kExtension = 'Extension'; |
| 714 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 715 | @override |
| 716 | void _update(Map<String, dynamic> map, bool mapIsRef) { |
| 717 | _loaded = true; |
| 718 | _upgradeCollection(map, owner); |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 719 | _kind = map['kind'] as String; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 720 | assert(map['isolate'] == null || owner == map['isolate']); |
| 721 | _timestamp = |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 722 | DateTime.fromMillisecondsSinceEpoch(map['timestamp'] as int); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 723 | if (map['extensionKind'] != null) { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 724 | _extensionKind = map['extensionKind'] as String; |
| 725 | _extensionData = castStringKeyedMap(map['extensionData']); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 726 | } |
liyuqian | 8e6f468 | 2019-02-26 16:38:59 -0800 | [diff] [blame] | 727 | // map['timelineEvents'] is List<dynamic> which can't be assigned to |
| 728 | // List<Map<String, dynamic>> directly. Unfortunately, we previously didn't |
| 729 | // catch this exception because json_rpc_2 is hiding all these exceptions |
| 730 | // on a Stream. |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 731 | final List<dynamic> dynamicList = map['timelineEvents'] as List<dynamic>; |
liyuqian | 8e6f468 | 2019-02-26 16:38:59 -0800 | [diff] [blame] | 732 | _timelineEvents = dynamicList?.cast<Map<String, dynamic>>(); |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 733 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 734 | final String base64Bytes = map['bytes'] as String; |
Jenn Magder | 2e7d913 | 2019-11-01 14:37:17 -0700 | [diff] [blame] | 735 | if (base64Bytes != null) { |
| 736 | _message = utf8.decode(base64.decode(base64Bytes)).trim(); |
| 737 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | bool get isPauseEvent { |
Alexandre Ardhuin | 2e80bf1 | 2018-02-06 09:00:11 +0100 | [diff] [blame] | 741 | return kind == kPauseStart || |
| 742 | kind == kPauseExit || |
| 743 | kind == kPauseBreakpoint || |
| 744 | kind == kPauseInterrupted || |
| 745 | kind == kPauseException || |
| 746 | kind == kPausePostRequest || |
| 747 | kind == kNone; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | /// A ServiceObjectOwner is either a [VM] or an [Isolate]. Owners can cache |
Greg Spencer | 0259be9 | 2017-11-17 10:05:21 -0800 | [diff] [blame] | 752 | /// and/or canonicalize service objects received over the wire. |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 753 | abstract class ServiceObjectOwner extends ServiceObject { |
| 754 | ServiceObjectOwner._empty(ServiceObjectOwner owner) : super._empty(owner); |
| 755 | |
| 756 | /// Returns the owning VM. |
| 757 | VM get vm => null; |
| 758 | |
| 759 | /// Returns the owning isolate (if any). |
| 760 | Isolate get isolate => null; |
| 761 | |
| 762 | /// Returns the vmService connection. |
| 763 | VMService get vmService => null; |
| 764 | |
| 765 | /// Builds a [ServiceObject] corresponding to the [id] from [map]. |
Alexandre Ardhuin | c02b6a8 | 2018-02-02 23:27:29 +0100 | [diff] [blame] | 766 | /// The result may come from the cache. The result will not necessarily |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 767 | /// be [loaded]. |
| 768 | ServiceObject getFromMap(Map<String, dynamic> map); |
| 769 | } |
| 770 | |
| 771 | /// There is only one instance of the VM class. The VM class owns [Isolate] |
| 772 | /// and [FlutterView] objects. |
| 773 | class VM extends ServiceObjectOwner { |
| 774 | VM._empty(this._vmService) : super._empty(null); |
| 775 | |
| 776 | /// Connection to the VMService. |
| 777 | final VMService _vmService; |
| 778 | @override |
| 779 | VMService get vmService => _vmService; |
| 780 | |
| 781 | @override |
| 782 | VM get vm => this; |
| 783 | |
| 784 | @override |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 785 | Future<Map<String, dynamic>> _fetchDirect() => invokeRpcRaw('getVM'); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 786 | |
| 787 | @override |
| 788 | void _update(Map<String, dynamic> map, bool mapIsRef) { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 789 | if (mapIsRef) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 790 | return; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 791 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 792 | |
| 793 | // Upgrade the collection. A side effect of this call is that any new |
| 794 | // isolates in the map are created and added to the isolate cache. |
| 795 | _upgradeCollection(map, this); |
| 796 | _loaded = true; |
| 797 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 798 | _pid = map['pid'] as int; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 799 | if (map['_heapAllocatedMemoryUsage'] != null) { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 800 | _heapAllocatedMemoryUsage = map['_heapAllocatedMemoryUsage'] as int; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 801 | } |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 802 | _maxRSS = map['_maxRSS'] as int; |
| 803 | _embedder = map['_embedder'] as String; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 804 | |
| 805 | // Remove any isolates which are now dead from the isolate cache. |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 806 | _removeDeadIsolates((map['isolates'] as List<dynamic>).cast<Isolate>()); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Alexandre Ardhuin | 329e52c | 2017-03-08 23:57:31 +0100 | [diff] [blame] | 809 | final Map<String, ServiceObject> _cache = <String,ServiceObject>{}; |
| 810 | final Map<String,Isolate> _isolateCache = <String,Isolate>{}; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 811 | |
| 812 | /// The list of live isolates, ordered by isolate start time. |
Alexandre Ardhuin | 329e52c | 2017-03-08 23:57:31 +0100 | [diff] [blame] | 813 | final List<Isolate> isolates = <Isolate>[]; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 814 | |
| 815 | /// The set of live views. |
Alexandre Ardhuin | 329e52c | 2017-03-08 23:57:31 +0100 | [diff] [blame] | 816 | final Map<String, FlutterView> _viewCache = <String, FlutterView>{}; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 817 | |
Jason Simmons | 67b3871 | 2017-04-07 13:41:29 -0700 | [diff] [blame] | 818 | /// The pid of the VM's process. |
| 819 | int _pid; |
Jason Simmons | 67b3871 | 2017-04-07 13:41:29 -0700 | [diff] [blame] | 820 | int get pid => _pid; |
| 821 | |
Zachary Anderson | 9b250cb | 2017-04-20 15:06:56 -0700 | [diff] [blame] | 822 | /// The number of bytes allocated (e.g. by malloc) in the native heap. |
| 823 | int _heapAllocatedMemoryUsage; |
Alexandre Ardhuin | 4fa32df | 2019-05-16 22:25:51 +0200 | [diff] [blame] | 824 | int get heapAllocatedMemoryUsage => _heapAllocatedMemoryUsage ?? 0; |
Zachary Anderson | 9b250cb | 2017-04-20 15:06:56 -0700 | [diff] [blame] | 825 | |
| 826 | /// The peak resident set size for the process. |
| 827 | int _maxRSS; |
Alexandre Ardhuin | 4fa32df | 2019-05-16 22:25:51 +0200 | [diff] [blame] | 828 | int get maxRSS => _maxRSS ?? 0; |
Zachary Anderson | 9b250cb | 2017-04-20 15:06:56 -0700 | [diff] [blame] | 829 | |
Ryan Macnak | a0ffe87 | 2018-02-26 15:14:09 -0800 | [diff] [blame] | 830 | // The embedder's name, Flutter or dart_runner. |
| 831 | String _embedder; |
| 832 | String get embedder => _embedder; |
| 833 | bool get isFlutterEngine => embedder == 'Flutter'; |
| 834 | bool get isDartRunner => embedder == 'dart_runner'; |
| 835 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 836 | int _compareIsolates(Isolate a, Isolate b) { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 837 | final DateTime aStart = a.startTime; |
| 838 | final DateTime bStart = b.startTime; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 839 | if (aStart == null) { |
| 840 | if (bStart == null) { |
| 841 | return 0; |
| 842 | } else { |
| 843 | return 1; |
| 844 | } |
| 845 | } |
| 846 | if (bStart == null) { |
| 847 | return -1; |
| 848 | } |
| 849 | return aStart.compareTo(bStart); |
| 850 | } |
| 851 | |
| 852 | void _buildIsolateList() { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 853 | final List<Isolate> isolateList = _isolateCache.values.toList(); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 854 | isolateList.sort(_compareIsolates); |
| 855 | isolates.clear(); |
| 856 | isolates.addAll(isolateList); |
| 857 | } |
| 858 | |
| 859 | void _removeDeadIsolates(List<Isolate> newIsolates) { |
| 860 | // Build a set of new isolates. |
Phil Quitslund | 802eca2 | 2019-03-06 11:05:16 -0800 | [diff] [blame] | 861 | final Set<String> newIsolateSet = <String>{}; |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 862 | for (final Isolate iso in newIsolates) { |
Alexandre Ardhuin | 700dc9f | 2017-10-19 08:16:16 +0200 | [diff] [blame] | 863 | newIsolateSet.add(iso.id); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 864 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 865 | |
| 866 | // Remove any old isolates which no longer exist. |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 867 | final List<String> toRemove = <String>[]; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 868 | _isolateCache.forEach((String id, _) { |
| 869 | if (!newIsolateSet.contains(id)) { |
| 870 | toRemove.add(id); |
| 871 | } |
| 872 | }); |
Alexandre Ardhuin | 2166ea5 | 2017-03-15 23:09:58 +0100 | [diff] [blame] | 873 | toRemove.forEach(_isolateCache.remove); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 874 | _buildIsolateList(); |
| 875 | } |
| 876 | |
| 877 | @override |
| 878 | ServiceObject getFromMap(Map<String, dynamic> map) { |
| 879 | if (map == null) { |
| 880 | return null; |
| 881 | } |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 882 | final String type = _stripRef(map['type'] as String); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 883 | if (type == 'VM') { |
| 884 | // Update this VM object. |
Alexander Aprelev | 0f3aa50 | 2018-02-09 10:33:24 -0800 | [diff] [blame] | 885 | updateFromMap(map); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 886 | return this; |
| 887 | } |
| 888 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 889 | final String mapId = map['id'] as String; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 890 | |
| 891 | switch (type) { |
Alexandre Ardhuin | b873162 | 2019-09-24 21:03:37 +0200 | [diff] [blame] | 892 | case 'Isolate': |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 893 | // Check cache. |
| 894 | Isolate isolate = _isolateCache[mapId]; |
| 895 | if (isolate == null) { |
| 896 | // Add new isolate to the cache. |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 897 | isolate = ServiceObject._fromMap(this, map) as Isolate; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 898 | _isolateCache[mapId] = isolate; |
| 899 | _buildIsolateList(); |
| 900 | |
| 901 | // Eagerly load the isolate. |
| 902 | isolate.load().catchError((dynamic e, StackTrace stack) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 903 | globals.printTrace('Eagerly loading an isolate failed: $e\n$stack'); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 904 | }); |
| 905 | } else { |
| 906 | // Existing isolate, update data. |
Alexander Aprelev | 0f3aa50 | 2018-02-09 10:33:24 -0800 | [diff] [blame] | 907 | isolate.updateFromMap(map); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 908 | } |
| 909 | return isolate; |
Alexandre Ardhuin | b873162 | 2019-09-24 21:03:37 +0200 | [diff] [blame] | 910 | case 'FlutterView': |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 911 | FlutterView view = _viewCache[mapId]; |
| 912 | if (view == null) { |
| 913 | // Add new view to the cache. |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 914 | view = ServiceObject._fromMap(this, map) as FlutterView; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 915 | _viewCache[mapId] = view; |
| 916 | } else { |
Alexander Aprelev | 0f3aa50 | 2018-02-09 10:33:24 -0800 | [diff] [blame] | 917 | view.updateFromMap(map); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 918 | } |
| 919 | return view; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 920 | default: |
Ryan Macnak | 315471b | 2019-10-17 16:56:57 -0700 | [diff] [blame] | 921 | // If we don't have a model object for this service object type, as a |
| 922 | // fallback return a ServiceMap object. |
| 923 | final ServiceObject serviceObject = ServiceMap._empty(owner); |
| 924 | // We have now constructed an empty service object, call update to populate it. |
| 925 | serviceObject.updateFromMap(map); |
| 926 | return serviceObject; |
Devon Carew | 6cdfd86 | 2016-06-24 10:26:43 -0700 | [diff] [blame] | 927 | } |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Devon Carew | 6737712 | 2017-01-12 14:03:04 -0800 | [diff] [blame] | 930 | // This function does not reload the isolate if it's found in the cache. |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 931 | Future<Isolate> getIsolate(String isolateId) { |
| 932 | if (!loaded) { |
| 933 | // Trigger a VM load, then get the isolate. Ignore any errors. |
Ian Hickson | 63aa139 | 2017-01-23 01:04:31 -0800 | [diff] [blame] | 934 | return load().then<Isolate>((ServiceObject serviceObject) => getIsolate(isolateId)).catchError((dynamic error) => null); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 935 | } |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 936 | return Future<Isolate>.value(_isolateCache[isolateId]); |
John McCutchan | 728e2a5 | 2016-08-11 12:56:47 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 939 | static String _truncate(String message, int width, String ellipsis) { |
| 940 | assert(ellipsis.length < width); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 941 | if (message.length <= width) { |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 942 | return message; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 943 | } |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 944 | return message.substring(0, width - ellipsis.length) + ellipsis; |
| 945 | } |
| 946 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 947 | /// Invoke the RPC and return the raw response. |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 948 | Future<Map<String, dynamic>> invokeRpcRaw( |
| 949 | String method, { |
Alexandre Ardhuin | 09276be | 2018-06-05 08:50:40 +0200 | [diff] [blame] | 950 | Map<String, dynamic> params = const <String, dynamic>{}, |
Zachary Anderson | 22ca3f9 | 2019-06-12 11:18:53 -0700 | [diff] [blame] | 951 | bool truncateLogs = true, |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 952 | }) async { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 953 | globals.printTrace('Sending to VM service: $method($params)'); |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 954 | assert(params != null); |
Yegor | 25e6cc6 | 2016-12-12 10:58:21 -0800 | [diff] [blame] | 955 | try { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 956 | final Map<String, dynamic> result = await _vmService._sendRequest(method, params); |
Zachary Anderson | 22ca3f9 | 2019-06-12 11:18:53 -0700 | [diff] [blame] | 957 | final String resultString = |
| 958 | truncateLogs ? _truncate(result.toString(), 250, '...') : result.toString(); |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 959 | globals.printTrace('Result: $resultString'); |
Yegor | 25e6cc6 | 2016-12-12 10:58:21 -0800 | [diff] [blame] | 960 | return result; |
Todd Volkert | d6f61b9 | 2017-02-15 06:52:28 -0800 | [diff] [blame] | 961 | } on WebSocketChannelException catch (error) { |
| 962 | throwToolExit('Error connecting to observatory: $error'); |
| 963 | return null; |
Ian Hickson | 8fd20b5 | 2017-11-01 15:04:43 -0700 | [diff] [blame] | 964 | } on rpc.RpcException catch (error) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 965 | globals.printError('Error ${error.code} received from application: ${error.message}'); |
| 966 | globals.printTrace('${error.data}'); |
Alexander Aprelev | ea30c95 | 2018-04-13 17:02:14 -0700 | [diff] [blame] | 967 | rethrow; |
Yegor | 25e6cc6 | 2016-12-12 10:58:21 -0800 | [diff] [blame] | 968 | } |
John McCutchan | 81b4e82 | 2016-08-05 12:04:33 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 971 | /// Invoke the RPC and return a [ServiceObject] response. |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 972 | Future<T> invokeRpc<T extends ServiceObject>( |
| 973 | String method, { |
Alexandre Ardhuin | 09276be | 2018-06-05 08:50:40 +0200 | [diff] [blame] | 974 | Map<String, dynamic> params = const <String, dynamic>{}, |
Zachary Anderson | 22ca3f9 | 2019-06-12 11:18:53 -0700 | [diff] [blame] | 975 | bool truncateLogs = true, |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 976 | }) async { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 977 | final Map<String, dynamic> response = await invokeRpcRaw( |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 978 | method, |
| 979 | params: params, |
Zachary Anderson | 22ca3f9 | 2019-06-12 11:18:53 -0700 | [diff] [blame] | 980 | truncateLogs: truncateLogs, |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 981 | ); |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 982 | final T serviceObject = ServiceObject._fromMap(this, response) as T; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 983 | if ((serviceObject != null) && (serviceObject._canCache)) { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 984 | final String serviceObjectId = serviceObject.id; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 985 | _cache.putIfAbsent(serviceObjectId, () => serviceObject); |
| 986 | } |
| 987 | return serviceObject; |
John McCutchan | 81b4e82 | 2016-08-05 12:04:33 -0700 | [diff] [blame] | 988 | } |
| 989 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 990 | /// Create a new development file system on the device. |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 991 | Future<Map<String, dynamic>> createDevFS(String fsName) { |
Alexandre Ardhuin | bfa1d25 | 2019-03-23 00:02:21 +0100 | [diff] [blame] | 992 | return invokeRpcRaw('_createDevFS', params: <String, dynamic>{'fsName': fsName}); |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 993 | } |
| 994 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 995 | /// List the development file system son the device. |
| 996 | Future<List<String>> listDevFS() async { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 997 | return (await invokeRpcRaw('_listDevFS'))['fsNames'] as List<String>; |
Devon Carew | b2938f40 | 2016-06-10 15:08:12 -0700 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | // Write one file into a file system. |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 1001 | Future<Map<String, dynamic>> writeDevFSFile( |
| 1002 | String fsName, { |
Alexandre Ardhuin | 2de61a0 | 2017-03-31 18:34:13 +0200 | [diff] [blame] | 1003 | @required String path, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 1004 | @required List<int> fileContents, |
Devon Carew | b2938f40 | 2016-06-10 15:08:12 -0700 | [diff] [blame] | 1005 | }) { |
| 1006 | assert(path != null); |
| 1007 | assert(fileContents != null); |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1008 | return invokeRpcRaw( |
| 1009 | '_writeDevFSFile', |
| 1010 | params: <String, dynamic>{ |
| 1011 | 'fsName': fsName, |
| 1012 | 'path': path, |
Jason Simmons | 466d154 | 2018-03-12 11:06:32 -0700 | [diff] [blame] | 1013 | 'fileContents': base64.encode(fileContents), |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1014 | }, |
| 1015 | ); |
Devon Carew | b2938f40 | 2016-06-10 15:08:12 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
John McCutchan | 0de6916 | 2016-07-20 12:59:30 -0700 | [diff] [blame] | 1018 | // Read one file from a file system. |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1019 | Future<List<int>> readDevFSFile(String fsName, String path) async { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 1020 | final Map<String, dynamic> response = await invokeRpcRaw( |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1021 | '_readDevFSFile', |
| 1022 | params: <String, dynamic>{ |
| 1023 | 'fsName': fsName, |
| 1024 | 'path': path, |
| 1025 | }, |
| 1026 | ); |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1027 | return base64.decode(response['fileContents'] as String); |
Devon Carew | b2938f40 | 2016-06-10 15:08:12 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Devon Carew | b2938f40 | 2016-06-10 15:08:12 -0700 | [diff] [blame] | 1030 | /// The complete list of a file system. |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1031 | Future<List<String>> listDevFSFiles(String fsName) async { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1032 | return (await invokeRpcRaw('_listDevFSFiles', params: <String, dynamic>{'fsName': fsName}))['files'] as List<String>; |
Devon Carew | b2938f40 | 2016-06-10 15:08:12 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /// Delete an existing file system. |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1036 | Future<Map<String, dynamic>> deleteDevFS(String fsName) { |
Alexandre Ardhuin | a6af422 | 2019-03-20 23:23:31 +0100 | [diff] [blame] | 1037 | return invokeRpcRaw('_deleteDevFS', params: <String, dynamic>{'fsName': fsName}); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 1040 | Future<ServiceMap> runInView( |
| 1041 | String viewId, |
| 1042 | Uri main, |
| 1043 | Uri packages, |
| 1044 | Uri assetsDirectory, |
| 1045 | ) { |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 1046 | return invokeRpc<ServiceMap>('_flutter.runInView', |
Alexandre Ardhuin | bfa1d25 | 2019-03-23 00:02:21 +0100 | [diff] [blame] | 1047 | params: <String, dynamic>{ |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 1048 | 'viewId': viewId, |
Danny Tuppeny | d89604d | 2018-10-02 18:31:55 +0100 | [diff] [blame] | 1049 | 'mainScript': main.toString(), |
| 1050 | 'packagesFile': packages.toString(), |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 1051 | 'assetDirectory': assetsDirectory.toString(), |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 1052 | }); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | Future<Map<String, dynamic>> clearVMTimeline() { |
Siva | f496595 | 2019-07-02 16:10:04 -0700 | [diff] [blame] | 1056 | return invokeRpcRaw('clearVMTimeline'); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1059 | Future<Map<String, dynamic>> setVMTimelineFlags(List<String> recordedStreams) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1060 | assert(recordedStreams != null); |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1061 | return invokeRpcRaw( |
Siva | f496595 | 2019-07-02 16:10:04 -0700 | [diff] [blame] | 1062 | 'setVMTimelineFlags', |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1063 | params: <String, dynamic>{ |
| 1064 | 'recordedStreams': recordedStreams, |
| 1065 | }, |
| 1066 | ); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | Future<Map<String, dynamic>> getVMTimeline() { |
Siva | f496595 | 2019-07-02 16:10:04 -0700 | [diff] [blame] | 1070 | return invokeRpcRaw('getVMTimeline'); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 1073 | Future<void> refreshViews({ bool waitForViews = false }) async { |
| 1074 | assert(waitForViews != null); |
| 1075 | assert(loaded); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1076 | if (!isFlutterEngine) { |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 1077 | return; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1078 | } |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 1079 | int failCount = 0; |
| 1080 | while (true) { |
| 1081 | _viewCache.clear(); |
| 1082 | // When the future returned by invokeRpc() below returns, |
| 1083 | // the _viewCache will have been updated. |
| 1084 | // This message updates all the views of every isolate. |
Zachary Anderson | 22ca3f9 | 2019-06-12 11:18:53 -0700 | [diff] [blame] | 1085 | await vmService.vm.invokeRpc<ServiceObject>( |
| 1086 | '_flutter.listViews', truncateLogs: false); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1087 | if (_viewCache.values.isNotEmpty || !waitForViews) { |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 1088 | return; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1089 | } |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 1090 | failCount += 1; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1091 | if (failCount == 5) { // waited 200ms |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 1092 | globals.printStatus('Flutter is taking longer than expected to report its views. Still trying...'); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1093 | } |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 1094 | await Future<void>.delayed(const Duration(milliseconds: 50)); |
| 1095 | await reload(); |
| 1096 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Zachary Anderson | 9a068c6 | 2017-03-16 14:35:53 -0700 | [diff] [blame] | 1099 | Iterable<FlutterView> get views => _viewCache.values; |
| 1100 | |
| 1101 | FlutterView get firstView { |
Jason Simmons | 59cacd7 | 2017-02-01 11:54:27 -0800 | [diff] [blame] | 1102 | return _viewCache.values.isEmpty ? null : _viewCache.values.first; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1103 | } |
Zachary Anderson | 9a068c6 | 2017-03-16 14:35:53 -0700 | [diff] [blame] | 1104 | |
Zachary Anderson | 9fdd4f4 | 2017-04-14 11:33:47 -0700 | [diff] [blame] | 1105 | List<FlutterView> allViewsWithName(String isolateFilter) { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1106 | if (_viewCache.values.isEmpty) { |
Zachary Anderson | 9a068c6 | 2017-03-16 14:35:53 -0700 | [diff] [blame] | 1107 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1108 | } |
Zachary Anderson | 9fdd4f4 | 2017-04-14 11:33:47 -0700 | [diff] [blame] | 1109 | return _viewCache.values.where( |
| 1110 | (FlutterView v) => v.uiIsolate.name.contains(isolateFilter) |
| 1111 | ).toList(); |
Zachary Anderson | 9a068c6 | 2017-03-16 14:35:53 -0700 | [diff] [blame] | 1112 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Zachary Anderson | a036980 | 2017-04-14 21:18:48 -0700 | [diff] [blame] | 1115 | class HeapSpace extends ServiceObject { |
| 1116 | HeapSpace._empty(ServiceObjectOwner owner) : super._empty(owner); |
| 1117 | |
| 1118 | int _used = 0; |
| 1119 | int _capacity = 0; |
| 1120 | int _external = 0; |
| 1121 | int _collections = 0; |
| 1122 | double _totalCollectionTimeInSeconds = 0.0; |
| 1123 | double _averageCollectionPeriodInMillis = 0.0; |
| 1124 | |
| 1125 | int get used => _used; |
| 1126 | int get capacity => _capacity; |
| 1127 | int get external => _external; |
| 1128 | |
| 1129 | Duration get avgCollectionTime { |
| 1130 | final double mcs = _totalCollectionTimeInSeconds * |
Jason Simmons | 466d154 | 2018-03-12 11:06:32 -0700 | [diff] [blame] | 1131 | Duration.microsecondsPerSecond / |
Zachary Anderson | a036980 | 2017-04-14 21:18:48 -0700 | [diff] [blame] | 1132 | math.max(_collections, 1); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 1133 | return Duration(microseconds: mcs.ceil()); |
Zachary Anderson | a036980 | 2017-04-14 21:18:48 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | Duration get avgCollectionPeriod { |
| 1137 | final double mcs = _averageCollectionPeriodInMillis * |
Jason Simmons | 466d154 | 2018-03-12 11:06:32 -0700 | [diff] [blame] | 1138 | Duration.microsecondsPerMillisecond; |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 1139 | return Duration(microseconds: mcs.ceil()); |
Zachary Anderson | a036980 | 2017-04-14 21:18:48 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | @override |
| 1143 | void _update(Map<String, dynamic> map, bool mapIsRef) { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1144 | _used = map['used'] as int; |
| 1145 | _capacity = map['capacity'] as int; |
| 1146 | _external = map['external'] as int; |
| 1147 | _collections = map['collections'] as int; |
| 1148 | _totalCollectionTimeInSeconds = map['time'] as double; |
| 1149 | _averageCollectionPeriodInMillis = map['avgCollectionPeriodMillis'] as double; |
Zachary Anderson | a036980 | 2017-04-14 21:18:48 -0700 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1153 | /// An isolate running inside the VM. Instances of the Isolate class are always |
| 1154 | /// canonicalized. |
| 1155 | class Isolate extends ServiceObjectOwner { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1156 | Isolate._empty(VM owner) : super._empty(owner); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1157 | |
| 1158 | @override |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1159 | VM get vm => owner as VM; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1160 | |
| 1161 | @override |
| 1162 | VMService get vmService => vm.vmService; |
| 1163 | |
| 1164 | @override |
| 1165 | Isolate get isolate => this; |
| 1166 | |
| 1167 | DateTime startTime; |
Devon Carew | 7fb6646 | 2017-05-02 16:09:57 -0700 | [diff] [blame] | 1168 | |
| 1169 | /// The last pause event delivered to the isolate. If the isolate is running, |
| 1170 | /// this will be a resume event. |
John McCutchan | 9112823 | 2016-10-20 07:30:24 -0700 | [diff] [blame] | 1171 | ServiceEvent pauseEvent; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1172 | |
Alexandre Ardhuin | 329e52c | 2017-03-08 23:57:31 +0100 | [diff] [blame] | 1173 | final Map<String, ServiceObject> _cache = <String, ServiceObject>{}; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1174 | |
Zachary Anderson | a036980 | 2017-04-14 21:18:48 -0700 | [diff] [blame] | 1175 | HeapSpace _newSpace; |
| 1176 | HeapSpace _oldSpace; |
| 1177 | |
| 1178 | HeapSpace get newSpace => _newSpace; |
| 1179 | HeapSpace get oldSpace => _oldSpace; |
| 1180 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1181 | @override |
| 1182 | ServiceObject getFromMap(Map<String, dynamic> map) { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1183 | if (map == null) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1184 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1185 | } |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1186 | final String mapType = _stripRef(map['type'] as String); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1187 | if (mapType == 'Isolate') { |
| 1188 | // There are sometimes isolate refs in ServiceEvents. |
| 1189 | return vm.getFromMap(map); |
| 1190 | } |
| 1191 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1192 | final String mapId = map['id'] as String; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1193 | ServiceObject serviceObject = (mapId != null) ? _cache[mapId] : null; |
| 1194 | if (serviceObject != null) { |
Alexander Aprelev | 0f3aa50 | 2018-02-09 10:33:24 -0800 | [diff] [blame] | 1195 | serviceObject.updateFromMap(map); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1196 | return serviceObject; |
| 1197 | } |
| 1198 | // Build the object from the map directly. |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 1199 | serviceObject = ServiceObject._fromMap(this, map); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1200 | if ((serviceObject != null) && serviceObject.canCache) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1201 | _cache[mapId] = serviceObject; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1202 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1203 | return serviceObject; |
| 1204 | } |
| 1205 | |
| 1206 | @override |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 1207 | Future<Map<String, dynamic>> _fetchDirect() => invokeRpcRaw('getIsolate'); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1208 | |
| 1209 | /// Invoke the RPC and return the raw response. |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 1210 | Future<Map<String, dynamic>> invokeRpcRaw( |
| 1211 | String method, { |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1212 | Map<String, dynamic> params, |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1213 | }) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1214 | // Inject the 'isolateId' parameter. |
| 1215 | if (params == null) { |
| 1216 | params = <String, dynamic>{ |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 1217 | 'isolateId': id, |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1218 | }; |
| 1219 | } else { |
| 1220 | params['isolateId'] = id; |
| 1221 | } |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1222 | return vm.invokeRpcRaw(method, params: params); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | /// Invoke the RPC and return a ServiceObject response. |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1226 | Future<ServiceObject> invokeRpc(String method, Map<String, dynamic> params) async { |
| 1227 | return getFromMap(await invokeRpcRaw(method, params: params)); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1228 | } |
| 1229 | |
Zachary Anderson | a036980 | 2017-04-14 21:18:48 -0700 | [diff] [blame] | 1230 | void _updateHeaps(Map<String, dynamic> map, bool mapIsRef) { |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 1231 | _newSpace ??= HeapSpace._empty(this); |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1232 | _newSpace._update(castStringKeyedMap(map['new']), mapIsRef); |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 1233 | _oldSpace ??= HeapSpace._empty(this); |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1234 | _oldSpace._update(castStringKeyedMap(map['old']), mapIsRef); |
Zachary Anderson | a036980 | 2017-04-14 21:18:48 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1237 | @override |
| 1238 | void _update(Map<String, dynamic> map, bool mapIsRef) { |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1239 | if (mapIsRef) { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1240 | return; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1241 | } |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1242 | _loaded = true; |
| 1243 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1244 | final int startTimeMillis = map['startTime'] as int; |
Alexandre Ardhuin | d927c93 | 2018-09-12 08:29:29 +0200 | [diff] [blame] | 1245 | startTime = DateTime.fromMillisecondsSinceEpoch(startTimeMillis); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1246 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1247 | _upgradeCollection(map, this); |
John McCutchan | 9112823 | 2016-10-20 07:30:24 -0700 | [diff] [blame] | 1248 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1249 | pauseEvent = map['pauseEvent'] as ServiceEvent; |
Zachary Anderson | a036980 | 2017-04-14 21:18:48 -0700 | [diff] [blame] | 1250 | |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1251 | _updateHeaps(castStringKeyedMap(map['_heaps']), mapIsRef); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Alexandre Ardhuin | 10f721c | 2018-01-25 19:28:22 +0100 | [diff] [blame] | 1254 | static const int kIsolateReloadBarred = 1005; |
John McCutchan | 852a00a | 2016-08-25 14:23:13 -0700 | [diff] [blame] | 1255 | |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 1256 | Future<Map<String, dynamic>> reloadSources({ |
| 1257 | bool pause = false, |
| 1258 | Uri rootLibUri, |
| 1259 | Uri packagesUri, |
| 1260 | }) async { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1261 | try { |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 1262 | final Map<String, dynamic> arguments = <String, dynamic>{ |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 1263 | 'pause': pause, |
John McCutchan | 26432eb | 2016-11-22 14:16:20 -0800 | [diff] [blame] | 1264 | }; |
Michael Goderbauer | 17057bb | 2017-03-01 10:11:56 -0800 | [diff] [blame] | 1265 | if (rootLibUri != null) { |
Alexander Aprelev | e4b7e87 | 2018-08-30 12:08:23 -0700 | [diff] [blame] | 1266 | arguments['rootLibUri'] = rootLibUri.toString(); |
John McCutchan | 26432eb | 2016-11-22 14:16:20 -0800 | [diff] [blame] | 1267 | } |
Michael Goderbauer | 17057bb | 2017-03-01 10:11:56 -0800 | [diff] [blame] | 1268 | if (packagesUri != null) { |
Alexander Aprelev | e4b7e87 | 2018-08-30 12:08:23 -0700 | [diff] [blame] | 1269 | arguments['packagesUri'] = packagesUri.toString(); |
John McCutchan | 26432eb | 2016-11-22 14:16:20 -0800 | [diff] [blame] | 1270 | } |
Chris Bracken | 7a09316 | 2017-03-03 17:50:46 -0800 | [diff] [blame] | 1271 | final Map<String, dynamic> response = await invokeRpcRaw('_reloadSources', params: arguments); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1272 | return response; |
Alexandre Ardhuin | 3c379aa | 2018-02-05 22:20:21 +0100 | [diff] [blame] | 1273 | } on rpc.RpcException catch (e) { |
Jonah Williams | 8e6205f | 2019-08-07 17:00:36 -0700 | [diff] [blame] | 1274 | return Future<Map<String, dynamic>>.value(<String, dynamic>{ |
John McCutchan | 852a00a | 2016-08-25 14:23:13 -0700 | [diff] [blame] | 1275 | 'code': e.code, |
| 1276 | 'message': e.message, |
| 1277 | 'data': e.data, |
| 1278 | }); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1279 | } |
Devon Carew | b2938f40 | 2016-06-10 15:08:12 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Ryan Macnak | 07a4b4c | 2017-10-12 15:58:40 -0700 | [diff] [blame] | 1282 | Future<Map<String, dynamic>> getObject(Map<String, dynamic> objectRef) { |
| 1283 | return invokeRpcRaw('getObject', |
| 1284 | params: <String, dynamic>{'objectId': objectRef['id']}); |
| 1285 | } |
| 1286 | |
John McCutchan | 2cf50ce | 2017-04-18 13:27:53 -0700 | [diff] [blame] | 1287 | /// Resumes the isolate. |
| 1288 | Future<Map<String, dynamic>> resume() { |
| 1289 | return invokeRpcRaw('resume'); |
| 1290 | } |
| 1291 | |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 1292 | // Flutter extension methods. |
| 1293 | |
John McCutchan | 24ab837 | 2016-09-15 13:18:32 -0700 | [diff] [blame] | 1294 | // Invoke a flutter extension method, if the flutter extension is not |
| 1295 | // available, returns null. |
| 1296 | Future<Map<String, dynamic>> invokeFlutterExtensionRpcRaw( |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1297 | String method, { |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 1298 | Map<String, dynamic> params, |
| 1299 | }) async { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1300 | try { |
| 1301 | return await invokeRpcRaw(method, params: params); |
| 1302 | } on rpc.RpcException catch (e) { |
| 1303 | // If an application is not using the framework |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1304 | if (e.code == rpc_error_code.METHOD_NOT_FOUND) { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1305 | return null; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1306 | } |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1307 | rethrow; |
| 1308 | } |
John McCutchan | 24ab837 | 2016-09-15 13:18:32 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1311 | Future<Map<String, dynamic>> flutterDebugDumpApp() { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1312 | return invokeFlutterExtensionRpcRaw('ext.flutter.debugDumpApp'); |
Devon Carew | ec752d8 | 2016-07-04 11:21:56 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1315 | Future<Map<String, dynamic>> flutterDebugDumpRenderTree() { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1316 | return invokeFlutterExtensionRpcRaw('ext.flutter.debugDumpRenderTree'); |
Devon Carew | ec752d8 | 2016-07-04 11:21:56 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Ian Hickson | db84df2 | 2017-05-12 15:51:58 -0700 | [diff] [blame] | 1319 | Future<Map<String, dynamic>> flutterDebugDumpLayerTree() { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1320 | return invokeFlutterExtensionRpcRaw('ext.flutter.debugDumpLayerTree'); |
Ian Hickson | db84df2 | 2017-05-12 15:51:58 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
Yegor | d354096 | 2018-04-23 14:23:49 -0700 | [diff] [blame] | 1323 | Future<Map<String, dynamic>> flutterDebugDumpSemanticsTreeInTraversalOrder() { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1324 | return invokeFlutterExtensionRpcRaw('ext.flutter.debugDumpSemanticsTreeInTraversalOrder'); |
Michael Goderbauer | 8ecf19d | 2017-08-28 15:49:16 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | Future<Map<String, dynamic>> flutterDebugDumpSemanticsTreeInInverseHitTestOrder() { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1328 | return invokeFlutterExtensionRpcRaw('ext.flutter.debugDumpSemanticsTreeInInverseHitTestOrder'); |
Ian Hickson | db84df2 | 2017-05-12 15:51:58 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Ian Hickson | e1adc52 | 2017-07-19 12:57:22 -0700 | [diff] [blame] | 1331 | Future<Map<String, dynamic>> _flutterToggle(String name) async { |
| 1332 | Map<String, dynamic> state = await invokeFlutterExtensionRpcRaw('ext.flutter.$name'); |
Ian Hickson | f17e3f4 | 2017-02-08 14:24:25 -0800 | [diff] [blame] | 1333 | if (state != null && state.containsKey('enabled') && state['enabled'] is String) { |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1334 | state = await invokeFlutterExtensionRpcRaw( |
Ian Hickson | e1adc52 | 2017-07-19 12:57:22 -0700 | [diff] [blame] | 1335 | 'ext.flutter.$name', |
Alexandre Ardhuin | a6af422 | 2019-03-20 23:23:31 +0100 | [diff] [blame] | 1336 | params: <String, dynamic>{'enabled': state['enabled'] == 'true' ? 'false' : 'true'}, |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1337 | ); |
| 1338 | } |
Ian Hickson | 27cceff | 2016-11-27 23:50:20 -0800 | [diff] [blame] | 1339 | return state; |
| 1340 | } |
| 1341 | |
Ian Hickson | e1adc52 | 2017-07-19 12:57:22 -0700 | [diff] [blame] | 1342 | Future<Map<String, dynamic>> flutterToggleDebugPaintSizeEnabled() => _flutterToggle('debugPaint'); |
| 1343 | |
Dan Field | d2790bd | 2019-04-10 14:57:46 -0700 | [diff] [blame] | 1344 | Future<Map<String, dynamic>> flutterToggleDebugCheckElevationsEnabled() => _flutterToggle('debugCheckElevationsEnabled'); |
| 1345 | |
Ian Hickson | e1adc52 | 2017-07-19 12:57:22 -0700 | [diff] [blame] | 1346 | Future<Map<String, dynamic>> flutterTogglePerformanceOverlayOverride() => _flutterToggle('showPerformanceOverlay'); |
| 1347 | |
Jacob Richman | ab9ba3f | 2018-04-16 10:04:40 -0700 | [diff] [blame] | 1348 | Future<Map<String, dynamic>> flutterToggleWidgetInspector() => _flutterToggle('inspector.show'); |
Jacob Richman | 5462ddb | 2017-08-21 16:17:54 -0700 | [diff] [blame] | 1349 | |
Jonah Williams | cadde24 | 2019-04-11 12:44:33 -0700 | [diff] [blame] | 1350 | Future<Map<String, dynamic>> flutterToggleProfileWidgetBuilds() => _flutterToggle('profileWidgetBuilds'); |
| 1351 | |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1352 | Future<Map<String, dynamic>> flutterDebugAllowBanner(bool show) { |
| 1353 | return invokeFlutterExtensionRpcRaw( |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1354 | 'ext.flutter.debugAllowBanner', |
Alexandre Ardhuin | a6af422 | 2019-03-20 23:23:31 +0100 | [diff] [blame] | 1355 | params: <String, dynamic>{'enabled': show ? 'true' : 'false'}, |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1356 | ); |
Ian Hickson | 5f38773 | 2017-02-01 22:47:53 -0800 | [diff] [blame] | 1357 | } |
| 1358 | |
Alexander Aprelev | 5fc6b87 | 2018-10-29 18:51:57 -0700 | [diff] [blame] | 1359 | Future<Map<String, dynamic>> flutterReassemble() { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1360 | return invokeFlutterExtensionRpcRaw('ext.flutter.reassemble'); |
| 1361 | } |
| 1362 | |
Jonah Williams | 81aa271 | 2019-12-09 21:31:34 -0800 | [diff] [blame] | 1363 | Future<Map<String, dynamic>> flutterFastReassemble(String classId) { |
| 1364 | return invokeFlutterExtensionRpcRaw('ext.flutter.fastReassemble', params: <String, Object>{ |
| 1365 | 'class': classId, |
| 1366 | }); |
| 1367 | } |
| 1368 | |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1369 | Future<bool> flutterAlreadyPaintedFirstUsefulFrame() async { |
liyuqian | e77237d | 2019-07-31 11:01:52 -0700 | [diff] [blame] | 1370 | final Map<String, dynamic> result = await invokeFlutterExtensionRpcRaw('ext.flutter.didSendFirstFrameRasterizedEvent'); |
liyuqian | 8e6f468 | 2019-02-26 16:38:59 -0800 | [diff] [blame] | 1371 | // result might be null when the service extension is not initialized |
| 1372 | return result != null && result['enabled'] == 'true'; |
John McCutchan | 9543366 | 2016-08-09 11:27:12 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Alexander Aprelev | 5fc6b87 | 2018-10-29 18:51:57 -0700 | [diff] [blame] | 1375 | Future<Map<String, dynamic>> uiWindowScheduleFrame() { |
| 1376 | return invokeFlutterExtensionRpcRaw('ext.ui.window.scheduleFrame'); |
John McCutchan | 24ab837 | 2016-09-15 13:18:32 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Alexander Aprelev | 5fc6b87 | 2018-10-29 18:51:57 -0700 | [diff] [blame] | 1379 | Future<Map<String, dynamic>> flutterEvictAsset(String assetPath) { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1380 | return invokeFlutterExtensionRpcRaw( |
| 1381 | 'ext.flutter.evict', |
Ian Hickson | dc634e1 | 2017-02-02 15:48:35 -0800 | [diff] [blame] | 1382 | params: <String, dynamic>{ |
| 1383 | 'value': assetPath, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 1384 | }, |
John McCutchan | 24ab837 | 2016-09-15 13:18:32 -0700 | [diff] [blame] | 1385 | ); |
| 1386 | } |
| 1387 | |
Stanislav Baranov | eb7a59b | 2018-12-20 16:07:36 -0800 | [diff] [blame] | 1388 | Future<List<int>> flutterDebugSaveCompilationTrace() async { |
Stanislav Baranov | c5251cd | 2018-12-14 16:09:17 -0800 | [diff] [blame] | 1389 | final Map<String, dynamic> result = |
Stanislav Baranov | eb7a59b | 2018-12-20 16:07:36 -0800 | [diff] [blame] | 1390 | await invokeFlutterExtensionRpcRaw('ext.flutter.saveCompilationTrace'); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1391 | if (result != null && result['value'] is List<dynamic>) { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1392 | return (result['value'] as List<dynamic>).cast<int>(); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1393 | } |
Stanislav Baranov | c5251cd | 2018-12-14 16:09:17 -0800 | [diff] [blame] | 1394 | return null; |
| 1395 | } |
| 1396 | |
John McCutchan | 24ab837 | 2016-09-15 13:18:32 -0700 | [diff] [blame] | 1397 | // Application control extension methods. |
Alexander Aprelev | 5fc6b87 | 2018-10-29 18:51:57 -0700 | [diff] [blame] | 1398 | Future<Map<String, dynamic>> flutterExit() { |
Ian Hickson | 31a9626 | 2019-01-19 00:31:05 -0800 | [diff] [blame] | 1399 | return invokeFlutterExtensionRpcRaw('ext.flutter.exit'); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1400 | } |
Ian Hickson | f9c2d7d | 2017-02-17 17:47:49 -0800 | [diff] [blame] | 1401 | |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 1402 | Future<String> flutterPlatformOverride([ String platform ]) async { |
Devon Carew | 9d9836f | 2018-07-09 12:22:46 -0700 | [diff] [blame] | 1403 | final Map<String, dynamic> result = await invokeFlutterExtensionRpcRaw( |
Ian Hickson | f9c2d7d | 2017-02-17 17:47:49 -0800 | [diff] [blame] | 1404 | 'ext.flutter.platformOverride', |
Alexandre Ardhuin | a6af422 | 2019-03-20 23:23:31 +0100 | [diff] [blame] | 1405 | params: platform != null ? <String, dynamic>{'value': platform} : <String, String>{}, |
Ian Hickson | f9c2d7d | 2017-02-17 17:47:49 -0800 | [diff] [blame] | 1406 | ); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1407 | if (result != null && result['value'] is String) { |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1408 | return result['value'] as String; |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1409 | } |
Ian Hickson | f9c2d7d | 2017-02-17 17:47:49 -0800 | [diff] [blame] | 1410 | return 'unknown'; |
| 1411 | } |
Devon Carew | 990dae8 | 2017-07-27 11:47:33 -0700 | [diff] [blame] | 1412 | |
| 1413 | @override |
| 1414 | String toString() => 'Isolate $id'; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | class ServiceMap extends ServiceObject implements Map<String, dynamic> { |
| 1418 | ServiceMap._empty(ServiceObjectOwner owner) : super._empty(owner); |
| 1419 | |
Alexandre Ardhuin | 329e52c | 2017-03-08 23:57:31 +0100 | [diff] [blame] | 1420 | final Map<String, dynamic> _map = <String, dynamic>{}; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1421 | |
| 1422 | @override |
| 1423 | void _update(Map<String, dynamic> map, bool mapIsRef) { |
| 1424 | _loaded = !mapIsRef; |
| 1425 | _upgradeCollection(map, owner); |
| 1426 | _map.clear(); |
| 1427 | _map.addAll(map); |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 1428 | } |
Devon Carew | ec75177 | 2016-05-26 15:26:14 -0700 | [diff] [blame] | 1429 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1430 | // Forward Map interface calls. |
| 1431 | @override |
| 1432 | void addAll(Map<String, dynamic> other) => _map.addAll(other); |
| 1433 | @override |
| 1434 | void clear() => _map.clear(); |
| 1435 | @override |
| 1436 | bool containsValue(dynamic v) => _map.containsValue(v); |
| 1437 | @override |
Adam Barth | 2c21d79 | 2016-10-07 11:27:54 -0700 | [diff] [blame] | 1438 | bool containsKey(Object k) => _map.containsKey(k); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1439 | @override |
Jacob Richman | 53fc96d | 2017-02-06 08:55:09 -0800 | [diff] [blame] | 1440 | void forEach(void f(String key, dynamic value)) => _map.forEach(f); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1441 | @override |
Jacob Richman | 53fc96d | 2017-02-06 08:55:09 -0800 | [diff] [blame] | 1442 | dynamic putIfAbsent(String key, dynamic ifAbsent()) => _map.putIfAbsent(key, ifAbsent); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1443 | @override |
Adam Barth | 2c21d79 | 2016-10-07 11:27:54 -0700 | [diff] [blame] | 1444 | void remove(Object key) => _map.remove(key); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1445 | @override |
Adam Barth | 2c21d79 | 2016-10-07 11:27:54 -0700 | [diff] [blame] | 1446 | dynamic operator [](Object k) => _map[k]; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1447 | @override |
| 1448 | void operator []=(String k, dynamic v) => _map[k] = v; |
| 1449 | @override |
| 1450 | bool get isEmpty => _map.isEmpty; |
| 1451 | @override |
| 1452 | bool get isNotEmpty => _map.isNotEmpty; |
| 1453 | @override |
| 1454 | Iterable<String> get keys => _map.keys; |
| 1455 | @override |
| 1456 | Iterable<dynamic> get values => _map.values; |
| 1457 | @override |
| 1458 | int get length => _map.length; |
| 1459 | @override |
| 1460 | String toString() => _map.toString(); |
Alexander Aprelev | 0f3aa50 | 2018-02-09 10:33:24 -0800 | [diff] [blame] | 1461 | @override |
| 1462 | void addEntries(Iterable<MapEntry<String, dynamic>> entries) => _map.addEntries(entries); |
| 1463 | @override |
| 1464 | Map<RK, RV> cast<RK, RV>() => _map.cast<RK, RV>(); |
| 1465 | @override |
| 1466 | void removeWhere(bool test(String key, dynamic value)) => _map.removeWhere(test); |
| 1467 | @override |
Alexandre Ardhuin | f62afdc | 2018-10-01 21:29:08 +0200 | [diff] [blame] | 1468 | Map<K2, V2> map<K2, V2>(MapEntry<K2, V2> transform(String key, dynamic value)) => _map.map<K2, V2>(transform); |
Alexander Aprelev | 0f3aa50 | 2018-02-09 10:33:24 -0800 | [diff] [blame] | 1469 | @override |
| 1470 | Iterable<MapEntry<String, dynamic>> get entries => _map.entries; |
| 1471 | @override |
| 1472 | void updateAll(dynamic update(String key, dynamic value)) => _map.updateAll(update); |
Michael Goderbauer | 6d20ff2 | 2019-01-30 08:56:12 -0800 | [diff] [blame] | 1473 | Map<RK, RV> retype<RK, RV>() => _map.cast<RK, RV>(); |
Alexander Aprelev | 0f3aa50 | 2018-02-09 10:33:24 -0800 | [diff] [blame] | 1474 | @override |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 1475 | dynamic update(String key, dynamic update(dynamic value), { dynamic ifAbsent() }) => _map.update(key, update, ifAbsent: ifAbsent); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1476 | } |
Devon Carew | ec75177 | 2016-05-26 15:26:14 -0700 | [diff] [blame] | 1477 | |
Mikkel Nygaard Ravn | 9b3f50b | 2018-07-04 11:07:14 +0200 | [diff] [blame] | 1478 | /// Peered to an Android/iOS FlutterView widget on a device. |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1479 | class FlutterView extends ServiceObject { |
| 1480 | FlutterView._empty(ServiceObjectOwner owner) : super._empty(owner); |
| 1481 | |
| 1482 | Isolate _uiIsolate; |
| 1483 | Isolate get uiIsolate => _uiIsolate; |
| 1484 | |
| 1485 | @override |
| 1486 | void _update(Map<String, dynamic> map, bool mapIsRef) { |
| 1487 | _loaded = !mapIsRef; |
| 1488 | _upgradeCollection(map, owner); |
Alexandre Ardhuin | adc7351 | 2019-11-19 07:57:42 +0100 | [diff] [blame] | 1489 | _uiIsolate = map['isolate'] as Isolate; |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | // TODO(johnmccutchan): Report errors when running failed. |
Alexandre Ardhuin | 5169ab5 | 2019-02-21 09:27:07 +0100 | [diff] [blame] | 1493 | Future<void> runFromSource( |
| 1494 | Uri entryUri, |
| 1495 | Uri packagesUri, |
| 1496 | Uri assetsDirectoryUri, |
| 1497 | ) async { |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1498 | final String viewId = id; |
| 1499 | // When this completer completes the isolate is running. |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 1500 | final Completer<void> completer = Completer<void>(); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1501 | final StreamSubscription<ServiceEvent> subscription = |
Alexander Aprelev | 09ca3c1 | 2018-04-18 01:21:33 -0700 | [diff] [blame] | 1502 | (await owner.vm.vmService.onIsolateEvent).listen((ServiceEvent event) { |
Alexandre Ardhuin | 440ce8f | 2019-03-07 21:09:28 +0100 | [diff] [blame] | 1503 | // TODO(johnmccutchan): Listen to the debug stream and catch initial |
| 1504 | // launch errors. |
| 1505 | if (event.kind == ServiceEvent.kIsolateRunnable) { |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 1506 | globals.printTrace('Isolate is runnable.'); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1507 | if (!completer.isCompleted) { |
Alexandre Ardhuin | 440ce8f | 2019-03-07 21:09:28 +0100 | [diff] [blame] | 1508 | completer.complete(); |
Zachary Anderson | e2340c6 | 2019-09-13 14:51:35 -0700 | [diff] [blame] | 1509 | } |
Alexandre Ardhuin | 440ce8f | 2019-03-07 21:09:28 +0100 | [diff] [blame] | 1510 | } |
| 1511 | }); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1512 | await owner.vm.runInView(viewId, |
Michael Goderbauer | e4f586d | 2017-03-02 20:05:47 -0800 | [diff] [blame] | 1513 | entryUri, |
| 1514 | packagesUri, |
| 1515 | assetsDirectoryUri); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1516 | await completer.future; |
Ian Hickson | 6bfd9bb | 2018-11-08 20:54:06 -0800 | [diff] [blame] | 1517 | await owner.vm.refreshViews(waitForViews: true); |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1518 | await subscription.cancel(); |
Devon Carew | ec75177 | 2016-05-26 15:26:14 -0700 | [diff] [blame] | 1519 | } |
| 1520 | |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 1521 | Future<void> setAssetDirectory(Uri assetsDirectory) async { |
Alexander Aprelev | 8da5af5 | 2018-01-04 14:27:25 -0800 | [diff] [blame] | 1522 | assert(assetsDirectory != null); |
Alexandre Ardhuin | f62afdc | 2018-10-01 21:29:08 +0200 | [diff] [blame] | 1523 | await owner.vmService.vm.invokeRpc<ServiceObject>('_flutter.setAssetBundlePath', |
Alexander Aprelev | 8da5af5 | 2018-01-04 14:27:25 -0800 | [diff] [blame] | 1524 | params: <String, dynamic>{ |
Alexander Aprelev | 89cb5d2 | 2018-05-17 11:37:36 -0700 | [diff] [blame] | 1525 | 'isolateId': _uiIsolate.id, |
Alexander Aprelev | 8da5af5 | 2018-01-04 14:27:25 -0800 | [diff] [blame] | 1526 | 'viewId': id, |
Alexandre Ardhuin | 387f885 | 2019-03-01 08:17:55 +0100 | [diff] [blame] | 1527 | 'assetDirectory': assetsDirectory.toFilePath(windows: false), |
Alexander Aprelev | 8da5af5 | 2018-01-04 14:27:25 -0800 | [diff] [blame] | 1528 | }); |
| 1529 | } |
| 1530 | |
Dan Field | e3acb5c | 2019-07-08 16:38:49 -0700 | [diff] [blame] | 1531 | Future<void> setSemanticsEnabled(bool enabled) async { |
| 1532 | assert(enabled != null); |
| 1533 | await owner.vmService.vm.invokeRpc<ServiceObject>('_flutter.setSemanticsEnabled', |
| 1534 | params: <String, dynamic>{ |
| 1535 | 'isolateId': _uiIsolate.id, |
| 1536 | 'viewId': id, |
| 1537 | 'enabled': enabled, |
| 1538 | }); |
| 1539 | } |
| 1540 | |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1541 | bool get hasIsolate => _uiIsolate != null; |
| 1542 | |
Alexandre Ardhuin | 2d3ff10 | 2018-10-05 07:54:56 +0200 | [diff] [blame] | 1543 | Future<void> flushUIThreadTasks() async { |
Alexander Aprelev | 89cb5d2 | 2018-05-17 11:37:36 -0700 | [diff] [blame] | 1544 | await owner.vm.invokeRpcRaw('_flutter.flushUIThreadTasks', |
| 1545 | params: <String, dynamic>{'isolateId': _uiIsolate.id}); |
Carlo Bernaschina | 741598d | 2017-07-19 16:54:10 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
Devon Carew | ec75177 | 2016-05-26 15:26:14 -0700 | [diff] [blame] | 1548 | @override |
John McCutchan | 3a012b3 | 2016-08-17 09:01:04 -0700 | [diff] [blame] | 1549 | String toString() => id; |
Devon Carew | 40c0d6e | 2016-05-12 18:15:23 -0700 | [diff] [blame] | 1550 | } |