Home | History | Annotate | Download | only in base
      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 #import <UIKit/UIKit.h>
      8 #include <mach/mach.h>
      9 #include <sys/sysctl.h>
     10 #include <sys/types.h>
     11 
     12 #include "base/logging.h"
     13 #include "base/mac/scoped_mach_port.h"
     14 #include "base/mac/scoped_nsautorelease_pool.h"
     15 #include "base/strings/sys_string_conversions.h"
     16 
     17 namespace base {
     18 
     19 // static
     20 std::string SysInfo::OperatingSystemName() {
     21   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   });
     28   // Examples of returned value: 'iPhone OS' on iPad 5.1.1
     29   // and iPhone 5.1.1.
     30   return *system_name;
     31 }
     32 
     33 // static
     34 std::string SysInfo::OperatingSystemVersion() {
     35   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;
     43 }
     44 
     45 // static
     46 void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
     47                                             int32* minor_version,
     48                                             int32* bugfix_version) {
     49   base::mac::ScopedNSAutoreleasePool pool;
     50   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   }
     62 }
     63 
     64 // static
     65 int64 SysInfo::AmountOfPhysicalMemory() {
     66   struct host_basic_info hostinfo;
     67   mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
     68   base::mac::ScopedMachPort host(mach_host_self());
     69   int result = host_info(host,
     70                          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 
     81 // static
     82 std::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 
     90 }  // namespace base
     91