Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / devtools / embedded_worker_devtools_manager.cc
bloba28cd9c0b29b07af6fb42588f87ce17def257a27
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/embedded_worker_devtools_manager.h"
7 #include "content/browser/devtools/devtools_manager_impl.h"
8 #include "content/browser/devtools/devtools_protocol.h"
9 #include "content/browser/devtools/devtools_protocol_constants.h"
10 #include "content/browser/devtools/embedded_worker_devtools_agent_host.h"
11 #include "content/browser/devtools/ipc_devtools_agent_host.h"
12 #include "content/browser/shared_worker/shared_worker_instance.h"
13 #include "content/common/devtools_messages.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/worker_service.h"
17 #include "ipc/ipc_listener.h"
19 namespace content {
21 // Called on the UI thread.
22 // static
23 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForWorker(
24 int worker_process_id,
25 int worker_route_id) {
26 return EmbeddedWorkerDevToolsManager::GetInstance()
27 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id);
30 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
31 const ServiceWorkerContextCore* context,
32 base::WeakPtr<ServiceWorkerContextCore> context_weak,
33 int64 version_id,
34 const GURL& url)
35 : context_(context),
36 context_weak_(context_weak),
37 version_id_(version_id),
38 url_(url) {
41 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier(
42 const ServiceWorkerIdentifier& other)
43 : context_(other.context_),
44 context_weak_(other.context_weak_),
45 version_id_(other.version_id_),
46 url_(other.url_) {
49 EmbeddedWorkerDevToolsManager::
50 ServiceWorkerIdentifier::~ServiceWorkerIdentifier() {
53 bool EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::Matches(
54 const ServiceWorkerIdentifier& other) const {
55 return context_ == other.context_ && version_id_ == other.version_id_;
58 const ServiceWorkerContextCore*
59 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context() const {
60 return context_;
63 base::WeakPtr<ServiceWorkerContextCore>
64 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context_weak() const {
65 return context_weak_;
68 int64
69 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::version_id() const {
70 return version_id_;
73 GURL EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::url() const {
74 return url_;
77 // static
78 EmbeddedWorkerDevToolsManager* EmbeddedWorkerDevToolsManager::GetInstance() {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80 return Singleton<EmbeddedWorkerDevToolsManager>::get();
83 DevToolsAgentHostImpl*
84 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker(
85 int worker_process_id,
86 int worker_route_id) {
87 AgentHostMap::iterator it = workers_.find(
88 WorkerId(worker_process_id, worker_route_id));
89 return it == workers_.end() ? NULL : it->second;
92 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager()
93 : debug_service_worker_on_start_(false) {
96 EmbeddedWorkerDevToolsManager::~EmbeddedWorkerDevToolsManager() {
99 bool EmbeddedWorkerDevToolsManager::SharedWorkerCreated(
100 int worker_process_id,
101 int worker_route_id,
102 const SharedWorkerInstance& instance) {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
104 const WorkerId id(worker_process_id, worker_route_id);
105 AgentHostMap::iterator it = FindExistingSharedWorkerAgentHost(instance);
106 if (it == workers_.end()) {
107 workers_[id] = new EmbeddedWorkerDevToolsAgentHost(id, instance);
108 return false;
110 WorkerRestarted(id, it);
111 return true;
114 bool EmbeddedWorkerDevToolsManager::ServiceWorkerCreated(
115 int worker_process_id,
116 int worker_route_id,
117 const ServiceWorkerIdentifier& service_worker_id) {
118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
119 const WorkerId id(worker_process_id, worker_route_id);
120 AgentHostMap::iterator it =
121 FindExistingServiceWorkerAgentHost(service_worker_id);
122 if (it == workers_.end()) {
123 workers_[id] = new EmbeddedWorkerDevToolsAgentHost(
124 id, service_worker_id, debug_service_worker_on_start_);
125 return debug_service_worker_on_start_;
127 WorkerRestarted(id, it);
128 return true;
131 void EmbeddedWorkerDevToolsManager::WorkerDestroyed(int worker_process_id,
132 int worker_route_id) {
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
134 const WorkerId id(worker_process_id, worker_route_id);
135 AgentHostMap::iterator it = workers_.find(id);
136 DCHECK(it != workers_.end());
137 it->second->WorkerDestroyed();
140 void EmbeddedWorkerDevToolsManager::WorkerReadyForInspection(
141 int worker_process_id,
142 int worker_route_id) {
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
144 const WorkerId id(worker_process_id, worker_route_id);
145 AgentHostMap::iterator it = workers_.find(id);
146 DCHECK(it != workers_.end());
147 it->second->WorkerReadyForInspection();
150 void EmbeddedWorkerDevToolsManager::WorkerContextStarted(int worker_process_id,
151 int worker_route_id) {
152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
153 const WorkerId id(worker_process_id, worker_route_id);
154 AgentHostMap::iterator it = workers_.find(id);
155 DCHECK(it != workers_.end());
156 it->second->WorkerContextStarted();
159 void EmbeddedWorkerDevToolsManager::RemoveInspectedWorkerData(WorkerId id) {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
161 workers_.erase(id);
164 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator
165 EmbeddedWorkerDevToolsManager::FindExistingSharedWorkerAgentHost(
166 const SharedWorkerInstance& instance) {
167 AgentHostMap::iterator it = workers_.begin();
168 for (; it != workers_.end(); ++it) {
169 if (it->second->Matches(instance))
170 break;
172 return it;
175 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator
176 EmbeddedWorkerDevToolsManager::FindExistingServiceWorkerAgentHost(
177 const ServiceWorkerIdentifier& service_worker_id) {
178 AgentHostMap::iterator it = workers_.begin();
179 for (; it != workers_.end(); ++it) {
180 if (it->second->Matches(service_worker_id))
181 break;
183 return it;
186 DevToolsAgentHost::List
187 EmbeddedWorkerDevToolsManager::GetOrCreateAllAgentHosts() {
188 DevToolsAgentHost::List result;
189 EmbeddedWorkerDevToolsManager* instance = GetInstance();
190 for (AgentHostMap::iterator it = instance->workers_.begin();
191 it != instance->workers_.end(); ++it) {
192 if (!it->second->IsTerminated())
193 result.push_back(it->second);
195 return result;
198 void EmbeddedWorkerDevToolsManager::WorkerRestarted(
199 const WorkerId& id,
200 const AgentHostMap::iterator& it) {
201 EmbeddedWorkerDevToolsAgentHost* agent_host = it->second;
202 agent_host->WorkerRestarted(id);
203 workers_.erase(it);
204 workers_[id] = agent_host;
207 void EmbeddedWorkerDevToolsManager::ResetForTesting() {
208 workers_.clear();
211 } // namespace content