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 985d991963775057126d94d4475ee47469145305..88fdbd44c3b59fd13e6348a42f664e936a38fb6f 100644 |
| --- a/content/browser/devtools/protocol/service_worker_handler.cc |
| +++ b/content/browser/devtools/protocol/service_worker_handler.cc |
| @@ -4,7 +4,8 @@ |
| #include "content/browser/devtools/protocol/service_worker_handler.h" |
| -#include "content/public/browser/devtools_agent_host.h" |
| +#include "content/browser/devtools/service_worker_devtools_agent_host.h" |
| +#include "content/browser/devtools/service_worker_devtools_manager.h" |
| namespace content { |
| namespace devtools { |
| @@ -12,7 +13,8 @@ namespace service_worker { |
| using Response = DevToolsProtocolClient::Response; |
| -ServiceWorkerHandler::ServiceWorkerHandler() { |
| +ServiceWorkerHandler::ServiceWorkerHandler() |
| + : enabled_(false) { |
| } |
| ServiceWorkerHandler::~ServiceWorkerHandler() { |
| @@ -24,20 +26,40 @@ void ServiceWorkerHandler::SetClient( |
| client_.swap(client); |
| } |
| +void ServiceWorkerHandler::SetURL(const GURL& url) { |
| + url_ = url; |
| + if (enabled_) { |
| + ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| + ServiceWorkerDevToolsManager::GetInstance()-> |
| + AddAllAgentHosts(&agent_hosts); |
| + for (auto host : agent_hosts) |
| + WorkerReadyForInspection(host.get()); |
|
dgozman
2015/03/08 07:49:56
You should also detach from the old ones, which ar
pfeldman
2015/03/08 08:30:57
Done.
|
| + } |
| +} |
| + |
| void ServiceWorkerHandler::Detached() { |
| Disable(); |
| } |
| Response ServiceWorkerHandler::Enable() { |
| - DevToolsAgentHost::List agent_hosts; |
| - ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); |
| + if (enabled_) |
| + return Response::OK(); |
| + enabled_ = true; |
| + |
| ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); |
| + |
| + ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| + ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); |
| for (auto host : agent_hosts) |
| - WorkerCreated(host.get()); |
| + WorkerReadyForInspection(host.get()); |
| return Response::OK(); |
| } |
| Response ServiceWorkerHandler::Disable() { |
| + if (!enabled_) |
| + return Response::OK(); |
| + enabled_ = false; |
| + |
| ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); |
| for (const auto& pair : attached_hosts_) |
| pair.second->DetachClient(); |
| @@ -51,38 +73,14 @@ Response ServiceWorkerHandler::SendMessage( |
| auto it = attached_hosts_.find(worker_id); |
| if (it == attached_hosts_.end()) |
| return Response::InternalError("Not connected to the worker"); |
| - |
| it->second->DispatchProtocolMessage(message); |
| return Response::OK(); |
| } |
| -Response ServiceWorkerHandler::Attach(const std::string& worker_id) { |
| - scoped_refptr<DevToolsAgentHost> host = |
| - DevToolsAgentHost::GetForId(worker_id); |
| - if (!host) |
| - return Response::InternalError("No such worker available"); |
| - |
| - if (host->IsAttached()) |
| - return Response::InternalError("Another client is already attached"); |
| - |
| - attached_hosts_[worker_id] = host; |
| - host->AttachClient(this); |
| - return Response::OK(); |
| -} |
| - |
| -Response ServiceWorkerHandler::Detach(const std::string& worker_id) { |
| - auto it = attached_hosts_.find(worker_id); |
| - if (it == attached_hosts_.end()) |
| - return Response::InternalError("Not connected to the worker"); |
| - |
| - attached_hosts_.erase(worker_id); |
| - it->second->DetachClient(); |
| - return Response::OK(); |
| -} |
| - |
| void ServiceWorkerHandler::DispatchProtocolMessage( |
| DevToolsAgentHost* host, |
| const std::string& message) { |
| + |
| auto it = attached_hosts_.find(host->GetId()); |
| if (it == attached_hosts_.end()) |
| return; // Already disconnected. |
| @@ -96,26 +94,46 @@ void ServiceWorkerHandler::DispatchProtocolMessage( |
| void ServiceWorkerHandler::AgentHostClosed( |
| DevToolsAgentHost* host, |
| bool replaced_with_another_client) { |
| - WorkerDestroyed(host); |
| + WorkerDestroyed(static_cast<ServiceWorkerDevToolsAgentHost*>(host)); |
| } |
| void ServiceWorkerHandler::WorkerCreated( |
| - DevToolsAgentHost* host) { |
| + ServiceWorkerDevToolsAgentHost* host) { |
| + if (!MatchesInspectedPage(host)) |
| + return; |
| + 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())); |
| } |
| void ServiceWorkerHandler::WorkerDestroyed( |
| - DevToolsAgentHost* host) { |
| + ServiceWorkerDevToolsAgentHost* host) { |
| auto it = attached_hosts_.find(host->GetId()); |
| if (it == attached_hosts_.end()) |
| return; |
| + it->second->DetachClient(); |
| client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
| set_worker_id(host->GetId())); |
| 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 |