Add google_apis_unittests, midi_unittests to GN swarming.
[chromium-blink-merge.git] / base / sys_info_android.cc
blobc288ae2fe87f7ce3e68edb513bd2c5c45849ef83
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.
5 #include "base/sys_info.h"
7 #include <dlfcn.h>
8 #include <sys/system_properties.h>
10 #include "base/android/sys_utils.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_piece.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/sys_info_internal.h"
18 #if (__ANDROID_API__ >= 21 /* 5.0 - Lollipop */)
20 namespace {
22 typedef int (SystemPropertyGetFunction)(const char*, char*);
24 SystemPropertyGetFunction* DynamicallyLoadRealSystemPropertyGet() {
25 // libc.so should already be open, get a handle to it.
26 void* handle = dlopen("libc.so", RTLD_NOLOAD);
27 if (!handle) {
28 LOG(FATAL) << "Cannot dlopen libc.so: " << dlerror();
30 SystemPropertyGetFunction* real_system_property_get =
31 reinterpret_cast<SystemPropertyGetFunction*>(
32 dlsym(handle, "__system_property_get"));
33 if (!real_system_property_get) {
34 LOG(FATAL) << "Cannot resolve __system_property_get(): " << dlerror();
36 return real_system_property_get;
39 static base::LazyInstance<base::internal::LazySysInfoValue<
40 SystemPropertyGetFunction*, DynamicallyLoadRealSystemPropertyGet> >::Leaky
41 g_lazy_real_system_property_get = LAZY_INSTANCE_INITIALIZER;
43 } // namespace
45 // Android 'L' removes __system_property_get from the NDK, however it is still
46 // a hidden symbol in libc. Until we remove all calls of __system_property_get
47 // from Chrome we work around this by defining a weak stub here, which uses
48 // dlsym to but ensures that Chrome uses the real system
49 // implementatation when loaded. http://crbug.com/392191.
50 BASE_EXPORT int __system_property_get(const char* name, char* value) {
51 return g_lazy_real_system_property_get.Get().value()(name, value);
54 #endif
56 namespace {
58 // Default version of Android to fall back to when actual version numbers
59 // cannot be acquired. Use the latest Android release with a higher bug fix
60 // version to avoid unnecessarily comparison errors with the latest release.
61 // This should be manually kept up-to-date on each Android release.
62 const int kDefaultAndroidMajorVersion = 5;
63 const int kDefaultAndroidMinorVersion = 1;
64 const int kDefaultAndroidBugfixVersion = 99;
66 // Parse out the OS version numbers from the system properties.
67 void ParseOSVersionNumbers(const char* os_version_str,
68 int32 *major_version,
69 int32 *minor_version,
70 int32 *bugfix_version) {
71 if (os_version_str[0]) {
72 // Try to parse out the version numbers from the string.
73 int num_read = sscanf(os_version_str, "%d.%d.%d", major_version,
74 minor_version, bugfix_version);
76 if (num_read > 0) {
77 // If we don't have a full set of version numbers, make the extras 0.
78 if (num_read < 2) *minor_version = 0;
79 if (num_read < 3) *bugfix_version = 0;
80 return;
84 // For some reason, we couldn't parse the version number string.
85 *major_version = kDefaultAndroidMajorVersion;
86 *minor_version = kDefaultAndroidMinorVersion;
87 *bugfix_version = kDefaultAndroidBugfixVersion;
90 // Parses a system property (specified with unit 'k','m' or 'g').
91 // Returns a value in bytes.
92 // Returns -1 if the string could not be parsed.
93 int64 ParseSystemPropertyBytes(const base::StringPiece& str) {
94 const int64 KB = 1024;
95 const int64 MB = 1024 * KB;
96 const int64 GB = 1024 * MB;
97 if (str.size() == 0u)
98 return -1;
99 int64 unit_multiplier = 1;
100 size_t length = str.size();
101 if (str[length - 1] == 'k') {
102 unit_multiplier = KB;
103 length--;
104 } else if (str[length - 1] == 'm') {
105 unit_multiplier = MB;
106 length--;
107 } else if (str[length - 1] == 'g') {
108 unit_multiplier = GB;
109 length--;
111 int64 result = 0;
112 bool parsed = base::StringToInt64(str.substr(0, length), &result);
113 bool negative = result <= 0;
114 bool overflow = result >= std::numeric_limits<int64>::max() / unit_multiplier;
115 if (!parsed || negative || overflow)
116 return -1;
117 return result * unit_multiplier;
120 int GetDalvikHeapSizeMB() {
121 char heap_size_str[PROP_VALUE_MAX];
122 __system_property_get("dalvik.vm.heapsize", heap_size_str);
123 // dalvik.vm.heapsize property is writable by a root user.
124 // Clamp it to reasonable range as a sanity check,
125 // a typical android device will never have less than 48MB.
126 const int64 MB = 1024 * 1024;
127 int64 result = ParseSystemPropertyBytes(heap_size_str);
128 if (result == -1) {
129 // We should consider not exposing these values if they are not reliable.
130 LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str;
131 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3;
133 result = std::min<int64>(std::max<int64>(32 * MB, result), 1024 * MB) / MB;
134 return static_cast<int>(result);
137 int GetDalvikHeapGrowthLimitMB() {
138 char heap_size_str[PROP_VALUE_MAX];
139 __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str);
140 // dalvik.vm.heapgrowthlimit property is writable by a root user.
141 // Clamp it to reasonable range as a sanity check,
142 // a typical android device will never have less than 24MB.
143 const int64 MB = 1024 * 1024;
144 int64 result = ParseSystemPropertyBytes(heap_size_str);
145 if (result == -1) {
146 // We should consider not exposing these values if they are not reliable.
147 LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str;
148 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6;
150 result = std::min<int64>(std::max<int64>(16 * MB, result), 512 * MB) / MB;
151 return static_cast<int>(result);
154 } // anonymous namespace
156 namespace base {
158 std::string SysInfo::HardwareModelName() {
159 char device_model_str[PROP_VALUE_MAX];
160 __system_property_get("ro.product.model", device_model_str);
161 return std::string(device_model_str);
164 std::string SysInfo::OperatingSystemName() {
165 return "Android";
168 std::string SysInfo::OperatingSystemVersion() {
169 int32 major, minor, bugfix;
170 OperatingSystemVersionNumbers(&major, &minor, &bugfix);
171 return StringPrintf("%d.%d.%d", major, minor, bugfix);
174 void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
175 int32* minor_version,
176 int32* bugfix_version) {
177 // Read the version number string out from the properties.
178 char os_version_str[PROP_VALUE_MAX];
179 __system_property_get("ro.build.version.release", os_version_str);
181 // Parse out the numbers.
182 ParseOSVersionNumbers(os_version_str, major_version, minor_version,
183 bugfix_version);
186 std::string SysInfo::GetAndroidBuildCodename() {
187 char os_version_codename_str[PROP_VALUE_MAX];
188 __system_property_get("ro.build.version.codename", os_version_codename_str);
189 return std::string(os_version_codename_str);
192 std::string SysInfo::GetAndroidBuildID() {
193 char os_build_id_str[PROP_VALUE_MAX];
194 __system_property_get("ro.build.id", os_build_id_str);
195 return std::string(os_build_id_str);
198 int SysInfo::DalvikHeapSizeMB() {
199 static int heap_size = GetDalvikHeapSizeMB();
200 return heap_size;
203 int SysInfo::DalvikHeapGrowthLimitMB() {
204 static int heap_growth_limit = GetDalvikHeapGrowthLimitMB();
205 return heap_growth_limit;
208 static base::LazyInstance<
209 base::internal::LazySysInfoValue<bool,
210 android::SysUtils::IsLowEndDeviceFromJni> >::Leaky
211 g_lazy_low_end_device = LAZY_INSTANCE_INITIALIZER;
213 bool SysInfo::IsLowEndDevice() {
214 return g_lazy_low_end_device.Get().value();
218 } // namespace base