Chromium Code Reviews| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "content/browser/devtools/devtools_manager.h" | 10 #include "content/browser/devtools/devtools_manager.h" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 DevToolsProtocolHandler::DevToolsProtocolHandler( | 38 DevToolsProtocolHandler::DevToolsProtocolHandler( |
| 39 DevToolsAgentHost* agent_host, const Notifier& notifier) | 39 DevToolsAgentHost* agent_host, const Notifier& notifier) |
| 40 : agent_host_(agent_host), | 40 : agent_host_(agent_host), |
| 41 client_(notifier), | 41 client_(notifier), |
| 42 dispatcher_(notifier) { | 42 dispatcher_(notifier) { |
| 43 } | 43 } |
| 44 | 44 |
| 45 DevToolsProtocolHandler::~DevToolsProtocolHandler() { | 45 DevToolsProtocolHandler::~DevToolsProtocolHandler() { |
| 46 } | 46 } |
| 47 | 47 |
| 48 void DevToolsProtocolHandler::HandleMessage(const std::string& message) { | 48 void DevToolsProtocolHandler::HandleMessage(int session_id, |
| 49 scoped_ptr<base::DictionaryValue> command = ParseCommand(message); | 49 const std::string& message) { |
| 50 scoped_ptr<base::DictionaryValue> command = ParseCommand(session_id, message); | |
| 50 if (!command) | 51 if (!command) |
| 51 return; | 52 return; |
| 52 if (PassCommandToDelegate(command.get())) | 53 if (PassCommandToDelegate(session_id, command.get())) |
| 53 return; | 54 return; |
| 54 HandleCommand(command.Pass()); | 55 HandleCommand(session_id, command.Pass()); |
| 55 } | 56 } |
| 56 | 57 |
| 57 bool DevToolsProtocolHandler::HandleOptionalMessage( | 58 bool DevToolsProtocolHandler::HandleOptionalMessage(int session_id, |
| 58 const std::string& message, int* call_id) { | 59 const std::string& message, |
| 59 scoped_ptr<base::DictionaryValue> command = ParseCommand(message); | 60 int* call_id) { |
| 61 scoped_ptr<base::DictionaryValue> command = ParseCommand(session_id, message); | |
| 60 if (!command) | 62 if (!command) |
| 61 return true; | 63 return true; |
| 62 if (PassCommandToDelegate(command.get())) | 64 if (PassCommandToDelegate(session_id, command.get())) |
| 63 return true; | 65 return true; |
| 64 return HandleOptionalCommand(command.Pass(), call_id); | 66 return HandleOptionalCommand(session_id, command.Pass(), call_id); |
| 65 } | 67 } |
| 66 | 68 |
| 67 bool DevToolsProtocolHandler::PassCommandToDelegate( | 69 bool DevToolsProtocolHandler::PassCommandToDelegate( |
| 70 int session_id, | |
| 68 base::DictionaryValue* command) { | 71 base::DictionaryValue* command) { |
| 69 DevToolsManagerDelegate* delegate = | 72 DevToolsManagerDelegate* delegate = |
| 70 DevToolsManager::GetInstance()->delegate(); | 73 DevToolsManager::GetInstance()->delegate(); |
| 71 if (!delegate) | 74 if (!delegate) |
| 72 return false; | 75 return false; |
| 73 | 76 |
| 74 scoped_ptr<base::DictionaryValue> response( | 77 scoped_ptr<base::DictionaryValue> response( |
| 75 delegate->HandleCommand(agent_host_, command)); | 78 delegate->HandleCommand(agent_host_, command)); |
| 76 if (response) { | 79 if (response) { |
| 77 std::string json_response; | 80 std::string json_response; |
| 78 base::JSONWriter::Write(*response, &json_response); | 81 base::JSONWriter::Write(*response, &json_response); |
| 79 client_.SendRawMessage(json_response); | 82 client_.SendRawMessage(session_id, json_response); |
| 80 return true; | 83 return true; |
| 81 } | 84 } |
| 82 | 85 |
| 83 return false; | 86 return false; |
| 84 } | 87 } |
| 85 | 88 |
| 86 scoped_ptr<base::DictionaryValue> | 89 scoped_ptr<base::DictionaryValue> DevToolsProtocolHandler::ParseCommand( |
| 87 DevToolsProtocolHandler::ParseCommand(const std::string& message) { | 90 int session_id, |
| 91 const std::string& message) { | |
| 88 scoped_ptr<base::Value> value = base::JSONReader::Read(message); | 92 scoped_ptr<base::Value> value = base::JSONReader::Read(message); |
| 89 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { | 93 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { |
| 90 client_.SendError(DevToolsProtocolClient::kNoId, | 94 client_.SendError( |
| 91 Response(kStatusParseError, | 95 session_id, DevToolsProtocolClient::kNoId, |
| 92 "Message must be in JSON format")); | 96 Response(kStatusParseError, "Message must be in JSON format")); |
| 93 return nullptr; | 97 return nullptr; |
| 94 } | 98 } |
| 95 | 99 |
| 96 scoped_ptr<base::DictionaryValue> command = | 100 scoped_ptr<base::DictionaryValue> command = |
| 97 make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); | 101 make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); |
| 98 int id = DevToolsProtocolClient::kNoId; | 102 int id = DevToolsProtocolClient::kNoId; |
|
dgozman
2015/11/06 22:57:27
nit: let's rename to command_id
kozy
2015/11/07 01:54:43
Done.
| |
| 99 bool ok = command->GetInteger(kIdParam, &id) && id >= 0; | 103 bool ok = command->GetInteger(kIdParam, &id) && id >= 0; |
| 100 if (!ok) { | 104 if (!ok) { |
| 101 client_.SendError(id, Response(kStatusInvalidRequest, | 105 client_.SendError(session_id, id, |
| 102 "The type of 'id' property must be number")); | 106 Response(kStatusInvalidRequest, |
| 107 "The type of 'id' property must be number")); | |
| 103 return nullptr; | 108 return nullptr; |
| 104 } | 109 } |
| 105 | 110 |
| 106 std::string method; | 111 std::string method; |
| 107 ok = command->GetString(kMethodParam, &method); | 112 ok = command->GetString(kMethodParam, &method); |
| 108 if (!ok) { | 113 if (!ok) { |
| 109 client_.SendError(id, | 114 client_.SendError(session_id, id, |
| 110 Response(kStatusInvalidRequest, | 115 Response(kStatusInvalidRequest, |
| 111 "The type of 'method' property must be string")); | 116 "The type of 'method' property must be string")); |
| 112 return nullptr; | 117 return nullptr; |
| 113 } | 118 } |
| 114 | 119 |
| 115 return command; | 120 return command; |
| 116 } | 121 } |
| 117 | 122 |
| 118 void DevToolsProtocolHandler::HandleCommand( | 123 void DevToolsProtocolHandler::HandleCommand( |
| 124 int session_id, | |
| 119 scoped_ptr<base::DictionaryValue> command) { | 125 scoped_ptr<base::DictionaryValue> command) { |
| 120 int id = DevToolsProtocolClient::kNoId; | 126 int id = DevToolsProtocolClient::kNoId; |
| 121 std::string method; | 127 std::string method; |
| 122 command->GetInteger(kIdParam, &id); | 128 command->GetInteger(kIdParam, &id); |
| 123 command->GetString(kMethodParam, &method); | 129 command->GetString(kMethodParam, &method); |
| 124 DevToolsProtocolDispatcher::CommandHandler command_handler( | 130 DevToolsProtocolDispatcher::CommandHandler command_handler( |
| 125 dispatcher_.FindCommandHandler(method)); | 131 dispatcher_.FindCommandHandler(method)); |
| 126 if (command_handler.is_null()) { | 132 if (command_handler.is_null()) { |
| 127 client_.SendError(id, Response(kStatusNoSuchMethod, "No such method")); | 133 client_.SendError(session_id, id, |
| 134 Response(kStatusNoSuchMethod, "No such method")); | |
| 128 return; | 135 return; |
| 129 } | 136 } |
| 130 | 137 |
| 131 bool result = | 138 bool result = command_handler.Run( |
| 132 command_handler.Run(id, TakeDictionary(command.get(), kParamsParam)); | 139 session_id, id, TakeDictionary(command.get(), kParamsParam)); |
| 133 DCHECK(result); | 140 DCHECK(result); |
| 134 } | 141 } |
| 135 | 142 |
| 136 bool DevToolsProtocolHandler::HandleOptionalCommand( | 143 bool DevToolsProtocolHandler::HandleOptionalCommand( |
| 137 scoped_ptr<base::DictionaryValue> command, int* call_id) { | 144 int session_id, |
| 145 scoped_ptr<base::DictionaryValue> command, | |
| 146 int* call_id) { | |
| 138 *call_id = DevToolsProtocolClient::kNoId; | 147 *call_id = DevToolsProtocolClient::kNoId; |
| 139 std::string method; | 148 std::string method; |
| 140 command->GetInteger(kIdParam, call_id); | 149 command->GetInteger(kIdParam, call_id); |
| 141 command->GetString(kMethodParam, &method); | 150 command->GetString(kMethodParam, &method); |
| 142 DevToolsProtocolDispatcher::CommandHandler command_handler( | 151 DevToolsProtocolDispatcher::CommandHandler command_handler( |
| 143 dispatcher_.FindCommandHandler(method)); | 152 dispatcher_.FindCommandHandler(method)); |
| 144 if (!command_handler.is_null()) { | 153 if (!command_handler.is_null()) { |
| 145 return command_handler.Run( | 154 return command_handler.Run(session_id, *call_id, |
| 146 *call_id, TakeDictionary(command.get(), kParamsParam)); | 155 TakeDictionary(command.get(), kParamsParam)); |
| 147 } | 156 } |
| 148 return false; | 157 return false; |
| 149 } | 158 } |
| 150 | 159 |
| 151 } // namespace content | 160 } // namespace content |
| OLD | NEW |