Chromium Code Reviews| 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 // StorageMonitorLinux implementation. | 5 // StorageMonitorLinux implementation. |
| 6 | 6 |
| 7 #include "chrome/browser/storage_monitor/storage_monitor_linux.h" | 7 #include "chrome/browser/storage_monitor/storage_monitor_linux.h" |
| 8 | 8 |
| 9 #include <mntent.h> | 9 #include <mntent.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 | 128 |
| 129 uint64 total_size_in_bytes = 0; | 129 uint64 total_size_in_bytes = 0; |
| 130 if (!base::StringToUint64(partition_size, &total_size_in_bytes)) | 130 if (!base::StringToUint64(partition_size, &total_size_in_bytes)) |
| 131 return 0; | 131 return 0; |
| 132 return (total_size_in_bytes <= kuint64max / 512) ? | 132 return (total_size_in_bytes <= kuint64max / 512) ? |
| 133 total_size_in_bytes * 512 : 0; | 133 total_size_in_bytes * 512 : 0; |
| 134 } | 134 } |
| 135 | 135 |
| 136 // Constructs the device name from the device properties. If the device details | 136 // Constructs the device name from the device properties. If the device details |
| 137 // are unavailable, returns an empty string. | 137 // are unavailable, returns an empty string. |
| 138 string16 GetDeviceName(struct udev_device* device) { | 138 string16 GetDeviceName(struct udev_device* device, |
| 139 string16* out_volume_label, | |
| 140 string16* out_vendor_name, | |
| 141 string16* out_model_name) { | |
| 139 std::string device_label = GetUdevDevicePropertyValue(device, kLabel); | 142 std::string device_label = GetUdevDevicePropertyValue(device, kLabel); |
| 143 std::string vendor_name = GetUdevDevicePropertyValue(device, kVendor); | |
| 144 std::string model_name = GetUdevDevicePropertyValue(device, kModel); | |
| 145 if (out_volume_label) | |
| 146 *out_volume_label = UTF8ToUTF16(device_label); | |
| 147 if (out_vendor_name) | |
| 148 *out_vendor_name = UTF8ToUTF16(vendor_name); | |
| 149 if (out_model_name) | |
| 150 *out_model_name = UTF8ToUTF16(model_name); | |
| 151 | |
| 140 if (!device_label.empty() && IsStringUTF8(device_label)) | 152 if (!device_label.empty() && IsStringUTF8(device_label)) |
| 141 return UTF8ToUTF16(device_label); | 153 return UTF8ToUTF16(device_label); |
| 142 | 154 |
| 143 device_label = GetUdevDevicePropertyValue(device, kFsUUID); | 155 device_label = GetUdevDevicePropertyValue(device, kFsUUID); |
| 144 // Keep track of device uuid, to see how often we receive empty uuid values. | 156 // Keep track of device uuid, to see how often we receive empty uuid values. |
| 145 UMA_HISTOGRAM_BOOLEAN( | 157 UMA_HISTOGRAM_BOOLEAN( |
| 146 "RemovableDeviceNotificationsLinux.device_file_system_uuid_available", | 158 "RemovableDeviceNotificationsLinux.device_file_system_uuid_available", |
| 147 !device_label.empty()); | 159 !device_label.empty()); |
| 148 | 160 |
| 149 const string16 name = GetFullProductName( | 161 const string16 name = GetFullProductName(vendor_name, model_name); |
| 150 GetUdevDevicePropertyValue(device, kVendor), | |
| 151 GetUdevDevicePropertyValue(device, kModel)); | |
| 152 | 162 |
| 153 const string16 device_label_utf16 = | 163 const string16 device_label_utf16 = |
| 154 (!device_label.empty() && IsStringUTF8(device_label)) ? | 164 (!device_label.empty() && IsStringUTF8(device_label)) ? |
| 155 UTF8ToUTF16(device_label) : string16(); | 165 UTF8ToUTF16(device_label) : string16(); |
| 156 if (!name.empty() && !device_label_utf16.empty()) | 166 if (!name.empty() && !device_label_utf16.empty()) |
| 157 return device_label_utf16 + ASCIIToUTF16(" ") + name; | 167 return device_label_utf16 + ASCIIToUTF16(" ") + name; |
| 158 return name.empty() ? device_label_utf16 : name; | 168 return name.empty() ? device_label_utf16 : name; |
| 159 } | 169 } |
| 160 | 170 |
| 161 // Get the device information using udev library. | 171 // Get the device information using udev library. |
| 162 // On success, returns true and fill in |unique_id|, |name|, |removable| and | 172 // On success, returns true and fill in |unique_id|, |name|, |removable| and |
| 163 // |partition_size_in_bytes|. | 173 // |partition_size_in_bytes|. |
| 164 void GetDeviceInfo(const base::FilePath& device_path, | 174 void GetDeviceInfo(const base::FilePath& device_path, |
| 165 std::string* unique_id, | 175 const base::FilePath& mount_point, |
| 176 std::string* device_id, | |
| 166 string16* name, | 177 string16* name, |
| 167 bool* removable, | 178 bool* removable, |
| 168 uint64* partition_size_in_bytes) { | 179 uint64* partition_size_in_bytes, |
| 180 string16* out_volume_label, | |
| 181 string16* out_vendor_name, | |
| 182 string16* out_model_name) { | |
| 169 DCHECK(!device_path.empty()); | 183 DCHECK(!device_path.empty()); |
| 170 ScopedUdevObject udev_obj(udev_new()); | 184 ScopedUdevObject udev_obj(udev_new()); |
| 171 if (!udev_obj.get()) { | 185 if (!udev_obj.get()) { |
| 172 RecordGetDeviceInfoResult(false); | 186 RecordGetDeviceInfoResult(false); |
| 173 return; | 187 return; |
| 174 } | 188 } |
| 175 | 189 |
| 176 struct stat device_stat; | 190 struct stat device_stat; |
| 177 if (stat(device_path.value().c_str(), &device_stat) < 0) { | 191 if (stat(device_path.value().c_str(), &device_stat) < 0) { |
| 178 RecordGetDeviceInfoResult(false); | 192 RecordGetDeviceInfoResult(false); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 189 return; // Not a supported type. | 203 return; // Not a supported type. |
| 190 } | 204 } |
| 191 | 205 |
| 192 ScopedUdevDeviceObject device( | 206 ScopedUdevDeviceObject device( |
| 193 udev_device_new_from_devnum(udev_obj, device_type, device_stat.st_rdev)); | 207 udev_device_new_from_devnum(udev_obj, device_type, device_stat.st_rdev)); |
| 194 if (!device.get()) { | 208 if (!device.get()) { |
| 195 RecordGetDeviceInfoResult(false); | 209 RecordGetDeviceInfoResult(false); |
| 196 return; | 210 return; |
| 197 } | 211 } |
| 198 | 212 |
| 213 string16 volume_label; | |
| 214 string16 vendor_name; | |
| 215 string16 model_name; | |
| 216 string16 device_name = GetDeviceName(device, &volume_label, | |
| 217 &vendor_name, &model_name); | |
| 199 if (name) | 218 if (name) |
| 200 *name = GetDeviceName(device); | 219 *name = device_name; |
| 201 | 220 |
| 202 if (unique_id) | 221 std::string unique_id = MakeDeviceUniqueId(device); |
| 203 *unique_id = MakeDeviceUniqueId(device); | 222 MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, device_name); |
| 204 | 223 |
| 224 const char* value = udev_device_get_sysattr_value(device, | |
| 225 kRemovableSysAttr); | |
| 226 if (!value) { | |
| 227 // |parent_device| is owned by |device| and does not need to be cleaned | |
| 228 // up. | |
| 229 struct udev_device* parent_device = | |
| 230 udev_device_get_parent_with_subsystem_devtype(device, | |
| 231 kBlockSubsystemKey, | |
| 232 kDiskDeviceTypeKey); | |
| 233 value = udev_device_get_sysattr_value(parent_device, kRemovableSysAttr); | |
| 234 } | |
| 235 bool is_removable = (value && atoi(value) == 1); | |
| 205 if (removable) { | 236 if (removable) { |
| 206 const char* value = udev_device_get_sysattr_value(device, | 237 *removable = is_removable; |
| 207 kRemovableSysAttr); | 238 } |
| 208 if (!value) { | 239 |
| 209 // |parent_device| is owned by |device| and does not need to be cleaned | 240 if (device_id) { |
| 210 // up. | 241 MediaStorageUtil::Type type = MediaStorageUtil::FIXED_MASS_STORAGE; |
| 211 struct udev_device* parent_device = | 242 if (is_removable) { |
| 212 udev_device_get_parent_with_subsystem_devtype(device, | 243 if (IsMediaDevice(mount_point.value())) |
| 213 kBlockSubsystemKey, | 244 type = MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM; |
| 214 kDiskDeviceTypeKey); | 245 else |
| 215 value = udev_device_get_sysattr_value(parent_device, kRemovableSysAttr); | 246 type = MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; |
| 216 } | 247 } |
| 217 *removable = (value && atoi(value) == 1); | 248 *device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); |
| 218 } | 249 } |
| 219 | 250 |
| 220 if (partition_size_in_bytes) | 251 if (partition_size_in_bytes) |
| 221 *partition_size_in_bytes = GetDeviceStorageSize(device_path, device); | 252 *partition_size_in_bytes = GetDeviceStorageSize(device_path, device); |
| 222 RecordGetDeviceInfoResult(true); | 253 RecordGetDeviceInfoResult(true); |
| 223 } | 254 } |
| 224 | 255 |
| 225 } // namespace | 256 } // namespace |
| 226 | 257 |
| 227 StorageMonitorLinux::StorageMonitorLinux(const base::FilePath& path) | 258 StorageMonitorLinux::StorageMonitorLinux(const base::FilePath& path) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 if (!path.IsAbsolute()) | 290 if (!path.IsAbsolute()) |
| 260 return false; | 291 return false; |
| 261 | 292 |
| 262 base::FilePath current = path; | 293 base::FilePath current = path; |
| 263 while (!ContainsKey(mount_info_map_, current) && current != current.DirName()) | 294 while (!ContainsKey(mount_info_map_, current) && current != current.DirName()) |
| 264 current = current.DirName(); | 295 current = current.DirName(); |
| 265 | 296 |
| 266 MountMap::const_iterator mount_info = mount_info_map_.find(current); | 297 MountMap::const_iterator mount_info = mount_info_map_.find(current); |
| 267 if (mount_info == mount_info_map_.end()) | 298 if (mount_info == mount_info_map_.end()) |
| 268 return false; | 299 return false; |
| 269 | 300 if (device_info) |
| 270 if (device_info) { | 301 *device_info = mount_info->second.storage_info; |
| 271 device_info->device_id = mount_info->second.device_id; | |
| 272 device_info->name = mount_info->second.device_name; | |
| 273 device_info->location = current.value(); | |
| 274 } | |
| 275 return true; | 302 return true; |
| 276 } | 303 } |
| 277 | 304 |
| 278 uint64 StorageMonitorLinux::GetStorageSize(const std::string& location) const { | 305 uint64 StorageMonitorLinux::GetStorageSize(const std::string& location) const { |
| 279 MountMap::const_iterator mount_info = mount_info_map_.find( | 306 MountMap::const_iterator mount_info = mount_info_map_.find( |
| 280 base::FilePath(location)); | 307 base::FilePath(location)); |
| 281 return (mount_info != mount_info_map_.end()) ? | 308 return (mount_info != mount_info_map_.end()) ? |
| 282 mount_info->second.partition_size_in_bytes : 0; | 309 mount_info->second.total_size_in_bytes : 0; |
| 283 } | 310 } |
| 284 | 311 |
| 285 void StorageMonitorLinux::OnFilePathChanged(const base::FilePath& path, | 312 void StorageMonitorLinux::OnFilePathChanged(const base::FilePath& path, |
| 286 bool error) { | 313 bool error) { |
| 287 if (path != mtab_path_) { | 314 if (path != mtab_path_) { |
| 288 // This cannot happen unless FilePathWatcher is buggy. Just ignore this | 315 // This cannot happen unless FilePathWatcher is buggy. Just ignore this |
| 289 // notification and do nothing. | 316 // notification and do nothing. |
| 290 NOTREACHED(); | 317 NOTREACHED(); |
| 291 return; | 318 return; |
| 292 } | 319 } |
| 293 if (error) { | 320 if (error) { |
| 294 LOG(ERROR) << "Error watching " << mtab_path_.value(); | 321 LOG(ERROR) << "Error watching " << mtab_path_.value(); |
| 295 return; | 322 return; |
| 296 } | 323 } |
| 297 | 324 |
| 298 UpdateMtab(); | 325 UpdateMtab(); |
| 299 } | 326 } |
| 300 | 327 |
| 301 StorageMonitorLinux::MountPointInfo::MountPointInfo() | |
| 302 : partition_size_in_bytes(0) { | |
| 303 } | |
| 304 | |
| 305 void StorageMonitorLinux::InitOnFileThread() { | 328 void StorageMonitorLinux::InitOnFileThread() { |
| 306 DCHECK(!initialized_); | 329 DCHECK(!initialized_); |
| 307 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 308 initialized_ = true; | 331 initialized_ = true; |
| 309 | 332 |
| 310 // The callback passed to Watch() has to be unretained. Otherwise | 333 // The callback passed to Watch() has to be unretained. Otherwise |
| 311 // StorageMonitorLinux will live longer than expected, and FilePathWatcher | 334 // StorageMonitorLinux will live longer than expected, and FilePathWatcher |
| 312 // will get in trouble at shutdown time. | 335 // will get in trouble at shutdown time. |
| 313 bool ret = file_watcher_.Watch( | 336 bool ret = file_watcher_.Watch( |
| 314 mtab_path_, false, | 337 mtab_path_, false, |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 // mount points. | 398 // mount points. |
| 376 for (std::list<base::FilePath>::const_iterator it = | 399 for (std::list<base::FilePath>::const_iterator it = |
| 377 multiple_mounted_devices_needing_reattachment.begin(); | 400 multiple_mounted_devices_needing_reattachment.begin(); |
| 378 it != multiple_mounted_devices_needing_reattachment.end(); | 401 it != multiple_mounted_devices_needing_reattachment.end(); |
| 379 ++it) { | 402 ++it) { |
| 380 ReferencedMountPoint::iterator first_mount_point_info = | 403 ReferencedMountPoint::iterator first_mount_point_info = |
| 381 mount_priority_map_.find(*it)->second.begin(); | 404 mount_priority_map_.find(*it)->second.begin(); |
| 382 const base::FilePath& mount_point = first_mount_point_info->first; | 405 const base::FilePath& mount_point = first_mount_point_info->first; |
| 383 first_mount_point_info->second = true; | 406 first_mount_point_info->second = true; |
| 384 | 407 |
| 385 const MountPointInfo& mount_info = | 408 const StorageInfo& mount_info = |
| 386 mount_info_map_.find(mount_point)->second; | 409 mount_info_map_.find(mount_point)->second.storage_info; |
| 387 DCHECK(MediaStorageUtil::IsRemovableDevice(mount_info.device_id)); | 410 DCHECK(MediaStorageUtil::IsRemovableDevice(mount_info.device_id)); |
| 388 receiver()->ProcessAttach(StorageInfo( | 411 receiver()->ProcessAttach(mount_info); |
| 389 mount_info.device_id, mount_info.device_name, mount_point.value())); | |
| 390 } | 412 } |
| 391 | 413 |
| 392 // Check new mtab entries against existing ones. | 414 // Check new mtab entries against existing ones. |
| 393 for (MountPointDeviceMap::iterator new_iter = new_mtab.begin(); | 415 for (MountPointDeviceMap::iterator new_iter = new_mtab.begin(); |
| 394 new_iter != new_mtab.end(); ++new_iter) { | 416 new_iter != new_mtab.end(); ++new_iter) { |
| 395 const base::FilePath& mount_point = new_iter->first; | 417 const base::FilePath& mount_point = new_iter->first; |
| 396 const base::FilePath& mount_device = new_iter->second; | 418 const base::FilePath& mount_device = new_iter->second; |
| 397 MountMap::iterator old_iter = mount_info_map_.find(mount_point); | 419 MountMap::iterator old_iter = mount_info_map_.find(mount_point); |
| 398 if (old_iter == mount_info_map_.end() || | 420 if (old_iter == mount_info_map_.end() || |
| 399 old_iter->second.mount_device != mount_device) { | 421 GetDeviceForMountPoint(old_iter->first) != mount_device) { |
|
vandebo (ex-Chrome)
2013/03/06 23:39:12
missed revert
Greg Billock
2013/03/07 21:06:07
Done.
| |
| 400 // New mount point found or an existing mount point found with a new | 422 // New mount point found or an existing mount point found with a new |
| 401 // device. | 423 // device. |
| 402 AddNewMount(mount_device, mount_point); | 424 AddNewMount(mount_device, mount_point); |
| 403 } | 425 } |
| 404 } | 426 } |
| 405 } | 427 } |
| 406 | 428 |
| 407 void StorageMonitorLinux::AddNewMount(const base::FilePath& mount_device, | 429 void StorageMonitorLinux::AddNewMount(const base::FilePath& mount_device, |
| 408 const base::FilePath& mount_point) { | 430 const base::FilePath& mount_point) { |
| 409 MountPriorityMap::iterator priority = | 431 MountPriorityMap::iterator priority = |
| 410 mount_priority_map_.find(mount_device); | 432 mount_priority_map_.find(mount_device); |
| 411 if (priority != mount_priority_map_.end()) { | 433 if (priority != mount_priority_map_.end()) { |
| 412 const base::FilePath& other_mount_point = priority->second.begin()->first; | 434 const base::FilePath& other_mount_point = priority->second.begin()->first; |
| 413 priority->second[mount_point] = false; | 435 priority->second[mount_point] = false; |
| 414 mount_info_map_[mount_point] = | 436 mount_info_map_[mount_point] = |
| 415 mount_info_map_.find(other_mount_point)->second; | 437 mount_info_map_.find(other_mount_point)->second; |
| 416 return; | 438 return; |
| 417 } | 439 } |
| 418 | 440 |
| 419 std::string unique_id; | 441 std::string device_id; |
| 420 string16 name; | 442 string16 name; |
| 421 bool removable; | 443 bool removable; |
| 422 uint64 partition_size_in_bytes; | 444 uint64 partition_size_in_bytes; |
| 423 get_device_info_func_(mount_device, &unique_id, &name, &removable, | 445 string16 volume_label; |
| 424 &partition_size_in_bytes); | 446 string16 vendor_name; |
| 447 string16 model_name; | |
| 448 get_device_info_func_(mount_device, mount_point, &device_id, &name, | |
| 449 &removable, &partition_size_in_bytes, | |
| 450 &volume_label, &vendor_name, &model_name); | |
| 425 | 451 |
| 426 // Keep track of device info details to see how often we get invalid values. | 452 // Keep track of device info details to see how often we get invalid values. |
| 427 MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, name); | 453 if (device_id.empty() || name.empty()) |
| 428 if (unique_id.empty() || name.empty()) | |
| 429 return; | 454 return; |
| 430 | 455 |
| 431 bool has_dcim = IsMediaDevice(mount_point.value()); | |
| 432 MediaStorageUtil::Type type; | |
| 433 if (removable) { | |
| 434 if (has_dcim) { | |
| 435 type = MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM; | |
| 436 } else { | |
| 437 type = MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; | |
| 438 } | |
| 439 } else { | |
| 440 type = MediaStorageUtil::FIXED_MASS_STORAGE; | |
| 441 } | |
| 442 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); | |
| 443 | |
| 444 MountPointInfo mount_point_info; | 456 MountPointInfo mount_point_info; |
| 445 mount_point_info.mount_device = mount_device; | 457 mount_point_info.mount_device = mount_device; |
| 446 mount_point_info.device_id = device_id; | 458 mount_point_info.storage_info = StorageInfo( |
| 447 mount_point_info.device_name = name; | 459 device_id, name, mount_point.value(), volume_label, |
| 448 mount_point_info.partition_size_in_bytes = partition_size_in_bytes; | 460 vendor_name, model_name, partition_size_in_bytes); |
| 449 | |
| 450 mount_info_map_[mount_point] = mount_point_info; | 461 mount_info_map_[mount_point] = mount_point_info; |
| 451 mount_priority_map_[mount_device][mount_point] = removable; | 462 mount_priority_map_[mount_device][mount_point] = removable; |
| 452 | 463 |
| 453 if (removable) { | 464 if (removable) { |
| 454 receiver()->ProcessAttach(StorageInfo( | 465 receiver()->ProcessAttach(StorageInfo( |
| 455 device_id, GetDisplayNameForDevice(partition_size_in_bytes, name), | 466 device_id, GetDisplayNameForDevice(partition_size_in_bytes, name), |
| 456 mount_point.value())); | 467 mount_point.value())); |
| 457 } | 468 } |
| 458 } | 469 } |
| 459 | 470 |
| 460 } // namespace chrome | 471 } // namespace chrome |
| OLD | NEW |