OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/devtools/devtools_protocol_handler.h" | 5 #include "content/browser/devtools/devtools_protocol_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "content/browser/devtools/devtools_agent_host_impl.h" | |
11 #include "content/browser/devtools/devtools_manager.h" | 10 #include "content/browser/devtools/devtools_manager.h" |
12 #include "content/public/browser/devtools_manager_delegate.h" | 11 #include "content/public/browser/devtools_manager_delegate.h" |
13 | 12 |
14 namespace content { | 13 namespace content { |
15 | 14 |
16 namespace { | 15 namespace { |
17 | 16 |
18 const char kIdParam[] = "id"; | 17 const char kIdParam[] = "id"; |
19 const char kMethodParam[] = "method"; | 18 const char kMethodParam[] = "method"; |
20 const char kParamsParam[] = "params"; | 19 const char kParamsParam[] = "params"; |
21 | 20 |
22 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object | 21 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object |
23 const int kStatusParseError = -32700; | 22 const int kStatusParseError = -32700; |
24 const int kStatusInvalidRequest = -32600; | 23 const int kStatusInvalidRequest = -32600; |
25 const int kStatusNoSuchMethod = -32601; | 24 const int kStatusNoSuchMethod = -32601; |
26 | 25 |
27 scoped_ptr<base::DictionaryValue> TakeDictionary(base::DictionaryValue* dict, | 26 scoped_ptr<base::DictionaryValue> TakeDictionary(base::DictionaryValue* dict, |
28 const std::string& key) { | 27 const std::string& key) { |
29 scoped_ptr<base::Value> value; | 28 scoped_ptr<base::Value> value; |
30 dict->Remove(key, &value); | 29 dict->Remove(key, &value); |
31 base::DictionaryValue* result = nullptr; | 30 base::DictionaryValue* result = nullptr; |
32 if (value) | 31 if (value) |
33 value.release()->GetAsDictionary(&result); | 32 value.release()->GetAsDictionary(&result); |
34 return make_scoped_ptr(result); | 33 return make_scoped_ptr(result); |
35 } | 34 } |
36 | 35 |
37 } // namespace | 36 } // namespace |
38 | 37 |
39 DevToolsProtocolHandler::DevToolsProtocolHandler( | 38 DevToolsProtocolHandler::DevToolsProtocolHandler( |
40 DevToolsAgentHostImpl* agent_host) | 39 DevToolsAgentHost* agent_host, const Notifier& notifier) |
41 : agent_host_(agent_host), client_(agent_host), dispatcher_(agent_host) {} | 40 : agent_host_(agent_host), |
| 41 client_(notifier), |
| 42 dispatcher_(notifier) { |
| 43 } |
42 | 44 |
43 DevToolsProtocolHandler::~DevToolsProtocolHandler() { | 45 DevToolsProtocolHandler::~DevToolsProtocolHandler() { |
44 } | 46 } |
45 | 47 |
46 void DevToolsProtocolHandler::HandleMessage(int session_id, | 48 void DevToolsProtocolHandler::HandleMessage(const std::string& message) { |
47 const std::string& message) { | 49 scoped_ptr<base::DictionaryValue> command = ParseCommand(message); |
48 scoped_ptr<base::DictionaryValue> command = ParseCommand(session_id, message); | |
49 if (!command) | 50 if (!command) |
50 return; | 51 return; |
51 if (PassCommandToDelegate(session_id, command.get())) | 52 if (PassCommandToDelegate(command.get())) |
52 return; | 53 return; |
53 HandleCommand(session_id, command.Pass()); | 54 HandleCommand(command.Pass()); |
54 } | 55 } |
55 | 56 |
56 bool DevToolsProtocolHandler::HandleOptionalMessage(int session_id, | 57 bool DevToolsProtocolHandler::HandleOptionalMessage( |
57 const std::string& message, | 58 const std::string& message, int* call_id) { |
58 int* call_id) { | 59 scoped_ptr<base::DictionaryValue> command = ParseCommand(message); |
59 scoped_ptr<base::DictionaryValue> command = ParseCommand(session_id, message); | |
60 if (!command) | 60 if (!command) |
61 return true; | 61 return true; |
62 if (PassCommandToDelegate(session_id, command.get())) | 62 if (PassCommandToDelegate(command.get())) |
63 return true; | 63 return true; |
64 return HandleOptionalCommand(session_id, command.Pass(), call_id); | 64 return HandleOptionalCommand(command.Pass(), call_id); |
65 } | 65 } |
66 | 66 |
67 bool DevToolsProtocolHandler::PassCommandToDelegate( | 67 bool DevToolsProtocolHandler::PassCommandToDelegate( |
68 int session_id, | |
69 base::DictionaryValue* command) { | 68 base::DictionaryValue* command) { |
70 DevToolsManagerDelegate* delegate = | 69 DevToolsManagerDelegate* delegate = |
71 DevToolsManager::GetInstance()->delegate(); | 70 DevToolsManager::GetInstance()->delegate(); |
72 if (!delegate) | 71 if (!delegate) |
73 return false; | 72 return false; |
74 | 73 |
75 scoped_ptr<base::DictionaryValue> response( | 74 scoped_ptr<base::DictionaryValue> response( |
76 delegate->HandleCommand(agent_host_, command)); | 75 delegate->HandleCommand(agent_host_, command)); |
77 if (response) { | 76 if (response) { |
78 client_.SendMessage(session_id, *response); | 77 std::string json_response; |
| 78 base::JSONWriter::Write(*response, &json_response); |
| 79 client_.SendRawMessage(json_response); |
79 return true; | 80 return true; |
80 } | 81 } |
81 | 82 |
82 return false; | 83 return false; |
83 } | 84 } |
84 | 85 |
85 scoped_ptr<base::DictionaryValue> DevToolsProtocolHandler::ParseCommand( | 86 scoped_ptr<base::DictionaryValue> |
86 int session_id, | 87 DevToolsProtocolHandler::ParseCommand(const std::string& message) { |
87 const std::string& message) { | |
88 scoped_ptr<base::Value> value = base::JSONReader::Read(message); | 88 scoped_ptr<base::Value> value = base::JSONReader::Read(message); |
89 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { | 89 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { |
90 client_.SendError( | 90 client_.SendError(DevToolsProtocolClient::kNoId, |
91 DevToolsCommandId(DevToolsCommandId::kNoId, session_id), | 91 Response(kStatusParseError, |
92 Response(kStatusParseError, "Message must be in JSON format")); | 92 "Message must be in JSON format")); |
93 return nullptr; | 93 return nullptr; |
94 } | 94 } |
95 | 95 |
96 scoped_ptr<base::DictionaryValue> command = | 96 scoped_ptr<base::DictionaryValue> command = |
97 make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); | 97 make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); |
98 int call_id = DevToolsCommandId::kNoId; | 98 int id = DevToolsProtocolClient::kNoId; |
99 bool ok = command->GetInteger(kIdParam, &call_id) && call_id >= 0; | 99 bool ok = command->GetInteger(kIdParam, &id) && id >= 0; |
100 if (!ok) { | 100 if (!ok) { |
101 client_.SendError(DevToolsCommandId(call_id, session_id), | 101 client_.SendError(id, Response(kStatusInvalidRequest, |
102 Response(kStatusInvalidRequest, | 102 "The type of 'id' property must be number")); |
103 "The type of 'id' property must be number")); | |
104 return nullptr; | 103 return nullptr; |
105 } | 104 } |
106 | 105 |
107 std::string method; | 106 std::string method; |
108 ok = command->GetString(kMethodParam, &method); | 107 ok = command->GetString(kMethodParam, &method); |
109 if (!ok) { | 108 if (!ok) { |
110 client_.SendError(DevToolsCommandId(call_id, session_id), | 109 client_.SendError(id, |
111 Response(kStatusInvalidRequest, | 110 Response(kStatusInvalidRequest, |
112 "The type of 'method' property must be string")); | 111 "The type of 'method' property must be string")); |
113 return nullptr; | 112 return nullptr; |
114 } | 113 } |
115 | 114 |
116 return command; | 115 return command; |
117 } | 116 } |
118 | 117 |
119 void DevToolsProtocolHandler::HandleCommand( | 118 void DevToolsProtocolHandler::HandleCommand( |
120 int session_id, | |
121 scoped_ptr<base::DictionaryValue> command) { | 119 scoped_ptr<base::DictionaryValue> command) { |
122 int call_id = DevToolsCommandId::kNoId; | 120 int id = DevToolsProtocolClient::kNoId; |
123 std::string method; | 121 std::string method; |
124 command->GetInteger(kIdParam, &call_id); | 122 command->GetInteger(kIdParam, &id); |
125 command->GetString(kMethodParam, &method); | 123 command->GetString(kMethodParam, &method); |
126 DevToolsProtocolDispatcher::CommandHandler command_handler( | 124 DevToolsProtocolDispatcher::CommandHandler command_handler( |
127 dispatcher_.FindCommandHandler(method)); | 125 dispatcher_.FindCommandHandler(method)); |
128 if (command_handler.is_null()) { | 126 if (command_handler.is_null()) { |
129 client_.SendError(DevToolsCommandId(call_id, session_id), | 127 client_.SendError(id, Response(kStatusNoSuchMethod, "No such method")); |
130 Response(kStatusNoSuchMethod, "No such method")); | |
131 return; | 128 return; |
132 } | 129 } |
133 | 130 |
134 bool result = | 131 bool result = |
135 command_handler.Run(DevToolsCommandId(call_id, session_id), | 132 command_handler.Run(id, TakeDictionary(command.get(), kParamsParam)); |
136 TakeDictionary(command.get(), kParamsParam)); | |
137 DCHECK(result); | 133 DCHECK(result); |
138 } | 134 } |
139 | 135 |
140 bool DevToolsProtocolHandler::HandleOptionalCommand( | 136 bool DevToolsProtocolHandler::HandleOptionalCommand( |
141 int session_id, | 137 scoped_ptr<base::DictionaryValue> command, int* call_id) { |
142 scoped_ptr<base::DictionaryValue> command, | 138 *call_id = DevToolsProtocolClient::kNoId; |
143 int* call_id) { | |
144 *call_id = DevToolsCommandId::kNoId; | |
145 std::string method; | 139 std::string method; |
146 command->GetInteger(kIdParam, call_id); | 140 command->GetInteger(kIdParam, call_id); |
147 command->GetString(kMethodParam, &method); | 141 command->GetString(kMethodParam, &method); |
148 DevToolsProtocolDispatcher::CommandHandler command_handler( | 142 DevToolsProtocolDispatcher::CommandHandler command_handler( |
149 dispatcher_.FindCommandHandler(method)); | 143 dispatcher_.FindCommandHandler(method)); |
150 if (!command_handler.is_null()) { | 144 if (!command_handler.is_null()) { |
151 return command_handler.Run(DevToolsCommandId(*call_id, session_id), | 145 return command_handler.Run( |
152 TakeDictionary(command.get(), kParamsParam)); | 146 *call_id, TakeDictionary(command.get(), kParamsParam)); |
153 } | 147 } |
154 return false; | 148 return false; |
155 } | 149 } |
156 | 150 |
157 } // namespace content | 151 } // namespace content |
OLD | NEW |