Chromium Code Reviews| Index: content/browser/devtools/protocol/service_worker_handler.cc |
| diff --git a/content/browser/devtools/protocol/service_worker_handler.cc b/content/browser/devtools/protocol/service_worker_handler.cc |
| index 68f05c08f01e0b08e914169631b867db7c22cdd6..703613e7e55902324d56f1d585c6f7c726f2360f 100644 |
| --- a/content/browser/devtools/protocol/service_worker_handler.cc |
| +++ b/content/browser/devtools/protocol/service_worker_handler.cc |
| @@ -8,6 +8,9 @@ |
| #include "base/strings/string_number_conversions.h" |
| #include "content/browser/devtools/service_worker_devtools_agent_host.h" |
| #include "content/browser/devtools/service_worker_devtools_manager.h" |
| +#include "content/browser/frame_host/frame_tree.h" |
| +#include "content/browser/frame_host/frame_tree_node.h" |
| +#include "content/browser/frame_host/render_frame_host_impl.h" |
| #include "content/browser/service_worker/service_worker_context_observer.h" |
| #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| #include "content/public/browser/browser_context.h" |
| @@ -102,6 +105,41 @@ scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( |
| return registration; |
| } |
| +static scoped_refptr<ServiceWorkerDevToolsAgentHost> GetMatchingServiceWorker( |
|
dgozman
2015/03/13 18:01:52
drop static
pfeldman
2015/03/13 18:05:36
Done.
|
| + const ServiceWorkerDevToolsAgentHost::List& agent_hosts, |
| + const GURL& url) { |
| + scoped_refptr<ServiceWorkerDevToolsAgentHost> best_host; |
| + std::string best_scope; |
| + for (auto host : agent_hosts) { |
| + if (host->GetURL().host() != url.host()) |
| + continue; |
| + std::string path = host->GetURL().path(); |
| + std::string file = host->GetURL().ExtractFileName(); |
| + std::string scope = path.substr(0, path.length() - file.length()); |
| + if (scope.length() > best_scope.length()) { |
| + best_host = host; |
| + best_scope = scope; |
| + } |
| + } |
| + return best_host; |
| +} |
| + |
| +static ServiceWorkerDevToolsAgentHost::Set GetMatchingServiceWorkers( |
|
dgozman
2015/03/13 18:01:52
ditto
pfeldman
2015/03/13 18:05:36
Done.
|
| + const std::set<GURL>& urls) { |
| + ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| + ServiceWorkerDevToolsManager::GetInstance()-> |
| + AddAllAgentHosts(&agent_hosts); |
| + ServiceWorkerDevToolsAgentHost::Set result; |
| + for (const GURL& url : urls) { |
| + scoped_refptr<ServiceWorkerDevToolsAgentHost> host = |
| + GetMatchingServiceWorker(agent_hosts, url); |
| + if (host) |
| + result.insert(host); |
| + } |
| + return result; |
| +} |
| + |
| + |
| } // namespace |
| using Response = DevToolsProtocolClient::Response; |
| @@ -266,7 +304,8 @@ ServiceWorkerHandler::~ServiceWorkerHandler() { |
| } |
| void ServiceWorkerHandler::SetRenderFrameHost( |
| - RenderFrameHost* render_frame_host) { |
| + RenderFrameHostImpl* render_frame_host) { |
| + render_frame_host_ = render_frame_host; |
|
dgozman
2015/03/13 18:01:52
UpdateURLs();
pfeldman
2015/03/13 18:05:36
Done.
|
| if (!render_frame_host) { |
| context_ = nullptr; |
| return; |
| @@ -283,27 +322,31 @@ void ServiceWorkerHandler::SetClient(scoped_ptr<Client> client) { |
| client_.swap(client); |
| } |
| -void ServiceWorkerHandler::SetURL(const GURL& url) { |
| - url_ = url; |
| - if (enabled_) { |
| - for (auto pair : attached_hosts_) { |
| - if (!MatchesInspectedPage(pair.second.get())) |
| - WorkerDestroyed(pair.second.get()); |
| - } |
| +static bool CollectURLs(std::set<GURL>* urls, FrameTreeNode* tree_node) { |
|
dgozman
2015/03/13 18:01:52
move to namespace above
pfeldman
2015/03/13 18:05:36
Done.
|
| + urls->insert(tree_node->current_url()); |
| + return false; |
| +} |
| - ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| - ServiceWorkerDevToolsManager::GetInstance()-> |
| - AddAllAgentHosts(&agent_hosts); |
| - for (auto host : agent_hosts) { |
| - if (!MatchesInspectedPage(host.get())) |
| - continue; |
| - if (attached_hosts_.find(host->GetId()) != attached_hosts_.end()) |
| - continue; |
| - // TODO(pfeldman): workers are created concurrently, we need |
| - // to get notification earlier to go through the Created/Ready |
| - // lifecycle. |
| - WorkerReadyForInspection(host.get()); |
| - } |
| +void ServiceWorkerHandler::UpdateURLs() { |
| + if (!enabled_) |
| + return; |
| + |
| + urls_.clear(); |
| + render_frame_host_->frame_tree_node()->frame_tree()->ForEach( |
| + base::Bind(&CollectURLs, &urls_)); |
| + |
| + AttachedHosts old_hosts = attached_hosts_; |
| + ServiceWorkerDevToolsAgentHost::Set new_hosts = |
| + GetMatchingServiceWorkers(urls_); |
| + |
| + for (auto pair : old_hosts) { |
| + if (new_hosts.find(pair.second) == new_hosts.end()) |
|
dgozman
2015/03/13 18:01:52
indentation
pfeldman
2015/03/13 18:05:36
Done.
|
| + WorkerDestroyed(pair.second.get()); |
| + } |
| + |
| + for (auto host : new_hosts) { |
| + if (old_hosts.find(host->GetId()) == old_hosts.end()) |
| + ReportWorkerCreated(host.get()); |
| } |
| } |
| @@ -319,14 +362,9 @@ Response ServiceWorkerHandler::Enable() { |
| enabled_ = true; |
| ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); |
| - |
| - ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| - ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); |
| - for (auto host : agent_hosts) |
| - WorkerReadyForInspection(host.get()); |
| - |
| context_observer_ = new ContextObserver(context_, weak_factory_.GetWeakPtr()); |
| context_observer_->Start(); |
| + UpdateURLs(); |
| return Response::OK(); |
| } |
| @@ -414,21 +452,14 @@ void ServiceWorkerHandler::AgentHostClosed( |
| void ServiceWorkerHandler::WorkerCreated( |
| ServiceWorkerDevToolsAgentHost* host) { |
| - if (!MatchesInspectedPage(host)) |
| - return; |
| - host->PauseForDebugOnStart(); |
| + if (MatchesInspectedPage(host)) |
| + host->PauseForDebugOnStart(); |
| } |
| void ServiceWorkerHandler::WorkerReadyForInspection( |
| ServiceWorkerDevToolsAgentHost* host) { |
| - if (host->IsAttached() || !MatchesInspectedPage(host)) |
| - return; |
| - |
| - attached_hosts_[host->GetId()] = host; |
| - host->AttachClient(this); |
| - client_->WorkerCreated(WorkerCreatedParams::Create()-> |
| - set_worker_id(host->GetId())-> |
| - set_url(host->GetURL().spec())); |
| + if (MatchesInspectedPage(host)) |
| + ReportWorkerCreated(host); |
| } |
| void ServiceWorkerHandler::WorkerDestroyed( |
| @@ -442,11 +473,21 @@ void ServiceWorkerHandler::WorkerDestroyed( |
| attached_hosts_.erase(it); |
| } |
| +void ServiceWorkerHandler::ReportWorkerCreated( |
| + ServiceWorkerDevToolsAgentHost* host) { |
| + if (host->IsAttached()) |
| + return; |
| + attached_hosts_[host->GetId()] = host; |
| + host->AttachClient(this); |
| + client_->WorkerCreated(WorkerCreatedParams::Create()-> |
| + set_worker_id(host->GetId())-> |
| + set_url(host->GetURL().spec())); |
| +} |
| + |
| bool ServiceWorkerHandler::MatchesInspectedPage( |
| ServiceWorkerDevToolsAgentHost* host) { |
| - // TODO(pfeldman): match based on scope. |
| - // TODO(pfeldman): match iframes. |
| - return host->GetURL().host() == url_.host(); |
| + auto hosts = GetMatchingServiceWorkers(urls_); |
| + return hosts.find(host) != hosts.end(); |
| } |
| } // namespace service_worker |