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