OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/browser/devtools/embedded_worker_devtools_manager.h" | |
6 | |
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/common/devtools_messages.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "content/public/browser/render_process_host.h" | |
15 #include "content/public/browser/worker_service.h" | |
16 #include "ipc/ipc_listener.h" | |
17 | |
18 namespace content { | |
19 | |
20 // Called on the UI thread. | |
21 // static | |
22 scoped_refptr<DevToolsAgentHost> DevToolsAgentHost::GetForWorker( | |
23 int worker_process_id, | |
24 int worker_route_id) { | |
25 return EmbeddedWorkerDevToolsManager::GetInstance() | |
26 ->GetDevToolsAgentHostForWorker(worker_process_id, worker_route_id); | |
27 } | |
28 | |
29 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier( | |
30 const ServiceWorkerContextCore* context, | |
31 base::WeakPtr<ServiceWorkerContextCore> context_weak, | |
32 int64 version_id, | |
33 const GURL& url) | |
34 : context_(context), | |
35 context_weak_(context_weak), | |
36 version_id_(version_id), | |
37 url_(url) { | |
38 } | |
39 | |
40 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier( | |
41 const ServiceWorkerIdentifier& other) | |
42 : context_(other.context_), | |
43 context_weak_(other.context_weak_), | |
44 version_id_(other.version_id_), | |
45 url_(other.url_) { | |
46 } | |
47 | |
48 EmbeddedWorkerDevToolsManager:: | |
49 ServiceWorkerIdentifier::~ServiceWorkerIdentifier() { | |
50 } | |
51 | |
52 bool EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::Matches( | |
53 const ServiceWorkerIdentifier& other) const { | |
54 return context_ == other.context_ && version_id_ == other.version_id_; | |
55 } | |
56 | |
57 const ServiceWorkerContextCore* | |
58 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context() const { | |
59 return context_; | |
60 } | |
61 | |
62 base::WeakPtr<ServiceWorkerContextCore> | |
63 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::context_weak() const { | |
64 return context_weak_; | |
65 } | |
66 | |
67 int64 | |
68 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::version_id() const { | |
69 return version_id_; | |
70 } | |
71 | |
72 GURL EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::url() const { | |
73 return url_; | |
74 } | |
75 | |
76 // static | |
77 EmbeddedWorkerDevToolsManager* EmbeddedWorkerDevToolsManager::GetInstance() { | |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
79 return Singleton<EmbeddedWorkerDevToolsManager>::get(); | |
80 } | |
81 | |
82 DevToolsAgentHostImpl* | |
83 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker( | |
84 int worker_process_id, | |
85 int worker_route_id) { | |
86 AgentHostMap::iterator it = workers_.find( | |
87 WorkerId(worker_process_id, worker_route_id)); | |
88 return it == workers_.end() ? NULL : it->second; | |
89 } | |
90 | |
91 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager() | |
92 : debug_service_worker_on_start_(false) { | |
93 } | |
94 | |
95 EmbeddedWorkerDevToolsManager::~EmbeddedWorkerDevToolsManager() { | |
96 } | |
97 | |
98 bool EmbeddedWorkerDevToolsManager::SharedWorkerCreated( | |
99 int worker_process_id, | |
100 int worker_route_id, | |
101 const SharedWorkerInstance& instance) { | |
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
103 const WorkerId id(worker_process_id, worker_route_id); | |
104 AgentHostMap::iterator it = FindExistingSharedWorkerAgentHost(instance); | |
105 if (it == workers_.end()) { | |
106 workers_[id] = new SharedWorkerDevToolsAgentHost(id, instance); | |
107 DevToolsManager::GetInstance()->AgentHostChanged(workers_[id]); | |
108 return false; | |
109 } | |
110 WorkerRestarted(id, it); | |
111 return true; | |
112 } | |
113 | |
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 ServiceWorkerDevToolsAgentHost( | |
124 id, service_worker_id, debug_service_worker_on_start_); | |
125 DevToolsManager::GetInstance()->AgentHostChanged(workers_[id]); | |
126 return debug_service_worker_on_start_; | |
127 } | |
128 WorkerRestarted(id, it); | |
129 return true; | |
130 } | |
131 | |
132 void EmbeddedWorkerDevToolsManager::WorkerDestroyed(int worker_process_id, | |
133 int worker_route_id) { | |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
135 const WorkerId id(worker_process_id, worker_route_id); | |
136 AgentHostMap::iterator it = workers_.find(id); | |
137 DCHECK(it != workers_.end()); | |
138 scoped_refptr<EmbeddedWorkerDevToolsAgentHost> agent_host(it->second); | |
139 agent_host->WorkerDestroyed(); | |
140 DevToolsManager::GetInstance()->AgentHostChanged(agent_host); | |
141 } | |
142 | |
143 void EmbeddedWorkerDevToolsManager::WorkerStopIgnored(int worker_process_id, | |
144 int worker_route_id) { | |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
146 // TODO(pfeldman): Show a console message to tell the user that UA didn't | |
147 // terminate the worker because devtools is attached. | |
148 } | |
149 | |
150 void EmbeddedWorkerDevToolsManager::WorkerReadyForInspection( | |
151 int worker_process_id, | |
152 int worker_route_id) { | |
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
154 const WorkerId id(worker_process_id, worker_route_id); | |
155 AgentHostMap::iterator it = workers_.find(id); | |
156 DCHECK(it != workers_.end()); | |
157 it->second->WorkerReadyForInspection(); | |
158 } | |
159 | |
160 void EmbeddedWorkerDevToolsManager::RemoveInspectedWorkerData(WorkerId id) { | |
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
162 workers_.erase(id); | |
163 } | |
164 | |
165 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator | |
166 EmbeddedWorkerDevToolsManager::FindExistingSharedWorkerAgentHost( | |
167 const SharedWorkerInstance& instance) { | |
168 AgentHostMap::iterator it = workers_.begin(); | |
169 for (; it != workers_.end(); ++it) { | |
170 if (it->second->Matches(instance)) | |
171 break; | |
172 } | |
173 return it; | |
174 } | |
175 | |
176 EmbeddedWorkerDevToolsManager::AgentHostMap::iterator | |
177 EmbeddedWorkerDevToolsManager::FindExistingServiceWorkerAgentHost( | |
178 const ServiceWorkerIdentifier& service_worker_id) { | |
179 AgentHostMap::iterator it = workers_.begin(); | |
180 for (; it != workers_.end(); ++it) { | |
181 if (it->second->Matches(service_worker_id)) | |
182 break; | |
183 } | |
184 return it; | |
185 } | |
186 | |
187 DevToolsAgentHost::List | |
188 EmbeddedWorkerDevToolsManager::GetOrCreateAllAgentHosts() { | |
189 DevToolsAgentHost::List result; | |
190 EmbeddedWorkerDevToolsManager* instance = GetInstance(); | |
191 for (AgentHostMap::iterator it = instance->workers_.begin(); | |
192 it != instance->workers_.end(); ++it) { | |
193 if (!it->second->IsTerminated()) | |
194 result.push_back(it->second); | |
195 } | |
196 return result; | |
197 } | |
198 | |
199 void EmbeddedWorkerDevToolsManager::WorkerRestarted( | |
200 const WorkerId& id, | |
201 const AgentHostMap::iterator& it) { | |
202 EmbeddedWorkerDevToolsAgentHost* agent_host = it->second; | |
203 agent_host->WorkerRestarted(id); | |
204 workers_.erase(it); | |
205 workers_[id] = agent_host; | |
206 DevToolsManager::GetInstance()->AgentHostChanged(agent_host); | |
207 } | |
208 | |
209 void EmbeddedWorkerDevToolsManager::ResetForTesting() { | |
210 workers_.clear(); | |
211 } | |
212 | |
213 } // namespace content | |
OLD | NEW |