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