Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / base / sys_info_ios.mm
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
3 // found in the LICENSE file.
4
5 #include "base/sys_info.h"
6
7 #include <mach/mach.h>
8 #include <stddef.h>
9 #include <stdint.h>
10 #include <sys/sysctl.h>
11 #include <sys/types.h>
12 #import <UIKit/UIKit.h>
13
14 #include "base/logging.h"
15 #include "base/mac/scoped_mach_port.h"
16 #include "base/mac/scoped_nsautorelease_pool.h"
17 #include "base/macros.h"
18 #include "base/process/process_metrics.h"
19 #include "base/strings/sys_string_conversions.h"
20
21 namespace base {
22
23 namespace {
24
25 // Queries sysctlbyname() for the given key and returns the value from the
26 // system or the empty string on failure.
27 std::string GetSysctlValue(const char* key_name) {
28   char value[256];
29   size_t len = arraysize(value);
30   if (sysctlbyname(key_name, &value, &len, nullptr, 0) == 0) {
31     DCHECK_GE(len, 1u);
32     DCHECK_EQ('\0', value[len - 1]);
33     return std::string(value, len - 1);
34   }
35   return std::string();
36 }
37
38 }  // namespace
39
40 // static
41 std::string SysInfo::OperatingSystemName() {
42   static dispatch_once_t get_system_name_once;
43   static std::string* system_name;
44   dispatch_once(&get_system_name_once, ^{
45       base::mac::ScopedNSAutoreleasePool pool;
46       system_name = new std::string(
47           SysNSStringToUTF8([[UIDevice currentDevice] systemName]));
48   });
49   // Examples of returned value: 'iPhone OS' on iPad 5.1.1
50   // and iPhone 5.1.1.
51   return *system_name;
52 }
53
54 // static
55 std::string SysInfo::OperatingSystemVersion() {
56   static dispatch_once_t get_system_version_once;
57   static std::string* system_version;
58   dispatch_once(&get_system_version_once, ^{
59       base::mac::ScopedNSAutoreleasePool pool;
60       system_version = new std::string(
61           SysNSStringToUTF8([[UIDevice currentDevice] systemVersion]));
62   });
63   return *system_version;
64 }
65
66 // static
67 void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
68                                             int32_t* minor_version,
69                                             int32_t* bugfix_version) {
70   base::mac::ScopedNSAutoreleasePool pool;
71   std::string system_version = OperatingSystemVersion();
72   if (!system_version.empty()) {
73     // Try to parse out the version numbers from the string.
74     int num_read = sscanf(system_version.c_str(), "%d.%d.%d", major_version,
75                           minor_version, bugfix_version);
76     if (num_read < 1)
77       *major_version = 0;
78     if (num_read < 2)
79       *minor_version = 0;
80     if (num_read < 3)
81       *bugfix_version = 0;
82   }
83 }
84
85 // static
86 int64_t SysInfo::AmountOfPhysicalMemoryImpl() {
87   struct host_basic_info hostinfo;
88   mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
89   base::mac::ScopedMachSendRight host(mach_host_self());
90   int result = host_info(host.get(),
91                          HOST_BASIC_INFO,
92                          reinterpret_cast<host_info_t>(&hostinfo),
93                          &count);
94   if (result != KERN_SUCCESS) {
95     NOTREACHED();
96     return 0;
97   }
98   DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
99   return static_cast<int64_t>(hostinfo.max_mem);
100 }
101
102 // static
103 int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
104   SystemMemoryInfoKB info;
105   if (!GetSystemMemoryInfo(&info))
106     return 0;
107   // We should add inactive file-backed memory also but there is no such
108   // information from iOS unfortunately.
109   return static_cast<int64_t>(info.free + info.speculative) * 1024;
110 }
111
112 // static
113 std::string SysInfo::CPUModelName() {
114   return GetSysctlValue("machdep.cpu.brand_string");
115 }
116
117 // static
118 std::string SysInfo::HardwareModelName() {
119 #if TARGET_OS_SIMULATOR
120   // On the simulator, "hw.machine" returns "i386" or "x86_64" which doesn't
121   // match the expected format, so supply a fake string here.
122   return "Simulator1,1";
123 #else
124   // Note: This uses "hw.machine" instead of "hw.model" like the Mac code,
125   // because "hw.model" doesn't always return the right string on some devices.
126   return GetSysctlValue("hw.machine");
127 #endif
128 }
129
130 }  // namespace base