| 7 | #import <UIKit/UIKit.h> |
| 8 | #include <mach/mach.h> |
| 9 | #include <stddef.h> |
| 10 | #include <stdint.h> |
| 11 | #include <sys/sysctl.h> |
| 12 | #include <sys/types.h> |
| 13 |
| 14 | #include "base/logging.h" |
| 15 | #include "base/mac/scoped_mach_port.h" |
| 16 | #include "base/process/process_metrics.h" |
| 17 | #include "base/stl_util.h" |
| 18 | #include "base/strings/string_util.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 = base::size(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 | @autoreleasepool { |
| 46 | system_name = new std::string( |
| 47 | SysNSStringToUTF8([[UIDevice currentDevice] systemName])); |
| 48 | } |
| 49 | }); |
| 50 | // Examples of returned value: 'iPhone OS' on iPad 5.1.1 |
| 51 | // and iPhone 5.1.1. |
| 52 | return *system_name; |
| 53 | } |
| 54 |
| 55 | // static |
| 56 | std::string SysInfo::OperatingSystemVersion() { |
| 57 | static dispatch_once_t get_system_version_once; |
| 58 | static std::string* system_version; |
| 59 | dispatch_once(&get_system_version_once, ^{ |
| 60 | @autoreleasepool { |
| 61 | system_version = new std::string( |
| 62 | SysNSStringToUTF8([[UIDevice currentDevice] systemVersion])); |
| 63 | } |
| 64 | }); |
| 65 | return *system_version; |
| 66 | } |
| 67 |
| 68 | // static |
| 69 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 70 | int32_t* minor_version, |
| 71 | int32_t* bugfix_version) { |
| 72 | @autoreleasepool { |
| 73 | std::string system_version = OperatingSystemVersion(); |
| 74 | if (!system_version.empty()) { |
| 75 | // Try to parse out the version numbers from the string. |
| 76 | int num_read = sscanf(system_version.c_str(), "%d.%d.%d", major_version, |
| 77 | minor_version, bugfix_version); |
| 78 | if (num_read < 1) |
| 79 | *major_version = 0; |
| 80 | if (num_read < 2) |
| 81 | *minor_version = 0; |
| 82 | if (num_read < 3) |
| 83 | *bugfix_version = 0; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 |
| 88 | // static |
| 89 | std::string SysInfo::GetIOSBuildNumber() { |
| 90 | int mib[2] = {CTL_KERN, KERN_OSVERSION}; |
| 91 | unsigned int namelen = sizeof(mib) / sizeof(mib[0]); |
| 92 | size_t buffer_size = 0; |
| 93 | sysctl(mib, namelen, nullptr, &buffer_size, nullptr, 0); |
| 94 | char build_number[buffer_size]; |
| 95 | int result = sysctl(mib, namelen, build_number, &buffer_size, nullptr, 0); |
| 96 | DCHECK(result == 0); |
| 97 | return build_number; |
| 98 | } |
| 99 |
| 100 | // static |
| 101 | int64_t SysInfo::AmountOfPhysicalMemoryImpl() { |
| 102 | struct host_basic_info hostinfo; |
| 103 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
| 104 | base::mac::ScopedMachSendRight host(mach_host_self()); |
| 105 | int result = host_info(host.get(), HOST_BASIC_INFO, |
| 106 | reinterpret_cast<host_info_t>(&hostinfo), &count); |
| 107 | if (result != KERN_SUCCESS) { |
| 108 | NOTREACHED(); |
| 109 | return 0; |
| 110 | } |
| 111 | DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
| 112 | return static_cast<int64_t>(hostinfo.max_mem); |
| 113 | } |
| 114 |
| 115 | // static |
| 116 | int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { |
| 117 | SystemMemoryInfoKB info; |
| 118 | if (!GetSystemMemoryInfo(&info)) |
| 119 | return 0; |
| 120 | // We should add inactive file-backed memory also but there is no such |
| 121 | // information from iOS unfortunately. |
| 122 | return static_cast<int64_t>(info.free + info.speculative) * 1024; |
| 123 | } |
| 124 |
| 125 | // static |
| 126 | std::string SysInfo::CPUModelName() { |
| 127 | return GetSysctlValue("machdep.cpu.brand_string"); |
| 128 | } |
| 129 |
| 130 | // static |
| 131 | std::string SysInfo::HardwareModelName() { |
| 132 | #if TARGET_OS_SIMULATOR |
| 133 | // On the simulator, "hw.machine" returns "i386" or "x86_64" which doesn't |
| 134 | // match the expected format, so supply a fake string here. |
| 135 | return "Simulator1,1"; |
| 136 | #else |
| 137 | // Note: This uses "hw.machine" instead of "hw.model" like the Mac code, |
| 138 | // because "hw.model" doesn't always return the right string on some devices. |
| 139 | return GetSysctlValue("hw.machine"); |
| 140 | #endif |
| 141 | } |
| 142 |
| 143 | // static |
| 144 | SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() { |
| 145 | HardwareInfo info; |
| 146 | info.manufacturer = "Apple Inc."; |
| 147 | info.model = HardwareModelName(); |
| 148 | DCHECK(IsStringUTF8(info.manufacturer)); |
| 149 | DCHECK(IsStringUTF8(info.model)); |
| 150 | return info; |
| 151 | } |
| 152 |
| 153 | } // namespace base |
| 154 |