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 import 'dart:typed_data'; | |
11 | |
12 import 'package:observatory/service_common.dart'; | |
13 | |
14 // Export the service library. | |
15 export 'package:observatory/service_common.dart'; | |
16 | |
17 class _HtmlWebSocket implements CommonWebSocket { | |
18 WebSocket _webSocket; | |
19 | |
20 void connect(String address, | |
21 void onOpen(), | |
22 void onMessage(dynamic data), | |
23 void onError(), | |
24 void onClose()) { | |
25 _webSocket = new WebSocket(address); | |
26 _webSocket.onClose.listen((CloseEvent) => onClose()); | |
27 _webSocket.onError.listen((Event) => onError()); | |
28 _webSocket.onOpen.listen((Event) => onOpen()); | |
29 _webSocket.onMessage.listen((MessageEvent event) => onMessage(event.data)); | |
30 } | |
31 | |
32 bool get isOpen => _webSocket.readyState == WebSocket.OPEN; | |
33 | |
34 void send(dynamic data) { | |
35 _webSocket.send(data); | |
36 } | |
37 | |
38 void close() { | |
39 _webSocket.close(); | |
40 } | |
41 | |
42 Future<ByteData> nonStringToByteData(dynamic data) { | |
43 assert(data is Blob); | |
44 FileReader fileReader = new FileReader(); | |
45 fileReader.readAsArrayBuffer(data); | |
46 return fileReader.onLoadEnd.first.then((e) { | |
47 var result = fileReader.result; | |
48 return new ByteData.view(result.buffer, | |
49 result.offsetInBytes, | |
50 result.length); | |
51 }); | |
52 } | |
53 } | |
54 | |
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 | |
57 /// make the service requests via the Chrome Remote Debugging Protocol. | |
58 class WebSocketVM extends CommonWebSocketVM { | |
59 WebSocketVM(WebSocketVMTarget target) : super(target, new _HtmlWebSocket()); | |
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 |