Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/devtools/protocol/service_worker_handler.h" | |
| 6 | |
| 7 #include "content/browser/devtools/service_worker_devtools_manager.h" | |
| 8 #include "content/public/browser/devtools_agent_host.h" | |
| 9 | |
| 10 namespace content { | |
| 11 namespace devtools { | |
| 12 namespace service_worker { | |
| 13 | |
| 14 using Response = DevToolsProtocolClient::Response; | |
| 15 | |
| 16 ServiceWorkerHandler::ServiceWorkerHandler() { | |
| 17 } | |
| 18 | |
| 19 ServiceWorkerHandler::~ServiceWorkerHandler() { | |
| 20 Disable(); | |
| 21 } | |
| 22 | |
| 23 void ServiceWorkerHandler::SetClient( | |
| 24 scoped_ptr<DevToolsProtocolClient> client) { | |
| 25 client_.swap(client); | |
| 26 } | |
| 27 | |
| 28 void ServiceWorkerHandler::Detached() { | |
| 29 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
| |
| 30 } | |
| 31 | |
| 32 Response ServiceWorkerHandler::Enable() { | |
| 33 DevToolsAgentHost::List agent_hosts; | |
| 34 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); | |
| 35 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); | |
| 36 for (auto host : agent_hosts) | |
| 37 WorkerCreated(host); | |
| 38 return Response::OK(); | |
| 39 } | |
| 40 | |
| 41 Response ServiceWorkerHandler::Disable() { | |
| 42 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); | |
| 43 for (const auto& pair : attached_hosts_) | |
| 44 pair.second->DetachClient(); | |
| 45 return Response::OK(); | |
| 46 } | |
| 47 | |
| 48 Response ServiceWorkerHandler::SendMessage( | |
| 49 const std::string& worker_id, | |
| 50 const std::string& message) { | |
| 51 auto it = attached_hosts_.find(worker_id); | |
| 52 if (it == attached_hosts_.end()) | |
| 53 return Response::InternalError("Not connected to the worker"); | |
| 54 | |
| 55 it->second->DispatchProtocolMessage(message); | |
| 56 return Response::OK(); | |
| 57 } | |
| 58 | |
| 59 Response ServiceWorkerHandler::Attach(const std::string& worker_id) { | |
| 60 scoped_refptr<DevToolsAgentHost> host = | |
| 61 DevToolsAgentHost::GetForId(worker_id); | |
| 62 if (!host) | |
| 63 return Response::InternalError("No such worker available"); | |
| 64 | |
| 65 if (host->IsAttached()) | |
| 66 return Response::InternalError("Another client is already attached"); | |
| 67 | |
| 68 attached_hosts_[worker_id] = host; | |
| 69 host->AttachClient(this); | |
| 70 return Response::OK(); | |
| 71 } | |
| 72 | |
| 73 Response ServiceWorkerHandler::Detach(const std::string& worker_id) { | |
| 74 auto it = attached_hosts_.find(worker_id); | |
| 75 if (it == attached_hosts_.end()) | |
| 76 return Response::InternalError("Not connected to the worker"); | |
| 77 | |
| 78 attached_hosts_.erase(worker_id); | |
| 79 it->second->DetachClient(); | |
| 80 return Response::OK(); | |
| 81 } | |
| 82 | |
| 83 void ServiceWorkerHandler::DispatchProtocolMessage( | |
| 84 DevToolsAgentHost* agent_host, | |
| 85 const std::string& message) { | |
| 86 auto it = attached_hosts_.find(agent_host->GetId()); | |
| 87 if (it == attached_hosts_.end()) | |
| 88 return; // Already disconnected. | |
| 89 | |
| 90 // TODO(pfeldman): uncomment once generator is in place. | |
| 91 // client_->DispatchMessage( | |
| 92 // DispatchMessageParams::Create()-> | |
| 93 // set_worker_id(agent_host->GetId())-> | |
| 94 // set_message(message)); | |
| 95 } | |
| 96 | |
| 97 void ServiceWorkerHandler::AgentHostClosed( | |
| 98 DevToolsAgentHost* agent_host, | |
| 99 bool replaced_with_another_client) { | |
| 100 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.
| |
| 101 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
| |
| 102 } | |
| 103 | |
| 104 void ServiceWorkerHandler::WorkerCreated( | |
| 105 scoped_refptr<DevToolsAgentHost> host) { | |
| 106 // TODO(pfeldman): uncomment once generator is in place. | |
| 107 // client_->WorkerCreated(WorkerCreatedParams::Create()-> | |
| 108 // set_worker_id(host->GetId())-> | |
| 109 // set_url(host->GetURL().spec())); | |
| 110 } | |
| 111 | |
| 112 void ServiceWorkerHandler::WorkerDestroyed( | |
| 113 scoped_refptr<DevToolsAgentHost> host) { | |
| 114 // TODO(pfeldman): uncomment once generator is in place. | |
| 115 // client_->WorkerTerminated(WorkerTerminatedParams::Create()-> | |
| 116 // set_worker_id(host->GetId())); | |
| 117 } | |
| 118 | |
| 119 } // namespace worker | |
|
dgozman
2015/03/05 20:17:06
service_worker
pfeldman
2015/03/05 20:26:26
Done.
| |
| 120 } // namespace devtools | |
| 121 } // namespace content | |
| OLD | NEW |