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