| 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_html; | 5 library service_html; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:html'; | 9 import 'dart:html'; |
| 10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 }); | 51 }); |
| 52 } | 52 } |
| 53 } | 53 } |
| 54 | 54 |
| 55 /// The [WebSocketVM] communicates with a Dart VM over WebSocket. The Dart VM | 55 /// The [WebSocketVM] communicates with a Dart VM over WebSocket. The Dart VM |
| 56 /// can be embedded in Chromium or standalone. In the case of Chromium, we | 56 /// can be embedded in Chromium or standalone. In the case of Chromium, we |
| 57 /// make the service requests via the Chrome Remote Debugging Protocol. | 57 /// make the service requests via the Chrome Remote Debugging Protocol. |
| 58 class WebSocketVM extends CommonWebSocketVM { | 58 class WebSocketVM extends CommonWebSocketVM { |
| 59 WebSocketVM(WebSocketVMTarget target) : super(target, new _HtmlWebSocket()); | 59 WebSocketVM(WebSocketVMTarget target) : super(target, new _HtmlWebSocket()); |
| 60 } | 60 } |
| 61 | |
| 62 // A VM that communicates with the service via posting messages from DevTools. | |
| 63 class PostMessageVM extends VM { | |
| 64 final Completer _connected = new Completer(); | |
| 65 final Completer _disconnected = new Completer(); | |
| 66 void disconnect() { /* nope */ } | |
| 67 Future get onConnect => _connected.future; | |
| 68 Future get onDisconnect => _disconnected.future; | |
| 69 final Map<String, Completer> _pendingRequests = | |
| 70 new Map<String, Completer>(); | |
| 71 int _requestSerial = 0; | |
| 72 | |
| 73 PostMessageVM() : super() { | |
| 74 window.onMessage.listen(_messageHandler); | |
| 75 _connected.complete(this); | |
| 76 } | |
| 77 | |
| 78 void _messageHandler(msg) { | |
| 79 var id = msg.data['id']; | |
| 80 var name = msg.data['name']; | |
| 81 var data = msg.data['data']; | |
| 82 if (name != 'observatoryData') { | |
| 83 return; | |
| 84 } | |
| 85 var completer = _pendingRequests[id]; | |
| 86 assert(completer != null); | |
| 87 _pendingRequests.remove(id); | |
| 88 completer.complete(data); | |
| 89 } | |
| 90 | |
| 91 Future<String> getString(String path) { | |
| 92 var idString = '$_requestSerial'; | |
| 93 Map message = {}; | |
| 94 message['id'] = idString; | |
| 95 message['method'] = 'observatoryQuery'; | |
| 96 message['query'] = '$path'; | |
| 97 _requestSerial++; | |
| 98 var completer = new Completer(); | |
| 99 _pendingRequests[idString] = completer; | |
| 100 window.parent.postMessage(JSON.encode(message), '*'); | |
| 101 return completer.future; | |
| 102 } | |
| 103 } | |
| OLD | NEW |