OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/sys_info.h" | 5 #include "base/sys_info.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/environment.h" |
8 #include "base/file_util.h" | 9 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
| 14 #include "base/strings/string_split.h" |
13 #include "base/strings/string_tokenizer.h" | 15 #include "base/strings/string_tokenizer.h" |
| 16 #include "base/strings/string_util.h" |
14 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
15 | 18 |
16 namespace base { | 19 namespace base { |
17 | 20 |
18 static const char* kLinuxStandardBaseVersionKeys[] = { | 21 namespace { |
| 22 |
| 23 const char* kLinuxStandardBaseVersionKeys[] = { |
19 "CHROMEOS_RELEASE_VERSION", | 24 "CHROMEOS_RELEASE_VERSION", |
20 "GOOGLE_RELEASE", | 25 "GOOGLE_RELEASE", |
21 "DISTRIB_RELEASE", | 26 "DISTRIB_RELEASE", |
22 NULL | |
23 }; | 27 }; |
| 28 const size_t kLinuxStandardBaseVersionKeysLength = |
| 29 arraysize(kLinuxStandardBaseVersionKeys); |
24 | 30 |
25 const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release"; | 31 const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release"; |
26 | 32 |
27 struct ChromeOSVersionNumbers { | 33 const char kLsbReleaseKey[] = "LSB_RELEASE"; |
28 ChromeOSVersionNumbers() | 34 const char kLsbReleaseTimeKey[] = "LSB_RELEASE_TIME"; // Seconds since epoch |
29 : major_version(0), | 35 |
30 minor_version(0), | 36 const char kLsbReleaseSourceKey[] = "lsb-release"; |
31 bugfix_version(0), | 37 const char kLsbReleaseSourceEnv[] = "env"; |
32 parsed(false) { | 38 const char kLsbReleaseSourceFile[] = "file"; |
| 39 |
| 40 class ChromeOSVersionInfo { |
| 41 public: |
| 42 ChromeOSVersionInfo() { |
| 43 Parse(); |
33 } | 44 } |
34 | 45 |
35 int32 major_version; | 46 void Parse() { |
36 int32 minor_version; | 47 lsb_release_map_.clear(); |
37 int32 bugfix_version; | 48 major_version_ = 0; |
38 bool parsed; | 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_; |
39 }; | 141 }; |
40 | 142 |
41 static LazyInstance<ChromeOSVersionNumbers> | 143 static LazyInstance<ChromeOSVersionInfo> |
42 g_chrome_os_version_numbers = LAZY_INSTANCE_INITIALIZER; | 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 |
43 | 151 |
44 // static | 152 // static |
45 void SysInfo::OperatingSystemVersionNumbers(int32* major_version, | 153 void SysInfo::OperatingSystemVersionNumbers(int32* major_version, |
46 int32* minor_version, | 154 int32* minor_version, |
47 int32* bugfix_version) { | 155 int32* bugfix_version) { |
48 if (!g_chrome_os_version_numbers.Get().parsed) { | 156 return GetChromeOSVersionInfo().GetVersionNumbers( |
49 // The other implementations of SysInfo don't block on the disk. | 157 major_version, minor_version, bugfix_version); |
50 // See http://code.google.com/p/chromium/issues/detail?id=60394 | |
51 // Perhaps the caller ought to cache this? | |
52 // Temporary allowing while we work the bug out. | |
53 ThreadRestrictions::ScopedAllowIO allow_io; | |
54 | |
55 FilePath path(kLinuxStandardBaseReleaseFile); | |
56 std::string contents; | |
57 if (ReadFileToString(path, &contents)) { | |
58 g_chrome_os_version_numbers.Get().parsed = true; | |
59 ParseLsbRelease(contents, | |
60 &(g_chrome_os_version_numbers.Get().major_version), | |
61 &(g_chrome_os_version_numbers.Get().minor_version), | |
62 &(g_chrome_os_version_numbers.Get().bugfix_version)); | |
63 } | |
64 } | |
65 *major_version = g_chrome_os_version_numbers.Get().major_version; | |
66 *minor_version = g_chrome_os_version_numbers.Get().minor_version; | |
67 *bugfix_version = g_chrome_os_version_numbers.Get().bugfix_version; | |
68 } | 158 } |
69 | 159 |
70 // static | 160 // static |
71 std::string SysInfo::GetLinuxStandardBaseVersionKey() { | 161 const SysInfo::LsbReleaseMap& SysInfo::GetLsbReleaseMap() { |
72 return std::string(kLinuxStandardBaseVersionKeys[0]); | 162 return GetChromeOSVersionInfo().lsb_release_map(); |
73 } | 163 } |
74 | 164 |
75 // static | 165 // static |
76 void SysInfo::ParseLsbRelease(const std::string& lsb_release, | 166 bool SysInfo::GetLsbReleaseValue(const std::string& key, std::string* value) { |
77 int32* major_version, | 167 return GetChromeOSVersionInfo().GetLsbReleaseValue(key, value); |
78 int32* minor_version, | |
79 int32* bugfix_version) { | |
80 size_t version_key_index = std::string::npos; | |
81 for (int i = 0; kLinuxStandardBaseVersionKeys[i] != NULL; ++i) { | |
82 version_key_index = lsb_release.find(kLinuxStandardBaseVersionKeys[i]); | |
83 if (std::string::npos != version_key_index) { | |
84 break; | |
85 } | |
86 } | |
87 if (std::string::npos == version_key_index) { | |
88 return; | |
89 } | |
90 | |
91 size_t start_index = lsb_release.find_first_of('=', version_key_index); | |
92 start_index++; // Move past '='. | |
93 size_t length = lsb_release.find_first_of('\n', start_index) - start_index; | |
94 std::string version = lsb_release.substr(start_index, length); | |
95 StringTokenizer tokenizer(version, "."); | |
96 for (int i = 0; i < 3 && tokenizer.GetNext(); ++i) { | |
97 if (0 == i) { | |
98 StringToInt(StringPiece(tokenizer.token_begin(), | |
99 tokenizer.token_end()), | |
100 major_version); | |
101 *minor_version = *bugfix_version = 0; | |
102 } else if (1 == i) { | |
103 StringToInt(StringPiece(tokenizer.token_begin(), | |
104 tokenizer.token_end()), | |
105 minor_version); | |
106 } else { // 2 == i | |
107 StringToInt(StringPiece(tokenizer.token_begin(), | |
108 tokenizer.token_end()), | |
109 bugfix_version); | |
110 } | |
111 } | |
112 } | 168 } |
113 | 169 |
114 // static | 170 // static |
115 FilePath SysInfo::GetLsbReleaseFilePath() { | 171 std::string SysInfo::GetLsbReleaseBoard() { |
116 return FilePath(kLinuxStandardBaseReleaseFile); | 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(); |
117 } | 192 } |
118 | 193 |
119 } // namespace base | 194 } // namespace base |
OLD | NEW |