| 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::StorageMonitorCros implementation. | 5 // chromeos::StorageMonitorCros implementation. |
| 6 | 6 |
| 7 #include "chrome/browser/storage_monitor/storage_monitor_chromeos.h" | 7 #include "chrome/browser/storage_monitor/storage_monitor_chromeos.h" |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 namespace chromeos { | 22 namespace chromeos { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // Constructs a device id using uuid or manufacturer (vendor and product) id | 26 // Constructs a device id using uuid or manufacturer (vendor and product) id |
| 27 // details. | 27 // details. |
| 28 std::string MakeDeviceUniqueId(const disks::DiskMountManager::Disk& disk) { | 28 std::string MakeDeviceUniqueId(const disks::DiskMountManager::Disk& disk) { |
| 29 std::string uuid = disk.fs_uuid(); | 29 std::string uuid = disk.fs_uuid(); |
| 30 if (!uuid.empty()) | 30 if (!uuid.empty()) |
| 31 return chrome::kFSUniqueIdPrefix + uuid; | 31 return kFSUniqueIdPrefix + uuid; |
| 32 | 32 |
| 33 // If one of the vendor or product information is missing, its value in the | 33 // If one of the vendor or product information is missing, its value in the |
| 34 // string is empty. | 34 // string is empty. |
| 35 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialInfo | 35 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialInfo |
| 36 // TODO(kmadhusu) Extract serial information for the disks and append it to | 36 // TODO(kmadhusu) Extract serial information for the disks and append it to |
| 37 // the device unique id. | 37 // the device unique id. |
| 38 const std::string& vendor = disk.vendor_id(); | 38 const std::string& vendor = disk.vendor_id(); |
| 39 const std::string& product = disk.product_id(); | 39 const std::string& product = disk.product_id(); |
| 40 if (vendor.empty() && product.empty()) | 40 if (vendor.empty() && product.empty()) |
| 41 return std::string(); | 41 return std::string(); |
| 42 return chrome::kVendorModelSerialPrefix + vendor + ":" + product + ":"; | 42 return kVendorModelSerialPrefix + vendor + ":" + product + ":"; |
| 43 } | 43 } |
| 44 | 44 |
| 45 // Returns true if the requested device is valid, else false. On success, fills | 45 // Returns true if the requested device is valid, else false. On success, fills |
| 46 // in |info|. | 46 // in |info|. |
| 47 bool GetDeviceInfo(const disks::DiskMountManager::MountPointInfo& mount_info, | 47 bool GetDeviceInfo(const disks::DiskMountManager::MountPointInfo& mount_info, |
| 48 bool has_dcim, | 48 bool has_dcim, |
| 49 chrome::StorageInfo* info) { | 49 StorageInfo* info) { |
| 50 DCHECK(info); | 50 DCHECK(info); |
| 51 std::string source_path = mount_info.source_path; | 51 std::string source_path = mount_info.source_path; |
| 52 | 52 |
| 53 const disks::DiskMountManager::Disk* disk = | 53 const disks::DiskMountManager::Disk* disk = |
| 54 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); | 54 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); |
| 55 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) | 55 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) |
| 56 return false; | 56 return false; |
| 57 | 57 |
| 58 std::string unique_id = MakeDeviceUniqueId(*disk); | 58 std::string unique_id = MakeDeviceUniqueId(*disk); |
| 59 // Keep track of device uuid and label, to see how often we receive empty | 59 // Keep track of device uuid and label, to see how often we receive empty |
| 60 // values. | 60 // values. |
| 61 string16 device_label = UTF8ToUTF16(disk->device_label()); | 61 string16 device_label = UTF8ToUTF16(disk->device_label()); |
| 62 chrome::MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, | 62 MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, device_label); |
| 63 device_label); | |
| 64 if (unique_id.empty()) | 63 if (unique_id.empty()) |
| 65 return false; | 64 return false; |
| 66 | 65 |
| 67 chrome::StorageInfo::Type type = has_dcim ? | 66 StorageInfo::Type type = has_dcim ? |
| 68 chrome::StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM : | 67 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM : |
| 69 chrome::StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM; | 68 StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM; |
| 70 | 69 |
| 71 *info = chrome::StorageInfo( | 70 *info = StorageInfo(StorageInfo::MakeDeviceId(type, unique_id), |
| 72 chrome::StorageInfo::MakeDeviceId(type, unique_id), | 71 string16(), |
| 73 string16(), | 72 mount_info.mount_path, |
| 74 mount_info.mount_path, | 73 device_label, |
| 75 device_label, | 74 UTF8ToUTF16(disk->vendor_name()), |
| 76 UTF8ToUTF16(disk->vendor_name()), | 75 UTF8ToUTF16(disk->product_name()), |
| 77 UTF8ToUTF16(disk->product_name()), | 76 disk->total_size_in_bytes()); |
| 78 disk->total_size_in_bytes()); | |
| 79 return true; | 77 return true; |
| 80 } | 78 } |
| 81 | 79 |
| 82 // Returns whether the mount point in |mount_info| is a media device or not. | 80 // Returns whether the mount point in |mount_info| is a media device or not. |
| 83 bool CheckMountedPathOnFileThread( | 81 bool CheckMountedPathOnFileThread( |
| 84 const disks::DiskMountManager::MountPointInfo& mount_info) { | 82 const disks::DiskMountManager::MountPointInfo& mount_info) { |
| 85 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 83 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 86 return chrome::MediaStorageUtil::HasDcim( | 84 return MediaStorageUtil::HasDcim(base::FilePath(mount_info.mount_path)); |
| 87 base::FilePath(mount_info.mount_path)); | |
| 88 } | 85 } |
| 89 | 86 |
| 90 } // namespace | 87 } // namespace |
| 91 | 88 |
| 92 using content::BrowserThread; | 89 using content::BrowserThread; |
| 93 using chrome::StorageInfo; | |
| 94 | 90 |
| 95 StorageMonitorCros::StorageMonitorCros() | 91 StorageMonitorCros::StorageMonitorCros() |
| 96 : weak_ptr_factory_(this) { | 92 : weak_ptr_factory_(this) { |
| 97 } | 93 } |
| 98 | 94 |
| 99 StorageMonitorCros::~StorageMonitorCros() { | 95 StorageMonitorCros::~StorageMonitorCros() { |
| 100 disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance(); | 96 disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance(); |
| 101 if (manager) { | 97 if (manager) { |
| 102 manager->RemoveObserver(this); | 98 manager->RemoveObserver(this); |
| 103 } | 99 } |
| 104 } | 100 } |
| 105 | 101 |
| 106 void StorageMonitorCros::Init() { | 102 void StorageMonitorCros::Init() { |
| 107 DCHECK(disks::DiskMountManager::GetInstance()); | 103 DCHECK(disks::DiskMountManager::GetInstance()); |
| 108 disks::DiskMountManager::GetInstance()->AddObserver(this); | 104 disks::DiskMountManager::GetInstance()->AddObserver(this); |
| 109 CheckExistingMountPoints(); | 105 CheckExistingMountPoints(); |
| 110 | 106 |
| 111 if (!media_transfer_protocol_manager_) { | 107 if (!media_transfer_protocol_manager_) { |
| 112 scoped_refptr<base::MessageLoopProxy> loop_proxy; | 108 scoped_refptr<base::MessageLoopProxy> loop_proxy; |
| 113 media_transfer_protocol_manager_.reset( | 109 media_transfer_protocol_manager_.reset( |
| 114 device::MediaTransferProtocolManager::Initialize(loop_proxy)); | 110 device::MediaTransferProtocolManager::Initialize(loop_proxy)); |
| 115 } | 111 } |
| 116 | 112 |
| 117 media_transfer_protocol_device_observer_.reset( | 113 media_transfer_protocol_device_observer_.reset( |
| 118 new chrome::MediaTransferProtocolDeviceObserverLinux( | 114 new MediaTransferProtocolDeviceObserverLinux( |
| 119 receiver(), media_transfer_protocol_manager_.get())); | 115 receiver(), media_transfer_protocol_manager_.get())); |
| 120 } | 116 } |
| 121 | 117 |
| 122 void StorageMonitorCros::CheckExistingMountPoints() { | 118 void StorageMonitorCros::CheckExistingMountPoints() { |
| 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 124 const disks::DiskMountManager::MountPointMap& mount_point_map = | 120 const disks::DiskMountManager::MountPointMap& mount_point_map = |
| 125 disks::DiskMountManager::GetInstance()->mount_points(); | 121 disks::DiskMountManager::GetInstance()->mount_points(); |
| 126 for (disks::DiskMountManager::MountPointMap::const_iterator it = | 122 for (disks::DiskMountManager::MountPointMap::const_iterator it = |
| 127 mount_point_map.begin(); it != mount_point_map.end(); ++it) { | 123 mount_point_map.begin(); it != mount_point_map.end(); ++it) { |
| 128 BrowserThread::PostTaskAndReplyWithResult( | 124 BrowserThread::PostTaskAndReplyWithResult( |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 if (info_it == mount_map_.end()) | 225 if (info_it == mount_map_.end()) |
| 230 return false; | 226 return false; |
| 231 | 227 |
| 232 *device_info = info_it->second; | 228 *device_info = info_it->second; |
| 233 return true; | 229 return true; |
| 234 } | 230 } |
| 235 | 231 |
| 236 // Callback executed when the unmount call is run by DiskMountManager. | 232 // Callback executed when the unmount call is run by DiskMountManager. |
| 237 // Forwards result to |EjectDevice| caller. | 233 // Forwards result to |EjectDevice| caller. |
| 238 void NotifyUnmountResult( | 234 void NotifyUnmountResult( |
| 239 base::Callback<void(chrome::StorageMonitor::EjectStatus)> callback, | 235 base::Callback<void(StorageMonitor::EjectStatus)> callback, |
| 240 chromeos::MountError error_code) { | 236 chromeos::MountError error_code) { |
| 241 if (error_code == MOUNT_ERROR_NONE) | 237 if (error_code == MOUNT_ERROR_NONE) |
| 242 callback.Run(chrome::StorageMonitor::EJECT_OK); | 238 callback.Run(StorageMonitor::EJECT_OK); |
| 243 else | 239 else |
| 244 callback.Run(chrome::StorageMonitor::EJECT_FAILURE); | 240 callback.Run(StorageMonitor::EJECT_FAILURE); |
| 245 } | 241 } |
| 246 | 242 |
| 247 void StorageMonitorCros::EjectDevice( | 243 void StorageMonitorCros::EjectDevice( |
| 248 const std::string& device_id, | 244 const std::string& device_id, |
| 249 base::Callback<void(EjectStatus)> callback) { | 245 base::Callback<void(EjectStatus)> callback) { |
| 250 StorageInfo::Type type; | 246 StorageInfo::Type type; |
| 251 if (!StorageInfo::CrackDeviceId(device_id, &type, NULL)) { | 247 if (!StorageInfo::CrackDeviceId(device_id, &type, NULL)) { |
| 252 callback.Run(EJECT_FAILURE); | 248 callback.Run(EJECT_FAILURE); |
| 253 return; | 249 return; |
| 254 } | 250 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 291 | 287 |
| 292 if (ContainsKey(mount_map_, mount_info.mount_path)) { | 288 if (ContainsKey(mount_map_, mount_info.mount_path)) { |
| 293 // CheckExistingMountPointsOnUIThread() added the mount point information | 289 // CheckExistingMountPointsOnUIThread() added the mount point information |
| 294 // in the map before the device attached handler is called. Therefore, an | 290 // in the map before the device attached handler is called. Therefore, an |
| 295 // entry for the device already exists in the map. | 291 // entry for the device already exists in the map. |
| 296 return; | 292 return; |
| 297 } | 293 } |
| 298 | 294 |
| 299 // Get the media device uuid and label if exists. | 295 // Get the media device uuid and label if exists. |
| 300 chrome::StorageInfo info; | 296 StorageInfo info; |
| 301 if (!GetDeviceInfo(mount_info, has_dcim, &info)) | 297 if (!GetDeviceInfo(mount_info, has_dcim, &info)) |
| 302 return; | 298 return; |
| 303 | 299 |
| 304 if (info.device_id().empty()) | 300 if (info.device_id().empty()) |
| 305 return; | 301 return; |
| 306 | 302 |
| 307 mount_map_.insert(std::make_pair(mount_info.mount_path, info)); | 303 mount_map_.insert(std::make_pair(mount_info.mount_path, info)); |
| 308 | 304 |
| 309 receiver()->ProcessAttach(info); | 305 receiver()->ProcessAttach(info); |
| 310 } | 306 } |
| 311 | 307 |
| 312 } // namespace chromeos | 308 } // namespace chromeos |
| 313 | 309 |
| 314 namespace chrome { | |
| 315 | |
| 316 StorageMonitor* StorageMonitor::Create() { | 310 StorageMonitor* StorageMonitor::Create() { |
| 317 return new chromeos::StorageMonitorCros(); | 311 return new chromeos::StorageMonitorCros(); |
| 318 } | 312 } |
| 319 | |
| 320 } // namespace chrome | |
| OLD | NEW |