Fix a crash in the LegacyRenderWidgetHostHWND destruction code path which occurs...
[chromium-blink-merge.git] / base / sys_info_android.cc
blobab7c05d7c6f00686baac50e539256f95a728ff92
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 <sys/system_properties.h>
9 #include "base/android/sys_utils.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/sys_info_internal.h"
17 namespace {
19 // Default version of Android to fall back to when actual version numbers
20 // cannot be acquired. This is an obviously invalid version number. Code
21 // doing version comparison should treat it as so and fail all comparisons.
22 const int kDefaultAndroidMajorVersion = 0;
23 const int kDefaultAndroidMinorVersion = 0;
24 const int kDefaultAndroidBugfixVersion = 0;
26 // Parse out the OS version numbers from the system properties.
27 void ParseOSVersionNumbers(const char* os_version_str,
28 int32 *major_version,
29 int32 *minor_version,
30 int32 *bugfix_version) {
31 if (os_version_str[0]) {
32 // Try to parse out the version numbers from the string.
33 int num_read = sscanf(os_version_str, "%d.%d.%d", major_version,
34 minor_version, bugfix_version);
36 if (num_read > 0) {
37 // If we don't have a full set of version numbers, make the extras 0.
38 if (num_read < 2) *minor_version = 0;
39 if (num_read < 3) *bugfix_version = 0;
40 return;
44 // For some reason, we couldn't parse the version number string.
45 *major_version = kDefaultAndroidMajorVersion;
46 *minor_version = kDefaultAndroidMinorVersion;
47 *bugfix_version = kDefaultAndroidBugfixVersion;
50 // Parses a system property (specified with unit 'k','m' or 'g').
51 // Returns a value in bytes.
52 // Returns -1 if the string could not be parsed.
53 int64 ParseSystemPropertyBytes(const base::StringPiece& str) {
54 const int64 KB = 1024;
55 const int64 MB = 1024 * KB;
56 const int64 GB = 1024 * MB;
57 if (str.size() == 0u)
58 return -1;
59 int64 unit_multiplier = 1;
60 size_t length = str.size();
61 if (str[length - 1] == 'k') {
62 unit_multiplier = KB;
63 length--;
64 } else if (str[length - 1] == 'm') {
65 unit_multiplier = MB;
66 length--;
67 } else if (str[length - 1] == 'g') {
68 unit_multiplier = GB;
69 length--;
71 int64 result = 0;
72 bool parsed = base::StringToInt64(str.substr(0, length), &result);
73 bool negative = result <= 0;
74 bool overflow = result >= std::numeric_limits<int64>::max() / unit_multiplier;
75 if (!parsed || negative || overflow)
76 return -1;
77 return result * unit_multiplier;
80 int GetDalvikHeapSizeMB() {
81 char heap_size_str[PROP_VALUE_MAX];
82 __system_property_get("dalvik.vm.heapsize", heap_size_str);
83 // dalvik.vm.heapsize property is writable by a root user.
84 // Clamp it to reasonable range as a sanity check,
85 // a typical android device will never have less than 48MB.
86 const int64 MB = 1024 * 1024;
87 int64 result = ParseSystemPropertyBytes(heap_size_str);
88 if (result == -1) {
89 // We should consider not exposing these values if they are not reliable.
90 LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str;
91 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3;
93 result = std::min<int64>(std::max<int64>(32 * MB, result), 1024 * MB) / MB;
94 return static_cast<int>(result);
97 int GetDalvikHeapGrowthLimitMB() {
98 char heap_size_str[PROP_VALUE_MAX];
99 __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str);
100 // dalvik.vm.heapgrowthlimit property is writable by a root user.
101 // Clamp it to reasonable range as a sanity check,
102 // a typical android device will never have less than 24MB.
103 const int64 MB = 1024 * 1024;
104 int64 result = ParseSystemPropertyBytes(heap_size_str);
105 if (result == -1) {
106 // We should consider not exposing these values if they are not reliable.
107 LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str;
108 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6;
110 result = std::min<int64>(std::max<int64>(16 * MB, result), 512 * MB) / MB;
111 return static_cast<int>(result);
114 } // anonymous namespace
116 namespace base {
118 std::string SysInfo::OperatingSystemName() {
119 return "Android";
122 std::string SysInfo::GetAndroidBuildCodename() {
123 char os_version_codename_str[PROP_VALUE_MAX];
124 __system_property_get("ro.build.version.codename", os_version_codename_str);
125 return std::string(os_version_codename_str);
128 std::string SysInfo::GetAndroidBuildID() {
129 char os_build_id_str[PROP_VALUE_MAX];
130 __system_property_get("ro.build.id", os_build_id_str);
131 return std::string(os_build_id_str);
134 std::string SysInfo::GetDeviceName() {
135 char device_model_str[PROP_VALUE_MAX];
136 __system_property_get("ro.product.model", device_model_str);
137 return std::string(device_model_str);
140 std::string SysInfo::OperatingSystemVersion() {
141 int32 major, minor, bugfix;
142 OperatingSystemVersionNumbers(&major, &minor, &bugfix);
143 return StringPrintf("%d.%d.%d", major, minor, bugfix);
146 void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
147 int32* minor_version,
148 int32* bugfix_version) {
149 // Read the version number string out from the properties.
150 char os_version_str[PROP_VALUE_MAX];
151 __system_property_get("ro.build.version.release", os_version_str);
153 // Parse out the numbers.
154 ParseOSVersionNumbers(os_version_str, major_version, minor_version,
155 bugfix_version);
158 int SysInfo::DalvikHeapSizeMB() {
159 static int heap_size = GetDalvikHeapSizeMB();
160 return heap_size;
163 int SysInfo::DalvikHeapGrowthLimitMB() {
164 static int heap_growth_limit = GetDalvikHeapGrowthLimitMB();
165 return heap_growth_limit;
168 static base::LazyInstance<
169 base::internal::LazySysInfoValue<bool,
170 android::SysUtils::IsLowEndDeviceFromJni> >::Leaky
171 g_lazy_low_end_device = LAZY_INSTANCE_INITIALIZER;
173 bool SysInfo::IsLowEndDevice() {
174 return g_lazy_low_end_device.Get().value();
178 } // namespace base