Change next_proto member type.
[chromium-blink-merge.git] / content / browser / devtools / embedded_worker_devtools_manager.cc
blob1e36e76c3bcec78db281b934070137eaedca3aca
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/embedded_worker_devtools_manager.h"
7 #include "content/browser/devtools/devtools_manager.h"
8 #include "content/browser/devtools/embedded_worker_devtools_agent_host.h"
9 #include "content/browser/devtools/ipc_devtools_agent_host.h"
10 #include "content/browser/shared_worker/shared_worker_instance.h"
11 #include "content/common/devtools_messages.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/worker_service.h"
15 #include "ipc/ipc_listener.h"
17 namespace content {
19 // Called on the UI thread.
20 // static
21 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForWorker(
22 int worker_process_id,
23 int worker_route_id) {
24 return EmbeddedWorkerDevToolsManager::GetInstance()
25 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id);
28 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
29 const ServiceWorkerContextCore* context,
30 base::WeakPtr<ServiceWorkerContextCore> context_weak,
31 int64 version_id,
32 const GURL& url)
33 : context_(context),
34 context_weak_(context_weak),
35 version_id_(version_id),
36 url_(url) {
39 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
40 const ServiceWorkerIdentifier& other)
41 : context_(other.context_),
42 context_weak_(other.context_weak_),
43 version_id_(other.version_id_),
44 url_(other.url_) {
47 EmbeddedWorkerDevToolsManager::
48 ServiceWorkerIdentifier::~ServiceWorkerIdentifier() {
51 bool EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::Matches(
52 const ServiceWorkerIdentifier& other) const {
53 return context_ == other.context_ && version_id_ == other.version_id_;
56 const ServiceWorkerContextCore*
57 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context() const {
58 return context_;
61 base::WeakPtr<ServiceWorkerContextCore>
62 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context_weak() const {
63 return context_weak_;
66 int64
67 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::version_id() const {
68 return version_id_;
71 GURL EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::url() const {
72 return url_;
75 // static
76 EmbeddedWorkerDevToolsManager* EmbeddedWorkerDevToolsManager::GetInstance() {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
78 return Singleton<EmbeddedWorkerDevToolsManager>::get();
81 DevToolsAgentHostImpl*
82 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker(
83 int worker_process_id,
84 int worker_route_id) {
85 AgentHostMap::iterator it = workers_.find(
86 WorkerId(worker_process_id, worker_route_id));
87 return it == workers_.end() ? NULL : it->second;
90 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager()
91 : debug_service_worker_on_start_(false) {
94 EmbeddedWorkerDevToolsManager::~EmbeddedWorkerDevToolsManager() {
97 bool EmbeddedWorkerDevToolsManager::SharedWorkerCreated(
98 int worker_process_id,
99 int worker_route_id,
100 const SharedWorkerInstance& instance) {
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
102 const WorkerId id(worker_process_id, worker_route_id);
103 AgentHostMap::iterator it = FindExistingSharedWorkerAgentHost(instance);
104 if (it == workers_.end()) {
105 workers_[id] = new EmbeddedWorkerDevToolsAgentHost(id, instance);
106 DevToolsManager::GetInstance()->AgentHostChanged(workers_[id]);
107 return false;
109 WorkerRestarted(id, it);
110 return true;
113 bool EmbeddedWorkerDevToolsManager::ServiceWorkerCreated(
114 int worker_process_id,
115 int worker_route_id,
116 const ServiceWorkerIdentifier& service_worker_id) {
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
118 const WorkerId id(worker_process_id, worker_route_id);
119 AgentHostMap::iterator it =
120 FindExistingServiceWorkerAgentHost(service_worker_id);
121 if (it == workers_.end()) {
122 workers_[id] = new EmbeddedWorkerDevToolsAgentHost(
123 id, service_worker_id, debug_service_worker_on_start_);
124 DevToolsManager::GetInstance()->AgentHostChanged(workers_[id]);
125 return debug_service_worker_on_start_;
127 WorkerRestarted(id, it);
128 return true;
131 void EmbeddedWorkerDevToolsManager::WorkerDestroyed(int worker_process_id,
132 int worker_route_id) {
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
134 const WorkerId id(worker_process_id, worker_route_id);
135 AgentHostMap::iterator it = workers_.find(id);
136 DCHECK(it != workers_.end());
137 scoped_refptr<EmbeddedWorkerDevToolsAgentHost> agent_host(it->second);
138 agent_host->WorkerDestroyed();
139 DevToolsManager::GetInstance()->AgentHostChanged(agent_host);
142 void EmbeddedWorkerDevToolsManager::WorkerReadyForInspection(
143 int worker_process_id,
144 int worker_route_id) {
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
146 const WorkerId id(worker_process_id, worker_route_id);
147 AgentHostMap::iterator it = workers_.find(id);
148 DCHECK(it != workers_.end());
149 it->second->WorkerReadyForInspection();
152 void EmbeddedWorkerDevToolsManager::RemoveInspectedWorkerData(WorkerId id) {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
154 workers_.erase(id);
157 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator
158 EmbeddedWorkerDevToolsManager::FindExistingSharedWorkerAgentHost(
159 const SharedWorkerInstance& instance) {
160 AgentHostMap::iterator it = workers_.begin();
161 for (; it != workers_.end(); ++it) {
162 if (it->second->Matches(instance))
163 break;
165 return it;
168 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator
169 EmbeddedWorkerDevToolsManager::FindExistingServiceWorkerAgentHost(
170 const ServiceWorkerIdentifier& service_worker_id) {
171 AgentHostMap::iterator it = workers_.begin();
172 for (; it != workers_.end(); ++it) {
173 if (it->second->Matches(service_worker_id))
174 break;
176 return it;
179 DevToolsAgentHost::List
180 EmbeddedWorkerDevToolsManager::GetOrCreateAllAgentHosts() {
181 DevToolsAgentHost::List result;
182 EmbeddedWorkerDevToolsManager* instance = GetInstance();
183 for (AgentHostMap::iterator it = instance->workers_.begin();
184 it != instance->workers_.end(); ++it) {
185 if (!it->second->IsTerminated())
186 result.push_back(it->second);
188 return result;
191 void EmbeddedWorkerDevToolsManager::WorkerRestarted(
192 const WorkerId& id,
193 const AgentHostMap::iterator& it) {
194 EmbeddedWorkerDevToolsAgentHost* agent_host = it->second;
195 agent_host->WorkerRestarted(id);
196 workers_.erase(it);
197 workers_[id] = agent_host;
198 DevToolsManager::GetInstance()->AgentHostChanged(agent_host);
201 void EmbeddedWorkerDevToolsManager::ResetForTesting() {
202 workers_.clear();
205 } // namespace content