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/string_util.h" | |
13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
14 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" | 15 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" |
15 #include "chrome/browser/system_monitor/media_storage_util.h" | 16 #include "chrome/browser/system_monitor/media_storage_util.h" |
16 #include "chrome/browser/system_monitor/removable_device_constants.h" | 17 #include "chrome/browser/system_monitor/removable_device_constants.h" |
17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
18 | 19 |
19 namespace chromeos { | 20 namespace chromeos { |
20 | 21 |
21 using base::SystemMonitor; | 22 using base::SystemMonitor; |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // Construct a device name using label or manufacturer (vendor and product) name | 26 // Constructs a device name using label or manufacturer (vendor and product) |
26 // details. | 27 // name details. |
27 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) { | 28 string16 GetDeviceName(const disks::DiskMountManager::Disk& disk) { |
28 std::string device_name = disk.device_label(); | 29 if (disk.device_type() == DEVICE_TYPE_SD) { |
29 if (device_name.empty()) { | 30 // Mount path of an SD card will be one of the following: |
30 device_name = disk.vendor_name(); | 31 // (1) /media/removable/<volume_label> |
31 const std::string& product_name = disk.product_name(); | 32 // (2) /media/removable/SD Card |
32 if (!product_name.empty()) { | 33 // If the volume label is available, mount path will be (1) else (2). |
33 if (!device_name.empty()) | 34 FilePath mount_point(disk.mount_path()); |
34 device_name += " "; | 35 if (!mount_point.BaseName().LossyDisplayName().empty()) |
Lei Zhang
2012/11/12 23:20:52
Save this to a local variable to avoid calling it
kmadhusu
2012/11/13 01:03:58
Done.
| |
35 device_name += product_name; | 36 return mount_point.BaseName().LossyDisplayName(); |
36 } | |
37 } | 37 } |
38 return UTF8ToUTF16(device_name); | 38 |
39 const std::string& device_label = disk.device_label(); | |
40 if (!device_label.empty() && IsStringUTF8(device_label)) | |
41 return UTF8ToUTF16(device_label); | |
42 | |
43 const std::string name = chrome::GetFullProductName(disk.vendor_name(), | |
44 disk.product_name()); | |
45 return (!name.empty() && IsStringUTF8(name)) ? UTF8ToUTF16(name) : string16(); | |
39 } | 46 } |
40 | 47 |
41 // Construct a device id using uuid or manufacturer (vendor and product) id | 48 // Constructs a device id using uuid or manufacturer (vendor and product) id |
42 // details. | 49 // details. |
43 std::string MakeDeviceUniqueId(const disks::DiskMountManager::Disk& disk) { | 50 std::string MakeDeviceUniqueId(const disks::DiskMountManager::Disk& disk) { |
44 std::string uuid = disk.fs_uuid(); | 51 std::string uuid = disk.fs_uuid(); |
45 if (!uuid.empty()) | 52 if (!uuid.empty()) |
46 return chrome::kFSUniqueIdPrefix + uuid; | 53 return chrome::kFSUniqueIdPrefix + uuid; |
47 | 54 |
48 // If one of the vendor or product information is missing, its value in the | 55 // If one of the vendor or product information is missing, its value in the |
49 // string is empty. | 56 // string is empty. |
50 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialInfo | 57 // Format: VendorModelSerial:VendorInfo:ModelInfo:SerialInfo |
51 // TODO(kmadhusu) Extract serial information for the disks and append it to | 58 // TODO(kmadhusu) Extract serial information for the disks and append it to |
52 // the device unique id. | 59 // the device unique id. |
53 const std::string& vendor = disk.vendor_id(); | 60 const std::string& vendor = disk.vendor_id(); |
54 const std::string& product = disk.product_id(); | 61 const std::string& product = disk.product_id(); |
55 if (vendor.empty() && product.empty()) | 62 if (vendor.empty() && product.empty()) |
56 return std::string(); | 63 return std::string(); |
57 return chrome::kVendorModelSerialPrefix + vendor + ":" + product + ":"; | 64 return chrome::kVendorModelSerialPrefix + vendor + ":" + product + ":"; |
58 } | 65 } |
59 | 66 |
60 static RemovableDeviceNotificationsCros* | 67 static RemovableDeviceNotificationsCros* |
61 g_removable_device_notifications_chromeos = NULL; | 68 g_removable_device_notifications_chromeos = NULL; |
62 | 69 |
63 // Returns true if the requested device is valid, else false. On success, fills | 70 // Returns true if the requested device is valid, else false. On success, fills |
64 // in |unique_id| and |device_label| | 71 // in |unique_id|, |device_label| and |storage_size_in_bytes|. |
65 bool GetDeviceInfo(const std::string& source_path, std::string* unique_id, | 72 bool GetDeviceInfo(const std::string& source_path, |
66 string16* device_label) { | 73 std::string* unique_id, |
67 // Get the media device uuid and label if exists. | 74 string16* device_label, |
75 uint64* storage_size_in_bytes) { | |
68 const disks::DiskMountManager::Disk* disk = | 76 const disks::DiskMountManager::Disk* disk = |
69 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); | 77 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path); |
70 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) | 78 if (!disk || disk->device_type() == DEVICE_TYPE_UNKNOWN) |
71 return false; | 79 return false; |
72 | 80 |
73 if (unique_id) | 81 if (unique_id) |
74 *unique_id = MakeDeviceUniqueId(*disk); | 82 *unique_id = MakeDeviceUniqueId(*disk); |
75 | 83 |
76 if (device_label) | 84 if (device_label) |
77 *device_label = GetDeviceName(*disk); | 85 *device_label = GetDeviceName(*disk); |
86 | |
87 if (storage_size_in_bytes) | |
88 *storage_size_in_bytes = disk->total_size_in_bytes(); | |
78 return true; | 89 return true; |
79 } | 90 } |
80 | 91 |
81 } // namespace | 92 } // namespace |
82 | 93 |
83 using chrome::MediaStorageUtil; | |
84 using content::BrowserThread; | 94 using content::BrowserThread; |
85 | 95 |
86 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() { | 96 RemovableDeviceNotificationsCros::RemovableDeviceNotificationsCros() { |
87 DCHECK(disks::DiskMountManager::GetInstance()); | 97 DCHECK(disks::DiskMountManager::GetInstance()); |
88 DCHECK(!g_removable_device_notifications_chromeos); | 98 DCHECK(!g_removable_device_notifications_chromeos); |
89 g_removable_device_notifications_chromeos = this; | 99 g_removable_device_notifications_chromeos = this; |
90 disks::DiskMountManager::GetInstance()->AddObserver(this); | 100 disks::DiskMountManager::GetInstance()->AddObserver(this); |
91 CheckExistingMountPointsOnUIThread(); | 101 CheckExistingMountPointsOnUIThread(); |
92 } | 102 } |
93 | 103 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 base::Bind( | 167 base::Bind( |
158 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, | 168 &RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread, |
159 this, mount_info)); | 169 this, mount_info)); |
160 break; | 170 break; |
161 } | 171 } |
162 case disks::DiskMountManager::UNMOUNTING: { | 172 case disks::DiskMountManager::UNMOUNTING: { |
163 MountMap::iterator it = mount_map_.find(mount_info.mount_path); | 173 MountMap::iterator it = mount_map_.find(mount_info.mount_path); |
164 if (it == mount_map_.end()) | 174 if (it == mount_map_.end()) |
165 return; | 175 return; |
166 SystemMonitor::Get()->ProcessRemovableStorageDetached( | 176 SystemMonitor::Get()->ProcessRemovableStorageDetached( |
167 it->second.device_id); | 177 it->second.storage_info.device_id); |
168 mount_map_.erase(it); | 178 mount_map_.erase(it); |
169 break; | 179 break; |
170 } | 180 } |
171 } | 181 } |
172 } | 182 } |
173 | 183 |
174 bool RemovableDeviceNotificationsCros::GetDeviceInfoForPath( | 184 bool RemovableDeviceNotificationsCros::GetDeviceInfoForPath( |
175 const FilePath& path, | 185 const FilePath& path, |
176 SystemMonitor::RemovableStorageInfo* device_info) const { | 186 SystemMonitor::RemovableStorageInfo* device_info) const { |
177 if (!path.IsAbsolute()) | 187 if (!path.IsAbsolute()) |
178 return false; | 188 return false; |
179 | 189 |
180 FilePath current = path; | 190 FilePath current = path; |
181 while (!ContainsKey(mount_map_, current.value()) && | 191 while (!ContainsKey(mount_map_, current.value()) && |
182 current != current.DirName()) { | 192 current != current.DirName()) { |
183 current = current.DirName(); | 193 current = current.DirName(); |
184 } | 194 } |
185 | 195 |
186 MountMap::const_iterator info_it = mount_map_.find(current.value()); | 196 MountMap::const_iterator info_it = mount_map_.find(current.value()); |
187 if (info_it == mount_map_.end()) | 197 if (info_it == mount_map_.end()) |
188 return false; | 198 return false; |
189 | 199 |
190 if (device_info) | 200 if (device_info) |
191 *device_info = info_it->second; | 201 *device_info = info_it->second.storage_info; |
192 return true; | 202 return true; |
193 } | 203 } |
194 | 204 |
205 uint64 RemovableDeviceNotificationsCros::GetStorageSize( | |
206 const std::string& device_location) { | |
207 MountMap::const_iterator info_it = mount_map_.find(device_location); | |
208 return (info_it != mount_map_.end()) ? | |
209 info_it->second.storage_size_in_bytes : 0; | |
210 } | |
211 | |
195 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( | 212 void RemovableDeviceNotificationsCros::CheckMountedPathOnFileThread( |
196 const disks::DiskMountManager::MountPointInfo& mount_info) { | 213 const disks::DiskMountManager::MountPointInfo& mount_info) { |
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
198 | 215 |
199 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); | 216 bool has_dcim = chrome::IsMediaDevice(mount_info.mount_path); |
200 | 217 |
201 BrowserThread::PostTask( | 218 BrowserThread::PostTask( |
202 BrowserThread::UI, FROM_HERE, | 219 BrowserThread::UI, FROM_HERE, |
203 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, | 220 base::Bind(&RemovableDeviceNotificationsCros::AddMountedPathOnUIThread, |
204 this, mount_info, has_dcim)); | 221 this, mount_info, has_dcim)); |
205 } | 222 } |
206 | 223 |
207 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread( | 224 void RemovableDeviceNotificationsCros::AddMountedPathOnUIThread( |
208 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { | 225 const disks::DiskMountManager::MountPointInfo& mount_info, bool has_dcim) { |
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
210 | 227 |
211 if (ContainsKey(mount_map_, mount_info.mount_path)) { | 228 if (ContainsKey(mount_map_, mount_info.mount_path)) { |
212 // CheckExistingMountPointsOnUIThread() added the mount point information | 229 // CheckExistingMountPointsOnUIThread() added the mount point information |
213 // in the map before the device attached handler is called. Therefore, an | 230 // in the map before the device attached handler is called. Therefore, an |
214 // entry for the device already exists in the map. | 231 // entry for the device already exists in the map. |
215 return; | 232 return; |
216 } | 233 } |
217 | 234 |
218 // Get the media device uuid and label if exists. | 235 // Get the media device uuid and label if exists. |
219 std::string unique_id; | 236 std::string unique_id; |
220 string16 device_label; | 237 string16 device_label; |
221 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label)) | 238 uint64 storage_size_in_bytes; |
239 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label, | |
240 &storage_size_in_bytes)) | |
222 return; | 241 return; |
223 | 242 |
224 // Keep track of device uuid and label, to see how often we receive empty | 243 // Keep track of device uuid and label, to see how often we receive empty |
225 // values. | 244 // values. |
226 MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, device_label); | 245 chrome::MediaStorageUtil::RecordDeviceInfoHistogram(true, unique_id, |
246 device_label); | |
227 if (unique_id.empty() || device_label.empty()) | 247 if (unique_id.empty() || device_label.empty()) |
228 return; | 248 return; |
229 | 249 |
230 MediaStorageUtil::Type type = has_dcim ? | 250 chrome::MediaStorageUtil::Type type = has_dcim ? |
231 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : | 251 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM : |
232 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; | 252 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; |
233 | 253 |
234 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, | 254 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(type, |
235 unique_id); | 255 unique_id); |
236 SystemMonitor::RemovableStorageInfo info(device_id, device_label, | 256 StorageObjectInfo object_info = { |
237 mount_info.mount_path); | 257 base::SystemMonitor::RemovableStorageInfo(device_id, device_label, |
238 mount_map_.insert(std::make_pair(mount_info.mount_path, info)); | 258 mount_info.mount_path), |
259 storage_size_in_bytes | |
260 }; | |
261 mount_map_.insert(std::make_pair(mount_info.mount_path, object_info)); | |
239 SystemMonitor::Get()->ProcessRemovableStorageAttached( | 262 SystemMonitor::Get()->ProcessRemovableStorageAttached( |
240 device_id, | 263 device_id, |
241 device_label, | 264 chrome::GetDisplayNameForDevice(storage_size_in_bytes, device_label), |
242 mount_info.mount_path); | 265 mount_info.mount_path); |
243 } | 266 } |
244 | 267 |
245 } // namespace chrome | 268 } // namespace chromeos |
OLD | NEW |