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/service_worker_devtools_manager.h"
7 #include "content/browser/devtools/ipc_devtools_agent_host.h"
8 #include "content/browser/devtools/service_worker_devtools_agent_host.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/browser/worker_service.h"
12 #include "ipc/ipc_listener.h"
16 ServiceWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
17 const ServiceWorkerContextCore
* context
,
18 base::WeakPtr
<ServiceWorkerContextCore
> context_weak
,
22 context_weak_(context_weak
),
23 version_id_(version_id
),
27 ServiceWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
28 const ServiceWorkerIdentifier
& other
)
29 : context_(other
.context_
),
30 context_weak_(other
.context_weak_
),
31 version_id_(other
.version_id_
),
35 ServiceWorkerDevToolsManager::
36 ServiceWorkerIdentifier::~ServiceWorkerIdentifier() {
39 bool ServiceWorkerDevToolsManager::ServiceWorkerIdentifier::Matches(
40 const ServiceWorkerIdentifier
& other
) const {
41 return context_
== other
.context_
&& version_id_
== other
.version_id_
;
45 ServiceWorkerDevToolsManager
* ServiceWorkerDevToolsManager::GetInstance() {
46 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
47 return Singleton
<ServiceWorkerDevToolsManager
>::get();
50 DevToolsAgentHostImpl
*
51 ServiceWorkerDevToolsManager::GetDevToolsAgentHostForWorker(
52 int worker_process_id
,
53 int worker_route_id
) {
54 AgentHostMap::iterator it
= workers_
.find(
55 WorkerId(worker_process_id
, worker_route_id
));
56 return it
== workers_
.end() ? NULL
: it
->second
;
59 void ServiceWorkerDevToolsManager::AddAllAgentHosts(
60 ServiceWorkerDevToolsAgentHost::List
* result
) {
61 for (auto& worker
: workers_
) {
62 if (!worker
.second
->IsTerminated())
63 result
->push_back(worker
.second
);
67 bool ServiceWorkerDevToolsManager::WorkerCreated(
68 int worker_process_id
,
70 const ServiceWorkerIdentifier
& service_worker_id
) {
71 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
72 const WorkerId
id(worker_process_id
, worker_route_id
);
73 AgentHostMap::iterator it
= FindExistingWorkerAgentHost(service_worker_id
);
74 if (it
== workers_
.end()) {
75 scoped_refptr
<ServiceWorkerDevToolsAgentHost
> host
=
76 new ServiceWorkerDevToolsAgentHost(
77 id
, service_worker_id
);
78 workers_
[id
] = host
.get();
79 FOR_EACH_OBSERVER(Observer
, observer_list_
, WorkerCreated(host
.get()));
80 if (debug_service_worker_on_start_
)
81 host
->PauseForDebugOnStart();
82 return host
->IsPausedForDebugOnStart();
85 // Worker was restarted.
86 ServiceWorkerDevToolsAgentHost
* agent_host
= it
->second
;
87 agent_host
->WorkerRestarted(id
);
89 workers_
[id
] = agent_host
;
91 return agent_host
->IsAttached();
94 void ServiceWorkerDevToolsManager::WorkerReadyForInspection(
95 int worker_process_id
,
96 int worker_route_id
) {
97 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
98 const WorkerId
id(worker_process_id
, worker_route_id
);
99 AgentHostMap::iterator it
= workers_
.find(id
);
100 DCHECK(it
!= workers_
.end());
101 scoped_refptr
<ServiceWorkerDevToolsAgentHost
> host
= it
->second
;
102 host
->WorkerReadyForInspection();
103 FOR_EACH_OBSERVER(Observer
, observer_list_
,
104 WorkerReadyForInspection(host
.get()));
106 // Then bring up UI for the ones not picked by other clients.
107 if (host
->IsPausedForDebugOnStart() && !host
->IsAttached()) {
108 host
->Inspect(RenderProcessHost::FromID(worker_process_id
)->
109 GetBrowserContext());
113 void ServiceWorkerDevToolsManager::WorkerStopIgnored(int worker_process_id
,
114 int worker_route_id
) {
115 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
116 // TODO(pfeldman): Show a console message to tell the user that UA didn't
117 // terminate the worker because devtools is attached.
120 void ServiceWorkerDevToolsManager::WorkerDestroyed(int worker_process_id
,
121 int worker_route_id
) {
122 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
123 const WorkerId
id(worker_process_id
, worker_route_id
);
124 AgentHostMap::iterator it
= workers_
.find(id
);
125 DCHECK(it
!= workers_
.end());
126 scoped_refptr
<WorkerDevToolsAgentHost
> agent_host(it
->second
);
127 agent_host
->WorkerDestroyed();
128 FOR_EACH_OBSERVER(Observer
, observer_list_
, WorkerDestroyed(it
->second
));
131 void ServiceWorkerDevToolsManager::RemoveInspectedWorkerData(WorkerId id
) {
132 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
136 void ServiceWorkerDevToolsManager::AddObserver(Observer
* observer
) {
137 observer_list_
.AddObserver(observer
);
140 void ServiceWorkerDevToolsManager::RemoveObserver(Observer
* observer
) {
141 observer_list_
.RemoveObserver(observer
);
144 void ServiceWorkerDevToolsManager::set_debug_service_worker_on_start(
145 bool debug_on_start
) {
146 debug_service_worker_on_start_
= debug_on_start
;
147 FOR_EACH_OBSERVER(Observer
, observer_list_
,
148 DebugOnStartUpdated(debug_on_start
));
151 ServiceWorkerDevToolsManager::ServiceWorkerDevToolsManager()
152 : debug_service_worker_on_start_(false) {
155 ServiceWorkerDevToolsManager::~ServiceWorkerDevToolsManager() {
158 ServiceWorkerDevToolsManager::AgentHostMap::iterator
159 ServiceWorkerDevToolsManager::FindExistingWorkerAgentHost(
160 const ServiceWorkerIdentifier
& service_worker_id
) {
161 AgentHostMap::iterator it
= workers_
.begin();
162 for (; it
!= workers_
.end(); ++it
) {
163 if (static_cast<ServiceWorkerDevToolsAgentHost
*>(
164 it
->second
)->Matches(service_worker_id
))
170 void ServiceWorkerDevToolsManager::ResetForTesting() {
174 } // namespace content