1 // Copyright 2012 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 "base/system/sys_info.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <sys/system_properties.h>
10
11 #include "base/android/sys_utils.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/system/sys_info_internal.h"
18
19 namespace {
20
21 // Default version of Android to fall back to when actual version numbers
22 // cannot be acquired. Use the latest Android release with a higher bug fix
23 // version to avoid unnecessarily comparison errors with the latest release.
24 // This should be manually kept up to date on each Android release.
25 const int kDefaultAndroidMajorVersion = 12;
26 const int kDefaultAndroidMinorVersion = 0;
27 const int kDefaultAndroidBugfixVersion = 99;
28
29 // Get and parse out the OS version numbers from the system properties.
30 // Note if parse fails, the "default" version is returned as fallback.
GetOsVersionStringAndNumbers(std::string * version_string,int32_t * major_version,int32_t * minor_version,int32_t * bugfix_version)31 void GetOsVersionStringAndNumbers(std::string* version_string,
32 int32_t* major_version,
33 int32_t* minor_version,
34 int32_t* bugfix_version) {
35 // Read the version number string out from the properties.
36 char os_version_str[PROP_VALUE_MAX];
37 __system_property_get("ro.build.version.release", os_version_str);
38
39 if (os_version_str[0]) {
40 // Try to parse out the version numbers from the string.
41 int num_read = sscanf(os_version_str, "%d.%d.%d", major_version,
42 minor_version, bugfix_version);
43
44 if (num_read > 0) {
45 // If we don't have a full set of version numbers, make the extras 0.
46 if (num_read < 2)
47 *minor_version = 0;
48 if (num_read < 3)
49 *bugfix_version = 0;
50 *version_string = std::string(os_version_str);
51 return;
52 }
53 }
54
55 // For some reason, we couldn't parse the version number string.
56 *major_version = kDefaultAndroidMajorVersion;
57 *minor_version = kDefaultAndroidMinorVersion;
58 *bugfix_version = kDefaultAndroidBugfixVersion;
59 *version_string = ::base::StringPrintf("%d.%d.%d", *major_version,
60 *minor_version, *bugfix_version);
61 }
62
HardwareManufacturerName()63 std::string HardwareManufacturerName() {
64 char device_model_str[PROP_VALUE_MAX];
65 __system_property_get("ro.product.manufacturer", device_model_str);
66 return std::string(device_model_str);
67 }
68
69 } // anonymous namespace
70
71 namespace base {
72
HardwareModelName()73 std::string SysInfo::HardwareModelName() {
74 char device_model_str[PROP_VALUE_MAX];
75 __system_property_get("ro.product.model", device_model_str);
76 return std::string(device_model_str);
77 }
78
SocManufacturer()79 std::string SysInfo::SocManufacturer() {
80 char soc_manufacturer_str[PROP_VALUE_MAX];
81 __system_property_get("ro.soc.manufacturer", soc_manufacturer_str);
82 return std::string(soc_manufacturer_str);
83 }
84
OperatingSystemName()85 std::string SysInfo::OperatingSystemName() {
86 return "Android";
87 }
88
OperatingSystemVersion()89 std::string SysInfo::OperatingSystemVersion() {
90 std::string version_string;
91 int32_t major, minor, bugfix;
92 GetOsVersionStringAndNumbers(&version_string, &major, &minor, &bugfix);
93 return version_string;
94 }
95
OperatingSystemVersionNumbers(int32_t * major_version,int32_t * minor_version,int32_t * bugfix_version)96 void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
97 int32_t* minor_version,
98 int32_t* bugfix_version) {
99 std::string version_string;
100 GetOsVersionStringAndNumbers(&version_string, major_version, minor_version,
101 bugfix_version);
102 }
103
GetAndroidBuildCodename()104 std::string SysInfo::GetAndroidBuildCodename() {
105 char os_version_codename_str[PROP_VALUE_MAX];
106 __system_property_get("ro.build.version.codename", os_version_codename_str);
107 return std::string(os_version_codename_str);
108 }
109
GetAndroidBuildID()110 std::string SysInfo::GetAndroidBuildID() {
111 char os_build_id_str[PROP_VALUE_MAX];
112 __system_property_get("ro.build.id", os_build_id_str);
113 return std::string(os_build_id_str);
114 }
115
GetAndroidHardwareEGL()116 std::string SysInfo::GetAndroidHardwareEGL() {
117 char os_hardware_egl_str[PROP_VALUE_MAX];
118 __system_property_get("ro.hardware.egl", os_hardware_egl_str);
119 return std::string(os_hardware_egl_str);
120 }
121
122 static base::LazyInstance<base::internal::LazySysInfoValue<
123 bool,
124 android::SysUtils::IsLowEndDeviceFromJni>>::Leaky g_lazy_low_end_device =
125 LAZY_INSTANCE_INITIALIZER;
126
IsLowEndDeviceImpl()127 bool SysInfo::IsLowEndDeviceImpl() {
128 // This code might be used in some environments
129 // which might not have a Java environment.
130 // Note that we need to call the Java version here.
131 // There exists a complete native implementation in
132 // sys_info.cc but calling that here would mean that
133 // the Java code and the native code would call different
134 // implementations which could give different results.
135 // Also the Java code cannot depend on the native code
136 // since it might not be loaded yet.
137 if (!base::android::IsVMInitialized())
138 return false;
139 return g_lazy_low_end_device.Get().value();
140 }
141
142 // static
GetHardwareInfoSync()143 SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() {
144 HardwareInfo info;
145 info.manufacturer = HardwareManufacturerName();
146 info.model = HardwareModelName();
147 DCHECK(IsStringUTF8(info.manufacturer));
148 DCHECK(IsStringUTF8(info.model));
149 return info;
150 }
151
152 } // namespace base
153