| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library service_html; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:convert'; | |
| 9 import 'dart:html'; | |
| 10 | |
| 11 import 'package:observatory/service_common.dart'; | |
| 12 | |
| 13 // Export the service library. | |
| 14 export 'package:observatory/service_common.dart'; | |
| 15 | |
| 16 class _HtmlWebSocket implements CommonWebSocket { | |
| 17 WebSocket _webSocket; | |
| 18 | |
| 19 void connect(String address, | |
| 20 void onOpen(), | |
| 21 void onMessage(dynamic data), | |
| 22 void onError(), | |
| 23 void onClose()) { | |
| 24 _webSocket = new WebSocket(address); | |
| 25 _webSocket.onClose.listen((CloseEvent) => onClose()); | |
| 26 _webSocket.onError.listen((Event) => onError()); | |
| 27 _webSocket.onOpen.listen((Event) => onOpen()); | |
| 28 _webSocket.onMessage.listen((MessageEvent event) => onMessage(event.data)); | |
| 29 } | |
| 30 | |
| 31 bool get isOpen => _webSocket.readyState == WebSocket.OPEN; | |
| 32 | |
| 33 void send(dynamic data) { | |
| 34 _webSocket.send(data); | |
| 35 } | |
| 36 | |
| 37 void close() { | |
| 38 _webSocket.close(); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 /// The [WebSocketVM] communicates with a Dart VM over WebSocket. The Dart VM | |
| 43 /// can be embedded in Chromium or standalone. In the case of Chromium, we | |
| 44 /// make the service requests via the Chrome Remote Debugging Protocol. | |
| 45 class WebSocketVM extends CommonWebSocketVM { | |
| 46 WebSocketVM(WebSocketVMTarget target) : super(target, new _HtmlWebSocket()); | |
| 47 } | |
| 48 | |
| 49 // A VM that communicates with the service via posting messages from DevTools. | |
| 50 class PostMessageVM extends VM { | |
| 51 final Completer _connected = new Completer(); | |
| 52 final Completer _disconnected = new Completer(); | |
| 53 void disconnect() { /* nope */ } | |
| 54 Future get onConnect => _connected.future; | |
| 55 Future get onDisconnect => _disconnected.future; | |
| 56 final Map<String, Completer> _pendingRequests = | |
| 57 new Map<String, Completer>(); | |
| 58 int _requestSerial = 0; | |
| 59 | |
| 60 PostMessageVM() : super() { | |
| 61 window.onMessage.listen(_messageHandler); | |
| 62 _connected.complete(this); | |
| 63 } | |
| 64 | |
| 65 void _messageHandler(msg) { | |
| 66 var id = msg.data['id']; | |
| 67 var name = msg.data['name']; | |
| 68 var data = msg.data['data']; | |
| 69 if (name != 'observatoryData') { | |
| 70 return; | |
| 71 } | |
| 72 var completer = _pendingRequests[id]; | |
| 73 assert(completer != null); | |
| 74 _pendingRequests.remove(id); | |
| 75 completer.complete(data); | |
| 76 } | |
| 77 | |
| 78 Future<String> getString(String path) { | |
| 79 var idString = '$_requestSerial'; | |
| 80 Map message = {}; | |
| 81 message['id'] = idString; | |
| 82 message['method'] = 'observatoryQuery'; | |
| 83 message['query'] = '$path'; | |
| 84 _requestSerial++; | |
| 85 var completer = new Completer(); | |
| 86 _pendingRequests[idString] = completer; | |
| 87 window.parent.postMessage(JSON.encode(message), '*'); | |
| 88 return completer.future; | |
| 89 } | |
| 90 } | |
| OLD | NEW |