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

Side by Side Diff: base/sys_info_chromeos.cc

Issue 23904025: Move IsRunningOnChromeOS to SysInfo (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 2 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
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/environment.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 10 matching lines...) Expand all
21 namespace { 21 namespace {
22 22
23 const char* kLinuxStandardBaseVersionKeys[] = { 23 const char* kLinuxStandardBaseVersionKeys[] = {
24 "CHROMEOS_RELEASE_VERSION", 24 "CHROMEOS_RELEASE_VERSION",
25 "GOOGLE_RELEASE", 25 "GOOGLE_RELEASE",
26 "DISTRIB_RELEASE", 26 "DISTRIB_RELEASE",
27 }; 27 };
28 const size_t kLinuxStandardBaseVersionKeysLength = 28 const size_t kLinuxStandardBaseVersionKeysLength =
29 arraysize(kLinuxStandardBaseVersionKeys); 29 arraysize(kLinuxStandardBaseVersionKeys);
30 30
31 static const char* kChromeOsReleaseNameKey = "CHROMEOS_RELEASE_NAME";
satorux1 2013/09/24 04:09:01 nit: 'static' is unnecessary. const char kChromeO
stevenjb 2013/09/26 22:07:38 Oops, thanks for catching. Done.
32
33 const char* kChromeOsReleaseNames[] = {
34 "Chrome OS",
35 "Chromium OS",
36 };
37 const size_t kChromeOsReleaseNamesLength = arraysize(kChromeOsReleaseNames);
38
31 const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release"; 39 const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
32 40
33 class ChromeOSVersionInfo { 41 class ChromeOSVersionInfo {
34 public: 42 public:
35 ChromeOSVersionInfo() { 43 ChromeOSVersionInfo() {
36 Parse(); 44 Parse();
37 } 45 }
38 46
39 void Parse() { 47 void Parse() {
40 lsb_release_map_.clear(); 48 lsb_release_map_.clear();
41 major_version_ = 0; 49 major_version_ = 0;
42 minor_version_ = 0; 50 minor_version_ = 0;
43 bugfix_version_ = 0; 51 bugfix_version_ = 0;
52 is_running_on_chromeos_ = false;
44 53
45 std::string lsb_release, lsb_release_time_str; 54 std::string lsb_release, lsb_release_time_str;
46 scoped_ptr<base::Environment> env(base::Environment::Create()); 55 scoped_ptr<base::Environment> env(base::Environment::Create());
47 bool parsed_from_env = 56 bool parsed_from_env =
48 env->GetVar("LSB_RELEASE", &lsb_release) && 57 env->GetVar("LSB_RELEASE", &lsb_release) &&
49 env->GetVar("LSB_RELEASE_TIME", &lsb_release_time_str); 58 env->GetVar("LSB_RELEASE_TIME", &lsb_release_time_str);
50 if (parsed_from_env) { 59 if (parsed_from_env) {
51 double us = 0; 60 double us = 0;
52 StringToDouble(lsb_release_time_str, &us); 61 StringToDouble(lsb_release_time_str, &us);
53 lsb_release_time_ = base::Time::FromDoubleT(us); 62 lsb_release_time_ = base::Time::FromDoubleT(us);
(...skipping 26 matching lines...) Expand all
80 int32* bugfix_version) { 89 int32* bugfix_version) {
81 *major_version = major_version_; 90 *major_version = major_version_;
82 *minor_version = minor_version_; 91 *minor_version = minor_version_;
83 *bugfix_version = bugfix_version_; 92 *bugfix_version = bugfix_version_;
84 } 93 }
85 94
86 const base::Time& lsb_release_time() const { return lsb_release_time_; } 95 const base::Time& lsb_release_time() const { return lsb_release_time_; }
87 const SysInfo::LsbReleaseMap& lsb_release_map() const { 96 const SysInfo::LsbReleaseMap& lsb_release_map() const {
88 return lsb_release_map_; 97 return lsb_release_map_;
89 } 98 }
99 bool is_running_on_chromeos() const { return is_running_on_chromeos_; }
90 100
91 private: 101 private:
92 void ParseLsbRelease(const std::string& lsb_release) { 102 void ParseLsbRelease(const std::string& lsb_release) {
93 // Parse lsb_release key pairs. 103 // Parse lsb_release key pairs.
94 std::vector<std::pair<std::string, std::string> > pairs; 104 std::vector<std::pair<std::string, std::string> > pairs;
95 base::SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs); 105 base::SplitStringIntoKeyValuePairs(lsb_release, '=', '\n', &pairs);
96 for (size_t i = 0; i < pairs.size(); ++i) { 106 for (size_t i = 0; i < pairs.size(); ++i) {
97 std::string key, value; 107 std::string key, value;
98 TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key); 108 TrimWhitespaceASCII(pairs[i].first, TRIM_ALL, &key);
99 TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value); 109 TrimWhitespaceASCII(pairs[i].second, TRIM_ALL, &value);
(...skipping 14 matching lines...) Expand all
114 &major_version_); 124 &major_version_);
115 } 125 }
116 if (tokenizer.GetNext()) { 126 if (tokenizer.GetNext()) {
117 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()), 127 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
118 &minor_version_); 128 &minor_version_);
119 } 129 }
120 if (tokenizer.GetNext()) { 130 if (tokenizer.GetNext()) {
121 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()), 131 StringToInt(StringPiece(tokenizer.token_begin(), tokenizer.token_end()),
122 &bugfix_version_); 132 &bugfix_version_);
123 } 133 }
134
135 // Check release name for Chrome OS.
136 std::string release_name;
137 if (GetLsbReleaseValue(kChromeOsReleaseNameKey, &release_name)) {
138 for (size_t i = 0; i < kChromeOsReleaseNamesLength; ++i) {
satorux1 2013/09/24 04:09:01 remove kChromeOsReleaseNamesLength? arraysize(kCh
stevenjb 2013/09/26 22:07:38 Done.
139 if (release_name == kChromeOsReleaseNames[i]) {
140 is_running_on_chromeos_ = true;
141 break;
142 }
143 }
144 }
124 } 145 }
125 146
126 base::Time lsb_release_time_; 147 base::Time lsb_release_time_;
127 SysInfo::LsbReleaseMap lsb_release_map_; 148 SysInfo::LsbReleaseMap lsb_release_map_;
128 int32 major_version_; 149 int32 major_version_;
129 int32 minor_version_; 150 int32 minor_version_;
130 int32 bugfix_version_; 151 int32 bugfix_version_;
152 bool is_running_on_chromeos_;
131 }; 153 };
132 154
133 static LazyInstance<ChromeOSVersionInfo> 155 static LazyInstance<ChromeOSVersionInfo>
134 g_chrome_os_version_info = LAZY_INSTANCE_INITIALIZER; 156 g_chrome_os_version_info = LAZY_INSTANCE_INITIALIZER;
135 157
136 ChromeOSVersionInfo& GetChromeOSVersionInfo() { 158 ChromeOSVersionInfo& GetChromeOSVersionInfo() {
137 return g_chrome_os_version_info.Get(); 159 return g_chrome_os_version_info.Get();
138 } 160 }
139 161
140 } // namespace 162 } // namespace
(...skipping 24 matching lines...) Expand all
165 board = "unknown"; 187 board = "unknown";
166 return board; 188 return board;
167 } 189 }
168 190
169 // static 191 // static
170 base::Time SysInfo::GetLsbReleaseTime() { 192 base::Time SysInfo::GetLsbReleaseTime() {
171 return GetChromeOSVersionInfo().lsb_release_time(); 193 return GetChromeOSVersionInfo().lsb_release_time();
172 } 194 }
173 195
174 // static 196 // static
197 bool SysInfo::IsRunningOnChromeOS() {
198 return GetChromeOSVersionInfo().is_running_on_chromeos();
199 }
200
201 // static
175 void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release, 202 void SysInfo::SetChromeOSVersionInfoForTest(const std::string& lsb_release,
176 double lsb_release_time) { 203 double lsb_release_time) {
177 scoped_ptr<base::Environment> env(base::Environment::Create()); 204 scoped_ptr<base::Environment> env(base::Environment::Create());
178 env->SetVar("LSB_RELEASE", lsb_release); 205 env->SetVar("LSB_RELEASE", lsb_release);
179 // LSB_RELEASE_TIME must be set to read LSB_RELEASE from the env. 206 // LSB_RELEASE_TIME must be set to read LSB_RELEASE from the env.
180 env->SetVar("LSB_RELEASE_TIME", base::DoubleToString(lsb_release_time)); 207 env->SetVar("LSB_RELEASE_TIME", base::DoubleToString(lsb_release_time));
181 g_chrome_os_version_info.Get().Parse(); 208 g_chrome_os_version_info.Get().Parse();
182 } 209 }
183 210
184 } // namespace base 211 } // namespace base
OLDNEW
« base/sys_info.h ('K') | « 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