| 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 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // that the Observatory is loaded from. | 25 // that the Observatory is loaded from. |
| 26 host = 'http://${window.location.host}/'; | 26 host = 'http://${window.location.host}/'; |
| 27 } else { | 27 } else { |
| 28 // Otherwise, assume we are running from the Dart Editor and | 28 // Otherwise, assume we are running from the Dart Editor and |
| 29 // want to connect on the default port. | 29 // want to connect on the default port. |
| 30 host = 'http://127.0.0.1:8181/'; | 30 host = 'http://127.0.0.1:8181/'; |
| 31 } | 31 } |
| 32 } | 32 } |
| 33 | 33 |
| 34 Future<String> getString(String id) { | 34 Future<String> getString(String id) { |
| 35 // Ensure we don't request host//id. |
| 36 if (host.endsWith('/') && id.startsWith('/')) { |
| 37 id = id.substring(1); |
| 38 } |
| 35 Logger.root.info('Fetching $id from $host'); | 39 Logger.root.info('Fetching $id from $host'); |
| 36 return HttpRequest.getString(host + id).catchError((error) { | 40 return HttpRequest.request(host + id, |
| 41 requestHeaders: { |
| 42 'Observatory-Version': '1.0' |
| 43 }).then((HttpRequest request) { |
| 44 return request.responseText; |
| 45 }).catchError((error) { |
| 37 // If we get an error here, the network request has failed. | 46 // If we get an error here, the network request has failed. |
| 38 Logger.root.severe('HttpRequest.getString failed.'); | 47 Logger.root.severe('HttpRequest.request failed.'); |
| 39 var request = error.target; | 48 var request = error.target; |
| 40 return JSON.encode({ | 49 return JSON.encode({ |
| 41 'type': 'ServiceException', | 50 'type': 'ServiceException', |
| 42 'id': '', | 51 'id': '', |
| 43 'response': error.target.responseText, | 52 'response': request.responseText, |
| 44 'kind': 'NetworkException', | 53 'kind': 'NetworkException', |
| 45 'message': 'Could not connect to service. Check that you started the' | 54 'message': 'Could not connect to service (${request.statusText}). ' |
| 46 ' VM with the following flags:\n --enable-vm-service' | 55 'Check that you started the VM with the following flags: ' |
| 47 ' --pause-isolates-on-exit' | 56 '--observe' |
| 48 }); | 57 }); |
| 49 }); | 58 }); |
| 50 } | 59 } |
| 51 } | 60 } |
| 52 | 61 |
| 53 class WebSocketVM extends VM { | 62 class WebSocketVM extends VM { |
| 54 final Map<int, Completer> _pendingRequests = | 63 final Map<int, Completer> _pendingRequests = |
| 55 new Map<int, Completer>(); | 64 new Map<int, Completer>(); |
| 56 int _requestSerial = 0; | 65 int _requestSerial = 0; |
| 57 | 66 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 message['id'] = idString; | 163 message['id'] = idString; |
| 155 message['method'] = 'observatoryQuery'; | 164 message['method'] = 'observatoryQuery'; |
| 156 message['query'] = '/$path'; | 165 message['query'] = '/$path'; |
| 157 _requestSerial++; | 166 _requestSerial++; |
| 158 var completer = new Completer(); | 167 var completer = new Completer(); |
| 159 _pendingRequests[idString] = completer; | 168 _pendingRequests[idString] = completer; |
| 160 window.parent.postMessage(JSON.encode(message), '*'); | 169 window.parent.postMessage(JSON.encode(message), '*'); |
| 161 return completer.future; | 170 return completer.future; |
| 162 } | 171 } |
| 163 } | 172 } |
| OLD | NEW |