| 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> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
| 16 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" |
| 20 #include "base/string_util.h" | 20 #include "base/string_util.h" |
| 21 #include "base/system_monitor/system_monitor.h" | |
| 22 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 23 #include "base/values.h" | 22 #include "base/values.h" |
| 24 #include "chrome/browser/extensions/extension_service.h" | 23 #include "chrome/browser/extensions/extension_service.h" |
| 25 #include "chrome/browser/extensions/extension_system.h" | 24 #include "chrome/browser/extensions/extension_system.h" |
| 26 #include "chrome/browser/media_gallery/media_file_system_context.h" | 25 #include "chrome/browser/media_gallery/media_file_system_context.h" |
| 27 #include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" | 26 #include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" |
| 28 #include "chrome/browser/media_gallery/scoped_mtp_device_map_entry.h" | 27 #include "chrome/browser/media_gallery/scoped_mtp_device_map_entry.h" |
| 29 #include "chrome/browser/prefs/pref_service.h" | 28 #include "chrome/browser/prefs/pref_service.h" |
| 30 #include "chrome/browser/profiles/profile.h" | 29 #include "chrome/browser/profiles/profile.h" |
| 31 #include "chrome/browser/system_monitor/media_storage_util.h" | 30 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 31 #include "chrome/browser/system_monitor/removable_storage_notifications.h" |
| 32 #include "chrome/common/chrome_notification_types.h" | 32 #include "chrome/common/chrome_notification_types.h" |
| 33 #include "chrome/common/chrome_paths.h" | 33 #include "chrome/common/chrome_paths.h" |
| 34 #include "chrome/common/chrome_switches.h" | 34 #include "chrome/common/chrome_switches.h" |
| 35 #include "chrome/common/extensions/extension_constants.h" | 35 #include "chrome/common/extensions/extension_constants.h" |
| 36 #include "chrome/common/extensions/extension_set.h" | 36 #include "chrome/common/extensions/extension_set.h" |
| 37 #include "chrome/common/extensions/extension.h" | 37 #include "chrome/common/extensions/extension.h" |
| 38 #include "chrome/common/pref_names.h" | 38 #include "chrome/common/pref_names.h" |
| 39 #include "content/public/browser/browser_thread.h" | 39 #include "content/public/browser/browser_thread.h" |
| 40 #include "content/public/browser/notification_details.h" | 40 #include "content/public/browser/notification_details.h" |
| 41 #include "content/public/browser/notification_observer.h" | 41 #include "content/public/browser/notification_observer.h" |
| 42 #include "content/public/browser/notification_registrar.h" | 42 #include "content/public/browser/notification_registrar.h" |
| 43 #include "content/public/browser/notification_source.h" | 43 #include "content/public/browser/notification_source.h" |
| 44 #include "content/public/browser/notification_types.h" | 44 #include "content/public/browser/notification_types.h" |
| 45 #include "content/public/browser/render_process_host.h" | 45 #include "content/public/browser/render_process_host.h" |
| 46 #include "content/public/browser/render_view_host.h" | 46 #include "content/public/browser/render_view_host.h" |
| 47 #include "content/public/browser/web_contents.h" | 47 #include "content/public/browser/web_contents.h" |
| 48 #include "webkit/fileapi/file_system_types.h" | 48 #include "webkit/fileapi/file_system_types.h" |
| 49 #include "webkit/fileapi/isolated_context.h" | 49 #include "webkit/fileapi/isolated_context.h" |
| 50 | 50 |
| 51 using base::SystemMonitor; | |
| 52 using content::BrowserThread; | 51 using content::BrowserThread; |
| 53 using content::NavigationController; | 52 using content::NavigationController; |
| 54 using content::RenderProcessHost; | 53 using content::RenderProcessHost; |
| 55 using content::WebContents; | 54 using content::WebContents; |
| 56 using fileapi::IsolatedContext; | 55 using fileapi::IsolatedContext; |
| 57 | 56 |
| 58 namespace chrome { | 57 namespace chrome { |
| 59 | 58 |
| 60 namespace { | 59 namespace { |
| 61 | 60 |
| (...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 callback); | 509 callback); |
| 511 } | 510 } |
| 512 | 511 |
| 513 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( | 512 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( |
| 514 Profile* profile) { | 513 Profile* profile) { |
| 515 MediaGalleriesPreferences* preferences = | 514 MediaGalleriesPreferences* preferences = |
| 516 MediaGalleriesPreferencesFactory::GetForProfile(profile); | 515 MediaGalleriesPreferencesFactory::GetForProfile(profile); |
| 517 if (ContainsKey(extension_hosts_map_, profile)) | 516 if (ContainsKey(extension_hosts_map_, profile)) |
| 518 return preferences; | 517 return preferences; |
| 519 | 518 |
| 520 // SystemMonitor may be NULL in unit tests. | 519 // RemovableStorageNotifications may be NULL in unit tests. |
| 521 SystemMonitor* system_monitor = SystemMonitor::Get(); | 520 RemovableStorageNotifications* notifications = |
| 522 if (!system_monitor) | 521 RemovableStorageNotifications::GetInstance(); |
| 522 if (!notifications) |
| 523 return preferences; | 523 return preferences; |
| 524 std::vector<SystemMonitor::RemovableStorageInfo> existing_devices = | 524 std::vector<RemovableStorageNotifications::StorageInfo> |
| 525 system_monitor->GetAttachedRemovableStorage(); | 525 existing_devices = notifications->GetAttachedStorage(); |
| 526 for (size_t i = 0; i < existing_devices.size(); i++) { | 526 for (size_t i = 0; i < existing_devices.size(); i++) { |
| 527 if (!MediaStorageUtil::IsMediaDevice(existing_devices[i].device_id)) | 527 if (!MediaStorageUtil::IsMediaDevice(existing_devices[i].device_id)) |
| 528 continue; | 528 continue; |
| 529 preferences->AddGallery(existing_devices[i].device_id, | 529 preferences->AddGallery(existing_devices[i].device_id, |
| 530 existing_devices[i].name, FilePath(), | 530 existing_devices[i].name, FilePath(), |
| 531 false /*not user added*/); | 531 false /*not user added*/); |
| 532 } | 532 } |
| 533 return preferences; | 533 return preferences; |
| 534 } | 534 } |
| 535 | 535 |
| 536 void MediaFileSystemRegistry::OnRemovableStorageAttached( | 536 void MediaFileSystemRegistry::OnRemovableStorageAttached( |
| 537 const std::string& id, const string16& name, | 537 const RemovableStorageNotifications::StorageInfo& info) { |
| 538 const FilePath::StringType& location) { | |
| 539 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 538 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 540 | 539 |
| 541 if (!MediaStorageUtil::IsMediaDevice(id)) | 540 if (!MediaStorageUtil::IsMediaDevice(info.device_id)) |
| 542 return; | 541 return; |
| 543 | 542 |
| 544 for (ExtensionGalleriesHostMap::iterator profile_it = | 543 for (ExtensionGalleriesHostMap::iterator profile_it = |
| 545 extension_hosts_map_.begin(); | 544 extension_hosts_map_.begin(); |
| 546 profile_it != extension_hosts_map_.end(); | 545 profile_it != extension_hosts_map_.end(); |
| 547 ++profile_it) { | 546 ++profile_it) { |
| 548 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); | 547 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); |
| 549 preferences->AddGallery(id, name, FilePath(), false /*not user added*/); | 548 preferences->AddGallery(info.device_id, info.name, FilePath(), |
| 549 false /*not user added*/); |
| 550 } | 550 } |
| 551 } | 551 } |
| 552 | 552 |
| 553 void MediaFileSystemRegistry::OnRemovableStorageDetached( | 553 void MediaFileSystemRegistry::OnRemovableStorageDetached( |
| 554 const std::string& id) { | 554 const RemovableStorageNotifications::StorageInfo& info) { |
| 555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 556 | 556 |
| 557 // Since revoking a gallery in the ExtensionGalleriesHost may cause it | 557 // Since revoking a gallery in the ExtensionGalleriesHost may cause it |
| 558 // 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 |
| 559 // 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 |
| 560 // 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 |
| 561 // a second step. | 561 // a second step. |
| 562 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; | 562 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; |
| 563 | 563 |
| 564 for (ExtensionGalleriesHostMap::iterator profile_it = | 564 for (ExtensionGalleriesHostMap::iterator profile_it = |
| 565 extension_hosts_map_.begin(); | 565 extension_hosts_map_.begin(); |
| 566 profile_it != extension_hosts_map_.end(); | 566 profile_it != extension_hosts_map_.end(); |
| 567 ++profile_it) { | 567 ++profile_it) { |
| 568 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); | 568 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); |
| 569 InvalidatedGalleriesInfo invalid_galleries_in_profile; | 569 InvalidatedGalleriesInfo invalid_galleries_in_profile; |
| 570 invalid_galleries_in_profile.pref_ids = | 570 invalid_galleries_in_profile.pref_ids = |
| 571 preferences->LookUpGalleriesByDeviceId(id); | 571 preferences->LookUpGalleriesByDeviceId(info.device_id); |
| 572 | 572 |
| 573 for (ExtensionHostMap::const_iterator extension_host_it = | 573 for (ExtensionHostMap::const_iterator extension_host_it = |
| 574 profile_it->second.begin(); | 574 profile_it->second.begin(); |
| 575 extension_host_it != profile_it->second.end(); | 575 extension_host_it != profile_it->second.end(); |
| 576 ++extension_host_it) { | 576 ++extension_host_it) { |
| 577 invalid_galleries_in_profile.extension_hosts.insert( | 577 invalid_galleries_in_profile.extension_hosts.insert( |
| 578 extension_host_it->second.get()); | 578 extension_host_it->second.get()); |
| 579 } | 579 } |
| 580 | 580 |
| 581 invalid_galleries_info.push_back(invalid_galleries_in_profile); | 581 invalid_galleries_info.push_back(invalid_galleries_in_profile); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 } | 665 } |
| 666 | 666 |
| 667 private: | 667 private: |
| 668 MediaFileSystemRegistry* registry_; | 668 MediaFileSystemRegistry* registry_; |
| 669 | 669 |
| 670 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); | 670 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); |
| 671 }; | 671 }; |
| 672 | 672 |
| 673 MediaFileSystemRegistry::MediaFileSystemRegistry() | 673 MediaFileSystemRegistry::MediaFileSystemRegistry() |
| 674 : file_system_context_(new MediaFileSystemContextImpl(this)) { | 674 : file_system_context_(new MediaFileSystemContextImpl(this)) { |
| 675 // SystemMonitor may be NULL in unit tests. | 675 // RemovableStorageNotifications may be NULL in unit tests. |
| 676 SystemMonitor* system_monitor = SystemMonitor::Get(); | 676 RemovableStorageNotifications* notifications = |
| 677 if (system_monitor) | 677 RemovableStorageNotifications::GetInstance(); |
| 678 system_monitor->AddDevicesChangedObserver(this); | 678 if (notifications) |
| 679 notifications->AddObserver(this); |
| 679 } | 680 } |
| 680 | 681 |
| 681 MediaFileSystemRegistry::~MediaFileSystemRegistry() { | 682 MediaFileSystemRegistry::~MediaFileSystemRegistry() { |
| 682 // SystemMonitor may be NULL in unit tests. | 683 // RemovableStorageNotifications may be NULL in unit tests. |
| 683 SystemMonitor* system_monitor = SystemMonitor::Get(); | 684 RemovableStorageNotifications* notifications = |
| 684 if (system_monitor) | 685 RemovableStorageNotifications::GetInstance(); |
| 685 system_monitor->RemoveDevicesChangedObserver(this); | 686 if (notifications) |
| 687 notifications->RemoveObserver(this); |
| 686 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 688 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 687 DCHECK(mtp_device_delegate_map_.empty()); | 689 DCHECK(mtp_device_delegate_map_.empty()); |
| 688 #endif | 690 #endif |
| 689 } | 691 } |
| 690 | 692 |
| 691 void MediaFileSystemRegistry::OnMediaGalleriesRememberedGalleriesChanged( | 693 void MediaFileSystemRegistry::OnMediaGalleriesRememberedGalleriesChanged( |
| 692 PrefServiceBase* prefs) { | 694 PrefServiceBase* prefs) { |
| 693 // Find the Profile that contains the source PrefService. | 695 // Find the Profile that contains the source PrefService. |
| 694 PrefChangeRegistrarMap::iterator pref_change_it = | 696 PrefChangeRegistrarMap::iterator pref_change_it = |
| 695 pref_change_registrar_map_.begin(); | 697 pref_change_registrar_map_.begin(); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 767 | 769 |
| 768 PrefChangeRegistrarMap::iterator pref_it = | 770 PrefChangeRegistrarMap::iterator pref_it = |
| 769 pref_change_registrar_map_.find(profile); | 771 pref_change_registrar_map_.find(profile); |
| 770 DCHECK(pref_it != pref_change_registrar_map_.end()); | 772 DCHECK(pref_it != pref_change_registrar_map_.end()); |
| 771 delete pref_it->second; | 773 delete pref_it->second; |
| 772 pref_change_registrar_map_.erase(pref_it); | 774 pref_change_registrar_map_.erase(pref_it); |
| 773 } | 775 } |
| 774 } | 776 } |
| 775 | 777 |
| 776 } // namespace chrome | 778 } // namespace chrome |
| OLD | NEW |