Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: base/sys_info_chromeos.cc

Issue 23588009: Parse /etc/lsb-release only once on ChromeOS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Feedback Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/sys_info.h ('k') | base/sys_info_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 kLsbReleaseSourceEnv[] = "env";
31 bugfix_version(0), 37 const char kLsbReleaseSourceFile[] = "file";
32 parsed(false) { 38
39 class ChromeOSVersionInfo {
40 public:
41 ChromeOSVersionInfo() {
42 Parse();
33 } 43 }
34 44
35 int32 major_version; 45 void Parse() {
36 int32 minor_version; 46 lsb_release_map_.clear();
37 int32 bugfix_version; 47 major_version_ = 0;
38 bool parsed; 48 minor_version_ = 0;
49 bugfix_version_ = 0;
50
51 std::string lsb_release, lsb_release_time_str;
52 scoped_ptr<base::Environment> env(base::Environment::Create());
53 bool parsed_from_env =
54 env->GetVar(kLsbReleaseKey, &lsb_release) &&
55 env->GetVar(kLsbReleaseTimeKey, &lsb_release_time_str);
56 if (parsed_from_env) {
57 double us = 0;
58 if (StringToDouble(lsb_release_time_str, &us))
59 lsb_release_time_ = base::Time::FromDoubleT(us);
60 } else {
61 // If the LSB_RELEASE and LSB_RELEASE_TIME environment variables are not
62 // set, fall back to a blocking read of the lsb_release file. This should
63 // only happen in non Chrome OS environments.
64 ThreadRestrictions::ScopedAllowIO allow_io;
65 FilePath path(kLinuxStandardBaseReleaseFile);
66 ReadFileToString(path, &lsb_release);
67 base::PlatformFileInfo fileinfo;
68 if (file_util::GetFileInfo(path, &fileinfo))
69 lsb_release_time_ = fileinfo.creation_time;
70 }
71 ParseLsbRelease(lsb_release);
72 // For debugging:
73 lsb_release_map_["lsb-source"] =
Daniel Erat 2013/09/24 18:09:09 nit: for consistency, the key string probably be i
stevenjb 2013/09/24 18:31:33 Good point. Done. (And yes, we just dump the whole
74 parsed_from_env ? kLsbReleaseSourceEnv : kLsbReleaseSourceFile;
75 }
76
77 bool GetLsbReleaseValue(const std::string& key, std::string* value) {
78 SysInfo::LsbReleaseMap::const_iterator iter = lsb_release_map_.find(key);
79 if (iter == lsb_release_map_.end())
80 return false;
81 *value = iter->second;
82 return true;
83 }
84
85 void GetVersionNumbers(int32* major_version,
86 int32* minor_version,
87 int32* bugfix_version) {
88 *major_version = major_version_;
89 *minor_version = minor_version_;
90 *bugfix_version = bugfix_version_;
91 }
92
93 const base::Time& lsb_release_time() const { return lsb_release_time_; }
94 const SysInfo::LsbReleaseMap& lsb_release_map() const {
95 return lsb_release_map_;
96 }
97
98 private:
99 void ParseLsbRelease(const std::string& lsb_release) {
100 // Parse and cache lsb_release key pairs. There should only be a handful
101 // of entries so the overhead for this will be small, and it can be
102 // useful for debugging.
103 std::vector<std::pair<std::string, std::string> > pairs;
104 base::SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs);
105 for (size_t i = 0; i < pairs.size(); ++i) {
106 std::string key, value;
107 TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key);
108 TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value);
109 if (key.empty())
110 continue;
111 lsb_release_map_[key] = value;
112 }
113 // Parse the version from the first matching recognized version key.
114 std::string version;
115 for (size_t i = 0; i < kLinuxStandardBaseVersionKeysLength; ++i) {
116 std::string key = kLinuxStandardBaseVersionKeys[i];
117 if (GetLsbReleaseValue(key, &version) && !version.empty())
118 break;
119 }
120 StringTokenizer tokenizer(version, ".");
121 if (tokenizer.GetNext()) {
122 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
123 &major_version_);
124 }
125 if (tokenizer.GetNext()) {
126 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
127 &minor_version_);
128 }
129 if (tokenizer.GetNext()) {
130 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
131 &bugfix_version_);
132 }
133 }
134
135 base::Time lsb_release_time_;
136 SysInfo::LsbReleaseMap lsb_release_map_;
137 int32 major_version_;
138 int32 minor_version_;
139 int32 bugfix_version_;
39 }; 140 };
40 141
41 static LazyInstance<ChromeOSVersionNumbers> 142 static LazyInstance<ChromeOSVersionInfo>
42 g_chrome_os_version_numbers = LAZY_INSTANCE_INITIALIZER; 143 g_chrome_os_version_info = LAZY_INSTANCE_INITIALIZER;
144
145 ChromeOSVersionInfo& GetChromeOSVersionInfo() {
146 return g_chrome_os_version_info.Get();
147 }
148
149 } // namespace
43 150
44 // static 151 // static
45 void SysInfo::OperatingSystemVersionNumbers(int32* major_version, 152 void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
46 int32* minor_version, 153 int32* minor_version,
47 int32* bugfix_version) { 154 int32* bugfix_version) {
48 if (!g_chrome_os_version_numbers.Get().parsed) { 155 return GetChromeOSVersionInfo().GetVersionNumbers(
49 // The other implementations of SysInfo don't block on the disk. 156 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 } 157 }
69 158
70 // static 159 // static
71 std::string SysInfo::GetLinuxStandardBaseVersionKey() { 160 const SysInfo::LsbReleaseMap& SysInfo::GetLsbReleaseMap() {
72 return std::string(kLinuxStandardBaseVersionKeys[0]); 161 return GetChromeOSVersionInfo().lsb_release_map();
73 } 162 }
74 163
75 // static 164 // static
76 void SysInfo::ParseLsbRelease(const std::string& lsb_release, 165 bool SysInfo::GetLsbReleaseValue(const std::string& key, std::string* value) {
77 int32* major_version, 166 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 } 167 }
113 168
114 // static 169 // static
115 FilePath SysInfo::GetLsbReleaseFilePath() { 170 std::string SysInfo::GetLsbReleaseBoard() {
116 return FilePath(kLinuxStandardBaseReleaseFile); 171 const char kMachineInfoBoard[] = "CHROMEOS_RELEASE_BOARD";
172 std::string board;
173 if (!GetLsbReleaseValue(kMachineInfoBoard, &board))
174 board = "unknown";
175 return board;
176 }
177
178 // static
179 base::Time SysInfo::GetLsbReleaseTime() {
180 return GetChromeOSVersionInfo().lsb_release_time();
181 }
182
183 // static
184 void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release,
185 const Time& lsb_release_time) {
186 scoped_ptr<base::Environment> env(base::Environment::Create());
187 env->SetVar(kLsbReleaseKey, lsb_release);
188 env->SetVar(kLsbReleaseTimeKey,
189 base::DoubleToString(lsb_release_time.ToDoubleT()));
190 g_chrome_os_version_info.Get().Parse();
117 } 191 }
118 192
119 } // namespace base 193 } // namespace base
OLDNEW
« no previous file with comments | « base/sys_info.h ('k') | base/sys_info_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698