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. 4 5 #include "base/sys_info.h" 6 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" 15 16 namespace base { 17 18 static const char* kLinuxStandardBaseVersionKeys[] = { 19 "CHROMEOS_RELEASE_VERSION", 20 "GOOGLE_RELEASE", 21 "DISTRIB_RELEASE", 22 NULL 23 }; 24 25 const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release"; 26 27 struct ChromeOSVersionNumbers { 28 ChromeOSVersionNumbers() 29 : major_version(0), 30 minor_version(0), 31 bugfix_version(0), 32 parsed(false) { 33 } 34 35 int32 major_version; 36 int32 minor_version; 37 int32 bugfix_version; 38 bool parsed; 39 }; 40 41 static LazyInstance<ChromeOSVersionNumbers> 42 g_chrome_os_version_numbers = LAZY_INSTANCE_INITIALIZER; 43 44 // static 45 void SysInfo::OperatingSystemVersionNumbers(int32* major_version, 46 int32* minor_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; 54 55 FilePath path(kLinuxStandardBaseReleaseFile); 56 std::string contents; 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)); 63 } 64 } 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; 68 } 69 70 // static 71 std::string SysInfo::GetLinuxStandardBaseVersionKey() { 72 return std::string(kLinuxStandardBaseVersionKeys[0]); 73 } 74 75 // static 76 void SysInfo::ParseLsbRelease(const std::string& lsb_release, 77 int32* major_version, 78 int32* minor_version, 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) { 84 break; 85 } 86 } 87 if (std::string::npos == version_key_index) { 88 return; 89 } 90 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) { 97 if (0 == i) { 98 StringToInt(StringPiece(tokenizer.token_begin(), 99 tokenizer.token_end()), 100 major_version); 101 *minor_version = *bugfix_version = 0; 102 } else if (1 == i) { 103 StringToInt(StringPiece(tokenizer.token_begin(), 104 tokenizer.token_end()), 105 minor_version); 106 } else { // 2 == i 107 StringToInt(StringPiece(tokenizer.token_begin(), 108 tokenizer.token_end()), 109 bugfix_version); 110 } 111 } 112 } 113 114 // static 115 FilePath SysInfo::GetLsbReleaseFilePath() { 116 return FilePath(kLinuxStandardBaseReleaseFile); 117 } 118 119 } // namespace base 120