Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 service_common; | 5 library service_common; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:typed_data'; | |
| 9 | 10 |
| 10 import 'package:logging/logging.dart'; | 11 import 'package:logging/logging.dart'; |
| 11 import 'package:observatory/service.dart'; | 12 import 'package:observatory/service.dart'; |
| 12 | 13 |
| 13 // Export the service library. | 14 // Export the service library. |
| 14 export 'package:observatory/service.dart'; | 15 export 'package:observatory/service.dart'; |
| 15 | 16 |
| 16 /// Description of a VM target. | 17 /// Description of a VM target. |
| 17 class WebSocketVMTarget { | 18 class WebSocketVMTarget { |
| 18 // Last time this VM has been connected to. | 19 // Last time this VM has been connected to. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 /// Minimal common interface for 'WebSocket' in [dart:io] and [dart:html]. | 63 /// Minimal common interface for 'WebSocket' in [dart:io] and [dart:html]. |
| 63 abstract class CommonWebSocket { | 64 abstract class CommonWebSocket { |
| 64 void connect(String address, | 65 void connect(String address, |
| 65 void onOpen(), | 66 void onOpen(), |
| 66 void onMessage(dynamic data), | 67 void onMessage(dynamic data), |
| 67 void onError(), | 68 void onError(), |
| 68 void onClose()); | 69 void onClose()); |
| 69 bool get isOpen; | 70 bool get isOpen; |
| 70 void send(dynamic data); | 71 void send(dynamic data); |
| 71 void close(); | 72 void close(); |
| 73 Future<ByteData> nonStringToByteData(dynamic data); | |
| 72 } | 74 } |
| 73 | 75 |
| 74 /// A [CommonWebSocketVM] communicates with a Dart VM over a CommonWebSocket. | 76 /// A [CommonWebSocketVM] communicates with a Dart VM over a CommonWebSocket. |
| 75 /// The Dart VM can be embedded in Chromium or standalone. In the case of | 77 /// The Dart VM can be embedded in Chromium or standalone. In the case of |
| 76 /// Chromium, we make the service requests via the Chrome Remote Debugging | 78 /// Chromium, we make the service requests via the Chrome Remote Debugging |
| 77 /// Protocol. | 79 /// Protocol. |
| 78 abstract class CommonWebSocketVM extends VM { | 80 abstract class CommonWebSocketVM extends VM { |
| 79 final Completer _connected = new Completer(); | 81 final Completer _connected = new Completer(); |
| 80 final Completer _disconnected = new Completer(); | 82 final Completer _disconnected = new Completer(); |
| 81 final WebSocketVMTarget target; | 83 final WebSocketVMTarget target; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 | 155 |
| 154 // WebSocket open event handler. | 156 // WebSocket open event handler. |
| 155 void _onOpen() { | 157 void _onOpen() { |
| 156 target.lastConnectionTime = new DateTime.now().millisecondsSinceEpoch; | 158 target.lastConnectionTime = new DateTime.now().millisecondsSinceEpoch; |
| 157 _sendAllDelayedRequests(); | 159 _sendAllDelayedRequests(); |
| 158 _notifyConnect(); | 160 _notifyConnect(); |
| 159 } | 161 } |
| 160 | 162 |
| 161 // WebSocket message event handler. | 163 // WebSocket message event handler. |
| 162 void _onMessage(dynamic data) { | 164 void _onMessage(dynamic data) { |
| 163 assert(data is String); // We don't handle binary data, yet. | 165 if (data is! String) { |
| 166 _webSocket.nonStringToByteData(data).then((ByteData bytes) { | |
| 167 // See format spec. in VMs Service::SendEvent. | |
| 168 int offset = 0; | |
| 169 int metaSize = bytes.getUint64(offset, Endianness.BIG_ENDIAN); | |
| 170 offset += 8; | |
| 171 var meta = new Utf8Decoder().convert(new Uint8List.view( | |
|
Cutch
2014/08/15 21:33:58
Can we reuse the Utf8Decoder?
koda
2014/08/15 22:20:53
Done.
| |
| 172 bytes.buffer, bytes.offsetInBytes + offset, metaSize)); | |
| 173 offset += metaSize; | |
| 174 var data = new ByteData.view( | |
| 175 bytes.buffer, bytes.offsetInBytes + offset); | |
| 176 postEventMessage(meta, data); | |
| 177 }); | |
| 178 return; | |
| 179 } | |
| 164 var map = JSON.decode(data); | 180 var map = JSON.decode(data); |
| 165 if (map == null) { | 181 if (map == null) { |
| 166 Logger.root.severe('WebSocketVM got empty message'); | 182 Logger.root.severe('WebSocketVM got empty message'); |
| 167 return; | 183 return; |
| 168 } | 184 } |
| 169 // Extract serial and response. | 185 // Extract serial and response. |
| 170 var serial; | 186 var serial; |
| 171 var response; | 187 var response; |
| 172 if (target.chrome) { | 188 if (target.chrome) { |
| 173 if (map['method'] != 'Dart.observatoryData') { | 189 if (map['method'] != 'Dart.observatoryData') { |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 'query': request.id | 273 'query': request.id |
| 258 } | 274 } |
| 259 }); | 275 }); |
| 260 } else { | 276 } else { |
| 261 message = JSON.encode({'seq': serial, 'request': request.id}); | 277 message = JSON.encode({'seq': serial, 'request': request.id}); |
| 262 } | 278 } |
| 263 // Send message. | 279 // Send message. |
| 264 _webSocket.send(message); | 280 _webSocket.send(message); |
| 265 } | 281 } |
| 266 } | 282 } |
| OLD | NEW |