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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..77aa2ee43e5480d93decbd97da519b009b98d645 |
| --- /dev/null |
| +++ b/content/browser/devtools/protocol/service_worker_handler.cc |
| @@ -0,0 +1,121 @@ |
| +// Copyright 2015 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/service_worker_handler.h" |
| + |
| +#include "content/browser/devtools/service_worker_devtools_manager.h" |
| +#include "content/public/browser/devtools_agent_host.h" |
| + |
| +namespace content { |
| +namespace devtools { |
| +namespace service_worker { |
| + |
| +using Response = DevToolsProtocolClient::Response; |
| + |
| +ServiceWorkerHandler::ServiceWorkerHandler() { |
| +} |
| + |
| +ServiceWorkerHandler::~ServiceWorkerHandler() { |
| + Disable(); |
| +} |
| + |
| +void ServiceWorkerHandler::SetClient( |
| + scoped_ptr<DevToolsProtocolClient> client) { |
| + client_.swap(client); |
| +} |
| + |
| +void ServiceWorkerHandler::Detached() { |
| + Disable(); |
|
dgozman
2015/03/05 20:17:06
This may lead to double |RemoveObserver|. Is it sa
pfeldman
2015/03/05 20:26:26
It will be. I'll clear attached_hosts_ in Disable
|
| +} |
| + |
| +Response ServiceWorkerHandler::Enable() { |
| + DevToolsAgentHost::List agent_hosts; |
| + ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); |
| + ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); |
| + for (auto host : agent_hosts) |
| + WorkerCreated(host); |
| + return Response::OK(); |
| +} |
| + |
| +Response ServiceWorkerHandler::Disable() { |
| + ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); |
| + for (const auto& pair : attached_hosts_) |
| + pair.second->DetachClient(); |
| + return Response::OK(); |
| +} |
| + |
| +Response ServiceWorkerHandler::SendMessage( |
| + const std::string& worker_id, |
| + const std::string& message) { |
| + 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* agent_host, |
| + const std::string& message) { |
| + auto it = attached_hosts_.find(agent_host->GetId()); |
| + if (it == attached_hosts_.end()) |
| + return; // Already disconnected. |
| + |
| + // TODO(pfeldman): uncomment once generator is in place. |
| + // client_->DispatchMessage( |
| + // DispatchMessageParams::Create()-> |
| + // set_worker_id(agent_host->GetId())-> |
| + // set_message(message)); |
| +} |
| + |
| +void ServiceWorkerHandler::AgentHostClosed( |
| + DevToolsAgentHost* agent_host, |
| + bool replaced_with_another_client) { |
| + attached_hosts_.erase(agent_host->GetId()); |
|
dgozman
2015/03/05 20:17:06
DCHECK it's there
pfeldman
2015/03/05 20:26:26
Done.
|
| + WorkerDestroyed(agent_host); |
|
dgozman
2015/03/05 20:17:06
Won't this lead to double WorkerDestroyed notifica
pfeldman
2015/03/05 20:26:26
Done.
pfeldman
2015/03/05 20:26:26
It will! I'll delegate there and make sure it is o
|
| +} |
| + |
| +void ServiceWorkerHandler::WorkerCreated( |
| + scoped_refptr<DevToolsAgentHost> host) { |
| + // TODO(pfeldman): uncomment once generator is in place. |
| + // client_->WorkerCreated(WorkerCreatedParams::Create()-> |
| + // set_worker_id(host->GetId())-> |
| + // set_url(host->GetURL().spec())); |
| +} |
| + |
| +void ServiceWorkerHandler::WorkerDestroyed( |
| + scoped_refptr<DevToolsAgentHost> host) { |
| + // TODO(pfeldman): uncomment once generator is in place. |
| + // client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
| + // set_worker_id(host->GetId())); |
| +} |
| + |
| +} // namespace worker |
|
dgozman
2015/03/05 20:17:06
service_worker
pfeldman
2015/03/05 20:26:26
Done.
|
| +} // namespace devtools |
| +} // namespace content |