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 "components/metrics/drive_metrics_provider.h" |
6 | 6 |
7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
8 #include <DiskArbitration/DiskArbitration.h> | 8 #include <DiskArbitration/DiskArbitration.h> |
9 #include <Foundation/Foundation.h> | 9 #include <Foundation/Foundation.h> |
10 #include <IOKit/IOKitLib.h> | 10 #include <IOKit/IOKitLib.h> |
11 #include <IOKit/storage/IOStorageDeviceCharacteristics.h> | 11 #include <IOKit/storage/IOStorageDeviceCharacteristics.h> |
12 #include <stdlib.h> | 12 #include <stdlib.h> |
13 #include <sys/stat.h> | 13 #include <sys/stat.h> |
14 | 14 |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/mac/foundation_util.h" | 16 #include "base/mac/foundation_util.h" |
17 #include "base/mac/mac_util.h" | 17 #include "base/mac/mac_util.h" |
18 #include "base/mac/scoped_cftyperef.h" | 18 #include "base/mac/scoped_cftyperef.h" |
19 #include "base/mac/scoped_ioobject.h" | 19 #include "base/mac/scoped_ioobject.h" |
20 | 20 |
| 21 namespace metrics { |
| 22 |
21 // static | 23 // static |
22 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path, | 24 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path, |
23 bool* has_seek_penalty) { | 25 bool* has_seek_penalty) { |
24 struct stat path_stat; | 26 struct stat path_stat; |
25 if (stat(path.value().c_str(), &path_stat) < 0) | 27 if (stat(path.value().c_str(), &path_stat) < 0) |
26 return false; | 28 return false; |
27 | 29 |
28 const char* dev_name = devname(path_stat.st_dev, S_IFBLK); | 30 const char* dev_name = devname(path_stat.st_dev, S_IFBLK); |
29 if (!dev_name) | 31 if (!dev_name) |
30 return false; | 32 return false; |
31 | 33 |
32 std::string bsd_name("/dev/"); | 34 std::string bsd_name("/dev/"); |
33 bsd_name.append(dev_name); | 35 bsd_name.append(dev_name); |
34 | 36 |
35 base::ScopedCFTypeRef<DASessionRef> session( | 37 base::ScopedCFTypeRef<DASessionRef> session( |
36 DASessionCreate(kCFAllocatorDefault)); | 38 DASessionCreate(kCFAllocatorDefault)); |
37 if (!session) | 39 if (!session) |
38 return false; | 40 return false; |
39 | 41 |
40 base::ScopedCFTypeRef<DADiskRef> disk(DADiskCreateFromBSDName( | 42 base::ScopedCFTypeRef<DADiskRef> disk( |
41 kCFAllocatorDefault, session, bsd_name.c_str())); | 43 DADiskCreateFromBSDName(kCFAllocatorDefault, session, bsd_name.c_str())); |
42 if (!disk) | 44 if (!disk) |
43 return false; | 45 return false; |
44 | 46 |
45 base::mac::ScopedIOObject<io_object_t> io_media(DADiskCopyIOMedia(disk)); | 47 base::mac::ScopedIOObject<io_object_t> io_media(DADiskCopyIOMedia(disk)); |
46 base::ScopedCFTypeRef<CFDictionaryRef> characteristics( | 48 base::ScopedCFTypeRef<CFDictionaryRef> characteristics( |
47 static_cast<CFDictionaryRef>(IORegistryEntrySearchCFProperty( | 49 static_cast<CFDictionaryRef>(IORegistryEntrySearchCFProperty( |
48 io_media, | 50 io_media, kIOServicePlane, CFSTR(kIOPropertyDeviceCharacteristicsKey), |
49 kIOServicePlane, | |
50 CFSTR(kIOPropertyDeviceCharacteristicsKey), | |
51 kCFAllocatorDefault, | 51 kCFAllocatorDefault, |
52 kIORegistryIterateRecursively | kIORegistryIterateParents))); | 52 kIORegistryIterateRecursively | kIORegistryIterateParents))); |
53 if (!characteristics) | 53 if (!characteristics) |
54 return false; | 54 return false; |
55 | 55 |
56 CFStringRef type_ref = base::mac::GetValueFromDictionary<CFStringRef>( | 56 CFStringRef type_ref = base::mac::GetValueFromDictionary<CFStringRef>( |
57 characteristics, CFSTR(kIOPropertyMediumTypeKey)); | 57 characteristics, CFSTR(kIOPropertyMediumTypeKey)); |
58 if (!type_ref) | 58 if (!type_ref) |
59 return false; | 59 return false; |
60 | 60 |
61 NSString* type = base::mac::CFToNSCast(type_ref); | 61 NSString* type = base::mac::CFToNSCast(type_ref); |
62 if ([type isEqualToString:@kIOPropertyMediumTypeRotationalKey]) { | 62 if ([type isEqualToString:@kIOPropertyMediumTypeRotationalKey]) { |
63 *has_seek_penalty = true; | 63 *has_seek_penalty = true; |
64 return true; | 64 return true; |
65 } else if ([type isEqualToString:@kIOPropertyMediumTypeSolidStateKey]) { | 65 } else if ([type isEqualToString:@kIOPropertyMediumTypeSolidStateKey]) { |
66 *has_seek_penalty = false; | 66 *has_seek_penalty = false; |
67 return true; | 67 return true; |
68 } | 68 } |
69 | 69 |
70 // TODO(dbeam): should I look for these Rotational/Solid State keys in | 70 // TODO(dbeam): should I look for these Rotational/Solid State keys in |
71 // |characteristics|? What if I find device characteristic but there's no | 71 // |characteristics|? What if I find device characteristic but there's no |
72 // type? Assume rotational? | 72 // type? Assume rotational? |
73 return false; | 73 return false; |
74 } | 74 } |
| 75 |
| 76 } // namespace metrics |
OLD | NEW |