| 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 /// Path. | 12 |
| 13 final bool isOld; |
| 14 |
| 15 // In new messages. |
| 16 final String method; |
| 17 |
| 18 // In old messages. |
| 13 final List path = new List(); | 19 final List path = new List(); |
| 14 /// Options. | 20 |
| 15 final Map options = new Map(); | 21 final Map params = new Map(); |
| 16 | 22 |
| 17 void _setPath(List<String> pathSegments) { | 23 void _setPath(List<String> pathSegments) { |
| 18 if (pathSegments == null) { | 24 if (pathSegments == null) { |
| 19 return; | 25 return; |
| 20 } | 26 } |
| 21 pathSegments.forEach((String segment) { | 27 pathSegments.forEach((String segment) { |
| 22 if (segment == null || segment == '') { | 28 if (segment == null || segment == '') { |
| 23 return; | 29 return; |
| 24 } | 30 } |
| 25 path.add(segment); | 31 path.add(segment); |
| 26 }); | 32 }); |
| 27 } | 33 } |
| 28 | 34 |
| 29 Message.fromUri(Uri uri) { | 35 Message.fromUri(Uri uri) : isOld = true { |
| 30 var split = uri.path.split('/'); | 36 var split = uri.path.split('/'); |
| 31 if (split.length == 0) { | 37 if (split.length == 0) { |
| 32 setErrorResponse('Invalid uri: $uri.'); | 38 setErrorResponse('Invalid uri: $uri.'); |
| 33 return; | 39 return; |
| 34 } | 40 } |
| 35 _setPath(split); | 41 _setPath(split); |
| 36 options.addAll(uri.queryParameters); | 42 params.addAll(uri.queryParameters); |
| 37 } | 43 } |
| 38 | 44 |
| 39 Message.fromMap(Map map) { | 45 Message.fromJsonRpc(this.method, Map rpcParams) |
| 46 : isOld = false { |
| 47 params.addAll(rpcParams); |
| 48 } |
| 49 |
| 50 Message.fromMap(Map map) : isOld = true { |
| 40 _setPath(map['path']); | 51 _setPath(map['path']); |
| 52 // TODO - turnidge - change this to params in sender. |
| 41 if (map['options'] != null) { | 53 if (map['options'] != null) { |
| 42 options.addAll(map['options']); | 54 params.addAll(map['options']); |
| 43 } | 55 } |
| 44 } | 56 } |
| 45 | 57 |
| 46 dynamic toJson() { | 58 dynamic toJson() { |
| 47 return { | 59 return { |
| 48 'path': path, | 60 'path': path, |
| 49 'options': options | 61 'params': params |
| 50 }; | 62 }; |
| 51 } | 63 } |
| 52 | 64 |
| 53 Future<String> send(SendPort sendPort) { | 65 Future<String> send(SendPort sendPort) { |
| 54 final receivePort = new RawReceivePort(); | 66 final receivePort = new RawReceivePort(); |
| 55 receivePort.handler = (value) { | 67 receivePort.handler = (value) { |
| 56 receivePort.close(); | 68 receivePort.close(); |
| 57 if (value is Exception) { | 69 if (value is Exception) { |
| 58 _completer.completeError(value); | 70 _completer.completeError(value); |
| 59 } else { | 71 } else { |
| 60 _completer.complete(value); | 72 _completer.complete(value); |
| 61 } | 73 } |
| 62 }; | 74 }; |
| 63 var keys = options.keys.toList(growable:false); | 75 var keys = params.keys.toList(growable:false); |
| 64 var values = options.values.toList(growable:false); | 76 var values = params.values.toList(growable:false); |
| 65 var request = new List(5) | 77 var request = new List(5) |
| 66 ..[0] = 0 // Make room for OOB message type. | 78 ..[0] = 0 // Make room for OOB message type. |
| 67 ..[1] = receivePort.sendPort | 79 ..[1] = receivePort.sendPort |
| 68 ..[2] = path | 80 ..[2] = (isOld ? path : method) |
| 69 ..[3] = keys | 81 ..[3] = keys |
| 70 ..[4] = values; | 82 ..[4] = values; |
| 71 sendIsolateServiceMessage(sendPort, request); | 83 sendIsolateServiceMessage(sendPort, request); |
| 72 return _completer.future; | 84 return _completer.future; |
| 73 } | 85 } |
| 74 | 86 |
| 75 Future<String> sendToVM() { | 87 Future<String> sendToVM() { |
| 76 final receivePort = new RawReceivePort(); | 88 final receivePort = new RawReceivePort(); |
| 77 receivePort.handler = (value) { | 89 receivePort.handler = (value) { |
| 78 receivePort.close(); | 90 receivePort.close(); |
| 79 if (value is Exception) { | 91 if (value is Exception) { |
| 80 _completer.completeError(value); | 92 _completer.completeError(value); |
| 81 } else { | 93 } else { |
| 82 _completer.complete(value); | 94 _completer.complete(value); |
| 83 } | 95 } |
| 84 }; | 96 }; |
| 85 var keys = options.keys.toList(growable:false); | 97 var keys = params.keys.toList(growable:false); |
| 86 var values = options.values.toList(growable:false); | 98 var values = params.values.toList(growable:false); |
| 87 var request = new List(5) | 99 var request = new List(5) |
| 88 ..[0] = 0 // Make room for OOB message type. | 100 ..[0] = 0 // Make room for OOB message type. |
| 89 ..[1] = receivePort.sendPort | 101 ..[1] = receivePort.sendPort |
| 90 ..[2] = path | 102 ..[2] = (isOld ? path : method) |
| 91 ..[3] = keys | 103 ..[3] = keys |
| 92 ..[4] = values; | 104 ..[4] = values; |
| 93 sendRootServiceMessage(request); | 105 sendRootServiceMessage(request); |
| 94 return _completer.future; | 106 return _completer.future; |
| 95 } | 107 } |
| 96 | 108 |
| 97 void setResponse(String response) { | 109 void setResponse(String response) { |
| 98 _completer.complete(response); | 110 _completer.complete(response); |
| 99 } | 111 } |
| 100 | 112 |
| 101 void setErrorResponse(String error) { | 113 void setErrorResponse(String error) { |
| 102 _completer.complete(JSON.encode({ | 114 _completer.complete(JSON.encode({ |
| 103 'type': 'ServiceError', | 115 'type': 'ServiceError', |
| 104 'id': '', | 116 'id': '', |
| 105 'kind': 'RequestError', | 117 'kind': 'RequestError', |
| 106 'message': error, | 118 'message': error, |
| 107 'path': path, | 119 'path': path, |
| 108 'options': options | 120 'params': params |
| 109 })); | 121 })); |
| 110 } | 122 } |
| 111 } | 123 } |
| 112 | 124 |
| 113 void sendIsolateServiceMessage(SendPort sp, List m) | 125 void sendIsolateServiceMessage(SendPort sp, List m) |
| 114 native "VMService_SendIsolateServiceMessage"; | 126 native "VMService_SendIsolateServiceMessage"; |
| 115 | 127 |
| 116 void sendRootServiceMessage(List m) | 128 void sendRootServiceMessage(List m) |
| 117 native "VMService_SendRootServiceMessage"; | 129 native "VMService_SendRootServiceMessage"; |
| OLD | NEW |