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 <utility> |
| 8 |
7 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/memory/ptr_util.h" |
9 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
10 | 13 |
11 namespace { | 14 namespace { |
12 | 15 |
13 const char kErrorCodeParam[] = "code"; | 16 const char kErrorCodeParam[] = "code"; |
14 const char kErrorParam[] = "error"; | 17 const char kErrorParam[] = "error"; |
15 const char kErrorMessageParam[] = "message"; | 18 const char kErrorMessageParam[] = "message"; |
16 const char kIdParam[] = "id"; | 19 const char kIdParam[] = "id"; |
17 const char kMethodParam[] = "method"; | 20 const char kMethodParam[] = "method"; |
18 const char kParamsParam[] = "params"; | 21 const char kParamsParam[] = "params"; |
19 const char kResultParam[] = "result"; | 22 const char kResultParam[] = "result"; |
20 | 23 |
21 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object | 24 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object |
22 enum Error { kErrorInvalidParams = -32602, kErrorServerError = -32000 }; | 25 enum Error { kErrorInvalidParams = -32602, kErrorServerError = -32000 }; |
23 | 26 |
24 } // namespace | 27 } // namespace |
25 | 28 |
26 // static | 29 // static |
27 std::string DevToolsProtocol::SerializeCommand( | 30 std::string DevToolsProtocol::SerializeCommand( |
28 int command_id, | 31 int command_id, |
29 const std::string& method, | 32 const std::string& method, |
30 std::unique_ptr<base::DictionaryValue> params) { | 33 std::unique_ptr<base::DictionaryValue> params) { |
31 base::DictionaryValue command; | 34 base::DictionaryValue command; |
32 command.SetInteger(kIdParam, command_id); | 35 command.SetInteger(kIdParam, command_id); |
33 command.SetString(kMethodParam, method); | 36 command.SetString(kMethodParam, method); |
34 if (params) | 37 if (params) |
35 command.Set(kParamsParam, params.release()); | 38 command.Set(kParamsParam, std::move(params)); |
36 | 39 |
37 std::string json_command; | 40 std::string json_command; |
38 base::JSONWriter::Write(command, &json_command); | 41 base::JSONWriter::Write(command, &json_command); |
39 return json_command; | 42 return json_command; |
40 } | 43 } |
41 | 44 |
42 // static | 45 // static |
43 std::unique_ptr<base::DictionaryValue> | 46 std::unique_ptr<base::DictionaryValue> |
44 DevToolsProtocol::CreateInvalidParamsResponse(int command_id, | 47 DevToolsProtocol::CreateInvalidParamsResponse(int command_id, |
45 const std::string& param) { | 48 const std::string& param) { |
46 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 49 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
47 response->SetInteger(kIdParam, command_id); | 50 response->SetInteger(kIdParam, command_id); |
48 base::DictionaryValue* error_object = new base::DictionaryValue(); | 51 auto error_object = base::MakeUnique<base::DictionaryValue>(); |
49 response->Set(kErrorParam, error_object); | |
50 error_object->SetInteger(kErrorCodeParam, kErrorInvalidParams); | 52 error_object->SetInteger(kErrorCodeParam, kErrorInvalidParams); |
51 error_object->SetString(kErrorMessageParam, | 53 error_object->SetString(kErrorMessageParam, |
52 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); | 54 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str())); |
| 55 response->Set(kErrorParam, std::move(error_object)); |
53 | 56 |
54 return response; | 57 return response; |
55 } | 58 } |
56 | 59 |
57 // static | 60 // static |
58 std::unique_ptr<base::DictionaryValue> DevToolsProtocol::CreateErrorResponse( | 61 std::unique_ptr<base::DictionaryValue> DevToolsProtocol::CreateErrorResponse( |
59 int command_id, | 62 int command_id, |
60 const std::string& error_message) { | 63 const std::string& error_message) { |
61 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 64 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
62 response->SetInteger(kIdParam, command_id); | 65 response->SetInteger(kIdParam, command_id); |
63 base::DictionaryValue* error_object = new base::DictionaryValue(); | 66 base::DictionaryValue* error_object = new base::DictionaryValue(); |
64 response->Set(kErrorParam, error_object); | 67 response->Set(kErrorParam, error_object); |
65 error_object->SetInteger(kErrorCodeParam, kErrorServerError); | 68 error_object->SetInteger(kErrorCodeParam, kErrorServerError); |
66 error_object->SetString(kErrorMessageParam, error_message); | 69 error_object->SetString(kErrorMessageParam, error_message); |
67 return response; | 70 return response; |
68 } | 71 } |
69 | 72 |
70 // static | 73 // static |
71 std::unique_ptr<base::DictionaryValue> DevToolsProtocol::CreateSuccessResponse( | 74 std::unique_ptr<base::DictionaryValue> DevToolsProtocol::CreateSuccessResponse( |
72 int command_id, | 75 int command_id, |
73 std::unique_ptr<base::DictionaryValue> result) { | 76 std::unique_ptr<base::DictionaryValue> result) { |
74 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); | 77 std::unique_ptr<base::DictionaryValue> response(new base::DictionaryValue()); |
75 response->SetInteger(kIdParam, command_id); | 78 response->SetInteger(kIdParam, command_id); |
76 response->Set(kResultParam, | 79 response->Set(kResultParam, result |
77 result ? result.release() : new base::DictionaryValue()); | 80 ? std::move(result) |
| 81 : base::MakeUnique<base::DictionaryValue>()); |
78 | 82 |
79 return response; | 83 return response; |
80 } | 84 } |
81 | 85 |
82 // static | 86 // static |
83 bool DevToolsProtocol::ParseCommand(base::DictionaryValue* command, | 87 bool DevToolsProtocol::ParseCommand(base::DictionaryValue* command, |
84 int* command_id, | 88 int* command_id, |
85 std::string* method, | 89 std::string* method, |
86 base::DictionaryValue** params) { | 90 base::DictionaryValue** params) { |
87 if (!command) | 91 if (!command) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 | 139 |
136 if (!dict->GetInteger(kIdParam, command_id)) | 140 if (!dict->GetInteger(kIdParam, command_id)) |
137 return false; | 141 return false; |
138 | 142 |
139 *error_code = 0; | 143 *error_code = 0; |
140 base::DictionaryValue* error_dict = nullptr; | 144 base::DictionaryValue* error_dict = nullptr; |
141 if (dict->GetDictionary(kErrorParam, &error_dict)) | 145 if (dict->GetDictionary(kErrorParam, &error_dict)) |
142 error_dict->GetInteger(kErrorCodeParam, error_code); | 146 error_dict->GetInteger(kErrorCodeParam, error_code); |
143 return true; | 147 return true; |
144 } | 148 } |
OLD | NEW |