OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // MediaFileSystemRegistry implementation. | 5 // MediaFileSystemRegistry implementation. |
6 | 6 |
7 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 7 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "chrome/common/extensions/extension_set.h" | 30 #include "chrome/common/extensions/extension_set.h" |
31 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
32 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
33 #include "content/public/browser/notification_details.h" | 33 #include "content/public/browser/notification_details.h" |
34 #include "content/public/browser/notification_observer.h" | 34 #include "content/public/browser/notification_observer.h" |
35 #include "content/public/browser/notification_registrar.h" | 35 #include "content/public/browser/notification_registrar.h" |
36 #include "content/public/browser/notification_source.h" | 36 #include "content/public/browser/notification_source.h" |
37 #include "content/public/browser/notification_types.h" | 37 #include "content/public/browser/notification_types.h" |
38 #include "content/public/browser/render_process_host.h" | 38 #include "content/public/browser/render_process_host.h" |
39 #include "content/public/browser/render_view_host.h" | 39 #include "content/public/browser/render_view_host.h" |
| 40 #include "content/public/browser/render_view_host_observer.h" |
40 #include "content/public/browser/web_contents.h" | 41 #include "content/public/browser/web_contents.h" |
41 #include "webkit/fileapi/file_system_types.h" | 42 #include "webkit/fileapi/file_system_types.h" |
42 #include "webkit/fileapi/isolated_context.h" | 43 #include "webkit/fileapi/isolated_context.h" |
43 | 44 |
44 using content::BrowserThread; | 45 using content::BrowserThread; |
45 using content::NavigationController; | 46 using content::NavigationController; |
46 using content::RenderProcessHost; | 47 using content::RenderProcessHost; |
47 using content::WebContents; | 48 using content::WebContents; |
48 using fileapi::IsolatedContext; | 49 using fileapi::IsolatedContext; |
49 | 50 |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 // host object. | 403 // host object. |
403 MediaDeviceEntryReferencesMap media_device_map_references_; | 404 MediaDeviceEntryReferencesMap media_device_map_references_; |
404 | 405 |
405 // The set of render processes and web contents that may have references to | 406 // The set of render processes and web contents that may have references to |
406 // the file system ids this instance manages. | 407 // the file system ids this instance manages. |
407 RPHReferenceManager rph_refs_; | 408 RPHReferenceManager rph_refs_; |
408 | 409 |
409 DISALLOW_COPY_AND_ASSIGN(ExtensionGalleriesHost); | 410 DISALLOW_COPY_AND_ASSIGN(ExtensionGalleriesHost); |
410 }; | 411 }; |
411 | 412 |
| 413 // This self-deleting transaction object contains the state needed to |
| 414 // asynchronously initialize the storage monitor if needed when an |
| 415 // extension calles |GetMediaFileSystemsForExtension|. Specifically, |
| 416 // it tracks if the RenderViewHost with which it was initialized was |
| 417 // destroyed, and if so, it never calls the callback. |
| 418 // When it receives the OnStorageMonitorInitialized callback, it deletes itself. |
| 419 class GetPreferencesTransaction : public content::RenderViewHostObserver { |
| 420 public: |
| 421 GetPreferencesTransaction( |
| 422 content::RenderViewHost* rvh, |
| 423 base::Closure callback) |
| 424 : content::RenderViewHostObserver(rvh), |
| 425 callback_(callback) {} |
| 426 virtual ~GetPreferencesTransaction() {} |
| 427 |
| 428 virtual void RenderViewHostDestroyed(content::RenderViewHost* rvh) OVERRIDE { |
| 429 callback_.Reset(); |
| 430 } |
| 431 |
| 432 void OnStorageMonitorInitialized() { |
| 433 if (!callback_.is_null()) |
| 434 callback_.Run(); |
| 435 |
| 436 delete this; |
| 437 } |
| 438 |
| 439 private: |
| 440 base::Closure callback_; |
| 441 }; |
| 442 |
412 /****************** | 443 /****************** |
413 * Public methods | 444 * Public methods |
414 ******************/ | 445 ******************/ |
415 | 446 |
416 void MediaFileSystemRegistry::GetMediaFileSystemsForExtension( | 447 void MediaFileSystemRegistry::GetMediaFileSystemsForExtension( |
417 const content::RenderViewHost* rvh, | 448 content::RenderViewHost* rvh, |
418 const extensions::Extension* extension, | 449 const extensions::Extension* extension, |
419 const MediaFileSystemsCallback& callback) { | 450 const MediaFileSystemsCallback& callback) { |
420 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 451 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
421 | 452 |
422 Profile* profile = | 453 // This object is self-deleting when it receives the callback |
423 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); | 454 // from StorageMonitor::Initialize. |
| 455 GetPreferencesTransaction* transaction = new GetPreferencesTransaction( |
| 456 rvh, |
| 457 base::Bind( |
| 458 &MediaFileSystemRegistry::GetMediaFileSystemsPostInit, |
| 459 weak_ptr_factory_.GetWeakPtr(), |
| 460 rvh, extension, callback)); |
| 461 StorageMonitor* monitor = StorageMonitor::GetInstance(); |
| 462 if (monitor) { |
| 463 monitor->Initialize(base::Bind( |
| 464 &GetPreferencesTransaction::OnStorageMonitorInitialized, |
| 465 base::Unretained(transaction))); |
| 466 } |
| 467 } |
| 468 |
| 469 void MediaFileSystemRegistry::GetMediaFileSystemsPostInit( |
| 470 const content::RenderViewHost* rvh, |
| 471 const extensions::Extension* extension, |
| 472 const MediaFileSystemsCallback& callback) { |
| 473 Profile* profile = Profile::FromBrowserContext( |
| 474 rvh->GetProcess()->GetBrowserContext()); |
424 MediaGalleriesPreferences* preferences = GetPreferences(profile); | 475 MediaGalleriesPreferences* preferences = GetPreferences(profile); |
425 MediaGalleryPrefIdSet galleries = | 476 MediaGalleryPrefIdSet galleries = |
426 preferences->GalleriesForExtension(*extension); | 477 preferences->GalleriesForExtension(*extension); |
427 | 478 |
428 if (galleries.empty()) { | 479 if (galleries.empty()) { |
429 callback.Run(std::vector<MediaFileSystemInfo>()); | 480 callback.Run(std::vector<MediaFileSystemInfo>()); |
430 return; | 481 return; |
431 } | 482 } |
432 | 483 |
433 if (!ContainsKey(pref_change_registrar_map_, profile)) { | 484 if (!ContainsKey(pref_change_registrar_map_, profile)) { |
(...skipping 26 matching lines...) Expand all Loading... |
460 Profile* profile) { | 511 Profile* profile) { |
461 MediaGalleriesPreferences* preferences = | 512 MediaGalleriesPreferences* preferences = |
462 MediaGalleriesPreferencesFactory::GetForProfile(profile); | 513 MediaGalleriesPreferencesFactory::GetForProfile(profile); |
463 if (ContainsKey(extension_hosts_map_, profile)) | 514 if (ContainsKey(extension_hosts_map_, profile)) |
464 return preferences; | 515 return preferences; |
465 | 516 |
466 // Create an empty entry so the initialization code below only gets called | 517 // Create an empty entry so the initialization code below only gets called |
467 // once per profile. | 518 // once per profile. |
468 extension_hosts_map_[profile] = ExtensionHostMap(); | 519 extension_hosts_map_[profile] = ExtensionHostMap(); |
469 | 520 |
470 // TODO(gbillock): Move this stanza to MediaGalleriesPreferences init code. | |
471 // StorageMonitor may be NULL in unit tests. | 521 // StorageMonitor may be NULL in unit tests. |
| 522 // TODO(gbillock): Make sure all tests have a storage monitor and remove this. |
472 StorageMonitor* monitor = StorageMonitor::GetInstance(); | 523 StorageMonitor* monitor = StorageMonitor::GetInstance(); |
473 if (!monitor) | 524 if (!monitor) |
474 return preferences; | 525 return preferences; |
| 526 DCHECK(monitor->IsInitialized()); |
475 std::vector<StorageInfo> existing_devices = monitor->GetAttachedStorage(); | 527 std::vector<StorageInfo> existing_devices = monitor->GetAttachedStorage(); |
476 for (size_t i = 0; i < existing_devices.size(); i++) { | 528 for (size_t i = 0; i < existing_devices.size(); i++) { |
477 if (!StorageInfo::IsMediaDevice(existing_devices[i].device_id)) | 529 if (!StorageInfo::IsMediaDevice(existing_devices[i].device_id)) |
478 continue; | 530 continue; |
479 if (!existing_devices[i].name.empty()) { | 531 if (!existing_devices[i].name.empty()) { |
480 preferences->AddGalleryWithName(existing_devices[i].device_id, | 532 preferences->AddGalleryWithName(existing_devices[i].device_id, |
481 existing_devices[i].name, | 533 existing_devices[i].name, |
482 base::FilePath(), | 534 base::FilePath(), |
483 false /*not user added*/); | 535 false /*not user added*/); |
484 } else { | 536 } else { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 return registry_; | 661 return registry_; |
610 } | 662 } |
611 | 663 |
612 private: | 664 private: |
613 MediaFileSystemRegistry* registry_; | 665 MediaFileSystemRegistry* registry_; |
614 | 666 |
615 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); | 667 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); |
616 }; | 668 }; |
617 | 669 |
618 MediaFileSystemRegistry::MediaFileSystemRegistry() | 670 MediaFileSystemRegistry::MediaFileSystemRegistry() |
619 : file_system_context_(new MediaFileSystemContextImpl(this)) { | 671 : file_system_context_(new MediaFileSystemContextImpl(this)), |
| 672 weak_ptr_factory_(this) { |
620 // StorageMonitor may be NULL in unit tests. | 673 // StorageMonitor may be NULL in unit tests. |
621 StorageMonitor* monitor = StorageMonitor::GetInstance(); | 674 StorageMonitor* monitor = StorageMonitor::GetInstance(); |
622 if (monitor) | 675 if (monitor) |
623 monitor->AddObserver(this); | 676 monitor->AddObserver(this); |
624 } | 677 } |
625 | 678 |
626 MediaFileSystemRegistry::~MediaFileSystemRegistry() { | 679 MediaFileSystemRegistry::~MediaFileSystemRegistry() { |
627 // StorageMonitor may be NULL in unit tests. | 680 // StorageMonitor may be NULL in unit tests. |
628 StorageMonitor* monitor = StorageMonitor::GetInstance(); | 681 StorageMonitor* monitor = StorageMonitor::GetInstance(); |
629 if (monitor) | 682 if (monitor) |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
720 // previously used. | 773 // previously used. |
721 PrefChangeRegistrarMap::iterator pref_it = | 774 PrefChangeRegistrarMap::iterator pref_it = |
722 pref_change_registrar_map_.find(profile); | 775 pref_change_registrar_map_.find(profile); |
723 DCHECK(pref_it != pref_change_registrar_map_.end()); | 776 DCHECK(pref_it != pref_change_registrar_map_.end()); |
724 delete pref_it->second; | 777 delete pref_it->second; |
725 pref_change_registrar_map_.erase(pref_it); | 778 pref_change_registrar_map_.erase(pref_it); |
726 } | 779 } |
727 } | 780 } |
728 | 781 |
729 } // namespace chrome | 782 } // namespace chrome |
OLD | NEW |