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