Update .DEPS.git
[chromium-blink-merge.git] / base / sys_info_android.cc
blobc0bbe446e4c90d7db5ba5ec04943dfba475f42e6
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/string_number_conversions.h"
11 #include "base/string_piece.h"
13 namespace {
15 // Default version of Android to fall back to when actual version numbers
16 // cannot be acquired.
17 // TODO(dfalcantara): Keep this reasonably up to date with the latest publicly
18 // available version of Android.
19 static const int kDefaultAndroidMajorVersion = 4;
20 static const int kDefaultAndroidMinorVersion = 0;
21 static const int kDefaultAndroidBugfixVersion = 3;
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 int ParseHeapSize(const base::StringPiece& str) {
48 const int64 KB = 1024;
49 const int64 MB = 1024 * KB;
50 const int64 GB = 1024 * MB;
51 CHECK_GT(str.size(), 0u);
52 int64 factor = 1;
53 size_t length = str.size();
54 if (str[length - 1] == 'k') {
55 factor = KB;
56 length--;
57 } else if (str[length - 1] == 'm') {
58 factor = MB;
59 length--;
60 } else if (str[length - 1] == 'g') {
61 factor = GB;
62 length--;
63 } else {
64 CHECK('0' <= str[length - 1] && str[length - 1] <= '9');
66 int64 result = 0;
67 bool parsed = base::StringToInt64(str.substr(0, length), &result);
68 CHECK(parsed);
69 result = result * factor / MB;
70 // dalvik.vm.heapsize property is writable by user,
71 // truncate it to reasonable value to avoid overflows later.
72 result = std::min<int64>(std::max<int64>(32, result), 1024);
73 return static_cast<int>(result);
76 int GetDalvikHeapSizeMB() {
77 char heap_size_str[PROP_VALUE_MAX];
78 __system_property_get("dalvik.vm.heapsize", heap_size_str);
79 return ParseHeapSize(heap_size_str);
82 } // anonymous namespace
84 namespace base {
86 std::string SysInfo::OperatingSystemName() {
87 return "Android";
90 std::string SysInfo::GetAndroidBuildCodename() {
91 char os_version_codename_str[PROP_VALUE_MAX];
92 __system_property_get("ro.build.version.codename", os_version_codename_str);
93 return std::string(os_version_codename_str);
96 std::string SysInfo::GetAndroidBuildID() {
97 char os_build_id_str[PROP_VALUE_MAX];
98 __system_property_get("ro.build.id", os_build_id_str);
99 return std::string(os_build_id_str);
102 std::string SysInfo::GetDeviceName() {
103 char device_model_str[PROP_VALUE_MAX];
104 __system_property_get("ro.product.model", device_model_str);
105 return std::string(device_model_str);
108 void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
109 int32* minor_version,
110 int32* bugfix_version) {
111 // Read the version number string out from the properties.
112 char os_version_str[PROP_VALUE_MAX];
113 __system_property_get("ro.build.version.release", os_version_str);
115 // Parse out the numbers.
116 ParseOSVersionNumbers(os_version_str, major_version, minor_version,
117 bugfix_version);
120 int SysInfo::DalvikHeapSizeMB() {
121 static int heap_size = GetDalvikHeapSizeMB();
122 return heap_size;
125 } // namespace base