Add depfile support to android build scripts
[chromium-blink-merge.git] / base / sys_info_android.cc
blob12da23b2878e329b54615857b4a91681bc0a1105
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/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_piece.h"
12 #include "base/strings/stringprintf.h"
14 namespace {
16 // Default version of Android to fall back to when actual version numbers
17 // cannot be acquired. This is an obviously invalid version number. Code
18 // doing version comparison should treat it as so and fail all comparisons.
19 const int kDefaultAndroidMajorVersion = 0;
20 const int kDefaultAndroidMinorVersion = 0;
21 const int kDefaultAndroidBugfixVersion = 0;
23 // Parse out the OS version numbers from the system properties.
24 void ParseOSVersionNumbers(const char* os_version_str,
25 int32 *major_version,
26 int32 *minor_version,
27 int32 *bugfix_version) {
28 if (os_version_str[0]) {
29 // Try to parse out the version numbers from the string.
30 int num_read = sscanf(os_version_str, "%d.%d.%d", major_version,
31 minor_version, bugfix_version);
33 if (num_read > 0) {
34 // If we don't have a full set of version numbers, make the extras 0.
35 if (num_read < 2) *minor_version = 0;
36 if (num_read < 3) *bugfix_version = 0;
37 return;
41 // For some reason, we couldn't parse the version number string.
42 *major_version = kDefaultAndroidMajorVersion;
43 *minor_version = kDefaultAndroidMinorVersion;
44 *bugfix_version = kDefaultAndroidBugfixVersion;
47 // Parses a system property (specified with unit 'k','m' or 'g').
48 // Returns a value in bytes.
49 // Returns -1 if the string could not be parsed.
50 int64 ParseSystemPropertyBytes(const base::StringPiece& str) {
51 const int64 KB = 1024;
52 const int64 MB = 1024 * KB;
53 const int64 GB = 1024 * MB;
54 if (str.size() == 0u)
55 return -1;
56 int64 unit_multiplier = 1;
57 size_t length = str.size();
58 if (str[length - 1] == 'k') {
59 unit_multiplier = KB;
60 length--;
61 } else if (str[length - 1] == 'm') {
62 unit_multiplier = MB;
63 length--;
64 } else if (str[length - 1] == 'g') {
65 unit_multiplier = GB;
66 length--;
68 int64 result = 0;
69 bool parsed = base::StringToInt64(str.substr(0, length), &result);
70 bool negative = result <= 0;
71 bool overflow = result >= std::numeric_limits<int64>::max() / unit_multiplier;
72 if (!parsed || negative || overflow)
73 return -1;
74 return result * unit_multiplier;
77 int GetDalvikHeapSizeMB() {
78 char heap_size_str[PROP_VALUE_MAX];
79 __system_property_get("dalvik.vm.heapsize", heap_size_str);
80 // dalvik.vm.heapsize property is writable by a root user.
81 // Clamp it to reasonable range as a sanity check,
82 // a typical android device will never have less than 48MB.
83 const int64 MB = 1024 * 1024;
84 int64 result = ParseSystemPropertyBytes(heap_size_str);
85 if (result == -1) {
86 // We should consider not exposing these values if they are not reliable.
87 LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str;
88 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3;
90 result = std::min<int64>(std::max<int64>(32 * MB, result), 1024 * MB) / MB;
91 return static_cast<int>(result);
94 int GetDalvikHeapGrowthLimitMB() {
95 char heap_size_str[PROP_VALUE_MAX];
96 __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str);
97 // dalvik.vm.heapgrowthlimit property is writable by a root user.
98 // Clamp it to reasonable range as a sanity check,
99 // a typical android device will never have less than 24MB.
100 const int64 MB = 1024 * 1024;
101 int64 result = ParseSystemPropertyBytes(heap_size_str);
102 if (result == -1) {
103 // We should consider not exposing these values if they are not reliable.
104 LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str;
105 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6;
107 result = std::min<int64>(std::max<int64>(16 * MB, result), 512 * MB) / MB;
108 return static_cast<int>(result);
111 } // anonymous namespace
113 namespace base {
115 std::string SysInfo::OperatingSystemName() {
116 return "Android";
119 std::string SysInfo::GetAndroidBuildCodename() {
120 char os_version_codename_str[PROP_VALUE_MAX];
121 __system_property_get("ro.build.version.codename", os_version_codename_str);
122 return std::string(os_version_codename_str);
125 std::string SysInfo::GetAndroidBuildID() {
126 char os_build_id_str[PROP_VALUE_MAX];
127 __system_property_get("ro.build.id", os_build_id_str);
128 return std::string(os_build_id_str);
131 std::string SysInfo::GetDeviceName() {
132 char device_model_str[PROP_VALUE_MAX];
133 __system_property_get("ro.product.model", device_model_str);
134 return std::string(device_model_str);
137 std::string SysInfo::OperatingSystemVersion() {
138 int32 major, minor, bugfix;
139 OperatingSystemVersionNumbers(&major, &minor, &bugfix);
140 return StringPrintf("%d.%d.%d", major, minor, bugfix);
143 void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
144 int32* minor_version,
145 int32* bugfix_version) {
146 // Read the version number string out from the properties.
147 char os_version_str[PROP_VALUE_MAX];
148 __system_property_get("ro.build.version.release", os_version_str);
150 // Parse out the numbers.
151 ParseOSVersionNumbers(os_version_str, major_version, minor_version,
152 bugfix_version);
155 int SysInfo::DalvikHeapSizeMB() {
156 static int heap_size = GetDalvikHeapSizeMB();
157 return heap_size;
160 int SysInfo::DalvikHeapGrowthLimitMB() {
161 static int heap_growth_limit = GetDalvikHeapGrowthLimitMB();
162 return heap_growth_limit;
166 } // namespace base