| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/embedded_worker_devtools_manager.h" | 5 #include "content/browser/devtools/embedded_worker_devtools_manager.h" |
| 6 | 6 |
| 7 #include "content/browser/devtools/devtools_manager_impl.h" | 7 #include "content/browser/devtools/devtools_manager_impl.h" |
| 8 #include "content/browser/devtools/devtools_protocol.h" | 8 #include "content/browser/devtools/devtools_protocol.h" |
| 9 #include "content/browser/devtools/devtools_protocol_constants.h" | 9 #include "content/browser/devtools/devtools_protocol_constants.h" |
| 10 #include "content/browser/devtools/ipc_devtools_agent_host.h" | 10 #include "content/browser/devtools/ipc_devtools_agent_host.h" |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 144 |
| 145 void DetachFromWorker() { | 145 void DetachFromWorker() { |
| 146 if (!worker_attached_) | 146 if (!worker_attached_) |
| 147 return; | 147 return; |
| 148 worker_attached_ = false; | 148 worker_attached_ = false; |
| 149 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 149 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 150 host->RemoveRoute(worker_id_.second); | 150 host->RemoveRoute(worker_id_.second); |
| 151 Release(); | 151 Release(); |
| 152 } | 152 } |
| 153 | 153 |
| 154 void SendDisconnectedNotification() { |
| 155 SendMessageToClient(DevToolsProtocol::CreateNotification( |
| 156 devtools::Worker::disconnectedFromWorker::kName, NULL)->Serialize()); |
| 157 } |
| 158 |
| 154 WorkerId worker_id() const { return worker_id_; } | 159 WorkerId worker_id() const { return worker_id_; } |
| 155 | 160 |
| 156 private: | 161 private: |
| 157 virtual ~EmbeddedWorkerDevToolsAgentHost() { | 162 virtual ~EmbeddedWorkerDevToolsAgentHost() { |
| 158 CHECK(!worker_attached_); | 163 CHECK(!worker_attached_); |
| 159 EmbeddedWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData( | 164 EmbeddedWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData( |
| 160 this); | 165 this); |
| 161 } | 166 } |
| 162 | 167 |
| 163 void OnDispatchOnInspectorFrontend(const std::string& message) { | 168 void OnDispatchOnInspectorFrontend(const std::string& message) { |
| 164 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend(this, | 169 SendMessageToClient(message); |
| 165 message); | |
| 166 } | 170 } |
| 167 | 171 |
| 168 void OnSaveAgentRuntimeState(const std::string& state) { state_ = state; } | 172 void OnSaveAgentRuntimeState(const std::string& state) { state_ = state; } |
| 169 | 173 |
| 170 void AttachToWorker() { | 174 void AttachToWorker() { |
| 171 if (worker_attached_) | 175 if (worker_attached_) |
| 172 return; | 176 return; |
| 173 worker_attached_ = true; | 177 worker_attached_ = true; |
| 174 AddRef(); | 178 AddRef(); |
| 175 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 179 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 176 host->AddRoute(worker_id_.second, this); | 180 host->AddRoute(worker_id_.second, this); |
| 177 } | 181 } |
| 178 | 182 |
| 179 WorkerId worker_id_; | 183 WorkerId worker_id_; |
| 180 bool worker_attached_; | 184 bool worker_attached_; |
| 181 std::string state_; | 185 std::string state_; |
| 182 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerDevToolsAgentHost); | 186 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerDevToolsAgentHost); |
| 183 }; | 187 }; |
| 184 | 188 |
| 185 // static | 189 // static |
| 186 EmbeddedWorkerDevToolsManager* EmbeddedWorkerDevToolsManager::GetInstance() { | 190 EmbeddedWorkerDevToolsManager* EmbeddedWorkerDevToolsManager::GetInstance() { |
| 187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 188 return Singleton<EmbeddedWorkerDevToolsManager>::get(); | 192 return Singleton<EmbeddedWorkerDevToolsManager>::get(); |
| 189 } | 193 } |
| 190 | 194 |
| 191 DevToolsAgentHost* EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker( | 195 DevToolsAgentHostImpl* |
| 196 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker( |
| 192 int worker_process_id, | 197 int worker_process_id, |
| 193 int worker_route_id) { | 198 int worker_route_id) { |
| 194 WorkerId id(worker_process_id, worker_route_id); | 199 WorkerId id(worker_process_id, worker_route_id); |
| 195 | 200 |
| 196 WorkerInfoMap::iterator it = workers_.find(id); | 201 WorkerInfoMap::iterator it = workers_.find(id); |
| 197 if (it == workers_.end()) | 202 if (it == workers_.end()) |
| 198 return NULL; | 203 return NULL; |
| 199 | 204 |
| 200 WorkerInfo* info = it->second; | 205 WorkerInfo* info = it->second; |
| 201 if (info->state() != WORKER_UNINSPECTED && | 206 if (info->state() != WORKER_UNINSPECTED && |
| 202 info->state() != WORKER_PAUSED_FOR_DEBUG_ON_START) { | 207 info->state() != WORKER_PAUSED_FOR_DEBUG_ON_START) { |
| 203 return info->agent_host(); | 208 return info->agent_host(); |
| 204 } | 209 } |
| 205 | 210 |
| 206 EmbeddedWorkerDevToolsAgentHost* agent_host = | 211 EmbeddedWorkerDevToolsAgentHost* agent_host = |
| 207 new EmbeddedWorkerDevToolsAgentHost(id); | 212 new EmbeddedWorkerDevToolsAgentHost(id); |
| 208 info->set_agent_host(agent_host); | 213 info->set_agent_host(agent_host); |
| 209 info->set_state(WORKER_INSPECTED); | 214 info->set_state(WORKER_INSPECTED); |
| 210 return agent_host; | 215 return agent_host; |
| 211 } | 216 } |
| 212 | 217 |
| 213 DevToolsAgentHost* | 218 DevToolsAgentHostImpl* |
| 214 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForServiceWorker( | 219 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForServiceWorker( |
| 215 const ServiceWorkerIdentifier& service_worker_id) { | 220 const ServiceWorkerIdentifier& service_worker_id) { |
| 216 WorkerInfoMap::iterator it = FindExistingServiceWorkerInfo(service_worker_id); | 221 WorkerInfoMap::iterator it = FindExistingServiceWorkerInfo(service_worker_id); |
| 217 if (it == workers_.end()) | 222 if (it == workers_.end()) |
| 218 return NULL; | 223 return NULL; |
| 219 return GetDevToolsAgentHostForWorker(it->first.first, it->first.second); | 224 return GetDevToolsAgentHostForWorker(it->first.first, it->first.second); |
| 220 } | 225 } |
| 221 | 226 |
| 222 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager() | 227 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager() |
| 223 : debug_service_worker_on_start_(false) { | 228 : debug_service_worker_on_start_(false) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 workers_.erase(it); | 278 workers_.erase(it); |
| 274 break; | 279 break; |
| 275 case WORKER_INSPECTED: { | 280 case WORKER_INSPECTED: { |
| 276 EmbeddedWorkerDevToolsAgentHost* agent_host = info->agent_host(); | 281 EmbeddedWorkerDevToolsAgentHost* agent_host = info->agent_host(); |
| 277 info->set_state(WORKER_TERMINATED); | 282 info->set_state(WORKER_TERMINATED); |
| 278 if (!agent_host->IsAttached()) { | 283 if (!agent_host->IsAttached()) { |
| 279 agent_host->DetachFromWorker(); | 284 agent_host->DetachFromWorker(); |
| 280 return; | 285 return; |
| 281 } | 286 } |
| 282 // Client host is debugging this worker agent host. | 287 // Client host is debugging this worker agent host. |
| 283 std::string notification = | 288 agent_host->SendDisconnectedNotification(); |
| 284 DevToolsProtocol::CreateNotification( | |
| 285 devtools::Worker::disconnectedFromWorker::kName, NULL) | |
| 286 ->Serialize(); | |
| 287 DevToolsManagerImpl::GetInstance()->DispatchOnInspectorFrontend( | |
| 288 agent_host, notification); | |
| 289 agent_host->DetachFromWorker(); | 289 agent_host->DetachFromWorker(); |
| 290 break; | 290 break; |
| 291 } | 291 } |
| 292 case WORKER_TERMINATED: | 292 case WORKER_TERMINATED: |
| 293 NOTREACHED(); | 293 NOTREACHED(); |
| 294 break; | 294 break; |
| 295 case WORKER_PAUSED_FOR_REATTACH: { | 295 case WORKER_PAUSED_FOR_REATTACH: { |
| 296 scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(it); | 296 scoped_ptr<WorkerInfo> worker_info = workers_.take_and_erase(it); |
| 297 worker_info->set_state(WORKER_TERMINATED); | 297 worker_info->set_state(WORKER_TERMINATED); |
| 298 const WorkerId old_id = worker_info->agent_host()->worker_id(); | 298 const WorkerId old_id = worker_info->agent_host()->worker_id(); |
| 299 workers_.set(old_id, worker_info.Pass()); | 299 workers_.set(old_id, worker_info.Pass()); |
| 300 break; | 300 break; |
| 301 } | 301 } |
| 302 } | 302 } |
| 303 } | 303 } |
| 304 | 304 |
| 305 void EmbeddedWorkerDevToolsManager::WorkerContextStarted(int worker_process_id, | 305 void EmbeddedWorkerDevToolsManager::WorkerContextStarted(int worker_process_id, |
| 306 int worker_route_id) { | 306 int worker_route_id) { |
| 307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 308 const WorkerId id(worker_process_id, worker_route_id); | 308 const WorkerId id(worker_process_id, worker_route_id); |
| 309 WorkerInfoMap::iterator it = workers_.find(id); | 309 WorkerInfoMap::iterator it = workers_.find(id); |
| 310 DCHECK(it != workers_.end()); | 310 DCHECK(it != workers_.end()); |
| 311 WorkerInfo* info = it->second; | 311 WorkerInfo* info = it->second; |
| 312 if (info->state() == WORKER_PAUSED_FOR_DEBUG_ON_START) { | 312 if (info->state() == WORKER_PAUSED_FOR_DEBUG_ON_START) { |
| 313 RenderProcessHost* rph = RenderProcessHost::FromID(worker_process_id); | 313 RenderProcessHost* rph = RenderProcessHost::FromID(worker_process_id); |
| 314 scoped_refptr<DevToolsAgentHost> agent_host( | 314 scoped_refptr<DevToolsAgentHostImpl> agent_host( |
| 315 GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id)); | 315 GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id)); |
| 316 DevToolsManagerImpl::GetInstance()->Inspect(rph->GetBrowserContext(), | 316 agent_host->Inspect(rph->GetBrowserContext()); |
| 317 agent_host.get()); | |
| 318 } else if (info->state() == WORKER_PAUSED_FOR_REATTACH) { | 317 } else if (info->state() == WORKER_PAUSED_FOR_REATTACH) { |
| 319 info->agent_host()->ReattachToWorker(id); | 318 info->agent_host()->ReattachToWorker(id); |
| 320 info->set_state(WORKER_INSPECTED); | 319 info->set_state(WORKER_INSPECTED); |
| 321 } | 320 } |
| 322 } | 321 } |
| 323 | 322 |
| 324 void EmbeddedWorkerDevToolsManager::RemoveInspectedWorkerData( | 323 void EmbeddedWorkerDevToolsManager::RemoveInspectedWorkerData( |
| 325 EmbeddedWorkerDevToolsAgentHost* agent_host) { | 324 EmbeddedWorkerDevToolsAgentHost* agent_host) { |
| 326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 327 const WorkerId id(agent_host->worker_id()); | 326 const WorkerId id(agent_host->worker_id()); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 scoped_ptr<WorkerInfo> info = workers_.take_and_erase(it); | 378 scoped_ptr<WorkerInfo> info = workers_.take_and_erase(it); |
| 380 info->set_state(WORKER_PAUSED_FOR_REATTACH); | 379 info->set_state(WORKER_PAUSED_FOR_REATTACH); |
| 381 workers_.set(id, info.Pass()); | 380 workers_.set(id, info.Pass()); |
| 382 } | 381 } |
| 383 | 382 |
| 384 void EmbeddedWorkerDevToolsManager::ResetForTesting() { | 383 void EmbeddedWorkerDevToolsManager::ResetForTesting() { |
| 385 workers_.clear(); | 384 workers_.clear(); |
| 386 } | 385 } |
| 387 | 386 |
| 388 } // namespace content | 387 } // namespace content |
| OLD | NEW |