| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_session.h" | 5 #include "content/browser/devtools/devtools_session.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "content/browser/devtools/devtools_agent_host_impl.h" | 9 #include "content/browser/devtools/devtools_agent_host_impl.h" |
| 10 #include "content/browser/devtools/devtools_manager.h" | 10 #include "content/browser/devtools/devtools_manager.h" |
| 11 #include "content/browser/devtools/protocol/protocol.h" | 11 #include "content/browser/devtools/protocol/protocol.h" |
| 12 #include "content/public/browser/devtools_manager_delegate.h" | 12 #include "content/public/browser/devtools_manager_delegate.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 | 15 |
| 16 DevToolsSession::DevToolsSession( | 16 DevToolsSession::DevToolsSession(DevToolsAgentHostImpl* agent_host, |
| 17 DevToolsAgentHostImpl* agent_host, | 17 DevToolsAgentHostClient* client, |
| 18 DevToolsAgentHostClient* client, | 18 int session_id) |
| 19 int session_id) | |
| 20 : agent_host_(agent_host), | 19 : agent_host_(agent_host), |
| 21 client_(client), | 20 client_(client), |
| 22 session_id_(session_id), | 21 session_id_(session_id), |
| 23 host_(nullptr), | 22 host_(nullptr), |
| 24 dispatcher_(new protocol::UberDispatcher(this)) { | 23 dispatcher_(new protocol::UberDispatcher(this)), |
| 25 } | 24 weak_factory_(this) {} |
| 26 | 25 |
| 27 DevToolsSession::~DevToolsSession() { | 26 DevToolsSession::~DevToolsSession() { |
| 28 dispatcher_.reset(); | 27 dispatcher_.reset(); |
| 29 for (auto& pair : handlers_) | 28 for (auto& pair : handlers_) |
| 30 pair.second->Disable(); | 29 pair.second->Disable(); |
| 31 handlers_.clear(); | 30 handlers_.clear(); |
| 32 } | 31 } |
| 33 | 32 |
| 34 void DevToolsSession::AddHandler( | 33 void DevToolsSession::AddHandler( |
| 35 std::unique_ptr<protocol::DevToolsDomainHandler> handler) { | 34 std::unique_ptr<protocol::DevToolsDomainHandler> handler) { |
| 36 handler->Wire(dispatcher_.get()); | 35 handler->Wire(dispatcher_.get()); |
| 37 handler->SetRenderFrameHost(host_); | 36 handler->SetRenderFrameHost(host_); |
| 38 handlers_[handler->name()] = std::move(handler); | 37 handlers_[handler->name()] = std::move(handler); |
| 39 } | 38 } |
| 40 | 39 |
| 41 void DevToolsSession::SetRenderFrameHost(RenderFrameHostImpl* host) { | 40 void DevToolsSession::SetRenderFrameHost(RenderFrameHostImpl* host) { |
| 42 host_ = host; | 41 host_ = host; |
| 43 for (auto& pair : handlers_) | 42 for (auto& pair : handlers_) |
| 44 pair.second->SetRenderFrameHost(host_); | 43 pair.second->SetRenderFrameHost(host_); |
| 45 } | 44 } |
| 46 | 45 |
| 47 void DevToolsSession::SetFallThroughForNotFound(bool value) { | 46 void DevToolsSession::SetFallThroughForNotFound(bool value) { |
| 48 dispatcher_->setFallThroughForNotFound(value); | 47 dispatcher_->setFallThroughForNotFound(value); |
| 49 } | 48 } |
| 50 | 49 |
| 50 void DevToolsSession::sendResponse( |
| 51 std::unique_ptr<base::DictionaryValue> response) { |
| 52 std::string json; |
| 53 base::JSONWriter::Write(*response.get(), &json); |
| 54 agent_host_->SendMessageToClient(session_id_, json); |
| 55 } |
| 56 |
| 51 protocol::Response::Status DevToolsSession::Dispatch( | 57 protocol::Response::Status DevToolsSession::Dispatch( |
| 52 const std::string& message, | 58 const std::string& message, |
| 53 int* call_id, | 59 int* call_id, |
| 54 std::string* method) { | 60 std::string* method) { |
| 55 std::unique_ptr<base::Value> value = base::JSONReader::Read(message); | 61 std::unique_ptr<base::Value> value = base::JSONReader::Read(message); |
| 56 | 62 |
| 57 DevToolsManagerDelegate* delegate = | 63 DevToolsManagerDelegate* delegate = |
| 58 DevToolsManager::GetInstance()->delegate(); | 64 DevToolsManager::GetInstance()->delegate(); |
| 59 if (value && value->IsType(base::Value::Type::DICTIONARY) && delegate) { | 65 if (value && value->IsType(base::Value::Type::DICTIONARY) && delegate) { |
| 60 std::unique_ptr<base::DictionaryValue> response(delegate->HandleCommand( | 66 base::DictionaryValue* dict_value = |
| 61 agent_host_, | 67 static_cast<base::DictionaryValue*>(value.get()); |
| 62 static_cast<base::DictionaryValue*>(value.get()))); | 68 std::unique_ptr<base::DictionaryValue> response( |
| 69 delegate->HandleCommand(agent_host_, dict_value)); |
| 63 if (response) { | 70 if (response) { |
| 64 std::string json; | 71 sendResponse(std::move(response)); |
| 65 base::JSONWriter::Write(*response.get(), &json); | |
| 66 agent_host_->SendMessageToClient(session_id_, json); | |
| 67 return protocol::Response::kSuccess; | 72 return protocol::Response::kSuccess; |
| 68 } | 73 } |
| 74 if (delegate->HandleAsyncCommand(agent_host_, dict_value, |
| 75 base::Bind(&DevToolsSession::sendResponse, |
| 76 weak_factory_.GetWeakPtr()))) { |
| 77 return protocol::Response::kAsync; |
| 78 } |
| 69 } | 79 } |
| 70 | 80 |
| 71 return dispatcher_->dispatch(protocol::toProtocolValue(value.get(), 1000), | 81 return dispatcher_->dispatch(protocol::toProtocolValue(value.get(), 1000), |
| 72 call_id, method); | 82 call_id, method); |
| 73 } | 83 } |
| 74 | 84 |
| 75 void DevToolsSession::sendProtocolResponse( | 85 void DevToolsSession::sendProtocolResponse( |
| 76 int call_id, | 86 int call_id, |
| 77 std::unique_ptr<protocol::Serializable> message) { | 87 std::unique_ptr<protocol::Serializable> message) { |
| 78 agent_host_->SendMessageToClient(session_id_, message->serialize()); | 88 agent_host_->SendMessageToClient(session_id_, message->serialize()); |
| 79 } | 89 } |
| 80 | 90 |
| 81 void DevToolsSession::sendProtocolNotification( | 91 void DevToolsSession::sendProtocolNotification( |
| 82 std::unique_ptr<protocol::Serializable> message) { | 92 std::unique_ptr<protocol::Serializable> message) { |
| 83 agent_host_->SendMessageToClient(session_id_, message->serialize()); | 93 agent_host_->SendMessageToClient(session_id_, message->serialize()); |
| 84 } | 94 } |
| 85 | 95 |
| 86 void DevToolsSession::flushProtocolNotifications() { | 96 void DevToolsSession::flushProtocolNotifications() { |
| 87 } | 97 } |
| 88 | 98 |
| 89 protocol::DevToolsDomainHandler* DevToolsSession::GetHandlerByName( | 99 protocol::DevToolsDomainHandler* DevToolsSession::GetHandlerByName( |
| 90 const std::string& name) { | 100 const std::string& name) { |
| 91 auto it = handlers_.find(name); | 101 auto it = handlers_.find(name); |
| 92 if (it == handlers_.end()) | 102 if (it == handlers_.end()) |
| 93 return nullptr; | 103 return nullptr; |
| 94 return it->second.get(); | 104 return it->second.get(); |
| 95 } | 105 } |
| 96 | 106 |
| 97 } // namespace content | 107 } // namespace content |
| OLD | NEW |