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/protocol/browser_handler.h" | 5 #include "content/browser/devtools/protocol/browser_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 7 #include "content/browser/devtools/devtools_manager.h" | 10 #include "content/browser/devtools/devtools_manager.h" |
| 8 #include "content/public/browser/devtools_manager_delegate.h" | 11 #include "content/public/browser/devtools_manager_delegate.h" |
| 9 | 12 |
| 10 namespace content { | 13 namespace content { |
| 11 namespace devtools { | 14 namespace devtools { |
| 12 namespace browser { | 15 namespace browser { |
| 13 | 16 |
| 14 using Response = DevToolsProtocolClient::Response; | 17 using Response = DevToolsProtocolClient::Response; |
| 15 | 18 |
| 16 BrowserHandler::BrowserHandler() { | 19 BrowserHandler::BrowserHandler() |
| 20 : weak_factory_(this) { | |
| 17 } | 21 } |
| 18 | 22 |
| 19 BrowserHandler::~BrowserHandler() { | 23 BrowserHandler::~BrowserHandler() { |
| 20 } | 24 } |
| 21 | 25 |
| 22 void BrowserHandler::SetClient(std::unique_ptr<Client> client) { | 26 void BrowserHandler::SetClient(std::unique_ptr<Client> client) { |
| 23 client_.swap(client); | 27 client_.swap(client); |
| 24 } | 28 } |
| 25 | 29 |
| 26 Response BrowserHandler::CreateBrowserContext(std::string* out_context_id) { | 30 Response BrowserHandler::CreateBrowserContext(std::string* out_context_id) { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 56 Response BrowserHandler::CloseTarget(const std::string& target_id, | 60 Response BrowserHandler::CloseTarget(const std::string& target_id, |
| 57 bool* out_success) { | 61 bool* out_success) { |
| 58 scoped_refptr<DevToolsAgentHost> agent_host = | 62 scoped_refptr<DevToolsAgentHost> agent_host = |
| 59 DevToolsAgentHost::GetForId(target_id); | 63 DevToolsAgentHost::GetForId(target_id); |
| 60 if (!agent_host) | 64 if (!agent_host) |
| 61 return Response::ServerError("No target with given id found"); | 65 return Response::ServerError("No target with given id found"); |
| 62 *out_success = agent_host->Close(); | 66 *out_success = agent_host->Close(); |
| 63 return Response::OK(); | 67 return Response::OK(); |
| 64 } | 68 } |
| 65 | 69 |
| 66 Response BrowserHandler::GetTargets(TargetInfos* infos) { | 70 Response BrowserHandler::GetTargets(DevToolsCommandId command_id) { |
| 67 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); | 71 DevToolsAgentHost::DiscoverAllHosts( |
| 68 for (DevToolsAgentHost::List::iterator it = agents.begin(); | 72 base::Bind(&BrowserHandler::RespondToGetTargets, |
| 69 it != agents.end(); ++it) { | 73 weak_factory_.GetWeakPtr(), |
| 70 DevToolsAgentHost* agent_host = (*it).get(); | 74 command_id)); |
| 75 return Response::OK(); | |
| 76 } | |
| 77 | |
| 78 void BrowserHandler::RespondToGetTargets( | |
| 79 DevToolsCommandId command_id, | |
| 80 DevToolsAgentHost::List agents) { | |
| 81 std::vector<scoped_refptr<devtools::browser::TargetInfo>> infos; | |
| 82 for (const auto& agent_host : agents) { | |
| 71 scoped_refptr<devtools::browser::TargetInfo> info = | 83 scoped_refptr<devtools::browser::TargetInfo> info = |
| 72 devtools::browser::TargetInfo::Create()-> | 84 devtools::browser::TargetInfo::Create()-> |
| 73 set_target_id(agent_host->GetId())-> | 85 set_target_id(agent_host->GetId())-> |
| 74 set_type(agent_host->GetType())-> | 86 set_type(agent_host->GetType())-> |
| 75 set_title(agent_host->GetTitle())-> | 87 set_title(agent_host->GetTitle())-> |
| 76 set_url(agent_host->GetURL().spec()); | 88 set_url(agent_host->GetURL().spec()); |
| 77 infos->push_back(info); | 89 infos.push_back(info); |
| 78 } | 90 } |
| 91 client_->SendGetTargetsResponse( | |
| 92 command_id, | |
| 93 GetTargetsResponse::Create()->set_target_info(std::move(infos))); | |
| 94 } | |
| 95 | |
| 96 Response BrowserHandler::Attach(DevToolsCommandId command_id, | |
| 97 const std::string& targetId) { | |
|
dgozman
2016/09/15 03:25:10
target_id
| |
| 98 DevToolsAgentHost::DiscoverAllHosts( | |
| 99 base::Bind(&BrowserHandler::RespondToAttach, | |
| 100 weak_factory_.GetWeakPtr(), command_id, targetId)); | |
| 79 return Response::OK(); | 101 return Response::OK(); |
| 80 } | 102 } |
| 81 | 103 |
| 82 Response BrowserHandler::Attach(const std::string& targetId) { | 104 void BrowserHandler::RespondToAttach(DevToolsCommandId command_id, |
| 105 const std::string& targetId, | |
| 106 DevToolsAgentHost::List agents) { | |
| 83 scoped_refptr<DevToolsAgentHost> agent_host = | 107 scoped_refptr<DevToolsAgentHost> agent_host = |
| 84 DevToolsAgentHost::GetForId(targetId); | 108 DevToolsAgentHost::GetForId(targetId); |
|
dgozman
2016/09/15 03:25:10
Should we look for one in the list instead? Otherw
| |
| 85 if (!agent_host) | 109 bool success = false; |
| 86 return Response::ServerError("No target with given id found"); | 110 if (agent_host) |
| 87 bool success = agent_host->AttachClient(this); | 111 success = agent_host->AttachClient(this); |
| 88 return success ? Response::OK() : | 112 attached_hosts_.push_back(agent_host); |
| 89 Response::ServerError("Target is already being debugged"); | 113 client_->SendAttachResponse( |
| 114 command_id, | |
| 115 AttachResponse::Create()->set_success(success)); | |
| 90 } | 116 } |
| 91 | 117 |
| 92 Response BrowserHandler::Detach(const std::string& targetId) { | 118 Response BrowserHandler::Detach(const std::string& targetId) { |
| 93 scoped_refptr<DevToolsAgentHost> agent_host = | 119 scoped_refptr<DevToolsAgentHost> agent_host = |
| 94 DevToolsAgentHost::GetForId(targetId); | 120 DevToolsAgentHost::GetForId(targetId); |
| 95 if (!agent_host) | 121 auto it = std::find( |
| 96 return Response::ServerError("No target with given id found"); | 122 attached_hosts_.begin(), attached_hosts_.end(), agent_host); |
| 97 bool success = agent_host->DetachClient(this); | 123 if (it != attached_hosts_.end()) |
| 124 attached_hosts_.erase(it); | |
| 125 bool success = agent_host && agent_host->DetachClient(this); | |
| 98 return success ? Response::OK() : | 126 return success ? Response::OK() : |
| 99 Response::ServerError("Target is not being debugged"); | 127 Response::ServerError("Target is not being debugged"); |
|
dgozman
2016/09/15 03:25:10
Let's make detach return success as well?
| |
| 100 } | 128 } |
| 101 | 129 |
| 102 Response BrowserHandler::SendMessage(const std::string& targetId, | 130 Response BrowserHandler::SendMessage(const std::string& targetId, |
| 103 const std::string& message) { | 131 const std::string& message) { |
| 104 scoped_refptr<DevToolsAgentHost> agent_host = | 132 scoped_refptr<DevToolsAgentHost> agent_host = |
| 105 DevToolsAgentHost::GetForId(targetId); | 133 DevToolsAgentHost::GetForId(targetId); |
| 106 if (!agent_host) | 134 if (!agent_host) |
| 107 return Response::ServerError("No target with given id found"); | 135 return Response::ServerError("No target with given id found"); |
| 108 agent_host->DispatchProtocolMessage(this, message); | 136 agent_host->DispatchProtocolMessage(this, message); |
| 109 return Response::OK(); | 137 return Response::OK(); |
| 110 } | 138 } |
| 111 | 139 |
| 112 void BrowserHandler::DispatchProtocolMessage( | 140 void BrowserHandler::DispatchProtocolMessage( |
| 113 DevToolsAgentHost* agent_host, const std::string& message) { | 141 DevToolsAgentHost* agent_host, const std::string& message) { |
| 114 client_->DispatchMessage(DispatchMessageParams::Create()-> | 142 client_->DispatchMessage(DispatchMessageParams::Create()-> |
| 115 set_target_id(agent_host->GetId())-> | 143 set_target_id(agent_host->GetId())-> |
| 116 set_message(message)); | 144 set_message(message)); |
| 117 } | 145 } |
| 118 | 146 |
| 119 void BrowserHandler::AgentHostClosed(DevToolsAgentHost* agent_host, | 147 void BrowserHandler::AgentHostClosed(DevToolsAgentHost* agent_host, |
| 120 bool replaced_with_another_client) { | 148 bool replaced_with_another_client) { |
| 149 auto it = std::find( | |
| 150 attached_hosts_.begin(), attached_hosts_.end(), agent_host); | |
| 151 if (it != attached_hosts_.end()) | |
| 152 attached_hosts_.erase(it); | |
| 121 } | 153 } |
| 122 | 154 |
| 123 } // namespace browser | 155 } // namespace browser |
| 124 } // namespace devtools | 156 } // namespace devtools |
| 125 } // namespace content | 157 } // namespace content |
| OLD | NEW |