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