| 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..903705fd94c90982fd7712ee3cbf4a08639af0fa 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,53 @@ void ServiceWorkerHandler::SetClient(
|
| 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());
|
| + }
|
| +
|
| + 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::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 +86,23 @@ 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) {
|
| +Response ServiceWorkerHandler::Stop(
|
| + 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();
|
| + it->second->Close();
|
| 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 +116,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
|
|
|