| 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_util.h" | 18 #include "base/string_util.h" |
| 19 #include "base/system_monitor/system_monitor.h" | |
| 20 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 21 #include "base/values.h" | 20 #include "base/values.h" |
| 22 #include "chrome/browser/extensions/extension_service.h" | 21 #include "chrome/browser/extensions/extension_service.h" |
| 23 #include "chrome/browser/extensions/extension_system.h" | 22 #include "chrome/browser/extensions/extension_system.h" |
| 24 #include "chrome/browser/media_gallery/media_file_system_context.h" | 23 #include "chrome/browser/media_gallery/media_file_system_context.h" |
| 25 #include "chrome/browser/media_gallery/media_galleries_preferences.h" | 24 #include "chrome/browser/media_gallery/media_galleries_preferences.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 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 callback); | 472 callback); |
| 474 } | 473 } |
| 475 | 474 |
| 476 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( | 475 MediaGalleriesPreferences* MediaFileSystemRegistry::GetPreferences( |
| 477 Profile* profile) { | 476 Profile* profile) { |
| 478 MediaGalleriesPreferences* preferences = | 477 MediaGalleriesPreferences* preferences = |
| 479 MediaGalleriesPreferencesFactory::GetForProfile(profile); | 478 MediaGalleriesPreferencesFactory::GetForProfile(profile); |
| 480 if (ContainsKey(extension_hosts_map_, profile)) | 479 if (ContainsKey(extension_hosts_map_, profile)) |
| 481 return preferences; | 480 return preferences; |
| 482 | 481 |
| 483 // SystemMonitor may be NULL in unit tests. | 482 // RemovableStorageNotifications may be NULL in unit tests. |
| 484 SystemMonitor* system_monitor = SystemMonitor::Get(); | 483 RemovableStorageNotifications* notifications = |
| 485 if (!system_monitor) | 484 RemovableStorageNotifications::GetInstance(); |
| 485 if (!notifications) |
| 486 return preferences; | 486 return preferences; |
| 487 std::vector<SystemMonitor::RemovableStorageInfo> existing_devices = | 487 std::vector<RemovableStorageNotifications::StorageInfo> |
| 488 system_monitor->GetAttachedRemovableStorage(); | 488 existing_devices = notifications->GetAttachedStorage(); |
| 489 for (size_t i = 0; i < existing_devices.size(); i++) { | 489 for (size_t i = 0; i < existing_devices.size(); i++) { |
| 490 if (!MediaStorageUtil::IsMediaDevice(existing_devices[i].device_id)) | 490 if (!MediaStorageUtil::IsMediaDevice(existing_devices[i].device_id)) |
| 491 continue; | 491 continue; |
| 492 preferences->AddGallery(existing_devices[i].device_id, | 492 preferences->AddGallery(existing_devices[i].device_id, |
| 493 existing_devices[i].name, FilePath(), | 493 existing_devices[i].name, FilePath(), |
| 494 false /*not user added*/); | 494 false /*not user added*/); |
| 495 } | 495 } |
| 496 return preferences; | 496 return preferences; |
| 497 } | 497 } |
| 498 | 498 |
| 499 void MediaFileSystemRegistry::OnRemovableStorageAttached( | 499 void MediaFileSystemRegistry::OnRemovableStorageAttached( |
| 500 const std::string& id, const string16& name, | 500 const RemovableStorageNotifications::StorageInfo& info) { |
| 501 const FilePath::StringType& location) { | |
| 502 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 501 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 503 transient_device_ids_.DeviceAttached(id); | 502 transient_device_ids_.DeviceAttached(info.device_id); |
| 504 | 503 |
| 505 if (!MediaStorageUtil::IsMediaDevice(id)) | 504 if (!MediaStorageUtil::IsMediaDevice(info.device_id)) |
| 506 return; | 505 return; |
| 507 | 506 |
| 508 for (ExtensionGalleriesHostMap::iterator profile_it = | 507 for (ExtensionGalleriesHostMap::iterator profile_it = |
| 509 extension_hosts_map_.begin(); | 508 extension_hosts_map_.begin(); |
| 510 profile_it != extension_hosts_map_.end(); | 509 profile_it != extension_hosts_map_.end(); |
| 511 ++profile_it) { | 510 ++profile_it) { |
| 512 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); | 511 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); |
| 513 preferences->AddGallery(id, name, FilePath(), false /*not user added*/); | 512 preferences->AddGallery(info.device_id, info.name, FilePath(), |
| 513 false /*not user added*/); |
| 514 } | 514 } |
| 515 } | 515 } |
| 516 | 516 |
| 517 void MediaFileSystemRegistry::OnRemovableStorageDetached( | 517 void MediaFileSystemRegistry::OnRemovableStorageDetached( |
| 518 const std::string& id) { | 518 const RemovableStorageNotifications::StorageInfo& info) { |
| 519 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 519 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 520 | 520 |
| 521 // Since revoking a gallery in the ExtensionGalleriesHost may cause it | 521 // Since revoking a gallery in the ExtensionGalleriesHost may cause it |
| 522 // to be removed from the map and therefore invalidate any iterator pointing | 522 // to be removed from the map and therefore invalidate any iterator pointing |
| 523 // to it, this code first copies all the invalid gallery ids and the | 523 // to it, this code first copies all the invalid gallery ids and the |
| 524 // extension hosts in which they may appear (per profile) and revoked it in | 524 // extension hosts in which they may appear (per profile) and revoked it in |
| 525 // a second step. | 525 // a second step. |
| 526 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; | 526 std::vector<InvalidatedGalleriesInfo> invalid_galleries_info; |
| 527 | 527 |
| 528 for (ExtensionGalleriesHostMap::iterator profile_it = | 528 for (ExtensionGalleriesHostMap::iterator profile_it = |
| 529 extension_hosts_map_.begin(); | 529 extension_hosts_map_.begin(); |
| 530 profile_it != extension_hosts_map_.end(); | 530 profile_it != extension_hosts_map_.end(); |
| 531 ++profile_it) { | 531 ++profile_it) { |
| 532 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); | 532 MediaGalleriesPreferences* preferences = GetPreferences(profile_it->first); |
| 533 InvalidatedGalleriesInfo invalid_galleries_in_profile; | 533 InvalidatedGalleriesInfo invalid_galleries_in_profile; |
| 534 invalid_galleries_in_profile.pref_ids = | 534 invalid_galleries_in_profile.pref_ids = |
| 535 preferences->LookUpGalleriesByDeviceId(id); | 535 preferences->LookUpGalleriesByDeviceId(info.device_id); |
| 536 | 536 |
| 537 for (ExtensionHostMap::const_iterator extension_host_it = | 537 for (ExtensionHostMap::const_iterator extension_host_it = |
| 538 profile_it->second.begin(); | 538 profile_it->second.begin(); |
| 539 extension_host_it != profile_it->second.end(); | 539 extension_host_it != profile_it->second.end(); |
| 540 ++extension_host_it) { | 540 ++extension_host_it) { |
| 541 invalid_galleries_in_profile.extension_hosts.insert( | 541 invalid_galleries_in_profile.extension_hosts.insert( |
| 542 extension_host_it->second.get()); | 542 extension_host_it->second.get()); |
| 543 } | 543 } |
| 544 | 544 |
| 545 invalid_galleries_info.push_back(invalid_galleries_in_profile); | 545 invalid_galleries_info.push_back(invalid_galleries_in_profile); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 } | 629 } |
| 630 | 630 |
| 631 private: | 631 private: |
| 632 MediaFileSystemRegistry* registry_; | 632 MediaFileSystemRegistry* registry_; |
| 633 | 633 |
| 634 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); | 634 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemContextImpl); |
| 635 }; | 635 }; |
| 636 | 636 |
| 637 MediaFileSystemRegistry::MediaFileSystemRegistry() | 637 MediaFileSystemRegistry::MediaFileSystemRegistry() |
| 638 : file_system_context_(new MediaFileSystemContextImpl(this)) { | 638 : file_system_context_(new MediaFileSystemContextImpl(this)) { |
| 639 // SystemMonitor may be NULL in unit tests. | 639 // RemovableStorageNotifications may be NULL in unit tests. |
| 640 SystemMonitor* system_monitor = SystemMonitor::Get(); | 640 RemovableStorageNotifications* notifications = |
| 641 if (system_monitor) { | 641 RemovableStorageNotifications::GetInstance(); |
| 642 system_monitor->AddDevicesChangedObserver(this); | 642 if (notifications) { |
| 643 notifications->AddObserver(this); |
| 643 | 644 |
| 644 // Add the devices that were already present before MediaFileSystemRegistry | 645 // Add the devices that were already present before MediaFileSystemRegistry |
| 645 // creation. | 646 // creation. |
| 646 std::vector<base::SystemMonitor::RemovableStorageInfo> storage_info = | 647 std::vector<RemovableStorageNotifications::StorageInfo> |
| 647 system_monitor->GetAttachedRemovableStorage(); | 648 storage_info = notifications->GetAttachedStorage(); |
| 648 for (size_t i = 0; i < storage_info.size(); ++i) | 649 for (size_t i = 0; i < storage_info.size(); ++i) |
| 649 transient_device_ids_.DeviceAttached(storage_info[i].device_id); | 650 transient_device_ids_.DeviceAttached(storage_info[i].device_id); |
| 650 } | 651 } |
| 651 } | 652 } |
| 652 | 653 |
| 653 MediaFileSystemRegistry::~MediaFileSystemRegistry() { | 654 MediaFileSystemRegistry::~MediaFileSystemRegistry() { |
| 654 // SystemMonitor may be NULL in unit tests. | 655 // RemovableStorageNotifications may be NULL in unit tests. |
| 655 SystemMonitor* system_monitor = SystemMonitor::Get(); | 656 RemovableStorageNotifications* notifications = |
| 656 if (system_monitor) | 657 RemovableStorageNotifications::GetInstance(); |
| 657 system_monitor->RemoveDevicesChangedObserver(this); | 658 if (notifications) |
| 659 notifications->RemoveObserver(this); |
| 658 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 660 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 659 DCHECK(mtp_device_delegate_map_.empty()); | 661 DCHECK(mtp_device_delegate_map_.empty()); |
| 660 #endif | 662 #endif |
| 661 } | 663 } |
| 662 | 664 |
| 663 void MediaFileSystemRegistry::OnMediaGalleriesRememberedGalleriesChanged( | 665 void MediaFileSystemRegistry::OnMediaGalleriesRememberedGalleriesChanged( |
| 664 PrefServiceBase* prefs) { | 666 PrefServiceBase* prefs) { |
| 665 // Find the Profile that contains the source PrefService. | 667 // Find the Profile that contains the source PrefService. |
| 666 PrefChangeRegistrarMap::iterator pref_change_it = | 668 PrefChangeRegistrarMap::iterator pref_change_it = |
| 667 pref_change_registrar_map_.begin(); | 669 pref_change_registrar_map_.begin(); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 739 | 741 |
| 740 PrefChangeRegistrarMap::iterator pref_it = | 742 PrefChangeRegistrarMap::iterator pref_it = |
| 741 pref_change_registrar_map_.find(profile); | 743 pref_change_registrar_map_.find(profile); |
| 742 DCHECK(pref_it != pref_change_registrar_map_.end()); | 744 DCHECK(pref_it != pref_change_registrar_map_.end()); |
| 743 delete pref_it->second; | 745 delete pref_it->second; |
| 744 pref_change_registrar_map_.erase(pref_it); | 746 pref_change_registrar_map_.erase(pref_it); |
| 745 } | 747 } |
| 746 } | 748 } |
| 747 | 749 |
| 748 } // namespace chrome | 750 } // namespace chrome |
| OLD | NEW |