OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/devtools/embedded_worker_devtools_manager.h" | 5 #include "content/browser/devtools/embedded_worker_devtools_manager.h" |
6 | 6 |
7 #include "content/browser/devtools/devtools_manager_impl.h" | 7 #include "content/browser/devtools/devtools_manager_impl.h" |
8 #include "content/browser/devtools/devtools_protocol.h" | 8 #include "content/browser/devtools/devtools_protocol.h" |
9 #include "content/browser/devtools/devtools_protocol_constants.h" | 9 #include "content/browser/devtools/devtools_protocol_constants.h" |
10 #include "content/browser/devtools/ipc_devtools_agent_host.h" | 10 #include "content/browser/devtools/ipc_devtools_agent_host.h" |
11 #include "content/browser/service_worker/service_worker_context_core.h" | |
12 #include "content/browser/service_worker/service_worker_version.h" | |
11 #include "content/browser/shared_worker/shared_worker_instance.h" | 13 #include "content/browser/shared_worker/shared_worker_instance.h" |
12 #include "content/common/devtools_messages.h" | 14 #include "content/common/devtools_messages.h" |
13 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
16 #include "content/public/browser/devtools_target.h" | |
14 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
15 #include "content/public/browser/worker_service.h" | 18 #include "content/public/browser/worker_service.h" |
16 #include "ipc/ipc_listener.h" | 19 #include "ipc/ipc_listener.h" |
17 | 20 |
18 namespace content { | 21 namespace content { |
19 | 22 |
20 namespace { | 23 namespace { |
21 | 24 |
22 bool SendMessageToWorker( | 25 bool SendMessageToWorker( |
23 const EmbeddedWorkerDevToolsManager::WorkerId& worker_id, | 26 const EmbeddedWorkerDevToolsManager::WorkerId& worker_id, |
24 IPC::Message* message) { | 27 IPC::Message* message) { |
25 RenderProcessHost* host = RenderProcessHost::FromID(worker_id.first); | 28 RenderProcessHost* host = RenderProcessHost::FromID(worker_id.first); |
26 if (!host) { | 29 if (!host) { |
27 delete message; | 30 delete message; |
28 return false; | 31 return false; |
29 } | 32 } |
30 message->set_routing_id(worker_id.second); | 33 message->set_routing_id(worker_id.second); |
31 host->Send(message); | 34 host->Send(message); |
32 return true; | 35 return true; |
33 } | 36 } |
34 | 37 |
38 void StatusNoOp(ServiceWorkerStatusCode status) { | |
39 } | |
40 | |
41 void CloseServiceWorkerOnIO(ServiceWorkerContextCore* context, | |
42 int64 version_id) { | |
43 if (ServiceWorkerVersion* version = context->GetLiveVersion(version_id)) | |
44 version->StopWorker(base::Bind(&StatusNoOp)); | |
45 } | |
46 | |
35 } // namespace | 47 } // namespace |
36 | 48 |
37 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier( | 49 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier( |
38 const ServiceWorkerContextCore* const service_worker_context, | 50 ServiceWorkerContextCore* const service_worker_context, |
39 int64 service_worker_version_id) | 51 int64 service_worker_version_id) |
40 : service_worker_context_(service_worker_context), | 52 : service_worker_context_(service_worker_context), |
41 service_worker_version_id_(service_worker_version_id) { | 53 service_worker_version_id_(service_worker_version_id) { |
42 } | 54 } |
43 | 55 |
44 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier( | 56 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier::ServiceWorkerIdentifier( |
45 const ServiceWorkerIdentifier& other) | 57 const ServiceWorkerIdentifier& other) |
46 : service_worker_context_(other.service_worker_context_), | 58 : service_worker_context_(other.service_worker_context_), |
47 service_worker_version_id_(other.service_worker_version_id_) { | 59 service_worker_version_id_(other.service_worker_version_id_) { |
48 } | 60 } |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 178 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
167 host->AddRoute(worker_id_.second, this); | 179 host->AddRoute(worker_id_.second, this); |
168 } | 180 } |
169 | 181 |
170 WorkerId worker_id_; | 182 WorkerId worker_id_; |
171 bool worker_attached_; | 183 bool worker_attached_; |
172 std::string state_; | 184 std::string state_; |
173 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerDevToolsAgentHost); | 185 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerDevToolsAgentHost); |
174 }; | 186 }; |
175 | 187 |
188 class EmbeddedWorkerDevToolsManager::ServiceWorkerTarget | |
189 : public DevToolsTarget { | |
190 public: | |
191 ServiceWorkerTarget( | |
192 EmbeddedWorkerDevToolsAgentHost* agent_host, const GURL& url) | |
193 : agent_host_(agent_host), | |
194 url_(url) { | |
195 } | |
196 | |
197 virtual ~ServiceWorkerTarget() { | |
198 } | |
199 | |
200 virtual std::string GetId() const OVERRIDE { | |
201 return agent_host_->GetId(); | |
202 } | |
203 | |
204 virtual std::string GetParentId() const OVERRIDE { | |
205 return ""; | |
206 } | |
207 | |
208 virtual std::string GetType() const OVERRIDE { | |
209 return "worker"; | |
210 } | |
211 | |
212 virtual std::string GetTitle() const OVERRIDE { | |
213 return "Service Worker"; | |
214 } | |
215 | |
216 virtual std::string GetDescription() const OVERRIDE { | |
217 return "Service Worker"; | |
218 } | |
219 | |
220 virtual GURL GetURL() const OVERRIDE { | |
221 return url_; | |
222 } | |
223 | |
224 virtual GURL GetFaviconURL() const OVERRIDE { | |
225 return GURL(); | |
226 } | |
227 | |
228 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE { | |
229 return base::TimeTicks(); | |
230 } | |
231 | |
232 virtual bool IsAttached() const OVERRIDE { | |
233 return agent_host_->IsAttached(); | |
234 } | |
235 | |
236 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE { | |
237 return agent_host_; | |
238 } | |
239 | |
240 virtual bool Activate() const OVERRIDE { | |
241 return false; | |
242 } | |
243 | |
244 virtual bool Close() const OVERRIDE { | |
245 return EmbeddedWorkerDevToolsManager::GetInstance()->CloseServiceWorker( | |
246 agent_host_->worker_id()); | |
247 } | |
248 | |
249 private: | |
250 EmbeddedWorkerDevToolsAgentHost* agent_host_; | |
251 GURL url_; | |
252 }; | |
253 | |
176 // static | 254 // static |
177 EmbeddedWorkerDevToolsManager* EmbeddedWorkerDevToolsManager::GetInstance() { | 255 EmbeddedWorkerDevToolsManager* EmbeddedWorkerDevToolsManager::GetInstance() { |
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 256 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
179 return Singleton<EmbeddedWorkerDevToolsManager>::get(); | 257 return Singleton<EmbeddedWorkerDevToolsManager>::get(); |
180 } | 258 } |
181 | 259 |
182 DevToolsAgentHost* EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker( | 260 DevToolsAgentHost* |
261 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForWorker( | |
262 int worker_process_id, | |
263 int worker_route_id) { | |
264 return GetOrCreateAgentHost(worker_process_id, worker_route_id); | |
265 } | |
266 | |
267 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsAgentHost* | |
268 EmbeddedWorkerDevToolsManager::GetOrCreateAgentHost( | |
183 int worker_process_id, | 269 int worker_process_id, |
184 int worker_route_id) { | 270 int worker_route_id) { |
185 WorkerId id(worker_process_id, worker_route_id); | 271 WorkerId id(worker_process_id, worker_route_id); |
186 | 272 |
187 WorkerInfoMap::iterator it = workers_.find(id); | 273 WorkerInfoMap::iterator it = workers_.find(id); |
188 if (it == workers_.end()) | 274 if (it == workers_.end()) |
189 return NULL; | 275 return NULL; |
190 | 276 |
191 WorkerInfo* info = it->second; | 277 WorkerInfo* info = it->second; |
192 if (info->state() != WORKER_UNINSPECTED && | 278 if (info->state() != WORKER_UNINSPECTED && |
(...skipping 10 matching lines...) Expand all Loading... | |
203 | 289 |
204 DevToolsAgentHost* | 290 DevToolsAgentHost* |
205 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForServiceWorker( | 291 EmbeddedWorkerDevToolsManager::GetDevToolsAgentHostForServiceWorker( |
206 const ServiceWorkerIdentifier& service_worker_id) { | 292 const ServiceWorkerIdentifier& service_worker_id) { |
207 WorkerInfoMap::iterator it = FindExistingServiceWorkerInfo(service_worker_id); | 293 WorkerInfoMap::iterator it = FindExistingServiceWorkerInfo(service_worker_id); |
208 if (it == workers_.end()) | 294 if (it == workers_.end()) |
209 return NULL; | 295 return NULL; |
210 return GetDevToolsAgentHostForWorker(it->first.first, it->first.second); | 296 return GetDevToolsAgentHostForWorker(it->first.first, it->first.second); |
211 } | 297 } |
212 | 298 |
299 void EmbeddedWorkerDevToolsManager::GetAllServiceWorkerTargets( | |
300 const TargetCallback& callback) { | |
301 std::map<WorkerId, ServiceWorkerIdentifier> ids; | |
302 for (WorkerInfoMap::iterator it = workers_.begin(); | |
303 it != workers_.end(); ++it) { | |
304 if (const ServiceWorkerIdentifier* worker_id = | |
305 it->second->service_worker_id()) { | |
306 ids.insert(std::make_pair(it->first, *worker_id)); | |
307 } | |
308 } | |
309 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
310 base::Bind(&EmbeddedWorkerDevToolsManager::GetServiceWorkerUrlsOnIO, | |
311 ids, callback)); | |
312 } | |
313 | |
314 // static | |
315 void EmbeddedWorkerDevToolsManager::GetServiceWorkerUrlsOnIO( | |
316 const std::map<WorkerId, ServiceWorkerIdentifier>& ids, | |
317 const TargetCallback& callback) { | |
318 std::map<WorkerId, GURL> url_map; | |
319 for (std::map<WorkerId, ServiceWorkerIdentifier>::const_iterator it = | |
320 ids.begin(); it != ids.end(); ++it) { | |
321 ServiceWorkerVersion* version = it->second.service_worker_context_-> | |
horo
2014/07/08 08:14:31
ServiceWorkerIdentifier's service_worker_context_
vkuzkokov
2014/07/08 09:38:48
ForEachStoragePartition requires passing instance
horo
2014/07/09 02:30:44
Humm..
Doesn't DevToolsHttpHandler have a way to g
| |
322 GetLiveVersion(it->second.service_worker_version_id_); | |
323 if (version) | |
324 url_map[it->first] = version->script_url(); | |
325 } | |
326 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
327 base::Bind(&EmbeddedWorkerDevToolsManager::CreateTargets, | |
328 url_map, callback)); | |
329 } | |
330 | |
331 // static | |
332 void EmbeddedWorkerDevToolsManager::CreateTargets( | |
333 const std::map<WorkerId, GURL> url_map, | |
334 const TargetCallback& callback) { | |
335 std::vector<DevToolsTarget*> targets; | |
336 EmbeddedWorkerDevToolsManager* instance = GetInstance(); | |
337 for (std::map<WorkerId, GURL>::const_iterator it = url_map.begin(); | |
338 it != url_map.end(); ++it) { | |
339 if (EmbeddedWorkerDevToolsAgentHost* agent_host = | |
340 instance->GetOrCreateAgentHost(it->first.first, it->first.second)) { | |
341 targets.push_back(new ServiceWorkerTarget(agent_host, it->second)); | |
342 } | |
343 } | |
344 callback.Run(targets); | |
345 } | |
346 | |
213 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager() | 347 EmbeddedWorkerDevToolsManager::EmbeddedWorkerDevToolsManager() |
214 : debug_service_worker_on_start_(false) { | 348 : debug_service_worker_on_start_(false) { |
215 } | 349 } |
216 | 350 |
217 EmbeddedWorkerDevToolsManager::~EmbeddedWorkerDevToolsManager() { | 351 EmbeddedWorkerDevToolsManager::~EmbeddedWorkerDevToolsManager() { |
218 } | 352 } |
219 | 353 |
220 bool EmbeddedWorkerDevToolsManager::SharedWorkerCreated( | 354 bool EmbeddedWorkerDevToolsManager::SharedWorkerCreated( |
221 int worker_process_id, | 355 int worker_process_id, |
222 int worker_route_id, | 356 int worker_route_id, |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
365 | 499 |
366 void EmbeddedWorkerDevToolsManager::MoveToPausedState( | 500 void EmbeddedWorkerDevToolsManager::MoveToPausedState( |
367 const WorkerId& id, | 501 const WorkerId& id, |
368 const WorkerInfoMap::iterator& it) { | 502 const WorkerInfoMap::iterator& it) { |
369 DCHECK_EQ(WORKER_TERMINATED, it->second->state()); | 503 DCHECK_EQ(WORKER_TERMINATED, it->second->state()); |
370 scoped_ptr<WorkerInfo> info = workers_.take_and_erase(it); | 504 scoped_ptr<WorkerInfo> info = workers_.take_and_erase(it); |
371 info->set_state(WORKER_PAUSED_FOR_REATTACH); | 505 info->set_state(WORKER_PAUSED_FOR_REATTACH); |
372 workers_.set(id, info.Pass()); | 506 workers_.set(id, info.Pass()); |
373 } | 507 } |
374 | 508 |
509 bool EmbeddedWorkerDevToolsManager::CloseServiceWorker(const WorkerId& id) { | |
510 WorkerInfoMap::iterator it = workers_.find(id); | |
511 if (it == workers_.end()) | |
512 return false; | |
513 ServiceWorkerIdentifier* service_worker = it->second->service_worker_id(); | |
514 if (!service_worker) | |
515 return false; | |
516 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
517 base::Bind(&CloseServiceWorkerOnIO, | |
518 service_worker->service_worker_context_, | |
519 service_worker->service_worker_version_id_)); | |
520 return true; | |
521 } | |
522 | |
375 void EmbeddedWorkerDevToolsManager::ResetForTesting() { | 523 void EmbeddedWorkerDevToolsManager::ResetForTesting() { |
376 workers_.clear(); | 524 workers_.clear(); |
377 } | 525 } |
378 | 526 |
379 } // namespace content | 527 } // namespace content |
OLD | NEW |