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
3 // found in the LICENSE file.
5 #include "base/sys_info.h"
7 #include "base/basictypes.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/lazy_instance.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_piece.h"
13 #include "base/strings/string_tokenizer.h"
14 #include "base/threading/thread_restrictions.h"
18 static const char* kLinuxStandardBaseVersionKeys
[] = {
19 "CHROMEOS_RELEASE_VERSION",
25 const char kLinuxStandardBaseReleaseFile
[] = "/etc/lsb-release";
27 struct ChromeOSVersionNumbers
{
28 ChromeOSVersionNumbers()
41 static LazyInstance
<ChromeOSVersionNumbers
>
42 g_chrome_os_version_numbers
= LAZY_INSTANCE_INITIALIZER
;
45 void SysInfo::OperatingSystemVersionNumbers(int32
* major_version
,
47 int32
* bugfix_version
) {
48 if (!g_chrome_os_version_numbers
.Get().parsed
) {
49 // The other implementations of SysInfo don't block on the disk.
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
;
55 FilePath
path(kLinuxStandardBaseReleaseFile
);
57 if (file_util::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
));
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
;
71 std::string
SysInfo::GetLinuxStandardBaseVersionKey() {
72 return std::string(kLinuxStandardBaseVersionKeys
[0]);
76 void SysInfo::ParseLsbRelease(const std::string
& lsb_release
,
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
) {
87 if (std::string::npos
== version_key_index
) {
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
) {
98 StringToInt(StringPiece(tokenizer
.token_begin(),
99 tokenizer
.token_end()),
101 *minor_version
= *bugfix_version
= 0;
103 StringToInt(StringPiece(tokenizer
.token_begin(),
104 tokenizer
.token_end()),
107 StringToInt(StringPiece(tokenizer
.token_begin(),
108 tokenizer
.token_end()),
115 FilePath
SysInfo::GetLsbReleaseFilePath() {
116 return FilePath(kLinuxStandardBaseReleaseFile
);