| 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 enum MessageType { Request, Notification, Response } |
| 8 |
| 7 class Message { | 9 class Message { |
| 8 final Completer _completer = new Completer.sync(); | 10 final Completer _completer = new Completer.sync(); |
| 9 bool get completed => _completer.isCompleted; | 11 bool get completed => _completer.isCompleted; |
| 10 | 12 |
| 11 /// Future of response. | 13 /// Future of response. |
| 12 Future<String> get response => _completer.future; | 14 Future<String> get response => _completer.future; |
| 13 Client client; | 15 Client client; |
| 14 | 16 |
| 17 // Is a notification message (no serial) |
| 18 final MessageType type; |
| 19 |
| 15 // Client-side identifier for this message. | 20 // Client-side identifier for this message. |
| 16 final serial; | 21 final serial; |
| 17 | 22 |
| 18 // In new messages. | 23 // In new messages. |
| 19 final String method; | 24 final String method; |
| 20 | 25 |
| 21 // In old messages. | 26 // In old messages. |
| 22 final List path = new List(); | 27 final List path = new List(); |
| 23 | 28 |
| 24 final Map params = new Map(); | 29 final Map params = new Map(); |
| 30 final Map result = new Map(); |
| 31 final Map error = new Map(); |
| 25 | 32 |
| 26 void _setPath(List<String> pathSegments) { | 33 void _setPath(List<String> pathSegments) { |
| 27 if (pathSegments == null) { | 34 if (pathSegments == null) { |
| 28 return; | 35 return; |
| 29 } | 36 } |
| 30 pathSegments.forEach((String segment) { | 37 pathSegments.forEach((String segment) { |
| 31 if (segment == null || segment == '') { | 38 if (segment == null || segment == '') { |
| 32 return; | 39 return; |
| 33 } | 40 } |
| 34 path.add(segment); | 41 path.add(segment); |
| 35 }); | 42 }); |
| 36 } | 43 } |
| 37 | 44 |
| 38 Message.fromJsonRpc(this.client, Map map) | 45 factory Message.fromJsonRpc(Client client, Map map) { |
| 39 : serial = map['id'], | 46 if (map.containsKey('id')) { |
| 47 final id = map['id']; |
| 48 if (id != null && id is! num && id is! String) { |
| 49 throw new Exception('"id" must be a number, string, or null.'); |
| 50 } |
| 51 if (map.containsKey('method')) { |
| 52 return new Message._fromJsonRpcRequest(client, map); |
| 53 } |
| 54 if (map.containsKey('result')) { |
| 55 return new Message._fromJsonRpcResult(client, map); |
| 56 } |
| 57 if (map.containsKey('error')) { |
| 58 return new Message._fromJsonRpcError(client, map); |
| 59 } |
| 60 } else if (map.containsKey('method')) { |
| 61 return new Message._fromJsonRpcNotification(client, map); |
| 62 } |
| 63 throw new Exception('Invalid message format'); |
| 64 } |
| 65 |
| 66 // http://www.jsonrpc.org/specification#request_object |
| 67 Message._fromJsonRpcRequest(Client client, Map map) |
| 68 : client = client, |
| 69 type = MessageType.Request, |
| 70 serial = map['id'], |
| 40 method = map['method'] { | 71 method = map['method'] { |
| 41 if (map['params'] != null) { | 72 if (map['params'] != null) { |
| 42 params.addAll(map['params']); | 73 params.addAll(map['params']); |
| 43 } | 74 } |
| 44 } | 75 } |
| 45 | 76 |
| 77 // http://www.jsonrpc.org/specification#notification |
| 78 Message._fromJsonRpcNotification(Client client, Map map) |
| 79 : client = client, |
| 80 type = MessageType.Notification, |
| 81 method = map['method'], |
| 82 serial = null { |
| 83 if (map['params'] != null) { |
| 84 params.addAll(map['params']); |
| 85 } |
| 86 } |
| 87 |
| 88 // http://www.jsonrpc.org/specification#response_object |
| 89 Message._fromJsonRpcResult(Client client, Map map) |
| 90 : client = client, |
| 91 type = MessageType.Response, |
| 92 serial = map['id'], |
| 93 method = null { |
| 94 result.addAll(map['result']); |
| 95 } |
| 96 |
| 97 // http://www.jsonrpc.org/specification#response_object |
| 98 Message._fromJsonRpcError(Client client, Map map) |
| 99 : client = client, |
| 100 type = MessageType.Response, |
| 101 serial = map['id'], |
| 102 method = null { |
| 103 error.addAll(map['error']); |
| 104 } |
| 105 |
| 46 static String _methodNameFromUri(Uri uri) { | 106 static String _methodNameFromUri(Uri uri) { |
| 47 if (uri == null) { | 107 if (uri == null) { |
| 48 return ''; | 108 return ''; |
| 49 } | 109 } |
| 50 if (uri.pathSegments.length == 0) { | 110 if (uri.pathSegments.length == 0) { |
| 51 return ''; | 111 return ''; |
| 52 } | 112 } |
| 53 return uri.pathSegments[0]; | 113 return uri.pathSegments[0]; |
| 54 } | 114 } |
| 55 | 115 |
| 56 Message.forMethod(String method) | 116 Message.forMethod(String method) |
| 57 : client = null, | 117 : client = null, |
| 58 method = method, | 118 method = method, |
| 119 type = MessageType.Request, |
| 59 serial = ''; | 120 serial = ''; |
| 60 | 121 |
| 61 Message.fromUri(this.client, Uri uri) | 122 Message.fromUri(this.client, Uri uri) |
| 62 : serial = '', | 123 : type = MessageType.Request, |
| 124 serial = '', |
| 63 method = _methodNameFromUri(uri) { | 125 method = _methodNameFromUri(uri) { |
| 64 params.addAll(uri.queryParameters); | 126 params.addAll(uri.queryParameters); |
| 65 } | 127 } |
| 66 | 128 |
| 67 Message.forIsolate(this.client, Uri uri, RunningIsolate isolate) | 129 Message.forIsolate(this.client, Uri uri, RunningIsolate isolate) |
| 68 : serial = '', | 130 : type = MessageType.Request, |
| 131 serial = '', |
| 69 method = _methodNameFromUri(uri) { | 132 method = _methodNameFromUri(uri) { |
| 70 params.addAll(uri.queryParameters); | 133 params.addAll(uri.queryParameters); |
| 71 params['isolateId'] = isolate.serviceId; | 134 params['isolateId'] = isolate.serviceId; |
| 72 } | 135 } |
| 73 | 136 |
| 74 Uri toUri() { | 137 Uri toUri() { |
| 75 return new Uri(path: method, queryParameters: params); | 138 return new Uri(path: method, queryParameters: params); |
| 76 } | 139 } |
| 77 | 140 |
| 78 dynamic toJson() { | 141 dynamic toJson() { |
| 79 return {'path': path, 'params': params}; | 142 return {'path': path, 'params': params}; |
| 80 } | 143 } |
| 81 | 144 |
| 145 dynamic forwardToJson([Map overloads]) { |
| 146 var json = {'jsonrpc': '2.0', 'id': serial}; |
| 147 switch (type) { |
| 148 case MessageType.Request: |
| 149 case MessageType.Notification: |
| 150 json['method'] = method; |
| 151 if (params.isNotEmpty) { |
| 152 json['params'] = params; |
| 153 } |
| 154 break; |
| 155 case MessageType.Response: |
| 156 if (result.isNotEmpty) { |
| 157 json['result'] = result; |
| 158 } |
| 159 if (error.isNotEmpty) { |
| 160 json['error'] = error; |
| 161 } |
| 162 } |
| 163 if (overloads != null) { |
| 164 json.addAll(overloads); |
| 165 } |
| 166 return json; |
| 167 } |
| 168 |
| 82 // Calls toString on all non-String elements of [list]. We do this so all | 169 // Calls toString on all non-String elements of [list]. We do this so all |
| 83 // elements in the list are strings, making consumption by C++ simpler. | 170 // elements in the list are strings, making consumption by C++ simpler. |
| 84 // This has a side effect that boolean literal values like true become 'true' | 171 // This has a side effect that boolean literal values like true become 'true' |
| 85 // and thus indistinguishable from the string literal 'true'. | 172 // and thus indistinguishable from the string literal 'true'. |
| 86 List _makeAllString(List list) { | 173 List _makeAllString(List list) { |
| 87 if (list == null) { | 174 if (list == null) { |
| 88 return null; | 175 return null; |
| 89 } | 176 } |
| 90 for (var i = 0; i < list.length; i++) { | 177 for (var i = 0; i < list.length; i++) { |
| 91 if (list[i] is String) { | 178 if (list[i] is String) { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 } | 275 } |
| 189 } | 276 } |
| 190 | 277 |
| 191 bool sendIsolateServiceMessage(SendPort sp, List m) | 278 bool sendIsolateServiceMessage(SendPort sp, List m) |
| 192 native "VMService_SendIsolateServiceMessage"; | 279 native "VMService_SendIsolateServiceMessage"; |
| 193 | 280 |
| 194 void sendRootServiceMessage(List m) native "VMService_SendRootServiceMessage"; | 281 void sendRootServiceMessage(List m) native "VMService_SendRootServiceMessage"; |
| 195 | 282 |
| 196 void sendObjectRootServiceMessage(List m) | 283 void sendObjectRootServiceMessage(List m) |
| 197 native "VMService_SendObjectRootServiceMessage"; | 284 native "VMService_SendObjectRootServiceMessage"; |
| OLD | NEW |