| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/devtools/protocol/service_worker_handler.h" | 5 #include "content/browser/devtools/protocol/service_worker_handler.h" |
| 6 | 6 |
| 7 #include "content/public/browser/devtools_agent_host.h" | 7 #include "content/browser/devtools/service_worker_devtools_agent_host.h" |
| 8 #include "content/browser/devtools/service_worker_devtools_manager.h" |
| 8 | 9 |
| 9 namespace content { | 10 namespace content { |
| 10 namespace devtools { | 11 namespace devtools { |
| 11 namespace service_worker { | 12 namespace service_worker { |
| 12 | 13 |
| 13 using Response = DevToolsProtocolClient::Response; | 14 using Response = DevToolsProtocolClient::Response; |
| 14 | 15 |
| 15 ServiceWorkerHandler::ServiceWorkerHandler() { | 16 ServiceWorkerHandler::ServiceWorkerHandler() |
| 17 : enabled_(false) { |
| 16 } | 18 } |
| 17 | 19 |
| 18 ServiceWorkerHandler::~ServiceWorkerHandler() { | 20 ServiceWorkerHandler::~ServiceWorkerHandler() { |
| 19 Disable(); | 21 Disable(); |
| 20 } | 22 } |
| 21 | 23 |
| 22 void ServiceWorkerHandler::SetClient( | 24 void ServiceWorkerHandler::SetClient( |
| 23 scoped_ptr<Client> client) { | 25 scoped_ptr<Client> client) { |
| 24 client_.swap(client); | 26 client_.swap(client); |
| 25 } | 27 } |
| 26 | 28 |
| 29 void ServiceWorkerHandler::SetURL(const GURL& url) { |
| 30 url_ = url; |
| 31 if (enabled_) { |
| 32 for (auto pair : attached_hosts_) { |
| 33 if (!MatchesInspectedPage(pair.second.get())) |
| 34 WorkerDestroyed(pair.second.get()); |
| 35 } |
| 36 |
| 37 ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| 38 ServiceWorkerDevToolsManager::GetInstance()-> |
| 39 AddAllAgentHosts(&agent_hosts); |
| 40 for (auto host : agent_hosts) { |
| 41 if (!MatchesInspectedPage(host.get())) |
| 42 continue; |
| 43 if (attached_hosts_.find(host->GetId()) != attached_hosts_.end()) |
| 44 continue; |
| 45 // TODO(pfeldman): workers are created concurrently, we need |
| 46 // to get notification earlier to go through the Created/Ready |
| 47 // lifecycle. |
| 48 WorkerReadyForInspection(host.get()); |
| 49 } |
| 50 } |
| 51 } |
| 52 |
| 27 void ServiceWorkerHandler::Detached() { | 53 void ServiceWorkerHandler::Detached() { |
| 28 Disable(); | 54 Disable(); |
| 29 } | 55 } |
| 30 | 56 |
| 31 Response ServiceWorkerHandler::Enable() { | 57 Response ServiceWorkerHandler::Enable() { |
| 32 DevToolsAgentHost::List agent_hosts; | 58 if (enabled_) |
| 59 return Response::OK(); |
| 60 enabled_ = true; |
| 61 |
| 62 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); |
| 63 |
| 64 ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| 33 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); | 65 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); |
| 34 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); | |
| 35 for (auto host : agent_hosts) | 66 for (auto host : agent_hosts) |
| 36 WorkerCreated(host.get()); | 67 WorkerReadyForInspection(host.get()); |
| 37 return Response::OK(); | 68 return Response::OK(); |
| 38 } | 69 } |
| 39 | 70 |
| 40 Response ServiceWorkerHandler::Disable() { | 71 Response ServiceWorkerHandler::Disable() { |
| 72 if (!enabled_) |
| 73 return Response::OK(); |
| 74 enabled_ = false; |
| 75 |
| 41 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); | 76 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); |
| 42 for (const auto& pair : attached_hosts_) | 77 for (const auto& pair : attached_hosts_) |
| 43 pair.second->DetachClient(); | 78 pair.second->DetachClient(); |
| 44 attached_hosts_.clear(); | 79 attached_hosts_.clear(); |
| 45 return Response::OK(); | 80 return Response::OK(); |
| 46 } | 81 } |
| 47 | 82 |
| 48 Response ServiceWorkerHandler::SendMessage( | 83 Response ServiceWorkerHandler::SendMessage( |
| 49 const std::string& worker_id, | 84 const std::string& worker_id, |
| 50 const std::string& message) { | 85 const std::string& message) { |
| 51 auto it = attached_hosts_.find(worker_id); | 86 auto it = attached_hosts_.find(worker_id); |
| 52 if (it == attached_hosts_.end()) | 87 if (it == attached_hosts_.end()) |
| 53 return Response::InternalError("Not connected to the worker"); | 88 return Response::InternalError("Not connected to the worker"); |
| 54 | |
| 55 it->second->DispatchProtocolMessage(message); | 89 it->second->DispatchProtocolMessage(message); |
| 56 return Response::OK(); | 90 return Response::OK(); |
| 57 } | 91 } |
| 58 | 92 |
| 59 Response ServiceWorkerHandler::Attach(const std::string& worker_id) { | 93 Response ServiceWorkerHandler::Stop( |
| 60 scoped_refptr<DevToolsAgentHost> host = | 94 const std::string& worker_id) { |
| 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); | 95 auto it = attached_hosts_.find(worker_id); |
| 75 if (it == attached_hosts_.end()) | 96 if (it == attached_hosts_.end()) |
| 76 return Response::InternalError("Not connected to the worker"); | 97 return Response::InternalError("Not connected to the worker"); |
| 77 | 98 it->second->Close(); |
| 78 attached_hosts_.erase(worker_id); | |
| 79 it->second->DetachClient(); | |
| 80 return Response::OK(); | 99 return Response::OK(); |
| 81 } | 100 } |
| 82 | 101 |
| 83 void ServiceWorkerHandler::DispatchProtocolMessage( | 102 void ServiceWorkerHandler::DispatchProtocolMessage( |
| 84 DevToolsAgentHost* host, | 103 DevToolsAgentHost* host, |
| 85 const std::string& message) { | 104 const std::string& message) { |
| 105 |
| 86 auto it = attached_hosts_.find(host->GetId()); | 106 auto it = attached_hosts_.find(host->GetId()); |
| 87 if (it == attached_hosts_.end()) | 107 if (it == attached_hosts_.end()) |
| 88 return; // Already disconnected. | 108 return; // Already disconnected. |
| 89 | 109 |
| 90 client_->DispatchMessage( | 110 client_->DispatchMessage( |
| 91 DispatchMessageParams::Create()-> | 111 DispatchMessageParams::Create()-> |
| 92 set_worker_id(host->GetId())-> | 112 set_worker_id(host->GetId())-> |
| 93 set_message(message)); | 113 set_message(message)); |
| 94 } | 114 } |
| 95 | 115 |
| 96 void ServiceWorkerHandler::AgentHostClosed( | 116 void ServiceWorkerHandler::AgentHostClosed( |
| 97 DevToolsAgentHost* host, | 117 DevToolsAgentHost* host, |
| 98 bool replaced_with_another_client) { | 118 bool replaced_with_another_client) { |
| 99 WorkerDestroyed(host); | 119 WorkerDestroyed(static_cast<ServiceWorkerDevToolsAgentHost*>(host)); |
| 100 } | 120 } |
| 101 | 121 |
| 102 void ServiceWorkerHandler::WorkerCreated( | 122 void ServiceWorkerHandler::WorkerCreated( |
| 103 DevToolsAgentHost* host) { | 123 ServiceWorkerDevToolsAgentHost* host) { |
| 124 if (!MatchesInspectedPage(host)) |
| 125 return; |
| 126 host->PauseForDebugOnStart(); |
| 127 } |
| 128 |
| 129 void ServiceWorkerHandler::WorkerReadyForInspection( |
| 130 ServiceWorkerDevToolsAgentHost* host) { |
| 131 if (host->IsAttached() || !MatchesInspectedPage(host)) |
| 132 return; |
| 133 |
| 134 attached_hosts_[host->GetId()] = host; |
| 135 host->AttachClient(this); |
| 104 client_->WorkerCreated(WorkerCreatedParams::Create()-> | 136 client_->WorkerCreated(WorkerCreatedParams::Create()-> |
| 105 set_worker_id(host->GetId())-> | 137 set_worker_id(host->GetId())-> |
| 106 set_url(host->GetURL().spec())); | 138 set_url(host->GetURL().spec())); |
| 107 } | 139 } |
| 108 | 140 |
| 109 void ServiceWorkerHandler::WorkerDestroyed( | 141 void ServiceWorkerHandler::WorkerDestroyed( |
| 110 DevToolsAgentHost* host) { | 142 ServiceWorkerDevToolsAgentHost* host) { |
| 111 auto it = attached_hosts_.find(host->GetId()); | 143 auto it = attached_hosts_.find(host->GetId()); |
| 112 if (it == attached_hosts_.end()) | 144 if (it == attached_hosts_.end()) |
| 113 return; | 145 return; |
| 146 it->second->DetachClient(); |
| 114 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> | 147 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
| 115 set_worker_id(host->GetId())); | 148 set_worker_id(host->GetId())); |
| 116 attached_hosts_.erase(it); | 149 attached_hosts_.erase(it); |
| 117 } | 150 } |
| 118 | 151 |
| 152 bool ServiceWorkerHandler::MatchesInspectedPage( |
| 153 ServiceWorkerDevToolsAgentHost* host) { |
| 154 // TODO(pfeldman): match based on scope. |
| 155 // TODO(pfeldman): match iframes. |
| 156 return host->GetURL().host() == url_.host(); |
| 157 } |
| 158 |
| 119 } // namespace service_worker | 159 } // namespace service_worker |
| 120 } // namespace devtools | 160 } // namespace devtools |
| 121 } // namespace content | 161 } // namespace content |
| OLD | NEW |