khorimoto@chromium.org | c7e4e00 | 2012-04-25 10:59:44 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 2 | // 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 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 10 | #include "base/environment.h" |
rvargas@chromium.org | b005b38 | 2014-01-07 19:06:58 +0900 | [diff] [blame] | 11 | #include "base/files/file.h" |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame] | 13 | #include "base/files/file_util.h" |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 14 | #include "base/lazy_instance.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 15 | #include "base/macros.h" |
brettw@chromium.org | abcde5c | 2013-02-07 11:57:22 +0900 | [diff] [blame] | 16 | #include "base/strings/string_number_conversions.h" |
tfarina@chromium.org | b6d4911 | 2013-03-30 23:29:00 +0900 | [diff] [blame] | 17 | #include "base/strings/string_piece.h" |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 18 | #include "base/strings/string_split.h" |
brettw@chromium.org | 2cbc287 | 2013-02-02 09:21:39 +0900 | [diff] [blame] | 19 | #include "base/strings/string_tokenizer.h" |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
brettw@chromium.org | 5b5f5e0 | 2011-01-01 10:01:06 +0900 | [diff] [blame] | 21 | #include "base/threading/thread_restrictions.h" |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 22 | |
| 23 | namespace base { |
| 24 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 25 | namespace { |
| 26 | |
thestig | dd7aaa0 | 2014-10-21 12:11:21 +0900 | [diff] [blame] | 27 | const char* const kLinuxStandardBaseVersionKeys[] = { |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 28 | "CHROMEOS_RELEASE_VERSION", |
| 29 | "GOOGLE_RELEASE", |
| 30 | "DISTRIB_RELEASE", |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 31 | }; |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 32 | |
| 33 | const char kChromeOsReleaseNameKey[] = "CHROMEOS_RELEASE_NAME"; |
| 34 | |
| 35 | const char* const kChromeOsReleaseNames[] = { |
| 36 | "Chrome OS", |
| 37 | "Chromium OS", |
| 38 | }; |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 39 | |
| 40 | const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release"; |
| 41 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 42 | const char kLsbReleaseKey[] = "LSB_RELEASE"; |
| 43 | const char kLsbReleaseTimeKey[] = "LSB_RELEASE_TIME"; // Seconds since epoch |
| 44 | |
| 45 | const char kLsbReleaseSourceKey[] = "lsb-release"; |
| 46 | const char kLsbReleaseSourceEnv[] = "env"; |
| 47 | const char kLsbReleaseSourceFile[] = "file"; |
| 48 | |
| 49 | class ChromeOSVersionInfo { |
| 50 | public: |
| 51 | ChromeOSVersionInfo() { |
| 52 | Parse(); |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 53 | } |
| 54 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 55 | void Parse() { |
| 56 | lsb_release_map_.clear(); |
| 57 | major_version_ = 0; |
| 58 | minor_version_ = 0; |
| 59 | bugfix_version_ = 0; |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 60 | is_running_on_chromeos_ = false; |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 61 | |
| 62 | std::string lsb_release, lsb_release_time_str; |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 63 | std::unique_ptr<Environment> env(Environment::Create()); |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 64 | 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)) |
brettw@chromium.org | a915403 | 2013-12-05 05:56:49 +0900 | [diff] [blame] | 70 | lsb_release_time_ = Time::FromDoubleT(us); |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 71 | } 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); |
rvargas@chromium.org | b005b38 | 2014-01-07 19:06:58 +0900 | [diff] [blame] | 78 | File::Info fileinfo; |
brettw@chromium.org | a915403 | 2013-12-05 05:56:49 +0900 | [diff] [blame] | 79 | if (GetFileInfo(path, &fileinfo)) |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 80 | 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 | |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 96 | void GetVersionNumbers(int32_t* major_version, |
| 97 | int32_t* minor_version, |
| 98 | int32_t* bugfix_version) { |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 99 | *major_version = major_version_; |
| 100 | *minor_version = minor_version_; |
| 101 | *bugfix_version = bugfix_version_; |
| 102 | } |
| 103 | |
brettw@chromium.org | a915403 | 2013-12-05 05:56:49 +0900 | [diff] [blame] | 104 | const Time& lsb_release_time() const { return lsb_release_time_; } |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 105 | const SysInfo::LsbReleaseMap& lsb_release_map() const { |
| 106 | return lsb_release_map_; |
| 107 | } |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 108 | bool is_running_on_chromeos() const { return is_running_on_chromeos_; } |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 109 | |
| 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. |
patrikackland | d5168d9 | 2015-04-28 06:29:45 +0900 | [diff] [blame] | 115 | base::StringPairs pairs; |
brettw@chromium.org | a915403 | 2013-12-05 05:56:49 +0900 | [diff] [blame] | 116 | SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs); |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 117 | 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; |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 127 | for (size_t i = 0; i < arraysize(kLinuxStandardBaseVersionKeys); ++i) { |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 128 | std::string key = kLinuxStandardBaseVersionKeys[i]; |
| 129 | if (GetLsbReleaseValue(key, &version) && !version.empty()) |
| 130 | break; |
| 131 | } |
| 132 | StringTokenizer tokenizer(version, "."); |
| 133 | if (tokenizer.GetNext()) { |
David Benjamin | c444733 | 2018-05-08 11:13:51 +0900 | [diff] [blame] | 134 | StringToInt(tokenizer.token_piece(), &major_version_); |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 135 | } |
| 136 | if (tokenizer.GetNext()) { |
David Benjamin | c444733 | 2018-05-08 11:13:51 +0900 | [diff] [blame] | 137 | StringToInt(tokenizer.token_piece(), &minor_version_); |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 138 | } |
| 139 | if (tokenizer.GetNext()) { |
David Benjamin | c444733 | 2018-05-08 11:13:51 +0900 | [diff] [blame] | 140 | StringToInt(tokenizer.token_piece(), &bugfix_version_); |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 141 | } |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 142 | |
| 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 | } |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 153 | } |
| 154 | |
brettw@chromium.org | a915403 | 2013-12-05 05:56:49 +0900 | [diff] [blame] | 155 | Time lsb_release_time_; |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 156 | SysInfo::LsbReleaseMap lsb_release_map_; |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 157 | int32_t major_version_; |
| 158 | int32_t minor_version_; |
| 159 | int32_t bugfix_version_; |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 160 | bool is_running_on_chromeos_; |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 161 | }; |
| 162 | |
jamescook | 01a7326 | 2016-07-20 07:11:10 +0900 | [diff] [blame] | 163 | static LazyInstance<ChromeOSVersionInfo>::Leaky |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 164 | g_chrome_os_version_info = LAZY_INSTANCE_INITIALIZER; |
| 165 | |
| 166 | ChromeOSVersionInfo& GetChromeOSVersionInfo() { |
| 167 | return g_chrome_os_version_info.Get(); |
| 168 | } |
| 169 | |
| 170 | } // namespace |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 171 | |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 172 | // static |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 173 | void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version, |
| 174 | int32_t* minor_version, |
| 175 | int32_t* bugfix_version) { |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 176 | return GetChromeOSVersionInfo().GetVersionNumbers( |
| 177 | major_version, minor_version, bugfix_version); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // static |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 181 | const SysInfo::LsbReleaseMap& SysInfo::GetLsbReleaseMap() { |
| 182 | return GetChromeOSVersionInfo().lsb_release_map(); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // static |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 186 | bool SysInfo::GetLsbReleaseValue(const std::string& key, std::string* value) { |
| 187 | return GetChromeOSVersionInfo().GetLsbReleaseValue(key, value); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 188 | } |
| 189 | |
khorimoto@chromium.org | c7e4e00 | 2012-04-25 10:59:44 +0900 | [diff] [blame] | 190 | // static |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 191 | std::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 |
brettw@chromium.org | a915403 | 2013-12-05 05:56:49 +0900 | [diff] [blame] | 200 | Time SysInfo::GetLsbReleaseTime() { |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 201 | return GetChromeOSVersionInfo().lsb_release_time(); |
| 202 | } |
| 203 | |
| 204 | // static |
stevenjb@chromium.org | 861313b | 2013-09-28 04:28:24 +0900 | [diff] [blame] | 205 | bool SysInfo::IsRunningOnChromeOS() { |
| 206 | return GetChromeOSVersionInfo().is_running_on_chromeos(); |
| 207 | } |
| 208 | |
| 209 | // static |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 210 | void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release, |
| 211 | const Time& lsb_release_time) { |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 212 | std::unique_ptr<Environment> env(Environment::Create()); |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 213 | env->SetVar(kLsbReleaseKey, lsb_release); |
Brett Wilson | bd836f1 | 2017-12-01 10:25:11 +0900 | [diff] [blame] | 214 | env->SetVar(kLsbReleaseTimeKey, NumberToString(lsb_release_time.ToDoubleT())); |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame] | 215 | g_chrome_os_version_info.Get().Parse(); |
khorimoto@chromium.org | c7e4e00 | 2012-04-25 10:59:44 +0900 | [diff] [blame] | 216 | } |
| 217 | |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 218 | } // namespace base |