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 #include "chrome/browser/storage_monitor/storage_monitor_mac.h" | 5 #include "chrome/browser/storage_monitor/storage_monitor_mac.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "base/mac/mac_util.h" | 8 #include "base/mac/mac_util.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 dict, kDADiskDescriptionDeviceVendorKey); | 67 dict, kDADiskDescriptionDeviceVendorKey); |
68 string16 model = GetUTF16FromDictionary( | 68 string16 model = GetUTF16FromDictionary( |
69 dict, kDADiskDescriptionDeviceModelKey); | 69 dict, kDADiskDescriptionDeviceModelKey); |
70 string16 label = GetUTF16FromDictionary( | 70 string16 label = GetUTF16FromDictionary( |
71 dict, kDADiskDescriptionVolumeNameKey); | 71 dict, kDADiskDescriptionVolumeNameKey); |
72 | 72 |
73 CFUUIDRef uuid = base::mac::GetValueFromDictionary<CFUUIDRef>( | 73 CFUUIDRef uuid = base::mac::GetValueFromDictionary<CFUUIDRef>( |
74 dict, kDADiskDescriptionVolumeUUIDKey); | 74 dict, kDADiskDescriptionVolumeUUIDKey); |
75 std::string unique_id; | 75 std::string unique_id; |
76 if (uuid) { | 76 if (uuid) { |
77 base::mac::ScopedCFTypeRef<CFStringRef> uuid_string( | 77 base::ScopedCFTypeRef<CFStringRef> uuid_string( |
78 CFUUIDCreateString(NULL, uuid)); | 78 CFUUIDCreateString(NULL, uuid)); |
79 if (uuid_string.get()) | 79 if (uuid_string.get()) |
80 unique_id = base::SysCFStringRefToUTF8(uuid_string); | 80 unique_id = base::SysCFStringRefToUTF8(uuid_string); |
81 } | 81 } |
82 if (unique_id.empty()) { | 82 if (unique_id.empty()) { |
83 string16 revision = GetUTF16FromDictionary( | 83 string16 revision = GetUTF16FromDictionary( |
84 dict, kDADiskDescriptionDeviceRevisionKey); | 84 dict, kDADiskDescriptionDeviceRevisionKey); |
85 string16 unique_id2 = vendor; | 85 string16 unique_id2 = vendor; |
86 unique_id2 = JoinName(unique_id2, model); | 86 unique_id2 = JoinName(unique_id2, model); |
87 unique_id2 = JoinName(unique_id2, revision); | 87 unique_id2 = JoinName(unique_id2, revision); |
(...skipping 10 matching lines...) Expand all Loading... |
98 std::string device_id; | 98 std::string device_id; |
99 if (!unique_id.empty()) | 99 if (!unique_id.empty()) |
100 device_id = StorageInfo::MakeDeviceId(device_type, unique_id); | 100 device_id = StorageInfo::MakeDeviceId(device_type, unique_id); |
101 | 101 |
102 return StorageInfo(device_id, string16(), location.value(), label, vendor, | 102 return StorageInfo(device_id, string16(), location.value(), label, vendor, |
103 model, size_in_bytes); | 103 model, size_in_bytes); |
104 } | 104 } |
105 | 105 |
106 void GetDiskInfoAndUpdateOnFileThread( | 106 void GetDiskInfoAndUpdateOnFileThread( |
107 const base::WeakPtr<StorageMonitorMac>& monitor, | 107 const base::WeakPtr<StorageMonitorMac>& monitor, |
108 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict, | 108 base::ScopedCFTypeRef<CFDictionaryRef> dict, |
109 StorageMonitorMac::UpdateType update_type) { | 109 StorageMonitorMac::UpdateType update_type) { |
110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
111 | 111 |
112 std::string bsd_name; | 112 std::string bsd_name; |
113 StorageInfo info = BuildStorageInfo(dict, &bsd_name); | 113 StorageInfo info = BuildStorageInfo(dict, &bsd_name); |
114 | 114 |
115 content::BrowserThread::PostTask( | 115 content::BrowserThread::PostTask( |
116 content::BrowserThread::UI, | 116 content::BrowserThread::UI, |
117 FROM_HERE, | 117 FROM_HERE, |
118 base::Bind(&StorageMonitorMac::UpdateDisk, | 118 base::Bind(&StorageMonitorMac::UpdateDisk, |
119 monitor, | 119 monitor, |
120 bsd_name, | 120 bsd_name, |
121 info, | 121 info, |
122 update_type)); | 122 update_type)); |
123 } | 123 } |
124 | 124 |
125 struct EjectDiskOptions { | 125 struct EjectDiskOptions { |
126 std::string bsd_name; | 126 std::string bsd_name; |
127 base::Callback<void(StorageMonitor::EjectStatus)> callback; | 127 base::Callback<void(StorageMonitor::EjectStatus)> callback; |
128 base::mac::ScopedCFTypeRef<DADiskRef> disk; | 128 base::ScopedCFTypeRef<DADiskRef> disk; |
129 }; | 129 }; |
130 | 130 |
131 void PostEjectCallback(DADiskRef disk, | 131 void PostEjectCallback(DADiskRef disk, |
132 DADissenterRef dissenter, | 132 DADissenterRef dissenter, |
133 void* context) { | 133 void* context) { |
134 scoped_ptr<EjectDiskOptions> options_deleter( | 134 scoped_ptr<EjectDiskOptions> options_deleter( |
135 static_cast<EjectDiskOptions*>(context)); | 135 static_cast<EjectDiskOptions*>(context)); |
136 if (dissenter) { | 136 if (dissenter) { |
137 options_deleter->callback.Run(StorageMonitor::EJECT_IN_USE); | 137 options_deleter->callback.Run(StorageMonitor::EJECT_IN_USE); |
138 return; | 138 return; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 } | 282 } |
283 } | 283 } |
284 | 284 |
285 if (bsd_name.empty()) { | 285 if (bsd_name.empty()) { |
286 callback.Run(EJECT_NO_SUCH_DEVICE); | 286 callback.Run(EJECT_NO_SUCH_DEVICE); |
287 return; | 287 return; |
288 } | 288 } |
289 | 289 |
290 receiver()->ProcessDetach(device_id); | 290 receiver()->ProcessDetach(device_id); |
291 | 291 |
292 base::mac::ScopedCFTypeRef<DADiskRef> disk( | 292 base::ScopedCFTypeRef<DADiskRef> disk( |
293 DADiskCreateFromBSDName(NULL, session_, bsd_name.c_str())); | 293 DADiskCreateFromBSDName(NULL, session_, bsd_name.c_str())); |
294 if (!disk.get()) { | 294 if (!disk.get()) { |
295 callback.Run(StorageMonitor::EJECT_FAILURE); | 295 callback.Run(StorageMonitor::EJECT_FAILURE); |
296 return; | 296 return; |
297 } | 297 } |
298 // Get the reference to the full disk for ejecting. | 298 // Get the reference to the full disk for ejecting. |
299 disk.reset(DADiskCopyWholeDisk(disk)); | 299 disk.reset(DADiskCopyWholeDisk(disk)); |
300 if (!disk.get()) { | 300 if (!disk.get()) { |
301 callback.Run(StorageMonitor::EJECT_FAILURE); | 301 callback.Run(StorageMonitor::EJECT_FAILURE); |
302 return; | 302 return; |
(...skipping 27 matching lines...) Expand all Loading... |
330 monitor->GetDiskInfoAndUpdate(disk, UPDATE_DEVICE_CHANGED); | 330 monitor->GetDiskInfoAndUpdate(disk, UPDATE_DEVICE_CHANGED); |
331 } | 331 } |
332 | 332 |
333 void StorageMonitorMac::GetDiskInfoAndUpdate( | 333 void StorageMonitorMac::GetDiskInfoAndUpdate( |
334 DADiskRef disk, | 334 DADiskRef disk, |
335 StorageMonitorMac::UpdateType update_type) { | 335 StorageMonitorMac::UpdateType update_type) { |
336 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 336 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
337 | 337 |
338 pending_disk_updates_++; | 338 pending_disk_updates_++; |
339 | 339 |
340 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict(DADiskCopyDescription(disk)); | 340 base::ScopedCFTypeRef<CFDictionaryRef> dict(DADiskCopyDescription(disk)); |
341 content::BrowserThread::PostTask( | 341 content::BrowserThread::PostTask( |
342 content::BrowserThread::FILE, | 342 content::BrowserThread::FILE, |
343 FROM_HERE, | 343 FROM_HERE, |
344 base::Bind(GetDiskInfoAndUpdateOnFileThread, | 344 base::Bind( |
345 AsWeakPtr(), dict, update_type)); | 345 GetDiskInfoAndUpdateOnFileThread, AsWeakPtr(), dict, update_type)); |
346 } | 346 } |
347 | 347 |
348 | 348 |
349 bool StorageMonitorMac::ShouldPostNotificationForDisk( | 349 bool StorageMonitorMac::ShouldPostNotificationForDisk( |
350 const StorageInfo& info) const { | 350 const StorageInfo& info) const { |
351 // Only post notifications about disks that have no empty fields and | 351 // Only post notifications about disks that have no empty fields and |
352 // are removable. Also exclude disk images (DMGs). | 352 // are removable. Also exclude disk images (DMGs). |
353 return !info.device_id().empty() && | 353 return !info.device_id().empty() && |
354 !info.location().empty() && | 354 !info.location().empty() && |
355 info.model_name() != ASCIIToUTF16(kDiskImageModelName) && | 355 info.model_name() != ASCIIToUTF16(kDiskImageModelName) && |
356 StorageInfo::IsMassStorageDevice(info.device_id()); | 356 StorageInfo::IsMassStorageDevice(info.device_id()); |
357 } | 357 } |
358 | 358 |
359 bool StorageMonitorMac::FindDiskWithMountPoint( | 359 bool StorageMonitorMac::FindDiskWithMountPoint( |
360 const base::FilePath& mount_point, | 360 const base::FilePath& mount_point, |
361 StorageInfo* info) const { | 361 StorageInfo* info) const { |
362 for (std::map<std::string, StorageInfo>::const_iterator | 362 for (std::map<std::string, StorageInfo>::const_iterator |
363 it = disk_info_map_.begin(); it != disk_info_map_.end(); ++it) { | 363 it = disk_info_map_.begin(); it != disk_info_map_.end(); ++it) { |
364 if (it->second.location() == mount_point.value()) { | 364 if (it->second.location() == mount_point.value()) { |
365 *info = it->second; | 365 *info = it->second; |
366 return true; | 366 return true; |
367 } | 367 } |
368 } | 368 } |
369 return false; | 369 return false; |
370 } | 370 } |
371 | 371 |
372 } // namespace chrome | 372 } // namespace chrome |
OLD | NEW |