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