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/service_worker/service_worker_internals_ui.h" | 5 #include "content/browser/service_worker/service_worker_internals_ui.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "content/browser/devtools/devtools_manager_impl.h" |
| 15 #include "content/browser/devtools/embedded_worker_devtools_manager.h" |
14 #include "content/browser/service_worker/service_worker_context_observer.h" | 16 #include "content/browser/service_worker/service_worker_context_observer.h" |
15 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
16 #include "content/browser/service_worker/service_worker_registration.h" | 18 #include "content/browser/service_worker/service_worker_registration.h" |
17 #include "content/browser/service_worker/service_worker_version.h" | 19 #include "content/browser/service_worker/service_worker_version.h" |
18 #include "content/public/browser/browser_context.h" | 20 #include "content/public/browser/browser_context.h" |
19 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/storage_partition.h" | 22 #include "content/public/browser/storage_partition.h" |
21 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
22 #include "content/public/browser/web_ui.h" | 24 #include "content/public/browser/web_ui.h" |
23 #include "content/public/browser/web_ui_data_source.h" | 25 #include "content/public/browser/web_ui_data_source.h" |
(...skipping 24 matching lines...) Expand all Loading... |
48 const base::FilePath& context_path); | 50 const base::FilePath& context_path); |
49 void UnregisterOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, | 51 void UnregisterOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, |
50 const GURL& scope); | 52 const GURL& scope); |
51 void StartWorkerOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, | 53 void StartWorkerOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, |
52 const GURL& scope); | 54 const GURL& scope); |
53 void StopWorkerOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, | 55 void StopWorkerOnIOThread(scoped_refptr<ServiceWorkerContextWrapper> context, |
54 const GURL& scope); | 56 const GURL& scope); |
55 void DispatchSyncEventToWorkerOnIOThread( | 57 void DispatchSyncEventToWorkerOnIOThread( |
56 scoped_refptr<ServiceWorkerContextWrapper> context, | 58 scoped_refptr<ServiceWorkerContextWrapper> context, |
57 const GURL& scope); | 59 const GURL& scope); |
| 60 void InspectWorkerOnIOThread( |
| 61 scoped_refptr<ServiceWorkerContextWrapper> context, |
| 62 const GURL& scope); |
58 | 63 |
59 private: | 64 private: |
60 friend class base::RefCountedThreadSafe<OperationProxy>; | 65 friend class base::RefCountedThreadSafe<OperationProxy>; |
61 ~OperationProxy() {} | 66 ~OperationProxy() {} |
62 void OnHaveRegistrations( | 67 void OnHaveRegistrations( |
63 int partition_id, | 68 int partition_id, |
64 const base::FilePath& context_path, | 69 const base::FilePath& context_path, |
65 const std::vector<ServiceWorkerRegistrationInfo>& registrations); | 70 const std::vector<ServiceWorkerRegistrationInfo>& registrations); |
66 | 71 |
67 void OperationComplete(ServiceWorkerStatusCode status); | 72 void OperationComplete(ServiceWorkerStatusCode status); |
68 | 73 |
69 void StartActiveWorker( | 74 void StartActiveWorker( |
70 ServiceWorkerStatusCode status, | 75 ServiceWorkerStatusCode status, |
71 const scoped_refptr<ServiceWorkerRegistration>& registration); | 76 const scoped_refptr<ServiceWorkerRegistration>& registration); |
72 | 77 |
73 void StopActiveWorker( | 78 void StopActiveWorker( |
74 ServiceWorkerStatusCode status, | 79 ServiceWorkerStatusCode status, |
75 const scoped_refptr<ServiceWorkerRegistration>& registration); | 80 const scoped_refptr<ServiceWorkerRegistration>& registration); |
76 | 81 |
77 void DispatchSyncEventToActiveWorker( | 82 void DispatchSyncEventToActiveWorker( |
78 ServiceWorkerStatusCode status, | 83 ServiceWorkerStatusCode status, |
79 const scoped_refptr<ServiceWorkerRegistration>& registration); | 84 const scoped_refptr<ServiceWorkerRegistration>& registration); |
80 | 85 |
| 86 void InspectActiveWorker( |
| 87 const ServiceWorkerContextCore* const service_worker_context, |
| 88 ServiceWorkerStatusCode status, |
| 89 const scoped_refptr<ServiceWorkerRegistration>& registration); |
| 90 |
| 91 void InspectWorkerOnUIThread( |
| 92 const ServiceWorkerContextCore* const service_worker_context, |
| 93 int64 version_id); |
| 94 |
81 WeakPtr<ServiceWorkerInternalsUI> internals_; | 95 WeakPtr<ServiceWorkerInternalsUI> internals_; |
82 scoped_ptr<ListValue> original_args_; | 96 scoped_ptr<ListValue> original_args_; |
83 }; | 97 }; |
84 | 98 |
85 class ServiceWorkerInternalsUI::PartitionObserver | 99 class ServiceWorkerInternalsUI::PartitionObserver |
86 : public ServiceWorkerContextObserver { | 100 : public ServiceWorkerContextObserver { |
87 public: | 101 public: |
88 PartitionObserver(int partition_id, WebUI* web_ui) | 102 PartitionObserver(int partition_id, WebUI* web_ui) |
89 : partition_id_(partition_id), web_ui_(web_ui) {} | 103 : partition_id_(partition_id), web_ui_(web_ui) {} |
90 virtual ~PartitionObserver() {} | 104 virtual ~PartitionObserver() {} |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 base::Bind(&ServiceWorkerInternalsUI::StopWorker, | 216 base::Bind(&ServiceWorkerInternalsUI::StopWorker, |
203 base::Unretained(this))); | 217 base::Unretained(this))); |
204 web_ui->RegisterMessageCallback( | 218 web_ui->RegisterMessageCallback( |
205 "unregister", | 219 "unregister", |
206 base::Bind(&ServiceWorkerInternalsUI::Unregister, | 220 base::Bind(&ServiceWorkerInternalsUI::Unregister, |
207 base::Unretained(this))); | 221 base::Unretained(this))); |
208 web_ui->RegisterMessageCallback( | 222 web_ui->RegisterMessageCallback( |
209 "sync", | 223 "sync", |
210 base::Bind(&ServiceWorkerInternalsUI::DispatchSyncEventToWorker, | 224 base::Bind(&ServiceWorkerInternalsUI::DispatchSyncEventToWorker, |
211 base::Unretained(this))); | 225 base::Unretained(this))); |
| 226 web_ui->RegisterMessageCallback( |
| 227 "inspect", |
| 228 base::Bind(&ServiceWorkerInternalsUI::InspectWorker, |
| 229 base::Unretained(this))); |
212 } | 230 } |
213 | 231 |
214 ServiceWorkerInternalsUI::~ServiceWorkerInternalsUI() { | 232 ServiceWorkerInternalsUI::~ServiceWorkerInternalsUI() { |
215 BrowserContext* browser_context = | 233 BrowserContext* browser_context = |
216 web_ui()->GetWebContents()->GetBrowserContext(); | 234 web_ui()->GetWebContents()->GetBrowserContext(); |
217 // Safe to use base::Unretained(this) because | 235 // Safe to use base::Unretained(this) because |
218 // ForEachStoragePartition is synchronous. | 236 // ForEachStoragePartition is synchronous. |
219 BrowserContext::StoragePartitionCallback remove_observer_cb = | 237 BrowserContext::StoragePartitionCallback remove_observer_cb = |
220 base::Bind(&ServiceWorkerInternalsUI::RemoveObserverFromStoragePartition, | 238 base::Bind(&ServiceWorkerInternalsUI::RemoveObserverFromStoragePartition, |
221 base::Unretained(this)); | 239 base::Unretained(this)); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 BrowserThread::PostTask( | 348 BrowserThread::PostTask( |
331 BrowserThread::IO, | 349 BrowserThread::IO, |
332 FROM_HERE, | 350 FROM_HERE, |
333 base::Bind(&ServiceWorkerInternalsUI::OperationProxy:: | 351 base::Bind(&ServiceWorkerInternalsUI::OperationProxy:: |
334 DispatchSyncEventToWorkerOnIOThread, | 352 DispatchSyncEventToWorkerOnIOThread, |
335 new OperationProxy(AsWeakPtr(), args_copy.Pass()), | 353 new OperationProxy(AsWeakPtr(), args_copy.Pass()), |
336 context, | 354 context, |
337 scope)); | 355 scope)); |
338 } | 356 } |
339 | 357 |
| 358 void ServiceWorkerInternalsUI::InspectWorker(const ListValue* args) { |
| 359 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 360 base::FilePath partition_path; |
| 361 GURL scope; |
| 362 scoped_refptr<ServiceWorkerContextWrapper> context; |
| 363 if (!GetRegistrationInfo(args, &partition_path, &scope, &context)) |
| 364 return; |
| 365 scoped_ptr<ListValue> args_copy(args->DeepCopy()); |
| 366 BrowserThread::PostTask( |
| 367 BrowserThread::IO, |
| 368 FROM_HERE, |
| 369 base::Bind( |
| 370 &ServiceWorkerInternalsUI::OperationProxy::InspectWorkerOnIOThread, |
| 371 new OperationProxy(AsWeakPtr(), args_copy.Pass()), |
| 372 context, |
| 373 scope)); |
| 374 } |
| 375 |
340 void ServiceWorkerInternalsUI::Unregister(const ListValue* args) { | 376 void ServiceWorkerInternalsUI::Unregister(const ListValue* args) { |
341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 377 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
342 base::FilePath partition_path; | 378 base::FilePath partition_path; |
343 GURL scope; | 379 GURL scope; |
344 scoped_refptr<ServiceWorkerContextWrapper> context; | 380 scoped_refptr<ServiceWorkerContextWrapper> context; |
345 if (!GetRegistrationInfo(args, &partition_path, &scope, &context)) | 381 if (!GetRegistrationInfo(args, &partition_path, &scope, &context)) |
346 return; | 382 return; |
347 | 383 |
348 scoped_ptr<ListValue> args_copy(args->DeepCopy()); | 384 scoped_ptr<ListValue> args_copy(args->DeepCopy()); |
349 BrowserThread::PostTask( | 385 BrowserThread::PostTask( |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 scoped_refptr<ServiceWorkerContextWrapper> context, | 482 scoped_refptr<ServiceWorkerContextWrapper> context, |
447 const GURL& scope) { | 483 const GURL& scope) { |
448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
449 context->context()->storage()->FindRegistrationForPattern( | 485 context->context()->storage()->FindRegistrationForPattern( |
450 scope, | 486 scope, |
451 base::Bind(&ServiceWorkerInternalsUI::OperationProxy:: | 487 base::Bind(&ServiceWorkerInternalsUI::OperationProxy:: |
452 DispatchSyncEventToActiveWorker, | 488 DispatchSyncEventToActiveWorker, |
453 this)); | 489 this)); |
454 } | 490 } |
455 | 491 |
| 492 void ServiceWorkerInternalsUI::OperationProxy::InspectWorkerOnIOThread( |
| 493 scoped_refptr<ServiceWorkerContextWrapper> context, |
| 494 const GURL& scope) { |
| 495 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 496 context->context()->storage()->FindRegistrationForPattern( |
| 497 scope, |
| 498 base::Bind(&ServiceWorkerInternalsUI::OperationProxy::InspectActiveWorker, |
| 499 this, |
| 500 context->context())); |
| 501 } |
| 502 |
456 namespace { | 503 namespace { |
457 void UpdateVersionInfo(const ServiceWorkerVersionInfo& version, | 504 void UpdateVersionInfo(const ServiceWorkerVersionInfo& version, |
458 DictionaryValue* info) { | 505 DictionaryValue* info) { |
459 switch (version.running_status) { | 506 switch (version.running_status) { |
460 case ServiceWorkerVersion::STOPPED: | 507 case ServiceWorkerVersion::STOPPED: |
461 info->SetString("running_status", "STOPPED"); | 508 info->SetString("running_status", "STOPPED"); |
462 break; | 509 break; |
463 case ServiceWorkerVersion::STARTING: | 510 case ServiceWorkerVersion::STARTING: |
464 info->SetString("running_status", "STARTING"); | 511 info->SetString("running_status", "STARTING"); |
465 break; | 512 break; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 registration->active_version()->status() == | 650 registration->active_version()->status() == |
604 ServiceWorkerVersion::ACTIVE) { | 651 ServiceWorkerVersion::ACTIVE) { |
605 registration->active_version()->DispatchSyncEvent(base::Bind( | 652 registration->active_version()->DispatchSyncEvent(base::Bind( |
606 &ServiceWorkerInternalsUI::OperationProxy::OperationComplete, this)); | 653 &ServiceWorkerInternalsUI::OperationProxy::OperationComplete, this)); |
607 return; | 654 return; |
608 } | 655 } |
609 | 656 |
610 OperationComplete(SERVICE_WORKER_ERROR_FAILED); | 657 OperationComplete(SERVICE_WORKER_ERROR_FAILED); |
611 } | 658 } |
612 | 659 |
| 660 void ServiceWorkerInternalsUI::OperationProxy::InspectActiveWorker( |
| 661 const ServiceWorkerContextCore* const service_worker_context, |
| 662 ServiceWorkerStatusCode status, |
| 663 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 664 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 665 if (status == SERVICE_WORKER_OK) { |
| 666 BrowserThread::PostTask( |
| 667 BrowserThread::UI, |
| 668 FROM_HERE, |
| 669 base::Bind(&OperationProxy::InspectWorkerOnUIThread, |
| 670 this, |
| 671 service_worker_context, |
| 672 registration->active_version()->version_id())); |
| 673 return; |
| 674 } |
| 675 |
| 676 OperationComplete(status); |
| 677 } |
| 678 |
| 679 void ServiceWorkerInternalsUI::OperationProxy::InspectWorkerOnUIThread( |
| 680 const ServiceWorkerContextCore* const service_worker_context, |
| 681 int64 version_id) { |
| 682 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 683 scoped_refptr<DevToolsAgentHost> agent_host( |
| 684 EmbeddedWorkerDevToolsManager::GetInstance() |
| 685 ->GetDevToolsAgentHostForServiceWorker( |
| 686 EmbeddedWorkerDevToolsManager::ServiceWorkerIdentifier( |
| 687 service_worker_context, version_id))); |
| 688 if (agent_host) { |
| 689 DevToolsManagerImpl::GetInstance()->Inspect( |
| 690 internals_->web_ui()->GetWebContents()->GetBrowserContext(), |
| 691 agent_host.get()); |
| 692 OperationComplete(SERVICE_WORKER_OK); |
| 693 return; |
| 694 } |
| 695 OperationComplete(SERVICE_WORKER_ERROR_NOT_FOUND); |
| 696 } |
| 697 |
613 } // namespace content | 698 } // namespace content |
OLD | NEW |