summaryrefslogtreecommitdiffstats
path: root/base/sys_info_chromeos.cc
blob: 583438932de722b620c1ded3417c47c8a7ba0e0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/sys_info.h"

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/string_number_conversions.h"
#include "base/string_tokenizer.h"
#include "base/threading/thread_restrictions.h"

namespace base {

#if defined(GOOGLE_CHROME_BUILD)
static const char kLinuxStandardBaseVersionKey[] = "GOOGLE_RELEASE";
#else
static const char kLinuxStandardBaseVersionKey[] = "DISTRIB_RELEASE";
#endif

const char kLinuxStandardBaseReleaseFile[] = "/etc/lsb-release";

// static
void SysInfo::OperatingSystemVersionNumbers(int32 *major_version,
                                            int32 *minor_version,
                                            int32 *bugfix_version) {
  // The other implementations of SysInfo don't block on the disk.
  // See http://code.google.com/p/chromium/issues/detail?id=60394
  // Perhaps the caller ought to cache this?
  // Temporary allowing while we work the bug out.
  base::ThreadRestrictions::ScopedAllowIO allow_io;

  // TODO(cmasone): If this gets called a lot, it may kill performance.
  // consider using static variables to cache these values?
  FilePath path(kLinuxStandardBaseReleaseFile);
  std::string contents;
  if (file_util::ReadFileToString(path, &contents)) {
    ParseLsbRelease(contents, major_version, minor_version, bugfix_version);
  }
}

// static
std::string SysInfo::GetLinuxStandardBaseVersionKey() {
  return std::string(kLinuxStandardBaseVersionKey);
}

// static
void SysInfo::ParseLsbRelease(const std::string& lsb_release,
                              int32 *major_version,
                              int32 *minor_version,
                              int32 *bugfix_version) {
  size_t version_key_index = lsb_release.find(kLinuxStandardBaseVersionKey);
  if (std::string::npos == version_key_index) {
    return;
  }
  size_t start_index = lsb_release.find_first_of('=', version_key_index);
  start_index++;  // Move past '='.
  size_t length = lsb_release.find_first_of('\n', start_index) - start_index;
  std::string version = lsb_release.substr(start_index, length);
  StringTokenizer tokenizer(version, ".");
  for (int i = 0; i < 3 && tokenizer.GetNext(); i++) {
    if (0 == i) {
      StringToInt(tokenizer.token_begin(),
                  tokenizer.token_end(),
                  major_version);
      *minor_version = *bugfix_version = 0;
    } else if (1 == i) {
      StringToInt(tokenizer.token_begin(),
                  tokenizer.token_end(),
                  minor_version);
    } else {  // 2 == i
      StringToInt(tokenizer.token_begin(),
                  tokenizer.token_end(),
                  bugfix_version);
    }
  }
}

}  // namespace base