Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / devtools / shared_worker_devtools_manager.cc
blob3fb2cf111c308d938fcdba8f03eed668babec7db
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
3 // found in the LICENSE file.
5 #include "content/browser/devtools/shared_worker_devtools_manager.h"
7 #include "content/browser/devtools/devtools_manager.h"
8 #include "content/browser/devtools/shared_worker_devtools_agent_host.h"
9 #include "content/browser/shared_worker/shared_worker_instance.h"
10 #include "content/public/browser/browser_thread.h"
12 namespace content {
14 // static
15 SharedWorkerDevToolsManager* SharedWorkerDevToolsManager::GetInstance() {
16 DCHECK_CURRENTLY_ON(BrowserThread::UI);
17 return Singleton<SharedWorkerDevToolsManager>::get();
20 DevToolsAgentHostImpl*
21 SharedWorkerDevToolsManager::GetDevToolsAgentHostForWorker(
22 int worker_process_id,
23 int worker_route_id) {
24 AgentHostMap::iterator it = workers_.find(
25 WorkerId(worker_process_id, worker_route_id));
26 return it == workers_.end() ? NULL : it->second;
29 void SharedWorkerDevToolsManager::AddAllAgentHosts(
30 SharedWorkerDevToolsAgentHost::List* result) {
31 for (auto& worker : workers_) {
32 if (!worker.second->IsTerminated())
33 result->push_back(worker.second);
37 bool SharedWorkerDevToolsManager::WorkerCreated(
38 int worker_process_id,
39 int worker_route_id,
40 const SharedWorkerInstance& instance) {
41 DCHECK_CURRENTLY_ON(BrowserThread::UI);
42 const WorkerId id(worker_process_id, worker_route_id);
43 AgentHostMap::iterator it =
44 FindExistingWorkerAgentHost(instance);
45 if (it == workers_.end()) {
46 workers_[id] = new SharedWorkerDevToolsAgentHost(id, instance);
47 DevToolsManager::GetInstance()->AgentHostChanged(workers_[id]);
48 return false;
51 // Worker restarted.
52 SharedWorkerDevToolsAgentHost* agent_host = it->second;
53 agent_host->WorkerRestarted(id);
54 workers_.erase(it);
55 workers_[id] = agent_host;
56 DevToolsManager::GetInstance()->AgentHostChanged(agent_host);
57 return true;
60 void SharedWorkerDevToolsManager::WorkerReadyForInspection(
61 int worker_process_id,
62 int worker_route_id) {
63 DCHECK_CURRENTLY_ON(BrowserThread::UI);
64 const WorkerId id(worker_process_id, worker_route_id);
65 AgentHostMap::iterator it = workers_.find(id);
66 DCHECK(it != workers_.end());
67 it->second->WorkerReadyForInspection();
70 void SharedWorkerDevToolsManager::WorkerDestroyed(
71 int worker_process_id,
72 int worker_route_id) {
73 DCHECK_CURRENTLY_ON(BrowserThread::UI);
74 const WorkerId id(worker_process_id, worker_route_id);
75 AgentHostMap::iterator it = workers_.find(id);
76 DCHECK(it != workers_.end());
77 scoped_refptr<SharedWorkerDevToolsAgentHost> agent_host(it->second);
78 agent_host->WorkerDestroyed();
79 DevToolsManager::GetInstance()->AgentHostChanged(agent_host);
82 void SharedWorkerDevToolsManager::RemoveInspectedWorkerData(WorkerId id) {
83 DCHECK_CURRENTLY_ON(BrowserThread::UI);
84 workers_.erase(id);
86 SharedWorkerDevToolsManager::SharedWorkerDevToolsManager() {
89 SharedWorkerDevToolsManager::~SharedWorkerDevToolsManager() {
92 SharedWorkerDevToolsManager::AgentHostMap::iterator
93 SharedWorkerDevToolsManager::FindExistingWorkerAgentHost(
94 const SharedWorkerInstance& instance) {
95 AgentHostMap::iterator it = workers_.begin();
96 for (; it != workers_.end(); ++it) {
97 if (it->second->Matches(instance))
98 break;
100 return it;
103 void SharedWorkerDevToolsManager::ResetForTesting() {
104 workers_.clear();
108 } // namespace content