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 // Gets the media device information given a |device_path|. On success, |
| 29 // returns true and fills in |device_name| and |device_id|. |
| 30 typedef bool (*GetDeviceInfoFunc)(const std::string& device_path, |
| 31 std::string* device_name, |
| 32 string16* device_id); |
| 33 |
28 namespace chrome { | 34 namespace chrome { |
29 | 35 |
30 class MediaDeviceNotificationsLinux | 36 class MediaDeviceNotificationsLinux |
31 : public base::RefCountedThreadSafe<MediaDeviceNotificationsLinux, | 37 : public base::RefCountedThreadSafe<MediaDeviceNotificationsLinux, |
32 content::BrowserThread::DeleteOnFileThread> { | 38 content::BrowserThread::DeleteOnFileThread> { |
33 public: | 39 public: |
34 explicit MediaDeviceNotificationsLinux(const FilePath& path); | 40 explicit MediaDeviceNotificationsLinux(const FilePath& path); |
35 | 41 |
36 // Must be called for MediaDeviceNotificationsLinux to work. | 42 // Must be called for MediaDeviceNotificationsLinux to work. |
37 void Init(); | 43 void Init(); |
38 | 44 |
39 protected: | 45 protected: |
| 46 // Only for use in unit tests. |
| 47 MediaDeviceNotificationsLinux(const FilePath& path, |
| 48 GetDeviceInfoFunc getDeviceInfo); |
| 49 |
40 // Avoids code deleting the object while there are references to it. | 50 // Avoids code deleting the object while there are references to it. |
41 // Aside from the base::RefCountedThreadSafe friend class, and derived | 51 // Aside from the base::RefCountedThreadSafe friend class, and derived |
42 // classes, any attempts to call this dtor will result in a compile-time | 52 // classes, any attempts to call this dtor will result in a compile-time |
43 // error. | 53 // error. |
44 virtual ~MediaDeviceNotificationsLinux(); | 54 virtual ~MediaDeviceNotificationsLinux(); |
45 | 55 |
46 virtual void OnFilePathChanged(const FilePath& path, bool error); | 56 virtual void OnFilePathChanged(const FilePath& path, bool error); |
47 | 57 |
48 private: | 58 private: |
49 friend class base::RefCountedThreadSafe<MediaDeviceNotificationsLinux>; | 59 friend class base::RefCountedThreadSafe<MediaDeviceNotificationsLinux>; |
50 friend class base::DeleteHelper<MediaDeviceNotificationsLinux>; | 60 friend class base::DeleteHelper<MediaDeviceNotificationsLinux>; |
51 friend struct content::BrowserThread::DeleteOnThread< | 61 friend struct content::BrowserThread::DeleteOnThread< |
52 content::BrowserThread::FILE>; | 62 content::BrowserThread::FILE>; |
53 | 63 |
54 // (mount device, device id) | 64 // Structure to save mounted device information such as device path and unique |
55 typedef std::pair<std::string, std::string> MountDeviceAndId; | 65 // identifier. |
| 66 struct MountDeviceAndId { |
| 67 std::string mount_device; |
| 68 std::string device_id; |
| 69 }; |
| 70 |
56 // Mapping of mount points to MountDeviceAndId. | 71 // Mapping of mount points to MountDeviceAndId. |
57 typedef std::map<std::string, MountDeviceAndId> MountMap; | 72 typedef std::map<std::string, MountDeviceAndId> MountMap; |
58 | 73 |
| 74 // (mount point, mount device) |
| 75 // Helper Map to get new entries from mtab file. |
| 76 typedef std::map<std::string, std::string> MountPointDeviceMap; |
| 77 |
59 void InitOnFileThread(); | 78 void InitOnFileThread(); |
60 | 79 |
61 // Parse the mtab file and find all changes. | 80 // Parses mtab file and find all changes. |
62 void UpdateMtab(); | 81 void UpdateMtab(); |
63 | 82 |
64 // Read the mtab file entries into |mtab|. | 83 // Reads mtab file entries into |mtab|. |
65 void ReadMtab(MountMap* mtab); | 84 void ReadMtab(MountPointDeviceMap* mtab); |
66 | 85 |
67 // Add a media device with a given device and mount device. Assign it a device | 86 // Checks and adds |mount_device| as media device given the |mount_point|. |
68 // id as well. | 87 void CheckAndAddMediaDevice(const std::string& mount_device, |
69 void AddNewDevice(const std::string& mount_device, | 88 const std::string& mount_point); |
70 const std::string& mount_point, | |
71 std::string* device_id); | |
72 | 89 |
73 // Remove a media device with a given device id. | 90 // Removes media device with a given device id. |
74 void RemoveOldDevice(const std::string& device_id); | 91 void RemoveOldDevice(const std::string& device_id); |
75 | 92 |
76 // Whether Init() has been called or not. | 93 // Whether Init() has been called or not. |
77 bool initialized_; | 94 bool initialized_; |
78 | 95 |
79 // Mtab file that lists the mount points. | 96 // Mtab file that lists the mount points. |
80 const FilePath mtab_path_; | 97 const FilePath mtab_path_; |
81 | 98 |
82 // Watcher for |mtab_path_|. | 99 // Watcher for |mtab_path_|. |
83 base::files::FilePathWatcher file_watcher_; | 100 base::files::FilePathWatcher file_watcher_; |
84 | 101 |
85 // Mapping of relevant mount points and their corresponding mount devices. | 102 // 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, | 103 // 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. | 104 // and multiple devices can be mounted at a mount point. |
88 MountMap mtab_; | 105 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 | 106 |
94 // Set of known file systems that we care about. | 107 // Set of known file systems that we care about. |
95 std::set<std::string> known_file_systems_; | 108 std::set<std::string> known_file_systems_; |
96 | 109 |
| 110 // Function handler to get device information. This is useful to set a mock |
| 111 // handler for unit testing. |
| 112 GetDeviceInfoFunc get_device_info_func_; |
| 113 |
97 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinux); | 114 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinux); |
98 }; | 115 }; |
99 | 116 |
100 } // namespace chrome | 117 } // namespace chrome |
101 | 118 |
102 #endif // CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ | 119 #endif // CHROME_BROWSER_MEDIA_GALLERY_MEDIA_DEVICE_NOTIFICATIONS_LINUX_H_ |
OLD | NEW |