Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(454)

Side by Side Diff: sdk/lib/vmservice/vmservice.dart

Issue 1640773005: Make it possible to share the embedder's dart sources for the vmservice (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« runtime/vm/service.cc ('K') | « sdk/lib/vmservice/asset.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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:isolate'; 10 import 'dart:isolate';
11 import 'dart:typed_data'; 11 import 'dart:typed_data';
12 12
13 part 'asset.dart'; 13 part 'asset.dart';
14 part 'client.dart'; 14 part 'client.dart';
15 part 'constants.dart'; 15 part 'constants.dart';
16 part 'running_isolate.dart'; 16 part 'running_isolate.dart';
17 part 'running_isolates.dart'; 17 part 'running_isolates.dart';
18 part 'message.dart'; 18 part 'message.dart';
19 part 'message_router.dart'; 19 part 'message_router.dart';
20 20
21 final RawReceivePort isolateLifecyclePort = new RawReceivePort(); 21 final RawReceivePort isolateLifecyclePort = new RawReceivePort();
22 final RawReceivePort scriptLoadPort = new RawReceivePort(); 22 final RawReceivePort scriptLoadPort = new RawReceivePort();
23 23
24 typedef ShutdownCallback();
25
26 // These must be kept in sync with the declarations in vm/json_stream.h. 24 // These must be kept in sync with the declarations in vm/json_stream.h.
27 const kInvalidParams = -32602; 25 const kInvalidParams = -32602;
28 const kInternalError = -32603; 26 const kInternalError = -32603;
29 const kStreamAlreadySubscribed = 103; 27 const kStreamAlreadySubscribed = 103;
30 const kStreamNotSubscribed = 104; 28 const kStreamNotSubscribed = 104;
31 29
32 var _errorMessages = { 30 var _errorMessages = {
33 kInvalidParams: 'Invalid params', 31 kInvalidParams: 'Invalid params',
34 kInternalError: 'Internal error', 32 kInternalError: 'Internal error',
35 kStreamAlreadySubscribed: 'Stream already subscribed', 33 kStreamAlreadySubscribed: 'Stream already subscribed',
(...skipping 19 matching lines...) Expand all
55 53
56 String encodeResult(Message message, Map result) { 54 String encodeResult(Message message, Map result) {
57 var response = { 55 var response = {
58 'jsonrpc': '2.0', 56 'jsonrpc': '2.0',
59 'id' : message.serial, 57 'id' : message.serial,
60 'result' : result, 58 'result' : result,
61 }; 59 };
62 return JSON.encode(response); 60 return JSON.encode(response);
63 } 61 }
64 62
63 const shortDelay = const Duration(milliseconds: 10);
64
65 /// Called when the server should be started.
66 typedef Future ServerStartCallback();
67
68 /// Called when the server should be stopped.
69 typedef Future ServerStopCallback();
70
71 /// Called when the service is exiting.
72 typedef Future CleanupCallback();
73
74 /// Hooks that are setup by the embedder.
75 class VMServiceEmbedderHooks {
76 static ServerStartCallback serverStart;
77 static ServerStopCallback serverStop;
78 static CleanupCallback cleanup;
79 }
65 80
66 class VMService extends MessageRouter { 81 class VMService extends MessageRouter {
67 static VMService _instance; 82 static VMService _instance;
68 83
69 /// Collection of currently connected clients. 84 /// Collection of currently connected clients.
70 final Set<Client> clients = new Set<Client>(); 85 final Set<Client> clients = new Set<Client>();
71 86
72 /// Collection of currently running isolates. 87 /// Collection of currently running isolates.
73 RunningIsolates runningIsolates = new RunningIsolates(); 88 RunningIsolates runningIsolates = new RunningIsolates();
74 89
75 /// A port used to receive events from the VM. 90 /// A port used to receive events from the VM.
76 final RawReceivePort eventPort; 91 final RawReceivePort eventPort;
77 92
78 ShutdownCallback onShutdown;
79
80 void _addClient(Client client) { 93 void _addClient(Client client) {
81 assert(client.streams.isEmpty); 94 assert(client.streams.isEmpty);
82 clients.add(client); 95 clients.add(client);
83 } 96 }
84 97
85 void _removeClient(Client client) { 98 void _removeClient(Client client) {
86 clients.remove(client); 99 clients.remove(client);
87 for (var streamId in client.streams) { 100 for (var streamId in client.streams) {
88 if (!_isAnyClientSubscribed(streamId)) { 101 if (!_isAnyClientSubscribed(streamId)) {
89 _vmCancelStream(streamId); 102 _vmCancelStream(streamId);
(...skipping 18 matching lines...) Expand all
108 switch (code) { 121 switch (code) {
109 case Constants.ISOLATE_STARTUP_MESSAGE_ID: 122 case Constants.ISOLATE_STARTUP_MESSAGE_ID:
110 runningIsolates.isolateStartup(portId, sp, name); 123 runningIsolates.isolateStartup(portId, sp, name);
111 break; 124 break;
112 case Constants.ISOLATE_SHUTDOWN_MESSAGE_ID: 125 case Constants.ISOLATE_SHUTDOWN_MESSAGE_ID:
113 runningIsolates.isolateShutdown(portId, sp); 126 runningIsolates.isolateShutdown(portId, sp);
114 break; 127 break;
115 } 128 }
116 } 129 }
117 130
118 void _exit() { 131 Future _exit() async {
132 // Stop the server.
133 if (VMServiceEmbedderHooks.serverStop != null) {
134 await VMServiceEmbedderHooks.serverStop();
135 }
136
137 // Close receive ports.
119 isolateLifecyclePort.close(); 138 isolateLifecyclePort.close();
120 scriptLoadPort.close(); 139 scriptLoadPort.close();
140
121 // Create a copy of the set as a list because client.disconnect() will 141 // Create a copy of the set as a list because client.disconnect() will
122 // alter the connected clients set. 142 // alter the connected clients set.
123 var clientsList = clients.toList(); 143 var clientsList = clients.toList();
124 for (var client in clientsList) { 144 for (var client in clientsList) {
125 client.disconnect(); 145 client.disconnect();
126 } 146 }
127 // Call embedder shutdown hook after the internal shutdown. 147
128 if (onShutdown != null) { 148 // Notify the VM that we have exited.
129 onShutdown();
130 }
131 _onExit(); 149 _onExit();
132 } 150 }
133 151
134 void messageHandler(message) { 152 void messageHandler(message) {
135 if (message is List) { 153 if (message is List) {
136 if (message.length == 2) { 154 if (message.length == 2) {
137 // This is an event. 155 // This is an event.
138 assert(message[0] is String); 156 assert(message[0] is String);
139 assert(message[1] is String || message[1] is Uint8List); 157 assert(message[1] is String || message[1] is Uint8List);
140 _eventMessageHandler(message); 158 _eventMessageHandler(message);
141 return; 159 return;
142 } 160 }
143 if (message.length == 1) { 161 if (message.length == 1) {
144 // This is a control message directing the vm service to exit. 162 // This is a control message directing the vm service to exit.
145 assert(message[0] == Constants.SERVICE_EXIT_MESSAGE_ID); 163 assert(message[0] == Constants.SERVICE_EXIT_MESSAGE_ID);
146 _exit(); 164 _exit();
147 return; 165 return;
148 } 166 }
149 if (message.length == 4) { 167 if (message.length == 4) {
150 // This is a message informing us of the birth or death of an 168 // This is a message informing us of the birth or death of an
151 // isolate. 169 // isolate.
152 _controlMessageHandler(message[0], message[1], message[2], message[3]); 170 _controlMessageHandler(message[0], message[1], message[2], message[3]);
153 return; 171 return;
154 } 172 }
155 } 173 }
156 print('Internal vm-service error: ignoring illegal message: $message'); 174 print('Internal vm-service error: ignoring illegal message: $message');
157 } 175 }
158 176
159 void _notSupported(_) {
160 throw new UnimplementedError('Service script loading not supported.');
161 }
162
163 VMService._internal() 177 VMService._internal()
164 : eventPort = isolateLifecyclePort { 178 : eventPort = isolateLifecyclePort {
165 scriptLoadPort.handler = _notSupported;
166 eventPort.handler = messageHandler; 179 eventPort.handler = messageHandler;
167 } 180 }
168 181
169 factory VMService() { 182 factory VMService() {
170 if (VMService._instance == null) { 183 if (VMService._instance == null) {
171 VMService._instance = new VMService._internal(); 184 VMService._instance = new VMService._internal();
172 _onStart(); 185 _onStart();
173 } 186 }
174 return _instance; 187 return _instance;
175 } 188 }
176 189
177 void _clientCollection(Message message) {
178 var members = [];
179 var result = {};
180 clients.forEach((client) {
181 members.add(client.toJson());
182 });
183 result['type'] = 'ClientList';
184 result['members'] = members;
185 message.setResponse(JSON.encode(result));
186 }
187
188 bool _isAnyClientSubscribed(String streamId) { 190 bool _isAnyClientSubscribed(String streamId) {
189 for (var client in clients) { 191 for (var client in clients) {
190 if (client.streams.contains(streamId)) { 192 if (client.streams.contains(streamId)) {
191 return true; 193 return true;
192 } 194 }
193 } 195 }
194 return false; 196 return false;
195 } 197 }
196 198
197 Future<String> _streamListen(Message message) async { 199 Future<String> _streamListen(Message message) async {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 288
287 // Encode the entire crash dump. 289 // Encode the entire crash dump.
288 return encodeResult(message, responses); 290 return encodeResult(message, responses);
289 } 291 }
290 292
291 Future<String> route(Message message) { 293 Future<String> route(Message message) {
292 if (message.completed) { 294 if (message.completed) {
293 return message.response; 295 return message.response;
294 } 296 }
295 // TODO(turnidge): Update to json rpc. BEFORE SUBMIT. 297 // TODO(turnidge): Update to json rpc. BEFORE SUBMIT.
296 if ((message.path.length == 1) && (message.path[0] == 'clients')) {
297 _clientCollection(message);
298 return message.response;
299 }
300 if (message.method == '_getCrashDump') { 298 if (message.method == '_getCrashDump') {
301 return _getCrashDump(message); 299 return _getCrashDump(message);
302 } 300 }
303 if (message.method == 'streamListen') { 301 if (message.method == 'streamListen') {
304 return _streamListen(message); 302 return _streamListen(message);
305 } 303 }
306 if (message.method == 'streamCancel') { 304 if (message.method == 'streamCancel') {
307 return _streamCancel(message); 305 return _streamCancel(message);
308 } 306 }
309 if (message.params['isolateId'] != null) { 307 if (message.params['isolateId'] != null) {
310 return runningIsolates.route(message); 308 return runningIsolates.route(message);
311 } 309 }
312 return message.sendToVM(); 310 return message.sendToVM();
313 } 311 }
314 } 312 }
315 313
316 RawReceivePort boot() { 314 RawReceivePort boot() {
317 // Return the port we expect isolate startup and shutdown messages on. 315 // Return the port we expect isolate startup and shutdown messages on.
318 return isolateLifecyclePort; 316 return isolateLifecyclePort;
319 } 317 }
320 318
321 void _registerIsolate(int port_id, SendPort sp, String name) { 319 void _registerIsolate(int port_id, SendPort sp, String name) {
322 var service = new VMService(); 320 var service = new VMService();
323 service.runningIsolates.isolateStartup(port_id, sp, name); 321 service.runningIsolates.isolateStartup(port_id, sp, name);
324 } 322 }
325 323
324 /// Notify the VM that the service is running.
326 external void _onStart(); 325 external void _onStart();
327 326
327 /// Notify the VM that the service is no longer running.
328 external void _onExit(); 328 external void _onExit();
329 329
330 /// Notify the VM that the server's address has changed.
331 external void onServerAddressChange(String address);
332
333 /// Subscribe to a service stream.
330 external bool _vmListenStream(String streamId); 334 external bool _vmListenStream(String streamId);
331 335
336 /// Cancel a subscription to a service stream.
332 external void _vmCancelStream(String streamId); 337 external void _vmCancelStream(String streamId);
333 338
339 /// Get the bytes to the tar archive.
334 external Uint8List _requestAssets(); 340 external Uint8List _requestAssets();
OLDNEW
« runtime/vm/service.cc ('K') | « sdk/lib/vmservice/asset.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698