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); |
35 command.SetString(kMethodParam, method); | 33 command.SetString(kMethodParam, method); |
36 if (params) | 34 if (params) |
37 command.Set(kParamsParam, params.release()); | 35 command.Set(kParamsParam, params.release()); |
38 | 36 |
39 std::string json_command; | 37 std::string json_command; |
40 base::JSONWriter::Write(command, &json_command); | 38 base::JSONWriter::Write(command, &json_command); |
41 return json_command; | 39 return json_command; |
42 } | 40 } |
43 | 41 |
44 // static | 42 // static |
45 std::unique_ptr<base::DictionaryValue> | 43 std::unique_ptr<base::DictionaryValue> |
46 DevToolsProtocol::CreateInvalidParamsResponse(int command_id, | 44 DevToolsProtocol::CreateInvalidParamsResponse(int command_id, |
47 const std::string& param) { | 45 const std::string& param) { |
48 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 46 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
| 47 response->SetInteger(kIdParam, command_id); |
49 base::DictionaryValue* error_object = new base::DictionaryValue(); | 48 base::DictionaryValue* error_object = new base::DictionaryValue(); |
50 response->Set(kErrorParam, error_object); | 49 response->Set(kErrorParam, error_object); |
51 error_object->SetInteger(kErrorCodeParam, kErrorInvalidParams); | 50 error_object->SetInteger(kErrorCodeParam, kErrorInvalidParams); |
52 error_object->SetString(kErrorMessageParam, | 51 error_object->SetString(kErrorMessageParam, |
53 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); | 52 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); |
54 | 53 |
55 return response; | 54 return response; |
56 } | 55 } |
57 | 56 |
58 // static | 57 // static |
| 58 std::unique_ptr<base::DictionaryValue> DevToolsProtocol::CreateErrorResponse( |
| 59 int command_id, |
| 60 const std::string& error_message) { |
| 61 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
| 62 response->SetInteger(kIdParam, command_id); |
| 63 base::DictionaryValue* error_object = new base::DictionaryValue(); |
| 64 response->Set(kErrorParam, error_object); |
| 65 error_object->SetInteger(kErrorCodeParam, kErrorServerError); |
| 66 error_object->SetString(kErrorMessageParam, error_message); |
| 67 return response; |
| 68 } |
| 69 |
| 70 // static |
59 std::unique_ptr<base::DictionaryValue> DevToolsProtocol::CreateSuccessResponse( | 71 std::unique_ptr<base::DictionaryValue> DevToolsProtocol::CreateSuccessResponse( |
60 int command_id, | 72 int command_id, |
61 std::unique_ptr<base::DictionaryValue> result) { | 73 std::unique_ptr<base::DictionaryValue> result) { |
62 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 74 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
63 response->SetInteger(kIdParam, command_id); | 75 response->SetInteger(kIdParam, command_id); |
64 response->Set(kResultParam, | 76 response->Set(kResultParam, |
65 result ? result.release() : new base::DictionaryValue()); | 77 result ? result.release() : new base::DictionaryValue()); |
66 | 78 |
67 return response; | 79 return response; |
68 } | 80 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 | 135 |
124 if (!dict->GetInteger(kIdParam, command_id)) | 136 if (!dict->GetInteger(kIdParam, command_id)) |
125 return false; | 137 return false; |
126 | 138 |
127 *error_code = 0; | 139 *error_code = 0; |
128 base::DictionaryValue* error_dict = nullptr; | 140 base::DictionaryValue* error_dict = nullptr; |
129 if (dict->GetDictionary(kErrorParam, &error_dict)) | 141 if (dict->GetDictionary(kErrorParam, &error_dict)) |
130 error_dict->GetInteger(kErrorCodeParam, error_code); | 142 error_dict->GetInteger(kErrorCodeParam, error_code); |
131 return true; | 143 return true; |
132 } | 144 } |
OLD | NEW |