|
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 #include "content/browser/media_device_notifications_linux.h" | |
6 | |
7 #include <mntent.h> | |
8 #include <stdio.h> | |
9 | |
10 #include "base/file_util.h" | |
11 #include "base/string_util.h" | |
12 #include "base/system_monitor/system_monitor.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 | |
15 using base::SystemMonitor; | |
16 | |
17 namespace { | |
18 | |
19 const char* const kDCIMDirName = "DCIM"; | |
20 | |
21 // List of file systems we care about. | |
22 const char* const kKnownFileSystems[] = { | |
23 "ext2", | |
24 "ext3", | |
25 "ext4", | |
26 "fat", | |
27 "hfsplus", | |
28 "iso9660", | |
29 "msdos", | |
30 "ntfs", | |
31 "udf", | |
32 "vfat", | |
33 }; | |
34 | |
35 } // namespace | |
36 | |
37 namespace content { | |
38 | |
39 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( | |
40 const FilePath& path) | |
41 : initialized_(false), | |
42 mtab_path_(path), | |
43 current_device_id_(0U) { | |
44 CHECK(!path.empty()); | |
45 | |
46 // Put |kKnownFileSystems| in std::set to get O(log N) access time. | |
47 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) | |
48 known_file_systems_.insert(kKnownFileSystems[i]); | |
49 } | |
50 | |
51 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { | |
52 } | |
53 | |
54 void MediaDeviceNotificationsLinux::InitOnFileThread() { | |
55 DCHECK(!initialized_); | |
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
57 initialized_ = true; | |
58 | |
59 watcher_delegate_ = new WatcherDelegate(this); | |
60 if (!file_watcher_.Watch(mtab_path_, watcher_delegate_)) { | |
61 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; | |
62 return; | |
63 } | |
64 | |
65 UpdateMtab(); | |
66 } | |
67 | |
68 void MediaDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path) { | |
69 if (path != mtab_path_) { | |
70 NOTREACHED(); | |
71 return; | |
72 } | |
73 | |
74 UpdateMtab(); | |
75 } | |
76 | |
77 void MediaDeviceNotificationsLinux::UpdateMtab() { | |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
79 | |
80 MountMap new_mtab; | |
81 ReadMtab(&new_mtab); | |
82 | |
83 // Check existing mtab entries for unaccounted mount points. | |
84 // These mount points must have been removed in the new mtab. | |
85 MountMap::iterator it = mtab_.begin(); | |
86 while (it != mtab_.end()) { | |
87 const MountPoint& mount_point(it->first); | |
88 // |mount_point| not in |new_mtab|. | |
89 if (new_mtab.find(mount_point) == new_mtab.end()) { | |
90 const SystemMonitor::DeviceIdType& device_id(it->second.second); | |
91 RemoveOldDevice(device_id); | |
92 mtab_.erase(it++); | |
93 continue; | |
94 } | |
95 // Existing mount point. Ignore and deal in the next loop. | |
96 ++it; | |
97 } | |
98 | |
99 // Check new mtab entries against existing ones. | |
100 for (MountMap::iterator newiter = new_mtab.begin(); | |
101 newiter != new_mtab.end(); | |
102 ++newiter) { | |
103 const MountPoint& mount_point(newiter->first); | |
104 MountDeviceAndId& mount_device_and_id(newiter->second); | |
105 MountMap::iterator olditer = mtab_.find(mount_point); | |
106 // Check to see if it is a new mount point. | |
107 if (olditer == mtab_.end()) { | |
108 if (IsNewDeviceAMediaDevice(mount_point, &mount_device_and_id.second)) { | |
109 AddNewDevice(mount_device_and_id, mount_point); | |
110 mtab_[mount_point] = mount_device_and_id; | |
111 } | |
112 continue; | |
113 } | |
114 | |
115 // Existing mount point. Check to see if a new device is mounted there. | |
116 MountDeviceAndId& old_mount_device_and_id(olditer->second); | |
117 if (mount_device_and_id.first == old_mount_device_and_id.first) | |
118 continue; | |
119 | |
120 // New device mounted. | |
121 RemoveOldDevice(old_mount_device_and_id.second); | |
122 if (IsNewDeviceAMediaDevice(mount_point, &mount_device_and_id.second)) { | |
123 AddNewDevice(mount_device_and_id, mount_point); | |
124 olditer->second = mount_device_and_id; | |
125 } | |
126 } | |
127 } | |
128 | |
129 void MediaDeviceNotificationsLinux::ReadMtab(MountMap* mtab) { | |
130 FILE* fp = setmntent(mtab_path_.value().c_str(), "r"); | |
131 if (!fp) | |
132 return; | |
133 | |
134 MountMap& new_mtab = *mtab; | |
135 struct mntent entry; | |
136 char buf[512]; | |
137 SystemMonitor::DeviceIdType mount_position = 0; | |
138 typedef std::pair<MountPoint, SystemMonitor::DeviceIdType> MountPointAndId; | |
139 typedef std::map<MountDevice, MountPointAndId> DeviceMap; | |
140 DeviceMap device_map; | |
141 while (getmntent_r(fp, &entry, buf, sizeof(buf))) { | |
142 // We only care about real file systems. | |
143 if (known_file_systems_.find(entry.mnt_type) == known_file_systems_.end()) | |
144 continue; | |
145 // Add entries, but overwrite entries for the same mount device. Keep track | |
146 // of the entry positions in the device id field and use that below to | |
147 // resolve multiple devices mounted at the same mount point. | |
148 device_map[entry.mnt_fsname] = | |
149 std::make_pair(entry.mnt_dir, mount_position++); | |
150 } | |
151 endmntent(fp); | |
152 | |
153 for (DeviceMap::iterator device_it = device_map.begin(); | |
154 device_it != device_map.end(); | |
155 ++device_it) { | |
156 const MountDevice& device = device_it->first; | |
157 const MountPoint& mount_point = device_it->second.first; | |
158 const SystemMonitor::DeviceIdType& position = device_it->second.second; | |
159 | |
160 // No device at |mount_point|, save |device| to it. | |
161 MountMap::iterator mount_it = new_mtab.find(mount_point); | |
162 if (mount_it == new_mtab.end()) { | |
163 new_mtab[mount_point] = std::make_pair(device, position); | |
164 continue; | |
165 } | |
166 | |
167 // There is already a device mounted at |mount_point|. Check to see if | |
168 // the existing mount entry is newer than the current one. | |
169 MountDevice& existing_device = mount_it->second.first; | |
170 SystemMonitor::DeviceIdType& existing_position = mount_it->second.second; | |
171 if (existing_position > position) | |
172 continue; | |
173 | |
174 // The current entry is newer, update the mount point entry. | |
175 existing_device = device; | |
176 existing_position = position; | |
177 } | |
178 } | |
179 | |
180 bool MediaDeviceNotificationsLinux::IsNewDeviceAMediaDevice( | |
vandebo (ex-Chrome)
2012/03/06 19:36:14
nit: IsMediaDevice?
Lei Zhang
2012/03/06 20:23:16
Done.
| |
181 const MountPoint& mount_point, | |
182 base::SystemMonitor::DeviceIdType* device_id) { | |
183 FilePath dcim_path(mount_point); | |
184 FilePath::StringType dcim_dir(kDCIMDirName); | |
185 if (!file_util::DirectoryExists(dcim_path.Append(dcim_dir))) { | |
186 // Check for lowercase 'dcim' as well. | |
187 FilePath dcim_path_lower(dcim_path.Append(StringToLowerASCII(dcim_dir))); | |
188 if (!file_util::DirectoryExists(dcim_path_lower)) { | |
189 return false; | |
190 } | |
191 } | |
192 *device_id = current_device_id_++; | |
vandebo (ex-Chrome)
2012/03/06 19:36:14
nit: maybe set device_id in AddNewDevice and retur
Lei Zhang
2012/03/06 20:23:16
I had it like that before, but having an explicit
| |
193 return true; | |
194 } | |
195 | |
196 void MediaDeviceNotificationsLinux::AddNewDevice( | |
197 const MountDeviceAndId& device_and_id, | |
198 const MountPoint& mount_point) { | |
199 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | |
200 system_monitor->ProcessMediaDeviceAttached(device_and_id.second, | |
201 device_and_id.first, | |
202 FilePath(mount_point)); | |
203 } | |
204 | |
205 void MediaDeviceNotificationsLinux::RemoveOldDevice( | |
206 const base::SystemMonitor::DeviceIdType& device_id) { | |
207 base::SystemMonitor* system_monitor = base::SystemMonitor::Get(); | |
208 system_monitor->ProcessMediaDeviceDetached(device_id); | |
209 } | |
210 | |
211 MediaDeviceNotificationsLinux::WatcherDelegate::WatcherDelegate( | |
212 MediaDeviceNotificationsLinux* notifier) | |
213 : notifier_(notifier) { | |
214 } | |
215 | |
216 MediaDeviceNotificationsLinux::WatcherDelegate::~WatcherDelegate() { | |
217 } | |
218 | |
219 void MediaDeviceNotificationsLinux::WatcherDelegate::OnFilePathChanged( | |
220 const FilePath& path) { | |
221 notifier_->OnFilePathChanged(path); | |
222 } | |
223 | |
224 } // namespace content | |
OLD | NEW |