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 implementation. | |
6 | |
7 #include "content/browser/media_device_notifications_linux.h" | |
8 | |
9 #include <mntent.h> | |
10 #include <stdio.h> | |
11 | |
12 #include <vector> | |
13 | |
14 #include "base/bind.h" | |
15 #include "base/file_path.h" | |
16 #include "base/file_util.h" | |
17 #include "base/string_util.h" | |
18 #include "base/system_monitor/system_monitor.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 | |
21 namespace { | |
22 | |
23 const char kDCIMDirName[] = "DCIM"; | |
24 | |
25 // List of file systems we care about. | |
26 const char* const kKnownFileSystems[] = { | |
27 "ext2", | |
28 "ext3", | |
29 "ext4", | |
30 "fat", | |
31 "hfsplus", | |
32 "iso9660", | |
33 "msdos", | |
34 "ntfs", | |
35 "udf", | |
36 "vfat", | |
37 }; | |
38 | |
39 } // namespace | |
40 | |
41 namespace content { | |
42 | |
43 using base::SystemMonitor; | |
44 | |
45 // A simple pass-through class. MediaDeviceNotificationsLinux cannot directly | |
46 // inherit from FilePathWatcher::Delegate due to multiple inheritance. | |
47 class MediaDeviceNotificationsLinux::WatcherDelegate | |
48 : public base::files::FilePathWatcher::Delegate { | |
49 public: | |
50 explicit WatcherDelegate(MediaDeviceNotificationsLinux* notifier); | |
51 | |
52 // base::files::FilePathWatcher::Delegate implementation. | |
53 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE; | |
54 | |
55 private: | |
56 friend class base::RefCountedThreadSafe<WatcherDelegate>; | |
57 | |
58 // Avoids code deleting the object while there are references to it. | |
59 // Aside from the base::RefCountedThreadSafe friend class, any attempts to | |
60 // call this dtor will result in a compile-time error. | |
61 virtual ~WatcherDelegate(); | |
62 | |
63 // The MediaDeviceNotificationsLinux instance that owns this WatcherDelegate. | |
64 // Since |notifier_| will destroy this WatcherDelegate before it goes away, | |
65 // the pointer is always valid. No need to add a reference count, as that | |
66 // would create a circular reference. | |
67 MediaDeviceNotificationsLinux* const notifier_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(WatcherDelegate); | |
70 }; | |
71 | |
72 MediaDeviceNotificationsLinux::WatcherDelegate::WatcherDelegate( | |
73 MediaDeviceNotificationsLinux* notifier) | |
74 : notifier_(notifier) { | |
75 } | |
76 | |
77 MediaDeviceNotificationsLinux::WatcherDelegate::~WatcherDelegate() { | |
78 } | |
79 | |
80 void MediaDeviceNotificationsLinux::WatcherDelegate::OnFilePathChanged( | |
81 const FilePath& path) { | |
82 notifier_->OnFilePathChanged(path); | |
83 } | |
84 | |
85 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( | |
86 const FilePath& path) | |
87 : initialized_(false), | |
88 mtab_path_(path), | |
89 current_device_id_(0U) { | |
90 CHECK(!path.empty()); | |
91 | |
92 // Put |kKnownFileSystems| in std::set to get O(log N) access time. | |
93 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) { | |
94 known_file_systems_.insert(kKnownFileSystems[i]); | |
95 } | |
96 } | |
97 | |
98 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { | |
99 } | |
100 | |
101 void MediaDeviceNotificationsLinux::Init() { | |
102 BrowserThread::PostTask( | |
103 BrowserThread::FILE, FROM_HERE, | |
104 base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread, this)); | |
105 } | |
106 | |
107 void MediaDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path) { | |
108 if (path != mtab_path_) { | |
109 // This cannot happen unless FileWatcher is buggy. Just ignore this | |
110 // notification and do nothing. | |
111 NOTREACHED(); | |
112 return; | |
113 } | |
114 | |
115 UpdateMtab(); | |
116 } | |
117 | |
118 void MediaDeviceNotificationsLinux::InitOnFileThread() { | |
119 DCHECK(!initialized_); | |
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
121 initialized_ = true; | |
122 | |
123 watcher_delegate_ = new WatcherDelegate(this); | |
124 if (!file_watcher_.Watch(mtab_path_, watcher_delegate_)) { | |
125 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; | |
126 return; | |
127 } | |
128 | |
129 UpdateMtab(); | |
130 } | |
131 | |
132 void MediaDeviceNotificationsLinux::UpdateMtab() { | |
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
134 | |
135 MountMap new_mtab; | |
136 ReadMtab(&new_mtab); | |
137 | |
138 // Check existing mtab entries for unaccounted mount points. | |
139 // These mount points must have been removed in the new mtab. | |
140 std::vector<std::string> mount_points_to_erase; | |
141 for (MountMap::const_iterator it = mtab_.begin(); it != mtab_.end(); ++it) { | |
142 const std::string& mount_point = it->first; | |
143 // |mount_point| not in |new_mtab|. | |
144 if (new_mtab.find(mount_point) == new_mtab.end()) { | |
145 const SystemMonitor::DeviceIdType& device_id = it->second.second; | |
146 RemoveOldDevice(device_id); | |
147 mount_points_to_erase.push_back(mount_point); | |
148 } | |
149 } | |
150 // Erase the |mtab_| entries afterwards. Erasing in the loop above using the | |
151 // iterator is slightly more efficient, but more tricky, since calling | |
152 // std::map::erase() on an iterator invalidates it. | |
153 for (size_t i = 0; i < mount_points_to_erase.size(); ++i) | |
154 mtab_.erase(mount_points_to_erase[i]); | |
155 | |
156 // Check new mtab entries against existing ones. | |
157 for (MountMap::iterator newiter = new_mtab.begin(); | |
158 newiter != new_mtab.end(); | |
159 ++newiter) { | |
160 const std::string& mount_point = newiter->first; | |
161 const MountDeviceAndId& mount_device_and_id = newiter->second; | |
162 const std::string& mount_device = mount_device_and_id.first; | |
163 SystemMonitor::DeviceIdType& id = newiter->second.second; | |
164 MountMap::iterator olditer = mtab_.find(mount_point); | |
165 // Check to see if it is a new mount point. | |
166 if (olditer == mtab_.end()) { | |
167 if (IsMediaDevice(mount_point)) { | |
168 AddNewDevice(mount_device, mount_point, &id); | |
169 mtab_.insert(std::make_pair(mount_point, mount_device_and_id)); | |
170 } | |
171 continue; | |
172 } | |
173 | |
174 // Existing mount point. Check to see if a new device is mounted there. | |
175 const MountDeviceAndId& old_mount_device_and_id = olditer->second; | |
176 if (mount_device == old_mount_device_and_id.first) | |
177 continue; | |
178 | |
179 // New device mounted. | |
180 RemoveOldDevice(old_mount_device_and_id.second); | |
181 if (IsMediaDevice(mount_point)) { | |
182 AddNewDevice(mount_device, mount_point, &id); | |
183 olditer->second = mount_device_and_id; | |
184 } | |
185 } | |
186 } | |
187 | |
188 void MediaDeviceNotificationsLinux::ReadMtab(MountMap* mtab) { | |
189 FILE* fp = setmntent(mtab_path_.value().c_str(), "r"); | |
190 if (!fp) | |
191 return; | |
192 | |
193 MountMap& new_mtab = *mtab; | |
194 mntent entry; | |
195 char buf[512]; | |
196 SystemMonitor::DeviceIdType mount_position = 0; | |
197 typedef std::pair<std::string, SystemMonitor::DeviceIdType> MountPointAndId; | |
198 typedef std::map<std::string, MountPointAndId> DeviceMap; | |
199 DeviceMap device_map; | |
200 while (getmntent_r(fp, &entry, buf, sizeof(buf))) { | |
201 // We only care about real file systems. | |
202 if (known_file_systems_.find(entry.mnt_type) == known_file_systems_.end()) | |
203 continue; | |
204 // Add entries, but overwrite entries for the same mount device. Keep track | |
205 // of the entry positions in the device id field and use that below to | |
206 // resolve multiple devices mounted at the same mount point. | |
207 MountPointAndId mount_point_and_id = | |
208 std::make_pair(entry.mnt_dir, mount_position++); | |
209 DeviceMap::iterator it = device_map.find(entry.mnt_fsname); | |
210 if (it == device_map.end()) { | |
211 device_map.insert(it, | |
212 std::make_pair(entry.mnt_fsname, mount_point_and_id)); | |
213 } else { | |
214 it->second = mount_point_and_id; | |
215 } | |
216 } | |
217 endmntent(fp); | |
218 | |
219 for (DeviceMap::const_iterator device_it = device_map.begin(); | |
220 device_it != device_map.end(); | |
221 ++device_it) { | |
222 const std::string& device = device_it->first; | |
223 const std::string& mount_point = device_it->second.first; | |
224 const SystemMonitor::DeviceIdType& position = device_it->second.second; | |
225 | |
226 // No device at |mount_point|, save |device| to it. | |
227 MountMap::iterator mount_it = new_mtab.find(mount_point); | |
228 if (mount_it == new_mtab.end()) { | |
229 new_mtab.insert(std::make_pair(mount_point, | |
230 std::make_pair(device, position))); | |
231 continue; | |
232 } | |
233 | |
234 // There is already a device mounted at |mount_point|. Check to see if | |
235 // the existing mount entry is newer than the current one. | |
236 std::string& existing_device = mount_it->second.first; | |
237 SystemMonitor::DeviceIdType& existing_position = mount_it->second.second; | |
238 if (existing_position > position) | |
239 continue; | |
240 | |
241 // The current entry is newer, update the mount point entry. | |
242 existing_device = device; | |
243 existing_position = position; | |
244 } | |
245 } | |
246 | |
247 bool MediaDeviceNotificationsLinux::IsMediaDevice( | |
248 const std::string& mount_point) { | |
249 FilePath dcim_path(mount_point); | |
250 FilePath::StringType dcim_dir = kDCIMDirName; | |
251 if (!file_util::DirectoryExists(dcim_path.Append(dcim_dir))) { | |
252 // Check for lowercase 'dcim' as well. | |
253 FilePath dcim_path_lower(dcim_path.Append(StringToLowerASCII(dcim_dir))); | |
254 if (!file_util::DirectoryExists(dcim_path_lower)) { | |
255 return false; | |
256 } | |
257 } | |
258 return true; | |
259 } | |
260 | |
261 void MediaDeviceNotificationsLinux::AddNewDevice( | |
262 const std::string& mount_device, | |
263 const std::string& mount_point, | |
264 base::SystemMonitor::DeviceIdType* device_id) { | |
265 *device_id = current_device_id_++; | |
266 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | |
267 system_monitor->ProcessMediaDeviceAttached(*device_id, | |
268 mount_device, | |
269 FilePath(mount_point)); | |
270 } | |
271 | |
272 void MediaDeviceNotificationsLinux::RemoveOldDevice( | |
273 const base::SystemMonitor::DeviceIdType& device_id) { | |
274 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | |
275 system_monitor->ProcessMediaDeviceDetached(device_id); | |
276 } | |
277 | |
278 } // namespace content | |
OLD | NEW |