| 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 // RemovableDeviceNotificationsLinux listens for mount point changes, notifies | 5 // RemovableDeviceNotificationsLinux listens for mount point changes, notifies |
| 6 // listeners about the addition and deletion of media devices, and | 6 // listeners about the addition and deletion of media devices, and |
| 7 // answers queries about mounted devices. | 7 // answers queries about mounted devices. |
| 8 | 8 |
| 9 #ifndef CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_LINUX_H_ | 9 #ifndef CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_LINUX_H_ |
| 10 #define CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_LINUX_H_ | 10 #define CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_LINUX_H_ |
| 11 | 11 |
| 12 #if defined(OS_CHROMEOS) | 12 #if defined(OS_CHROMEOS) |
| 13 #error "Use the ChromeOS-specific implementation instead." | 13 #error "Use the ChromeOS-specific implementation instead." |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 #include <map> | 16 #include <map> |
| 17 #include <set> | 17 #include <set> |
| 18 #include <string> | 18 #include <string> |
| 19 #include <utility> | 19 #include <utility> |
| 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_watcher.h" | 23 #include "base/files/file_path_watcher.h" |
| 24 #include "base/memory/ref_counted.h" | 24 #include "base/memory/ref_counted.h" |
| 25 #include "chrome/browser/system_monitor/removable_storage_notifications.h" | 25 #include "chrome/browser/system_monitor/removable_storage_notifications.h" |
| 26 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 27 | 27 |
| 28 namespace base { |
| 28 class FilePath; | 29 class FilePath; |
| 30 } |
| 29 | 31 |
| 30 // Gets device information given a |device_path|. On success, fills in | 32 // Gets device information given a |device_path|. On success, fills in |
| 31 // |unique_id|, |name|, |removable| and |partition_size_in_bytes|. | 33 // |unique_id|, |name|, |removable| and |partition_size_in_bytes|. |
| 32 typedef void (*GetDeviceInfoFunc)(const FilePath& device_path, | 34 typedef void (*GetDeviceInfoFunc)(const base::FilePath& device_path, |
| 33 std::string* unique_id, | 35 std::string* unique_id, |
| 34 string16* name, | 36 string16* name, |
| 35 bool* removable, | 37 bool* removable, |
| 36 uint64* partition_size_in_bytes); | 38 uint64* partition_size_in_bytes); |
| 37 | 39 |
| 38 namespace chrome { | 40 namespace chrome { |
| 39 | 41 |
| 40 class RemovableDeviceNotificationsLinux | 42 class RemovableDeviceNotificationsLinux |
| 41 : public RemovableStorageNotifications, | 43 : public RemovableStorageNotifications, |
| 42 public base::RefCountedThreadSafe<RemovableDeviceNotificationsLinux, | 44 public base::RefCountedThreadSafe<RemovableDeviceNotificationsLinux, |
| 43 content::BrowserThread::DeleteOnFileThread> { | 45 content::BrowserThread::DeleteOnFileThread> { |
| 44 public: | 46 public: |
| 45 // Should only be called by browser start up code. Use GetInstance() instead. | 47 // Should only be called by browser start up code. Use GetInstance() instead. |
| 46 explicit RemovableDeviceNotificationsLinux(const FilePath& path); | 48 explicit RemovableDeviceNotificationsLinux(const base::FilePath& path); |
| 47 | 49 |
| 48 // Must be called for RemovableDeviceNotificationsLinux to work. | 50 // Must be called for RemovableDeviceNotificationsLinux to work. |
| 49 void Init(); | 51 void Init(); |
| 50 | 52 |
| 51 // Finds the device that contains |path| and populates |device_info|. | 53 // Finds the device that contains |path| and populates |device_info|. |
| 52 // Returns false if unable to find the device. | 54 // Returns false if unable to find the device. |
| 53 virtual bool GetDeviceInfoForPath( | 55 virtual bool GetDeviceInfoForPath( |
| 54 const FilePath& path, | 56 const base::FilePath& path, |
| 55 StorageInfo* device_info) const OVERRIDE; | 57 StorageInfo* device_info) const OVERRIDE; |
| 56 | 58 |
| 57 // Returns the storage partition size of the device present at |location|. | 59 // Returns the storage partition size of the device present at |location|. |
| 58 // If the requested information is unavailable, returns 0. | 60 // If the requested information is unavailable, returns 0. |
| 59 virtual uint64 GetStorageSize(const std::string& location) const OVERRIDE; | 61 virtual uint64 GetStorageSize(const std::string& location) const OVERRIDE; |
| 60 | 62 |
| 61 protected: | 63 protected: |
| 62 // Only for use in unit tests. | 64 // Only for use in unit tests. |
| 63 RemovableDeviceNotificationsLinux(const FilePath& path, | 65 RemovableDeviceNotificationsLinux(const base::FilePath& path, |
| 64 GetDeviceInfoFunc getDeviceInfo); | 66 GetDeviceInfoFunc getDeviceInfo); |
| 65 | 67 |
| 66 // Avoids code deleting the object while there are references to it. | 68 // Avoids code deleting the object while there are references to it. |
| 67 // Aside from the base::RefCountedThreadSafe friend class, and derived | 69 // Aside from the base::RefCountedThreadSafe friend class, and derived |
| 68 // classes, any attempts to call this dtor will result in a compile-time | 70 // classes, any attempts to call this dtor will result in a compile-time |
| 69 // error. | 71 // error. |
| 70 virtual ~RemovableDeviceNotificationsLinux(); | 72 virtual ~RemovableDeviceNotificationsLinux(); |
| 71 | 73 |
| 72 virtual void OnFilePathChanged(const FilePath& path, bool error); | 74 virtual void OnFilePathChanged(const base::FilePath& path, bool error); |
| 73 | 75 |
| 74 private: | 76 private: |
| 75 friend class base::RefCountedThreadSafe<RemovableDeviceNotificationsLinux>; | 77 friend class base::RefCountedThreadSafe<RemovableDeviceNotificationsLinux>; |
| 76 friend class base::DeleteHelper<RemovableDeviceNotificationsLinux>; | 78 friend class base::DeleteHelper<RemovableDeviceNotificationsLinux>; |
| 77 friend struct content::BrowserThread::DeleteOnThread< | 79 friend struct content::BrowserThread::DeleteOnThread< |
| 78 content::BrowserThread::FILE>; | 80 content::BrowserThread::FILE>; |
| 79 | 81 |
| 80 // Structure to save mounted device information such as device path, unique | 82 // Structure to save mounted device information such as device path, unique |
| 81 // identifier, device name and partition size. | 83 // identifier, device name and partition size. |
| 82 struct MountPointInfo { | 84 struct MountPointInfo { |
| 83 MountPointInfo(); | 85 MountPointInfo(); |
| 84 | 86 |
| 85 FilePath mount_device; | 87 base::FilePath mount_device; |
| 86 std::string device_id; | 88 std::string device_id; |
| 87 string16 device_name; | 89 string16 device_name; |
| 88 uint64 partition_size_in_bytes; | 90 uint64 partition_size_in_bytes; |
| 89 }; | 91 }; |
| 90 | 92 |
| 91 // Mapping of mount points to MountPointInfo. | 93 // Mapping of mount points to MountPointInfo. |
| 92 typedef std::map<FilePath, MountPointInfo> MountMap; | 94 typedef std::map<base::FilePath, MountPointInfo> MountMap; |
| 93 | 95 |
| 94 // (mount point, priority) | 96 // (mount point, priority) |
| 95 // For devices that are mounted to multiple mount points, this helps us track | 97 // For devices that are mounted to multiple mount points, this helps us track |
| 96 // which one we've notified system monitor about. | 98 // which one we've notified system monitor about. |
| 97 typedef std::map<FilePath, bool> ReferencedMountPoint; | 99 typedef std::map<base::FilePath, bool> ReferencedMountPoint; |
| 98 | 100 |
| 99 // (mount device, map of known mount points) | 101 // (mount device, map of known mount points) |
| 100 // For each mount device, track the places it is mounted and which one (if | 102 // For each mount device, track the places it is mounted and which one (if |
| 101 // any) we have notified system monitor about. | 103 // any) we have notified system monitor about. |
| 102 typedef std::map<FilePath, ReferencedMountPoint> MountPriorityMap; | 104 typedef std::map<base::FilePath, ReferencedMountPoint> MountPriorityMap; |
| 103 | 105 |
| 104 // Do initialization on the File Thread. | 106 // Do initialization on the File Thread. |
| 105 void InitOnFileThread(); | 107 void InitOnFileThread(); |
| 106 | 108 |
| 107 // Parses mtab file and find all changes. | 109 // Parses mtab file and find all changes. |
| 108 void UpdateMtab(); | 110 void UpdateMtab(); |
| 109 | 111 |
| 110 // Adds |mount_device| as mounted on |mount_point|. If the device is a new | 112 // Adds |mount_device| as mounted on |mount_point|. If the device is a new |
| 111 // device any listeners are notified. | 113 // device any listeners are notified. |
| 112 void AddNewMount(const FilePath& mount_device, const FilePath& mount_point); | 114 void AddNewMount(const base::FilePath& mount_device, const base::FilePath& mou
nt_point); |
| 113 | 115 |
| 114 // Whether Init() has been called or not. | 116 // Whether Init() has been called or not. |
| 115 bool initialized_; | 117 bool initialized_; |
| 116 | 118 |
| 117 // Mtab file that lists the mount points. | 119 // Mtab file that lists the mount points. |
| 118 const FilePath mtab_path_; | 120 const base::FilePath mtab_path_; |
| 119 | 121 |
| 120 // Watcher for |mtab_path_|. | 122 // Watcher for |mtab_path_|. |
| 121 base::FilePathWatcher file_watcher_; | 123 base::FilePathWatcher file_watcher_; |
| 122 | 124 |
| 123 // Set of known file systems that we care about. | 125 // Set of known file systems that we care about. |
| 124 std::set<std::string> known_file_systems_; | 126 std::set<std::string> known_file_systems_; |
| 125 | 127 |
| 126 // Function handler to get device information. This is useful to set a mock | 128 // Function handler to get device information. This is useful to set a mock |
| 127 // handler for unit testing. | 129 // handler for unit testing. |
| 128 GetDeviceInfoFunc get_device_info_func_; | 130 GetDeviceInfoFunc get_device_info_func_; |
| 129 | 131 |
| 130 // Mapping of relevant mount points and their corresponding mount devices. | 132 // Mapping of relevant mount points and their corresponding mount devices. |
| 131 // Keep in mind on Linux, a device can be mounted at multiple mount points, | 133 // Keep in mind on Linux, a device can be mounted at multiple mount points, |
| 132 // and multiple devices can be mounted at a mount point. | 134 // and multiple devices can be mounted at a mount point. |
| 133 MountMap mount_info_map_; | 135 MountMap mount_info_map_; |
| 134 | 136 |
| 135 // Because a device can be mounted to multiple places, we only want to | 137 // Because a device can be mounted to multiple places, we only want to |
| 136 // notify about one of them. If (and only if) that one is unmounted, we need | 138 // notify about one of them. If (and only if) that one is unmounted, we need |
| 137 // to notify about it's departure and notify about another one of it's mount | 139 // to notify about it's departure and notify about another one of it's mount |
| 138 // points. | 140 // points. |
| 139 MountPriorityMap mount_priority_map_; | 141 MountPriorityMap mount_priority_map_; |
| 140 | 142 |
| 141 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationsLinux); | 143 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationsLinux); |
| 142 }; | 144 }; |
| 143 | 145 |
| 144 } // namespace chrome | 146 } // namespace chrome |
| 145 | 147 |
| 146 #endif // CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_LINUX_H_ | 148 #endif // CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_LINUX_H_ |
| OLD | NEW |