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 b28c83679d68721e554977dea0b20ce0881f0b35..e6386f5215b5176c866fd9d8bfd545c1c095ce28 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,45 @@ scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( |
return registration; |
} |
+scoped_refptr<ServiceWorkerDevToolsAgentHost> GetMatchingServiceWorker( |
+ 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; |
+} |
+ |
+ServiceWorkerDevToolsAgentHost::Map GetMatchingServiceWorkers( |
+ const std::set<GURL>& urls) { |
+ ServiceWorkerDevToolsAgentHost::List agent_hosts; |
+ ServiceWorkerDevToolsManager::GetInstance()-> |
+ AddAllAgentHosts(&agent_hosts); |
+ ServiceWorkerDevToolsAgentHost::Map result; |
+ for (const GURL& url : urls) { |
+ scoped_refptr<ServiceWorkerDevToolsAgentHost> host = |
+ GetMatchingServiceWorker(agent_hosts, url); |
+ if (host) |
+ result[host->GetId()] = host; |
+ } |
+ return result; |
+} |
+ |
+bool CollectURLs(std::set<GURL>* urls, FrameTreeNode* tree_node) { |
+ urls->insert(tree_node->current_url()); |
+ return false; |
+} |
+ |
} // namespace |
using Response = DevToolsProtocolClient::Response; |
@@ -266,7 +308,9 @@ ServiceWorkerHandler::~ServiceWorkerHandler() { |
} |
void ServiceWorkerHandler::SetRenderFrameHost( |
- RenderFrameHost* render_frame_host) { |
+ RenderFrameHostImpl* render_frame_host) { |
+ render_frame_host_ = render_frame_host; |
+ // Do not call UpdateHosts yet, wait for load to commit. |
if (!render_frame_host) { |
context_ = nullptr; |
return; |
@@ -283,27 +327,28 @@ 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()); |
- } |
+void ServiceWorkerHandler::UpdateHosts() { |
+ if (!enabled_) |
+ return; |
- 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()); |
- } |
+ urls_.clear(); |
+ if (render_frame_host_) { |
+ render_frame_host_->frame_tree_node()->frame_tree()->ForEach( |
+ base::Bind(&CollectURLs, &urls_)); |
+ } |
+ |
+ ServiceWorkerDevToolsAgentHost::Map old_hosts = attached_hosts_; |
+ ServiceWorkerDevToolsAgentHost::Map new_hosts = |
+ GetMatchingServiceWorkers(urls_); |
+ |
+ for (auto pair : old_hosts) { |
+ if (new_hosts.find(pair.first) == new_hosts.end()) |
+ ReportWorkerTerminated(pair.second.get()); |
+ } |
+ |
+ for (auto pair : new_hosts) { |
+ if (old_hosts.find(pair.first) == old_hosts.end()) |
+ ReportWorkerCreated(pair.second.get()); |
} |
} |
@@ -319,14 +364,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(); |
+ UpdateHosts(); |
return Response::OK(); |
} |
@@ -414,16 +454,25 @@ void ServiceWorkerHandler::AgentHostClosed( |
void ServiceWorkerHandler::WorkerCreated( |
ServiceWorkerDevToolsAgentHost* host) { |
- if (!MatchesInspectedPage(host)) |
- return; |
- host->PauseForDebugOnStart(); |
+ auto hosts = GetMatchingServiceWorkers(urls_); |
+ if (hosts.find(host->GetId()) != hosts.end()) |
+ host->PauseForDebugOnStart(); |
} |
void ServiceWorkerHandler::WorkerReadyForInspection( |
ServiceWorkerDevToolsAgentHost* host) { |
- if (host->IsAttached() || !MatchesInspectedPage(host)) |
- return; |
+ UpdateHosts(); |
+} |
+void ServiceWorkerHandler::WorkerDestroyed( |
+ ServiceWorkerDevToolsAgentHost* host) { |
+ UpdateHosts(); |
+} |
+ |
+void ServiceWorkerHandler::ReportWorkerCreated( |
+ ServiceWorkerDevToolsAgentHost* host) { |
+ if (host->IsAttached()) |
+ return; |
attached_hosts_[host->GetId()] = host; |
host->AttachClient(this); |
client_->WorkerCreated(WorkerCreatedParams::Create()-> |
@@ -431,7 +480,7 @@ void ServiceWorkerHandler::WorkerReadyForInspection( |
set_url(host->GetURL().spec())); |
} |
-void ServiceWorkerHandler::WorkerDestroyed( |
+void ServiceWorkerHandler::ReportWorkerTerminated( |
ServiceWorkerDevToolsAgentHost* host) { |
auto it = attached_hosts_.find(host->GetId()); |
if (it == attached_hosts_.end()) |
@@ -442,13 +491,6 @@ void ServiceWorkerHandler::WorkerDestroyed( |
attached_hosts_.erase(it); |
} |
-bool ServiceWorkerHandler::MatchesInspectedPage( |
- ServiceWorkerDevToolsAgentHost* host) { |
- // TODO(pfeldman): match based on scope. |
- // TODO(pfeldman): match iframes. |
- return host->GetURL().host() == url_.host(); |
-} |
- |
} // namespace service_worker |
} // namespace devtools |
} // namespace content |