| 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 dart._vmservice; | 5 part of dart._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 |
| 10 /// Future of response. | 11 /// Future of response. |
| 11 Future<String> get response => _completer.future; | 12 Future<String> get response => _completer.future; |
| 12 Client client; | 13 Client client; |
| 13 | 14 |
| 14 // Client-side identifier for this message. | 15 // Client-side identifier for this message. |
| 15 final serial; | 16 final serial; |
| 16 | 17 |
| 17 // In new messages. | 18 // In new messages. |
| 18 final String method; | 19 final String method; |
| 19 | 20 |
| 20 // In old messages. | 21 // In old messages. |
| 21 final List path = new List(); | 22 final List path = new List(); |
| 22 | 23 |
| 23 final Map params = new Map(); | 24 final Map params = new Map(); |
| 24 | 25 |
| 25 void _setPath(List<String> pathSegments) { | 26 void _setPath(List<String> pathSegments) { |
| 26 if (pathSegments == null) { | 27 if (pathSegments == null) { |
| 27 return; | 28 return; |
| 28 } | 29 } |
| 29 pathSegments.forEach((String segment) { | 30 pathSegments.forEach((String segment) { |
| 30 if (segment == null || segment == '') { | 31 if (segment == null || segment == '') { |
| 31 return; | 32 return; |
| 32 } | 33 } |
| 33 path.add(segment); | 34 path.add(segment); |
| 34 }); | 35 }); |
| 35 } | 36 } |
| 36 | 37 |
| 37 Message.fromJsonRpc(this.client, Map map) | 38 Message.fromJsonRpc(this.client, Map map) |
| 38 : serial = map['id'], method = map['method'] { | 39 : serial = map['id'], |
| 40 method = map['method'] { |
| 39 if (map['params'] != null) { | 41 if (map['params'] != null) { |
| 40 params.addAll(map['params']); | 42 params.addAll(map['params']); |
| 41 } | 43 } |
| 42 } | 44 } |
| 43 | 45 |
| 44 static String _methodNameFromUri(Uri uri) { | 46 static String _methodNameFromUri(Uri uri) { |
| 45 if (uri == null) { | 47 if (uri == null) { |
| 46 return ''; | 48 return ''; |
| 47 } | 49 } |
| 48 if (uri.pathSegments.length == 0) { | 50 if (uri.pathSegments.length == 0) { |
| 49 return ''; | 51 return ''; |
| 50 } | 52 } |
| 51 return uri.pathSegments[0]; | 53 return uri.pathSegments[0]; |
| 52 } | 54 } |
| 53 | 55 |
| 54 Message.forMethod(String method) | 56 Message.forMethod(String method) |
| 55 : client = null, method = method, serial = ''; | 57 : client = null, |
| 58 method = method, |
| 59 serial = ''; |
| 56 | 60 |
| 57 Message.fromUri(this.client, Uri uri) | 61 Message.fromUri(this.client, Uri uri) |
| 58 : serial = '', method = _methodNameFromUri(uri) { | 62 : serial = '', |
| 63 method = _methodNameFromUri(uri) { |
| 59 params.addAll(uri.queryParameters); | 64 params.addAll(uri.queryParameters); |
| 60 } | 65 } |
| 61 | 66 |
| 62 Message.forIsolate(this.client, Uri uri, RunningIsolate isolate) | 67 Message.forIsolate(this.client, Uri uri, RunningIsolate isolate) |
| 63 : serial = '', method = _methodNameFromUri(uri) { | 68 : serial = '', |
| 69 method = _methodNameFromUri(uri) { |
| 64 params.addAll(uri.queryParameters); | 70 params.addAll(uri.queryParameters); |
| 65 params['isolateId'] = isolate.serviceId; | 71 params['isolateId'] = isolate.serviceId; |
| 66 } | 72 } |
| 67 | 73 |
| 68 Uri toUri() { | 74 Uri toUri() { |
| 69 return new Uri(path: method, queryParameters: params); | 75 return new Uri(path: method, queryParameters: params); |
| 70 } | 76 } |
| 71 | 77 |
| 72 dynamic toJson() { | 78 dynamic toJson() { |
| 73 return { | 79 return {'path': path, 'params': params}; |
| 74 'path': path, | |
| 75 'params': params | |
| 76 }; | |
| 77 } | 80 } |
| 78 | 81 |
| 79 // Calls toString on all non-String elements of [list]. We do this so all | 82 // Calls toString on all non-String elements of [list]. We do this so all |
| 80 // elements in the list are strings, making consumption by C++ simpler. | 83 // elements in the list are strings, making consumption by C++ simpler. |
| 81 // This has a side effect that boolean literal values like true become 'true' | 84 // This has a side effect that boolean literal values like true become 'true' |
| 82 // and thus indistinguishable from the string literal 'true'. | 85 // and thus indistinguishable from the string literal 'true'. |
| 83 List _makeAllString(List list) { | 86 List _makeAllString(List list) { |
| 84 if (list == null) { | 87 if (list == null) { |
| 85 return null; | 88 return null; |
| 86 } | 89 } |
| 87 for (var i = 0; i < list.length; i++) { | 90 for (var i = 0; i < list.length; i++) { |
| 88 if (list[i] is String) { | 91 if (list[i] is String) { |
| 89 continue; | 92 continue; |
| 90 } | 93 } |
| 91 list[i] = list[i].toString(); | 94 list[i] = list[i].toString(); |
| 92 } | 95 } |
| 93 return list; | 96 return list; |
| 94 } | 97 } |
| 95 | 98 |
| 96 Future<String> send(SendPort sendPort) { | 99 Future<String> send(SendPort sendPort) { |
| 97 final receivePort = new RawReceivePort(); | 100 final receivePort = new RawReceivePort(); |
| 98 receivePort.handler = (value) { | 101 receivePort.handler = (value) { |
| 99 receivePort.close(); | 102 receivePort.close(); |
| 100 _completer.complete(value); | 103 _completer.complete(value); |
| 101 }; | 104 }; |
| 102 var keys = _makeAllString(params.keys.toList(growable:false)); | 105 var keys = _makeAllString(params.keys.toList(growable: false)); |
| 103 var values = _makeAllString(params.values.toList(growable:false)); | 106 var values = _makeAllString(params.values.toList(growable: false)); |
| 104 var request = new List(6) | 107 var request = new List(6) |
| 105 ..[0] = 0 // Make room for OOB message type. | 108 ..[0] = 0 // Make room for OOB message type. |
| 106 ..[1] = receivePort.sendPort | 109 ..[1] = receivePort.sendPort |
| 107 ..[2] = serial | 110 ..[2] = serial |
| 108 ..[3] = method | 111 ..[3] = method |
| 109 ..[4] = keys | 112 ..[4] = keys |
| 110 ..[5] = values; | 113 ..[5] = values; |
| 111 if (!sendIsolateServiceMessage(sendPort, request)) { | 114 if (!sendIsolateServiceMessage(sendPort, request)) { |
| 112 receivePort.close(); | 115 receivePort.close(); |
| 113 _completer.complete(JSON.encode({ | 116 _completer.complete(JSON.encode({ |
| 114 'type': 'ServiceError', | 117 'type': 'ServiceError', |
| 115 'id': '', | 118 'id': '', |
| 116 'kind': 'InternalError', | 119 'kind': 'InternalError', |
| 117 'message': 'could not send message [${serial}] to isolate', | 120 'message': 'could not send message [${serial}] to isolate', |
| 118 })); | 121 })); |
| 119 } | 122 } |
| 120 return _completer.future; | 123 return _completer.future; |
| 121 } | 124 } |
| 122 | 125 |
| 123 // We currently support two ways of passing parameters from Dart code to C | 126 // We currently support two ways of passing parameters from Dart code to C |
| 124 // code. The original way always converts the parameters to strings before | 127 // code. The original way always converts the parameters to strings before |
| 125 // passing them over. Our goal is to convert all C handlers to take the | 128 // passing them over. Our goal is to convert all C handlers to take the |
| 126 // parameters as Dart objects but until the conversion is complete, we | 129 // parameters as Dart objects but until the conversion is complete, we |
| 127 // maintain the list of supported methods below. | 130 // maintain the list of supported methods below. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 142 } | 145 } |
| 143 | 146 |
| 144 Future<String> sendToVM() { | 147 Future<String> sendToVM() { |
| 145 final receivePort = new RawReceivePort(); | 148 final receivePort = new RawReceivePort(); |
| 146 receivePort.handler = (value) { | 149 receivePort.handler = (value) { |
| 147 receivePort.close(); | 150 receivePort.close(); |
| 148 _completer.complete(value); | 151 _completer.complete(value); |
| 149 }; | 152 }; |
| 150 if (_methodNeedsObjectParameters(method)) { | 153 if (_methodNeedsObjectParameters(method)) { |
| 151 // We use a different method invocation path here. | 154 // We use a different method invocation path here. |
| 152 var keys = params.keys.toList(growable:false); | 155 var keys = params.keys.toList(growable: false); |
| 153 var values = params.values.toList(growable:false); | 156 var values = params.values.toList(growable: false); |
| 154 var request = new List(6) | 157 var request = new List(6) |
| 155 ..[0] = 0 // Make room for OOB message type. | 158 ..[0] = 0 // Make room for OOB message type. |
| 156 ..[1] = receivePort.sendPort | 159 ..[1] = receivePort.sendPort |
| 157 ..[2] = serial | 160 ..[2] = serial |
| 158 ..[3] = method | 161 ..[3] = method |
| 159 ..[4] = keys | 162 ..[4] = keys |
| 160 ..[5] = values; | 163 ..[5] = values; |
| 161 sendObjectRootServiceMessage(request); | 164 sendObjectRootServiceMessage(request); |
| 162 return _completer.future; | 165 return _completer.future; |
| 163 } else { | 166 } else { |
| 164 var keys = _makeAllString(params.keys.toList(growable:false)); | 167 var keys = _makeAllString(params.keys.toList(growable: false)); |
| 165 var values = _makeAllString(params.values.toList(growable:false)); | 168 var values = _makeAllString(params.values.toList(growable: false)); |
| 166 var request = new List(6) | 169 var request = new List(6) |
| 167 ..[0] = 0 // Make room for OOB message type. | 170 ..[0] = 0 // Make room for OOB message type. |
| 168 ..[1] = receivePort.sendPort | 171 ..[1] = receivePort.sendPort |
| 169 ..[2] = serial | 172 ..[2] = serial |
| 170 ..[3] = method | 173 ..[3] = method |
| 171 ..[4] = keys | 174 ..[4] = keys |
| 172 ..[5] = values; | 175 ..[5] = values; |
| 173 sendRootServiceMessage(request); | 176 sendRootServiceMessage(request); |
| 174 return _completer.future; | 177 return _completer.future; |
| 175 } | 178 } |
| 176 } | 179 } |
| 177 | 180 |
| 178 void setResponse(String response) { | 181 void setResponse(String response) { |
| 179 _completer.complete(response); | 182 _completer.complete(response); |
| 180 } | 183 } |
| 181 | 184 |
| 182 void setErrorResponse(int code, String details) { | 185 void setErrorResponse(int code, String details) { |
| 183 _completer.complete(encodeRpcError(this, code, | 186 _completer |
| 184 details: '$method: $details')); | 187 .complete(encodeRpcError(this, code, details: '$method: $details')); |
| 185 } | 188 } |
| 186 } | 189 } |
| 187 | 190 |
| 188 external bool sendIsolateServiceMessage(SendPort sp, List m); | 191 external bool sendIsolateServiceMessage(SendPort sp, List m); |
| 189 external void sendRootServiceMessage(List m); | 192 external void sendRootServiceMessage(List m); |
| 190 external void sendObjectRootServiceMessage(List m); | 193 external void sendObjectRootServiceMessage(List m); |
| OLD | NEW |