| 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:logging/logging.dart'; |
| 12 import 'package:observatory/service.dart'; |
| 13 |
| 14 // Export the service library. |
| 15 export 'package:observatory/service.dart'; |
| 16 |
| 17 class HttpVM extends VM { |
| 18 final String address; |
| 19 |
| 20 HttpVM(this.address) : super(); |
| 21 |
| 22 Future<String> getString(String id) { |
| 23 Logger.root.info('Fetching $id from $address'); |
| 24 return HttpRequest.getString(address + id).catchError((error) { |
| 25 // If we get an error here, the network request has failed. |
| 26 Logger.root.severe('HttpRequest.getString failed.'); |
| 27 return JSON.encode({ |
| 28 'type': 'Error', |
| 29 'id': '', |
| 30 'kind': 'NetworkError', |
| 31 'message': 'Could not connect to service. Check that you started the' |
| 32 ' VM with the following flags:\n --enable-vm-service' |
| 33 ' --pin-isolates' |
| 34 }); |
| 35 }); |
| 36 } |
| 37 } |
| 38 |
| 39 class DartiumVM extends VM { |
| 40 final Map _outstandingRequests = new Map(); |
| 41 int _requestSerial = 0; |
| 42 |
| 43 DartiumVM() : super() { |
| 44 window.onMessage.listen(_messageHandler); |
| 45 Logger.root.info('Connected to DartiumVM'); |
| 46 } |
| 47 |
| 48 void _messageHandler(msg) { |
| 49 var id = msg.data['id']; |
| 50 var name = msg.data['name']; |
| 51 var data = msg.data['data']; |
| 52 if (name != 'observatoryData') { |
| 53 return; |
| 54 } |
| 55 var completer = _outstandingRequests[id]; |
| 56 assert(completer != null); |
| 57 _outstandingRequests.remove(id); |
| 58 completer.complete(data); |
| 59 } |
| 60 |
| 61 Future<String> getString(String path) { |
| 62 var idString = '$_requestSerial'; |
| 63 Map message = {}; |
| 64 message['id'] = idString; |
| 65 message['method'] = 'observatoryQuery'; |
| 66 message['query'] = '/$path'; |
| 67 _requestSerial++; |
| 68 var completer = new Completer(); |
| 69 _outstandingRequests[idString] = completer; |
| 70 window.parent.postMessage(JSON.encode(message), '*'); |
| 71 return completer.future; |
| 72 } |
| 73 } |
| OLD | NEW |