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_path.h"
9 #include "base/file_util.h"
10 #include "base/lazy_instance.h"
11 #include "base/string_number_conversions.h"
12 #include "base/string_piece.h"
13 #include "base/string_tokenizer.h"
14 #include "base/threading/thread_restrictions.h"
20 static const char* kLinuxStandardBaseVersionKeys
[] = {
21 "CHROMEOS_RELEASE_VERSION",
27 const char kLinuxStandardBaseReleaseFile
[] = "/etc/lsb-release";
29 struct ChromeOSVersionNumbers
{
30 ChromeOSVersionNumbers()
43 static base::LazyInstance
<ChromeOSVersionNumbers
>
44 g_chrome_os_version_numbers
= LAZY_INSTANCE_INITIALIZER
;
47 void SysInfo::OperatingSystemVersionNumbers(int32
* major_version
,
49 int32
* bugfix_version
) {
50 if (!g_chrome_os_version_numbers
.Get().parsed
) {
51 // The other implementations of SysInfo don't block on the disk.
52 // See http://code.google.com/p/chromium/issues/detail?id=60394
53 // Perhaps the caller ought to cache this?
54 // Temporary allowing while we work the bug out.
55 base::ThreadRestrictions::ScopedAllowIO allow_io
;
57 FilePath
path(kLinuxStandardBaseReleaseFile
);
59 if (file_util::ReadFileToString(path
, &contents
)) {
60 g_chrome_os_version_numbers
.Get().parsed
= true;
61 ParseLsbRelease(contents
,
62 &(g_chrome_os_version_numbers
.Get().major_version
),
63 &(g_chrome_os_version_numbers
.Get().minor_version
),
64 &(g_chrome_os_version_numbers
.Get().bugfix_version
));
67 *major_version
= g_chrome_os_version_numbers
.Get().major_version
;
68 *minor_version
= g_chrome_os_version_numbers
.Get().minor_version
;
69 *bugfix_version
= g_chrome_os_version_numbers
.Get().bugfix_version
;
73 std::string
SysInfo::GetLinuxStandardBaseVersionKey() {
74 return std::string(kLinuxStandardBaseVersionKeys
[0]);
78 void SysInfo::ParseLsbRelease(const std::string
& lsb_release
,
81 int32
* bugfix_version
) {
82 size_t version_key_index
= std::string::npos
;
83 for (int i
= 0; kLinuxStandardBaseVersionKeys
[i
] != NULL
; ++i
) {
84 version_key_index
= lsb_release
.find(kLinuxStandardBaseVersionKeys
[i
]);
85 if (std::string::npos
!= version_key_index
) {
89 if (std::string::npos
== version_key_index
) {
93 size_t start_index
= lsb_release
.find_first_of('=', version_key_index
);
94 start_index
++; // Move past '='.
95 size_t length
= lsb_release
.find_first_of('\n', start_index
) - start_index
;
96 std::string version
= lsb_release
.substr(start_index
, length
);
97 StringTokenizer
tokenizer(version
, ".");
98 for (int i
= 0; i
< 3 && tokenizer
.GetNext(); ++i
) {
100 StringToInt(StringPiece(tokenizer
.token_begin(),
101 tokenizer
.token_end()),
103 *minor_version
= *bugfix_version
= 0;
105 StringToInt(StringPiece(tokenizer
.token_begin(),
106 tokenizer
.token_end()),
109 StringToInt(StringPiece(tokenizer
.token_begin(),
110 tokenizer
.token_end()),
117 FilePath
SysInfo::GetLsbReleaseFilePath() {
118 return FilePath(kLinuxStandardBaseReleaseFile
);