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