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 SetDevToolsAttachedOnIO(
30 base::WeakPtr
<ServiceWorkerContextCore
> context_weak
,
33 if (ServiceWorkerContextCore
* context
= context_weak
.get()) {
34 if (ServiceWorkerVersion
* version
= context
->GetLiveVersion(version_id
))
35 version
->SetDevToolsAttached(attached
);
41 ServiceWorkerDevToolsAgentHost::ServiceWorkerDevToolsAgentHost(
43 const ServiceWorkerIdentifier
& service_worker
,
44 bool debug_service_worker_on_start
)
45 : WorkerDevToolsAgentHost(worker_id
),
46 service_worker_(new ServiceWorkerIdentifier(service_worker
)) {
47 if (debug_service_worker_on_start
)
48 set_state(WORKER_PAUSED_FOR_DEBUG_ON_START
);
51 DevToolsAgentHost::Type
ServiceWorkerDevToolsAgentHost::GetType() {
52 return TYPE_SERVICE_WORKER
;
55 std::string
ServiceWorkerDevToolsAgentHost::GetTitle() {
56 if (RenderProcessHost
* host
= RenderProcessHost::FromID(worker_id().first
)) {
57 return base::StringPrintf("Worker pid:%d",
58 base::GetProcId(host
->GetHandle()));
63 GURL
ServiceWorkerDevToolsAgentHost::GetURL() {
64 return service_worker_
->url();
67 bool ServiceWorkerDevToolsAgentHost::Activate() {
71 bool ServiceWorkerDevToolsAgentHost::Close() {
72 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
73 base::Bind(&TerminateServiceWorkerOnIO
,
74 service_worker_
->context_weak(),
75 service_worker_
->version_id()));
79 void ServiceWorkerDevToolsAgentHost::OnClientAttached() {
80 WorkerDevToolsAgentHost::OnClientAttached();
81 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
82 base::Bind(&SetDevToolsAttachedOnIO
,
83 service_worker_
->context_weak(),
84 service_worker_
->version_id(),
88 void ServiceWorkerDevToolsAgentHost::OnClientDetached() {
89 WorkerDevToolsAgentHost::OnClientDetached();
90 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
91 base::Bind(&SetDevToolsAttachedOnIO
,
92 service_worker_
->context_weak(),
93 service_worker_
->version_id(),
97 bool ServiceWorkerDevToolsAgentHost::Matches(
98 const ServiceWorkerIdentifier
& other
) {
99 return service_worker_
->Matches(other
);
102 ServiceWorkerDevToolsAgentHost::~ServiceWorkerDevToolsAgentHost() {
103 ServiceWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData(
107 } // namespace content