| Index: content/browser/devtools/protocol/target_handler.cc
|
| diff --git a/content/browser/devtools/protocol/target_handler.cc b/content/browser/devtools/protocol/target_handler.cc
|
| index d87a7de72a5fc89cf1b4539990b5a7487f2314f8..55c87cdc65fd8bf3bb8017bb7f39b25811bc29cb 100644
|
| --- a/content/browser/devtools/protocol/target_handler.cc
|
| +++ b/content/browser/devtools/protocol/target_handler.cc
|
| @@ -175,7 +175,7 @@ void TargetHandler::ReattachTargetsOfType(
|
| if (pair.second->GetType() == type &&
|
| new_hosts.find(pair.first) == new_hosts.end()) {
|
| DetachFromTargetInternal(pair.second.get());
|
| - TargetRemovedInternal(pair.second.get());
|
| + TargetDestroyedInternal(pair.second.get(), true);
|
| }
|
| }
|
| for (const auto& pair : new_hosts) {
|
| @@ -187,17 +187,28 @@ void TargetHandler::ReattachTargetsOfType(
|
| }
|
|
|
| void TargetHandler::TargetCreatedInternal(DevToolsAgentHost* host) {
|
| + if (reported_hosts_.find(host->GetId()) != reported_hosts_.end())
|
| + return;
|
| client_->TargetCreated(
|
| TargetCreatedParams::Create()->set_target_info(
|
| TargetInfo::Create()->set_target_id(host->GetId())
|
| ->set_title(host->GetTitle())
|
| ->set_url(host->GetURL().spec())
|
| ->set_type(host->GetType())));
|
| + reported_hosts_[host->GetId()] = host;
|
| }
|
|
|
| -void TargetHandler::TargetRemovedInternal(DevToolsAgentHost* host) {
|
| - client_->TargetRemoved(TargetRemovedParams::Create()
|
| +void TargetHandler::TargetDestroyedInternal(
|
| + DevToolsAgentHost* host,
|
| + bool keep_for_discovery) {
|
| + if (discover_ && keep_for_discovery)
|
| + return;
|
| + auto it = reported_hosts_.find(host->GetId());
|
| + if (it == reported_hosts_.end())
|
| + return;
|
| + client_->TargetDestroyed(TargetDestroyedParams::Create()
|
| ->set_target_id(host->GetId()));
|
| + reported_hosts_.erase(it);
|
| }
|
|
|
| bool TargetHandler::AttachToTargetInternal(
|
| @@ -227,7 +238,16 @@ Response TargetHandler::SetDiscoverTargets(bool discover) {
|
| if (discover_ == discover)
|
| return Response::OK();
|
| discover_ = discover;
|
| - // TODO(dgozman): observe all agent hosts here.
|
| + if (discover_) {
|
| + DevToolsManager::GetInstance()->AddObserver(this);
|
| + } else {
|
| + DevToolsManager::GetInstance()->RemoveObserver(this);
|
| + RawHostsMap copy = reported_hosts_;
|
| + for (const auto& id_host : copy) {
|
| + if (attached_hosts_.find(id_host.first) == attached_hosts_.end())
|
| + TargetDestroyedInternal(id_host.second, false);
|
| + }
|
| + }
|
| return Response::OK();
|
| }
|
|
|
| @@ -265,11 +285,10 @@ Response TargetHandler::SetAttachToFrames(bool value) {
|
|
|
| Response TargetHandler::AttachToTarget(const std::string& target_id,
|
| bool* out_success) {
|
| - scoped_refptr<DevToolsAgentHost> agent_host(
|
| - DevToolsAgentHost::GetForId(target_id));
|
| - if (!agent_host)
|
| + auto it = reported_hosts_.find(target_id);
|
| + if (it == reported_hosts_.end())
|
| return Response::InvalidParams("No target with such id");
|
| - *out_success = AttachToTargetInternal(agent_host.get(), false);
|
| + *out_success = AttachToTargetInternal(it->second, false);
|
| return Response::OK();
|
| }
|
|
|
| @@ -277,7 +296,9 @@ Response TargetHandler::DetachFromTarget(const std::string& target_id) {
|
| auto it = attached_hosts_.find(target_id);
|
| if (it == attached_hosts_.end())
|
| return Response::InternalError("Not attached to the target");
|
| - DetachFromTargetInternal(it->second.get());
|
| + DevToolsAgentHost* agent_host = it->second.get();
|
| + DetachFromTargetInternal(agent_host);
|
| + TargetDestroyedInternal(agent_host, true);
|
| return Response::OK();
|
| }
|
|
|
| @@ -294,6 +315,7 @@ Response TargetHandler::SendMessageToTarget(
|
| Response TargetHandler::GetTargetInfo(
|
| const std::string& target_id,
|
| scoped_refptr<TargetInfo>* target_info) {
|
| + // TODO(dgozman): only allow reported hosts.
|
| scoped_refptr<DevToolsAgentHost> agent_host(
|
| DevToolsAgentHost::GetForId(target_id));
|
| if (!agent_host)
|
| @@ -307,6 +329,7 @@ Response TargetHandler::GetTargetInfo(
|
| }
|
|
|
| Response TargetHandler::ActivateTarget(const std::string& target_id) {
|
| + // TODO(dgozman): only allow reported hosts.
|
| scoped_refptr<DevToolsAgentHost> agent_host(
|
| DevToolsAgentHost::GetForId(target_id));
|
| if (!agent_host)
|
| @@ -336,7 +359,19 @@ void TargetHandler::AgentHostClosed(
|
| client_->DetachedFromTarget(DetachedFromTargetParams::Create()->
|
| set_target_id(host->GetId()));
|
| attached_hosts_.erase(host->GetId());
|
| - TargetRemovedInternal(host);
|
| + TargetDestroyedInternal(host, false);
|
| +}
|
| +
|
| +// -------------- DevToolsManager::Observer -----------------
|
| +
|
| +void TargetHandler::AgentHostCreated(DevToolsAgentHostImpl* agent_host) {
|
| + DCHECK(attached_hosts_.find(agent_host->GetId()) == attached_hosts_.end());
|
| + TargetCreatedInternal(agent_host);
|
| +}
|
| +
|
| +void TargetHandler::AgentHostDestroyed(DevToolsAgentHostImpl* agent_host) {
|
| + DCHECK(attached_hosts_.find(agent_host->GetId()) == attached_hosts_.end());
|
| + TargetDestroyedInternal(agent_host, false);
|
| }
|
|
|
| // -------- ServiceWorkerDevToolsManager::Observer ----------
|
|
|