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