Chromium Code Reviews| 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_manager.h" | 9 #include "content/browser/devtools/devtools_manager.h" |
| 10 #include "content/browser/devtools/protocol/protocol.h" | 10 #include "content/browser/devtools/protocol/protocol.h" |
| 11 #include "content/public/browser/devtools_manager_delegate.h" | 11 #include "content/public/browser/devtools_manager_delegate.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 DevToolsSession::DevToolsSession(DevToolsAgentHostImpl* agent_host, | 15 DevToolsSession::DevToolsSession(DevToolsAgentHostImpl* agent_host, |
| 16 DevToolsAgentHostClient* client, | 16 DevToolsAgentHostClient* client, |
| 17 int session_id) | 17 int session_id) |
| 18 : agent_host_(agent_host), | 18 : agent_host_(agent_host), |
| 19 client_(client), | 19 client_(client), |
| 20 session_id_(session_id), | 20 session_id_(session_id), |
| 21 host_(nullptr), | 21 host_(nullptr), |
| 22 dispatcher_(new protocol::UberDispatcher(this)), | 22 dispatcher_(new protocol::UberDispatcher(this)), |
| 23 chunk_processor_(base::Bind(&DevToolsSession::SendMessageToClient, | |
| 24 base::Unretained(this))), | |
| 23 weak_factory_(this) {} | 25 weak_factory_(this) {} |
| 24 | 26 |
| 25 DevToolsSession::~DevToolsSession() { | 27 DevToolsSession::~DevToolsSession() { |
| 26 dispatcher_.reset(); | 28 dispatcher_.reset(); |
| 27 for (auto& pair : handlers_) | 29 for (auto& pair : handlers_) |
| 28 pair.second->Disable(); | 30 pair.second->Disable(); |
| 29 handlers_.clear(); | 31 handlers_.clear(); |
| 30 } | 32 } |
| 31 | 33 |
| 32 void DevToolsSession::AddHandler( | 34 void DevToolsSession::AddHandler( |
| 33 std::unique_ptr<protocol::DevToolsDomainHandler> handler) { | 35 std::unique_ptr<protocol::DevToolsDomainHandler> handler) { |
| 34 handler->Wire(dispatcher_.get()); | 36 handler->Wire(dispatcher_.get()); |
| 35 handler->SetRenderFrameHost(host_); | 37 handler->SetRenderFrameHost(host_); |
| 36 handlers_[handler->name()] = std::move(handler); | 38 handlers_[handler->name()] = std::move(handler); |
| 37 } | 39 } |
| 38 | 40 |
| 39 void DevToolsSession::SetRenderFrameHost(RenderFrameHostImpl* host) { | 41 void DevToolsSession::SetRenderFrameHost(RenderFrameHostImpl* host) { |
| 40 host_ = host; | 42 host_ = host; |
| 41 for (auto& pair : handlers_) | 43 for (auto& pair : handlers_) |
| 42 pair.second->SetRenderFrameHost(host_); | 44 pair.second->SetRenderFrameHost(host_); |
| 43 } | 45 } |
| 44 | 46 |
| 45 void DevToolsSession::SetFallThroughForNotFound(bool value) { | 47 void DevToolsSession::SetFallThroughForNotFound(bool value) { |
| 46 dispatcher_->setFallThroughForNotFound(value); | 48 dispatcher_->setFallThroughForNotFound(value); |
| 47 } | 49 } |
| 48 | 50 |
| 49 void DevToolsSession::sendResponse( | 51 void DevToolsSession::SendMessageToClient(int session_id, |
| 52 const std::string& message) { | |
| 53 if (session_id != session_id_) | |
|
caseq
2017/06/13 19:52:25
nit: I wonder if this check would make more sense
dgozman
2017/06/16 17:58:46
Right. I will probably follow up by inlining chunk
| |
| 54 return; | |
| 55 int id = chunk_processor_.last_call_id(); | |
| 56 waiting_for_response_messages_.erase(id); | |
| 57 client_->DispatchProtocolMessage(agent_host_, message); | |
| 58 // |this| may be deleted at this point. | |
| 59 } | |
| 60 | |
| 61 void DevToolsSession::SendResponse( | |
| 50 std::unique_ptr<base::DictionaryValue> response) { | 62 std::unique_ptr<base::DictionaryValue> response) { |
| 51 std::string json; | 63 std::string json; |
| 52 base::JSONWriter::Write(*response.get(), &json); | 64 base::JSONWriter::Write(*response.get(), &json); |
| 53 agent_host_->SendMessageToClient(session_id_, json); | 65 client_->DispatchProtocolMessage(agent_host_, json); |
| 54 } | 66 } |
| 55 | 67 |
| 56 protocol::Response::Status DevToolsSession::Dispatch( | 68 protocol::Response::Status DevToolsSession::Dispatch( |
| 57 const std::string& message, | 69 const std::string& message, |
| 58 int* call_id, | 70 int* call_id, |
| 59 std::string* method) { | 71 std::string* method) { |
| 60 std::unique_ptr<base::Value> value = base::JSONReader::Read(message); | 72 std::unique_ptr<base::Value> value = base::JSONReader::Read(message); |
| 61 | 73 |
| 62 DevToolsManagerDelegate* delegate = | 74 DevToolsManagerDelegate* delegate = |
| 63 DevToolsManager::GetInstance()->delegate(); | 75 DevToolsManager::GetInstance()->delegate(); |
| 64 if (value && value->IsType(base::Value::Type::DICTIONARY) && delegate) { | 76 if (value && value->IsType(base::Value::Type::DICTIONARY) && delegate) { |
| 65 base::DictionaryValue* dict_value = | 77 base::DictionaryValue* dict_value = |
| 66 static_cast<base::DictionaryValue*>(value.get()); | 78 static_cast<base::DictionaryValue*>(value.get()); |
| 67 std::unique_ptr<base::DictionaryValue> response( | 79 std::unique_ptr<base::DictionaryValue> response( |
| 68 delegate->HandleCommand(agent_host_, dict_value)); | 80 delegate->HandleCommand(agent_host_, dict_value)); |
| 69 if (response) { | 81 if (response) { |
| 70 sendResponse(std::move(response)); | 82 SendResponse(std::move(response)); |
| 71 return protocol::Response::kSuccess; | 83 return protocol::Response::kSuccess; |
| 72 } | 84 } |
| 73 if (delegate->HandleAsyncCommand(agent_host_, dict_value, | 85 if (delegate->HandleAsyncCommand(agent_host_, dict_value, |
| 74 base::Bind(&DevToolsSession::sendResponse, | 86 base::Bind(&DevToolsSession::SendResponse, |
| 75 weak_factory_.GetWeakPtr()))) { | 87 weak_factory_.GetWeakPtr()))) { |
| 76 return protocol::Response::kAsync; | 88 return protocol::Response::kAsync; |
| 77 } | 89 } |
| 78 } | 90 } |
| 79 | 91 |
| 80 return dispatcher_->dispatch(protocol::toProtocolValue(value.get(), 1000), | 92 return dispatcher_->dispatch(protocol::toProtocolValue(value.get(), 1000), |
| 81 call_id, method); | 93 call_id, method); |
| 82 } | 94 } |
| 83 | 95 |
| 96 bool DevToolsSession::ReceiveMessageChunk(const DevToolsMessageChunk& chunk) { | |
| 97 return chunk_processor_.ProcessChunkedMessageFromAgent(chunk); | |
| 98 } | |
| 99 | |
| 84 void DevToolsSession::sendProtocolResponse( | 100 void DevToolsSession::sendProtocolResponse( |
| 85 int call_id, | 101 int call_id, |
| 86 std::unique_ptr<protocol::Serializable> message) { | 102 std::unique_ptr<protocol::Serializable> message) { |
| 87 agent_host_->SendMessageToClient(session_id_, message->serialize()); | 103 client_->DispatchProtocolMessage(agent_host_, message->serialize()); |
| 88 } | 104 } |
| 89 | 105 |
| 90 void DevToolsSession::sendProtocolNotification( | 106 void DevToolsSession::sendProtocolNotification( |
| 91 std::unique_ptr<protocol::Serializable> message) { | 107 std::unique_ptr<protocol::Serializable> message) { |
| 92 agent_host_->SendMessageToClient(session_id_, message->serialize()); | 108 client_->DispatchProtocolMessage(agent_host_, message->serialize()); |
| 93 } | 109 } |
| 94 | 110 |
| 95 void DevToolsSession::flushProtocolNotifications() { | 111 void DevToolsSession::flushProtocolNotifications() { |
| 96 } | 112 } |
| 97 | 113 |
| 98 } // namespace content | 114 } // namespace content |
| OLD | NEW |