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 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 | 10 |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 return false; | 209 return false; |
210 return true; | 210 return true; |
211 } | 211 } |
212 | 212 |
213 // static | 213 // static |
214 scoped_refptr<DevToolsProtocol::Command> DevToolsProtocol::ParseCommand( | 214 scoped_refptr<DevToolsProtocol::Command> DevToolsProtocol::ParseCommand( |
215 const std::string& json, | 215 const std::string& json, |
216 std::string* error_response) { | 216 std::string* error_response) { |
217 scoped_ptr<base::DictionaryValue> command_dict( | 217 scoped_ptr<base::DictionaryValue> command_dict( |
218 ParseMessage(json, error_response)); | 218 ParseMessage(json, error_response)); |
| 219 return ParseCommand(command_dict.get(), error_response); |
| 220 } |
| 221 |
| 222 // static |
| 223 scoped_refptr<DevToolsProtocol::Command> DevToolsProtocol::ParseCommand( |
| 224 base::DictionaryValue* command_dict, |
| 225 std::string* error_response) { |
219 if (!command_dict) | 226 if (!command_dict) |
220 return NULL; | 227 return NULL; |
221 | 228 |
222 int id; | 229 int id; |
223 std::string method; | 230 std::string method; |
224 bool ok = command_dict->GetInteger(kIdParam, &id) && id >= 0; | 231 bool ok = command_dict->GetInteger(kIdParam, &id) && id >= 0; |
225 ok = ok && ParseMethod(command_dict.get(), &method); | 232 ok = ok && ParseMethod(command_dict, &method); |
226 if (!ok) { | 233 if (!ok) { |
227 scoped_refptr<Response> response = | 234 scoped_refptr<Response> response = |
228 new Response(kNoId, kErrorInvalidRequest, "No such method"); | 235 new Response(kNoId, kErrorInvalidRequest, "No such method"); |
229 *error_response = response->Serialize(); | 236 *error_response = response->Serialize(); |
230 return NULL; | 237 return NULL; |
231 } | 238 } |
232 | 239 |
233 base::DictionaryValue* params = NULL; | 240 base::DictionaryValue* params = NULL; |
234 command_dict->GetDictionary(kParamsParam, ¶ms); | 241 command_dict->GetDictionary(kParamsParam, ¶ms); |
235 return new Command(id, method, params ? params->DeepCopy() : NULL); | 242 return new Command(id, method, params ? params->DeepCopy() : NULL); |
236 } | 243 } |
237 | 244 |
238 // static | 245 // static |
239 scoped_refptr<DevToolsProtocol::Command> | 246 scoped_refptr<DevToolsProtocol::Command> |
240 DevToolsProtocol::CreateCommand( | 247 DevToolsProtocol::CreateCommand( |
241 int id, | 248 int id, |
242 const std::string& method, | 249 const std::string& method, |
243 base::DictionaryValue* params) { | 250 base::DictionaryValue* params) { |
244 return new Command(id, method, params); | 251 return new Command(id, method, params); |
245 } | 252 } |
246 | 253 |
| 254 //static |
| 255 scoped_refptr<DevToolsProtocol::Response> |
| 256 DevToolsProtocol::ParseResponse( |
| 257 base::DictionaryValue* response_dict) { |
| 258 int id; |
| 259 if (!response_dict->GetInteger(kIdParam, &id)) |
| 260 id = kNoId; |
| 261 |
| 262 int error_code; |
| 263 if (!response_dict->GetInteger(kErrorCodeParam, &error_code)) |
| 264 return new Response(id, kErrorInternalError, "Invalid response"); |
| 265 |
| 266 if (error_code) { |
| 267 std::string error_message; |
| 268 response_dict->GetString(kErrorMessageParam, &error_message); |
| 269 return new Response(id, error_code, error_message); |
| 270 } |
| 271 |
| 272 const base::DictionaryValue* result = NULL; |
| 273 response_dict->GetDictionary(kResultParam, &result); |
| 274 return new Response(id, result ? result->DeepCopy() : NULL); |
| 275 } |
| 276 |
247 // static | 277 // static |
248 scoped_refptr<DevToolsProtocol::Notification> | 278 scoped_refptr<DevToolsProtocol::Notification> |
249 DevToolsProtocol::ParseNotification(const std::string& json) { | 279 DevToolsProtocol::ParseNotification(const std::string& json) { |
250 scoped_ptr<base::DictionaryValue> dict(ParseMessage(json, NULL)); | 280 scoped_ptr<base::DictionaryValue> dict(ParseMessage(json, NULL)); |
251 if (!dict) | 281 if (!dict) |
252 return NULL; | 282 return NULL; |
253 | 283 |
254 std::string method; | 284 std::string method; |
255 bool ok = ParseMethod(dict.get(), &method); | 285 bool ok = ParseMethod(dict.get(), &method); |
256 if (!ok) | 286 if (!ok) |
(...skipping 27 matching lines...) Expand all Loading... |
284 new Response(0, kErrorParseError, error_message); | 314 new Response(0, kErrorParseError, error_message); |
285 if (error_response) | 315 if (error_response) |
286 *error_response = response->Serialize(); | 316 *error_response = response->Serialize(); |
287 return NULL; | 317 return NULL; |
288 } | 318 } |
289 | 319 |
290 return static_cast<base::DictionaryValue*>(message.release()); | 320 return static_cast<base::DictionaryValue*>(message.release()); |
291 } | 321 } |
292 | 322 |
293 } // namespace content | 323 } // namespace content |
OLD | NEW |