blob: 29f83845dc52edbf3113cf4412bb7e3a77eb8acf [file] [log] [blame]
[email protected]af25b472012-04-25 01:59:441// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]61b8ad72009-07-22 00:35:182// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/sys_info.h"
6
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8#include <stdint.h>
9
[email protected]a0a1c5a2013-09-26 21:51:2310#include "base/environment.h"
[email protected]54124ed02014-01-07 10:06:5811#include "base/files/file.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
[email protected]e3177dd52014-08-13 20:22:1413#include "base/files/file_util.h"
[email protected]8c32ac12011-04-05 11:31:4214#include "base/lazy_instance.h"
avi9b6f42932015-12-26 22:15:1415#include "base/macros.h"
[email protected]dfa049e2013-02-07 02:57:2216#include "base/strings/string_number_conversions.h"
[email protected]eb62f7262013-03-30 14:29:0017#include "base/strings/string_piece.h"
[email protected]a0a1c5a2013-09-26 21:51:2318#include "base/strings/string_split.h"
[email protected]f4ebe772013-02-02 00:21:3919#include "base/strings/string_tokenizer.h"
[email protected]a0a1c5a2013-09-26 21:51:2320#include "base/strings/string_util.h"
[email protected]34b99632011-01-01 01:01:0621#include "base/threading/thread_restrictions.h"
[email protected]61b8ad72009-07-22 00:35:1822
23namespace base {
24
[email protected]a0a1c5a2013-09-26 21:51:2325namespace {
26
thestig073d514d2014-10-21 03:11:2127const char* const kLinuxStandardBaseVersionKeys[] = {
[email protected]8c32ac12011-04-05 11:31:4228 "CHROMEOS_RELEASE_VERSION",
29 "GOOGLE_RELEASE",
30 "DISTRIB_RELEASE",
[email protected]8c32ac12011-04-05 11:31:4231};
[email protected]49c4cf852013-09-27 19:28:2432
33const char kChromeOsReleaseNameKey[] = "CHROMEOS_RELEASE_NAME";
34
35const char* const kChromeOsReleaseNames[] = {
36 "Chrome OS",
37 "Chromium OS",
38};
[email protected]61b8ad72009-07-22 00:35:1839
40const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
41
[email protected]a0a1c5a2013-09-26 21:51:2342const char kLsbReleaseKey[] = "LSB_RELEASE";
43const char kLsbReleaseTimeKey[] = "LSB_RELEASE_TIME"; // Seconds since epoch
44
45const char kLsbReleaseSourceKey[] = "lsb-release";
46const char kLsbReleaseSourceEnv[] = "env";
47const char kLsbReleaseSourceFile[] = "file";
48
49class ChromeOSVersionInfo {
50 public:
51 ChromeOSVersionInfo() {
52 Parse();
[email protected]8c32ac12011-04-05 11:31:4253 }
54
[email protected]a0a1c5a2013-09-26 21:51:2355 void Parse() {
56 lsb_release_map_.clear();
57 major_version_ = 0;
58 minor_version_ = 0;
59 bugfix_version_ = 0;
[email protected]49c4cf852013-09-27 19:28:2460 is_running_on_chromeos_ = false;
[email protected]a0a1c5a2013-09-26 21:51:2361
62 std::string lsb_release, lsb_release_time_str;
dcheng093de9b2016-04-04 21:25:5163 std::unique_ptr<Environment> env(Environment::Create());
[email protected]a0a1c5a2013-09-26 21:51:2364 bool parsed_from_env =
65 env->GetVar(kLsbReleaseKey, &lsb_release) &&
66 env->GetVar(kLsbReleaseTimeKey, &lsb_release_time_str);
67 if (parsed_from_env) {
68 double us = 0;
69 if (StringToDouble(lsb_release_time_str, &us))
[email protected]9eae4e62013-12-04 20:56:4970 lsb_release_time_ = Time::FromDoubleT(us);
[email protected]a0a1c5a2013-09-26 21:51:2371 } else {
72 // If the LSB_RELEASE and LSB_RELEASE_TIME environment variables are not
73 // set, fall back to a blocking read of the lsb_release file. This should
74 // only happen in non Chrome OS environments.
75 ThreadRestrictions::ScopedAllowIO allow_io;
76 FilePath path(kLinuxStandardBaseReleaseFile);
77 ReadFileToString(path, &lsb_release);
[email protected]54124ed02014-01-07 10:06:5878 File::Info fileinfo;
[email protected]9eae4e62013-12-04 20:56:4979 if (GetFileInfo(path, &fileinfo))
[email protected]a0a1c5a2013-09-26 21:51:2380 lsb_release_time_ = fileinfo.creation_time;
81 }
82 ParseLsbRelease(lsb_release);
83 // For debugging:
84 lsb_release_map_[kLsbReleaseSourceKey] =
85 parsed_from_env ? kLsbReleaseSourceEnv : kLsbReleaseSourceFile;
86 }
87
88 bool GetLsbReleaseValue(const std::string& key, std::string* value) {
89 SysInfo::LsbReleaseMap::const_iterator iter = lsb_release_map_.find(key);
90 if (iter == lsb_release_map_.end())
91 return false;
92 *value = iter->second;
93 return true;
94 }
95
avidd4e614352015-12-09 00:44:4996 void GetVersionNumbers(int32_t* major_version,
97 int32_t* minor_version,
98 int32_t* bugfix_version) {
[email protected]a0a1c5a2013-09-26 21:51:2399 *major_version = major_version_;
100 *minor_version = minor_version_;
101 *bugfix_version = bugfix_version_;
102 }
103
[email protected]9eae4e62013-12-04 20:56:49104 const Time& lsb_release_time() const { return lsb_release_time_; }
[email protected]a0a1c5a2013-09-26 21:51:23105 const SysInfo::LsbReleaseMap& lsb_release_map() const {
106 return lsb_release_map_;
107 }
[email protected]49c4cf852013-09-27 19:28:24108 bool is_running_on_chromeos() const { return is_running_on_chromeos_; }
[email protected]a0a1c5a2013-09-26 21:51:23109
110 private:
111 void ParseLsbRelease(const std::string& lsb_release) {
112 // Parse and cache lsb_release key pairs. There should only be a handful
113 // of entries so the overhead for this will be small, and it can be
114 // useful for debugging.
patrikackland484fbc42015-04-27 21:29:45115 base::StringPairs pairs;
[email protected]9eae4e62013-12-04 20:56:49116 SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs);
[email protected]a0a1c5a2013-09-26 21:51:23117 for (size_t i = 0; i < pairs.size(); ++i) {
118 std::string key, value;
119 TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key);
120 TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value);
121 if (key.empty())
122 continue;
123 lsb_release_map_[key] = value;
124 }
125 // Parse the version from the first matching recognized version key.
126 std::string version;
[email protected]49c4cf852013-09-27 19:28:24127 for (size_t i = 0; i < arraysize(kLinuxStandardBaseVersionKeys); ++i) {
[email protected]a0a1c5a2013-09-26 21:51:23128 std::string key = kLinuxStandardBaseVersionKeys[i];
129 if (GetLsbReleaseValue(key, &version) && !version.empty())
130 break;
131 }
132 StringTokenizer tokenizer(version, ".");
133 if (tokenizer.GetNext()) {
134 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
135 &major_version_);
136 }
137 if (tokenizer.GetNext()) {
138 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
139 &minor_version_);
140 }
141 if (tokenizer.GetNext()) {
142 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
143 &bugfix_version_);
144 }
[email protected]49c4cf852013-09-27 19:28:24145
146 // Check release name for Chrome OS.
147 std::string release_name;
148 if (GetLsbReleaseValue(kChromeOsReleaseNameKey, &release_name)) {
149 for (size_t i = 0; i < arraysize(kChromeOsReleaseNames); ++i) {
150 if (release_name == kChromeOsReleaseNames[i]) {
151 is_running_on_chromeos_ = true;
152 break;
153 }
154 }
155 }
[email protected]a0a1c5a2013-09-26 21:51:23156 }
157
[email protected]9eae4e62013-12-04 20:56:49158 Time lsb_release_time_;
[email protected]a0a1c5a2013-09-26 21:51:23159 SysInfo::LsbReleaseMap lsb_release_map_;
avidd4e614352015-12-09 00:44:49160 int32_t major_version_;
161 int32_t minor_version_;
162 int32_t bugfix_version_;
[email protected]49c4cf852013-09-27 19:28:24163 bool is_running_on_chromeos_;
[email protected]8c32ac12011-04-05 11:31:42164};
165
jamescook5aa2d8c2016-07-19 22:11:10166static LazyInstance<ChromeOSVersionInfo>::Leaky
[email protected]a0a1c5a2013-09-26 21:51:23167 g_chrome_os_version_info = LAZY_INSTANCE_INITIALIZER;
168
169ChromeOSVersionInfo& GetChromeOSVersionInfo() {
170 return g_chrome_os_version_info.Get();
171}
172
173} // namespace
[email protected]8c32ac12011-04-05 11:31:42174
[email protected]61b8ad72009-07-22 00:35:18175// static
avidd4e614352015-12-09 00:44:49176void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
177 int32_t* minor_version,
178 int32_t* bugfix_version) {
[email protected]a0a1c5a2013-09-26 21:51:23179 return GetChromeOSVersionInfo().GetVersionNumbers(
180 major_version, minor_version, bugfix_version);
[email protected]61b8ad72009-07-22 00:35:18181}
182
183// static
[email protected]a0a1c5a2013-09-26 21:51:23184const SysInfo::LsbReleaseMap& SysInfo::GetLsbReleaseMap() {
185 return GetChromeOSVersionInfo().lsb_release_map();
[email protected]61b8ad72009-07-22 00:35:18186}
187
188// static
[email protected]a0a1c5a2013-09-26 21:51:23189bool SysInfo::GetLsbReleaseValue(const std::string& key, std::string* value) {
190 return GetChromeOSVersionInfo().GetLsbReleaseValue(key, value);
[email protected]61b8ad72009-07-22 00:35:18191}
192
[email protected]af25b472012-04-25 01:59:44193// static
[email protected]a0a1c5a2013-09-26 21:51:23194std::string SysInfo::GetLsbReleaseBoard() {
195 const char kMachineInfoBoard[] = "CHROMEOS_RELEASE_BOARD";
196 std::string board;
197 if (!GetLsbReleaseValue(kMachineInfoBoard, &board))
198 board = "unknown";
199 return board;
200}
201
202// static
igorcov1a5a3f52016-12-01 19:42:32203std::string SysInfo::GetStrippedReleaseBoard() {
204 std::string board = GetLsbReleaseBoard();
205 const size_t index = board.find("-signed-");
206 if (index != std::string::npos)
207 board.resize(index);
208
209 return base::ToLowerASCII(board);
210}
211
212// static
[email protected]9eae4e62013-12-04 20:56:49213Time SysInfo::GetLsbReleaseTime() {
[email protected]a0a1c5a2013-09-26 21:51:23214 return GetChromeOSVersionInfo().lsb_release_time();
215}
216
217// static
[email protected]49c4cf852013-09-27 19:28:24218bool SysInfo::IsRunningOnChromeOS() {
219 return GetChromeOSVersionInfo().is_running_on_chromeos();
220}
221
222// static
[email protected]a0a1c5a2013-09-26 21:51:23223void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release,
224 const Time& lsb_release_time) {
dcheng093de9b2016-04-04 21:25:51225 std::unique_ptr<Environment> env(Environment::Create());
[email protected]a0a1c5a2013-09-26 21:51:23226 env->SetVar(kLsbReleaseKey, lsb_release);
227 env->SetVar(kLsbReleaseTimeKey,
[email protected]9eae4e62013-12-04 20:56:49228 DoubleToString(lsb_release_time.ToDoubleT()));
[email protected]a0a1c5a2013-09-26 21:51:23229 g_chrome_os_version_info.Get().Parse();
[email protected]af25b472012-04-25 01:59:44230}
231
[email protected]61b8ad72009-07-22 00:35:18232} // namespace base