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/devtools_manager.h"
8 #include "content/browser/devtools/ipc_devtools_agent_host.h"
9 #include "content/browser/devtools/service_worker_devtools_agent_host.h"
10 #include "content/browser/devtools/shared_worker_devtools_agent_host.h"
11 #include "content/browser/shared_worker/shared_worker_instance.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 ServiceWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
20 const ServiceWorkerContextCore
* context
,
21 base::WeakPtr
<ServiceWorkerContextCore
> context_weak
,
25 context_weak_(context_weak
),
26 version_id_(version_id
),
30 ServiceWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
31 const ServiceWorkerIdentifier
& other
)
32 : context_(other
.context_
),
33 context_weak_(other
.context_weak_
),
34 version_id_(other
.version_id_
),
38 ServiceWorkerDevToolsManager::
39 ServiceWorkerIdentifier::~ServiceWorkerIdentifier() {
42 bool ServiceWorkerDevToolsManager::ServiceWorkerIdentifier::Matches(
43 const ServiceWorkerIdentifier
& other
) const {
44 return context_
== other
.context_
&& version_id_
== other
.version_id_
;
48 ServiceWorkerDevToolsManager
* ServiceWorkerDevToolsManager::GetInstance() {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
50 return Singleton
<ServiceWorkerDevToolsManager
>::get();
53 bool ServiceWorkerDevToolsManager::WorkerCreated(
54 int worker_process_id
,
56 const ServiceWorkerIdentifier
& service_worker_id
) {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
58 const WorkerId
id(worker_process_id
, worker_route_id
);
59 AgentHostMap::iterator it
= FindExistingWorkerAgentHost(service_worker_id
);
60 if (it
== workers().end()) {
61 workers()[id
] = new ServiceWorkerDevToolsAgentHost(
62 id
, service_worker_id
, debug_service_worker_on_start_
);
63 DevToolsManager::GetInstance()->AgentHostChanged(workers()[id
]);
64 return debug_service_worker_on_start_
;
66 WorkerRestarted(id
, it
);
67 return it
->second
->IsAttached();
70 void ServiceWorkerDevToolsManager::WorkerStopIgnored(int worker_process_id
,
71 int worker_route_id
) {
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
73 // TODO(pfeldman): Show a console message to tell the user that UA didn't
74 // terminate the worker because devtools is attached.
77 ServiceWorkerDevToolsManager::ServiceWorkerDevToolsManager()
78 : debug_service_worker_on_start_(false) {
81 ServiceWorkerDevToolsManager::~ServiceWorkerDevToolsManager() {
84 ServiceWorkerDevToolsManager::AgentHostMap::iterator
85 ServiceWorkerDevToolsManager::FindExistingWorkerAgentHost(
86 const ServiceWorkerIdentifier
& service_worker_id
) {
87 AgentHostMap::iterator it
= workers().begin();
88 for (; it
!= workers().end(); ++it
) {
89 if (static_cast<ServiceWorkerDevToolsAgentHost
*>(
90 it
->second
)->Matches(service_worker_id
))
96 } // namespace content