| 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/devtools_protocol_handler.h" | 5 #include "content/browser/devtools/devtools_protocol_handler.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/memory/ptr_util.h" |
| 12 #include "content/browser/devtools/devtools_agent_host_impl.h" | 13 #include "content/browser/devtools/devtools_agent_host_impl.h" |
| 13 #include "content/browser/devtools/devtools_manager.h" | 14 #include "content/browser/devtools/devtools_manager.h" |
| 14 #include "content/public/browser/devtools_manager_delegate.h" | 15 #include "content/public/browser/devtools_manager_delegate.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 const char kIdParam[] = "id"; | 21 const char kIdParam[] = "id"; |
| 21 const char kMethodParam[] = "method"; | 22 const char kMethodParam[] = "method"; |
| 22 const char kParamsParam[] = "params"; | 23 const char kParamsParam[] = "params"; |
| 23 | 24 |
| 24 // 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 |
| 25 const int kStatusParseError = -32700; | 26 const int kStatusParseError = -32700; |
| 26 const int kStatusInvalidRequest = -32600; | 27 const int kStatusInvalidRequest = -32600; |
| 27 const int kStatusNoSuchMethod = -32601; | 28 const int kStatusNoSuchMethod = -32601; |
| 28 | 29 |
| 29 scoped_ptr<base::DictionaryValue> TakeDictionary(base::DictionaryValue* dict, | 30 std::unique_ptr<base::DictionaryValue> TakeDictionary( |
| 30 const std::string& key) { | 31 base::DictionaryValue* dict, |
| 31 scoped_ptr<base::Value> value; | 32 const std::string& key) { |
| 33 std::unique_ptr<base::Value> value; |
| 32 dict->Remove(key, &value); | 34 dict->Remove(key, &value); |
| 33 base::DictionaryValue* result = nullptr; | 35 base::DictionaryValue* result = nullptr; |
| 34 if (value) | 36 if (value) |
| 35 value.release()->GetAsDictionary(&result); | 37 value.release()->GetAsDictionary(&result); |
| 36 return make_scoped_ptr(result); | 38 return base::WrapUnique(result); |
| 37 } | 39 } |
| 38 | 40 |
| 39 } // namespace | 41 } // namespace |
| 40 | 42 |
| 41 DevToolsProtocolHandler::DevToolsProtocolHandler( | 43 DevToolsProtocolHandler::DevToolsProtocolHandler( |
| 42 DevToolsAgentHostImpl* agent_host) | 44 DevToolsAgentHostImpl* agent_host) |
| 43 : agent_host_(agent_host), client_(agent_host), dispatcher_(agent_host) {} | 45 : agent_host_(agent_host), client_(agent_host), dispatcher_(agent_host) {} |
| 44 | 46 |
| 45 DevToolsProtocolHandler::~DevToolsProtocolHandler() { | 47 DevToolsProtocolHandler::~DevToolsProtocolHandler() { |
| 46 } | 48 } |
| 47 | 49 |
| 48 void DevToolsProtocolHandler::HandleMessage(int session_id, | 50 void DevToolsProtocolHandler::HandleMessage(int session_id, |
| 49 const std::string& message) { | 51 const std::string& message) { |
| 50 scoped_ptr<base::DictionaryValue> command = ParseCommand(session_id, message); | 52 std::unique_ptr<base::DictionaryValue> command = |
| 53 ParseCommand(session_id, message); |
| 51 if (!command) | 54 if (!command) |
| 52 return; | 55 return; |
| 53 if (PassCommandToDelegate(session_id, command.get())) | 56 if (PassCommandToDelegate(session_id, command.get())) |
| 54 return; | 57 return; |
| 55 HandleCommand(session_id, std::move(command)); | 58 HandleCommand(session_id, std::move(command)); |
| 56 } | 59 } |
| 57 | 60 |
| 58 bool DevToolsProtocolHandler::HandleOptionalMessage(int session_id, | 61 bool DevToolsProtocolHandler::HandleOptionalMessage(int session_id, |
| 59 const std::string& message, | 62 const std::string& message, |
| 60 int* call_id) { | 63 int* call_id) { |
| 61 scoped_ptr<base::DictionaryValue> command = ParseCommand(session_id, message); | 64 std::unique_ptr<base::DictionaryValue> command = |
| 65 ParseCommand(session_id, message); |
| 62 if (!command) | 66 if (!command) |
| 63 return true; | 67 return true; |
| 64 if (PassCommandToDelegate(session_id, command.get())) | 68 if (PassCommandToDelegate(session_id, command.get())) |
| 65 return true; | 69 return true; |
| 66 return HandleOptionalCommand(session_id, std::move(command), call_id); | 70 return HandleOptionalCommand(session_id, std::move(command), call_id); |
| 67 } | 71 } |
| 68 | 72 |
| 69 bool DevToolsProtocolHandler::PassCommandToDelegate( | 73 bool DevToolsProtocolHandler::PassCommandToDelegate( |
| 70 int session_id, | 74 int session_id, |
| 71 base::DictionaryValue* command) { | 75 base::DictionaryValue* command) { |
| 72 DevToolsManagerDelegate* delegate = | 76 DevToolsManagerDelegate* delegate = |
| 73 DevToolsManager::GetInstance()->delegate(); | 77 DevToolsManager::GetInstance()->delegate(); |
| 74 if (!delegate) | 78 if (!delegate) |
| 75 return false; | 79 return false; |
| 76 | 80 |
| 77 scoped_ptr<base::DictionaryValue> response( | 81 std::unique_ptr<base::DictionaryValue> response( |
| 78 delegate->HandleCommand(agent_host_, command)); | 82 delegate->HandleCommand(agent_host_, command)); |
| 79 if (response) { | 83 if (response) { |
| 80 client_.SendMessage(session_id, *response); | 84 client_.SendMessage(session_id, *response); |
| 81 return true; | 85 return true; |
| 82 } | 86 } |
| 83 | 87 |
| 84 return false; | 88 return false; |
| 85 } | 89 } |
| 86 | 90 |
| 87 scoped_ptr<base::DictionaryValue> DevToolsProtocolHandler::ParseCommand( | 91 std::unique_ptr<base::DictionaryValue> DevToolsProtocolHandler::ParseCommand( |
| 88 int session_id, | 92 int session_id, |
| 89 const std::string& message) { | 93 const std::string& message) { |
| 90 scoped_ptr<base::Value> value = base::JSONReader::Read(message); | 94 std::unique_ptr<base::Value> value = base::JSONReader::Read(message); |
| 91 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { | 95 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 92 client_.SendError( | 96 client_.SendError( |
| 93 DevToolsCommandId(DevToolsCommandId::kNoId, session_id), | 97 DevToolsCommandId(DevToolsCommandId::kNoId, session_id), |
| 94 Response(kStatusParseError, "Message must be in JSON format")); | 98 Response(kStatusParseError, "Message must be in JSON format")); |
| 95 return nullptr; | 99 return nullptr; |
| 96 } | 100 } |
| 97 | 101 |
| 98 scoped_ptr<base::DictionaryValue> command = | 102 std::unique_ptr<base::DictionaryValue> command = |
| 99 make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); | 103 base::WrapUnique(static_cast<base::DictionaryValue*>(value.release())); |
| 100 int call_id = DevToolsCommandId::kNoId; | 104 int call_id = DevToolsCommandId::kNoId; |
| 101 bool ok = command->GetInteger(kIdParam, &call_id) && call_id >= 0; | 105 bool ok = command->GetInteger(kIdParam, &call_id) && call_id >= 0; |
| 102 if (!ok) { | 106 if (!ok) { |
| 103 client_.SendError(DevToolsCommandId(call_id, session_id), | 107 client_.SendError(DevToolsCommandId(call_id, session_id), |
| 104 Response(kStatusInvalidRequest, | 108 Response(kStatusInvalidRequest, |
| 105 "The type of 'id' property must be number")); | 109 "The type of 'id' property must be number")); |
| 106 return nullptr; | 110 return nullptr; |
| 107 } | 111 } |
| 108 | 112 |
| 109 std::string method; | 113 std::string method; |
| 110 ok = command->GetString(kMethodParam, &method); | 114 ok = command->GetString(kMethodParam, &method); |
| 111 if (!ok) { | 115 if (!ok) { |
| 112 client_.SendError(DevToolsCommandId(call_id, session_id), | 116 client_.SendError(DevToolsCommandId(call_id, session_id), |
| 113 Response(kStatusInvalidRequest, | 117 Response(kStatusInvalidRequest, |
| 114 "The type of 'method' property must be string")); | 118 "The type of 'method' property must be string")); |
| 115 return nullptr; | 119 return nullptr; |
| 116 } | 120 } |
| 117 | 121 |
| 118 return command; | 122 return command; |
| 119 } | 123 } |
| 120 | 124 |
| 121 void DevToolsProtocolHandler::HandleCommand( | 125 void DevToolsProtocolHandler::HandleCommand( |
| 122 int session_id, | 126 int session_id, |
| 123 scoped_ptr<base::DictionaryValue> command) { | 127 std::unique_ptr<base::DictionaryValue> command) { |
| 124 int call_id = DevToolsCommandId::kNoId; | 128 int call_id = DevToolsCommandId::kNoId; |
| 125 std::string method; | 129 std::string method; |
| 126 command->GetInteger(kIdParam, &call_id); | 130 command->GetInteger(kIdParam, &call_id); |
| 127 command->GetString(kMethodParam, &method); | 131 command->GetString(kMethodParam, &method); |
| 128 DevToolsProtocolDispatcher::CommandHandler command_handler( | 132 DevToolsProtocolDispatcher::CommandHandler command_handler( |
| 129 dispatcher_.FindCommandHandler(method)); | 133 dispatcher_.FindCommandHandler(method)); |
| 130 if (command_handler.is_null()) { | 134 if (command_handler.is_null()) { |
| 131 client_.SendError(DevToolsCommandId(call_id, session_id), | 135 client_.SendError(DevToolsCommandId(call_id, session_id), |
| 132 Response(kStatusNoSuchMethod, "No such method")); | 136 Response(kStatusNoSuchMethod, "No such method")); |
| 133 return; | 137 return; |
| 134 } | 138 } |
| 135 | 139 |
| 136 bool result = | 140 bool result = |
| 137 command_handler.Run(DevToolsCommandId(call_id, session_id), | 141 command_handler.Run(DevToolsCommandId(call_id, session_id), |
| 138 TakeDictionary(command.get(), kParamsParam)); | 142 TakeDictionary(command.get(), kParamsParam)); |
| 139 DCHECK(result); | 143 DCHECK(result); |
| 140 } | 144 } |
| 141 | 145 |
| 142 bool DevToolsProtocolHandler::HandleOptionalCommand( | 146 bool DevToolsProtocolHandler::HandleOptionalCommand( |
| 143 int session_id, | 147 int session_id, |
| 144 scoped_ptr<base::DictionaryValue> command, | 148 std::unique_ptr<base::DictionaryValue> command, |
| 145 int* call_id) { | 149 int* call_id) { |
| 146 *call_id = DevToolsCommandId::kNoId; | 150 *call_id = DevToolsCommandId::kNoId; |
| 147 std::string method; | 151 std::string method; |
| 148 command->GetInteger(kIdParam, call_id); | 152 command->GetInteger(kIdParam, call_id); |
| 149 command->GetString(kMethodParam, &method); | 153 command->GetString(kMethodParam, &method); |
| 150 DevToolsProtocolDispatcher::CommandHandler command_handler( | 154 DevToolsProtocolDispatcher::CommandHandler command_handler( |
| 151 dispatcher_.FindCommandHandler(method)); | 155 dispatcher_.FindCommandHandler(method)); |
| 152 if (!command_handler.is_null()) { | 156 if (!command_handler.is_null()) { |
| 153 return command_handler.Run(DevToolsCommandId(*call_id, session_id), | 157 return command_handler.Run(DevToolsCommandId(*call_id, session_id), |
| 154 TakeDictionary(command.get(), kParamsParam)); | 158 TakeDictionary(command.get(), kParamsParam)); |
| 155 } | 159 } |
| 156 return false; | 160 return false; |
| 157 } | 161 } |
| 158 | 162 |
| 159 } // namespace content | 163 } // namespace content |
| OLD | NEW |