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_gallery/media_file_system_registry.h" | 7 #include "chrome/browser/media_gallery/media_file_system_registry.h" |
8 | 8 |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 } | 473 } |
474 | 474 |
475 void MediaFileSystemRegistry::GetMediaFileSystemsForExtension( | 475 void MediaFileSystemRegistry::GetMediaFileSystemsForExtension( |
476 const content::RenderViewHost* rvh, | 476 const content::RenderViewHost* rvh, |
477 const extensions::Extension* extension, | 477 const extensions::Extension* extension, |
478 const MediaFileSystemsCallback& callback) { | 478 const MediaFileSystemsCallback& callback) { |
479 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 479 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
480 | 480 |
481 Profile* profile = | 481 Profile* profile = |
482 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); | 482 Profile::FromBrowserContext(rvh->GetProcess()->GetBrowserContext()); |
483 MediaGalleriesPreferences* preferences = | 483 MediaGalleriesPreferences* preferences = GetProfilePreferences(profile); |
484 MediaGalleriesPreferencesFactory::GetForProfile(profile); | |
485 | |
486 if (!ContainsKey(extension_hosts_map_, profile)) | |
487 AddAttachedMediaDeviceGalleries(preferences); | |
488 MediaGalleryPrefIdSet galleries = | 484 MediaGalleryPrefIdSet galleries = |
489 preferences->GalleriesForExtension(*extension); | 485 preferences->GalleriesForExtension(*extension); |
490 | 486 |
491 // If the extension has no galleries and it didn't have any last time, just | 487 // If the extension has no galleries and it didn't have any last time, just |
492 // return the empty list. The second check is needed because of | 488 // return the empty list. The second check is needed because of |
493 // http://crbug.com/145855. | 489 // http://crbug.com/145855. |
494 bool has_extension_host = ContainsKey(extension_hosts_map_, profile) && | 490 bool has_extension_host = ContainsKey(extension_hosts_map_, profile) && |
495 ContainsKey(extension_hosts_map_[profile], extension->id()); | 491 ContainsKey(extension_hosts_map_[profile], extension->id()); |
496 if (galleries.empty() && !has_extension_host) { | 492 if (galleries.empty() && !has_extension_host) { |
497 callback.Run(std::vector<MediaFileSystemInfo>()); | 493 callback.Run(std::vector<MediaFileSystemInfo>()); |
498 return; | 494 return; |
499 } | 495 } |
500 | 496 |
501 ExtensionGalleriesHost* extension_host = | 497 ExtensionGalleriesHost* extension_host = |
502 extension_hosts_map_[profile][extension->id()].get(); | 498 extension_hosts_map_[profile][extension->id()].get(); |
503 if (!extension_host) { | 499 if (!extension_host) { |
504 extension_host = new ExtensionGalleriesHost( | 500 extension_host = new ExtensionGalleriesHost( |
505 file_system_context_.get(), | 501 file_system_context_.get(), |
506 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty, | 502 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty, |
507 base::Unretained(this), profile, extension->id())); | 503 base::Unretained(this), profile, extension->id())); |
508 extension_hosts_map_[profile][extension->id()] = extension_host; | 504 extension_hosts_map_[profile][extension->id()] = extension_host; |
509 } | 505 } |
510 extension_host->ReferenceFromRVH(rvh); | 506 extension_host->ReferenceFromRVH(rvh); |
511 | 507 |
512 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(), | 508 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(), |
513 callback); | 509 callback); |
514 } | 510 } |
515 | 511 |
| 512 MediaGalleriesPreferences* MediaFileSystemRegistry::GetProfilePreferences( |
| 513 Profile* profile) { |
| 514 MediaGalleriesPreferences* preferences = |
| 515 MediaGalleriesPreferencesFactory::GetForProfile(profile); |
| 516 if (ContainsKey(extension_hosts_map_, profile)) |
| 517 return preferences; |
| 518 |
| 519 // SystemMonitor may be NULL in unit tests. |
| 520 SystemMonitor* system_monitor = SystemMonitor::Get(); |
| 521 if (!system_monitor) |
| 522 return preferences; |
| 523 std::vector<SystemMonitor::RemovableStorageInfo> existing_devices = |
| 524 system_monitor->GetAttachedRemovableStorage(); |
| 525 for (size_t i = 0; i < existing_devices.size(); i++) { |
| 526 if (!MediaStorageUtil::IsMediaDevice(existing_devices[i].device_id)) |
| 527 continue; |
| 528 preferences->AddGallery(existing_devices[i].device_id, |
| 529 existing_devices[i].name, FilePath(), |
| 530 false /*not user added*/); |
| 531 } |
| 532 return preferences; |
| 533 } |
| 534 |
516 void MediaFileSystemRegistry::OnRemovableStorageAttached( | 535 void MediaFileSystemRegistry::OnRemovableStorageAttached( |
517 const std::string& id, const string16& name, | 536 const std::string& id, const string16& name, |
518 const FilePath::StringType& location) { | 537 const FilePath::StringType& location) { |
519 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 538 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
520 | 539 |
521 if (!MediaStorageUtil::IsMediaDevice(id)) | 540 if (!MediaStorageUtil::IsMediaDevice(id)) |
522 return; | 541 return; |
523 | 542 |
524 for (ExtensionGalleriesHostMap::iterator profile_it = | 543 for (ExtensionGalleriesHostMap::iterator profile_it = |
525 extension_hosts_map_.begin(); | 544 extension_hosts_map_.begin(); |
526 profile_it != extension_hosts_map_.end(); | 545 profile_it != extension_hosts_map_.end(); |
527 ++profile_it) { | 546 ++profile_it) { |
528 MediaGalleriesPreferences* preferences = | 547 MediaGalleriesPreferences* preferences = |
529 MediaGalleriesPreferencesFactory::GetForProfile(profile_it->first); | 548 GetProfilePreferences(profile_it->first); |
530 preferences->AddGallery(id, name, FilePath(), false /*not user added*/); | 549 preferences->AddGallery(id, name, FilePath(), false /*not user added*/); |
531 } | 550 } |
532 } | 551 } |
533 | 552 |
534 void MediaFileSystemRegistry::OnRemovableStorageDetached( | 553 void MediaFileSystemRegistry::OnRemovableStorageDetached( |
535 const std::string& id) { | 554 const std::string& id) { |
536 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
537 | 556 |
538 // Since revoking a gallery in the ExtensionGalleriesHost may cause it | 557 // Since revoking a gallery in the ExtensionGalleriesHost may cause it |
539 // to be removed from the map and therefore invalidate any iterator pointing | 558 // to be removed from the map and therefore invalidate any iterator pointing |
540 // to it, this code first copies all the invalid gallery ids and the | 559 // to it, this code first copies all the invalid gallery ids and the |
541 // extension hosts in which they may appear (per profile) and revoked it in | 560 // extension hosts in which they may appear (per profile) and revoked it in |
542 // a second step. | 561 // a second step. |
543 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; | 562 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; |
544 | 563 |
545 for (ExtensionGalleriesHostMap::iterator profile_it = | 564 for (ExtensionGalleriesHostMap::iterator profile_it = |
546 extension_hosts_map_.begin(); | 565 extension_hosts_map_.begin(); |
547 profile_it != extension_hosts_map_.end(); | 566 profile_it != extension_hosts_map_.end(); |
548 ++profile_it) { | 567 ++profile_it) { |
549 MediaGalleriesPreferences* preferences = | 568 MediaGalleriesPreferences* preferences = |
550 MediaGalleriesPreferencesFactory::GetForProfile(profile_it->first); | 569 GetProfilePreferences(profile_it->first); |
551 InvalidatedGalleriesInfo invalid_galleries_in_profile; | 570 InvalidatedGalleriesInfo invalid_galleries_in_profile; |
552 invalid_galleries_in_profile.pref_ids = | 571 invalid_galleries_in_profile.pref_ids = |
553 preferences->LookUpGalleriesByDeviceId(id); | 572 preferences->LookUpGalleriesByDeviceId(id); |
554 | 573 |
555 for (ExtensionHostMap::const_iterator extension_host_it = | 574 for (ExtensionHostMap::const_iterator extension_host_it = |
556 profile_it->second.begin(); | 575 profile_it->second.begin(); |
557 extension_host_it != profile_it->second.end(); | 576 extension_host_it != profile_it->second.end(); |
558 ++extension_host_it) { | 577 ++extension_host_it) { |
559 invalid_galleries_in_profile.extension_hosts.insert( | 578 invalid_galleries_in_profile.extension_hosts.insert( |
560 extension_host_it->second.get()); | 579 extension_host_it->second.get()); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 | 690 |
672 void MediaFileSystemRegistry::RemoveScopedMtpDeviceMapEntry( | 691 void MediaFileSystemRegistry::RemoveScopedMtpDeviceMapEntry( |
673 const FilePath::StringType& device_location) { | 692 const FilePath::StringType& device_location) { |
674 MTPDeviceDelegateMap::iterator delegate_it = | 693 MTPDeviceDelegateMap::iterator delegate_it = |
675 mtp_delegate_map_.find(device_location); | 694 mtp_delegate_map_.find(device_location); |
676 DCHECK(delegate_it != mtp_delegate_map_.end()); | 695 DCHECK(delegate_it != mtp_delegate_map_.end()); |
677 mtp_delegate_map_.erase(delegate_it); | 696 mtp_delegate_map_.erase(delegate_it); |
678 } | 697 } |
679 #endif | 698 #endif |
680 | 699 |
681 void MediaFileSystemRegistry::AddAttachedMediaDeviceGalleries( | |
682 MediaGalleriesPreferences* preferences) { | |
683 // SystemMonitor may be NULL in unit tests. | |
684 SystemMonitor* system_monitor = SystemMonitor::Get(); | |
685 if (!system_monitor) | |
686 return; | |
687 | |
688 std::vector<SystemMonitor::RemovableStorageInfo> existing_devices = | |
689 system_monitor->GetAttachedRemovableStorage(); | |
690 for (size_t i = 0; i < existing_devices.size(); i++) { | |
691 if (!MediaStorageUtil::IsMediaDevice(existing_devices[i].device_id)) | |
692 continue; | |
693 preferences->AddGallery(existing_devices[i].device_id, | |
694 existing_devices[i].name, FilePath(), | |
695 false /*not user added*/); | |
696 } | |
697 } | |
698 | |
699 void MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty( | 700 void MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty( |
700 Profile* profile, const std::string& extension_id) { | 701 Profile* profile, const std::string& extension_id) { |
701 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 702 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
702 | 703 |
703 ExtensionGalleriesHostMap::iterator extension_hosts = | 704 ExtensionGalleriesHostMap::iterator extension_hosts = |
704 extension_hosts_map_.find(profile); | 705 extension_hosts_map_.find(profile); |
705 DCHECK(extension_hosts != extension_hosts_map_.end()); | 706 DCHECK(extension_hosts != extension_hosts_map_.end()); |
706 ExtensionHostMap::size_type erase_count = | 707 ExtensionHostMap::size_type erase_count = |
707 extension_hosts->second.erase(extension_id); | 708 extension_hosts->second.erase(extension_id); |
708 DCHECK_EQ(1U, erase_count); | 709 DCHECK_EQ(1U, erase_count); |
709 if (extension_hosts->second.empty()) | 710 if (extension_hosts->second.empty()) |
710 extension_hosts_map_.erase(extension_hosts); | 711 extension_hosts_map_.erase(extension_hosts); |
711 } | 712 } |
712 | 713 |
713 } // namespace chrome | 714 } // namespace chrome |
OLD | NEW |