OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/lazy_instance.h" |
10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
11 #include "base/string_tokenizer.h" | 12 #include "base/string_tokenizer.h" |
12 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
13 | 14 |
| 15 #include <execinfo.h> |
| 16 |
14 namespace base { | 17 namespace base { |
15 | 18 |
16 #if defined(GOOGLE_CHROME_BUILD) | 19 static const char* kLinuxStandardBaseVersionKeys[] = { |
17 static const char kLinuxStandardBaseVersionKey[] = "GOOGLE_RELEASE"; | 20 "CHROMEOS_RELEASE_VERSION", |
18 #else | 21 "GOOGLE_RELEASE", |
19 static const char kLinuxStandardBaseVersionKey[] = "DISTRIB_RELEASE"; | 22 "DISTRIB_RELEASE", |
20 #endif | 23 NULL |
| 24 }; |
21 | 25 |
22 const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release"; | 26 const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release"; |
23 | 27 |
| 28 struct ChromeOSVersionNumbers { |
| 29 ChromeOSVersionNumbers() |
| 30 : major_version(0), |
| 31 minor_version(0), |
| 32 bugfix_version(0), |
| 33 parsed(false) { |
| 34 } |
| 35 |
| 36 int32 major_version; |
| 37 int32 minor_version; |
| 38 int32 bugfix_version; |
| 39 bool parsed; |
| 40 }; |
| 41 |
| 42 static base::LazyInstance<ChromeOSVersionNumbers> |
| 43 g_chrome_os_version_numbers(base::LINKER_INITIALIZED); |
| 44 |
24 // static | 45 // static |
25 void SysInfo::OperatingSystemVersionNumbers(int32 *major_version, | 46 void SysInfo::OperatingSystemVersionNumbers(int32 *major_version, |
26 int32 *minor_version, | 47 int32 *minor_version, |
27 int32 *bugfix_version) { | 48 int32 *bugfix_version) { |
28 // The other implementations of SysInfo don't block on the disk. | 49 if (!g_chrome_os_version_numbers.Get().parsed) { |
29 // See http://code.google.com/p/chromium/issues/detail?id=60394 | 50 // The other implementations of SysInfo don't block on the disk. |
30 // Perhaps the caller ought to cache this? | 51 // See http://code.google.com/p/chromium/issues/detail?id=60394 |
31 // Temporary allowing while we work the bug out. | 52 // Perhaps the caller ought to cache this? |
32 base::ThreadRestrictions::ScopedAllowIO allow_io; | 53 // Temporary allowing while we work the bug out. |
| 54 base::ThreadRestrictions::ScopedAllowIO allow_io; |
33 | 55 |
34 // TODO(cmasone): If this gets called a lot, it may kill performance. | 56 FilePath path(kLinuxStandardBaseReleaseFile); |
35 // consider using static variables to cache these values? | 57 std::string contents; |
36 FilePath path(kLinuxStandardBaseReleaseFile); | 58 if (file_util::ReadFileToString(path, &contents)) { |
37 std::string contents; | 59 g_chrome_os_version_numbers.Get().parsed = true; |
38 if (file_util::ReadFileToString(path, &contents)) { | 60 ParseLsbRelease(contents, |
39 ParseLsbRelease(contents, major_version, minor_version, bugfix_version); | 61 &(g_chrome_os_version_numbers.Get().major_version), |
| 62 &(g_chrome_os_version_numbers.Get().minor_version), |
| 63 &(g_chrome_os_version_numbers.Get().bugfix_version)); |
| 64 } |
40 } | 65 } |
| 66 *major_version = g_chrome_os_version_numbers.Get().major_version; |
| 67 *minor_version = g_chrome_os_version_numbers.Get().minor_version; |
| 68 *bugfix_version = g_chrome_os_version_numbers.Get().bugfix_version; |
41 } | 69 } |
42 | 70 |
43 // static | 71 // static |
44 std::string SysInfo::GetLinuxStandardBaseVersionKey() { | 72 std::string SysInfo::GetLinuxStandardBaseVersionKey() { |
45 return std::string(kLinuxStandardBaseVersionKey); | 73 return std::string(kLinuxStandardBaseVersionKeys[0]); |
46 } | 74 } |
47 | 75 |
48 // static | 76 // static |
49 void SysInfo::ParseLsbRelease(const std::string& lsb_release, | 77 void SysInfo::ParseLsbRelease(const std::string& lsb_release, |
50 int32 *major_version, | 78 int32 *major_version, |
51 int32 *minor_version, | 79 int32 *minor_version, |
52 int32 *bugfix_version) { | 80 int32 *bugfix_version) { |
53 size_t version_key_index = lsb_release.find(kLinuxStandardBaseVersionKey); | 81 size_t version_key_index = std::string::npos; |
| 82 for (int i = 0; kLinuxStandardBaseVersionKeys[i] != NULL; ++i) { |
| 83 version_key_index = lsb_release.find(kLinuxStandardBaseVersionKeys[i]); |
| 84 if (std::string::npos != version_key_index) { |
| 85 break; |
| 86 } |
| 87 } |
54 if (std::string::npos == version_key_index) { | 88 if (std::string::npos == version_key_index) { |
55 return; | 89 return; |
56 } | 90 } |
| 91 |
57 size_t start_index = lsb_release.find_first_of('=', version_key_index); | 92 size_t start_index = lsb_release.find_first_of('=', version_key_index); |
58 start_index++; // Move past '='. | 93 start_index++; // Move past '='. |
59 size_t length = lsb_release.find_first_of('\n', start_index) - start_index; | 94 size_t length = lsb_release.find_first_of('\n', start_index) - start_index; |
60 std::string version = lsb_release.substr(start_index, length); | 95 std::string version = lsb_release.substr(start_index, length); |
61 StringTokenizer tokenizer(version, "."); | 96 StringTokenizer tokenizer(version, "."); |
62 for (int i = 0; i < 3 && tokenizer.GetNext(); i++) { | 97 for (int i = 0; i < 3 && tokenizer.GetNext(); i++) { |
63 if (0 == i) { | 98 if (0 == i) { |
64 StringToInt(tokenizer.token_begin(), | 99 StringToInt(tokenizer.token_begin(), |
65 tokenizer.token_end(), | 100 tokenizer.token_end(), |
66 major_version); | 101 major_version); |
67 *minor_version = *bugfix_version = 0; | 102 *minor_version = *bugfix_version = 0; |
68 } else if (1 == i) { | 103 } else if (1 == i) { |
69 StringToInt(tokenizer.token_begin(), | 104 StringToInt(tokenizer.token_begin(), |
70 tokenizer.token_end(), | 105 tokenizer.token_end(), |
71 minor_version); | 106 minor_version); |
72 } else { // 2 == i | 107 } else { // 2 == i |
73 StringToInt(tokenizer.token_begin(), | 108 StringToInt(tokenizer.token_begin(), |
74 tokenizer.token_end(), | 109 tokenizer.token_end(), |
75 bugfix_version); | 110 bugfix_version); |
76 } | 111 } |
77 } | 112 } |
78 } | 113 } |
79 | 114 |
80 } // namespace base | 115 } // namespace base |
OLD | NEW |