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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/devtools/devtools_protocol.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/strings/stringprintf.h"
namespace content {
namespace {
const char kIdParam[] = "id";
const char kMethodParam[] = "method";
const char kParamsParam[] = "params";
const char kResultParam[] = "result";
const char kErrorParam[] = "error";
const char kErrorCodeParam[] = "code";
const char kErrorMessageParam[] = "message";
const int kNoId = -1;
// JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
enum Error {
kErrorParseError = -32700,
kErrorInvalidRequest = -32600,
kErrorNoSuchMethod = -32601,
kErrorInvalidParams = -32602,
kErrorInternalError = -32603,
kErrorServerError = -32000
};
} // namespace
using base::Value;
DevToolsProtocol::Message::~Message() {
}
DevToolsProtocol::Message::Message(const std::string& method,
base::DictionaryValue* params)
: method_(method),
params_(params) {
size_t pos = method.find(".");
if (pos != std::string::npos && pos > 0)
domain_ = method.substr(0, pos);
}
DevToolsProtocol::Command::~Command() {
}
std::string DevToolsProtocol::Command::Serialize() {
base::DictionaryValue command;
command.SetInteger(kIdParam, id_);
command.SetString(kMethodParam, method_);
if (params_)
command.Set(kParamsParam, params_->DeepCopy());
std::string json_command;
base::JSONWriter::Write(&command, &json_command);
return json_command;
}
scoped_refptr<DevToolsProtocol::Response>
DevToolsProtocol::Command::SuccessResponse(base::DictionaryValue* result) {
return new DevToolsProtocol::Response(id_, result);
}
scoped_refptr<DevToolsProtocol::Response>
DevToolsProtocol::Command::InternalErrorResponse(const std::string& message) {
return new DevToolsProtocol::Response(id_, kErrorInternalError, message);
}
scoped_refptr<DevToolsProtocol::Response>
DevToolsProtocol::Command::InvalidParamResponse(const std::string& param) {
std::string message =
base::StringPrintf("Missing or invalid '%s' parameter", param.c_str());
return new DevToolsProtocol::Response(id_, kErrorInvalidParams, message);
}
scoped_refptr<DevToolsProtocol::Response>
DevToolsProtocol::Command::NoSuchMethodErrorResponse() {
return new Response(id_, kErrorNoSuchMethod, "No such method");
}
scoped_refptr<DevToolsProtocol::Response>
DevToolsProtocol::Command::ServerErrorResponse(const std::string& message) {
return new Response(id_, kErrorServerError, message);
}
scoped_refptr<DevToolsProtocol::Response>
DevToolsProtocol::Command::AsyncResponsePromise() {
scoped_refptr<DevToolsProtocol::Response> promise =
new DevToolsProtocol::Response(0, NULL);
promise->is_async_promise_ = true;
return promise;
}
DevToolsProtocol::Command::Command(int id,
const std::string& method,
base::DictionaryValue* params)
: Message(method, params),
id_(id) {
}
DevToolsProtocol::Response::~Response() {
}
std::string DevToolsProtocol::Response::Serialize() {
base::DictionaryValue response;
if (id_ != kNoId)
response.SetInteger(kIdParam, id_);
if (error_code_) {
base::DictionaryValue* error_object = new base::DictionaryValue();
response.Set(kErrorParam, error_object);
error_object->SetInteger(kErrorCodeParam, error_code_);
if (!error_message_.empty())
error_object->SetString(kErrorMessageParam, error_message_);
} else {
if (result_)
response.Set(kResultParam, result_->DeepCopy());
else
response.Set(kResultParam, new base::DictionaryValue());
}
std::string json_response;
base::JSONWriter::Write(&response, &json_response);
return json_response;
}
DevToolsProtocol::Response::Response(int id, base::DictionaryValue* result)
: id_(id),
result_(result),
error_code_(0),
is_async_promise_(false) {
}
DevToolsProtocol::Response::Response(int id,
int error_code,
const std::string& error_message)
: id_(id),
error_code_(error_code),
error_message_(error_message),
is_async_promise_(false) {
}
DevToolsProtocol::Notification::Notification(const std::string& method,
base::DictionaryValue* params)
: Message(method, params) {
}
DevToolsProtocol::Notification::~Notification() {
}
std::string DevToolsProtocol::Notification::Serialize() {
base::DictionaryValue notification;
notification.SetString(kMethodParam, method_);
if (params_)
notification.Set(kParamsParam, params_->DeepCopy());
std::string json_notification;
base::JSONWriter::Write(¬ification, &json_notification);
return json_notification;
}
DevToolsProtocol::Handler::~Handler() {
}
scoped_refptr<DevToolsProtocol::Response>
DevToolsProtocol::Handler::HandleCommand(
scoped_refptr<DevToolsProtocol::Command> command) {
CommandHandlers::iterator it = command_handlers_.find(command->method());
if (it == command_handlers_.end())
return NULL;
return (it->second).Run(command);
}
void DevToolsProtocol::Handler::HandleNotification(
scoped_refptr<DevToolsProtocol::Notification> notification) {
NotificationHandlers::iterator it =
notification_handlers_.find(notification->method());
if (it == notification_handlers_.end())
return;
(it->second).Run(notification);
}
void DevToolsProtocol::Handler::SetNotifier(const Notifier& notifier) {
notifier_ = notifier;
}
DevToolsProtocol::Handler::Handler() {
}
void DevToolsProtocol::Handler::RegisterCommandHandler(
const std::string& command,
const CommandHandler& handler) {
command_handlers_[command] = handler;
}
void DevToolsProtocol::Handler::RegisterNotificationHandler(
const std::string& notification,
const NotificationHandler& handler) {
notification_handlers_[notification] = handler;
}
void DevToolsProtocol::Handler::SendNotification(
const std::string& method,
base::DictionaryValue* params) {
scoped_refptr<DevToolsProtocol::Notification> notification =
new DevToolsProtocol::Notification(method, params);
SendRawMessage(notification->Serialize());
}
void DevToolsProtocol::Handler::SendAsyncResponse(
scoped_refptr<DevToolsProtocol::Response> response) {
SendRawMessage(response->Serialize());
}
void DevToolsProtocol::Handler::SendRawMessage(const std::string& message) {
if (!notifier_.is_null())
notifier_.Run(message);
}
static bool ParseMethod(base::DictionaryValue* command,
std::string* method) {
if (!command->GetString(kMethodParam, method))
return false;
size_t pos = method->find(".");
if (pos == std::string::npos || pos == 0)
return false;
return true;
}
// static
scoped_refptr<DevToolsProtocol::Command> DevToolsProtocol::ParseCommand(
const std::string& json,
std::string* error_response) {
scoped_ptr<base::DictionaryValue> command_dict(
ParseMessage(json, error_response));
return ParseCommand(command_dict.get(), error_response);
}
// static
scoped_refptr<DevToolsProtocol::Command> DevToolsProtocol::ParseCommand(
base::DictionaryValue* command_dict,
std::string* error_response) {
if (!command_dict)
return NULL;
int id;
std::string method;
bool ok = command_dict->GetInteger(kIdParam, &id) && id >= 0;
ok = ok && ParseMethod(command_dict, &method);
if (!ok) {
scoped_refptr<Response> response =
new Response(kNoId, kErrorInvalidRequest, "No such method");
*error_response = response->Serialize();
return NULL;
}
base::DictionaryValue* params = NULL;
command_dict->GetDictionary(kParamsParam, ¶ms);
return new Command(id, method, params ? params->DeepCopy() : NULL);
}
// static
scoped_refptr<DevToolsProtocol::Command>
DevToolsProtocol::CreateCommand(
int id,
const std::string& method,
base::DictionaryValue* params) {
return new Command(id, method, params);
}
//static
scoped_refptr<DevToolsProtocol::Response>
DevToolsProtocol::ParseResponse(
base::DictionaryValue* response_dict) {
int id;
if (!response_dict->GetInteger(kIdParam, &id))
id = kNoId;
const base::DictionaryValue* error_dict;
if (response_dict->GetDictionary(kErrorParam, &error_dict)) {
int error_code = kErrorInternalError;
response_dict->GetInteger(kErrorCodeParam, &error_code);
std::string error_message;
response_dict->GetString(kErrorMessageParam, &error_message);
return new Response(id, error_code, error_message);
}
const base::DictionaryValue* result = NULL;
response_dict->GetDictionary(kResultParam, &result);
return new Response(id, result ? result->DeepCopy() : NULL);
}
// static
scoped_refptr<DevToolsProtocol::Notification>
DevToolsProtocol::ParseNotification(const std::string& json) {
scoped_ptr<base::DictionaryValue> dict(ParseMessage(json, NULL));
if (!dict)
return NULL;
std::string method;
bool ok = ParseMethod(dict.get(), &method);
if (!ok)
return NULL;
base::DictionaryValue* params = NULL;
dict->GetDictionary(kParamsParam, ¶ms);
return new Notification(method, params ? params->DeepCopy() : NULL);
}
//static
scoped_refptr<DevToolsProtocol::Notification>
DevToolsProtocol::CreateNotification(
const std::string& method,
base::DictionaryValue* params) {
return new Notification(method, params);
}
// static
base::DictionaryValue* DevToolsProtocol::ParseMessage(
const std::string& json,
std::string* error_response) {
int parse_error_code;
std::string error_message;
scoped_ptr<base::Value> message(
base::JSONReader::ReadAndReturnError(
json, 0, &parse_error_code, &error_message));
if (!message || !message->IsType(base::Value::TYPE_DICTIONARY)) {
scoped_refptr<Response> response =
new Response(0, kErrorParseError, error_message);
if (error_response)
*error_response = response->Serialize();
return NULL;
}
return static_cast<base::DictionaryValue*>(message.release());
}
} // namespace content
|