blob: ef5f1fedb51d576a69d1503c583dcc80f5d29e1f [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
7#include "base/basictypes.h"
[email protected]a0a1c5a2013-09-26 21:51:238#include "base/environment.h"
[email protected]54124ed02014-01-07 10:06:589#include "base/files/file.h"
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
[email protected]e3177dd52014-08-13 20:22:1411#include "base/files/file_util.h"
[email protected]8c32ac12011-04-05 11:31:4212#include "base/lazy_instance.h"
[email protected]dfa049e2013-02-07 02:57:2213#include "base/strings/string_number_conversions.h"
[email protected]eb62f7262013-03-30 14:29:0014#include "base/strings/string_piece.h"
[email protected]a0a1c5a2013-09-26 21:51:2315#include "base/strings/string_split.h"
[email protected]f4ebe772013-02-02 00:21:3916#include "base/strings/string_tokenizer.h"
[email protected]a0a1c5a2013-09-26 21:51:2317#include "base/strings/string_util.h"
[email protected]34b99632011-01-01 01:01:0618#include "base/threading/thread_restrictions.h"
[email protected]61b8ad72009-07-22 00:35:1819
20namespace base {
21
[email protected]a0a1c5a2013-09-26 21:51:2322namespace {
23
thestig073d514d2014-10-21 03:11:2124const char* const kLinuxStandardBaseVersionKeys[] = {
[email protected]8c32ac12011-04-05 11:31:4225 "CHROMEOS_RELEASE_VERSION",
26 "GOOGLE_RELEASE",
27 "DISTRIB_RELEASE",
[email protected]8c32ac12011-04-05 11:31:4228};
[email protected]49c4cf852013-09-27 19:28:2429
30const char kChromeOsReleaseNameKey[] = "CHROMEOS_RELEASE_NAME";
31
32const char* const kChromeOsReleaseNames[] = {
33 "Chrome OS",
34 "Chromium OS",
35};
[email protected]61b8ad72009-07-22 00:35:1836
37const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
38
[email protected]a0a1c5a2013-09-26 21:51:2339const char kLsbReleaseKey[] = "LSB_RELEASE";
40const char kLsbReleaseTimeKey[] = "LSB_RELEASE_TIME"; // Seconds since epoch
41
42const char kLsbReleaseSourceKey[] = "lsb-release";
43const char kLsbReleaseSourceEnv[] = "env";
44const char kLsbReleaseSourceFile[] = "file";
45
46class ChromeOSVersionInfo {
47 public:
48 ChromeOSVersionInfo() {
49 Parse();
[email protected]8c32ac12011-04-05 11:31:4250 }
51
[email protected]a0a1c5a2013-09-26 21:51:2352 void Parse() {
53 lsb_release_map_.clear();
54 major_version_ = 0;
55 minor_version_ = 0;
56 bugfix_version_ = 0;
[email protected]49c4cf852013-09-27 19:28:2457 is_running_on_chromeos_ = false;
[email protected]a0a1c5a2013-09-26 21:51:2358
59 std::string lsb_release, lsb_release_time_str;
[email protected]9eae4e62013-12-04 20:56:4960 scoped_ptr<Environment> env(Environment::Create());
[email protected]a0a1c5a2013-09-26 21:51:2361 bool parsed_from_env =
62 env->GetVar(kLsbReleaseKey, &lsb_release) &&
63 env->GetVar(kLsbReleaseTimeKey, &lsb_release_time_str);
64 if (parsed_from_env) {
65 double us = 0;
66 if (StringToDouble(lsb_release_time_str, &us))
[email protected]9eae4e62013-12-04 20:56:4967 lsb_release_time_ = Time::FromDoubleT(us);
[email protected]a0a1c5a2013-09-26 21:51:2368 } else {
69 // If the LSB_RELEASE and LSB_RELEASE_TIME environment variables are not
70 // set, fall back to a blocking read of the lsb_release file. This should
71 // only happen in non Chrome OS environments.
72 ThreadRestrictions::ScopedAllowIO allow_io;
73 FilePath path(kLinuxStandardBaseReleaseFile);
74 ReadFileToString(path, &lsb_release);
[email protected]54124ed02014-01-07 10:06:5875 File::Info fileinfo;
[email protected]9eae4e62013-12-04 20:56:4976 if (GetFileInfo(path, &fileinfo))
[email protected]a0a1c5a2013-09-26 21:51:2377 lsb_release_time_ = fileinfo.creation_time;
78 }
79 ParseLsbRelease(lsb_release);
80 // For debugging:
81 lsb_release_map_[kLsbReleaseSourceKey] =
82 parsed_from_env ? kLsbReleaseSourceEnv : kLsbReleaseSourceFile;
83 }
84
85 bool GetLsbReleaseValue(const std::string& key, std::string* value) {
86 SysInfo::LsbReleaseMap::const_iterator iter = lsb_release_map_.find(key);
87 if (iter == lsb_release_map_.end())
88 return false;
89 *value = iter->second;
90 return true;
91 }
92
93 void GetVersionNumbers(int32* major_version,
94 int32* minor_version,
95 int32* bugfix_version) {
96 *major_version = major_version_;
97 *minor_version = minor_version_;
98 *bugfix_version = bugfix_version_;
99 }
100
[email protected]9eae4e62013-12-04 20:56:49101 const Time& lsb_release_time() const { return lsb_release_time_; }
[email protected]a0a1c5a2013-09-26 21:51:23102 const SysInfo::LsbReleaseMap& lsb_release_map() const {
103 return lsb_release_map_;
104 }
[email protected]49c4cf852013-09-27 19:28:24105 bool is_running_on_chromeos() const { return is_running_on_chromeos_; }
[email protected]a0a1c5a2013-09-26 21:51:23106
107 private:
108 void ParseLsbRelease(const std::string& lsb_release) {
109 // Parse and cache lsb_release key pairs. There should only be a handful
110 // of entries so the overhead for this will be small, and it can be
111 // useful for debugging.
112 std::vector<std::pair<std::string, std::string> > pairs;
[email protected]9eae4e62013-12-04 20:56:49113 SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs);
[email protected]a0a1c5a2013-09-26 21:51:23114 for (size_t i = 0; i < pairs.size(); ++i) {
115 std::string key, value;
116 TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key);
117 TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value);
118 if (key.empty())
119 continue;
120 lsb_release_map_[key] = value;
121 }
122 // Parse the version from the first matching recognized version key.
123 std::string version;
[email protected]49c4cf852013-09-27 19:28:24124 for (size_t i = 0; i < arraysize(kLinuxStandardBaseVersionKeys); ++i) {
[email protected]a0a1c5a2013-09-26 21:51:23125 std::string key = kLinuxStandardBaseVersionKeys[i];
126 if (GetLsbReleaseValue(key, &version) && !version.empty())
127 break;
128 }
129 StringTokenizer tokenizer(version, ".");
130 if (tokenizer.GetNext()) {
131 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
132 &major_version_);
133 }
134 if (tokenizer.GetNext()) {
135 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
136 &minor_version_);
137 }
138 if (tokenizer.GetNext()) {
139 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
140 &bugfix_version_);
141 }
[email protected]49c4cf852013-09-27 19:28:24142
143 // Check release name for Chrome OS.
144 std::string release_name;
145 if (GetLsbReleaseValue(kChromeOsReleaseNameKey, &release_name)) {
146 for (size_t i = 0; i < arraysize(kChromeOsReleaseNames); ++i) {
147 if (release_name == kChromeOsReleaseNames[i]) {
148 is_running_on_chromeos_ = true;
149 break;
150 }
151 }
152 }
[email protected]a0a1c5a2013-09-26 21:51:23153 }
154
[email protected]9eae4e62013-12-04 20:56:49155 Time lsb_release_time_;
[email protected]a0a1c5a2013-09-26 21:51:23156 SysInfo::LsbReleaseMap lsb_release_map_;
157 int32 major_version_;
158 int32 minor_version_;
159 int32 bugfix_version_;
[email protected]49c4cf852013-09-27 19:28:24160 bool is_running_on_chromeos_;
[email protected]8c32ac12011-04-05 11:31:42161};
162
[email protected]a0a1c5a2013-09-26 21:51:23163static LazyInstance<ChromeOSVersionInfo>
164 g_chrome_os_version_info = LAZY_INSTANCE_INITIALIZER;
165
166ChromeOSVersionInfo& GetChromeOSVersionInfo() {
167 return g_chrome_os_version_info.Get();
168}
169
170} // namespace
[email protected]8c32ac12011-04-05 11:31:42171
[email protected]61b8ad72009-07-22 00:35:18172// static
[email protected]f481221192011-04-07 22:15:34173void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
174 int32* minor_version,
175 int32* bugfix_version) {
[email protected]a0a1c5a2013-09-26 21:51:23176 return GetChromeOSVersionInfo().GetVersionNumbers(
177 major_version, minor_version, bugfix_version);
[email protected]61b8ad72009-07-22 00:35:18178}
179
180// static
[email protected]a0a1c5a2013-09-26 21:51:23181const SysInfo::LsbReleaseMap& SysInfo::GetLsbReleaseMap() {
182 return GetChromeOSVersionInfo().lsb_release_map();
[email protected]61b8ad72009-07-22 00:35:18183}
184
185// static
[email protected]a0a1c5a2013-09-26 21:51:23186bool SysInfo::GetLsbReleaseValue(const std::string& key, std::string* value) {
187 return GetChromeOSVersionInfo().GetLsbReleaseValue(key, value);
[email protected]61b8ad72009-07-22 00:35:18188}
189
[email protected]af25b472012-04-25 01:59:44190// static
[email protected]a0a1c5a2013-09-26 21:51:23191std::string SysInfo::GetLsbReleaseBoard() {
192 const char kMachineInfoBoard[] = "CHROMEOS_RELEASE_BOARD";
193 std::string board;
194 if (!GetLsbReleaseValue(kMachineInfoBoard, &board))
195 board = "unknown";
196 return board;
197}
198
199// static
[email protected]9eae4e62013-12-04 20:56:49200Time SysInfo::GetLsbReleaseTime() {
[email protected]a0a1c5a2013-09-26 21:51:23201 return GetChromeOSVersionInfo().lsb_release_time();
202}
203
204// static
[email protected]49c4cf852013-09-27 19:28:24205bool SysInfo::IsRunningOnChromeOS() {
206 return GetChromeOSVersionInfo().is_running_on_chromeos();
207}
208
209// static
[email protected]a0a1c5a2013-09-26 21:51:23210void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release,
211 const Time& lsb_release_time) {
[email protected]9eae4e62013-12-04 20:56:49212 scoped_ptr<Environment> env(Environment::Create());
[email protected]a0a1c5a2013-09-26 21:51:23213 env->SetVar(kLsbReleaseKey, lsb_release);
214 env->SetVar(kLsbReleaseTimeKey,
[email protected]9eae4e62013-12-04 20:56:49215 DoubleToString(lsb_release_time.ToDoubleT()));
[email protected]a0a1c5a2013-09-26 21:51:23216 g_chrome_os_version_info.Get().Parse();
[email protected]af25b472012-04-25 01:59:44217}
218
[email protected]61b8ad72009-07-22 00:35:18219} // namespace base