Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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.h" | 5 #include "content/browser/devtools/devtools_protocol.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 | 9 |
| 10 namespace content { | 10 namespace content { |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 params_(params) { | 102 params_(params) { |
| 103 } | 103 } |
| 104 | 104 |
| 105 DevToolsProtocol::Notification::~Notification() { | 105 DevToolsProtocol::Notification::~Notification() { |
| 106 } | 106 } |
| 107 | 107 |
| 108 std::string DevToolsProtocol::Notification::Serialize() { | 108 std::string DevToolsProtocol::Notification::Serialize() { |
| 109 DictionaryValue response; | 109 DictionaryValue response; |
| 110 | 110 |
| 111 base::DictionaryValue notification; | 111 base::DictionaryValue notification; |
| 112 notification.SetString("method", method_); | 112 notification.SetString(kMethodParam, method_); |
| 113 if (params_.get()) | 113 if (params_.get()) |
| 114 notification.Set("params", params_->DeepCopy()); | 114 notification.Set(kParamsParam, params_->DeepCopy()); |
| 115 | 115 |
| 116 std::string json_notification; | 116 std::string json_notification; |
| 117 base::JSONWriter::Write(¬ification, &json_notification); | 117 base::JSONWriter::Write(¬ification, &json_notification); |
| 118 return json_notification; | 118 return json_notification; |
| 119 } | 119 } |
| 120 | 120 |
| 121 DevToolsProtocol::Event::~Event() { | |
| 122 } | |
| 123 | |
| 124 std::string DevToolsProtocol::Event::Serialize() { | |
| 125 DictionaryValue dictionary; | |
| 126 | |
| 127 dictionary.SetString(kMethodParam, method_); | |
| 128 if (params_) | |
| 129 dictionary.Set(kParamsParam, params_->DeepCopy()); | |
| 130 | |
| 131 std::string result; | |
| 132 base::JSONWriter::Write(&dictionary, &result); | |
| 133 return result; | |
| 134 } | |
| 135 | |
| 136 DevToolsProtocol::Event::Event(const std::string& method, | |
| 137 DictionaryValue* params) | |
| 138 : method_(method), | |
| 139 params_(params) { | |
| 140 } | |
| 141 | |
| 121 DevToolsProtocol::Handler::~Handler() { | 142 DevToolsProtocol::Handler::~Handler() { |
| 122 } | 143 } |
| 123 | 144 |
| 124 scoped_ptr<DevToolsProtocol::Response> | 145 scoped_ptr<DevToolsProtocol::Response> |
| 125 DevToolsProtocol::Handler::HandleCommand( | 146 DevToolsProtocol::Handler::HandleCommand( |
| 126 DevToolsProtocol::Command* command) { | 147 DevToolsProtocol::Command* command) { |
| 127 CommandHandlers::iterator it = command_handlers_.find(command->method()); | 148 CommandHandlers::iterator it = command_handlers_.find(command->method()); |
| 128 if (it == command_handlers_.end()) | 149 if (it == command_handlers_.end()) |
| 129 return scoped_ptr<DevToolsProtocol::Response>(); | 150 return scoped_ptr<DevToolsProtocol::Response>(); |
| 130 return (it->second).Run(command); | 151 return (it->second).Run(command); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 *error_response = response.Serialize(); | 187 *error_response = response.Serialize(); |
| 167 return NULL; | 188 return NULL; |
| 168 } | 189 } |
| 169 | 190 |
| 170 base::DictionaryValue* command_dict = NULL; | 191 base::DictionaryValue* command_dict = NULL; |
| 171 command->GetAsDictionary(&command_dict); | 192 command->GetAsDictionary(&command_dict); |
| 172 | 193 |
| 173 int id; | 194 int id; |
| 174 std::string method; | 195 std::string method; |
| 175 bool ok = true; | 196 bool ok = true; |
| 176 ok &= command_dict->GetInteger("id", &id); | 197 ok &= command_dict->GetInteger(kIdParam, &id); |
| 177 ok &= id >= 0; | 198 ok &= id >= 0; |
| 178 ok &= command_dict->GetString("method", &method); | 199 ok &= command_dict->GetString(kMethodParam, &method); |
| 179 if (!ok) { | 200 if (!ok) { |
| 180 Response response(kNoId, kErrorInvalidRequest, "Invalid request"); | 201 Response response(kNoId, kErrorInvalidRequest, "Invalid request"); |
| 181 *error_response = response.Serialize(); | 202 *error_response = response.Serialize(); |
| 182 return NULL; | 203 return NULL; |
| 183 } | 204 } |
| 184 | 205 |
| 185 size_t pos = method.find("."); | 206 size_t pos = method.find("."); |
| 186 if (pos == std::string::npos || pos == 0) { | 207 if (pos == std::string::npos || pos == 0) { |
| 187 Response response(id, kErrorNoSuchMethod, "No such method"); | 208 Response response(id, kErrorNoSuchMethod, "No such method"); |
| 188 *error_response = response.Serialize(); | 209 *error_response = response.Serialize(); |
| 189 return NULL; | 210 return NULL; |
| 190 } | 211 } |
| 191 | 212 |
| 192 std::string domain = method.substr(0, pos); | 213 std::string domain = method.substr(0, pos); |
| 193 | 214 |
| 194 base::DictionaryValue* params = NULL; | 215 base::DictionaryValue* params = NULL; |
| 195 command_dict->GetDictionary("params", ¶ms); | 216 command_dict->GetDictionary(kParamsParam, ¶ms); |
| 196 return new Command(id, domain, method, params ? params->DeepCopy() : NULL); | 217 return new Command(id, domain, method, params ? params->DeepCopy() : NULL); |
| 197 } | 218 } |
| 198 | 219 |
| 220 //static | |
| 221 DevToolsProtocol::Event* DevToolsProtocol::CreateEvent( | |
|
pfeldman
2013/03/21 06:30:08
Make sure order of implementation matches order of
Vladislav Kaznacheev
2013/03/21 07:24:02
I believe it does.
On 2013/03/21 06:30:08, pfeldma
| |
| 222 const std::string& method, | |
| 223 base::DictionaryValue* params) { | |
| 224 return new Event(method, params); | |
| 225 } | |
| 226 | |
| 199 } // namespace content | 227 } // namespace content |
| OLD | NEW |