[email protected] | 61b8ad7 | 2009-07-22 00:35:18 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/sys_info.h" |
| 6 | |
| 7 | #include "base/basictypes.h" |
| 8 | #include "base/file_path.h" |
| 9 | #include "base/file_util.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 10 | #include "base/string_number_conversions.h" |
[email protected] | 61b8ad7 | 2009-07-22 00:35:18 | [diff] [blame] | 11 | #include "base/string_tokenizer.h" |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame^] | 12 | #include "base/thread_restrictions.h" |
[email protected] | 61b8ad7 | 2009-07-22 00:35:18 | [diff] [blame] | 13 | |
| 14 | namespace base { |
| 15 | |
[email protected] | 3214ef1 | 2009-10-19 16:40:57 | [diff] [blame] | 16 | #if defined(GOOGLE_CHROME_BUILD) |
[email protected] | 61b8ad7 | 2009-07-22 00:35:18 | [diff] [blame] | 17 | static const char kLinuxStandardBaseVersionKey[] = "GOOGLE_RELEASE"; |
| 18 | #else |
| 19 | static const char kLinuxStandardBaseVersionKey[] = "DISTRIB_RELEASE"; |
| 20 | #endif |
| 21 | |
| 22 | const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release"; |
| 23 | |
| 24 | // static |
| 25 | void SysInfo::OperatingSystemVersionNumbers(int32 *major_version, |
| 26 | int32 *minor_version, |
| 27 | int32 *bugfix_version) { |
[email protected] | ba74b0d2 | 2010-10-23 05:19:20 | [diff] [blame^] | 28 | // The other implementations of SysInfo don't block on the disk. |
| 29 | // See http://code.google.com/p/chromium/issues/detail?id=60394 |
| 30 | // Perhaps the caller ought to cache this? |
| 31 | // Temporary allowing while we work the bug out. |
| 32 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 33 | |
[email protected] | 61b8ad7 | 2009-07-22 00:35:18 | [diff] [blame] | 34 | // TODO(cmasone): If this gets called a lot, it may kill performance. |
| 35 | // consider using static variables to cache these values? |
| 36 | FilePath path(kLinuxStandardBaseReleaseFile); |
| 37 | std::string contents; |
| 38 | if (file_util::ReadFileToString(path, &contents)) { |
| 39 | ParseLsbRelease(contents, major_version, minor_version, bugfix_version); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // static |
| 44 | std::string SysInfo::GetLinuxStandardBaseVersionKey() { |
| 45 | return std::string(kLinuxStandardBaseVersionKey); |
| 46 | } |
| 47 | |
| 48 | // static |
| 49 | void SysInfo::ParseLsbRelease(const std::string& lsb_release, |
| 50 | int32 *major_version, |
| 51 | int32 *minor_version, |
| 52 | int32 *bugfix_version) { |
[email protected] | 323b877 | 2009-12-09 01:55:04 | [diff] [blame] | 53 | size_t version_key_index = lsb_release.find(kLinuxStandardBaseVersionKey); |
[email protected] | 61b8ad7 | 2009-07-22 00:35:18 | [diff] [blame] | 54 | if (std::string::npos == version_key_index) { |
| 55 | return; |
| 56 | } |
| 57 | size_t start_index = lsb_release.find_first_of('=', version_key_index); |
| 58 | start_index++; // Move past '='. |
| 59 | size_t length = lsb_release.find_first_of('\n', start_index) - start_index; |
| 60 | std::string version = lsb_release.substr(start_index, length); |
| 61 | StringTokenizer tokenizer(version, "."); |
| 62 | for (int i = 0; i < 3 && tokenizer.GetNext(); i++) { |
| 63 | if (0 == i) { |
[email protected] | c2750c6 | 2010-10-22 16:05:47 | [diff] [blame] | 64 | StringToInt(tokenizer.token_begin(), |
| 65 | tokenizer.token_end(), |
| 66 | major_version); |
[email protected] | 61b8ad7 | 2009-07-22 00:35:18 | [diff] [blame] | 67 | *minor_version = *bugfix_version = 0; |
| 68 | } else if (1 == i) { |
[email protected] | c2750c6 | 2010-10-22 16:05:47 | [diff] [blame] | 69 | StringToInt(tokenizer.token_begin(), |
| 70 | tokenizer.token_end(), |
| 71 | minor_version); |
[email protected] | 61b8ad7 | 2009-07-22 00:35:18 | [diff] [blame] | 72 | } else { // 2 == i |
[email protected] | c2750c6 | 2010-10-22 16:05:47 | [diff] [blame] | 73 | StringToInt(tokenizer.token_begin(), |
| 74 | tokenizer.token_end(), |
| 75 | bugfix_version); |
[email protected] | 61b8ad7 | 2009-07-22 00:35:18 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | } // namespace base |