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 // chromeos::RemovableDeviceNotificationsCros implementation. | 5 // chromeos::RemovableDeviceNotificationsCros implementation. |
| 6 | 6 |
| 7 #include "chrome/browser/system_monitor/removable_device_notifications_chromeos. h" | 7 #include "chrome/browser/system_monitor/removable_device_notifications_chromeos. h" |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" | 14 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" |
| 15 #include "chrome/browser/system_monitor/media_storage_util.h" | 15 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 16 #include "chrome/browser/system_monitor/removable_device_constants.h" | 16 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 | 18 |
| 19 namespace chromeos { | 19 namespace chromeos { |
| 20 | 20 |
| 21 using base::SystemMonitor; | 21 using base::SystemMonitor; |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // Construct a device name using label or manufacturer (vendor and product) name | 25 // Returns a human readable device size string, e.g if the device total size |
| 26 // details. | 26 // is 2048 bytes, this function returns "2KB". |
| 27 string16 GetDeviceSizeAsString(uint64 total_size_in_bytes) { | |
|
Lei Zhang
2012/11/08 07:42:44
I think you have reinvented the unlocalized versio
kmadhusu
2012/11/08 18:50:35
oops.. I didn't know that. Fixed.
| |
| 28 uint index = 0; | |
| 29 double block_size = 1024.0; | |
| 30 double size_in_bytes = static_cast<double>(total_size_in_bytes); | |
| 31 for (; ((size_in_bytes / block_size > 1.0) && (index < 5)); ++index) | |
| 32 size_in_bytes /= block_size; | |
| 33 | |
| 34 std::string block_name; | |
| 35 switch (index) { | |
| 36 case 0: | |
| 37 block_name = "bytes"; | |
| 38 break; | |
| 39 case 1: | |
| 40 block_name = "KB"; | |
| 41 break; | |
| 42 case 2: | |
| 43 block_name = "MB"; | |
| 44 break; | |
| 45 case 3: | |
| 46 block_name = "GB"; | |
| 47 break; | |
| 48 case 4: | |
| 49 block_name = "TB"; | |
| 50 break; | |
| 51 case 5: | |
| 52 block_name = "PB"; | |
| 53 break; | |
| 54 default: | |
| 55 NOTREACHED(); | |
| 56 } | |
| 57 return base::UintToString16(static_cast<uint64>(size_in_bytes)) + | |
| 58 ASCIIToUTF16(block_name); | |
| 59 } | |
| 60 | |
| 61 // Constructs a device name using label or manufacturer (vendor and product) | |
| 62 // name details. | |
| 27 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) { | 63 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) { |
| 28 std::string device_name = disk.device_label(); | 64 std::string device_name = (disk.device_type() == DEVICE_TYPE_SD) ? |
| 29 if (device_name.empty()) { | 65 "SD Card" : disk.device_label(); |
|
Lei Zhang
2012/11/08 07:42:44
Similarly, "SD Card" may not work well for other l
kmadhusu
2012/11/08 18:50:35
ChromeOS file manager is not localizing this text.
Lei Zhang
2012/11/09 01:12:18
Ok, can you add a comment for that?
kmadhusu
2012/11/09 21:59:40
As we discussed, we will be showing the base name
| |
| 30 device_name = disk.vendor_name(); | 66 if (device_name.empty()) |
| 31 const std::string& product_name = disk.product_name(); | 67 device_name = "{" + disk.vendor_name() + ", " + disk.product_name() + "}"; |
|
Lei Zhang
2012/11/08 07:42:44
Did you and Steve decide to use braces?
kmadhusu
2012/11/08 18:50:35
Yes (along with the PM's).
| |
| 32 if (!product_name.empty()) { | |
| 33 if (!device_name.empty()) | |
| 34 device_name += " "; | |
| 35 device_name += product_name; | |
| 36 } | |
| 37 } | |
| 38 return UTF8ToUTF16(device_name); | 68 return UTF8ToUTF16(device_name); |
| 39 } | 69 } |
| 40 | 70 |
| 41 // Construct a device id using uuid or manufacturer (vendor and product) id | 71 // Constructs a device id using uuid or manufacturer (vendor and product) id |
| 42 // details. | 72 // details. |
| 43 std::string MakeDeviceUniqueId(const disks::DiskMountManager::Disk& disk) { | 73 std::string MakeDeviceUniqueId(const disks::DiskMountManager::Disk& disk) { |
| 44 std::string uuid = disk.fs_uuid(); | 74 std::string uuid = disk.fs_uuid(); |
| 45 if (!uuid.empty()) | 75 if (!uuid.empty()) |
| 46 return chrome::kFSUniqueIdPrefix + uuid; | 76 return chrome::kFSUniqueIdPrefix + uuid; |
| 47 | 77 |
| 48 // If one of the vendor or product information is missing, its value in the | 78 // If one of the vendor or product information is missing, its value in the |
| 49 // string is empty. | 79 // string is empty. |
| 50 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialInfo | 80 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialInfo |
| 51 // TODO(kmadhusu) Extract serial information for the disks and append it to | 81 // TODO(kmadhusu) Extract serial information for the disks and append it to |
| 52 // the device unique id. | 82 // the device unique id. |
| 53 const std::string& vendor = disk.vendor_id(); | 83 const std::string& vendor = disk.vendor_id(); |
| 54 const std::string& product = disk.product_id(); | 84 const std::string& product = disk.product_id(); |
| 55 if (vendor.empty() && product.empty()) | 85 if (vendor.empty() && product.empty()) |
| 56 return std::string(); | 86 return std::string(); |
| 57 return chrome::kVendorModelSerialPrefix + vendor + ":" + product + ":"; | 87 return chrome::kVendorModelSerialPrefix + vendor + ":" + product + ":"; |
| 58 } | 88 } |
| 59 | 89 |
| 60 static RemovableDeviceNotificationsCros* | 90 static RemovableDeviceNotificationsCros* |
| 61 g_removable_device_notifications_chromeos = NULL; | 91 g_removable_device_notifications_chromeos = NULL; |
| 62 | 92 |
| 63 // Returns true if the requested device is valid, else false. On success, fills | 93 // Returns true if the requested device is valid, else false. On success, fills |
| 64 // in |unique_id| and |device_label| | 94 // in |unique_id|, |device_label| and |device_size|. |
| 65 bool GetDeviceInfo(const std::string& source_path, std::string* unique_id, | 95 bool GetDeviceInfo(const std::string& source_path, |
| 66 string16* device_label) { | 96 std::string* unique_id, |
| 67 // Get the media device uuid and label if exists. | 97 string16* device_label, |
| 98 string16* device_size) { | |
| 99 DCHECK(device_size); | |
| 68 const disks::DiskMountManager::Disk* disk = | 100 const disks::DiskMountManager::Disk* disk = |
| 69 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); | 101 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); |
| 70 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) | 102 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) |
| 71 return false; | 103 return false; |
| 72 | 104 |
| 73 if (unique_id) | 105 if (unique_id) |
| 74 *unique_id = MakeDeviceUniqueId(*disk); | 106 *unique_id = MakeDeviceUniqueId(*disk); |
| 75 | 107 |
| 76 if (device_label) | 108 if (device_label) |
| 77 *device_label = GetDeviceName(*disk); | 109 *device_label = GetDeviceName(*disk); |
| 110 *device_size = GetDeviceSizeAsString(disk->total_size_in_bytes()); | |
| 78 return true; | 111 return true; |
| 79 } | 112 } |
| 80 | 113 |
| 81 } // namespace | 114 } // namespace |
| 82 | 115 |
| 83 using chrome::MediaStorageUtil; | |
| 84 using content::BrowserThread; | 116 using content::BrowserThread; |
| 85 | 117 |
| 86 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() { | 118 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() { |
| 87 DCHECK(disks::DiskMountManager::GetInstance()); | 119 DCHECK(disks::DiskMountManager::GetInstance()); |
| 88 DCHECK(!g_removable_device_notifications_chromeos); | 120 DCHECK(!g_removable_device_notifications_chromeos); |
| 89 g_removable_device_notifications_chromeos = this; | 121 g_removable_device_notifications_chromeos = this; |
| 90 disks::DiskMountManager::GetInstance()->AddObserver(this); | 122 disks::DiskMountManager::GetInstance()->AddObserver(this); |
| 91 CheckExistingMountPointsOnUIThread(); | 123 CheckExistingMountPointsOnUIThread(); |
| 92 } | 124 } |
| 93 | 125 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 base::Bind( | 189 base::Bind( |
| 158 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, | 190 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, |
| 159 this, mount_info)); | 191 this, mount_info)); |
| 160 break; | 192 break; |
| 161 } | 193 } |
| 162 case disks::DiskMountManager::UNMOUNTING: { | 194 case disks::DiskMountManager::UNMOUNTING: { |
| 163 MountMap::iterator it = mount_map_.find(mount_info.mount_path); | 195 MountMap::iterator it = mount_map_.find(mount_info.mount_path); |
| 164 if (it == mount_map_.end()) | 196 if (it == mount_map_.end()) |
| 165 return; | 197 return; |
| 166 SystemMonitor::Get()->ProcessRemovableStorageDetached( | 198 SystemMonitor::Get()->ProcessRemovableStorageDetached( |
| 167 it->second.device_id); | 199 it->second.storage_info.device_id); |
| 168 mount_map_.erase(it); | 200 mount_map_.erase(it); |
| 169 break; | 201 break; |
| 170 } | 202 } |
| 171 } | 203 } |
| 172 } | 204 } |
| 173 | 205 |
| 174 bool RemovableDeviceNotificationsCros::GetDeviceInfoForPath( | 206 bool RemovableDeviceNotificationsCros::GetDeviceInfoForPath( |
| 175 const FilePath& path, | 207 const FilePath& path, |
| 176 SystemMonitor::RemovableStorageInfo* device_info) const { | 208 SystemMonitor::RemovableStorageInfo* device_info) const { |
| 177 if (!path.IsAbsolute()) | 209 if (!path.IsAbsolute()) |
| 178 return false; | 210 return false; |
| 179 | 211 |
| 180 FilePath current = path; | 212 FilePath current = path; |
| 181 while (!ContainsKey(mount_map_, current.value()) && | 213 while (!ContainsKey(mount_map_, current.value()) && |
| 182 current != current.DirName()) { | 214 current != current.DirName()) { |
| 183 current = current.DirName(); | 215 current = current.DirName(); |
| 184 } | 216 } |
| 185 | 217 |
| 186 MountMap::const_iterator info_it = mount_map_.find(current.value()); | 218 MountMap::const_iterator info_it = mount_map_.find(current.value()); |
| 187 if (info_it == mount_map_.end()) | 219 if (info_it == mount_map_.end()) |
| 188 return false; | 220 return false; |
| 189 | 221 |
| 190 if (device_info) | 222 if (device_info) |
| 191 *device_info = info_it->second; | 223 *device_info = info_it->second.storage_info; |
| 192 return true; | 224 return true; |
| 193 } | 225 } |
| 194 | 226 |
| 227 string16 RemovableDeviceNotificationsCros::GetStorageSizeInfo( | |
| 228 const std::string& device_location) { | |
| 229 MountMap::const_iterator info_it = mount_map_.find(device_location); | |
| 230 return (info_it != mount_map_.end()) ? | |
| 231 info_it->second.storage_size_info : string16(); | |
| 232 } | |
| 233 | |
| 195 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( | 234 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( |
| 196 const disks::DiskMountManager::MountPointInfo& mount_info) { | 235 const disks::DiskMountManager::MountPointInfo& mount_info) { |
| 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 198 | 237 |
| 199 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); | 238 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); |
| 200 | 239 |
| 201 BrowserThread::PostTask( | 240 BrowserThread::PostTask( |
| 202 BrowserThread::UI, FROM_HERE, | 241 BrowserThread::UI, FROM_HERE, |
| 203 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, | 242 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, |
| 204 this, mount_info, has_dcim)); | 243 this, mount_info, has_dcim)); |
| 205 } | 244 } |
| 206 | 245 |
| 207 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread( | 246 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread( |
| 208 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { | 247 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 210 | 249 |
| 211 if (ContainsKey(mount_map_, mount_info.mount_path)) { | 250 if (ContainsKey(mount_map_, mount_info.mount_path)) { |
| 212 // CheckExistingMountPointsOnUIThread() added the mount point information | 251 // CheckExistingMountPointsOnUIThread() added the mount point information |
| 213 // in the map before the device attached handler is called. Therefore, an | 252 // in the map before the device attached handler is called. Therefore, an |
| 214 // entry for the device already exists in the map. | 253 // entry for the device already exists in the map. |
| 215 return; | 254 return; |
| 216 } | 255 } |
| 217 | 256 |
| 218 // Get the media device uuid and label if exists. | 257 // Get the media device uuid and label if exists. |
| 219 std::string unique_id; | 258 std::string unique_id; |
| 220 string16 device_label; | 259 string16 device_label; |
| 221 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label)) | 260 string16 device_size; |
| 261 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label, | |
| 262 &device_size)) | |
| 222 return; | 263 return; |
| 223 | 264 |
| 224 // Keep track of device uuid and label, to see how often we receive empty | 265 // Keep track of device uuid and label, to see how often we receive empty |
| 225 // values. | 266 // values. |
| 226 MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, device_label); | 267 chrome::MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, |
| 268 device_label); | |
| 227 if (unique_id.empty() || device_label.empty()) | 269 if (unique_id.empty() || device_label.empty()) |
| 228 return; | 270 return; |
| 229 | 271 |
| 230 MediaStorageUtil::Type type = has_dcim ? | 272 chrome::MediaStorageUtil::Type type = has_dcim ? |
| 231 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : | 273 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : |
| 232 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; | 274 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; |
| 233 | 275 |
| 234 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, | 276 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, |
| 235 unique_id); | 277 unique_id); |
| 236 SystemMonitor::RemovableStorageInfo info(device_id, device_label, | 278 StorageObjectInfo object_info = { |
| 237 mount_info.mount_path); | 279 base::SystemMonitor::RemovableStorageInfo(device_id, device_label, |
| 238 mount_map_.insert(std::make_pair(mount_info.mount_path, info)); | 280 mount_info.mount_path), |
| 281 device_size | |
| 282 }; | |
| 283 mount_map_.insert(std::make_pair(mount_info.mount_path, object_info)); | |
| 239 SystemMonitor::Get()->ProcessRemovableStorageAttached( | 284 SystemMonitor::Get()->ProcessRemovableStorageAttached( |
| 240 device_id, | 285 device_id, |
| 241 device_label, | 286 device_size + ASCIIToUTF16(" ") + device_label, |
| 242 mount_info.mount_path); | 287 mount_info.mount_path); |
| 243 } | 288 } |
| 244 | 289 |
| 245 } // namespace chrome | 290 } // namespace chrome |
| OLD | NEW |