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