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