Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: chrome/browser/media_gallery/media_file_system_registry.cc

Issue 11304022: [Media Gallery] Add existing attached devices to the prefs in time for media galleries dialog. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 = GetPreferences(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::GetPreferences(
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 = GetPreferences(profile_it->first);
529 MediaGalleriesPreferencesFactory::GetForProfile(profile_it->first);
530 preferences->AddGallery(id, name, FilePath(), false /*not user added*/); 548 preferences->AddGallery(id, name, FilePath(), false /*not user added*/);
531 } 549 }
532 } 550 }
533 551
534 void MediaFileSystemRegistry::OnRemovableStorageDetached( 552 void MediaFileSystemRegistry::OnRemovableStorageDetached(
535 const std::string& id) { 553 const std::string& id) {
536 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 554 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
537 555
538 // Since revoking a gallery in the ExtensionGalleriesHost may cause it 556 // Since revoking a gallery in the ExtensionGalleriesHost may cause it
539 // to be removed from the map and therefore invalidate any iterator pointing 557 // 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 558 // 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 559 // extension hosts in which they may appear (per profile) and revoked it in
542 // a second step. 560 // a second step.
543 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; 561 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info;
544 562
545 for (ExtensionGalleriesHostMap::iterator profile_it = 563 for (ExtensionGalleriesHostMap::iterator profile_it =
546 extension_hosts_map_.begin(); 564 extension_hosts_map_.begin();
547 profile_it != extension_hosts_map_.end(); 565 profile_it != extension_hosts_map_.end();
548 ++profile_it) { 566 ++profile_it) {
549 MediaGalleriesPreferences* preferences = 567 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first);
550 MediaGalleriesPreferencesFactory::GetForProfile(profile_it->first);
551 InvalidatedGalleriesInfo invalid_galleries_in_profile; 568 InvalidatedGalleriesInfo invalid_galleries_in_profile;
552 invalid_galleries_in_profile.pref_ids = 569 invalid_galleries_in_profile.pref_ids =
553 preferences->LookUpGalleriesByDeviceId(id); 570 preferences->LookUpGalleriesByDeviceId(id);
554 571
555 for (ExtensionHostMap::const_iterator extension_host_it = 572 for (ExtensionHostMap::const_iterator extension_host_it =
556 profile_it->second.begin(); 573 profile_it->second.begin();
557 extension_host_it != profile_it->second.end(); 574 extension_host_it != profile_it->second.end();
558 ++extension_host_it) { 575 ++extension_host_it) {
559 invalid_galleries_in_profile.extension_hosts.insert( 576 invalid_galleries_in_profile.extension_hosts.insert(
560 extension_host_it->second.get()); 577 extension_host_it->second.get());
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 688
672 void MediaFileSystemRegistry::RemoveScopedMtpDeviceMapEntry( 689 void MediaFileSystemRegistry::RemoveScopedMtpDeviceMapEntry(
673 const FilePath::StringType& device_location) { 690 const FilePath::StringType& device_location) {
674 MTPDeviceDelegateMap::iterator delegate_it = 691 MTPDeviceDelegateMap::iterator delegate_it =
675 mtp_delegate_map_.find(device_location); 692 mtp_delegate_map_.find(device_location);
676 DCHECK(delegate_it != mtp_delegate_map_.end()); 693 DCHECK(delegate_it != mtp_delegate_map_.end());
677 mtp_delegate_map_.erase(delegate_it); 694 mtp_delegate_map_.erase(delegate_it);
678 } 695 }
679 #endif 696 #endif
680 697
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( 698 void MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty(
700 Profile* profile, const std::string& extension_id) { 699 Profile* profile, const std::string& extension_id) {
701 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 700 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
702 701
703 ExtensionGalleriesHostMap::iterator extension_hosts = 702 ExtensionGalleriesHostMap::iterator extension_hosts =
704 extension_hosts_map_.find(profile); 703 extension_hosts_map_.find(profile);
705 DCHECK(extension_hosts != extension_hosts_map_.end()); 704 DCHECK(extension_hosts != extension_hosts_map_.end());
706 ExtensionHostMap::size_type erase_count = 705 ExtensionHostMap::size_type erase_count =
707 extension_hosts->second.erase(extension_id); 706 extension_hosts->second.erase(extension_id);
708 DCHECK_EQ(1U, erase_count); 707 DCHECK_EQ(1U, erase_count);
709 if (extension_hosts->second.empty()) 708 if (extension_hosts->second.empty())
710 extension_hosts_map_.erase(extension_hosts); 709 extension_hosts_map_.erase(extension_hosts);
711 } 710 }
712 711
713 } // namespace chrome 712 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698