| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/devtools/protocol/devtools_protocol_client.h" | 5 #include "content/browser/devtools/protocol/devtools_protocol_client.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "content/browser/devtools/protocol/devtools_protocol_delegate.h" | |
| 10 | 9 |
| 11 namespace content { | 10 namespace content { |
| 12 | 11 |
| 13 namespace { | 12 namespace { |
| 14 | 13 |
| 15 const char kIdParam[] = "id"; | 14 const char kIdParam[] = "id"; |
| 16 const char kMethodParam[] = "method"; | 15 const char kMethodParam[] = "method"; |
| 17 const char kParamsParam[] = "params"; | 16 const char kParamsParam[] = "params"; |
| 18 const char kResultParam[] = "result"; | 17 const char kResultParam[] = "result"; |
| 19 const char kErrorParam[] = "error"; | 18 const char kErrorParam[] = "error"; |
| 20 const char kErrorCodeParam[] = "code"; | 19 const char kErrorCodeParam[] = "code"; |
| 21 const char kErrorMessageParam[] = "message"; | 20 const char kErrorMessageParam[] = "message"; |
| 22 | 21 |
| 23 // Special values. | 22 // Special values. |
| 24 const int kStatusOk = -1; | 23 const int kStatusOk = -1; |
| 25 const int kStatusFallThrough = -2; | 24 const int kStatusFallThrough = -2; |
| 26 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object | 25 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object |
| 27 const int kStatusInvalidParams = -32602; | 26 const int kStatusInvalidParams = -32602; |
| 28 const int kStatusInternalError = -32603; | 27 const int kStatusInternalError = -32603; |
| 29 const int kStatusServerError = -32000; | 28 const int kStatusServerError = -32000; |
| 30 | 29 |
| 31 } // namespace | 30 } // namespace |
| 32 | 31 |
| 33 // static | 32 // static |
| 34 const int DevToolsCommandId::kNoId = -1; | 33 const DevToolsCommandId DevToolsProtocolClient::kNoId = -1; |
| 35 | 34 |
| 36 DevToolsProtocolClient::DevToolsProtocolClient( | 35 DevToolsProtocolClient::DevToolsProtocolClient( |
| 37 DevToolsProtocolDelegate* notifier) | 36 const RawMessageCallback& raw_message_callback) |
| 38 : notifier_(notifier) {} | 37 : raw_message_callback_(raw_message_callback) { |
| 38 } |
| 39 | 39 |
| 40 DevToolsProtocolClient::~DevToolsProtocolClient() { | 40 DevToolsProtocolClient::~DevToolsProtocolClient() { |
| 41 } | 41 } |
| 42 | 42 |
| 43 void DevToolsProtocolClient::SendRawNotification(const std::string& message) { | 43 void DevToolsProtocolClient::SendRawMessage(const std::string& message) { |
| 44 notifier_->SendProtocolNotification(message); | 44 raw_message_callback_.Run(message); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void DevToolsProtocolClient::SendMessage(int session_id, | 47 void DevToolsProtocolClient::SendMessage(const base::DictionaryValue& message) { |
| 48 const base::DictionaryValue& message) { | |
| 49 std::string json_message; | 48 std::string json_message; |
| 50 base::JSONWriter::Write(message, &json_message); | 49 base::JSONWriter::Write(message, &json_message); |
| 51 notifier_->SendProtocolResponse(session_id, json_message); | 50 SendRawMessage(json_message); |
| 52 } | 51 } |
| 53 | 52 |
| 54 void DevToolsProtocolClient::SendNotification( | 53 void DevToolsProtocolClient::SendNotification( |
| 55 const std::string& method, | 54 const std::string& method, |
| 56 scoped_ptr<base::DictionaryValue> params) { | 55 scoped_ptr<base::DictionaryValue> params) { |
| 57 base::DictionaryValue notification; | 56 base::DictionaryValue notification; |
| 58 notification.SetString(kMethodParam, method); | 57 notification.SetString(kMethodParam, method); |
| 59 if (params) | 58 if (params) |
| 60 notification.Set(kParamsParam, params.release()); | 59 notification.Set(kParamsParam, params.release()); |
| 61 | 60 |
| 62 std::string json_message; | 61 SendMessage(notification); |
| 63 base::JSONWriter::Write(notification, &json_message); | |
| 64 SendRawNotification(json_message); | |
| 65 } | 62 } |
| 66 | 63 |
| 67 void DevToolsProtocolClient::SendSuccess( | 64 void DevToolsProtocolClient::SendSuccess( |
| 68 DevToolsCommandId command_id, | 65 DevToolsCommandId command_id, |
| 69 scoped_ptr<base::DictionaryValue> params) { | 66 scoped_ptr<base::DictionaryValue> params) { |
| 70 base::DictionaryValue response; | 67 base::DictionaryValue response; |
| 71 response.SetInteger(kIdParam, command_id.call_id); | 68 response.SetInteger(kIdParam, command_id); |
| 72 | 69 |
| 73 response.Set(kResultParam, | 70 response.Set(kResultParam, |
| 74 params ? params.release() : new base::DictionaryValue()); | 71 params ? params.release() : new base::DictionaryValue()); |
| 75 | 72 |
| 76 SendMessage(command_id.session_id, response); | 73 SendMessage(response); |
| 77 } | 74 } |
| 78 | 75 |
| 79 bool DevToolsProtocolClient::SendError(DevToolsCommandId command_id, | 76 bool DevToolsProtocolClient::SendError(DevToolsCommandId command_id, |
| 80 const Response& response) { | 77 const Response& response) { |
| 81 if (response.status() == kStatusOk || | 78 if (response.status() == kStatusOk || |
| 82 response.status() == kStatusFallThrough) { | 79 response.status() == kStatusFallThrough) { |
| 83 return false; | 80 return false; |
| 84 } | 81 } |
| 85 base::DictionaryValue dict; | 82 base::DictionaryValue dict; |
| 86 if (command_id.call_id == DevToolsCommandId::kNoId) | 83 if (command_id == kNoId) |
| 87 dict.Set(kIdParam, base::Value::CreateNullValue()); | 84 dict.Set(kIdParam, base::Value::CreateNullValue()); |
| 88 else | 85 else |
| 89 dict.SetInteger(kIdParam, command_id.call_id); | 86 dict.SetInteger(kIdParam, command_id); |
| 90 | 87 |
| 91 base::DictionaryValue* error_object = new base::DictionaryValue(); | 88 base::DictionaryValue* error_object = new base::DictionaryValue(); |
| 92 error_object->SetInteger(kErrorCodeParam, response.status()); | 89 error_object->SetInteger(kErrorCodeParam, response.status()); |
| 93 if (!response.message().empty()) | 90 if (!response.message().empty()) |
| 94 error_object->SetString(kErrorMessageParam, response.message()); | 91 error_object->SetString(kErrorMessageParam, response.message()); |
| 95 | 92 |
| 96 dict.Set(kErrorParam, error_object); | 93 dict.Set(kErrorParam, error_object); |
| 97 SendMessage(command_id.session_id, dict); | 94 SendMessage(dict); |
| 98 return true; | 95 return true; |
| 99 } | 96 } |
| 100 | 97 |
| 101 typedef DevToolsProtocolClient::Response Response; | 98 typedef DevToolsProtocolClient::Response Response; |
| 102 | 99 |
| 103 Response Response::FallThrough() { | 100 Response Response::FallThrough() { |
| 104 return Response(kStatusFallThrough); | 101 return Response(kStatusFallThrough); |
| 105 } | 102 } |
| 106 | 103 |
| 107 Response Response::OK() { | 104 Response Response::OK() { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 136 Response::Response(int status) | 133 Response::Response(int status) |
| 137 : status_(status) { | 134 : status_(status) { |
| 138 } | 135 } |
| 139 | 136 |
| 140 Response::Response(int status, const std::string& message) | 137 Response::Response(int status, const std::string& message) |
| 141 : status_(status), | 138 : status_(status), |
| 142 message_(message) { | 139 message_(message) { |
| 143 } | 140 } |
| 144 | 141 |
| 145 } // namespace content | 142 } // namespace content |
| OLD | NEW |