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