OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/metrics/drive_metrics_provider.h" | 5 #include "chrome/browser/metrics/drive_metrics_provider.h" |
6 | 6 |
7 #include <CoreFoundation/CoreFoundation.h> | |
8 #include <DiskArbitration/DiskArbitration.h> | |
9 #include <Foundation/Foundation.h> | |
10 #include <IOKit/IOKitLib.h> | |
11 #include <IOKit/storage/IOStorageDeviceCharacteristics.h> | |
12 #include <sys/stat.h> | |
13 | |
14 #include "base/files/file_path.h" | |
15 #include "base/mac/foundation_util.h" | |
16 #include "base/mac/mac_util.h" | |
17 #include "base/mac/scoped_cftyperef.h" | |
18 #include "base/mac/scoped_ioobject.h" | |
19 | |
7 // static | 20 // static |
8 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path, | 21 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path, |
9 bool* has_seek_penalty) { | 22 bool* has_seek_penalty) { |
10 // TODO(dbeam): implement. | 23 struct stat path_stat; |
24 if (stat(path.value().c_str(), &path_stat) < 0) | |
25 return false; | |
26 | |
27 std::string bsd_name(devname(path_stat.st_dev, S_IFBLK)); | |
28 if (bsd_name.empty() || bsd_name == "null") | |
Alexei Svitkine (slow)
2015/04/09 20:21:12
According to devname() make page, it can return NU
Dan Beam
2015/04/09 21:12:08
ah, printf("%s", null) confused me. fixed.
| |
29 return false; | |
30 | |
31 bsd_name.insert(0, "/dev/"); | |
Alexei Svitkine (slow)
2015/04/09 20:21:12
Nit: I suggest just initializing bsd_name to "/dev
Dan Beam
2015/04/09 21:12:08
Done.
| |
32 | |
33 base::ScopedCFTypeRef<DASessionRef> session( | |
34 DASessionCreate(kCFAllocatorDefault)); | |
35 if (!session) | |
36 return false; | |
37 | |
38 base::ScopedCFTypeRef<DADiskRef> disk(DADiskCreateFromBSDName( | |
39 kCFAllocatorDefault, session, bsd_name.c_str())); | |
40 if (!disk) | |
41 return false; | |
42 | |
43 base::mac::ScopedIOObject<io_object_t> io_media(DADiskCopyIOMedia(disk)); | |
44 base::ScopedCFTypeRef<CFDictionaryRef> characteristics( | |
45 static_cast<CFDictionaryRef>(IORegistryEntrySearchCFProperty( | |
46 io_media, | |
47 kIOServicePlane, | |
48 CFSTR(kIOPropertyDeviceCharacteristicsKey), | |
49 kCFAllocatorDefault, | |
50 kIORegistryIterateRecursively | kIORegistryIterateParents))); | |
51 if (!characteristics) | |
52 return false; | |
53 | |
54 CFStringRef type_ref = base::mac::GetValueFromDictionary<CFStringRef>( | |
55 characteristics, CFSTR(kIOPropertyMediumTypeKey)); | |
56 if (!type_ref) | |
57 return false; | |
58 | |
59 NSString* type = base::mac::CFToNSCast(type_ref); | |
60 if ([type isEqualToString:@kIOPropertyMediumTypeRotationalKey]) { | |
61 *has_seek_penalty = true; | |
62 return true; | |
63 } else if ([type isEqualToString:@kIOPropertyMediumTypeSolidStateKey]) { | |
64 *has_seek_penalty = false; | |
65 return true; | |
66 } | |
67 | |
68 // TODO(dbeam): should I look for these Rotational/Solid State keys in | |
69 // |characteristics|? What if I find device characteristic but there's no | |
70 // type? Assume rotational? | |
11 return false; | 71 return false; |
12 } | 72 } |
OLD | NEW |