Chromium Code Reviews| 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 ServiceWorkerDevToolsAgentHost::List agent_hosts; | |
| 33 ServiceWorkerDevToolsManager::GetInstance()-> | |
| 34 AddAllAgentHosts(&agent_hosts); | |
| 35 for (auto host : agent_hosts) | |
| 36 WorkerReadyForInspection(host.get()); | |
|
dgozman
2015/03/08 07:49:56
You should also detach from the old ones, which ar
pfeldman
2015/03/08 08:30:57
Done.
| |
| 37 } | |
| 38 } | |
| 39 | |
| 27 void ServiceWorkerHandler::Detached() { | 40 void ServiceWorkerHandler::Detached() { |
| 28 Disable(); | 41 Disable(); |
| 29 } | 42 } |
| 30 | 43 |
| 31 Response ServiceWorkerHandler::Enable() { | 44 Response ServiceWorkerHandler::Enable() { |
| 32 DevToolsAgentHost::List agent_hosts; | 45 if (enabled_) |
| 46 return Response::OK(); | |
| 47 enabled_ = true; | |
| 48 | |
| 49 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); | |
| 50 | |
| 51 ServiceWorkerDevToolsAgentHost::List agent_hosts; | |
| 33 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); | 52 ServiceWorkerDevToolsManager::GetInstance()->AddAllAgentHosts(&agent_hosts); |
| 34 ServiceWorkerDevToolsManager::GetInstance()->AddObserver(this); | |
| 35 for (auto host : agent_hosts) | 53 for (auto host : agent_hosts) |
| 36 WorkerCreated(host.get()); | 54 WorkerReadyForInspection(host.get()); |
| 37 return Response::OK(); | 55 return Response::OK(); |
| 38 } | 56 } |
| 39 | 57 |
| 40 Response ServiceWorkerHandler::Disable() { | 58 Response ServiceWorkerHandler::Disable() { |
| 59 if (!enabled_) | |
| 60 return Response::OK(); | |
| 61 enabled_ = false; | |
| 62 | |
| 41 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); | 63 ServiceWorkerDevToolsManager::GetInstance()->RemoveObserver(this); |
| 42 for (const auto& pair : attached_hosts_) | 64 for (const auto& pair : attached_hosts_) |
| 43 pair.second->DetachClient(); | 65 pair.second->DetachClient(); |
| 44 attached_hosts_.clear(); | 66 attached_hosts_.clear(); |
| 45 return Response::OK(); | 67 return Response::OK(); |
| 46 } | 68 } |
| 47 | 69 |
| 48 Response ServiceWorkerHandler::SendMessage( | 70 Response ServiceWorkerHandler::SendMessage( |
| 49 const std::string& worker_id, | 71 const std::string& worker_id, |
| 50 const std::string& message) { | 72 const std::string& message) { |
| 51 auto it = attached_hosts_.find(worker_id); | 73 auto it = attached_hosts_.find(worker_id); |
| 52 if (it == attached_hosts_.end()) | 74 if (it == attached_hosts_.end()) |
| 53 return Response::InternalError("Not connected to the worker"); | 75 return Response::InternalError("Not connected to the worker"); |
| 54 | |
| 55 it->second->DispatchProtocolMessage(message); | 76 it->second->DispatchProtocolMessage(message); |
| 56 return Response::OK(); | 77 return Response::OK(); |
| 57 } | 78 } |
| 58 | 79 |
| 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( | 80 void ServiceWorkerHandler::DispatchProtocolMessage( |
| 84 DevToolsAgentHost* host, | 81 DevToolsAgentHost* host, |
| 85 const std::string& message) { | 82 const std::string& message) { |
| 83 | |
| 86 auto it = attached_hosts_.find(host->GetId()); | 84 auto it = attached_hosts_.find(host->GetId()); |
| 87 if (it == attached_hosts_.end()) | 85 if (it == attached_hosts_.end()) |
| 88 return; // Already disconnected. | 86 return; // Already disconnected. |
| 89 | 87 |
| 90 client_->DispatchMessage( | 88 client_->DispatchMessage( |
| 91 DispatchMessageParams::Create()-> | 89 DispatchMessageParams::Create()-> |
| 92 set_worker_id(host->GetId())-> | 90 set_worker_id(host->GetId())-> |
| 93 set_message(message)); | 91 set_message(message)); |
| 94 } | 92 } |
| 95 | 93 |
| 96 void ServiceWorkerHandler::AgentHostClosed( | 94 void ServiceWorkerHandler::AgentHostClosed( |
| 97 DevToolsAgentHost* host, | 95 DevToolsAgentHost* host, |
| 98 bool replaced_with_another_client) { | 96 bool replaced_with_another_client) { |
| 99 WorkerDestroyed(host); | 97 WorkerDestroyed(static_cast<ServiceWorkerDevToolsAgentHost*>(host)); |
| 100 } | 98 } |
| 101 | 99 |
| 102 void ServiceWorkerHandler::WorkerCreated( | 100 void ServiceWorkerHandler::WorkerCreated( |
| 103 DevToolsAgentHost* host) { | 101 ServiceWorkerDevToolsAgentHost* host) { |
| 102 if (!MatchesInspectedPage(host)) | |
| 103 return; | |
| 104 host->PauseForDebugOnStart(); | |
| 105 } | |
| 106 | |
| 107 void ServiceWorkerHandler::WorkerReadyForInspection( | |
| 108 ServiceWorkerDevToolsAgentHost* host) { | |
| 109 if (host->IsAttached() || !MatchesInspectedPage(host)) | |
| 110 return; | |
| 111 | |
| 112 attached_hosts_[host->GetId()] = host; | |
| 113 host->AttachClient(this); | |
| 104 client_->WorkerCreated(WorkerCreatedParams::Create()-> | 114 client_->WorkerCreated(WorkerCreatedParams::Create()-> |
| 105 set_worker_id(host->GetId())-> | 115 set_worker_id(host->GetId())-> |
| 106 set_url(host->GetURL().spec())); | 116 set_url(host->GetURL().spec())); |
| 107 } | 117 } |
| 108 | 118 |
| 109 void ServiceWorkerHandler::WorkerDestroyed( | 119 void ServiceWorkerHandler::WorkerDestroyed( |
| 110 DevToolsAgentHost* host) { | 120 ServiceWorkerDevToolsAgentHost* host) { |
| 111 auto it = attached_hosts_.find(host->GetId()); | 121 auto it = attached_hosts_.find(host->GetId()); |
| 112 if (it == attached_hosts_.end()) | 122 if (it == attached_hosts_.end()) |
| 113 return; | 123 return; |
| 124 it->second->DetachClient(); | |
| 114 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> | 125 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
| 115 set_worker_id(host->GetId())); | 126 set_worker_id(host->GetId())); |
| 116 attached_hosts_.erase(it); | 127 attached_hosts_.erase(it); |
| 117 } | 128 } |
| 118 | 129 |
| 130 bool ServiceWorkerHandler::MatchesInspectedPage( | |
| 131 ServiceWorkerDevToolsAgentHost* host) { | |
| 132 // TODO(pfeldman): match based on scope. | |
| 133 // TODO(pfeldman): match iframes. | |
| 134 return host->GetURL().host() == url_.host(); | |
| 135 } | |
| 136 | |
| 119 } // namespace service_worker | 137 } // namespace service_worker |
| 120 } // namespace devtools | 138 } // namespace devtools |
| 121 } // namespace content | 139 } // namespace content |
| OLD | NEW |