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 implementation. | 5 // RemovableDeviceNotificationsLinux implementation. |
6 | 6 |
7 #include "chrome/browser/media_gallery/media_device_notifications_linux.h" | 7 #include "chrome/browser/media_gallery/removable_device_notifications_linux.h" |
8 | 8 |
9 #include <libudev.h> | 9 #include <libudev.h> |
10 #include <mntent.h> | 10 #include <mntent.h> |
11 #include <stdio.h> | 11 #include <stdio.h> |
12 | 12 |
13 #include <vector> | 13 #include <list> |
14 | 14 |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/file_path.h" | 16 #include "base/file_path.h" |
17 #include "base/memory/scoped_generic_obj.h" | 17 #include "base/memory/scoped_generic_obj.h" |
18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
20 #include "base/string_number_conversions.h" | 20 #include "base/string_number_conversions.h" |
21 #include "base/string_util.h" | 21 #include "base/string_util.h" |
22 #include "base/stringprintf.h" | 22 #include "base/stringprintf.h" |
23 #include "base/system_monitor/system_monitor.h" | 23 #include "base/system_monitor/system_monitor.h" |
24 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
25 #include "chrome/browser/media_gallery/media_device_notifications_utils.h" | 25 #include "chrome/browser/media_gallery/media_device_notifications_utils.h" |
26 #include "chrome/browser/media_gallery/media_gallery_constants.h" | 26 #include "chrome/browser/media_gallery/media_gallery_constants.h" |
27 #include "chrome/browser/media_gallery/media_storage_util.h" | 27 #include "chrome/browser/media_gallery/media_storage_util.h" |
28 | 28 |
29 namespace chrome { | 29 namespace chrome { |
30 | 30 |
31 using base::SystemMonitor; | 31 using base::SystemMonitor; |
32 using content::BrowserThread; | 32 using content::BrowserThread; |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 static RemovableDeviceNotificationsLinux* | |
37 g_removable_device_notifications_linux = NULL; | |
38 | |
36 // List of file systems we care about. | 39 // List of file systems we care about. |
37 const char* const kKnownFileSystems[] = { | 40 const char* const kKnownFileSystems[] = { |
38 "ext2", | 41 "ext2", |
39 "ext3", | 42 "ext3", |
40 "ext4", | 43 "ext4", |
41 "fat", | 44 "fat", |
42 "hfsplus", | 45 "hfsplus", |
43 "iso9660", | 46 "iso9660", |
44 "msdos", | 47 "msdos", |
45 "ntfs", | 48 "ntfs", |
46 "udf", | 49 "udf", |
47 "vfat", | 50 "vfat", |
48 }; | 51 }; |
49 | 52 |
50 // udev device property constants. | 53 // udev device property constants. |
54 const char kBlockSubsystemKey[] = "block"; | |
51 const char kDevName[] = "DEVNAME"; | 55 const char kDevName[] = "DEVNAME"; |
56 const char kDiskDeviceTypeKey[] = "disk"; | |
52 const char kFsUUID[] = "ID_FS_UUID"; | 57 const char kFsUUID[] = "ID_FS_UUID"; |
53 const char kLabel[] = "ID_FS_LABEL"; | 58 const char kLabel[] = "ID_FS_LABEL"; |
54 const char kModel[] = "ID_MODEL"; | 59 const char kModel[] = "ID_MODEL"; |
55 const char kModelID[] = "ID_MODEL_ID"; | 60 const char kModelID[] = "ID_MODEL_ID"; |
61 const char kRemovableSysAttr[] = "removable"; | |
56 const char kSerial[] = "ID_SERIAL"; | 62 const char kSerial[] = "ID_SERIAL"; |
57 const char kSerialShort[] = "ID_SERIAL_SHORT"; | 63 const char kSerialShort[] = "ID_SERIAL_SHORT"; |
58 const char kVendor[] = "ID_VENDOR"; | 64 const char kVendor[] = "ID_VENDOR"; |
59 const char kVendorID[] = "ID_VENDOR_ID"; | 65 const char kVendorID[] = "ID_VENDOR_ID"; |
60 | 66 |
61 // Device mount point details. | 67 // (mount point, mount device) |
62 struct MountPointEntryInfo { | 68 // A mapping from mount point to mount device, as extracted from the mtab |
63 std::string mount_point; | 69 // file. |
64 int entry_pos; | 70 typedef std::map<FilePath, FilePath> MountPointDeviceMap; |
65 }; | 71 |
72 // Reads mtab file entries into |mtab|. | |
73 void ReadMtab(const FilePath& mtab_path, | |
74 const std::set<std::string>& interesting_file_systems, | |
75 MountPointDeviceMap* mtab) { | |
76 mtab->clear(); | |
77 | |
78 FILE* fp = setmntent(mtab_path.value().c_str(), "r"); | |
79 if (!fp) | |
80 return; | |
81 | |
82 mntent entry; | |
83 char buf[512]; | |
84 | |
85 // We return the same device mounted to multiple locations, but hide | |
86 // devices that have been mounted over. | |
87 while (getmntent_r(fp, &entry, buf, sizeof(buf))) { | |
88 // We only care about real file systems. | |
89 if (!ContainsKey(interesting_file_systems, entry.mnt_type)) | |
90 continue; | |
91 | |
92 (*mtab)[FilePath(entry.mnt_dir)] = FilePath(entry.mnt_fsname); | |
93 } | |
94 endmntent(fp); | |
95 } | |
66 | 96 |
67 // ScopedGenericObj functor for UdevObjectRelease(). | 97 // ScopedGenericObj functor for UdevObjectRelease(). |
68 class ScopedReleaseUdevObject { | 98 class ScopedReleaseUdevObject { |
69 public: | 99 public: |
70 void operator()(struct udev* udev) const { | 100 void operator()(struct udev* udev) const { |
71 udev_unref(udev); | 101 udev_unref(udev); |
72 } | 102 } |
73 }; | 103 }; |
74 typedef ScopedGenericObj<struct udev*, | 104 typedef ScopedGenericObj<struct udev*, |
75 ScopedReleaseUdevObject> ScopedUdevObject; | 105 ScopedReleaseUdevObject> ScopedUdevObject; |
(...skipping 11 matching lines...) Expand all Loading... | |
87 // Wrapper function for udev_device_get_property_value() that also checks for | 117 // Wrapper function for udev_device_get_property_value() that also checks for |
88 // valid but empty values. | 118 // valid but empty values. |
89 std::string GetUdevDevicePropertyValue(struct udev_device* udev_device, | 119 std::string GetUdevDevicePropertyValue(struct udev_device* udev_device, |
90 const char* key) { | 120 const char* key) { |
91 const char* value = udev_device_get_property_value(udev_device, key); | 121 const char* value = udev_device_get_property_value(udev_device, key); |
92 if (!value) | 122 if (!value) |
93 return std::string(); | 123 return std::string(); |
94 return (strlen(value) > 0) ? value : std::string(); | 124 return (strlen(value) > 0) ? value : std::string(); |
95 } | 125 } |
96 | 126 |
127 // Construct a device id using label or manufacturer (vendor and model) details. | |
128 std::string MakeDeviceUniqueId(struct udev_device* device) { | |
129 std::string uuid = GetUdevDevicePropertyValue(device, kFsUUID); | |
130 if (!uuid.empty()) | |
131 return kFSUniqueIdPrefix + uuid; | |
132 | |
133 // If one of the vendor, model, serial information is missing, its value | |
134 // in the string is empty. | |
135 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialShortInfo | |
136 // E.g.: VendorModelSerial:Kn:DataTravel_12.10:8000000000006CB02CDB | |
137 std::string vendor = GetUdevDevicePropertyValue(device, kVendorID); | |
138 std::string model = GetUdevDevicePropertyValue(device, kModelID); | |
139 std::string serial_short = GetUdevDevicePropertyValue(device, | |
140 kSerialShort); | |
141 if (vendor.empty() && model.empty() && serial_short.empty()) | |
142 return std::string(); | |
143 | |
144 return base::StringPrintf("%s%s%s%s%s%s", | |
145 kVendorModelSerialPrefix, | |
146 vendor.c_str(), kNonSpaceDelim, | |
147 model.c_str(), kNonSpaceDelim, | |
148 serial_short.c_str()); | |
149 } | |
150 | |
97 // Get the device information using udev library. | 151 // Get the device information using udev library. |
98 // On success, returns true and fill in |id| and |name|. | 152 // On success, returns true and fill in |unique_id|, |name|, and |removable|. |
99 bool GetDeviceInfo(const std::string& device_path, std::string* id, | 153 bool GetDeviceInfo(const FilePath& device_path, std::string* unique_id, |
100 string16* name) { | 154 string16* name, bool* removable) { |
101 DCHECK(!device_path.empty()); | 155 DCHECK(!device_path.empty()); |
102 | 156 |
103 ScopedUdevObject udev_obj(udev_new()); | 157 ScopedUdevObject udev_obj(udev_new()); |
104 if (!udev_obj.get()) | 158 if (!udev_obj.get()) |
105 return false; | 159 return false; |
106 | 160 |
107 struct stat device_stat; | 161 struct stat device_stat; |
108 if (stat(device_path.c_str(), &device_stat) < 0) | 162 if (stat(device_path.value().c_str(), &device_stat) < 0) |
109 return false; | 163 return false; |
110 | 164 |
111 char device_type; | 165 char device_type; |
112 if (S_ISCHR(device_stat.st_mode)) | 166 if (S_ISCHR(device_stat.st_mode)) |
113 device_type = 'c'; | 167 device_type = 'c'; |
114 else if (S_ISBLK(device_stat.st_mode)) | 168 else if (S_ISBLK(device_stat.st_mode)) |
115 device_type = 'b'; | 169 device_type = 'b'; |
116 else | 170 else |
117 return false; // Not a supported type. | 171 return false; // Not a supported type. |
118 | 172 |
(...skipping 21 matching lines...) Expand all Loading... | |
140 else if (model_name.empty()) | 194 else if (model_name.empty()) |
141 device_label = vendor_name; | 195 device_label = vendor_name; |
142 else | 196 else |
143 device_label = vendor_name + kSpaceDelim + model_name; | 197 device_label = vendor_name + kSpaceDelim + model_name; |
144 } | 198 } |
145 | 199 |
146 if (IsStringUTF8(device_label)) | 200 if (IsStringUTF8(device_label)) |
147 *name = UTF8ToUTF16(device_label); | 201 *name = UTF8ToUTF16(device_label); |
148 } | 202 } |
149 | 203 |
150 // Construct a device id using label or manufacturer (vendor and model) | 204 if (unique_id) { |
151 // details. | 205 *unique_id = MakeDeviceUniqueId(device); |
152 if (id) { | 206 if (unique_id->empty()) |
153 std::string unique_id = GetUdevDevicePropertyValue(device, kFsUUID); | 207 return false; |
154 if (unique_id.empty()) { | 208 } |
155 // If one of the vendor, model, serial information is missing, its value | |
156 // in the string is empty. | |
157 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialShortInfo | |
158 // E.g.: VendorModelSerial:Kn:DataTravel_12.10:8000000000006CB02CDB | |
159 std::string vendor = GetUdevDevicePropertyValue(device, kVendorID); | |
160 std::string model = GetUdevDevicePropertyValue(device, kModelID); | |
161 std::string serial_short = GetUdevDevicePropertyValue(device, | |
162 kSerialShort); | |
163 if (vendor.empty() && model.empty() && serial_short.empty()) | |
164 return false; | |
165 | 209 |
166 unique_id = base::StringPrintf("%s%s%s%s%s%s", | 210 if (removable) { |
167 kVendorModelSerialPrefix, | 211 const char* value = udev_device_get_sysattr_value(device, |
168 vendor.c_str(), | 212 kRemovableSysAttr); |
169 kNonSpaceDelim, | 213 if (!value) { |
170 model.c_str(), | 214 // |parent_device| is owned by |device| and does not need to be cleaned |
171 kNonSpaceDelim, | 215 // up. |
172 serial_short.c_str()); | 216 struct udev_device* parent_device = |
173 } else { | 217 udev_device_get_parent_with_subsystem_devtype(device, |
174 unique_id = kFSUniqueIdPrefix + unique_id; | 218 kBlockSubsystemKey, |
219 kDiskDeviceTypeKey); | |
220 value = udev_device_get_sysattr_value(parent_device, kRemovableSysAttr); | |
175 } | 221 } |
176 *id = MediaStorageUtil::MakeDeviceId( | 222 *removable = (value && atoi(value) == 1); |
177 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id); | |
178 } | 223 } |
179 return true; | 224 return true; |
180 } | 225 } |
181 | 226 |
182 } // namespace | 227 } // namespace |
183 | 228 |
184 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( | 229 RemovableDeviceNotificationsLinux::RemovableDeviceNotificationsLinux( |
185 const FilePath& path) | 230 const FilePath& path) |
186 : initialized_(false), | 231 : initialized_(false), |
187 mtab_path_(path), | 232 mtab_path_(path), |
188 get_device_info_func_(&GetDeviceInfo) { | 233 get_device_info_func_(&GetDeviceInfo) { |
234 DCHECK(!g_removable_device_notifications_linux); | |
235 g_removable_device_notifications_linux = this; | |
189 } | 236 } |
190 | 237 |
191 MediaDeviceNotificationsLinux::MediaDeviceNotificationsLinux( | 238 RemovableDeviceNotificationsLinux::RemovableDeviceNotificationsLinux( |
192 const FilePath& path, | 239 const FilePath& path, |
193 GetDeviceInfoFunc get_device_info_func) | 240 GetDeviceInfoFunc get_device_info_func) |
194 : initialized_(false), | 241 : initialized_(false), |
195 mtab_path_(path), | 242 mtab_path_(path), |
196 get_device_info_func_(get_device_info_func) { | 243 get_device_info_func_(get_device_info_func) { |
244 DCHECK(!g_removable_device_notifications_linux); | |
245 g_removable_device_notifications_linux = this; | |
197 } | 246 } |
198 | 247 |
199 MediaDeviceNotificationsLinux::~MediaDeviceNotificationsLinux() { | 248 RemovableDeviceNotificationsLinux::~RemovableDeviceNotificationsLinux() { |
200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
250 DCHECK_EQ(this, g_removable_device_notifications_linux); | |
251 g_removable_device_notifications_linux = NULL; | |
201 } | 252 } |
202 | 253 |
203 void MediaDeviceNotificationsLinux::Init() { | 254 // static |
255 RemovableDeviceNotificationsLinux* | |
256 RemovableDeviceNotificationsLinux::GetInstance() { | |
257 return g_removable_device_notifications_linux; | |
258 } | |
259 | |
260 void RemovableDeviceNotificationsLinux::Init() { | |
204 DCHECK(!mtab_path_.empty()); | 261 DCHECK(!mtab_path_.empty()); |
205 | 262 |
206 // Put |kKnownFileSystems| in std::set to get O(log N) access time. | 263 // Put |kKnownFileSystems| in std::set to get O(log N) access time. |
207 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) | 264 for (size_t i = 0; i < arraysize(kKnownFileSystems); ++i) |
208 known_file_systems_.insert(kKnownFileSystems[i]); | 265 known_file_systems_.insert(kKnownFileSystems[i]); |
209 | 266 |
210 BrowserThread::PostTask( | 267 BrowserThread::PostTask( |
211 BrowserThread::FILE, FROM_HERE, | 268 BrowserThread::FILE, FROM_HERE, |
212 base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread, this)); | 269 base::Bind(&RemovableDeviceNotificationsLinux::InitOnFileThread, this)); |
213 } | 270 } |
214 | 271 |
215 void MediaDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path, | 272 FilePath RemovableDeviceNotificationsLinux::GetDeviceMountPoint( |
216 bool error) { | 273 const std::string& device_id) const { |
274 | |
275 MediaStorageUtil::Type type; | |
276 MediaStorageUtil::CrackDeviceId(device_id, &type, NULL); | |
277 if (type == MediaStorageUtil::MTP_OR_PTP) | |
278 return FilePath(); | |
279 DCHECK(type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM || | |
280 type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM || | |
281 type == MediaStorageUtil::FIXED_MASS_STORAGE); | |
282 | |
283 FilePath mount_device; | |
284 for (MountMap::const_iterator it = mount_info_map_.begin(); | |
285 it != mount_info_map_.end(); | |
286 ++it) { | |
287 if (it->second.device_id == device_id) { | |
288 mount_device = it->second.mount_device; | |
289 break; | |
290 } | |
291 } | |
292 if (mount_device.empty()) | |
293 return mount_device; | |
294 | |
295 const ReferencedMountPoint& referenced_info = | |
296 mount_priority_map_.find(mount_device)->second; | |
297 for (ReferencedMountPoint::const_iterator it = referenced_info.begin(); | |
298 it != referenced_info.end(); | |
299 ++it) { | |
300 if (it->second) | |
301 return it->first; | |
302 } | |
303 // If none of them are default, just return the first. | |
304 return FilePath(referenced_info.begin()->first); | |
305 } | |
306 | |
307 std::string RemovableDeviceNotificationsLinux::GetDeviceIdForPath( | |
308 const FilePath& path, FilePath* mount_point) const { | |
309 if (!path.IsAbsolute()) | |
310 return std::string(); | |
311 | |
312 FilePath current = path; | |
313 while (mount_info_map_.find(current) == mount_info_map_.end() && | |
Lei Zhang
2012/08/28 20:49:04
while (!ContainsKey(mount_info_map_, current) && c
vandebo (ex-Chrome)
2012/08/28 21:25:26
Done.
| |
314 current != current.DirName()) { | |
315 current = current.DirName(); | |
316 } | |
317 | |
318 MountMap::const_iterator mount_info = mount_info_map_.find(current); | |
319 if (mount_info == mount_info_map_.end()) | |
320 return std::string(); | |
321 | |
322 if (mount_point) | |
323 *mount_point = current; | |
324 | |
325 return mount_info->second.device_id; | |
326 } | |
327 | |
328 void RemovableDeviceNotificationsLinux::OnFilePathChanged(const FilePath& path, | |
329 bool error) { | |
217 if (path != mtab_path_) { | 330 if (path != mtab_path_) { |
218 // This cannot happen unless FilePathWatcher is buggy. Just ignore this | 331 // This cannot happen unless FilePathWatcher is buggy. Just ignore this |
219 // notification and do nothing. | 332 // notification and do nothing. |
220 NOTREACHED(); | 333 NOTREACHED(); |
221 return; | 334 return; |
222 } | 335 } |
223 if (error) { | 336 if (error) { |
224 LOG(ERROR) << "Error watching " << mtab_path_.value(); | 337 LOG(ERROR) << "Error watching " << mtab_path_.value(); |
225 return; | 338 return; |
226 } | 339 } |
227 | 340 |
228 UpdateMtab(); | 341 UpdateMtab(); |
229 } | 342 } |
230 | 343 |
231 void MediaDeviceNotificationsLinux::InitOnFileThread() { | 344 void RemovableDeviceNotificationsLinux::InitOnFileThread() { |
232 DCHECK(!initialized_); | 345 DCHECK(!initialized_); |
233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
234 initialized_ = true; | 347 initialized_ = true; |
235 | 348 |
236 // The callback passed to Watch() has to be unretained. Otherwise | 349 // The callback passed to Watch() has to be unretained. Otherwise |
237 // MediaDeviceNotificationsLinux will live longer than expected, and | 350 // RemovableDeviceNotificationsLinux will live longer than expected, and |
238 // FilePathWatcher will get in trouble at shutdown time. | 351 // FilePathWatcher will get in trouble at shutdown time. |
239 bool ret = file_watcher_.Watch( | 352 bool ret = file_watcher_.Watch( |
240 mtab_path_, | 353 mtab_path_, |
241 base::Bind(&MediaDeviceNotificationsLinux::OnFilePathChanged, | 354 base::Bind(&RemovableDeviceNotificationsLinux::OnFilePathChanged, |
242 base::Unretained(this))); | 355 base::Unretained(this))); |
243 if (!ret) { | 356 if (!ret) { |
244 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; | 357 LOG(ERROR) << "Adding watch for " << mtab_path_.value() << " failed"; |
245 return; | 358 return; |
246 } | 359 } |
247 | 360 |
248 UpdateMtab(); | 361 UpdateMtab(); |
249 } | 362 } |
250 | 363 |
251 void MediaDeviceNotificationsLinux::UpdateMtab() { | 364 void RemovableDeviceNotificationsLinux::UpdateMtab() { |
252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 365 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
253 | 366 |
254 MountPointDeviceMap new_mtab; | 367 MountPointDeviceMap new_mtab; |
255 ReadMtab(&new_mtab); | 368 ReadMtab(mtab_path_, known_file_systems_, &new_mtab); |
256 | 369 |
257 // Check existing mtab entries for unaccounted mount points. | 370 // Check existing mtab entries for unaccounted mount points. |
258 // These mount points must have been removed in the new mtab. | 371 // These mount points must have been removed in the new mtab. |
259 std::vector<std::string> mount_points_to_erase; | 372 std::list<FilePath> mount_points_to_erase; |
373 std::list<FilePath> multiple_mounted_devices_needing_reattachment; | |
260 for (MountMap::const_iterator old_iter = mount_info_map_.begin(); | 374 for (MountMap::const_iterator old_iter = mount_info_map_.begin(); |
261 old_iter != mount_info_map_.end(); ++old_iter) { | 375 old_iter != mount_info_map_.end(); ++old_iter) { |
262 const std::string& mount_point = old_iter->first; | 376 const FilePath& mount_point = old_iter->first; |
263 const std::string& mount_device = old_iter->second.mount_device; | 377 const FilePath& mount_device = old_iter->second.mount_device; |
264 MountPointDeviceMap::iterator new_iter = new_mtab.find(mount_point); | 378 MountPointDeviceMap::iterator new_iter = new_mtab.find(mount_point); |
265 // |mount_point| not in |new_mtab| or |mount_device| is no longer mounted at | 379 // |mount_point| not in |new_mtab| or |mount_device| is no longer mounted at |
266 // |mount_point|. | 380 // |mount_point|. |
267 if (new_iter == new_mtab.end() || (new_iter->second != mount_device)) { | 381 if (new_iter == new_mtab.end() || (new_iter->second != mount_device)) { |
268 RemoveOldDevice(old_iter->second.device_id); | 382 MountPriorityMap::iterator priority = |
383 mount_priority_map_.find(mount_device); | |
384 DCHECK(priority != mount_priority_map_.end()); | |
385 ReferencedMountPoint::const_iterator has_priority = | |
386 priority->second.find(mount_point); | |
387 if (old_iter->second.has_dcim) { | |
388 DCHECK(has_priority != priority->second.end()); | |
389 if (has_priority->second) | |
390 RemoveMediaMount(old_iter->second.device_id); | |
391 if (priority->second.size() > 1) | |
392 multiple_mounted_devices_needing_reattachment.push_back(mount_device); | |
393 } | |
394 priority->second.erase(mount_point); | |
395 if (priority->second.empty()) | |
396 mount_priority_map_.erase(mount_device); | |
269 mount_points_to_erase.push_back(mount_point); | 397 mount_points_to_erase.push_back(mount_point); |
270 } | 398 } |
271 } | 399 } |
400 | |
272 // Erase the |mount_info_map_| entries afterwards. Erasing in the loop above | 401 // Erase the |mount_info_map_| entries afterwards. Erasing in the loop above |
273 // using the iterator is slightly more efficient, but more tricky, since | 402 // using the iterator is slightly more efficient, but more tricky, since |
274 // calling std::map::erase() on an iterator invalidates it. | 403 // calling std::map::erase() on an iterator invalidates it. |
275 for (size_t i = 0; i < mount_points_to_erase.size(); ++i) | 404 for (std::list<FilePath>::const_iterator it = mount_points_to_erase.begin(); |
276 mount_info_map_.erase(mount_points_to_erase[i]); | 405 it != mount_points_to_erase.end(); |
406 ++it) { | |
407 mount_info_map_.erase(*it); | |
408 } | |
409 | |
410 // For any multiply mounted device where the mount that we had notified | |
411 // got detached, send a notification of attachment for one of the other | |
412 // mount points. | |
413 for (std::list<FilePath>::const_iterator it = | |
414 multiple_mounted_devices_needing_reattachment.begin(); | |
415 it != multiple_mounted_devices_needing_reattachment.end(); | |
416 ++it) { | |
417 const FilePath& mount_point = mount_priority_map_[*it].begin()->first; | |
418 DCHECK(!mount_point.empty()); | |
419 mount_priority_map_[*it].begin()->second = true; | |
420 DCHECK(mount_info_map_[mount_point].has_dcim); | |
421 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( | |
422 mount_info_map_[mount_point].device_id, | |
423 mount_info_map_[mount_point].device_name, | |
424 mount_point.value()); | |
425 } | |
277 | 426 |
278 // Check new mtab entries against existing ones. | 427 // Check new mtab entries against existing ones. |
279 for (MountPointDeviceMap::iterator new_iter = new_mtab.begin(); | 428 for (MountPointDeviceMap::iterator new_iter = new_mtab.begin(); |
280 new_iter != new_mtab.end(); ++new_iter) { | 429 new_iter != new_mtab.end(); ++new_iter) { |
281 const std::string& mount_point = new_iter->first; | 430 const FilePath& mount_point = new_iter->first; |
282 const std::string& mount_device = new_iter->second; | 431 const FilePath& mount_device = new_iter->second; |
283 MountMap::iterator old_iter = mount_info_map_.find(mount_point); | 432 MountMap::iterator old_iter = mount_info_map_.find(mount_point); |
284 if (old_iter == mount_info_map_.end() || | 433 if (old_iter == mount_info_map_.end() || |
285 old_iter->second.mount_device != mount_device) { | 434 old_iter->second.mount_device != mount_device) { |
286 // New mount point found or an existing mount point found with a new | 435 // New mount point found or an existing mount point found with a new |
287 // device. | 436 // device. |
288 CheckAndAddMediaDevice(mount_device, mount_point); | 437 AddNewMount(mount_device, mount_point); |
289 } | 438 } |
290 } | 439 } |
291 } | 440 } |
292 | 441 |
293 void MediaDeviceNotificationsLinux::ReadMtab(MountPointDeviceMap* mtab) { | 442 void RemovableDeviceNotificationsLinux::AddNewMount( |
294 FILE* fp = setmntent(mtab_path_.value().c_str(), "r"); | 443 const FilePath& mount_device, const FilePath& mount_point) { |
295 if (!fp) | 444 MountPriorityMap::iterator priority = |
445 mount_priority_map_.find(mount_device); | |
446 if (priority != mount_priority_map_.end()) { | |
447 const FilePath& other_mount_point = priority->second.begin()->first; | |
448 priority->second[mount_point] = false; | |
449 mount_info_map_[mount_point] = mount_info_map_[other_mount_point]; | |
296 return; | 450 return; |
451 } | |
297 | 452 |
298 mntent entry; | 453 std::string unique_id; |
299 char buf[512]; | |
300 | |
301 // Keep track of mount point entry positions in mtab file. | |
302 int entry_pos = 0; | |
303 | |
304 // Helper map to store the device mount point details. | |
305 // (mount device, MountPointEntryInfo) | |
306 typedef std::map<std::string, MountPointEntryInfo> DeviceMap; | |
307 DeviceMap device_map; | |
308 while (getmntent_r(fp, &entry, buf, sizeof(buf))) { | |
309 // We only care about real file systems. | |
310 if (!ContainsKey(known_file_systems_, entry.mnt_type)) | |
311 continue; | |
312 | |
313 // Add entries, but overwrite entries for the same mount device. Keep track | |
314 // of the entry positions in |entry_info| and use that below to resolve | |
315 // multiple devices mounted at the same mount point. | |
316 MountPointEntryInfo entry_info; | |
317 entry_info.mount_point = entry.mnt_dir; | |
318 entry_info.entry_pos = entry_pos++; | |
319 device_map[entry.mnt_fsname] = entry_info; | |
320 } | |
321 endmntent(fp); | |
322 | |
323 // Helper map to store mount point entries. | |
324 // (mount point, entry position in mtab file) | |
325 typedef std::map<std::string, int> MountPointsInfoMap; | |
326 MountPointsInfoMap mount_points_info_map; | |
327 MountPointDeviceMap& new_mtab = *mtab; | |
328 for (DeviceMap::const_iterator device_it = device_map.begin(); | |
329 device_it != device_map.end(); | |
330 ++device_it) { | |
331 // Add entries, but overwrite entries for the same mount point. Keep track | |
332 // of the entry positions and use that information to resolve multiple | |
333 // devices mounted at the same mount point. | |
334 const std::string& mount_device = device_it->first; | |
335 const std::string& mount_point = device_it->second.mount_point; | |
336 const int entry_pos = device_it->second.entry_pos; | |
337 MountPointDeviceMap::iterator new_it = new_mtab.find(mount_point); | |
338 MountPointsInfoMap::iterator mount_point_info_map_it = | |
339 mount_points_info_map.find(mount_point); | |
340 | |
341 // New mount point entry found or there is already a device mounted at | |
342 // |mount_point| and the existing mount entry is older than the current one. | |
343 if (new_it == new_mtab.end() || | |
344 (mount_point_info_map_it != mount_points_info_map.end() && | |
345 mount_point_info_map_it->second < entry_pos)) { | |
346 new_mtab[mount_point] = mount_device; | |
347 mount_points_info_map[mount_point] = entry_pos; | |
348 } | |
349 } | |
350 } | |
351 | |
352 void MediaDeviceNotificationsLinux::CheckAndAddMediaDevice( | |
353 const std::string& mount_device, | |
354 const std::string& mount_point) { | |
355 if (!IsMediaDevice(mount_point)) | |
356 return; | |
357 | |
358 std::string id; | |
359 string16 name; | 454 string16 name; |
360 bool result = get_device_info_func_(mount_device, &id, &name); | 455 bool removable; |
456 bool result = get_device_info_func_(mount_device, &unique_id, &name, | |
457 &removable); | |
361 | 458 |
362 // Keep track of GetDeviceInfo result, to see how often we fail to get device | 459 // Keep track of GetDeviceInfo result, to see how often we fail to get device |
363 // details. | 460 // details. |
364 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_info_available", | 461 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_info_available", |
365 result); | 462 result); |
366 if (!result) | 463 if (!result) |
367 return; | 464 return; |
368 | 465 |
369 // Keep track of device uuid, to see how often we receive empty values. | 466 // Keep track of device uuid, to see how often we receive empty values. |
370 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_uuid_available", | 467 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_uuid_available", |
371 !id.empty()); | 468 !unique_id.empty()); |
372 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_name_available", | 469 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_name_available", |
373 !name.empty()); | 470 !name.empty()); |
374 | 471 |
375 if (id.empty() || name.empty()) | 472 if (unique_id.empty() || name.empty()) |
376 return; | 473 return; |
377 | 474 |
378 MountDeviceAndId mount_device_and_id; | 475 bool has_dcim = IsMediaDevice(mount_point.value()); |
379 mount_device_and_id.mount_device = mount_device; | 476 MediaStorageUtil::Type type; |
380 mount_device_and_id.device_id = id; | 477 if (removable) { |
381 mount_info_map_[mount_point] = mount_device_and_id; | 478 if (has_dcim) { |
479 type = MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM; | |
480 } else { | |
481 type = MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; | |
482 } | |
483 } else { | |
484 type = MediaStorageUtil::FIXED_MASS_STORAGE; | |
485 } | |
486 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); | |
382 | 487 |
383 SystemMonitor::Get()->ProcessRemovableStorageAttached(id, name, mount_point); | 488 MountPointInfo mount_point_info; |
489 mount_point_info.mount_device = mount_device; | |
490 mount_point_info.device_id = device_id; | |
491 mount_point_info.device_name = name; | |
492 mount_point_info.has_dcim = has_dcim; | |
493 | |
494 mount_info_map_[mount_point] = mount_point_info; | |
495 mount_priority_map_[mount_device][mount_point] = has_dcim; | |
496 | |
497 if (mount_point_info.has_dcim) { | |
498 SystemMonitor::Get()->ProcessRemovableStorageAttached(device_id, name, | |
499 mount_point.value()); | |
500 } | |
384 } | 501 } |
385 | 502 |
386 void MediaDeviceNotificationsLinux::RemoveOldDevice( | 503 void RemovableDeviceNotificationsLinux::RemoveMediaMount( |
387 const std::string& device_id) { | 504 const std::string& device_id) { |
388 SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id); | 505 SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id); |
389 } | 506 } |
390 | 507 |
391 } // namespace chrome | 508 } // namespace chrome |
OLD | NEW |