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 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 extension_hosts_map_.find(profile); | 451 extension_hosts_map_.find(profile); |
452 if (extension_hosts->second.empty()) | 452 if (extension_hosts->second.empty()) |
453 preferences->AddGalleryChangeObserver(this); | 453 preferences->AddGalleryChangeObserver(this); |
454 | 454 |
455 ExtensionGalleriesHost* extension_host = | 455 ExtensionGalleriesHost* extension_host = |
456 extension_hosts->second[extension->id()].get(); | 456 extension_hosts->second[extension->id()].get(); |
457 if (!extension_host) { | 457 if (!extension_host) { |
458 extension_host = new ExtensionGalleriesHost( | 458 extension_host = new ExtensionGalleriesHost( |
459 file_system_context_.get(), | 459 file_system_context_.get(), |
460 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty, | 460 base::Bind(&MediaFileSystemRegistry::OnExtensionGalleriesHostEmpty, |
461 base::Unretained(this), profile, extension->id())); | 461 base::Unretained(this), |
| 462 profile, |
| 463 extension->id())); |
462 extension_hosts_map_[profile][extension->id()] = extension_host; | 464 extension_hosts_map_[profile][extension->id()] = extension_host; |
463 } | 465 } |
464 extension_host->ReferenceFromRVH(rvh); | 466 extension_host->ReferenceFromRVH(rvh); |
465 | 467 |
466 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(), | 468 extension_host->GetMediaFileSystems(galleries, preferences->known_galleries(), |
467 callback); | 469 callback); |
468 } | 470 } |
469 | 471 |
470 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( | 472 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( |
471 Profile* profile) { | 473 Profile* profile) { |
472 MediaGalleriesPreferences* preferences = | 474 // Create an empty ExtensionHostMap for this profile on first initialization. |
473 MediaGalleriesPreferencesFactory::GetForProfile(profile); | 475 if (!ContainsKey(extension_hosts_map_, profile)) |
474 if (ContainsKey(extension_hosts_map_, profile)) | 476 extension_hosts_map_[profile] = ExtensionHostMap(); |
475 return preferences; | |
476 | |
477 // Create an empty entry so the initialization code below only gets called | |
478 // once per profile. | |
479 extension_hosts_map_[profile] = ExtensionHostMap(); | |
480 media_galleries::UsageCount(media_galleries::PROFILES_WITH_USAGE); | 477 media_galleries::UsageCount(media_galleries::PROFILES_WITH_USAGE); |
481 | 478 |
482 // TODO(gbillock): Move this stanza to MediaGalleriesPreferences init code. | 479 return MediaGalleriesPreferencesFactory::GetForProfile(profile); |
483 StorageMonitor* monitor = StorageMonitor::GetInstance(); | |
484 DCHECK(monitor->IsInitialized()); | |
485 std::vector<StorageInfo> existing_devices = | |
486 monitor->GetAllAvailableStorages(); | |
487 for (size_t i = 0; i < existing_devices.size(); i++) { | |
488 if (!(StorageInfo::IsMediaDevice(existing_devices[i].device_id()) && | |
489 StorageInfo::IsRemovableDevice(existing_devices[i].device_id()))) | |
490 continue; | |
491 preferences->AddGallery(existing_devices[i].device_id(), | |
492 base::FilePath(), | |
493 false, | |
494 existing_devices[i].storage_label(), | |
495 existing_devices[i].vendor_name(), | |
496 existing_devices[i].model_name(), | |
497 existing_devices[i].total_size_in_bytes(), | |
498 base::Time::Now()); | |
499 } | |
500 return preferences; | |
501 } | 480 } |
502 | 481 |
503 void MediaFileSystemRegistry::OnRemovableStorageDetached( | 482 void MediaFileSystemRegistry::OnRemovableStorageDetached( |
504 const StorageInfo& info) { | 483 const StorageInfo& info) { |
505 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
506 | 485 |
507 // Since revoking a gallery in the ExtensionGalleriesHost may cause it | 486 // Since revoking a gallery in the ExtensionGalleriesHost may cause it |
508 // to be removed from the map and therefore invalidate any iterator pointing | 487 // to be removed from the map and therefore invalidate any iterator pointing |
509 // to it, this code first copies all the invalid gallery ids and the | 488 // to it, this code first copies all the invalid gallery ids and the |
510 // extension hosts in which they may appear (per profile) and revoked it in | 489 // extension hosts in which they may appear (per profile) and revoked it in |
511 // a second step. | 490 // a second step. |
512 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; | 491 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; |
513 | 492 |
514 for (ExtensionGalleriesHostMap::iterator profile_it = | 493 for (ExtensionGalleriesHostMap::iterator profile_it = |
515 extension_hosts_map_.begin(); | 494 extension_hosts_map_.begin(); |
516 profile_it != extension_hosts_map_.end(); | 495 profile_it != extension_hosts_map_.end(); |
517 ++profile_it) { | 496 ++profile_it) { |
518 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); | 497 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); |
| 498 // If |preferences| is not yet initialized, it won't contain any galleries. |
| 499 if (!preferences->IsInitialized()) |
| 500 continue; |
| 501 |
519 InvalidatedGalleriesInfo invalid_galleries_in_profile; | 502 InvalidatedGalleriesInfo invalid_galleries_in_profile; |
520 invalid_galleries_in_profile.pref_ids = | 503 invalid_galleries_in_profile.pref_ids = |
521 preferences->LookUpGalleriesByDeviceId(info.device_id()); | 504 preferences->LookUpGalleriesByDeviceId(info.device_id()); |
522 | 505 |
523 for (ExtensionHostMap::const_iterator extension_host_it = | 506 for (ExtensionHostMap::const_iterator extension_host_it = |
524 profile_it->second.begin(); | 507 profile_it->second.begin(); |
525 extension_host_it != profile_it->second.end(); | 508 extension_host_it != profile_it->second.end(); |
526 ++extension_host_it) { | 509 ++extension_host_it) { |
527 invalid_galleries_in_profile.extension_hosts.insert( | 510 invalid_galleries_in_profile.extension_hosts.insert( |
528 extension_host_it->second.get()); | 511 extension_host_it->second.get()); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 | 597 |
615 IsolatedContext::GetInstance()->RevokeFileSystem(fsid); | 598 IsolatedContext::GetInstance()->RevokeFileSystem(fsid); |
616 } | 599 } |
617 | 600 |
618 private: | 601 private: |
619 MediaFileSystemRegistry* registry_; | 602 MediaFileSystemRegistry* registry_; |
620 | 603 |
621 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); | 604 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); |
622 }; | 605 }; |
623 | 606 |
| 607 // Constructor in 'private' section because depends on private class definition. |
624 MediaFileSystemRegistry::MediaFileSystemRegistry() | 608 MediaFileSystemRegistry::MediaFileSystemRegistry() |
625 : file_system_context_(new MediaFileSystemContextImpl(this)) { | 609 : file_system_context_(new MediaFileSystemContextImpl(this)) { |
626 StorageMonitor::GetInstance()->AddObserver(this); | 610 StorageMonitor::GetInstance()->AddObserver(this); |
627 } | 611 } |
628 | 612 |
629 MediaFileSystemRegistry::~MediaFileSystemRegistry() { | 613 MediaFileSystemRegistry::~MediaFileSystemRegistry() { |
630 // TODO(gbillock): This is needed because the unit test uses the | 614 // TODO(gbillock): This is needed because the unit test uses the |
631 // g_browser_process registry. We should create one in the unit test, | 615 // g_browser_process registry. We should create one in the unit test, |
632 // and then can remove this. | 616 // and then can remove this. |
633 if (StorageMonitor::GetInstance()) | 617 if (StorageMonitor::GetInstance()) |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 DCHECK_EQ(1U, erase_count); | 715 DCHECK_EQ(1U, erase_count); |
732 if (extension_hosts->second.empty()) { | 716 if (extension_hosts->second.empty()) { |
733 // When a profile has no ExtensionGalleriesHosts left, remove the | 717 // When a profile has no ExtensionGalleriesHosts left, remove the |
734 // matching gallery-change-watcher since it is no longer needed. Leave the | 718 // matching gallery-change-watcher since it is no longer needed. Leave the |
735 // |extension_hosts| entry alone, since it indicates the profile has been | 719 // |extension_hosts| entry alone, since it indicates the profile has been |
736 // previously used. | 720 // previously used. |
737 MediaGalleriesPreferences* preferences = GetPreferences(profile); | 721 MediaGalleriesPreferences* preferences = GetPreferences(profile); |
738 preferences->RemoveGalleryChangeObserver(this); | 722 preferences->RemoveGalleryChangeObserver(this); |
739 } | 723 } |
740 } | 724 } |
OLD | NEW |