sys_info_chromeos.cc revision 513209b27ff55e2841eac0e4120199c23acce758
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"
10#include "base/string_number_conversions.h"
11#include "base/string_tokenizer.h"
12#include "base/thread_restrictions.h"
13
14namespace base {
15
16#if defined(GOOGLE_CHROME_BUILD)
17static const char kLinuxStandardBaseVersionKey[] = "GOOGLE_RELEASE";
18#else
19static const char kLinuxStandardBaseVersionKey[] = "DISTRIB_RELEASE";
20#endif
21
22const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";
23
24// static
25void SysInfo::OperatingSystemVersionNumbers(int32 *major_version,
26                                            int32 *minor_version,
27                                            int32 *bugfix_version) {
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
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
44std::string SysInfo::GetLinuxStandardBaseVersionKey() {
45  return std::string(kLinuxStandardBaseVersionKey);
46}
47
48// static
49void SysInfo::ParseLsbRelease(const std::string& lsb_release,
50                              int32 *major_version,
51                              int32 *minor_version,
52                              int32 *bugfix_version) {
53  size_t version_key_index = lsb_release.find(kLinuxStandardBaseVersionKey);
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) {
64      StringToInt(tokenizer.token_begin(),
65                  tokenizer.token_end(),
66                  major_version);
67      *minor_version = *bugfix_version = 0;
68    } else if (1 == i) {
69      StringToInt(tokenizer.token_begin(),
70                  tokenizer.token_end(),
71                  minor_version);
72    } else {  // 2 == i
73      StringToInt(tokenizer.token_begin(),
74                  tokenizer.token_end(),
75                  bugfix_version);
76    }
77  }
78}
79
80}  // namespace base
81