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 // 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 CHROME_BROWSER_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ | 11 #ifndef CHROME_BROWSER_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ |
12 #define CHROME_BROWSER_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ | 12 #define CHROME_BROWSER_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 <string> | 19 #include <string> |
20 | 20 |
21 #include "base/basictypes.h" | 21 #include "base/basictypes.h" |
22 #include "base/compiler_specific.h" | 22 #include "base/compiler_specific.h" |
23 #include "base/files/file_path.h" | 23 #include "base/files/file_path.h" |
24 #include "base/files/file_path_watcher.h" | 24 #include "base/files/file_path_watcher.h" |
25 #include "base/memory/scoped_ptr.h" | 25 #include "base/memory/scoped_ptr.h" |
| 26 #include "base/memory/weak_ptr.h" |
26 #include "chrome/browser/storage_monitor/mtab_watcher_linux.h" | 27 #include "chrome/browser/storage_monitor/mtab_watcher_linux.h" |
27 #include "chrome/browser/storage_monitor/storage_monitor.h" | 28 #include "chrome/browser/storage_monitor/storage_monitor.h" |
28 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
29 | 30 |
30 namespace chrome { | 31 namespace chrome { |
31 | 32 |
32 class MediaTransferProtocolDeviceObserverLinux; | 33 class MediaTransferProtocolDeviceObserverLinux; |
33 | 34 |
34 class StorageMonitorLinux : public StorageMonitor, | 35 class StorageMonitorLinux : public StorageMonitor, |
35 public MtabWatcherLinux::Delegate { | 36 public MtabWatcherLinux::Delegate { |
36 public: | 37 public: |
37 // Should only be called by browser start up code. Use GetInstance() instead. | 38 // Should only be called by browser start up code. |
38 explicit StorageMonitorLinux(const base::FilePath& path); | 39 // Use StorageMonitor::GetInstance() instead. |
| 40 // |mtab_file_path| is the path to a mtab file to watch for mount points. |
| 41 explicit StorageMonitorLinux(const base::FilePath& mtab_file_path); |
39 virtual ~StorageMonitorLinux(); | 42 virtual ~StorageMonitorLinux(); |
40 | 43 |
41 // Must be called for StorageMonitorLinux to work. | 44 // Must be called for StorageMonitorLinux to work. |
42 void Init(); | 45 void Init(); |
43 | 46 |
44 // Finds the device that contains |path| and populates |device_info|. | |
45 // Returns false if unable to find the device. | |
46 virtual bool GetStorageInfoForPath( | |
47 const base::FilePath& path, | |
48 StorageInfo* device_info) const OVERRIDE; | |
49 | |
50 protected: | 47 protected: |
51 // Gets device information given a |device_path| and |mount_point|. | 48 // Gets device information given a |device_path| and |mount_point|. |
52 typedef base::Callback<scoped_ptr<StorageInfo>( | 49 typedef base::Callback<scoped_ptr<StorageInfo>( |
53 const base::FilePath& device_path, | 50 const base::FilePath& device_path, |
54 const base::FilePath& mount_point)> GetDeviceInfoCallback; | 51 const base::FilePath& mount_point)> GetDeviceInfoCallback; |
55 | 52 |
56 void SetGetDeviceInfoCallbackForTest( | 53 void SetGetDeviceInfoCallbackForTest( |
57 const GetDeviceInfoCallback& get_device_info_callback); | 54 const GetDeviceInfoCallback& get_device_info_callback); |
58 | 55 |
| 56 void SetMediaTransferProtocolManagerForTest( |
| 57 device::MediaTransferProtocolManager* test_manager); |
| 58 |
59 // MtabWatcherLinux::Delegate implementation. | 59 // MtabWatcherLinux::Delegate implementation. |
60 virtual void UpdateMtab( | 60 virtual void UpdateMtab( |
61 const MtabWatcherLinux::MountPointDeviceMap& new_mtab) OVERRIDE; | 61 const MtabWatcherLinux::MountPointDeviceMap& new_mtab) OVERRIDE; |
62 | 62 |
63 private: | 63 private: |
64 // Structure to save mounted device information such as device path, unique | 64 // Structure to save mounted device information such as device path, unique |
65 // identifier, device name and partition size. | 65 // identifier, device name and partition size. |
66 struct MountPointInfo { | 66 struct MountPointInfo { |
67 base::FilePath mount_device; | 67 base::FilePath mount_device; |
68 StorageInfo storage_info; | 68 StorageInfo storage_info; |
(...skipping 13 matching lines...) Expand all Loading... |
82 // (mount point, priority) | 82 // (mount point, priority) |
83 // For devices that are mounted to multiple mount points, this helps us track | 83 // For devices that are mounted to multiple mount points, this helps us track |
84 // which one we've notified system monitor about. | 84 // which one we've notified system monitor about. |
85 typedef std::map<base::FilePath, bool> ReferencedMountPoint; | 85 typedef std::map<base::FilePath, bool> ReferencedMountPoint; |
86 | 86 |
87 // (mount device, map of known mount points) | 87 // (mount device, map of known mount points) |
88 // For each mount device, track the places it is mounted and which one (if | 88 // For each mount device, track the places it is mounted and which one (if |
89 // any) we have notified system monitor about. | 89 // any) we have notified system monitor about. |
90 typedef std::map<base::FilePath, ReferencedMountPoint> MountPriorityMap; | 90 typedef std::map<base::FilePath, ReferencedMountPoint> MountPriorityMap; |
91 | 91 |
| 92 // StorageMonitor implementation. |
| 93 virtual bool GetStorageInfoForPath(const base::FilePath& path, |
| 94 StorageInfo* device_info) const OVERRIDE; |
| 95 virtual device::MediaTransferProtocolManager* |
| 96 media_transfer_protocol_manager() OVERRIDE; |
| 97 |
92 // Called when the MtabWatcher has been created. | 98 // Called when the MtabWatcher has been created. |
93 void OnMtabWatcherCreated(MtabWatcherLinux* watcher); | 99 void OnMtabWatcherCreated(MtabWatcherLinux* watcher); |
94 | 100 |
95 bool IsDeviceAlreadyMounted(const base::FilePath& mount_device) const; | 101 bool IsDeviceAlreadyMounted(const base::FilePath& mount_device) const; |
96 | 102 |
97 // Assuming |mount_device| is already mounted, and it gets mounted again at | 103 // Assuming |mount_device| is already mounted, and it gets mounted again at |
98 // |mount_point|, update the mappings. | 104 // |mount_point|, update the mappings. |
99 void HandleDeviceMountedMultipleTimes(const base::FilePath& mount_device, | 105 void HandleDeviceMountedMultipleTimes(const base::FilePath& mount_device, |
100 const base::FilePath& mount_point); | 106 const base::FilePath& mount_point); |
101 | 107 |
(...skipping 12 matching lines...) Expand all Loading... |
114 // Keep in mind on Linux, a device can be mounted at multiple mount points, | 120 // Keep in mind on Linux, a device can be mounted at multiple mount points, |
115 // and multiple devices can be mounted at a mount point. | 121 // and multiple devices can be mounted at a mount point. |
116 MountMap mount_info_map_; | 122 MountMap mount_info_map_; |
117 | 123 |
118 // Because a device can be mounted to multiple places, we only want to | 124 // Because a device can be mounted to multiple places, we only want to |
119 // notify about one of them. If (and only if) that one is unmounted, we need | 125 // notify about one of them. If (and only if) that one is unmounted, we need |
120 // to notify about it's departure and notify about another one of it's mount | 126 // to notify about it's departure and notify about another one of it's mount |
121 // points. | 127 // points. |
122 MountPriorityMap mount_priority_map_; | 128 MountPriorityMap mount_priority_map_; |
123 | 129 |
| 130 scoped_ptr<device::MediaTransferProtocolManager> |
| 131 media_transfer_protocol_manager_; |
124 scoped_ptr<MediaTransferProtocolDeviceObserverLinux> | 132 scoped_ptr<MediaTransferProtocolDeviceObserverLinux> |
125 media_transfer_protocol_device_observer_; | 133 media_transfer_protocol_device_observer_; |
126 | 134 |
127 scoped_ptr<MtabWatcherLinux, MtabWatcherLinuxDeleter> mtab_watcher_; | 135 scoped_ptr<MtabWatcherLinux, MtabWatcherLinuxDeleter> mtab_watcher_; |
128 | 136 |
129 base::WeakPtrFactory<StorageMonitorLinux> weak_ptr_factory_; | 137 base::WeakPtrFactory<StorageMonitorLinux> weak_ptr_factory_; |
130 | 138 |
131 DISALLOW_COPY_AND_ASSIGN(StorageMonitorLinux); | 139 DISALLOW_COPY_AND_ASSIGN(StorageMonitorLinux); |
132 }; | 140 }; |
133 | 141 |
134 } // namespace chrome | 142 } // namespace chrome |
135 | 143 |
136 #endif // CHROME_BROWSER_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ | 144 #endif // CHROME_BROWSER_STORAGE_MONITOR_STORAGE_MONITOR_LINUX_H_ |
OLD | NEW |