| 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/file_path.h" | 14 #include "base/file_path.h" |
| 15 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
| 19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 20 #include "base/system_monitor/system_monitor.h" | |
| 21 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 22 #include "base/values.h" | 21 #include "base/values.h" |
| 23 #include "chrome/browser/extensions/extension_service.h" | 22 #include "chrome/browser/extensions/extension_service.h" |
| 24 #include "chrome/browser/extensions/extension_system.h" | 23 #include "chrome/browser/extensions/extension_system.h" |
| 25 #include "chrome/browser/media_gallery/media_file_system_context.h" | 24 #include "chrome/browser/media_gallery/media_file_system_context.h" |
| 26 #include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" | 25 #include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" |
| 27 #include "chrome/browser/media_gallery/scoped_mtp_device_map_entry.h" | 26 #include "chrome/browser/media_gallery/scoped_mtp_device_map_entry.h" |
| 28 #include "chrome/browser/prefs/pref_service.h" | 27 #include "chrome/browser/prefs/pref_service.h" |
| 29 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 30 #include "chrome/browser/system_monitor/media_storage_util.h" | 29 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 30 #include "chrome/browser/system_monitor/removable_storage_notifications.h" |
| 31 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" |
| 32 #include "chrome/common/chrome_paths.h" | 32 #include "chrome/common/chrome_paths.h" |
| 33 #include "chrome/common/extensions/extension_constants.h" | 33 #include "chrome/common/extensions/extension_constants.h" |
| 34 #include "chrome/common/extensions/extension_set.h" | 34 #include "chrome/common/extensions/extension_set.h" |
| 35 #include "chrome/common/extensions/extension.h" | 35 #include "chrome/common/extensions/extension.h" |
| 36 #include "chrome/common/pref_names.h" | 36 #include "chrome/common/pref_names.h" |
| 37 #include "content/public/browser/browser_thread.h" | 37 #include "content/public/browser/browser_thread.h" |
| 38 #include "content/public/browser/notification_details.h" | 38 #include "content/public/browser/notification_details.h" |
| 39 #include "content/public/browser/notification_observer.h" | 39 #include "content/public/browser/notification_observer.h" |
| 40 #include "content/public/browser/notification_registrar.h" | 40 #include "content/public/browser/notification_registrar.h" |
| 41 #include "content/public/browser/notification_source.h" | 41 #include "content/public/browser/notification_source.h" |
| 42 #include "content/public/browser/notification_types.h" | 42 #include "content/public/browser/notification_types.h" |
| 43 #include "content/public/browser/render_process_host.h" | 43 #include "content/public/browser/render_process_host.h" |
| 44 #include "content/public/browser/render_view_host.h" | 44 #include "content/public/browser/render_view_host.h" |
| 45 #include "content/public/browser/web_contents.h" | 45 #include "content/public/browser/web_contents.h" |
| 46 #include "webkit/fileapi/file_system_types.h" | 46 #include "webkit/fileapi/file_system_types.h" |
| 47 #include "webkit/fileapi/isolated_context.h" | 47 #include "webkit/fileapi/isolated_context.h" |
| 48 | 48 |
| 49 using base::SystemMonitor; | |
| 50 using content::BrowserThread; | 49 using content::BrowserThread; |
| 51 using content::NavigationController; | 50 using content::NavigationController; |
| 52 using content::RenderProcessHost; | 51 using content::RenderProcessHost; |
| 53 using content::WebContents; | 52 using content::WebContents; |
| 54 using fileapi::IsolatedContext; | 53 using fileapi::IsolatedContext; |
| 55 | 54 |
| 56 namespace chrome { | 55 namespace chrome { |
| 57 | 56 |
| 58 namespace { | 57 namespace { |
| 59 | 58 |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 callback); | 486 callback); |
| 488 } | 487 } |
| 489 | 488 |
| 490 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( | 489 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( |
| 491 Profile* profile) { | 490 Profile* profile) { |
| 492 MediaGalleriesPreferences* preferences = | 491 MediaGalleriesPreferences* preferences = |
| 493 MediaGalleriesPreferencesFactory::GetForProfile(profile); | 492 MediaGalleriesPreferencesFactory::GetForProfile(profile); |
| 494 if (ContainsKey(extension_hosts_map_, profile)) | 493 if (ContainsKey(extension_hosts_map_, profile)) |
| 495 return preferences; | 494 return preferences; |
| 496 | 495 |
| 497 // SystemMonitor may be NULL in unit tests. | 496 // RemovableStorageNotifications may be NULL in unit tests. |
| 498 SystemMonitor* system_monitor = SystemMonitor::Get(); | 497 RemovableStorageNotifications* notifications = |
| 499 if (!system_monitor) | 498 RemovableStorageNotifications::GetInstance(); |
| 499 if (!notifications) |
| 500 return preferences; | 500 return preferences; |
| 501 std::vector<SystemMonitor::RemovableStorageInfo> existing_devices = | 501 std::vector<RemovableStorageNotifications::StorageInfo> |
| 502 system_monitor->GetAttachedRemovableStorage(); | 502 existing_devices = notifications->GetAttachedStorage(); |
| 503 for (size_t i = 0; i < existing_devices.size(); i++) { | 503 for (size_t i = 0; i < existing_devices.size(); i++) { |
| 504 if (!MediaStorageUtil::IsMediaDevice(existing_devices[i].device_id)) | 504 if (!MediaStorageUtil::IsMediaDevice(existing_devices[i].device_id)) |
| 505 continue; | 505 continue; |
| 506 preferences->AddGallery(existing_devices[i].device_id, | 506 preferences->AddGallery(existing_devices[i].device_id, |
| 507 existing_devices[i].name, FilePath(), | 507 existing_devices[i].name, FilePath(), |
| 508 false /*not user added*/); | 508 false /*not user added*/); |
| 509 } | 509 } |
| 510 return preferences; | 510 return preferences; |
| 511 } | 511 } |
| 512 | 512 |
| 513 void MediaFileSystemRegistry::OnRemovableStorageAttached( | 513 void MediaFileSystemRegistry::OnRemovableStorageAttached( |
| 514 const std::string& id, const string16& name, | 514 const RemovableStorageNotifications::StorageInfo& info) { |
| 515 const FilePath::StringType& location) { | |
| 516 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 515 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 517 | 516 |
| 518 if (!MediaStorageUtil::IsMediaDevice(id)) | 517 if (!MediaStorageUtil::IsMediaDevice(info.device_id)) |
| 519 return; | 518 return; |
| 520 | 519 |
| 521 for (ExtensionGalleriesHostMap::iterator profile_it = | 520 for (ExtensionGalleriesHostMap::iterator profile_it = |
| 522 extension_hosts_map_.begin(); | 521 extension_hosts_map_.begin(); |
| 523 profile_it != extension_hosts_map_.end(); | 522 profile_it != extension_hosts_map_.end(); |
| 524 ++profile_it) { | 523 ++profile_it) { |
| 525 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); | 524 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); |
| 526 preferences->AddGallery(id, name, FilePath(), false /*not user added*/); | 525 preferences->AddGallery(info.device_id, info.name, FilePath(), |
| 526 false /*not user added*/); |
| 527 } | 527 } |
| 528 } | 528 } |
| 529 | 529 |
| 530 void MediaFileSystemRegistry::OnRemovableStorageDetached( | 530 void MediaFileSystemRegistry::OnRemovableStorageDetached( |
| 531 const std::string& id) { | 531 const RemovableStorageNotifications::StorageInfo& info) { |
| 532 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 532 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 533 | 533 |
| 534 // Since revoking a gallery in the ExtensionGalleriesHost may cause it | 534 // Since revoking a gallery in the ExtensionGalleriesHost may cause it |
| 535 // to be removed from the map and therefore invalidate any iterator pointing | 535 // to be removed from the map and therefore invalidate any iterator pointing |
| 536 // to it, this code first copies all the invalid gallery ids and the | 536 // to it, this code first copies all the invalid gallery ids and the |
| 537 // extension hosts in which they may appear (per profile) and revoked it in | 537 // extension hosts in which they may appear (per profile) and revoked it in |
| 538 // a second step. | 538 // a second step. |
| 539 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; | 539 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; |
| 540 | 540 |
| 541 for (ExtensionGalleriesHostMap::iterator profile_it = | 541 for (ExtensionGalleriesHostMap::iterator profile_it = |
| 542 extension_hosts_map_.begin(); | 542 extension_hosts_map_.begin(); |
| 543 profile_it != extension_hosts_map_.end(); | 543 profile_it != extension_hosts_map_.end(); |
| 544 ++profile_it) { | 544 ++profile_it) { |
| 545 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); | 545 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); |
| 546 InvalidatedGalleriesInfo invalid_galleries_in_profile; | 546 InvalidatedGalleriesInfo invalid_galleries_in_profile; |
| 547 invalid_galleries_in_profile.pref_ids = | 547 invalid_galleries_in_profile.pref_ids = |
| 548 preferences->LookUpGalleriesByDeviceId(id); | 548 preferences->LookUpGalleriesByDeviceId(info.device_id); |
| 549 | 549 |
| 550 for (ExtensionHostMap::const_iterator extension_host_it = | 550 for (ExtensionHostMap::const_iterator extension_host_it = |
| 551 profile_it->second.begin(); | 551 profile_it->second.begin(); |
| 552 extension_host_it != profile_it->second.end(); | 552 extension_host_it != profile_it->second.end(); |
| 553 ++extension_host_it) { | 553 ++extension_host_it) { |
| 554 invalid_galleries_in_profile.extension_hosts.insert( | 554 invalid_galleries_in_profile.extension_hosts.insert( |
| 555 extension_host_it->second.get()); | 555 extension_host_it->second.get()); |
| 556 } | 556 } |
| 557 | 557 |
| 558 invalid_galleries_info.push_back(invalid_galleries_in_profile); | 558 invalid_galleries_info.push_back(invalid_galleries_in_profile); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 } | 642 } |
| 643 | 643 |
| 644 private: | 644 private: |
| 645 MediaFileSystemRegistry* registry_; | 645 MediaFileSystemRegistry* registry_; |
| 646 | 646 |
| 647 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); | 647 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); |
| 648 }; | 648 }; |
| 649 | 649 |
| 650 MediaFileSystemRegistry::MediaFileSystemRegistry() | 650 MediaFileSystemRegistry::MediaFileSystemRegistry() |
| 651 : file_system_context_(new MediaFileSystemContextImpl(this)) { | 651 : file_system_context_(new MediaFileSystemContextImpl(this)) { |
| 652 // SystemMonitor may be NULL in unit tests. | 652 // RemovableStorageNotifications may be NULL in unit tests. |
| 653 SystemMonitor* system_monitor = SystemMonitor::Get(); | 653 RemovableStorageNotifications* notifications = |
| 654 if (system_monitor) | 654 RemovableStorageNotifications::GetInstance(); |
| 655 system_monitor->AddDevicesChangedObserver(this); | 655 if (notifications) |
| 656 notifications->AddObserver(this); |
| 656 } | 657 } |
| 657 | 658 |
| 658 MediaFileSystemRegistry::~MediaFileSystemRegistry() { | 659 MediaFileSystemRegistry::~MediaFileSystemRegistry() { |
| 659 // SystemMonitor may be NULL in unit tests. | 660 // RemovableStorageNotifications may be NULL in unit tests. |
| 660 SystemMonitor* system_monitor = SystemMonitor::Get(); | 661 RemovableStorageNotifications* notifications = |
| 661 if (system_monitor) | 662 RemovableStorageNotifications::GetInstance(); |
| 662 system_monitor->RemoveDevicesChangedObserver(this); | 663 if (notifications) |
| 664 notifications->RemoveObserver(this); |
| 663 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 665 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 664 DCHECK(mtp_device_delegate_map_.empty()); | 666 DCHECK(mtp_device_delegate_map_.empty()); |
| 665 #endif | 667 #endif |
| 666 } | 668 } |
| 667 | 669 |
| 668 void MediaFileSystemRegistry::OnMediaGalleriesRememberedGalleriesChanged( | 670 void MediaFileSystemRegistry::OnMediaGalleriesRememberedGalleriesChanged( |
| 669 PrefServiceBase* prefs) { | 671 PrefServiceBase* prefs) { |
| 670 // Find the Profile that contains the source PrefService. | 672 // Find the Profile that contains the source PrefService. |
| 671 PrefChangeRegistrarMap::iterator pref_change_it = | 673 PrefChangeRegistrarMap::iterator pref_change_it = |
| 672 pref_change_registrar_map_.begin(); | 674 pref_change_registrar_map_.begin(); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 744 | 746 |
| 745 PrefChangeRegistrarMap::iterator pref_it = | 747 PrefChangeRegistrarMap::iterator pref_it = |
| 746 pref_change_registrar_map_.find(profile); | 748 pref_change_registrar_map_.find(profile); |
| 747 DCHECK(pref_it != pref_change_registrar_map_.end()); | 749 DCHECK(pref_it != pref_change_registrar_map_.end()); |
| 748 delete pref_it->second; | 750 delete pref_it->second; |
| 749 pref_change_registrar_map_.erase(pref_it); | 751 pref_change_registrar_map_.erase(pref_it); |
| 750 } | 752 } |
| 751 } | 753 } |
| 752 | 754 |
| 753 } // namespace chrome | 755 } // namespace chrome |
| OLD | NEW |