| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/storage_monitor/storage_monitor_linux.h" | 7 #include "components/storage_monitor/storage_monitor_linux.h" |
| 8 | 8 |
| 9 #include <mntent.h> | 9 #include <mntent.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 #include <limits> | 12 #include <limits> |
| 13 #include <list> | 13 #include <list> |
| 14 #include <utility> | 14 #include <utility> |
| 15 | 15 |
| 16 #include "base/bind.h" | 16 #include "base/bind.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" |
| 18 #include "base/metrics/histogram_macros.h" | 19 #include "base/metrics/histogram_macros.h" |
| 19 #include "base/process/kill.h" | 20 #include "base/process/kill.h" |
| 20 #include "base/process/launch.h" | 21 #include "base/process/launch.h" |
| 21 #include "base/process/process.h" | 22 #include "base/process/process.h" |
| 22 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
| 23 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
| 24 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
| 25 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
| 26 #include "components/storage_monitor/media_storage_util.h" | 27 #include "components/storage_monitor/media_storage_util.h" |
| 27 #include "components/storage_monitor/removable_device_constants.h" | 28 #include "components/storage_monitor/removable_device_constants.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 }; | 98 }; |
| 98 | 99 |
| 99 // Returns the storage partition size of the device specified by |device_path|. | 100 // Returns the storage partition size of the device specified by |device_path|. |
| 100 // If the requested information is unavailable, returns 0. | 101 // If the requested information is unavailable, returns 0. |
| 101 uint64_t GetDeviceStorageSize(const base::FilePath& device_path, | 102 uint64_t GetDeviceStorageSize(const base::FilePath& device_path, |
| 102 struct udev_device* device) { | 103 struct udev_device* device) { |
| 103 // sysfs provides the device size in units of 512-byte blocks. | 104 // sysfs provides the device size in units of 512-byte blocks. |
| 104 const std::string partition_size = | 105 const std::string partition_size = |
| 105 device::UdevDeviceGetSysattrValue(device, kSizeSysAttr); | 106 device::UdevDeviceGetSysattrValue(device, kSizeSysAttr); |
| 106 | 107 |
| 107 // Keep track of device size, to see how often this information is | |
| 108 // unavailable. | |
| 109 UMA_HISTOGRAM_BOOLEAN( | |
| 110 "RemovableDeviceNotificationsLinux.device_partition_size_available", | |
| 111 !partition_size.empty()); | |
| 112 | |
| 113 uint64_t total_size_in_bytes = 0; | 108 uint64_t total_size_in_bytes = 0; |
| 114 if (!base::StringToUint64(partition_size, &total_size_in_bytes)) | 109 if (!base::StringToUint64(partition_size, &total_size_in_bytes)) |
| 115 return 0; | 110 return 0; |
| 116 return (total_size_in_bytes <= std::numeric_limits<uint64_t>::max() / 512) | 111 return (total_size_in_bytes <= std::numeric_limits<uint64_t>::max() / 512) |
| 117 ? total_size_in_bytes * 512 | 112 ? total_size_in_bytes * 512 |
| 118 : 0; | 113 : 0; |
| 119 } | 114 } |
| 120 | 115 |
| 121 // Gets the device information using udev library. | 116 // Gets the device information using udev library. |
| 122 std::unique_ptr<StorageInfo> GetDeviceInfo(const base::FilePath& device_path, | 117 std::unique_ptr<StorageInfo> GetDeviceInfo(const base::FilePath& device_path, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 151 return storage_info; | 146 return storage_info; |
| 152 | 147 |
| 153 base::string16 volume_label = base::UTF8ToUTF16( | 148 base::string16 volume_label = base::UTF8ToUTF16( |
| 154 device::UdevDeviceGetPropertyValue(device.get(), kLabel)); | 149 device::UdevDeviceGetPropertyValue(device.get(), kLabel)); |
| 155 base::string16 vendor_name = base::UTF8ToUTF16( | 150 base::string16 vendor_name = base::UTF8ToUTF16( |
| 156 device::UdevDeviceGetPropertyValue(device.get(), kVendor)); | 151 device::UdevDeviceGetPropertyValue(device.get(), kVendor)); |
| 157 base::string16 model_name = base::UTF8ToUTF16( | 152 base::string16 model_name = base::UTF8ToUTF16( |
| 158 device::UdevDeviceGetPropertyValue(device.get(), kModel)); | 153 device::UdevDeviceGetPropertyValue(device.get(), kModel)); |
| 159 | 154 |
| 160 std::string unique_id = MakeDeviceUniqueId(device.get()); | 155 std::string unique_id = MakeDeviceUniqueId(device.get()); |
| 161 | |
| 162 // Keep track of device info details to see how often we get invalid values. | |
| 163 MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, volume_label); | |
| 164 | |
| 165 const char* value = | 156 const char* value = |
| 166 device::udev_device_get_sysattr_value(device.get(), kRemovableSysAttr); | 157 device::udev_device_get_sysattr_value(device.get(), kRemovableSysAttr); |
| 167 if (!value) { | 158 if (!value) { |
| 168 // |parent_device| is owned by |device| and does not need to be cleaned | 159 // |parent_device| is owned by |device| and does not need to be cleaned |
| 169 // up. | 160 // up. |
| 170 struct udev_device* parent_device = | 161 struct udev_device* parent_device = |
| 171 device::udev_device_get_parent_with_subsystem_devtype( | 162 device::udev_device_get_parent_with_subsystem_devtype( |
| 172 device.get(), | 163 device.get(), |
| 173 kBlockSubsystemKey, | 164 kBlockSubsystemKey, |
| 174 kDiskDeviceTypeKey); | 165 kDiskDeviceTypeKey); |
| 175 value = device::udev_device_get_sysattr_value(parent_device, | 166 value = device::udev_device_get_sysattr_value(parent_device, |
| 176 kRemovableSysAttr); | 167 kRemovableSysAttr); |
| 177 } | 168 } |
| 178 const bool is_removable = (value && atoi(value) == 1); | 169 const bool is_removable = (value && atoi(value) == 1); |
| 179 | 170 |
| 180 StorageInfo::Type type = StorageInfo::FIXED_MASS_STORAGE; | 171 StorageInfo::Type type = StorageInfo::FIXED_MASS_STORAGE; |
| 181 if (is_removable) { | 172 if (is_removable) { |
| 182 if (MediaStorageUtil::HasDcim(mount_point)) | 173 type = MediaStorageUtil::HasDcim(mount_point) |
| 183 type = StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM; | 174 ? StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM |
| 184 else | 175 : StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM; |
| 185 type = StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM; | |
| 186 } | 176 } |
| 187 | 177 |
| 188 results_recorder.set_result(true); | 178 results_recorder.set_result(true); |
| 189 | 179 |
| 190 storage_info.reset(new StorageInfo( | 180 storage_info = base::MakeUnique<StorageInfo>( |
| 191 StorageInfo::MakeDeviceId(type, unique_id), | 181 StorageInfo::MakeDeviceId(type, unique_id), mount_point.value(), |
| 192 mount_point.value(), | 182 volume_label, vendor_name, model_name, |
| 193 volume_label, | 183 GetDeviceStorageSize(device_path, device.get())); |
| 194 vendor_name, | |
| 195 model_name, | |
| 196 GetDeviceStorageSize(device_path, device.get()))); | |
| 197 return storage_info; | 184 return storage_info; |
| 198 } | 185 } |
| 199 | 186 |
| 200 MtabWatcherLinux* CreateMtabWatcherLinuxOnFileThread( | 187 MtabWatcherLinux* CreateMtabWatcherLinuxOnFileThread( |
| 201 const base::FilePath& mtab_path, | 188 const base::FilePath& mtab_path, |
| 202 base::WeakPtr<MtabWatcherLinux::Delegate> delegate) { | 189 base::WeakPtr<MtabWatcherLinux::Delegate> delegate) { |
| 203 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 190 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 204 // Owned by caller. | 191 // Owned by caller. |
| 205 return new MtabWatcherLinux(mtab_path, delegate); | 192 return new MtabWatcherLinux(mtab_path, delegate); |
| 206 } | 193 } |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 mount_priority_map_[mount_device][mount_point] = removable; | 459 mount_priority_map_[mount_device][mount_point] = removable; |
| 473 receiver()->ProcessAttach(*storage_info); | 460 receiver()->ProcessAttach(*storage_info); |
| 474 } | 461 } |
| 475 | 462 |
| 476 StorageMonitor* StorageMonitor::CreateInternal() { | 463 StorageMonitor* StorageMonitor::CreateInternal() { |
| 477 const base::FilePath kDefaultMtabPath("/etc/mtab"); | 464 const base::FilePath kDefaultMtabPath("/etc/mtab"); |
| 478 return new StorageMonitorLinux(kDefaultMtabPath); | 465 return new StorageMonitorLinux(kDefaultMtabPath); |
| 479 } | 466 } |
| 480 | 467 |
| 481 } // namespace storage_monitor | 468 } // namespace storage_monitor |
| OLD | NEW |