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 | 10 |
10 namespace { | 11 namespace { |
| 12 |
| 13 const char kErrorCodeParam[] = "code"; |
| 14 const char kErrorParam[] = "error"; |
| 15 const char kErrorMessageParam[] = "message"; |
11 const char kIdParam[] = "id"; | 16 const char kIdParam[] = "id"; |
12 const char kMethodParam[] = "method"; | 17 const char kMethodParam[] = "method"; |
13 const char kParamsParam[] = "params"; | 18 const char kParamsParam[] = "params"; |
14 const char kErrorParam[] = "error"; | 19 const char kResultParam[] = "result"; |
15 const char kErrorCodeParam[] = "code"; | 20 |
| 21 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object |
| 22 enum Error { |
| 23 kErrorInvalidParams = -32602 |
| 24 }; |
| 25 |
16 } // namespace | 26 } // namespace |
17 | 27 |
18 DevToolsProtocol::Message::~Message() { | 28 DevToolsProtocol::Message::~Message() { |
19 } | 29 } |
20 | 30 |
21 DevToolsProtocol::Message::Message(const std::string& method, | 31 DevToolsProtocol::Message::Message(const std::string& method, |
22 base::DictionaryValue* params) | 32 base::DictionaryValue* params) |
23 : method_(method), | 33 : method_(method), |
24 params_(params ? params->DeepCopy() : NULL) { | 34 params_(params ? params->DeepCopy() : NULL) { |
25 } | 35 } |
(...skipping 13 matching lines...) Expand all Loading... |
39 command.SetInteger(kIdParam, id_); | 49 command.SetInteger(kIdParam, id_); |
40 command.SetString(kMethodParam, method_); | 50 command.SetString(kMethodParam, method_); |
41 if (params_) | 51 if (params_) |
42 command.Set(kParamsParam, params_->DeepCopy()); | 52 command.Set(kParamsParam, params_->DeepCopy()); |
43 | 53 |
44 std::string json_command; | 54 std::string json_command; |
45 base::JSONWriter::Write(&command, &json_command); | 55 base::JSONWriter::Write(&command, &json_command); |
46 return json_command; | 56 return json_command; |
47 } | 57 } |
48 | 58 |
| 59 scoped_ptr<DevToolsProtocol::Response> |
| 60 DevToolsProtocol::Command::SuccessResponse(base::DictionaryValue* result) { |
| 61 return scoped_ptr<DevToolsProtocol::Response>( |
| 62 new DevToolsProtocol::Response(id_, result)); |
| 63 } |
| 64 |
| 65 scoped_ptr<DevToolsProtocol::Response> |
| 66 DevToolsProtocol::Command::InvalidParamResponse(const std::string& param) { |
| 67 std::string message = |
| 68 base::StringPrintf("Missing or invalid '%s' parameter", param.c_str()); |
| 69 return scoped_ptr<DevToolsProtocol::Response>( |
| 70 new DevToolsProtocol::Response(id_, kErrorInvalidParams, message)); |
| 71 } |
| 72 |
49 DevToolsProtocol::Notification::~Notification() { | 73 DevToolsProtocol::Notification::~Notification() { |
50 } | 74 } |
51 | 75 |
52 DevToolsProtocol::Notification::Notification(const std::string& method, | 76 DevToolsProtocol::Notification::Notification(const std::string& method, |
53 base::DictionaryValue* params) | 77 base::DictionaryValue* params) |
54 : Message(method, params) { | 78 : Message(method, params) { |
55 } | 79 } |
56 | 80 |
57 DevToolsProtocol::Response::~Response() { | 81 DevToolsProtocol::Response::~Response() { |
58 } | 82 } |
59 | 83 |
60 DevToolsProtocol::Response::Response(int id, int error_code) | 84 DevToolsProtocol::Response::Response(int id, |
| 85 int error_code, |
| 86 const std::string error_message) |
61 : id_(id), | 87 : id_(id), |
62 error_code_(error_code) { | 88 error_code_(error_code), |
| 89 error_message_(error_message) { |
| 90 } |
| 91 |
| 92 DevToolsProtocol::Response::Response(int id, base::DictionaryValue* result) |
| 93 : id_(id), |
| 94 error_code_(0), |
| 95 result_(result) { |
| 96 } |
| 97 |
| 98 base::DictionaryValue* DevToolsProtocol::Response::Serialize() { |
| 99 base::DictionaryValue* response = new base::DictionaryValue(); |
| 100 |
| 101 response->SetInteger(kIdParam, id_); |
| 102 |
| 103 if (error_code_) { |
| 104 base::DictionaryValue* error_object = new base::DictionaryValue(); |
| 105 response->Set(kErrorParam, error_object); |
| 106 error_object->SetInteger(kErrorCodeParam, error_code_); |
| 107 if (!error_message_.empty()) |
| 108 error_object->SetString(kErrorMessageParam, error_message_); |
| 109 } else if (result_) { |
| 110 response->Set(kResultParam, result_->DeepCopy()); |
| 111 } |
| 112 |
| 113 return response; |
63 } | 114 } |
64 | 115 |
65 // static | 116 // static |
| 117 DevToolsProtocol::Command* DevToolsProtocol::ParseCommand( |
| 118 base::DictionaryValue* command_dict) { |
| 119 if (!command_dict) |
| 120 return NULL; |
| 121 |
| 122 int id; |
| 123 if (!command_dict->GetInteger(kIdParam, &id) || id < 0) |
| 124 return NULL; |
| 125 |
| 126 std::string method; |
| 127 if (!command_dict->GetString(kMethodParam, &method)) |
| 128 return NULL; |
| 129 |
| 130 base::DictionaryValue* params = NULL; |
| 131 command_dict->GetDictionary(kParamsParam, ¶ms); |
| 132 return new Command(id, method, params ? params->DeepCopy() : NULL); |
| 133 } |
| 134 |
| 135 // static |
66 DevToolsProtocol::Notification* DevToolsProtocol::ParseNotification( | 136 DevToolsProtocol::Notification* DevToolsProtocol::ParseNotification( |
67 const std::string& json) { | 137 const std::string& json) { |
68 scoped_ptr<base::Value> value(base::JSONReader::Read(json)); | 138 scoped_ptr<base::Value> value(base::JSONReader::Read(json)); |
69 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) | 139 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) |
70 return NULL; | 140 return NULL; |
71 | 141 |
72 scoped_ptr<base::DictionaryValue> dict( | 142 scoped_ptr<base::DictionaryValue> dict( |
73 static_cast<base::DictionaryValue*>(value.release())); | 143 static_cast<base::DictionaryValue*>(value.release())); |
74 | 144 |
75 std::string method; | 145 std::string method; |
(...skipping 15 matching lines...) Expand all Loading... |
91 static_cast<base::DictionaryValue*>(value.release())); | 161 static_cast<base::DictionaryValue*>(value.release())); |
92 | 162 |
93 int id; | 163 int id; |
94 if (!dict->GetInteger(kIdParam, &id)) | 164 if (!dict->GetInteger(kIdParam, &id)) |
95 return NULL; | 165 return NULL; |
96 | 166 |
97 int error_code = 0; | 167 int error_code = 0; |
98 base::DictionaryValue* error_dict = NULL; | 168 base::DictionaryValue* error_dict = NULL; |
99 if (dict->GetDictionary(kErrorParam, &error_dict)) | 169 if (dict->GetDictionary(kErrorParam, &error_dict)) |
100 error_dict->GetInteger(kErrorCodeParam, &error_code); | 170 error_dict->GetInteger(kErrorCodeParam, &error_code); |
101 return new Response(id, error_code); | 171 return new Response(id, error_code, std::string()); |
102 } | 172 } |
OLD | NEW |