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"
19 // Called on the UI thread.
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
,
34 context_weak_(context_weak
),
35 version_id_(version_id
),
39 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
40 const ServiceWorkerIdentifier
& other
)
41 : context_(other
.context_
),
42 context_weak_(other
.context_weak_
),
43 version_id_(other
.version_id_
),
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 {
61 base::WeakPtr
<ServiceWorkerContextCore
>
62 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context_weak() const {
67 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::version_id() const {
71 GURL
EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::url() const {
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
,
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
]);
109 WorkerRestarted(id
, it
);
113 bool EmbeddedWorkerDevToolsManager::ServiceWorkerCreated(
114 int worker_process_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
);
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
));
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
))
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
))
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
);
191 void EmbeddedWorkerDevToolsManager::WorkerRestarted(
193 const AgentHostMap::iterator
& it
) {
194 EmbeddedWorkerDevToolsAgentHost
* agent_host
= it
->second
;
195 agent_host
->WorkerRestarted(id
);
197 workers_
[id
] = agent_host
;
198 DevToolsManager::GetInstance()->AgentHostChanged(agent_host
);
201 void EmbeddedWorkerDevToolsManager::ResetForTesting() {
205 } // namespace content