blob: de68fcf37c1ab5261b0a231eb68d540af270aa12 [file] [log] [blame]
[email protected]c6d5f8c2012-07-11 12:15:081// 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#import <UIKit/UIKit.h>
8#include <mach/mach.h>
[email protected]d5df0c8c2012-09-08 09:53:019#include <sys/sysctl.h>
10#include <sys/types.h>
[email protected]c6d5f8c2012-07-11 12:15:0811
12#include "base/logging.h"
[email protected]431d0a12012-10-16 20:17:5813#include "base/mac/scoped_mach_port.h"
[email protected]c6d5f8c2012-07-11 12:15:0814#include "base/mac/scoped_nsautorelease_pool.h"
[email protected]9fe1a5b2013-02-07 19:18:0315#include "base/strings/sys_string_conversions.h"
[email protected]c6d5f8c2012-07-11 12:15:0816
17namespace base {
18
19// static
20std::string SysInfo::OperatingSystemName() {
[email protected]dba7cf22012-12-10 18:50:3521 static dispatch_once_t get_system_name_once;
22 static std::string* system_name;
23 dispatch_once(&get_system_name_once, ^{
24 base::mac::ScopedNSAutoreleasePool pool;
25 system_name = new std::string(
26 SysNSStringToUTF8([[UIDevice currentDevice] systemName]));
27 });
[email protected]c6d5f8c2012-07-11 12:15:0828 // Examples of returned value: 'iPhone OS' on iPad 5.1.1
29 // and iPhone 5.1.1.
[email protected]dba7cf22012-12-10 18:50:3530 return *system_name;
[email protected]c6d5f8c2012-07-11 12:15:0831}
32
33// static
34std::string SysInfo::OperatingSystemVersion() {
[email protected]dba7cf22012-12-10 18:50:3535 static dispatch_once_t get_system_version_once;
36 static std::string* system_version;
37 dispatch_once(&get_system_version_once, ^{
38 base::mac::ScopedNSAutoreleasePool pool;
39 system_version = new std::string(
40 SysNSStringToUTF8([[UIDevice currentDevice] systemVersion]));
41 });
42 return *system_version;
[email protected]c6d5f8c2012-07-11 12:15:0843}
44
45// static
46void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
47 int32* minor_version,
48 int32* bugfix_version) {
49 base::mac::ScopedNSAutoreleasePool pool;
[email protected]dba7cf22012-12-10 18:50:3550 std::string system_version = OperatingSystemVersion();
51 if (!system_version.empty()) {
52 // Try to parse out the version numbers from the string.
53 int num_read = sscanf(system_version.c_str(), "%d.%d.%d", major_version,
54 minor_version, bugfix_version);
55 if (num_read < 1)
56 *major_version = 0;
57 if (num_read < 2)
58 *minor_version = 0;
59 if (num_read < 3)
60 *bugfix_version = 0;
61 }
[email protected]c6d5f8c2012-07-11 12:15:0862}
63
64// static
65int64 SysInfo::AmountOfPhysicalMemory() {
66 struct host_basic_info hostinfo;
67 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
[email protected]431d0a12012-10-16 20:17:5868 base::mac::ScopedMachPort host(mach_host_self());
69 int result = host_info(host,
[email protected]c6d5f8c2012-07-11 12:15:0870 HOST_BASIC_INFO,
71 reinterpret_cast<host_info_t>(&hostinfo),
72 &count);
73 if (result != KERN_SUCCESS) {
74 NOTREACHED();
75 return 0;
76 }
77 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
78 return static_cast<int64>(hostinfo.max_mem);
79}
80
[email protected]d5df0c8c2012-09-08 09:53:0181// static
82std::string SysInfo::CPUModelName() {
83 char name[256];
84 size_t len = arraysize(name);
85 if (sysctlbyname("machdep.cpu.brand_string", &name, &len, NULL, 0) == 0)
86 return name;
87 return std::string();
88}
89
[email protected]c6d5f8c2012-07-11 12:15:0890} // namespace base