Chromium Code Reviews| Index: runtime/bin/vmservice/client/lib/src/service/vm.dart |
| diff --git a/runtime/bin/vmservice/client/lib/src/service/vm.dart b/runtime/bin/vmservice/client/lib/src/service/vm.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3ae98a674a7748ebdc182b192c4ad2d26d2aa019 |
| --- /dev/null |
| +++ b/runtime/bin/vmservice/client/lib/src/service/vm.dart |
| @@ -0,0 +1,105 @@ |
| +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of service; |
| + |
| +abstract class VM extends Observable { |
| + IsolateList _isolates; |
| + @observable IsolateList get isolates => _isolates; |
| + |
| + VM() { |
| + _isolates = new IsolateList(this); |
| + } |
| + |
| + /// Fetch [id] as an [ObservableMap] from the service. |
| + Future<ObservableMap> fetchMap(String id) { |
|
turnidge
2014/03/10 21:03:28
What do you think of renaming this to "getAsMap"?
Cutch
2014/03/11 03:17:48
Done.
|
| + return _fetchString(id).then((response) { |
| + try { |
| + var map = JSON.decode(response); |
| + Logger.root.info('Decoded $id'); |
| + return toObservable(map); |
| + } catch (e, st) { |
| + return toObservable({ |
| + 'type': 'Error', |
| + 'id': '', |
| + 'kind': 'DecodeError', |
| + 'message': '$e', |
| + 'stackTrace': st |
| + }); |
| + } |
| + }).catchError((error) { |
| + if (error is ProgressEvent) { |
| + return toObservable({ |
| + 'type': 'Error', |
| + 'id': '', |
| + 'kind': 'FetchError', |
| + 'message': 'Could not connect to service. Check that you started the' |
| + ' VM with the following flags:\n --enable-vm-service' |
| + ' --pin-isolates' |
| + }); |
| + } |
| + return toObservable({ |
| + 'type': 'Error', |
| + 'id': '', |
| + 'kind': 'FetchError', |
| + 'message': '$error' |
| + }); |
| + }); |
| + } |
| + |
| + /// get [id] and associate it with [isolate]. [isolate] may be null. |
| + Future<ServiceObject> get(Isolate isolate, String id) { |
| + return fetchMap(id).then((m) => _upgradeToServiceObject(this, isolate, m)); |
| + } |
| + |
| + Future<String> _fetchString(String id); |
|
turnidge
2014/03/10 21:03:28
again, "get" and "fetch" mean almost the same thin
Cutch
2014/03/11 03:17:48
Renamed to _getString
|
| +} |
| + |
| + |
| +class HttpVM extends VM { |
| + final String address; |
| + |
| + HttpVM(this.address) : super(); |
| + |
| + Future<String> _fetchString(String id) { |
| + Logger.root.info('Fetching $id from $address'); |
| + return HttpRequest.getString(address + id); |
| + } |
| +} |
| + |
| +class DartiumVM extends VM { |
| + final Map _outstandingRequests = new Map(); |
| + int _requestSerial = 0; |
| + |
| + DartiumVM() : super() { |
| + window.onMessage.listen(_messageHandler); |
| + Logger.root.info('Connected to DartiumVM'); |
| + } |
| + |
| + void _messageHandler(msg) { |
| + var id = msg.data['id']; |
| + var name = msg.data['name']; |
| + var data = msg.data['data']; |
| + if (name != 'observatoryData') { |
| + return; |
| + } |
| + var completer = _outstandingRequests[id]; |
| + assert(completer != null); |
| + _outstandingRequests.remove(id); |
| + completer.complete(data); |
| + } |
| + |
| + Future<String> _fetchString(String path) { |
| + var idString = '$_requestSerial'; |
| + Map message = {}; |
| + message['id'] = idString; |
| + message['method'] = 'observatoryQuery'; |
| + message['query'] = '/$path'; |
| + _requestSerial++; |
| + var completer = new Completer(); |
| + _outstandingRequests[idString] = completer; |
| + window.parent.postMessage(JSON.encode(message), '*'); |
| + return completer.future; |
| + } |
| +} |