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(); |
| 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.get()); |
| 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 attached_hosts_.clear(); |
| 46 return Response::OK(); |
| 47 } |
| 48 |
| 49 Response ServiceWorkerHandler::SendMessage( |
| 50 const std::string& worker_id, |
| 51 const std::string& message) { |
| 52 auto it = attached_hosts_.find(worker_id); |
| 53 if (it == attached_hosts_.end()) |
| 54 return Response::InternalError("Not connected to the worker"); |
| 55 |
| 56 it->second->DispatchProtocolMessage(message); |
| 57 return Response::OK(); |
| 58 } |
| 59 |
| 60 Response ServiceWorkerHandler::Attach(const std::string& worker_id) { |
| 61 scoped_refptr<DevToolsAgentHost> host = |
| 62 DevToolsAgentHost::GetForId(worker_id); |
| 63 if (!host) |
| 64 return Response::InternalError("No such worker available"); |
| 65 |
| 66 if (host->IsAttached()) |
| 67 return Response::InternalError("Another client is already attached"); |
| 68 |
| 69 attached_hosts_[worker_id] = host; |
| 70 host->AttachClient(this); |
| 71 return Response::OK(); |
| 72 } |
| 73 |
| 74 Response ServiceWorkerHandler::Detach(const std::string& worker_id) { |
| 75 auto it = attached_hosts_.find(worker_id); |
| 76 if (it == attached_hosts_.end()) |
| 77 return Response::InternalError("Not connected to the worker"); |
| 78 |
| 79 attached_hosts_.erase(worker_id); |
| 80 it->second->DetachClient(); |
| 81 return Response::OK(); |
| 82 } |
| 83 |
| 84 void ServiceWorkerHandler::DispatchProtocolMessage( |
| 85 DevToolsAgentHost* host, |
| 86 const std::string& message) { |
| 87 auto it = attached_hosts_.find(host->GetId()); |
| 88 if (it == attached_hosts_.end()) |
| 89 return; // Already disconnected. |
| 90 |
| 91 // TODO(pfeldman): uncomment once generator is in place. |
| 92 // client_->DispatchMessage( |
| 93 // DispatchMessageParams::Create()-> |
| 94 // set_worker_id(host->GetId())-> |
| 95 // set_message(message)); |
| 96 } |
| 97 |
| 98 void ServiceWorkerHandler::AgentHostClosed( |
| 99 DevToolsAgentHost* host, |
| 100 bool replaced_with_another_client) { |
| 101 WorkerDestroyed(host); |
| 102 } |
| 103 |
| 104 void ServiceWorkerHandler::WorkerCreated( |
| 105 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 DevToolsAgentHost* host) { |
| 114 auto it = attached_hosts_.find(host->GetId()); |
| 115 if (it == attached_hosts_.end()) |
| 116 return; |
| 117 // TODO(pfeldman): uncomment once generator is in place. |
| 118 // client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
| 119 // set_worker_id(host->GetId())); |
| 120 attached_hosts_.erase(it); |
| 121 } |
| 122 |
| 123 } // namespace service_worker |
| 124 } // namespace devtools |
| 125 } // namespace content |
OLD | NEW |