| 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/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" | 15 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" |
| 16 #include "chrome/browser/system_monitor/media_storage_util.h" | 16 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 17 #include "chrome/browser/system_monitor/removable_device_constants.h" | 17 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 | 19 |
| 20 namespace chromeos { | 20 namespace chromeos { |
| 21 | 21 |
| 22 using base::SystemMonitor; |
| 23 |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| 24 // Construct a device name using label or manufacturer (vendor and product) name | 26 // Construct a device name using label or manufacturer (vendor and product) name |
| 25 // details. | 27 // details. |
| 26 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) { | 28 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) { |
| 27 std::string device_name = disk.device_label(); | 29 std::string device_name = disk.device_label(); |
| 28 if (device_name.empty()) { | 30 if (device_name.empty()) { |
| 29 device_name = disk.vendor_name(); | 31 device_name = disk.vendor_name(); |
| 30 const std::string& product_name = disk.product_name(); | 32 const std::string& product_name = disk.product_name(); |
| 31 if (!product_name.empty()) { | 33 if (!product_name.empty()) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 52 const std::string& vendor = disk.vendor_id(); | 54 const std::string& vendor = disk.vendor_id(); |
| 53 const std::string& product = disk.product_id(); | 55 const std::string& product = disk.product_id(); |
| 54 if (vendor.empty() && product.empty()) | 56 if (vendor.empty() && product.empty()) |
| 55 return std::string(); | 57 return std::string(); |
| 56 return base::StringPrintf("%s%s%s%s%s", | 58 return base::StringPrintf("%s%s%s%s%s", |
| 57 chrome::kVendorModelSerialPrefix, | 59 chrome::kVendorModelSerialPrefix, |
| 58 vendor.c_str(), chrome::kNonSpaceDelim, | 60 vendor.c_str(), chrome::kNonSpaceDelim, |
| 59 product.c_str(), chrome::kNonSpaceDelim); | 61 product.c_str(), chrome::kNonSpaceDelim); |
| 60 } | 62 } |
| 61 | 63 |
| 64 static RemovableDeviceNotificationsCros* |
| 65 g_removable_device_notifications_chromeos = NULL; |
| 66 |
| 62 // Returns true if the requested device is valid, else false. On success, fills | 67 // Returns true if the requested device is valid, else false. On success, fills |
| 63 // in |unique_id| and |device_label| | 68 // in |unique_id| and |device_label| |
| 64 bool GetDeviceInfo(const std::string& source_path, std::string* unique_id, | 69 bool GetDeviceInfo(const std::string& source_path, std::string* unique_id, |
| 65 string16* device_label) { | 70 string16* device_label) { |
| 66 // Get the media device uuid and label if exists. | 71 // Get the media device uuid and label if exists. |
| 67 const disks::DiskMountManager::Disk* disk = | 72 const disks::DiskMountManager::Disk* disk = |
| 68 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); | 73 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); |
| 69 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) | 74 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) |
| 70 return false; | 75 return false; |
| 71 | 76 |
| 72 if (unique_id) | 77 if (unique_id) |
| 73 *unique_id = MakeDeviceUniqueId(*disk); | 78 *unique_id = MakeDeviceUniqueId(*disk); |
| 74 | 79 |
| 75 if (device_label) | 80 if (device_label) |
| 76 *device_label = GetDeviceName(*disk); | 81 *device_label = GetDeviceName(*disk); |
| 77 return true; | 82 return true; |
| 78 } | 83 } |
| 79 | 84 |
| 80 } // namespace | 85 } // namespace |
| 81 | 86 |
| 82 using chrome::MediaStorageUtil; | 87 using chrome::MediaStorageUtil; |
| 83 using content::BrowserThread; | 88 using content::BrowserThread; |
| 84 | 89 |
| 85 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() { | 90 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() { |
| 86 DCHECK(disks::DiskMountManager::GetInstance()); | 91 DCHECK(disks::DiskMountManager::GetInstance()); |
| 92 DCHECK(!g_removable_device_notifications_chromeos); |
| 93 g_removable_device_notifications_chromeos = this; |
| 87 disks::DiskMountManager::GetInstance()->AddObserver(this); | 94 disks::DiskMountManager::GetInstance()->AddObserver(this); |
| 88 CheckExistingMountPointsOnUIThread(); | 95 CheckExistingMountPointsOnUIThread(); |
| 89 } | 96 } |
| 90 | 97 |
| 91 RemovableDeviceNotificationsCros::~RemovableDeviceNotificationsCros() { | 98 RemovableDeviceNotificationsCros::~RemovableDeviceNotificationsCros() { |
| 99 DCHECK_EQ(this, g_removable_device_notifications_chromeos); |
| 100 g_removable_device_notifications_chromeos = NULL; |
| 92 disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance(); | 101 disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance(); |
| 93 if (manager) { | 102 if (manager) { |
| 94 manager->RemoveObserver(this); | 103 manager->RemoveObserver(this); |
| 95 } | 104 } |
| 96 } | 105 } |
| 97 | 106 |
| 107 // static |
| 108 RemovableDeviceNotificationsCros* |
| 109 RemovableDeviceNotificationsCros::GetInstance() { |
| 110 return g_removable_device_notifications_chromeos; |
| 111 } |
| 112 |
| 98 void RemovableDeviceNotificationsCros::CheckExistingMountPointsOnUIThread() { | 113 void RemovableDeviceNotificationsCros::CheckExistingMountPointsOnUIThread() { |
| 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 100 const disks::DiskMountManager::MountPointMap& mount_point_map = | 115 const disks::DiskMountManager::MountPointMap& mount_point_map = |
| 101 disks::DiskMountManager::GetInstance()->mount_points(); | 116 disks::DiskMountManager::GetInstance()->mount_points(); |
| 102 for (disks::DiskMountManager::MountPointMap::const_iterator it = | 117 for (disks::DiskMountManager::MountPointMap::const_iterator it = |
| 103 mount_point_map.begin(); it != mount_point_map.end(); ++it) { | 118 mount_point_map.begin(); it != mount_point_map.end(); ++it) { |
| 104 BrowserThread::PostTask( | 119 BrowserThread::PostTask( |
| 105 BrowserThread::FILE, FROM_HERE, | 120 BrowserThread::FILE, FROM_HERE, |
| 106 base::Bind( | 121 base::Bind( |
| 107 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, | 122 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 BrowserThread::FILE, FROM_HERE, | 160 BrowserThread::FILE, FROM_HERE, |
| 146 base::Bind( | 161 base::Bind( |
| 147 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, | 162 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, |
| 148 this, mount_info)); | 163 this, mount_info)); |
| 149 break; | 164 break; |
| 150 } | 165 } |
| 151 case disks::DiskMountManager::UNMOUNTING: { | 166 case disks::DiskMountManager::UNMOUNTING: { |
| 152 MountMap::iterator it = mount_map_.find(mount_info.mount_path); | 167 MountMap::iterator it = mount_map_.find(mount_info.mount_path); |
| 153 if (it == mount_map_.end()) | 168 if (it == mount_map_.end()) |
| 154 return; | 169 return; |
| 155 base::SystemMonitor::Get()->ProcessRemovableStorageDetached(it->second); | 170 SystemMonitor::Get()->ProcessRemovableStorageDetached( |
| 171 it->second.device_id); |
| 156 mount_map_.erase(it); | 172 mount_map_.erase(it); |
| 157 break; | 173 break; |
| 158 } | 174 } |
| 159 } | 175 } |
| 160 } | 176 } |
| 161 | 177 |
| 178 bool RemovableDeviceNotificationsCros::GetDeviceInfoForPath( |
| 179 const FilePath& path, |
| 180 SystemMonitor::RemovableStorageInfo* device_info) const { |
| 181 if (!path.IsAbsolute()) |
| 182 return false; |
| 183 |
| 184 FilePath current = path; |
| 185 while (!ContainsKey(mount_map_, current.value()) && |
| 186 current != current.DirName()) { |
| 187 current = current.DirName(); |
| 188 } |
| 189 |
| 190 MountMap::const_iterator info_it = mount_map_.find(current.value()); |
| 191 if (info_it == mount_map_.end()) |
| 192 return false; |
| 193 |
| 194 if (device_info) |
| 195 *device_info = info_it->second; |
| 196 return true; |
| 197 } |
| 198 |
| 162 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( | 199 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( |
| 163 const disks::DiskMountManager::MountPointInfo& mount_info) { | 200 const disks::DiskMountManager::MountPointInfo& mount_info) { |
| 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 165 | 202 |
| 166 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); | 203 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); |
| 167 | 204 |
| 168 BrowserThread::PostTask( | 205 BrowserThread::PostTask( |
| 169 BrowserThread::UI, FROM_HERE, | 206 BrowserThread::UI, FROM_HERE, |
| 170 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, | 207 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, |
| 171 this, mount_info, has_dcim)); | 208 this, mount_info, has_dcim)); |
| 172 } | 209 } |
| 173 | 210 |
| 174 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread( | 211 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread( |
| 175 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { | 212 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { |
| 176 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 177 | 214 |
| 178 if (ContainsKey(mount_map_, mount_info.mount_path)) { | 215 if (ContainsKey(mount_map_, mount_info.mount_path)) { |
| 179 NOTREACHED(); | 216 // CheckExistingMountPointsOnUIThread() added the mount point information |
| 217 // in the map before the device attached handler is called. Therefore, an |
| 218 // entry for the device already exists in the map. |
| 180 return; | 219 return; |
| 181 } | 220 } |
| 182 | 221 |
| 183 // Get the media device uuid and label if exists. | 222 // Get the media device uuid and label if exists. |
| 184 std::string unique_id; | 223 std::string unique_id; |
| 185 string16 device_label; | 224 string16 device_label; |
| 186 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label)) | 225 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label)) |
| 187 return; | 226 return; |
| 188 | 227 |
| 189 // Keep track of device uuid and label, to see how often we receive empty | 228 // Keep track of device uuid and label, to see how often we receive empty |
| 190 // values. | 229 // values. |
| 191 MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, device_label); | 230 MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, device_label); |
| 192 if (unique_id.empty() || device_label.empty()) | 231 if (unique_id.empty() || device_label.empty()) |
| 193 return; | 232 return; |
| 194 | 233 |
| 195 MediaStorageUtil::Type type = has_dcim ? | 234 MediaStorageUtil::Type type = has_dcim ? |
| 196 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : | 235 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : |
| 197 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; | 236 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; |
| 198 | 237 |
| 199 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); | 238 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, |
| 200 mount_map_.insert(std::make_pair(mount_info.mount_path, device_id)); | 239 unique_id); |
| 201 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( | 240 SystemMonitor::RemovableStorageInfo info(device_id, device_label, |
| 241 mount_info.mount_path); |
| 242 mount_map_.insert(std::make_pair(mount_info.mount_path, info)); |
| 243 SystemMonitor::Get()->ProcessRemovableStorageAttached( |
| 202 device_id, | 244 device_id, |
| 203 device_label, | 245 device_label, |
| 204 mount_info.mount_path); | 246 mount_info.mount_path); |
| 205 } | 247 } |
| 206 | 248 |
| 207 } // namespace chrome | 249 } // namespace chrome |
| OLD | NEW |