summaryrefslogtreecommitdiffstats
path: root/content/browser/devtools/service_worker_devtools_agent_host.cc
blob: 777b438ad988e65558b86fb085ec480dd267a0dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/devtools/service_worker_devtools_agent_host.h"

#include "base/strings/stringprintf.h"
#include "content/browser/devtools/service_worker_devtools_manager.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_version.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"

namespace content {

namespace {

void StatusNoOp(ServiceWorkerStatusCode status) {}

void TerminateServiceWorkerOnIO(
    base::WeakPtr<ServiceWorkerContextCore> context_weak,
    int64 version_id) {
  if (ServiceWorkerContextCore* context = context_weak.get()) {
    if (ServiceWorkerVersion* version = context->GetLiveVersion(version_id))
      version->StopWorker(base::Bind(&StatusNoOp));
  }
}

void SetDevToolsAttachedOnIO(
    base::WeakPtr<ServiceWorkerContextCore> context_weak,
    int64 version_id,
    bool attached) {
  if (ServiceWorkerContextCore* context = context_weak.get()) {
    if (ServiceWorkerVersion* version = context->GetLiveVersion(version_id))
      version->SetDevToolsAttached(attached);
  }
}

}  // namespace

ServiceWorkerDevToolsAgentHost::ServiceWorkerDevToolsAgentHost(
    WorkerId worker_id,
    const ServiceWorkerIdentifier& service_worker,
    bool debug_service_worker_on_start)
    : WorkerDevToolsAgentHost(worker_id),
      service_worker_(new ServiceWorkerIdentifier(service_worker)) {
  if (debug_service_worker_on_start)
    set_state(WORKER_PAUSED_FOR_DEBUG_ON_START);
}

DevToolsAgentHost::Type ServiceWorkerDevToolsAgentHost::GetType() {
  return TYPE_SERVICE_WORKER;
}

std::string ServiceWorkerDevToolsAgentHost::GetTitle() {
  if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id().first)) {
    return base::StringPrintf("Worker pid:%d",
                              base::GetProcId(host->GetHandle()));
  }
  return "";
}

GURL ServiceWorkerDevToolsAgentHost::GetURL() {
  return service_worker_->url();
}

bool ServiceWorkerDevToolsAgentHost::Activate() {
  return false;
}

bool ServiceWorkerDevToolsAgentHost::Close() {
  BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
      base::Bind(&TerminateServiceWorkerOnIO,
                  service_worker_->context_weak(),
                  service_worker_->version_id()));
  return true;
}

void ServiceWorkerDevToolsAgentHost::OnClientAttached() {
  WorkerDevToolsAgentHost::OnClientAttached();
  BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
      base::Bind(&SetDevToolsAttachedOnIO,
                  service_worker_->context_weak(),
                  service_worker_->version_id(),
                  true));
}

void ServiceWorkerDevToolsAgentHost::OnClientDetached() {
  WorkerDevToolsAgentHost::OnClientDetached();
  BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
      base::Bind(&SetDevToolsAttachedOnIO,
                  service_worker_->context_weak(),
                  service_worker_->version_id(),
                  false));
}

bool ServiceWorkerDevToolsAgentHost::Matches(
    const ServiceWorkerIdentifier& other) {
  return service_worker_->Matches(other);
}

ServiceWorkerDevToolsAgentHost::~ServiceWorkerDevToolsAgentHost() {
  ServiceWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData(
      worker_id());
}

}  // namespace content