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 // MediaDeviceNotificationsLinux listens for mount point changes and notifies |
6 // the SystemMonitor about the addition and deletion of media devices. | 6 // the SystemMonitor about the addition and deletion of media devices. |
7 | 7 |
8 #ifndef CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ | 8 #ifndef CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ |
9 #define CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ | 9 #define CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ |
10 | 10 |
11 #if defined(OS_CHROMEOS) | 11 #if defined(OS_CHROMEOS) |
12 #error "Use the ChromeOS-specific implementation instead." | 12 #error "Use the ChromeOS-specific implementation instead." |
13 #endif | 13 #endif |
14 | 14 |
15 #include <map> | 15 #include <map> |
16 #include <set> | 16 #include <set> |
17 #include <string> | 17 #include <string> |
18 #include <utility> | 18 #include <utility> |
19 | 19 |
20 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
21 #include "base/compiler_specific.h" | 21 #include "base/compiler_specific.h" |
22 #include "base/files/file_path_watcher.h" | 22 #include "base/files/file_path_watcher.h" |
23 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
25 | 25 |
26 class FilePath; | 26 class FilePath; |
27 | 27 |
| 28 typedef bool (*GetDeviceInfoFunc)(const std::string& device_path, |
| 29 std::string* device_name, |
| 30 string16* device_id); |
| 31 |
28 namespace chrome { | 32 namespace chrome { |
29 | 33 |
30 class MediaDeviceNotificationsLinux | 34 class MediaDeviceNotificationsLinux |
31 : public base::RefCountedThreadSafe<MediaDeviceNotificationsLinux, | 35 : public base::RefCountedThreadSafe<MediaDeviceNotificationsLinux, |
32 content::BrowserThread::DeleteOnFileThread> { | 36 content::BrowserThread::DeleteOnFileThread> { |
33 public: | 37 public: |
34 explicit MediaDeviceNotificationsLinux(const FilePath& path); | 38 explicit MediaDeviceNotificationsLinux(const FilePath& path); |
35 | 39 |
36 // Must be called for MediaDeviceNotificationsLinux to work. | 40 // Must be called for MediaDeviceNotificationsLinux to work. |
37 void Init(); | 41 void Init(); |
38 | 42 |
39 protected: | 43 protected: |
| 44 // Only for use in unit tests. |
| 45 MediaDeviceNotificationsLinux(const FilePath& path, |
| 46 GetDeviceInfoFunc getDeviceInfo); |
| 47 |
40 // Avoids code deleting the object while there are references to it. | 48 // Avoids code deleting the object while there are references to it. |
41 // Aside from the base::RefCountedThreadSafe friend class, and derived | 49 // Aside from the base::RefCountedThreadSafe friend class, and derived |
42 // classes, any attempts to call this dtor will result in a compile-time | 50 // classes, any attempts to call this dtor will result in a compile-time |
43 // error. | 51 // error. |
44 virtual ~MediaDeviceNotificationsLinux(); | 52 virtual ~MediaDeviceNotificationsLinux(); |
45 | 53 |
46 virtual void OnFilePathChanged(const FilePath& path, bool error); | 54 virtual void OnFilePathChanged(const FilePath& path, bool error); |
47 | 55 |
48 private: | 56 private: |
49 friend class base::RefCountedThreadSafe<MediaDeviceNotificationsLinux>; | 57 friend class base::RefCountedThreadSafe<MediaDeviceNotificationsLinux>; |
50 friend class base::DeleteHelper<MediaDeviceNotificationsLinux>; | 58 friend class base::DeleteHelper<MediaDeviceNotificationsLinux>; |
51 friend struct content::BrowserThread::DeleteOnThread< | 59 friend struct content::BrowserThread::DeleteOnThread< |
52 content::BrowserThread::FILE>; | 60 content::BrowserThread::FILE>; |
53 | 61 |
54 // (mount device, device id) | 62 // Store mounted device path and id. |
55 typedef std::pair<std::string, std::string> MountDeviceAndId; | 63 struct MountDeviceAndId { |
| 64 std::string mount_device; |
| 65 std::string device_id; |
| 66 }; |
| 67 |
56 // Mapping of mount points to MountDeviceAndId. | 68 // Mapping of mount points to MountDeviceAndId. |
57 typedef std::map<std::string, MountDeviceAndId> MountMap; | 69 typedef std::map<std::string, MountDeviceAndId> MountMap; |
58 | 70 |
| 71 // (mount point, mount device) |
| 72 // Helper map to get new entries from mtab file. |
| 73 typedef std::map<std::string, std::string> MountPointDeviceMap; |
| 74 |
59 void InitOnFileThread(); | 75 void InitOnFileThread(); |
60 | 76 |
61 // Parse the mtab file and find all changes. | 77 // Parse the mtab file and find all changes. |
62 void UpdateMtab(); | 78 void UpdateMtab(); |
63 | 79 |
64 // Read the mtab file entries into |mtab|. | 80 // Read the mtab file entries into |mtab|. |
65 void ReadMtab(MountMap* mtab); | 81 void ReadMtab(MountPointDeviceMap* mtab); |
66 | 82 |
67 // Add a media device with a given device and mount device. Assign it a device | 83 // Check and add |mount_device| as media device given the |mount_point|. |
68 // id as well. | 84 void CheckAndAddMediaDevice(const std::string& mount_device, |
69 void AddNewDevice(const std::string& mount_device, | 85 const std::string& mount_point); |
70 const std::string& mount_point, | |
71 std::string* device_id); | |
72 | 86 |
73 // Remove a media device with a given device id. | 87 // Remove a media device with a given device id. |
74 void RemoveOldDevice(const std::string& device_id); | 88 void RemoveOldDevice(const std::string& device_id); |
75 | 89 |
76 // Whether Init() has been called or not. | 90 // Whether Init() has been called or not. |
77 bool initialized_; | 91 bool initialized_; |
78 | 92 |
79 // Mtab file that lists the mount points. | 93 // Mtab file that lists the mount points. |
80 const FilePath mtab_path_; | 94 const FilePath mtab_path_; |
81 | 95 |
82 // Watcher for |mtab_path_|. | 96 // Watcher for |mtab_path_|. |
83 base::files::FilePathWatcher file_watcher_; | 97 base::files::FilePathWatcher file_watcher_; |
84 | 98 |
85 // Mapping of relevant mount points and their corresponding mount devices. | 99 // Mapping of relevant mount points and their corresponding mount devices. |
86 // Keep in mind on Linux, a device can be mounted at multiple mount points, | 100 // Keep in mind on Linux, a device can be mounted at multiple mount points, |
87 // and multiple devices can be mounted at a mount point. | 101 // and multiple devices can be mounted at a mount point. |
88 MountMap mtab_; | 102 MountMap mount_info_map_; |
89 | |
90 // The lowest available device id number. | |
91 // TODO(thestig) Remove this and use a real per-device unique id instead. | |
92 int current_device_id_; | |
93 | 103 |
94 // Set of known file systems that we care about. | 104 // Set of known file systems that we care about. |
95 std::set<std::string> known_file_systems_; | 105 std::set<std::string> known_file_systems_; |
96 | 106 |
| 107 GetDeviceInfoFunc get_device_info_func_; |
| 108 |
97 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinux); | 109 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinux); |
98 }; | 110 }; |
99 | 111 |
100 } // namespace chrome | 112 } // namespace chrome |
101 | 113 |
102 #endif // CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ | 114 #endif // CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ |
OLD | NEW |