| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library dart._vmservice; | 5 library dart._vmservice; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:convert'; | 9 import 'dart:convert'; |
| 10 import 'dart:developer' show ServiceProtocolInfo; |
| 10 import 'dart:isolate'; | 11 import 'dart:isolate'; |
| 11 import 'dart:typed_data'; | 12 import 'dart:typed_data'; |
| 12 | 13 |
| 13 part 'asset.dart'; | 14 part 'asset.dart'; |
| 14 part 'client.dart'; | 15 part 'client.dart'; |
| 15 part 'devfs.dart'; | 16 part 'devfs.dart'; |
| 16 part 'constants.dart'; | 17 part 'constants.dart'; |
| 17 part 'running_isolate.dart'; | 18 part 'running_isolate.dart'; |
| 18 part 'running_isolates.dart'; | 19 part 'running_isolates.dart'; |
| 19 part 'message.dart'; | 20 part 'message.dart'; |
| 20 part 'message_router.dart'; | 21 part 'message_router.dart'; |
| 21 | 22 |
| 22 final RawReceivePort isolateLifecyclePort = new RawReceivePort(); | 23 final RawReceivePort isolateControlPort = new RawReceivePort(); |
| 23 final RawReceivePort scriptLoadPort = new RawReceivePort(); | 24 final RawReceivePort scriptLoadPort = new RawReceivePort(); |
| 24 | 25 |
| 25 abstract class IsolateEmbedderData { | 26 abstract class IsolateEmbedderData { |
| 26 void cleanup(); | 27 void cleanup(); |
| 27 } | 28 } |
| 28 | 29 |
| 29 // This is for use by the embedder. It is a map from the isolateId to | 30 // This is for use by the embedder. It is a map from the isolateId to |
| 30 // anything implementing IsolateEmbedderData. When an isolate goes away, | 31 // anything implementing IsolateEmbedderData. When an isolate goes away, |
| 31 // the cleanup method will be invoked after being removed from the map. | 32 // the cleanup method will be invoked after being removed from the map. |
| 32 final Map<int, IsolateEmbedderData> isolateEmbedderData = | 33 final Map<int, IsolateEmbedderData> isolateEmbedderData = |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 119 |
| 119 /// Called to write a stream into a file. | 120 /// Called to write a stream into a file. |
| 120 typedef Future WriteStreamFileCallback(Uri path, Stream<List<int>> bytes); | 121 typedef Future WriteStreamFileCallback(Uri path, Stream<List<int>> bytes); |
| 121 | 122 |
| 122 /// Called to read a file. | 123 /// Called to read a file. |
| 123 typedef Future<List<int>> ReadFileCallback(Uri path); | 124 typedef Future<List<int>> ReadFileCallback(Uri path); |
| 124 | 125 |
| 125 /// Called to list all files under some path. | 126 /// Called to list all files under some path. |
| 126 typedef Future<List<Map<String,String>>> ListFilesCallback(Uri path); | 127 typedef Future<List<Map<String,String>>> ListFilesCallback(Uri path); |
| 127 | 128 |
| 129 /// Called when we need information about the server. |
| 130 typedef Future<Uri> ServerInformationCallback(); |
| 131 |
| 132 /// Called when we want to [enable] or disable the web server. |
| 133 typedef Future<Uri> WebServerControlCallback(bool enable); |
| 134 |
| 128 /// Hooks that are setup by the embedder. | 135 /// Hooks that are setup by the embedder. |
| 129 class VMServiceEmbedderHooks { | 136 class VMServiceEmbedderHooks { |
| 130 static ServerStartCallback serverStart; | 137 static ServerStartCallback serverStart; |
| 131 static ServerStopCallback serverStop; | 138 static ServerStopCallback serverStop; |
| 132 static CleanupCallback cleanup; | 139 static CleanupCallback cleanup; |
| 133 static CreateTempDirCallback createTempDir; | 140 static CreateTempDirCallback createTempDir; |
| 134 static DeleteDirCallback deleteDir; | 141 static DeleteDirCallback deleteDir; |
| 135 static WriteFileCallback writeFile; | 142 static WriteFileCallback writeFile; |
| 136 static WriteStreamFileCallback writeStreamFile; | 143 static WriteStreamFileCallback writeStreamFile; |
| 137 static ReadFileCallback readFile; | 144 static ReadFileCallback readFile; |
| 138 static ListFilesCallback listFiles; | 145 static ListFilesCallback listFiles; |
| 146 static ServerInformationCallback serverInformation; |
| 147 static WebServerControlCallback webServerControl; |
| 139 } | 148 } |
| 140 | 149 |
| 141 class VMService extends MessageRouter { | 150 class VMService extends MessageRouter { |
| 142 static VMService _instance; | 151 static VMService _instance; |
| 143 | 152 |
| 144 /// Collection of currently connected clients. | 153 /// Collection of currently connected clients. |
| 145 final Set<Client> clients = new Set<Client>(); | 154 final Set<Client> clients = new Set<Client>(); |
| 146 | 155 |
| 147 /// Collection of currently running isolates. | 156 /// Collection of currently running isolates. |
| 148 RunningIsolates runningIsolates = new RunningIsolates(); | 157 RunningIsolates runningIsolates = new RunningIsolates(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 case Constants.ISOLATE_SHUTDOWN_MESSAGE_ID: | 196 case Constants.ISOLATE_SHUTDOWN_MESSAGE_ID: |
| 188 runningIsolates.isolateShutdown(portId, sp); | 197 runningIsolates.isolateShutdown(portId, sp); |
| 189 IsolateEmbedderData ied = isolateEmbedderData.remove(portId); | 198 IsolateEmbedderData ied = isolateEmbedderData.remove(portId); |
| 190 if (ied != null) { | 199 if (ied != null) { |
| 191 ied.cleanup(); | 200 ied.cleanup(); |
| 192 } | 201 } |
| 193 break; | 202 break; |
| 194 } | 203 } |
| 195 } | 204 } |
| 196 | 205 |
| 206 Future<Null> _serverMessageHandler(int code, SendPort sp, bool enable) async { |
| 207 switch (code) { |
| 208 case Constants.WEB_SERVER_CONTROL_MESSAGE_ID: |
| 209 if (VMServiceEmbedderHooks.webServerControl == null) { |
| 210 sp.send(null); |
| 211 return; |
| 212 } |
| 213 Uri uri = await VMServiceEmbedderHooks.webServerControl(enable); |
| 214 sp.send(uri); |
| 215 break; |
| 216 case Constants.SERVER_INFO_MESSAGE_ID: |
| 217 if (VMServiceEmbedderHooks.serverInformation == null) { |
| 218 sp.send(null); |
| 219 return; |
| 220 } |
| 221 Uri uri = await VMServiceEmbedderHooks.serverInformation(); |
| 222 sp.send(uri); |
| 223 break; |
| 224 } |
| 225 } |
| 226 |
| 197 Future _exit() async { | 227 Future _exit() async { |
| 198 // Stop the server. | 228 // Stop the server. |
| 199 if (VMServiceEmbedderHooks.serverStop != null) { | 229 if (VMServiceEmbedderHooks.serverStop != null) { |
| 200 await VMServiceEmbedderHooks.serverStop(); | 230 await VMServiceEmbedderHooks.serverStop(); |
| 201 } | 231 } |
| 202 | 232 |
| 203 // Close receive ports. | 233 // Close receive ports. |
| 204 isolateLifecyclePort.close(); | 234 isolateControlPort.close(); |
| 205 scriptLoadPort.close(); | 235 scriptLoadPort.close(); |
| 206 | 236 |
| 207 // Create a copy of the set as a list because client.disconnect() will | 237 // Create a copy of the set as a list because client.disconnect() will |
| 208 // alter the connected clients set. | 238 // alter the connected clients set. |
| 209 var clientsList = clients.toList(); | 239 var clientsList = clients.toList(); |
| 210 for (var client in clientsList) { | 240 for (var client in clientsList) { |
| 211 client.disconnect(); | 241 client.disconnect(); |
| 212 } | 242 } |
| 213 devfs.cleanup(); | 243 devfs.cleanup(); |
| 214 if (VMServiceEmbedderHooks.cleanup != null) { | 244 if (VMServiceEmbedderHooks.cleanup != null) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 226 assert(message[1] is String || message[1] is Uint8List); | 256 assert(message[1] is String || message[1] is Uint8List); |
| 227 _eventMessageHandler(message); | 257 _eventMessageHandler(message); |
| 228 return; | 258 return; |
| 229 } | 259 } |
| 230 if (message.length == 1) { | 260 if (message.length == 1) { |
| 231 // This is a control message directing the vm service to exit. | 261 // This is a control message directing the vm service to exit. |
| 232 assert(message[0] == Constants.SERVICE_EXIT_MESSAGE_ID); | 262 assert(message[0] == Constants.SERVICE_EXIT_MESSAGE_ID); |
| 233 _exit(); | 263 _exit(); |
| 234 return; | 264 return; |
| 235 } | 265 } |
| 266 if (message.length == 3) { |
| 267 // This is a message interacting with the web server. |
| 268 assert((message[0] == Constants.WEB_SERVER_CONTROL_MESSAGE_ID) || |
| 269 (message[0] == Constants.SERVER_INFO_MESSAGE_ID)); |
| 270 _serverMessageHandler(message[0], message[1], message[2]); |
| 271 return; |
| 272 } |
| 236 if (message.length == 4) { | 273 if (message.length == 4) { |
| 237 // This is a message informing us of the birth or death of an | 274 // This is a message informing us of the birth or death of an |
| 238 // isolate. | 275 // isolate. |
| 239 _controlMessageHandler(message[0], message[1], message[2], message[3]); | 276 _controlMessageHandler(message[0], message[1], message[2], message[3]); |
| 240 return; | 277 return; |
| 241 } | 278 } |
| 242 } | 279 } |
| 243 print('Internal vm-service error: ignoring illegal message: $message'); | 280 print('Internal vm-service error: ignoring illegal message: $message'); |
| 244 } | 281 } |
| 245 | 282 |
| 246 VMService._internal() | 283 VMService._internal() |
| 247 : eventPort = isolateLifecyclePort { | 284 : eventPort = isolateControlPort { |
| 248 eventPort.handler = messageHandler; | 285 eventPort.handler = messageHandler; |
| 249 } | 286 } |
| 250 | 287 |
| 251 factory VMService() { | 288 factory VMService() { |
| 252 if (VMService._instance == null) { | 289 if (VMService._instance == null) { |
| 253 VMService._instance = new VMService._internal(); | 290 VMService._instance = new VMService._internal(); |
| 254 _onStart(); | 291 _onStart(); |
| 255 } | 292 } |
| 256 return _instance; | 293 return _instance; |
| 257 } | 294 } |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 return devfs.handleMessage(message); | 455 return devfs.handleMessage(message); |
| 419 } | 456 } |
| 420 if (message.params['isolateId'] != null) { | 457 if (message.params['isolateId'] != null) { |
| 421 return runningIsolates.route(message); | 458 return runningIsolates.route(message); |
| 422 } | 459 } |
| 423 return message.sendToVM(); | 460 return message.sendToVM(); |
| 424 } | 461 } |
| 425 } | 462 } |
| 426 | 463 |
| 427 RawReceivePort boot() { | 464 RawReceivePort boot() { |
| 428 // Return the port we expect isolate startup and shutdown messages on. | 465 // Return the port we expect isolate control messages on. |
| 429 return isolateLifecyclePort; | 466 return isolateControlPort; |
| 430 } | 467 } |
| 431 | 468 |
| 432 void _registerIsolate(int port_id, SendPort sp, String name) { | 469 void _registerIsolate(int port_id, SendPort sp, String name) { |
| 433 var service = new VMService(); | 470 var service = new VMService(); |
| 434 service.runningIsolates.isolateStartup(port_id, sp, name); | 471 service.runningIsolates.isolateStartup(port_id, sp, name); |
| 435 } | 472 } |
| 436 | 473 |
| 437 /// Notify the VM that the service is running. | 474 /// Notify the VM that the service is running. |
| 438 external void _onStart(); | 475 external void _onStart(); |
| 439 | 476 |
| 440 /// Notify the VM that the service is no longer running. | 477 /// Notify the VM that the service is no longer running. |
| 441 external void _onExit(); | 478 external void _onExit(); |
| 442 | 479 |
| 443 /// Notify the VM that the server's address has changed. | 480 /// Notify the VM that the server's address has changed. |
| 444 external void onServerAddressChange(String address); | 481 external void onServerAddressChange(String address); |
| 445 | 482 |
| 446 /// Subscribe to a service stream. | 483 /// Subscribe to a service stream. |
| 447 external bool _vmListenStream(String streamId); | 484 external bool _vmListenStream(String streamId); |
| 448 | 485 |
| 449 /// Cancel a subscription to a service stream. | 486 /// Cancel a subscription to a service stream. |
| 450 external void _vmCancelStream(String streamId); | 487 external void _vmCancelStream(String streamId); |
| 451 | 488 |
| 452 /// Get the bytes to the tar archive. | 489 /// Get the bytes to the tar archive. |
| 453 external Uint8List _requestAssets(); | 490 external Uint8List _requestAssets(); |
| 454 | 491 |
| 455 /// Notify the vm service that an isolate has been spawned via rpc. | 492 /// Notify the vm service that an isolate has been spawned via rpc. |
| 456 external void _spawnUriNotify(obj, String token); | 493 external void _spawnUriNotify(obj, String token); |
| OLD | NEW |