| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of vmservice; | 5 part of vmservice; |
| 6 | 6 |
| 7 class Message { | 7 class Message { |
| 8 final Completer _completer = new Completer.sync(); | 8 final Completer _completer = new Completer.sync(); |
| 9 bool get completed => _completer.isCompleted; | 9 bool get completed => _completer.isCompleted; |
| 10 /// Future of response. | 10 /// Future of response. |
| 11 Future<String> get response => _completer.future; | 11 Future<String> get response => _completer.future; |
| 12 | 12 |
| 13 final bool isOld; | |
| 14 | |
| 15 // In new messages. | 13 // In new messages. |
| 16 final String method; | 14 final String method; |
| 17 | 15 |
| 18 // In old messages. | 16 // In old messages. |
| 19 final List path = new List(); | 17 final List path = new List(); |
| 20 | 18 |
| 21 final Map params = new Map(); | 19 final Map params = new Map(); |
| 22 | 20 |
| 23 void _setPath(List<String> pathSegments) { | 21 void _setPath(List<String> pathSegments) { |
| 24 if (pathSegments == null) { | 22 if (pathSegments == null) { |
| 25 return; | 23 return; |
| 26 } | 24 } |
| 27 pathSegments.forEach((String segment) { | 25 pathSegments.forEach((String segment) { |
| 28 if (segment == null || segment == '') { | 26 if (segment == null || segment == '') { |
| 29 return; | 27 return; |
| 30 } | 28 } |
| 31 path.add(segment); | 29 path.add(segment); |
| 32 }); | 30 }); |
| 33 } | 31 } |
| 34 | 32 |
| 35 Message.fromUri(Uri uri) : isOld = true { | 33 Message.fromJsonRpc(this.method, Map rpcParams) { |
| 36 var split = uri.path.split('/'); | |
| 37 if (split.length == 0) { | |
| 38 setErrorResponse('Invalid uri: $uri.'); | |
| 39 return; | |
| 40 } | |
| 41 _setPath(split); | |
| 42 params.addAll(uri.queryParameters); | |
| 43 } | |
| 44 | |
| 45 Message.fromJsonRpc(this.method, Map rpcParams) | |
| 46 : isOld = false { | |
| 47 params.addAll(rpcParams); | 34 params.addAll(rpcParams); |
| 48 } | 35 } |
| 49 | 36 |
| 50 Message.fromMap(Map map) : isOld = true { | |
| 51 _setPath(map['path']); | |
| 52 // TODO - turnidge - change this to params in sender. | |
| 53 if (map['options'] != null) { | |
| 54 params.addAll(map['options']); | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 dynamic toJson() { | 37 dynamic toJson() { |
| 59 return { | 38 return { |
| 60 'path': path, | 39 'path': path, |
| 61 'params': params | 40 'params': params |
| 62 }; | 41 }; |
| 63 } | 42 } |
| 64 | 43 |
| 65 // Calls toString on all non-String elements of [list]. We do this so all | 44 // Calls toString on all non-String elements of [list]. We do this so all |
| 66 // elements in the list are strings, making consumption by C++ simpler. | 45 // elements in the list are strings, making consumption by C++ simpler. |
| 67 // This has a side effect that boolean literal values like true become 'true' | 46 // This has a side effect that boolean literal values like true become 'true' |
| (...skipping 19 matching lines...) Expand all Loading... |
| 87 _completer.completeError(value); | 66 _completer.completeError(value); |
| 88 } else { | 67 } else { |
| 89 _completer.complete(value); | 68 _completer.complete(value); |
| 90 } | 69 } |
| 91 }; | 70 }; |
| 92 var keys = _makeAllString(params.keys.toList(growable:false)); | 71 var keys = _makeAllString(params.keys.toList(growable:false)); |
| 93 var values = _makeAllString(params.values.toList(growable:false)); | 72 var values = _makeAllString(params.values.toList(growable:false)); |
| 94 var request = new List(5) | 73 var request = new List(5) |
| 95 ..[0] = 0 // Make room for OOB message type. | 74 ..[0] = 0 // Make room for OOB message type. |
| 96 ..[1] = receivePort.sendPort | 75 ..[1] = receivePort.sendPort |
| 97 ..[2] = (isOld ? path : method) | 76 ..[2] = method |
| 98 ..[3] = keys | 77 ..[3] = keys |
| 99 ..[4] = values; | 78 ..[4] = values; |
| 100 sendIsolateServiceMessage(sendPort, request); | 79 sendIsolateServiceMessage(sendPort, request); |
| 101 return _completer.future; | 80 return _completer.future; |
| 102 } | 81 } |
| 103 | 82 |
| 104 Future<String> sendToVM() { | 83 Future<String> sendToVM() { |
| 105 final receivePort = new RawReceivePort(); | 84 final receivePort = new RawReceivePort(); |
| 106 receivePort.handler = (value) { | 85 receivePort.handler = (value) { |
| 107 receivePort.close(); | 86 receivePort.close(); |
| 108 if (value is Exception) { | 87 if (value is Exception) { |
| 109 _completer.completeError(value); | 88 _completer.completeError(value); |
| 110 } else { | 89 } else { |
| 111 _completer.complete(value); | 90 _completer.complete(value); |
| 112 } | 91 } |
| 113 }; | 92 }; |
| 114 var keys = _makeAllString(params.keys.toList(growable:false)); | 93 var keys = _makeAllString(params.keys.toList(growable:false)); |
| 115 var values = _makeAllString(params.values.toList(growable:false)); | 94 var values = _makeAllString(params.values.toList(growable:false)); |
| 116 var request = new List(5) | 95 var request = new List(5) |
| 117 ..[0] = 0 // Make room for OOB message type. | 96 ..[0] = 0 // Make room for OOB message type. |
| 118 ..[1] = receivePort.sendPort | 97 ..[1] = receivePort.sendPort |
| 119 ..[2] = (isOld ? path : method) | 98 ..[2] = method |
| 120 ..[3] = keys | 99 ..[3] = keys |
| 121 ..[4] = values; | 100 ..[4] = values; |
| 122 sendRootServiceMessage(request); | 101 sendRootServiceMessage(request); |
| 123 return _completer.future; | 102 return _completer.future; |
| 124 } | 103 } |
| 125 | 104 |
| 126 void setResponse(String response) { | 105 void setResponse(String response) { |
| 127 _completer.complete(response); | 106 _completer.complete(response); |
| 128 } | 107 } |
| 129 | 108 |
| 130 void setErrorResponse(String error) { | 109 void setErrorResponse(String error) { |
| 131 _completer.complete(JSON.encode({ | 110 _completer.complete(JSON.encode({ |
| 132 'type': 'ServiceError', | 111 'type': 'ServiceError', |
| 133 'id': '', | 112 'id': '', |
| 134 'kind': 'RequestError', | 113 'kind': 'RequestError', |
| 135 'message': error, | 114 'message': error, |
| 136 'path': path, | 115 'path': path, |
| 137 'params': params | 116 'params': params |
| 138 })); | 117 })); |
| 139 } | 118 } |
| 140 } | 119 } |
| 141 | 120 |
| 142 void sendIsolateServiceMessage(SendPort sp, List m) | 121 void sendIsolateServiceMessage(SendPort sp, List m) |
| 143 native "VMService_SendIsolateServiceMessage"; | 122 native "VMService_SendIsolateServiceMessage"; |
| 144 | 123 |
| 145 void sendRootServiceMessage(List m) | 124 void sendRootServiceMessage(List m) |
| 146 native "VMService_SendRootServiceMessage"; | 125 native "VMService_SendRootServiceMessage"; |
| OLD | NEW |