Split gdata_file_system_interface.h from gdata_file_system.h
[chromium-blink-merge.git] / chrome / browser / chromeos / version_loader.cc
blob4f70e121f114239bff80cdd225808ea565d3c45a
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 "chrome/browser/chromeos/version_loader.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/file_path.h"
11 #include "base/file_util.h"
12 #include "base/message_loop.h"
13 #include "base/string_split.h"
14 #include "base/string_util.h"
15 #include "base/stringprintf.h"
16 #include "base/threading/thread.h"
17 #include "base/time.h"
18 #include "chrome/browser/browser_process.h"
19 #include "content/public/browser/browser_thread.h"
21 using content::BrowserThread;
23 namespace chromeos {
25 // File to look for version number in.
26 static const char kPathVersion[] = "/etc/lsb-release";
28 // File to look for firmware number in.
29 static const char kPathFirmware[] = "/var/log/bios_info.txt";
31 VersionLoader::VersionLoader() : backend_(new Backend()) {}
33 VersionLoader::~VersionLoader() {}
35 // Beginning of line we look for that gives full version number.
36 // Format: x.x.xx.x (Developer|Official build extra info) board info
37 // static
38 const char VersionLoader::kFullVersionPrefix[] =
39 "CHROMEOS_RELEASE_DESCRIPTION=";
41 // Same but for short version (x.x.xx.x).
42 // static
43 const char VersionLoader::kVersionPrefix[] = "CHROMEOS_RELEASE_VERSION=";
45 // Beginning of line we look for that gives the firmware version.
46 const char VersionLoader::kFirmwarePrefix[] = "version";
48 VersionLoader::Handle VersionLoader::GetVersion(
49 CancelableRequestConsumerBase* consumer,
50 const VersionLoader::GetVersionCallback& callback,
51 VersionFormat format) {
52 if (!BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
53 // This should only happen if Chrome is shutting down, so we don't do
54 // anything.
55 return 0;
58 scoped_refptr<GetVersionRequest> request(new GetVersionRequest(callback));
59 AddRequest(request, consumer);
61 BrowserThread::PostTask(
62 BrowserThread::FILE, FROM_HERE,
63 base::Bind(&Backend::GetVersion, backend_.get(), request, format));
64 return request->handle();
67 VersionLoader::Handle VersionLoader::GetFirmware(
68 CancelableRequestConsumerBase* consumer,
69 const VersionLoader::GetFirmwareCallback& callback) {
70 if (!BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
71 // This should only happen if Chrome is shutting down, so we don't do
72 // anything.
73 return 0;
76 scoped_refptr<GetFirmwareRequest> request(new GetFirmwareRequest(callback));
77 AddRequest(request, consumer);
79 BrowserThread::PostTask(
80 BrowserThread::FILE, FROM_HERE,
81 base::Bind(&Backend::GetFirmware, backend_.get(), request));
82 return request->handle();
85 // static
86 std::string VersionLoader::ParseVersion(const std::string& contents,
87 const std::string& prefix) {
88 // The file contains lines such as:
89 // XXX=YYY
90 // AAA=ZZZ
91 // Split the lines and look for the one that starts with prefix. The version
92 // file is small, which is why we don't try and be tricky.
93 std::vector<std::string> lines;
94 base::SplitString(contents, '\n', &lines);
95 for (size_t i = 0; i < lines.size(); ++i) {
96 if (StartsWithASCII(lines[i], prefix, false)) {
97 std::string version = lines[i].substr(std::string(prefix).size());
98 if (version.size() > 1 && version[0] == '"' &&
99 version[version.size() - 1] == '"') {
100 // Trim trailing and leading quotes.
101 version = version.substr(1, version.size() - 2);
103 return version;
106 return std::string();
109 // static
110 std::string VersionLoader::ParseFirmware(const std::string& contents) {
111 // The file contains lines such as:
112 // vendor | ...
113 // version | ...
114 // release_date | ...
115 // We don't make any assumption that the spaces between "version" and "|" is
116 // fixed. So we just match kFirmwarePrefix at the start of the line and find
117 // the first character that is not "|" or space
119 std::vector<std::string> lines;
120 base::SplitString(contents, '\n', &lines);
121 for (size_t i = 0; i < lines.size(); ++i) {
122 if (StartsWithASCII(lines[i], kFirmwarePrefix, false)) {
123 std::string str = lines[i].substr(std::string(kFirmwarePrefix).size());
124 size_t found = str.find_first_not_of("| ");
125 if (found != std::string::npos)
126 return str.substr(found);
129 return std::string();
132 void VersionLoader::Backend::GetVersion(
133 scoped_refptr<GetVersionRequest> request,
134 VersionFormat format) {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
136 if (request->canceled())
137 return;
139 std::string version;
140 std::string contents;
141 const FilePath file_path(kPathVersion);
142 if (file_util::ReadFileToString(file_path, &contents)) {
143 version = ParseVersion(
144 contents,
145 (format == VERSION_FULL) ? kFullVersionPrefix : kVersionPrefix);
148 if (format == VERSION_SHORT_WITH_DATE) {
149 base::PlatformFileInfo fileinfo;
150 if (file_util::GetFileInfo(file_path, &fileinfo)) {
151 base::Time::Exploded ctime;
152 fileinfo.creation_time.UTCExplode(&ctime);
153 version += base::StringPrintf("-%02u.%02u.%02u",
154 ctime.year % 100,
155 ctime.month,
156 ctime.day_of_month);
160 request->ForwardResult(request->handle(), version);
163 void VersionLoader::Backend::GetFirmware(
164 scoped_refptr<GetFirmwareRequest> request) {
165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
166 if (request->canceled())
167 return;
169 std::string firmware;
170 std::string contents;
171 const FilePath file_path(kPathFirmware);
172 if (file_util::ReadFileToString(file_path, &contents)) {
173 firmware = ParseFirmware(contents);
176 request->ForwardResult(request->handle(), firmware);
179 } // namespace chromeos