| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // StorageMonitorLinux processes mount point change events, notifies listeners | 5 // StorageMonitorLinux processes mount point change events, notifies listeners |
| 6 // about the addition and deletion of media devices, and answers queries about | 6 // about the addition and deletion of media devices, and answers queries about |
| 7 // mounted devices. | 7 // mounted devices. |
| 8 // StorageMonitorLinux lives on the UI thread, and uses a MtabWatcherLinux on | 8 // StorageMonitorLinux lives on the UI thread, and uses a MtabWatcherLinux on |
| 9 // the FILE thread to get mount point change events. | 9 // the FILE thread to get mount point change events. |
| 10 | 10 |
| 11 #ifndef COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ | 11 #ifndef COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ |
| 12 #define COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ | 12 #define COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ |
| 13 | 13 |
| 14 #if defined(OS_CHROMEOS) | 14 #if defined(OS_CHROMEOS) |
| 15 #error "Use the ChromeOS-specific implementation instead." | 15 #error "Use the ChromeOS-specific implementation instead." |
| 16 #endif | 16 #endif |
| 17 | 17 |
| 18 #include <map> | 18 #include <map> |
| 19 #include <memory> |
| 19 #include <string> | 20 #include <string> |
| 20 | 21 |
| 21 #include "base/compiler_specific.h" | 22 #include "base/compiler_specific.h" |
| 22 #include "base/files/file_path.h" | 23 #include "base/files/file_path.h" |
| 23 #include "base/files/file_path_watcher.h" | 24 #include "base/files/file_path_watcher.h" |
| 24 #include "base/macros.h" | 25 #include "base/macros.h" |
| 25 #include "base/memory/scoped_ptr.h" | |
| 26 #include "base/memory/weak_ptr.h" | 26 #include "base/memory/weak_ptr.h" |
| 27 #include "build/build_config.h" | 27 #include "build/build_config.h" |
| 28 #include "components/storage_monitor/mtab_watcher_linux.h" | 28 #include "components/storage_monitor/mtab_watcher_linux.h" |
| 29 #include "components/storage_monitor/storage_monitor.h" | 29 #include "components/storage_monitor/storage_monitor.h" |
| 30 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 31 | 31 |
| 32 namespace storage_monitor { | 32 namespace storage_monitor { |
| 33 | 33 |
| 34 class MediaTransferProtocolDeviceObserverLinux; | 34 class MediaTransferProtocolDeviceObserverLinux; |
| 35 | 35 |
| 36 class StorageMonitorLinux : public StorageMonitor, | 36 class StorageMonitorLinux : public StorageMonitor, |
| 37 public MtabWatcherLinux::Delegate { | 37 public MtabWatcherLinux::Delegate { |
| 38 public: | 38 public: |
| 39 // Should only be called by browser start up code. | 39 // Should only be called by browser start up code. |
| 40 // Use StorageMonitor::GetInstance() instead. | 40 // Use StorageMonitor::GetInstance() instead. |
| 41 // |mtab_file_path| is the path to a mtab file to watch for mount points. | 41 // |mtab_file_path| is the path to a mtab file to watch for mount points. |
| 42 explicit StorageMonitorLinux(const base::FilePath& mtab_file_path); | 42 explicit StorageMonitorLinux(const base::FilePath& mtab_file_path); |
| 43 ~StorageMonitorLinux() override; | 43 ~StorageMonitorLinux() override; |
| 44 | 44 |
| 45 // Must be called for StorageMonitorLinux to work. | 45 // Must be called for StorageMonitorLinux to work. |
| 46 void Init() override; | 46 void Init() override; |
| 47 | 47 |
| 48 protected: | 48 protected: |
| 49 // Gets device information given a |device_path| and |mount_point|. | 49 // Gets device information given a |device_path| and |mount_point|. |
| 50 typedef base::Callback<scoped_ptr<StorageInfo>( | 50 using GetDeviceInfoCallback = base::Callback<std::unique_ptr<StorageInfo>( |
| 51 const base::FilePath& device_path, | 51 const base::FilePath& device_path, |
| 52 const base::FilePath& mount_point)> GetDeviceInfoCallback; | 52 const base::FilePath& mount_point)>; |
| 53 | 53 |
| 54 void SetGetDeviceInfoCallbackForTest( | 54 void SetGetDeviceInfoCallbackForTest( |
| 55 const GetDeviceInfoCallback& get_device_info_callback); | 55 const GetDeviceInfoCallback& get_device_info_callback); |
| 56 | 56 |
| 57 void SetMediaTransferProtocolManagerForTest( | 57 void SetMediaTransferProtocolManagerForTest( |
| 58 device::MediaTransferProtocolManager* test_manager); | 58 device::MediaTransferProtocolManager* test_manager); |
| 59 | 59 |
| 60 // MtabWatcherLinux::Delegate implementation. | 60 // MtabWatcherLinux::Delegate implementation. |
| 61 void UpdateMtab( | 61 void UpdateMtab( |
| 62 const MtabWatcherLinux::MountPointDeviceMap& new_mtab) override; | 62 const MtabWatcherLinux::MountPointDeviceMap& new_mtab) override; |
| 63 | 63 |
| 64 private: | 64 private: |
| 65 // Structure to save mounted device information such as device path, unique | 65 // Structure to save mounted device information such as device path, unique |
| 66 // identifier, device name and partition size. | 66 // identifier, device name and partition size. |
| 67 struct MountPointInfo { | 67 struct MountPointInfo { |
| 68 base::FilePath mount_device; | 68 base::FilePath mount_device; |
| 69 StorageInfo storage_info; | 69 StorageInfo storage_info; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 // For use with scoped_ptr. | 72 // For use with std::unique_ptr. |
| 73 struct MtabWatcherLinuxDeleter { | 73 struct MtabWatcherLinuxDeleter { |
| 74 void operator()(MtabWatcherLinux* mtab_watcher) { | 74 void operator()(MtabWatcherLinux* mtab_watcher) { |
| 75 content::BrowserThread::DeleteSoon(content::BrowserThread::FILE, | 75 content::BrowserThread::DeleteSoon(content::BrowserThread::FILE, |
| 76 FROM_HERE, mtab_watcher); | 76 FROM_HERE, mtab_watcher); |
| 77 } | 77 } |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 // Mapping of mount points to MountPointInfo. | 80 // Mapping of mount points to MountPointInfo. |
| 81 typedef std::map<base::FilePath, MountPointInfo> MountMap; | 81 typedef std::map<base::FilePath, MountPointInfo> MountMap; |
| 82 | 82 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 103 | 103 |
| 104 bool IsDeviceAlreadyMounted(const base::FilePath& mount_device) const; | 104 bool IsDeviceAlreadyMounted(const base::FilePath& mount_device) const; |
| 105 | 105 |
| 106 // Assuming |mount_device| is already mounted, and it gets mounted again at | 106 // Assuming |mount_device| is already mounted, and it gets mounted again at |
| 107 // |mount_point|, update the mappings. | 107 // |mount_point|, update the mappings. |
| 108 void HandleDeviceMountedMultipleTimes(const base::FilePath& mount_device, | 108 void HandleDeviceMountedMultipleTimes(const base::FilePath& mount_device, |
| 109 const base::FilePath& mount_point); | 109 const base::FilePath& mount_point); |
| 110 | 110 |
| 111 // Adds |mount_device| to the mappings and notify listeners, if any. | 111 // Adds |mount_device| to the mappings and notify listeners, if any. |
| 112 void AddNewMount(const base::FilePath& mount_device, | 112 void AddNewMount(const base::FilePath& mount_device, |
| 113 scoped_ptr<StorageInfo> storage_info); | 113 std::unique_ptr<StorageInfo> storage_info); |
| 114 | 114 |
| 115 // Mtab file that lists the mount points. | 115 // Mtab file that lists the mount points. |
| 116 const base::FilePath mtab_path_; | 116 const base::FilePath mtab_path_; |
| 117 | 117 |
| 118 // Callback to get device information. Set this to a custom callback for | 118 // Callback to get device information. Set this to a custom callback for |
| 119 // testing. | 119 // testing. |
| 120 GetDeviceInfoCallback get_device_info_callback_; | 120 GetDeviceInfoCallback get_device_info_callback_; |
| 121 | 121 |
| 122 // Mapping of relevant mount points and their corresponding mount devices. | 122 // Mapping of relevant mount points and their corresponding mount devices. |
| 123 // Keep in mind on Linux, a device can be mounted at multiple mount points, | 123 // Keep in mind on Linux, a device can be mounted at multiple mount points, |
| 124 // and multiple devices can be mounted at a mount point. | 124 // and multiple devices can be mounted at a mount point. |
| 125 MountMap mount_info_map_; | 125 MountMap mount_info_map_; |
| 126 | 126 |
| 127 // Because a device can be mounted to multiple places, we only want to | 127 // Because a device can be mounted to multiple places, we only want to |
| 128 // notify about one of them. If (and only if) that one is unmounted, we need | 128 // notify about one of them. If (and only if) that one is unmounted, we need |
| 129 // to notify about it's departure and notify about another one of it's mount | 129 // to notify about it's departure and notify about another one of it's mount |
| 130 // points. | 130 // points. |
| 131 MountPriorityMap mount_priority_map_; | 131 MountPriorityMap mount_priority_map_; |
| 132 | 132 |
| 133 scoped_ptr<device::MediaTransferProtocolManager> | 133 std::unique_ptr<device::MediaTransferProtocolManager> |
| 134 media_transfer_protocol_manager_; | 134 media_transfer_protocol_manager_; |
| 135 scoped_ptr<MediaTransferProtocolDeviceObserverLinux> | 135 std::unique_ptr<MediaTransferProtocolDeviceObserverLinux> |
| 136 media_transfer_protocol_device_observer_; | 136 media_transfer_protocol_device_observer_; |
| 137 | 137 |
| 138 scoped_ptr<MtabWatcherLinux, MtabWatcherLinuxDeleter> mtab_watcher_; | 138 std::unique_ptr<MtabWatcherLinux, MtabWatcherLinuxDeleter> mtab_watcher_; |
| 139 | 139 |
| 140 base::WeakPtrFactory<StorageMonitorLinux> weak_ptr_factory_; | 140 base::WeakPtrFactory<StorageMonitorLinux> weak_ptr_factory_; |
| 141 | 141 |
| 142 DISALLOW_COPY_AND_ASSIGN(StorageMonitorLinux); | 142 DISALLOW_COPY_AND_ASSIGN(StorageMonitorLinux); |
| 143 }; | 143 }; |
| 144 | 144 |
| 145 } // namespace storage_monitor | 145 } // namespace storage_monitor |
| 146 | 146 |
| 147 #endif // COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ | 147 #endif // COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ |
| OLD | NEW |