| Index: content/browser/devtools/protocol/target_auto_attacher.cc
|
| diff --git a/content/browser/devtools/protocol/target_handler.cc b/content/browser/devtools/protocol/target_auto_attacher.cc
|
| similarity index 36%
|
| copy from content/browser/devtools/protocol/target_handler.cc
|
| copy to content/browser/devtools/protocol/target_auto_attacher.cc
|
| index 2cbdaffc5b454e2a2de8c5db7eff17084b33ad32..dbccd7b1292946e8f334c65b7d2191ffbe2cf7bb 100644
|
| --- a/content/browser/devtools/protocol/target_handler.cc
|
| +++ b/content/browser/devtools/protocol/target_auto_attacher.cc
|
| @@ -1,11 +1,9 @@
|
| -// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "content/browser/devtools/protocol/target_handler.h"
|
| +#include "content/browser/devtools/protocol/target_auto_attacher.h"
|
|
|
| -#include "content/browser/devtools/devtools_manager.h"
|
| -#include "content/browser/devtools/devtools_session.h"
|
| #include "content/browser/devtools/render_frame_devtools_agent_host.h"
|
| #include "content/browser/devtools/service_worker_devtools_agent_host.h"
|
| #include "content/browser/frame_host/frame_tree.h"
|
| @@ -22,9 +20,9 @@ using ScopeAgentsMap =
|
|
|
| void GetMatchingHostsByScopeMap(
|
| const ServiceWorkerDevToolsAgentHost::List& agent_hosts,
|
| - const std::set<GURL>& urls,
|
| + const base::flat_set<GURL>& urls,
|
| ScopeAgentsMap* scope_agents_map) {
|
| - std::set<base::StringPiece> host_name_set;
|
| + base::flat_set<base::StringPiece> host_name_set;
|
| for (const GURL& url : urls)
|
| host_name_set.insert(url.host_piece());
|
| for (const auto& host : agent_hosts) {
|
| @@ -65,7 +63,7 @@ void AddEligibleHosts(const ServiceWorkerDevToolsAgentHost::List& list,
|
|
|
| ServiceWorkerDevToolsAgentHost::Map GetMatchingServiceWorkers(
|
| BrowserContext* browser_context,
|
| - const std::set<GURL>& urls) {
|
| + const base::flat_set<GURL>& urls) {
|
| ServiceWorkerDevToolsAgentHost::Map result;
|
| if (!browser_context)
|
| return result;
|
| @@ -83,65 +81,35 @@ ServiceWorkerDevToolsAgentHost::Map GetMatchingServiceWorkers(
|
| return result;
|
| }
|
|
|
| -std::unique_ptr<Target::TargetInfo> CreateInfo(DevToolsAgentHost* host) {
|
| - return Target::TargetInfo::Create()
|
| - .SetTargetId(host->GetId())
|
| - .SetTitle(host->GetTitle())
|
| - .SetUrl(host->GetURL().spec())
|
| - .SetType(host->GetType())
|
| - .SetAttached(host->IsAttached())
|
| - .Build();
|
| -}
|
| -
|
| } // namespace
|
|
|
| -TargetHandler::TargetHandler()
|
| - : DevToolsDomainHandler(Target::Metainfo::domainName),
|
| - discover_(false),
|
| +TargetAutoAttacher::TargetAutoAttacher(AttachCallback attach_callback,
|
| + DetachCallback detach_callback)
|
| + : attach_callback_(attach_callback),
|
| + detach_callback_(detach_callback),
|
| + render_frame_host_(nullptr),
|
| auto_attach_(false),
|
| wait_for_debugger_on_start_(false),
|
| - attach_to_frames_(false),
|
| - render_frame_host_(nullptr) {
|
| -}
|
| -
|
| -TargetHandler::~TargetHandler() {
|
| -}
|
| + attach_to_frames_(false) {}
|
|
|
| -// static
|
| -std::vector<TargetHandler*> TargetHandler::ForAgentHost(
|
| - DevToolsAgentHostImpl* host) {
|
| - return DevToolsSession::HandlersForAgentHost<TargetHandler>(
|
| - host, Target::Metainfo::domainName);
|
| -}
|
| -
|
| -void TargetHandler::Wire(UberDispatcher* dispatcher) {
|
| - frontend_.reset(new Target::Frontend(dispatcher->channel()));
|
| - Target::Dispatcher::wire(dispatcher, this);
|
| -}
|
| +TargetAutoAttacher::~TargetAutoAttacher() {}
|
|
|
| -void TargetHandler::SetRenderFrameHost(RenderFrameHostImpl* render_frame_host) {
|
| +void TargetAutoAttacher::SetRenderFrameHost(
|
| + RenderFrameHostImpl* render_frame_host) {
|
| render_frame_host_ = render_frame_host;
|
| UpdateFrames();
|
| + ReattachServiceWorkers(false);
|
| }
|
|
|
| -Response TargetHandler::Disable() {
|
| - SetAutoAttach(false, false);
|
| - SetDiscoverTargets(false);
|
| - for (const auto& id_host : attached_hosts_)
|
| - id_host.second->DetachClient(this);
|
| - attached_hosts_.clear();
|
| - return Response::OK();
|
| -}
|
| -
|
| -void TargetHandler::UpdateServiceWorkers() {
|
| - UpdateServiceWorkers(false);
|
| +void TargetAutoAttacher::UpdateServiceWorkers() {
|
| + ReattachServiceWorkers(false);
|
| }
|
|
|
| -void TargetHandler::UpdateFrames() {
|
| +void TargetAutoAttacher::UpdateFrames() {
|
| if (!auto_attach_ || !attach_to_frames_)
|
| return;
|
|
|
| - HostsMap new_hosts;
|
| + Hosts new_hosts;
|
| if (render_frame_host_) {
|
| FrameTreeNode* root = render_frame_host_->frame_tree_node();
|
| std::queue<FrameTreeNode*> queue;
|
| @@ -153,7 +121,7 @@ void TargetHandler::UpdateFrames() {
|
| if (node != root && cross_process) {
|
| scoped_refptr<DevToolsAgentHost> new_host =
|
| RenderFrameDevToolsAgentHost::GetOrCreateFor(node);
|
| - new_hosts[new_host->GetId()] = new_host;
|
| + new_hosts.insert(new_host);
|
| } else {
|
| for (size_t i = 0; i < node->child_count(); ++i)
|
| queue.push(node->child_at(i));
|
| @@ -165,7 +133,11 @@ void TargetHandler::UpdateFrames() {
|
| ReattachTargetsOfType(new_hosts, DevToolsAgentHost::kTypeFrame, false);
|
| }
|
|
|
| -void TargetHandler::UpdateServiceWorkers(bool waiting_for_debugger) {
|
| +void TargetAutoAttacher::AgentHostClosed(DevToolsAgentHost* host) {
|
| + auto_attached_hosts_.erase(host);
|
| +}
|
| +
|
| +void TargetAutoAttacher::ReattachServiceWorkers(bool waiting_for_debugger) {
|
| if (!auto_attach_)
|
| return;
|
|
|
| @@ -181,272 +153,69 @@ void TargetHandler::UpdateServiceWorkers(bool waiting_for_debugger) {
|
| }
|
|
|
| auto matching = GetMatchingServiceWorkers(browser_context, frame_urls_);
|
| - HostsMap new_hosts;
|
| + Hosts new_hosts;
|
| for (const auto& pair : matching) {
|
| if (pair.second->IsReadyForInspection())
|
| - new_hosts[pair.first] = pair.second;
|
| + new_hosts.insert(pair.second);
|
| }
|
| - ReattachTargetsOfType(
|
| - new_hosts, DevToolsAgentHost::kTypeServiceWorker, waiting_for_debugger);
|
| -}
|
| -
|
| -void TargetHandler::ReattachTargetsOfType(
|
| - const HostsMap& new_hosts,
|
| - const std::string& type,
|
| - bool waiting_for_debugger) {
|
| - HostsMap old_hosts = attached_hosts_;
|
| - for (const auto& pair : old_hosts) {
|
| - if (pair.second->GetType() == type &&
|
| - new_hosts.find(pair.first) == new_hosts.end()) {
|
| - DetachFromTargetInternal(pair.second.get());
|
| + ReattachTargetsOfType(new_hosts, DevToolsAgentHost::kTypeServiceWorker,
|
| + waiting_for_debugger);
|
| +}
|
| +
|
| +void TargetAutoAttacher::ReattachTargetsOfType(const Hosts& new_hosts,
|
| + const std::string& type,
|
| + bool waiting_for_debugger) {
|
| + Hosts old_hosts = auto_attached_hosts_;
|
| + for (auto& it : old_hosts) {
|
| + DevToolsAgentHost* host = it.get();
|
| + if (host->GetType() == type && new_hosts.find(host) == new_hosts.end()) {
|
| + auto_attached_hosts_.erase(host);
|
| + detach_callback_.Run(host);
|
| }
|
| }
|
| - for (const auto& pair : new_hosts) {
|
| - if (old_hosts.find(pair.first) == old_hosts.end())
|
| - AttachToTargetInternal(pair.second.get(), waiting_for_debugger);
|
| - }
|
| -}
|
| -
|
| -void TargetHandler::TargetCreatedInternal(DevToolsAgentHost* host) {
|
| - if (reported_hosts_.find(host->GetId()) != reported_hosts_.end())
|
| - return;
|
| - frontend_->TargetCreated(CreateInfo(host));
|
| - reported_hosts_[host->GetId()] = host;
|
| -}
|
| -
|
| -void TargetHandler::TargetInfoChangedInternal(DevToolsAgentHost* host) {
|
| - if (reported_hosts_.find(host->GetId()) == reported_hosts_.end())
|
| - return;
|
| - frontend_->TargetInfoChanged(CreateInfo(host));
|
| -}
|
| -
|
| -void TargetHandler::TargetDestroyedInternal(DevToolsAgentHost* host) {
|
| - auto it = reported_hosts_.find(host->GetId());
|
| - if (it == reported_hosts_.end())
|
| - return;
|
| - if (discover_)
|
| - frontend_->TargetDestroyed(host->GetId());
|
| - reported_hosts_.erase(it);
|
| -}
|
| -
|
| -bool TargetHandler::AttachToTargetInternal(
|
| - DevToolsAgentHost* host, bool waiting_for_debugger) {
|
| - attached_hosts_[host->GetId()] = host;
|
| - if (!host->AttachClient(this)) {
|
| - attached_hosts_.erase(host->GetId());
|
| - return false;
|
| - }
|
| - frontend_->AttachedToTarget(CreateInfo(host), waiting_for_debugger);
|
| - return true;
|
| -}
|
| -
|
| -void TargetHandler::DetachFromTargetInternal(DevToolsAgentHost* host) {
|
| - auto it = attached_hosts_.find(host->GetId());
|
| - if (it == attached_hosts_.end())
|
| - return;
|
| - host->DetachClient(this);
|
| - frontend_->DetachedFromTarget(host->GetId());
|
| - attached_hosts_.erase(it);
|
| -}
|
| -
|
| -// ----------------- Protocol ----------------------
|
| -
|
| -Response TargetHandler::SetDiscoverTargets(bool discover) {
|
| - if (discover_ == discover)
|
| - return Response::OK();
|
| - discover_ = discover;
|
| - if (discover_) {
|
| - DevToolsAgentHost::AddObserver(this);
|
| - } else {
|
| - DevToolsAgentHost::RemoveObserver(this);
|
| - RawHostsMap copy = reported_hosts_;
|
| - for (const auto& id_host : copy)
|
| - TargetDestroyedInternal(id_host.second);
|
| + for (auto& it : new_hosts) {
|
| + DevToolsAgentHost* host = it.get();
|
| + if (old_hosts.find(host) == old_hosts.end()) {
|
| + if (attach_callback_.Run(host, waiting_for_debugger))
|
| + auto_attached_hosts_.insert(host);
|
| + }
|
| }
|
| - return Response::OK();
|
| }
|
|
|
| -Response TargetHandler::SetAutoAttach(
|
| - bool auto_attach, bool wait_for_debugger_on_start) {
|
| +void TargetAutoAttacher::SetAutoAttach(bool auto_attach,
|
| + bool wait_for_debugger_on_start) {
|
| wait_for_debugger_on_start_ = wait_for_debugger_on_start;
|
| if (auto_attach_ == auto_attach)
|
| - return Response::FallThrough();
|
| + return;
|
| auto_attach_ = auto_attach;
|
| if (auto_attach_) {
|
| ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this);
|
| - UpdateServiceWorkers();
|
| + ReattachServiceWorkers(false);
|
| UpdateFrames();
|
| } else {
|
| ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this);
|
| - HostsMap empty;
|
| + Hosts empty;
|
| ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false);
|
| ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeServiceWorker, false);
|
| + DCHECK(auto_attached_hosts_.empty());
|
| }
|
| - return Response::FallThrough();
|
| }
|
|
|
| -Response TargetHandler::SetAttachToFrames(bool value) {
|
| - if (attach_to_frames_ == value)
|
| - return Response::OK();
|
| - attach_to_frames_ = value;
|
| +void TargetAutoAttacher::SetAttachToFrames(bool attach_to_frames) {
|
| + if (attach_to_frames_ == attach_to_frames)
|
| + return;
|
| + attach_to_frames_ = attach_to_frames;
|
| if (attach_to_frames_) {
|
| UpdateFrames();
|
| } else {
|
| - HostsMap empty;
|
| + Hosts empty;
|
| ReattachTargetsOfType(empty, DevToolsAgentHost::kTypeFrame, false);
|
| }
|
| - return Response::OK();
|
| -}
|
| -
|
| -Response TargetHandler::SetRemoteLocations(
|
| - std::unique_ptr<protocol::Array<Target::RemoteLocation>>) {
|
| - return Response::Error("Not supported");
|
| -}
|
| -
|
| -Response TargetHandler::AttachToTarget(const std::string& target_id,
|
| - bool* out_success) {
|
| - // TODO(dgozman): only allow reported hosts.
|
| - scoped_refptr<DevToolsAgentHost> agent_host =
|
| - DevToolsAgentHost::GetForId(target_id);
|
| - if (!agent_host)
|
| - return Response::InvalidParams("No target with given id found");
|
| - *out_success = AttachToTargetInternal(agent_host.get(), false);
|
| - return Response::OK();
|
| -}
|
| -
|
| -Response TargetHandler::DetachFromTarget(const std::string& target_id) {
|
| - auto it = attached_hosts_.find(target_id);
|
| - if (it == attached_hosts_.end())
|
| - return Response::Error("Not attached to the target");
|
| - DevToolsAgentHost* agent_host = it->second.get();
|
| - DetachFromTargetInternal(agent_host);
|
| - return Response::OK();
|
| -}
|
| -
|
| -Response TargetHandler::SendMessageToTarget(
|
| - const std::string& target_id,
|
| - const std::string& message) {
|
| - auto it = attached_hosts_.find(target_id);
|
| - if (it == attached_hosts_.end())
|
| - return Response::FallThrough();
|
| - it->second->DispatchProtocolMessage(this, message);
|
| - return Response::OK();
|
| -}
|
| -
|
| -Response TargetHandler::GetTargetInfo(
|
| - const std::string& target_id,
|
| - std::unique_ptr<Target::TargetInfo>* target_info) {
|
| - // TODO(dgozman): only allow reported hosts.
|
| - scoped_refptr<DevToolsAgentHost> agent_host(
|
| - DevToolsAgentHost::GetForId(target_id));
|
| - if (!agent_host)
|
| - return Response::InvalidParams("No target with given id found");
|
| - *target_info = CreateInfo(agent_host.get());
|
| - return Response::OK();
|
| -}
|
| -
|
| -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)
|
| - return Response::InvalidParams("No target with given id found");
|
| - agent_host->Activate();
|
| - return Response::OK();
|
| -}
|
| -
|
| -Response TargetHandler::CloseTarget(const std::string& target_id,
|
| - bool* out_success) {
|
| - scoped_refptr<DevToolsAgentHost> agent_host =
|
| - DevToolsAgentHost::GetForId(target_id);
|
| - if (!agent_host)
|
| - return Response::InvalidParams("No target with given id found");
|
| - *out_success = agent_host->Close();
|
| - return Response::OK();
|
| -}
|
| -
|
| -Response TargetHandler::CreateBrowserContext(std::string* out_context_id) {
|
| - return Response::Error("Not supported");
|
| -}
|
| -
|
| -Response TargetHandler::DisposeBrowserContext(const std::string& context_id,
|
| - bool* out_success) {
|
| - return Response::Error("Not supported");
|
| -}
|
| -
|
| -Response TargetHandler::CreateTarget(const std::string& url,
|
| - Maybe<int> width,
|
| - Maybe<int> height,
|
| - Maybe<std::string> context_id,
|
| - std::string* out_target_id) {
|
| - DevToolsManagerDelegate* delegate =
|
| - DevToolsManager::GetInstance()->delegate();
|
| - if (!delegate)
|
| - return Response::Error("Not supported");
|
| - scoped_refptr<content::DevToolsAgentHost> agent_host =
|
| - delegate->CreateNewTarget(GURL(url));
|
| - if (!agent_host)
|
| - return Response::Error("Not supported");
|
| - *out_target_id = agent_host->GetId();
|
| - return Response::OK();
|
| -}
|
| -
|
| -Response TargetHandler::GetTargets(
|
| - std::unique_ptr<protocol::Array<Target::TargetInfo>>* target_infos) {
|
| - *target_infos = protocol::Array<Target::TargetInfo>::create();
|
| - for (const auto& host : DevToolsAgentHost::GetOrCreateAll())
|
| - (*target_infos)->addItem(CreateInfo(host.get()));
|
| - return Response::OK();
|
| -}
|
| -
|
| -// ---------------- DevToolsAgentHostClient ----------------
|
| -
|
| -void TargetHandler::DispatchProtocolMessage(
|
| - DevToolsAgentHost* host,
|
| - const std::string& message) {
|
| - auto it = attached_hosts_.find(host->GetId());
|
| - if (it == attached_hosts_.end())
|
| - return; // Already disconnected.
|
| -
|
| - frontend_->ReceivedMessageFromTarget(host->GetId(), message);
|
| -}
|
| -
|
| -void TargetHandler::AgentHostClosed(
|
| - DevToolsAgentHost* host,
|
| - bool replaced_with_another_client) {
|
| - frontend_->DetachedFromTarget(host->GetId());
|
| - attached_hosts_.erase(host->GetId());
|
| -}
|
| -
|
| -// -------------- DevToolsAgentHostObserver -----------------
|
| -
|
| -bool TargetHandler::ShouldForceDevToolsAgentHostCreation() {
|
| - return true;
|
| -}
|
| -
|
| -void TargetHandler::DevToolsAgentHostCreated(DevToolsAgentHost* agent_host) {
|
| - // If we start discovering late, all existing agent hosts will be reported,
|
| - // but we could have already attached to some.
|
| - TargetCreatedInternal(agent_host);
|
| -}
|
| -
|
| -void TargetHandler::DevToolsAgentHostDestroyed(DevToolsAgentHost* agent_host) {
|
| - DCHECK(attached_hosts_.find(agent_host->GetId()) == attached_hosts_.end());
|
| - TargetDestroyedInternal(agent_host);
|
| -}
|
| -
|
| -void TargetHandler::DevToolsAgentHostAttached(DevToolsAgentHost* host) {
|
| - TargetInfoChangedInternal(host);
|
| -}
|
| -
|
| -void TargetHandler::DevToolsAgentHostDetached(DevToolsAgentHost* host) {
|
| - TargetInfoChangedInternal(host);
|
| }
|
|
|
| // -------- ServiceWorkerDevToolsManager::Observer ----------
|
|
|
| -void TargetHandler::WorkerCreated(
|
| - ServiceWorkerDevToolsAgentHost* host) {
|
| +void TargetAutoAttacher::WorkerCreated(ServiceWorkerDevToolsAgentHost* host) {
|
| BrowserContext* browser_context = nullptr;
|
| if (render_frame_host_)
|
| browser_context = render_frame_host_->GetProcess()->GetBrowserContext();
|
| @@ -457,7 +226,7 @@ void TargetHandler::WorkerCreated(
|
| }
|
| }
|
|
|
| -void TargetHandler::WorkerReadyForInspection(
|
| +void TargetAutoAttacher::WorkerReadyForInspection(
|
| ServiceWorkerDevToolsAgentHost* host) {
|
| DCHECK(host->IsReadyForInspection());
|
| if (ServiceWorkerDevToolsManager::GetInstance()
|
| @@ -466,22 +235,21 @@ void TargetHandler::WorkerReadyForInspection(
|
| // be opened in ServiceWorkerDevToolsManager::WorkerReadyForInspection.
|
| return;
|
| }
|
| - UpdateServiceWorkers(host->IsPausedForDebugOnStart());
|
| + ReattachServiceWorkers(host->IsPausedForDebugOnStart());
|
| }
|
|
|
| -void TargetHandler::WorkerVersionInstalled(
|
| +void TargetAutoAttacher::WorkerVersionInstalled(
|
| ServiceWorkerDevToolsAgentHost* host) {
|
| - UpdateServiceWorkers();
|
| + ReattachServiceWorkers(false);
|
| }
|
|
|
| -void TargetHandler::WorkerVersionDoomed(
|
| +void TargetAutoAttacher::WorkerVersionDoomed(
|
| ServiceWorkerDevToolsAgentHost* host) {
|
| - UpdateServiceWorkers();
|
| + ReattachServiceWorkers(false);
|
| }
|
|
|
| -void TargetHandler::WorkerDestroyed(
|
| - ServiceWorkerDevToolsAgentHost* host) {
|
| - UpdateServiceWorkers();
|
| +void TargetAutoAttacher::WorkerDestroyed(ServiceWorkerDevToolsAgentHost* host) {
|
| + ReattachServiceWorkers(false);
|
| }
|
|
|
| } // namespace protocol
|
|
|