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