-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
message.dart
262 lines (229 loc) · 7.57 KB
/
message.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
part of dart._vmservice;
enum MessageType { Request, Notification, Response }
class Message {
final Completer<Response> _completer = new Completer<Response>.sync();
bool get completed => _completer.isCompleted;
/// Future of response.
Future<Response> get response => _completer.future;
Client client;
// Is a notification message (no serial)
final MessageType type;
// Client-side identifier for this message.
final serial;
final String method;
final Map params = new Map();
final Map result = new Map();
final Map error = new Map();
factory Message.fromJsonRpc(Client client, Map map) {
if (map.containsKey('id')) {
final id = map['id'];
if (id != null && id is! num && id is! String) {
throw new Exception('"id" must be a number, string, or null.');
}
if (map.containsKey('method')) {
return new Message._fromJsonRpcRequest(client, map);
}
if (map.containsKey('result')) {
return new Message._fromJsonRpcResult(client, map);
}
if (map.containsKey('error')) {
return new Message._fromJsonRpcError(client, map);
}
} else if (map.containsKey('method')) {
return new Message._fromJsonRpcNotification(client, map);
}
throw new Exception('Invalid message format');
}
// http://www.jsonrpc.org/specification#request_object
Message._fromJsonRpcRequest(Client client, Map map)
: client = client,
type = MessageType.Request,
serial = map['id'],
method = map['method'] {
if (map['params'] != null) {
params.addAll(map['params']);
}
}
// http://www.jsonrpc.org/specification#notification
Message._fromJsonRpcNotification(Client client, Map map)
: client = client,
type = MessageType.Notification,
method = map['method'],
serial = null {
if (map['params'] != null) {
params.addAll(map['params']);
}
}
// http://www.jsonrpc.org/specification#response_object
Message._fromJsonRpcResult(Client client, Map map)
: client = client,
type = MessageType.Response,
serial = map['id'],
method = null {
result.addAll(map['result']);
}
// http://www.jsonrpc.org/specification#response_object
Message._fromJsonRpcError(Client client, Map map)
: client = client,
type = MessageType.Response,
serial = map['id'],
method = null {
error.addAll(map['error']);
}
static String _methodNameFromUri(Uri uri) {
if (uri == null) {
return '';
}
if (uri.pathSegments.length == 0) {
return '';
}
return uri.pathSegments[0];
}
Message.forMethod(String method)
: client = null,
method = method,
type = MessageType.Request,
serial = '';
Message.fromUri(this.client, Uri uri)
: type = MessageType.Request,
serial = '',
method = _methodNameFromUri(uri) {
params.addAll(uri.queryParameters);
}
Message.forIsolate(this.client, Uri uri, RunningIsolate isolate)
: type = MessageType.Request,
serial = '',
method = _methodNameFromUri(uri) {
params.addAll(uri.queryParameters);
params['isolateId'] = isolate.serviceId;
}
Uri toUri() {
return new Uri(path: method, queryParameters: params);
}
dynamic toJson() {
throw 'unsupported';
}
dynamic forwardToJson([Map overloads]) {
Map<dynamic, dynamic> json = {'jsonrpc': '2.0', 'id': serial};
switch (type) {
case MessageType.Request:
case MessageType.Notification:
json['method'] = method;
if (params.isNotEmpty) {
json['params'] = params;
}
break;
case MessageType.Response:
if (result.isNotEmpty) {
json['result'] = result;
}
if (error.isNotEmpty) {
json['error'] = error;
}
}
if (overloads != null) {
json.addAll(overloads);
}
return json;
}
// Calls toString on all non-String elements of [list]. We do this so all
// elements in the list are strings, making consumption by C++ simpler.
// This has a side effect that boolean literal values like true become 'true'
// and thus indistinguishable from the string literal 'true'.
List<String> _makeAllString(List list) {
if (list == null) {
return null;
}
var new_list = new List<String>(list.length);
for (var i = 0; i < list.length; i++) {
new_list[i] = list[i].toString();
}
return new_list;
}
Future<Response> sendToIsolate(SendPort sendPort) {
final receivePort = new RawReceivePort();
receivePort.handler = (value) {
receivePort.close();
_setResponseFromPort(value);
};
var keys = _makeAllString(params.keys.toList(growable: false));
var values = _makeAllString(params.values.toList(growable: false));
var request = new List(6)
..[0] = 0 // Make room for OOB message type.
..[1] = receivePort.sendPort
..[2] = serial
..[3] = method
..[4] = keys
..[5] = values;
if (!sendIsolateServiceMessage(sendPort, request)) {
receivePort.close();
_completer.complete(new Response.internalError(
'could not send message [${serial}] to isolate'));
}
return _completer.future;
}
// We currently support two ways of passing parameters from Dart code to C
// code. The original way always converts the parameters to strings before
// passing them over. Our goal is to convert all C handlers to take the
// parameters as Dart objects but until the conversion is complete, we
// maintain the list of supported methods below.
bool _methodNeedsObjectParameters(String method) {
switch (method) {
case '_listDevFS':
case '_listDevFSFiles':
case '_createDevFS':
case '_deleteDevFS':
case '_writeDevFSFile':
case '_writeDevFSFiles':
case '_readDevFSFile':
case '_spawnUri':
return true;
default:
return false;
}
}
Future<Response> sendToVM() {
final receivePort = new RawReceivePort();
receivePort.handler = (value) {
receivePort.close();
_setResponseFromPort(value);
};
var keys = params.keys.toList(growable: false);
var values = params.values.toList(growable: false);
if (!_methodNeedsObjectParameters(method)) {
keys = _makeAllString(keys);
values = _makeAllString(values);
}
final request = new List(6)
..[0] = 0 // Make room for OOB message type.
..[1] = receivePort.sendPort
..[2] = serial
..[3] = method
..[4] = keys
..[5] = values;
if (_methodNeedsObjectParameters(method)) {
// We use a different method invocation path here.
sendObjectRootServiceMessage(request);
} else {
sendRootServiceMessage(request);
}
return _completer.future;
}
void _setResponseFromPort(dynamic response) {
_completer.complete(new Response.from(response));
}
void setResponse(String response) {
_completer.complete(new Response(ResponsePayloadKind.String, response));
}
void setErrorResponse(int code, String details) {
setResponse(encodeRpcError(this, code, details: '$method: $details'));
}
}
bool sendIsolateServiceMessage(SendPort sp, List m)
native "VMService_SendIsolateServiceMessage";
void sendRootServiceMessage(List m) native "VMService_SendRootServiceMessage";
void sendObjectRootServiceMessage(List m)
native "VMService_SendObjectRootServiceMessage";