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_agent_host.h"
7 #include "base/strings/stringprintf.h"
8 #include "content/browser/devtools/service_worker_devtools_manager.h"
9 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_version.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/render_process_host.h"
18 void StatusNoOp(ServiceWorkerStatusCode status
) {}
20 void TerminateServiceWorkerOnIO(
21 base::WeakPtr
<ServiceWorkerContextCore
> context_weak
,
23 if (ServiceWorkerContextCore
* context
= context_weak
.get()) {
24 if (ServiceWorkerVersion
* version
= context
->GetLiveVersion(version_id
))
25 version
->StopWorker(base::Bind(&StatusNoOp
));
29 void UnregisterServiceWorkerOnIO(
30 base::WeakPtr
<ServiceWorkerContextCore
> context_weak
,
32 if (ServiceWorkerContextCore
* context
= context_weak
.get()) {
33 if (ServiceWorkerVersion
* version
= context
->GetLiveVersion(version_id
)) {
34 version
->StopWorker(base::Bind(&StatusNoOp
));
35 context
->UnregisterServiceWorker(
36 version
->scope(), base::Bind(&StatusNoOp
));
41 void SetDevToolsAttachedOnIO(
42 base::WeakPtr
<ServiceWorkerContextCore
> context_weak
,
45 if (ServiceWorkerContextCore
* context
= context_weak
.get()) {
46 if (ServiceWorkerVersion
* version
= context
->GetLiveVersion(version_id
))
47 version
->SetDevToolsAttached(attached
);
53 ServiceWorkerDevToolsAgentHost::ServiceWorkerDevToolsAgentHost(
55 const ServiceWorkerIdentifier
& service_worker
)
56 : WorkerDevToolsAgentHost(worker_id
),
57 service_worker_(new ServiceWorkerIdentifier(service_worker
)) {
60 DevToolsAgentHost::Type
ServiceWorkerDevToolsAgentHost::GetType() {
61 return TYPE_SERVICE_WORKER
;
64 std::string
ServiceWorkerDevToolsAgentHost::GetTitle() {
65 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id().first
)) {
66 return base::StringPrintf("Worker pid:%d",
67 base::GetProcId(host
->GetHandle()));
72 GURL
ServiceWorkerDevToolsAgentHost::GetURL() {
73 return service_worker_
->url();
76 bool ServiceWorkerDevToolsAgentHost::Activate() {
80 bool ServiceWorkerDevToolsAgentHost::Close() {
81 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
82 base::Bind(&TerminateServiceWorkerOnIO
,
83 service_worker_
->context_weak(),
84 service_worker_
->version_id()));
88 void ServiceWorkerDevToolsAgentHost::UnregisterWorker() {
89 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
90 base::Bind(&UnregisterServiceWorkerOnIO
,
91 service_worker_
->context_weak(),
92 service_worker_
->version_id()));
95 void ServiceWorkerDevToolsAgentHost::OnClientAttached(bool reattached
) {
96 WorkerDevToolsAgentHost::OnClientAttached(reattached
);
97 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
98 base::Bind(&SetDevToolsAttachedOnIO
,
99 service_worker_
->context_weak(),
100 service_worker_
->version_id(),
104 void ServiceWorkerDevToolsAgentHost::OnClientDetached() {
105 WorkerDevToolsAgentHost::OnClientDetached();
106 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
107 base::Bind(&SetDevToolsAttachedOnIO
,
108 service_worker_
->context_weak(),
109 service_worker_
->version_id(),
113 bool ServiceWorkerDevToolsAgentHost::Matches(
114 const ServiceWorkerIdentifier
& other
) {
115 return service_worker_
->Matches(other
);
118 ServiceWorkerDevToolsAgentHost::~ServiceWorkerDevToolsAgentHost() {
119 ServiceWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData(
123 } // namespace content