| 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 "chrome/browser/devtools/devtools_protocol.h" | 5 #include "chrome/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 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 const char kErrorCodeParam[] = "code"; | 13 const char kErrorCodeParam[] = "code"; |
| 14 const char kErrorParam[] = "error"; | 14 const char kErrorParam[] = "error"; |
| 15 const char kErrorMessageParam[] = "message"; | 15 const char kErrorMessageParam[] = "message"; |
| 16 const char kIdParam[] = "id"; | 16 const char kIdParam[] = "id"; |
| 17 const char kMethodParam[] = "method"; | 17 const char kMethodParam[] = "method"; |
| 18 const char kParamsParam[] = "params"; | 18 const char kParamsParam[] = "params"; |
| 19 const char kResultParam[] = "result"; | 19 const char kResultParam[] = "result"; |
| 20 | 20 |
| 21 // 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 |
| 22 enum Error { | 22 enum Error { kErrorInvalidParams = -32602, kErrorServerError = -32000 }; |
| 23 kErrorInvalidParams = -32602 | |
| 24 }; | |
| 25 | 23 |
| 26 } // namespace | 24 } // namespace |
| 27 | 25 |
| 28 // static | 26 // static |
| 29 std::string DevToolsProtocol::SerializeCommand( | 27 std::string DevToolsProtocol::SerializeCommand( |
| 30 int command_id, | 28 int command_id, |
| 31 const std::string& method, | 29 const std::string& method, |
| 32 std::unique_ptr<base::DictionaryValue> params) { | 30 std::unique_ptr<base::DictionaryValue> params) { |
| 33 base::DictionaryValue command; | 31 base::DictionaryValue command; |
| 34 command.SetInteger(kIdParam, command_id); | 32 command.SetInteger(kIdParam, command_id); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 49 base::DictionaryValue* error_object = new base::DictionaryValue(); | 47 base::DictionaryValue* error_object = new base::DictionaryValue(); |
| 50 response->Set(kErrorParam, error_object); | 48 response->Set(kErrorParam, error_object); |
| 51 error_object->SetInteger(kErrorCodeParam, kErrorInvalidParams); | 49 error_object->SetInteger(kErrorCodeParam, kErrorInvalidParams); |
| 52 error_object->SetString(kErrorMessageParam, | 50 error_object->SetString(kErrorMessageParam, |
| 53 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); | 51 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); |
| 54 | 52 |
| 55 return response; | 53 return response; |
| 56 } | 54 } |
| 57 | 55 |
| 58 // static | 56 // static |
| 57 std::unique_ptr<base::DictionaryValue> DevToolsProtocol::CreateErrorResponse( |
| 58 int command_id, |
| 59 const std::string& error_message) { |
| 60 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
| 61 base::DictionaryValue* error_object = new base::DictionaryValue(); |
| 62 response->Set(kErrorParam, error_object); |
| 63 error_object->SetInteger(kErrorCodeParam, kErrorServerError); |
| 64 error_object->SetString(kErrorMessageParam, error_message); |
| 65 return response; |
| 66 } |
| 67 |
| 68 // static |
| 59 std::unique_ptr<base::DictionaryValue> DevToolsProtocol::CreateSuccessResponse( | 69 std::unique_ptr<base::DictionaryValue> DevToolsProtocol::CreateSuccessResponse( |
| 60 int command_id, | 70 int command_id, |
| 61 std::unique_ptr<base::DictionaryValue> result) { | 71 std::unique_ptr<base::DictionaryValue> result) { |
| 62 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 72 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
| 63 response->SetInteger(kIdParam, command_id); | 73 response->SetInteger(kIdParam, command_id); |
| 64 response->Set(kResultParam, | 74 response->Set(kResultParam, |
| 65 result ? result.release() : new base::DictionaryValue()); | 75 result ? result.release() : new base::DictionaryValue()); |
| 66 | 76 |
| 67 return response; | 77 return response; |
| 68 } | 78 } |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 133 |
| 124 if (!dict->GetInteger(kIdParam, command_id)) | 134 if (!dict->GetInteger(kIdParam, command_id)) |
| 125 return false; | 135 return false; |
| 126 | 136 |
| 127 *error_code = 0; | 137 *error_code = 0; |
| 128 base::DictionaryValue* error_dict = nullptr; | 138 base::DictionaryValue* error_dict = nullptr; |
| 129 if (dict->GetDictionary(kErrorParam, &error_dict)) | 139 if (dict->GetDictionary(kErrorParam, &error_dict)) |
| 130 error_dict->GetInteger(kErrorCodeParam, error_code); | 140 error_dict->GetInteger(kErrorCodeParam, error_code); |
| 131 return true; | 141 return true; |
| 132 } | 142 } |
| OLD | NEW |