[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / metrics / drive_metrics_provider_mac.mm
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/metrics/drive_metrics_provider.h"
6
7 #include <CoreFoundation/CoreFoundation.h>
8 #include <DiskArbitration/DiskArbitration.h>
9 #import <Foundation/Foundation.h>
10 #include <IOKit/IOKitLib.h>
11 #include <IOKit/storage/IOStorageDeviceCharacteristics.h>
12 #include <stdlib.h>
13 #include <sys/stat.h>
14
15 #include "base/apple/bridging.h"
16 #include "base/apple/foundation_util.h"
17 #include "base/apple/scoped_cftyperef.h"
18 #include "base/files/file_path.h"
19 #include "base/mac/mac_util.h"
20 #include "base/mac/scoped_ioobject.h"
21
22 namespace metrics {
23
24 // static
25 bool DriveMetricsProvider::HasSeekPenalty(const base::FilePath& path,
26                                           bool* has_seek_penalty) {
27   struct stat path_stat;
28   if (stat(path.value().c_str(), &path_stat) < 0)
29     return false;
30
31   const char* dev_name = devname(path_stat.st_dev, S_IFBLK);
32   if (!dev_name)
33     return false;
34
35   std::string bsd_name("/dev/");
36   bsd_name.append(dev_name);
37
38   base::apple::ScopedCFTypeRef<DASessionRef> session(
39       DASessionCreate(kCFAllocatorDefault));
40   if (!session)
41     return false;
42
43   base::apple::ScopedCFTypeRef<DADiskRef> disk(
44       DADiskCreateFromBSDName(kCFAllocatorDefault, session, bsd_name.c_str()));
45   if (!disk)
46     return false;
47
48   base::mac::ScopedIOObject<io_object_t> io_media(DADiskCopyIOMedia(disk));
49   base::apple::ScopedCFTypeRef<CFDictionaryRef> characteristics(
50       static_cast<CFDictionaryRef>(IORegistryEntrySearchCFProperty(
51           io_media, kIOServicePlane, CFSTR(kIOPropertyDeviceCharacteristicsKey),
52           kCFAllocatorDefault,
53           kIORegistryIterateRecursively | kIORegistryIterateParents)));
54   if (!characteristics)
55     return false;
56
57   CFStringRef type_ref = base::apple::GetValueFromDictionary<CFStringRef>(
58       characteristics, CFSTR(kIOPropertyMediumTypeKey));
59   if (!type_ref)
60     return false;
61
62   NSString* type = base::apple::CFToNSPtrCast(type_ref);
63   if ([type isEqualToString:@kIOPropertyMediumTypeRotationalKey]) {
64     *has_seek_penalty = true;
65     return true;
66   }
67   if ([type isEqualToString:@kIOPropertyMediumTypeSolidStateKey]) {
68     *has_seek_penalty = false;
69     return true;
70   }
71
72   // TODO(dbeam): should I look for these Rotational/Solid State keys in
73   // |characteristics|? What if I find device characteristic but there's no
74   // type? Assume rotational?
75   return false;
76 }
77
78 }  // namespace metrics