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 | |
| 7 | #include "base/basictypes.h" |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 8 | #include "base/environment.h" |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 9 | #include "base/file_util.h" |
brettw@chromium.org | 59eef1f | 2013-02-24 14:40:52 +0900 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 11 | #include "base/lazy_instance.h" |
brettw@chromium.org | abcde5c | 2013-02-07 11:57:22 +0900 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
tfarina@chromium.org | b6d4911 | 2013-03-30 23:29:00 +0900 | [diff] [blame] | 13 | #include "base/strings/string_piece.h" |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 14 | #include "base/strings/string_split.h" |
brettw@chromium.org | 2cbc287 | 2013-02-02 09:21:39 +0900 | [diff] [blame] | 15 | #include "base/strings/string_tokenizer.h" |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 16 | #include "base/strings/string_util.h" |
brettw@chromium.org | 5b5f5e0 | 2011-01-01 10:01:06 +0900 | [diff] [blame] | 17 | #include "base/threading/thread_restrictions.h" |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 18 | |
| 19 | namespace base { |
| 20 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 21 | namespace { |
| 22 | |
| 23 | const char* kLinuxStandardBaseVersionKeys[] = { |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 24 | "CHROMEOS_RELEASE_VERSION", |
| 25 | "GOOGLE_RELEASE", |
| 26 | "DISTRIB_RELEASE", |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 27 | }; |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 28 | const size_t kLinuxStandardBaseVersionKeysLength = |
| 29 | arraysize(kLinuxStandardBaseVersionKeys); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 30 | |
| 31 | const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release"; |
| 32 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 33 | const char kLsbReleaseKey[] = "LSB_RELEASE"; |
| 34 | const char kLsbReleaseTimeKey[] = "LSB_RELEASE_TIME"; // Seconds since epoch |
| 35 | |
| 36 | const char kLsbReleaseSourceKey[] = "lsb-release"; |
| 37 | const char kLsbReleaseSourceEnv[] = "env"; |
| 38 | const char kLsbReleaseSourceFile[] = "file"; |
| 39 | |
| 40 | class ChromeOSVersionInfo { |
| 41 | public: |
| 42 | ChromeOSVersionInfo() { |
| 43 | Parse(); |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 44 | } |
| 45 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 46 | void Parse() { |
| 47 | lsb_release_map_.clear(); |
| 48 | major_version_ = 0; |
| 49 | minor_version_ = 0; |
| 50 | bugfix_version_ = 0; |
| 51 | |
| 52 | std::string lsb_release, lsb_release_time_str; |
| 53 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 54 | bool parsed_from_env = |
| 55 | env->GetVar(kLsbReleaseKey, &lsb_release) && |
| 56 | env->GetVar(kLsbReleaseTimeKey, &lsb_release_time_str); |
| 57 | if (parsed_from_env) { |
| 58 | double us = 0; |
| 59 | if (StringToDouble(lsb_release_time_str, &us)) |
| 60 | lsb_release_time_ = base::Time::FromDoubleT(us); |
| 61 | } else { |
| 62 | // If the LSB_RELEASE and LSB_RELEASE_TIME environment variables are not |
| 63 | // set, fall back to a blocking read of the lsb_release file. This should |
| 64 | // only happen in non Chrome OS environments. |
| 65 | ThreadRestrictions::ScopedAllowIO allow_io; |
| 66 | FilePath path(kLinuxStandardBaseReleaseFile); |
| 67 | ReadFileToString(path, &lsb_release); |
| 68 | base::PlatformFileInfo fileinfo; |
| 69 | if (file_util::GetFileInfo(path, &fileinfo)) |
| 70 | lsb_release_time_ = fileinfo.creation_time; |
| 71 | } |
| 72 | ParseLsbRelease(lsb_release); |
| 73 | // For debugging: |
| 74 | lsb_release_map_[kLsbReleaseSourceKey] = |
| 75 | parsed_from_env ? kLsbReleaseSourceEnv : kLsbReleaseSourceFile; |
| 76 | } |
| 77 | |
| 78 | bool GetLsbReleaseValue(const std::string& key, std::string* value) { |
| 79 | SysInfo::LsbReleaseMap::const_iterator iter = lsb_release_map_.find(key); |
| 80 | if (iter == lsb_release_map_.end()) |
| 81 | return false; |
| 82 | *value = iter->second; |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | void GetVersionNumbers(int32* major_version, |
| 87 | int32* minor_version, |
| 88 | int32* bugfix_version) { |
| 89 | *major_version = major_version_; |
| 90 | *minor_version = minor_version_; |
| 91 | *bugfix_version = bugfix_version_; |
| 92 | } |
| 93 | |
| 94 | const base::Time& lsb_release_time() const { return lsb_release_time_; } |
| 95 | const SysInfo::LsbReleaseMap& lsb_release_map() const { |
| 96 | return lsb_release_map_; |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | void ParseLsbRelease(const std::string& lsb_release) { |
| 101 | // Parse and cache lsb_release key pairs. There should only be a handful |
| 102 | // of entries so the overhead for this will be small, and it can be |
| 103 | // useful for debugging. |
| 104 | std::vector<std::pair<std::string, std::string> > pairs; |
| 105 | base::SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs); |
| 106 | for (size_t i = 0; i < pairs.size(); ++i) { |
| 107 | std::string key, value; |
| 108 | TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key); |
| 109 | TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value); |
| 110 | if (key.empty()) |
| 111 | continue; |
| 112 | lsb_release_map_[key] = value; |
| 113 | } |
| 114 | // Parse the version from the first matching recognized version key. |
| 115 | std::string version; |
| 116 | for (size_t i = 0; i < kLinuxStandardBaseVersionKeysLength; ++i) { |
| 117 | std::string key = kLinuxStandardBaseVersionKeys[i]; |
| 118 | if (GetLsbReleaseValue(key, &version) && !version.empty()) |
| 119 | break; |
| 120 | } |
| 121 | StringTokenizer tokenizer(version, "."); |
| 122 | if (tokenizer.GetNext()) { |
| 123 | StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()), |
| 124 | &major_version_); |
| 125 | } |
| 126 | if (tokenizer.GetNext()) { |
| 127 | StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()), |
| 128 | &minor_version_); |
| 129 | } |
| 130 | if (tokenizer.GetNext()) { |
| 131 | StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()), |
| 132 | &bugfix_version_); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | base::Time lsb_release_time_; |
| 137 | SysInfo::LsbReleaseMap lsb_release_map_; |
| 138 | int32 major_version_; |
| 139 | int32 minor_version_; |
| 140 | int32 bugfix_version_; |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 141 | }; |
| 142 | |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 143 | static LazyInstance<ChromeOSVersionInfo> |
| 144 | g_chrome_os_version_info = LAZY_INSTANCE_INITIALIZER; |
| 145 | |
| 146 | ChromeOSVersionInfo& GetChromeOSVersionInfo() { |
| 147 | return g_chrome_os_version_info.Get(); |
| 148 | } |
| 149 | |
| 150 | } // namespace |
dgozman@chromium.org | 4fadf42 | 2011-04-05 20:31:42 +0900 | [diff] [blame] | 151 | |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 152 | // static |
pkasting@chromium.org | 271d9db | 2011-04-08 07:15:34 +0900 | [diff] [blame] | 153 | void SysInfo::OperatingSystemVersionNumbers(int32* major_version, |
| 154 | int32* minor_version, |
| 155 | int32* bugfix_version) { |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 156 | return GetChromeOSVersionInfo().GetVersionNumbers( |
| 157 | major_version, minor_version, bugfix_version); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // static |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 161 | const SysInfo::LsbReleaseMap& SysInfo::GetLsbReleaseMap() { |
| 162 | return GetChromeOSVersionInfo().lsb_release_map(); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // static |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 166 | bool SysInfo::GetLsbReleaseValue(const std::string& key, std::string* value) { |
| 167 | return GetChromeOSVersionInfo().GetLsbReleaseValue(key, value); |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 168 | } |
| 169 | |
khorimoto@chromium.org | c7e4e00 | 2012-04-25 10:59:44 +0900 | [diff] [blame] | 170 | // static |
stevenjb@chromium.org | 2f4de04 | 2013-09-27 06:51:23 +0900 | [diff] [blame^] | 171 | std::string SysInfo::GetLsbReleaseBoard() { |
| 172 | const char kMachineInfoBoard[] = "CHROMEOS_RELEASE_BOARD"; |
| 173 | std::string board; |
| 174 | if (!GetLsbReleaseValue(kMachineInfoBoard, &board)) |
| 175 | board = "unknown"; |
| 176 | return board; |
| 177 | } |
| 178 | |
| 179 | // static |
| 180 | base::Time SysInfo::GetLsbReleaseTime() { |
| 181 | return GetChromeOSVersionInfo().lsb_release_time(); |
| 182 | } |
| 183 | |
| 184 | // static |
| 185 | void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release, |
| 186 | const Time& lsb_release_time) { |
| 187 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 188 | env->SetVar(kLsbReleaseKey, lsb_release); |
| 189 | env->SetVar(kLsbReleaseTimeKey, |
| 190 | base::DoubleToString(lsb_release_time.ToDoubleT())); |
| 191 | g_chrome_os_version_info.Get().Parse(); |
khorimoto@chromium.org | c7e4e00 | 2012-04-25 10:59:44 +0900 | [diff] [blame] | 192 | } |
| 193 | |
evan@chromium.org | 54b7210 | 2009-07-22 09:35:18 +0900 | [diff] [blame] | 194 | } // namespace base |