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
|
// Copyright 2014 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/protocol/devtools_protocol_handler.h"
#include "base/bind.h"
#include "base/json/json_reader.h"
namespace content {
namespace {
const char kIdParam[] = "id";
const char kMethodParam[] = "method";
const char kParamsParam[] = "params";
// JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
const int kStatusParseError = -32700;
const int kStatusInvalidRequest = -32600;
const int kStatusNoSuchMethod = -32601;
scoped_ptr<base::DictionaryValue> TakeDictionary(base::DictionaryValue* dict,
const std::string& key) {
scoped_ptr<base::Value> value;
dict->Remove(key, &value);
base::DictionaryValue* result = nullptr;
if (value)
value.release()->GetAsDictionary(&result);
return make_scoped_ptr(result);
}
} // namespace
DevToolsProtocolHandler::DevToolsProtocolHandler(const Notifier& notifier)
: client_(notifier),
dispatcher_(notifier) {
}
DevToolsProtocolHandler::~DevToolsProtocolHandler() {
}
scoped_ptr<base::DictionaryValue>
DevToolsProtocolHandler::ParseCommand(const std::string& message) {
scoped_ptr<base::Value> value(base::JSONReader::Read(message));
if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) {
client_.SendError(DevToolsProtocolClient::kNoId,
Response(kStatusParseError,
"Message must be in JSON format"));
return nullptr;
}
scoped_ptr<base::DictionaryValue> command =
make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release()));
int id = DevToolsProtocolClient::kNoId;
bool ok = command->GetInteger(kIdParam, &id) && id >= 0;
if (!ok) {
client_.SendError(id, Response(kStatusInvalidRequest,
"The type of 'id' property must be number"));
return nullptr;
}
std::string method;
ok = command->GetString(kMethodParam, &method);
if (!ok) {
client_.SendError(id,
Response(kStatusInvalidRequest,
"The type of 'method' property must be string"));
return nullptr;
}
return command;
}
void DevToolsProtocolHandler::HandleCommand(
scoped_ptr<base::DictionaryValue> command) {
int id = DevToolsProtocolClient::kNoId;
std::string method;
command->GetInteger(kIdParam, &id);
command->GetString(kMethodParam, &method);
DevToolsProtocolDispatcher::CommandHandler command_handler(
dispatcher_.FindCommandHandler(method));
if (command_handler.is_null()) {
client_.SendError(id, Response(kStatusNoSuchMethod, "No such method"));
return;
}
bool result =
command_handler.Run(id, TakeDictionary(command.get(), kParamsParam));
DCHECK(result);
}
bool DevToolsProtocolHandler::HandleOptionalCommand(
scoped_ptr<base::DictionaryValue> command) {
int id = DevToolsProtocolClient::kNoId;
std::string method;
command->GetInteger(kIdParam, &id);
command->GetString(kMethodParam, &method);
DevToolsProtocolDispatcher::CommandHandler command_handler(
dispatcher_.FindCommandHandler(method));
if (!command_handler.is_null())
return command_handler.Run(id, TakeDictionary(command.get(), kParamsParam));
return false;
}
} // namespace content
|