Convert cacheinvalidation_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / chromeos / system / version_loader.cc
blob4e6272c27e477d58b3479b204c7db2aba046b9bd
1 // Copyright (c) 2011 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.
5 #include "chromeos/system/version_loader.h"
7 #include <vector>
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/location.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/sys_info.h"
16 #include "base/time/time.h"
18 namespace chromeos {
19 namespace version_loader {
21 namespace {
23 // Beginning of line we look for that gives full version number.
24 // Format: x.x.xx.x (Developer|Official build extra info) board info
25 const char kFullVersionKey[] = "CHROMEOS_RELEASE_DESCRIPTION";
27 // Same but for short version (x.x.xx.x).
28 const char kVersionKey[] = "CHROMEOS_RELEASE_VERSION";
30 // Beginning of line we look for that gives the firmware version.
31 const char kFirmwarePrefix[] = "version";
33 // File to look for firmware number in.
34 const char kPathFirmware[] = "/var/log/bios_info.txt";
36 } // namespace
38 std::string GetVersion(VersionFormat format) {
39 std::string version;
40 std::string key = (format == VERSION_FULL ?
41 kFullVersionKey : kVersionKey);
42 if (!base::SysInfo::GetLsbReleaseValue(key, &version)) {
43 LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS())
44 << "No LSB version key: " << key;
45 version = "0.0.0.0";
47 if (format == VERSION_SHORT_WITH_DATE) {
48 base::Time::Exploded ctime;
49 base::SysInfo::GetLsbReleaseTime().UTCExplode(&ctime);
50 version += base::StringPrintf("-%02u.%02u.%02u",
51 ctime.year % 100,
52 ctime.month,
53 ctime.day_of_month);
56 return version;
59 std::string GetFirmware() {
60 std::string firmware;
61 std::string contents;
62 const base::FilePath file_path(kPathFirmware);
63 if (base::ReadFileToString(file_path, &contents)) {
64 firmware = ParseFirmware(contents);
66 return firmware;
69 std::string ParseFirmware(const std::string& contents) {
70 // The file contains lines such as:
71 // vendor | ...
72 // version | ...
73 // release_date | ...
74 // We don't make any assumption that the spaces between "version" and "|" is
75 // fixed. So we just match kFirmwarePrefix at the start of the line and find
76 // the first character that is not "|" or space
78 std::vector<std::string> lines;
79 base::SplitString(contents, '\n', &lines);
80 for (size_t i = 0; i < lines.size(); ++i) {
81 if (base::StartsWithASCII(lines[i], kFirmwarePrefix, false)) {
82 std::string str = lines[i].substr(std::string(kFirmwarePrefix).size());
83 size_t found = str.find_first_not_of("| ");
84 if (found != std::string::npos)
85 return str.substr(found);
88 return std::string();
91 } // namespace version_loader
92 } // namespace chromeos