Add whitelist for extensions to put <all_urls> in externally_connectable
[chromium-blink-merge.git] / base / sys_info_android.cc
blobab760e5579721843c64244f59d52cf23c037c7cc
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 // TODO(rmcilroy): Update API level when 'L' gets an official API level.
19 #if (__ANDROID_API__ >= 9999 /* 'L' */)
21 namespace {
23 typedef int (SystemPropertyGetFunction)(const char*, char*);
25 SystemPropertyGetFunction* DynamicallyLoadRealSystemPropertyGet() {
26 // libc.so should already be open, get a handle to it.
27 void* handle = dlopen("libc.so", RTLD_NOLOAD);
28 if (!handle) {
29 LOG(FATAL) << "Cannot dlopen libc.so: " << dlerror();
31 SystemPropertyGetFunction* real_system_property_get =
32 reinterpret_cast<SystemPropertyGetFunction*>(
33 dlsym(handle, "__system_property_get"));
34 if (!real_system_property_get) {
35 LOG(FATAL) << "Cannot resolve __system_property_get(): " << dlerror();
37 return real_system_property_get;
40 static base::LazyInstance<base::internal::LazySysInfoValue<
41 SystemPropertyGetFunction*, DynamicallyLoadRealSystemPropertyGet> >::Leaky
42 g_lazy_real_system_property_get = LAZY_INSTANCE_INITIALIZER;
44 } // namespace
46 // Android 'L' removes __system_property_get from the NDK, however it is still
47 // a hidden symbol in libc. Until we remove all calls of __system_property_get
48 // from Chrome we work around this by defining a weak stub here, which uses
49 // dlsym to but ensures that Chrome uses the real system
50 // implementatation when loaded. http://crbug.com/392191.
51 int __system_property_get(const char* name, char* value) {
52 return g_lazy_real_system_property_get.Get().value()(name, value);
55 #endif
57 namespace {
59 // Default version of Android to fall back to when actual version numbers
60 // cannot be acquired. Use the latest Android release with a higher bug fix
61 // version to avoid unnecessarily comparison errors with the latest release.
62 // This should be manually kept up-to-date on each Android release.
63 const int kDefaultAndroidMajorVersion = 4;
64 const int kDefaultAndroidMinorVersion = 4;
65 const int kDefaultAndroidBugfixVersion = 99;
67 // Parse out the OS version numbers from the system properties.
68 void ParseOSVersionNumbers(const char* os_version_str,
69 int32 *major_version,
70 int32 *minor_version,
71 int32 *bugfix_version) {
72 if (os_version_str[0]) {
73 // Try to parse out the version numbers from the string.
74 int num_read = sscanf(os_version_str, "%d.%d.%d", major_version,
75 minor_version, bugfix_version);
77 if (num_read > 0) {
78 // If we don't have a full set of version numbers, make the extras 0.
79 if (num_read < 2) *minor_version = 0;
80 if (num_read < 3) *bugfix_version = 0;
81 return;
85 // For some reason, we couldn't parse the version number string.
86 *major_version = kDefaultAndroidMajorVersion;
87 *minor_version = kDefaultAndroidMinorVersion;
88 *bugfix_version = kDefaultAndroidBugfixVersion;
91 // Parses a system property (specified with unit 'k','m' or 'g').
92 // Returns a value in bytes.
93 // Returns -1 if the string could not be parsed.
94 int64 ParseSystemPropertyBytes(const base::StringPiece& str) {
95 const int64 KB = 1024;
96 const int64 MB = 1024 * KB;
97 const int64 GB = 1024 * MB;
98 if (str.size() == 0u)
99 return -1;
100 int64 unit_multiplier = 1;
101 size_t length = str.size();
102 if (str[length - 1] == 'k') {
103 unit_multiplier = KB;
104 length--;
105 } else if (str[length - 1] == 'm') {
106 unit_multiplier = MB;
107 length--;
108 } else if (str[length - 1] == 'g') {
109 unit_multiplier = GB;
110 length--;
112 int64 result = 0;
113 bool parsed = base::StringToInt64(str.substr(0, length), &result);
114 bool negative = result <= 0;
115 bool overflow = result >= std::numeric_limits<int64>::max() / unit_multiplier;
116 if (!parsed || negative || overflow)
117 return -1;
118 return result * unit_multiplier;
121 int GetDalvikHeapSizeMB() {
122 char heap_size_str[PROP_VALUE_MAX];
123 __system_property_get("dalvik.vm.heapsize", heap_size_str);
124 // dalvik.vm.heapsize property is writable by a root user.
125 // Clamp it to reasonable range as a sanity check,
126 // a typical android device will never have less than 48MB.
127 const int64 MB = 1024 * 1024;
128 int64 result = ParseSystemPropertyBytes(heap_size_str);
129 if (result == -1) {
130 // We should consider not exposing these values if they are not reliable.
131 LOG(ERROR) << "Can't parse dalvik.vm.heapsize: " << heap_size_str;
132 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 3;
134 result = std::min<int64>(std::max<int64>(32 * MB, result), 1024 * MB) / MB;
135 return static_cast<int>(result);
138 int GetDalvikHeapGrowthLimitMB() {
139 char heap_size_str[PROP_VALUE_MAX];
140 __system_property_get("dalvik.vm.heapgrowthlimit", heap_size_str);
141 // dalvik.vm.heapgrowthlimit property is writable by a root user.
142 // Clamp it to reasonable range as a sanity check,
143 // a typical android device will never have less than 24MB.
144 const int64 MB = 1024 * 1024;
145 int64 result = ParseSystemPropertyBytes(heap_size_str);
146 if (result == -1) {
147 // We should consider not exposing these values if they are not reliable.
148 LOG(ERROR) << "Can't parse dalvik.vm.heapgrowthlimit: " << heap_size_str;
149 result = base::SysInfo::AmountOfPhysicalMemoryMB() / 6;
151 result = std::min<int64>(std::max<int64>(16 * MB, result), 512 * MB) / MB;
152 return static_cast<int>(result);
155 } // anonymous namespace
157 namespace base {
159 std::string SysInfo::OperatingSystemName() {
160 return "Android";
163 std::string SysInfo::GetAndroidBuildCodename() {
164 char os_version_codename_str[PROP_VALUE_MAX];
165 __system_property_get("ro.build.version.codename", os_version_codename_str);
166 return std::string(os_version_codename_str);
169 std::string SysInfo::GetAndroidBuildID() {
170 char os_build_id_str[PROP_VALUE_MAX];
171 __system_property_get("ro.build.id", os_build_id_str);
172 return std::string(os_build_id_str);
175 std::string SysInfo::GetDeviceName() {
176 char device_model_str[PROP_VALUE_MAX];
177 __system_property_get("ro.product.model", device_model_str);
178 return std::string(device_model_str);
181 std::string SysInfo::OperatingSystemVersion() {
182 int32 major, minor, bugfix;
183 OperatingSystemVersionNumbers(&major, &minor, &bugfix);
184 return StringPrintf("%d.%d.%d", major, minor, bugfix);
187 void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
188 int32* minor_version,
189 int32* bugfix_version) {
190 // Read the version number string out from the properties.
191 char os_version_str[PROP_VALUE_MAX];
192 __system_property_get("ro.build.version.release", os_version_str);
194 // Parse out the numbers.
195 ParseOSVersionNumbers(os_version_str, major_version, minor_version,
196 bugfix_version);
199 int SysInfo::DalvikHeapSizeMB() {
200 static int heap_size = GetDalvikHeapSizeMB();
201 return heap_size;
204 int SysInfo::DalvikHeapGrowthLimitMB() {
205 static int heap_growth_limit = GetDalvikHeapGrowthLimitMB();
206 return heap_growth_limit;
209 static base::LazyInstance<
210 base::internal::LazySysInfoValue<bool,
211 android::SysUtils::IsLowEndDeviceFromJni> >::Leaky
212 g_lazy_low_end_device = LAZY_INSTANCE_INITIALIZER;
214 bool SysInfo::IsLowEndDevice() {
215 return g_lazy_low_end_device.Get().value();
219 } // namespace base