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/devtools_protocol.h"
9 #include "content/browser/devtools/devtools_protocol_constants.h"
10 #include "content/browser/devtools/embedded_worker_devtools_agent_host.h"
11 #include "content/browser/devtools/ipc_devtools_agent_host.h"
12 #include "content/browser/shared_worker/shared_worker_instance.h"
13 #include "content/common/devtools_messages.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/worker_service.h"
17 #include "ipc/ipc_listener.h"
21 // Called on the UI thread.
23 scoped_refptr
<DevToolsAgentHost
> DevToolsAgentHost::GetForWorker(
24 int worker_process_id
,
25 int worker_route_id
) {
26 return EmbeddedWorkerDevToolsManager::GetInstance()
27 ->GetDevToolsAgentHostForWorker(worker_process_id
, worker_route_id
);
30 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
31 const ServiceWorkerContextCore
* context
,
32 base::WeakPtr
<ServiceWorkerContextCore
> context_weak
,
36 context_weak_(context_weak
),
37 version_id_(version_id
),
41 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
42 const ServiceWorkerIdentifier
& other
)
43 : context_(other
.context_
),
44 context_weak_(other
.context_weak_
),
45 version_id_(other
.version_id_
),
49 EmbeddedWorkerDevToolsManager::
50 ServiceWorkerIdentifier::~ServiceWorkerIdentifier() {
53 bool EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::Matches(
54 const ServiceWorkerIdentifier
& other
) const {
55 return context_
== other
.context_
&& version_id_
== other
.version_id_
;
58 const ServiceWorkerContextCore
*
59 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context() const {
63 base::WeakPtr
<ServiceWorkerContextCore
>
64 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context_weak() const {
69 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::version_id() const {
73 GURL
EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::url() const {
78 EmbeddedWorkerDevToolsManager
* EmbeddedWorkerDevToolsManager::GetInstance() {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
80 return Singleton
<EmbeddedWorkerDevToolsManager
>::get();
83 DevToolsAgentHostImpl
*
84 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker(
85 int worker_process_id
,
86 int worker_route_id
) {
87 AgentHostMap::iterator it
= workers_
.find(
88 WorkerId(worker_process_id
, worker_route_id
));
89 return it
== workers_
.end() ? NULL
: it
->second
;
92 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager()
93 : debug_service_worker_on_start_(false) {
96 EmbeddedWorkerDevToolsManager::~EmbeddedWorkerDevToolsManager() {
99 bool EmbeddedWorkerDevToolsManager::SharedWorkerCreated(
100 int worker_process_id
,
102 const SharedWorkerInstance
& instance
) {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
104 const WorkerId
id(worker_process_id
, worker_route_id
);
105 AgentHostMap::iterator it
= FindExistingSharedWorkerAgentHost(instance
);
106 if (it
== workers_
.end()) {
107 workers_
[id
] = new EmbeddedWorkerDevToolsAgentHost(id
, instance
);
108 DevToolsManager::GetInstance()->AgentHostChanged(workers_
[id
]);
111 WorkerRestarted(id
, it
);
115 bool EmbeddedWorkerDevToolsManager::ServiceWorkerCreated(
116 int worker_process_id
,
118 const ServiceWorkerIdentifier
& service_worker_id
) {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
120 const WorkerId
id(worker_process_id
, worker_route_id
);
121 AgentHostMap::iterator it
=
122 FindExistingServiceWorkerAgentHost(service_worker_id
);
123 if (it
== workers_
.end()) {
124 workers_
[id
] = new EmbeddedWorkerDevToolsAgentHost(
125 id
, service_worker_id
, debug_service_worker_on_start_
);
126 DevToolsManager::GetInstance()->AgentHostChanged(workers_
[id
]);
127 return debug_service_worker_on_start_
;
129 WorkerRestarted(id
, it
);
133 void EmbeddedWorkerDevToolsManager::WorkerDestroyed(int worker_process_id
,
134 int worker_route_id
) {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
136 const WorkerId
id(worker_process_id
, worker_route_id
);
137 AgentHostMap::iterator it
= workers_
.find(id
);
138 DCHECK(it
!= workers_
.end());
139 scoped_refptr
<EmbeddedWorkerDevToolsAgentHost
> agent_host(it
->second
);
140 agent_host
->WorkerDestroyed();
141 DevToolsManager::GetInstance()->AgentHostChanged(agent_host
);
144 void EmbeddedWorkerDevToolsManager::WorkerReadyForInspection(
145 int worker_process_id
,
146 int worker_route_id
) {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
148 const WorkerId
id(worker_process_id
, worker_route_id
);
149 AgentHostMap::iterator it
= workers_
.find(id
);
150 DCHECK(it
!= workers_
.end());
151 it
->second
->WorkerReadyForInspection();
154 void EmbeddedWorkerDevToolsManager::RemoveInspectedWorkerData(WorkerId id
) {
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
159 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator
160 EmbeddedWorkerDevToolsManager::FindExistingSharedWorkerAgentHost(
161 const SharedWorkerInstance
& instance
) {
162 AgentHostMap::iterator it
= workers_
.begin();
163 for (; it
!= workers_
.end(); ++it
) {
164 if (it
->second
->Matches(instance
))
170 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator
171 EmbeddedWorkerDevToolsManager::FindExistingServiceWorkerAgentHost(
172 const ServiceWorkerIdentifier
& service_worker_id
) {
173 AgentHostMap::iterator it
= workers_
.begin();
174 for (; it
!= workers_
.end(); ++it
) {
175 if (it
->second
->Matches(service_worker_id
))
181 DevToolsAgentHost::List
182 EmbeddedWorkerDevToolsManager::GetOrCreateAllAgentHosts() {
183 DevToolsAgentHost::List result
;
184 EmbeddedWorkerDevToolsManager
* instance
= GetInstance();
185 for (AgentHostMap::iterator it
= instance
->workers_
.begin();
186 it
!= instance
->workers_
.end(); ++it
) {
187 if (!it
->second
->IsTerminated())
188 result
.push_back(it
->second
);
193 void EmbeddedWorkerDevToolsManager::WorkerRestarted(
195 const AgentHostMap::iterator
& it
) {
196 EmbeddedWorkerDevToolsAgentHost
* agent_host
= it
->second
;
197 agent_host
->WorkerRestarted(id
);
199 workers_
[id
] = agent_host
;
200 DevToolsManager::GetInstance()->AgentHostChanged(agent_host
);
203 void EmbeddedWorkerDevToolsManager::ResetForTesting() {
207 } // namespace content