Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: chrome/browser/storage_monitor/storage_monitor_mac.mm

Issue 15294020: StorageMonitor: Make StorageInfo a real class. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 29 matching lines...) Expand all
40 return StorageInfo::FIXED_MASS_STORAGE; 40 return StorageInfo::FIXED_MASS_STORAGE;
41 if (has_dcim) 41 if (has_dcim)
42 return StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM; 42 return StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM;
43 return StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM; 43 return StorageInfo::REMOVABLE_MASS_STORAGE_NO_DCIM;
44 } 44 }
45 45
46 StorageInfo BuildStorageInfo( 46 StorageInfo BuildStorageInfo(
47 CFDictionaryRef dict, std::string* bsd_name) { 47 CFDictionaryRef dict, std::string* bsd_name) {
48 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 48 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
49 49
50 StorageInfo info;
51
52 CFStringRef device_bsd_name = base::mac::GetValueFromDictionary<CFStringRef>( 50 CFStringRef device_bsd_name = base::mac::GetValueFromDictionary<CFStringRef>(
53 dict, kDADiskDescriptionMediaBSDNameKey); 51 dict, kDADiskDescriptionMediaBSDNameKey);
54 if (device_bsd_name && bsd_name) 52 if (device_bsd_name && bsd_name)
55 *bsd_name = base::SysCFStringRefToUTF8(device_bsd_name); 53 *bsd_name = base::SysCFStringRefToUTF8(device_bsd_name);
56 54
57 CFURLRef url = base::mac::GetValueFromDictionary<CFURLRef>( 55 CFURLRef url = base::mac::GetValueFromDictionary<CFURLRef>(
58 dict, kDADiskDescriptionVolumePathKey); 56 dict, kDADiskDescriptionVolumePathKey);
59 NSURL* nsurl = base::mac::CFToNSCast(url); 57 NSURL* nsurl = base::mac::CFToNSCast(url);
60 info.location = base::mac::NSStringToFilePath([nsurl path]).value(); 58 base::FilePath location = base::mac::NSStringToFilePath([nsurl path]);
61 CFNumberRef size_number = 59 CFNumberRef size_number =
62 base::mac::GetValueFromDictionary<CFNumberRef>( 60 base::mac::GetValueFromDictionary<CFNumberRef>(
63 dict, kDADiskDescriptionMediaSizeKey); 61 dict, kDADiskDescriptionMediaSizeKey);
64 if (size_number) { 62 uint64 size_in_bytes = 0;
65 CFNumberGetValue(size_number, kCFNumberLongLongType, 63 if (size_number)
66 &(info.total_size_in_bytes)); 64 CFNumberGetValue(size_number, kCFNumberLongLongType, &(size_in_bytes));
Greg Billock 2013/05/22 23:39:00 need parens? (Not sure we ever did...)
Lei Zhang 2013/05/23 16:44:49 Nope
67 }
68 65
69 info.vendor_name = GetUTF16FromDictionary( 66 string16 vendor = GetUTF16FromDictionary(
70 dict, kDADiskDescriptionDeviceVendorKey); 67 dict, kDADiskDescriptionDeviceVendorKey);
71 info.model_name = GetUTF16FromDictionary( 68 string16 model = GetUTF16FromDictionary(
72 dict, kDADiskDescriptionDeviceModelKey); 69 dict, kDADiskDescriptionDeviceModelKey);
73 info.storage_label = GetUTF16FromDictionary( 70 string16 label = GetUTF16FromDictionary(
74 dict, kDADiskDescriptionVolumeNameKey); 71 dict, kDADiskDescriptionVolumeNameKey);
75 72
76 CFUUIDRef uuid = base::mac::GetValueFromDictionary<CFUUIDRef>( 73 CFUUIDRef uuid = base::mac::GetValueFromDictionary<CFUUIDRef>(
77 dict, kDADiskDescriptionVolumeUUIDKey); 74 dict, kDADiskDescriptionVolumeUUIDKey);
78 std::string unique_id; 75 std::string unique_id;
79 if (uuid) { 76 if (uuid) {
80 base::mac::ScopedCFTypeRef<CFStringRef> uuid_string( 77 base::mac::ScopedCFTypeRef<CFStringRef> uuid_string(
81 CFUUIDCreateString(NULL, uuid)); 78 CFUUIDCreateString(NULL, uuid));
82 if (uuid_string.get()) 79 if (uuid_string.get())
83 unique_id = base::SysCFStringRefToUTF8(uuid_string); 80 unique_id = base::SysCFStringRefToUTF8(uuid_string);
84 } 81 }
85 if (unique_id.empty()) { 82 if (unique_id.empty()) {
86 string16 revision = GetUTF16FromDictionary( 83 string16 revision = GetUTF16FromDictionary(
87 dict, kDADiskDescriptionDeviceRevisionKey); 84 dict, kDADiskDescriptionDeviceRevisionKey);
88 string16 unique_id2 = info.vendor_name; 85 string16 unique_id2 = vendor;
89 unique_id2 = JoinName(unique_id2, info.model_name); 86 unique_id2 = JoinName(unique_id2, model);
90 unique_id2 = JoinName(unique_id2, revision); 87 unique_id2 = JoinName(unique_id2, revision);
91 unique_id = UTF16ToUTF8(unique_id2); 88 unique_id = UTF16ToUTF8(unique_id2);
92 } 89 }
93 90
94 CFBooleanRef is_removable_ref = 91 CFBooleanRef is_removable_ref =
95 base::mac::GetValueFromDictionary<CFBooleanRef>( 92 base::mac::GetValueFromDictionary<CFBooleanRef>(
96 dict, kDADiskDescriptionMediaRemovableKey); 93 dict, kDADiskDescriptionMediaRemovableKey);
97 bool is_removable = is_removable_ref && CFBooleanGetValue(is_removable_ref); 94 bool is_removable = is_removable_ref && CFBooleanGetValue(is_removable_ref);
98 // Checking for DCIM only matters on removable devices. 95 // Checking for DCIM only matters on removable devices.
99 bool has_dcim = is_removable && 96 bool has_dcim = is_removable && MediaStorageUtil::HasDcim(location);
100 MediaStorageUtil::HasDcim(base::FilePath(info.location));
101 StorageInfo::Type device_type = GetDeviceType(is_removable, has_dcim); 97 StorageInfo::Type device_type = GetDeviceType(is_removable, has_dcim);
98 std::string device_id;
102 if (!unique_id.empty()) 99 if (!unique_id.empty())
103 info.device_id = StorageInfo::MakeDeviceId(device_type, 100 device_id = StorageInfo::MakeDeviceId(device_type, unique_id);
104 unique_id);
105 101
106 return info; 102 return StorageInfo(device_id, string16(), location.value(), label, vendor,
103 model, size_in_bytes);
107 } 104 }
108 105
109 void GetDiskInfoAndUpdateOnFileThread( 106 void GetDiskInfoAndUpdateOnFileThread(
110 const base::WeakPtr<StorageMonitorMac>& monitor, 107 const base::WeakPtr<StorageMonitorMac>& monitor,
111 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict, 108 base::mac::ScopedCFTypeRef<CFDictionaryRef> dict,
112 StorageMonitorMac::UpdateType update_type) { 109 StorageMonitorMac::UpdateType update_type) {
113 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
114 111
115 std::string bsd_name; 112 std::string bsd_name;
116 StorageInfo info = BuildStorageInfo(dict, &bsd_name); 113 StorageInfo info = BuildStorageInfo(dict, &bsd_name);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 const std::string& bsd_name, 207 const std::string& bsd_name,
211 const StorageInfo& info, 208 const StorageInfo& info,
212 UpdateType update_type) { 209 UpdateType update_type) {
213 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 210 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
214 211
215 pending_disk_updates_--; 212 pending_disk_updates_--;
216 bool initialization_complete = false; 213 bool initialization_complete = false;
217 if (!IsInitialized() && pending_disk_updates_ == 0) 214 if (!IsInitialized() && pending_disk_updates_ == 0)
218 initialization_complete = true; 215 initialization_complete = true;
219 216
220 if (info.device_id.empty() || bsd_name.empty()) { 217 if (info.device_id().empty() || bsd_name.empty()) {
221 if (initialization_complete) 218 if (initialization_complete)
222 MarkInitialized(); 219 MarkInitialized();
223 return; 220 return;
224 } 221 }
225 222
226 std::map<std::string, StorageInfo>::iterator it = 223 std::map<std::string, StorageInfo>::iterator it =
227 disk_info_map_.find(bsd_name); 224 disk_info_map_.find(bsd_name);
228 if (it != disk_info_map_.end()) { 225 if (it != disk_info_map_.end()) {
229 // If an attached notification was previously posted then post a detached 226 // If an attached notification was previously posted then post a detached
230 // notification now. This is used for devices that are being removed or 227 // notification now. This is used for devices that are being removed or
231 // devices that have changed. 228 // devices that have changed.
232 if (ShouldPostNotificationForDisk(it->second)) { 229 if (ShouldPostNotificationForDisk(it->second)) {
233 receiver()->ProcessDetach(it->second.device_id); 230 receiver()->ProcessDetach(it->second.device_id());
234 } 231 }
235 } 232 }
236 233
237 if (update_type == UPDATE_DEVICE_REMOVED) { 234 if (update_type == UPDATE_DEVICE_REMOVED) {
238 if (it != disk_info_map_.end()) 235 if (it != disk_info_map_.end())
239 disk_info_map_.erase(it); 236 disk_info_map_.erase(it);
240 } else { 237 } else {
241 disk_info_map_[bsd_name] = info; 238 disk_info_map_[bsd_name] = info;
242 MediaStorageUtil::RecordDeviceInfoHistogram(true, info.device_id, 239 MediaStorageUtil::RecordDeviceInfoHistogram(true, info.device_id(),
243 info.storage_label); 240 info.storage_label());
244 if (ShouldPostNotificationForDisk(info)) 241 if (ShouldPostNotificationForDisk(info))
245 receiver()->ProcessAttach(info); 242 receiver()->ProcessAttach(info);
246 } 243 }
247 244
248 // We're not really honestly sure we're done, but this looks the best we 245 // We're not really honestly sure we're done, but this looks the best we
249 // can do. Any misses should go out through notifications. 246 // can do. Any misses should go out through notifications.
250 if (initialization_complete) 247 if (initialization_complete)
251 MarkInitialized(); 248 MarkInitialized();
252 } 249 }
253 250
(...skipping 17 matching lines...) Expand all
271 268
272 return false; 269 return false;
273 } 270 }
274 271
275 void StorageMonitorMac::EjectDevice( 272 void StorageMonitorMac::EjectDevice(
276 const std::string& device_id, 273 const std::string& device_id,
277 base::Callback<void(EjectStatus)> callback) { 274 base::Callback<void(EjectStatus)> callback) {
278 std::string bsd_name; 275 std::string bsd_name;
279 for (std::map<std::string, StorageInfo>::iterator 276 for (std::map<std::string, StorageInfo>::iterator
280 it = disk_info_map_.begin(); it != disk_info_map_.end(); ++it) { 277 it = disk_info_map_.begin(); it != disk_info_map_.end(); ++it) {
281 if (it->second.device_id == device_id) { 278 if (it->second.device_id() == device_id) {
282 bsd_name = it->first; 279 bsd_name = it->first;
283 disk_info_map_.erase(it); 280 disk_info_map_.erase(it);
284 break; 281 break;
285 } 282 }
286 } 283 }
287 284
288 if (bsd_name.empty()) { 285 if (bsd_name.empty()) {
289 callback.Run(EJECT_NO_SUCH_DEVICE); 286 callback.Run(EJECT_NO_SUCH_DEVICE);
290 return; 287 return;
291 } 288 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 FROM_HERE, 343 FROM_HERE,
347 base::Bind(GetDiskInfoAndUpdateOnFileThread, 344 base::Bind(GetDiskInfoAndUpdateOnFileThread,
348 AsWeakPtr(), dict, update_type)); 345 AsWeakPtr(), dict, update_type));
349 } 346 }
350 347
351 348
352 bool StorageMonitorMac::ShouldPostNotificationForDisk( 349 bool StorageMonitorMac::ShouldPostNotificationForDisk(
353 const StorageInfo& info) const { 350 const StorageInfo& info) const {
354 // Only post notifications about disks that have no empty fields and 351 // Only post notifications about disks that have no empty fields and
355 // are removable. Also exclude disk images (DMGs). 352 // are removable. Also exclude disk images (DMGs).
356 return !info.device_id.empty() && 353 return !info.device_id().empty() &&
357 !info.location.empty() && 354 !info.location().empty() &&
358 info.model_name != ASCIIToUTF16(kDiskImageModelName) && 355 info.model_name() != ASCIIToUTF16(kDiskImageModelName) &&
359 StorageInfo::IsRemovableDevice(info.device_id) && 356 StorageInfo::IsRemovableDevice(info.device_id()) &&
360 StorageInfo::IsMassStorageDevice(info.device_id); 357 StorageInfo::IsMassStorageDevice(info.device_id());
361 } 358 }
362 359
363 bool StorageMonitorMac::FindDiskWithMountPoint( 360 bool StorageMonitorMac::FindDiskWithMountPoint(
364 const base::FilePath& mount_point, 361 const base::FilePath& mount_point,
365 StorageInfo* info) const { 362 StorageInfo* info) const {
366 for (std::map<std::string, StorageInfo>::const_iterator 363 for (std::map<std::string, StorageInfo>::const_iterator
367 it = disk_info_map_.begin(); it != disk_info_map_.end(); ++it) { 364 it = disk_info_map_.begin(); it != disk_info_map_.end(); ++it) {
368 if (it->second.location == mount_point.value()) { 365 if (it->second.location() == mount_point.value()) {
369 *info = it->second; 366 *info = it->second;
370 return true; 367 return true;
371 } 368 }
372 } 369 }
373 return false; 370 return false;
374 } 371 }
375 372
376 } // namespace chrome 373 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698