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"
9 #include "base/file_path.h"
10 #include "base/file_util.h"
11 #include "base/message_loop.h"
12 #include "base/string_split.h"
13 #include "base/string_util.h"
14 #include "base/stringprintf.h"
15 #include "base/threading/thread.h"
16 #include "base/time.h"
17 #include "chrome/browser/browser_process.h"
18 #include "content/public/browser/browser_thread.h"
20 using content::BrowserThread
;
24 // File to look for version number in.
25 static const char kPathVersion
[] = "/etc/lsb-release";
27 // File to look for firmware number in.
28 static const char kPathFirmware
[] = "/var/log/bios_info.txt";
30 VersionLoader::VersionLoader() : backend_(new Backend()) {}
32 VersionLoader::~VersionLoader() {}
34 // Beginning of line we look for that gives full version number.
35 // Format: x.x.xx.x (Developer|Official build extra info) board info
37 const char VersionLoader::kFullVersionPrefix
[] =
38 "CHROMEOS_RELEASE_DESCRIPTION=";
40 // Same but for short version (x.x.xx.x).
42 const char VersionLoader::kVersionPrefix
[] = "CHROMEOS_RELEASE_VERSION=";
44 // Beginning of line we look for that gives the firmware version.
45 const char VersionLoader::kFirmwarePrefix
[] = "version";
47 VersionLoader::Handle
VersionLoader::GetVersion(
48 CancelableRequestConsumerBase
* consumer
,
49 const VersionLoader::GetVersionCallback
& callback
,
50 VersionFormat format
) {
51 if (!BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
52 // This should only happen if Chrome is shutting down, so we don't do
57 scoped_refptr
<GetVersionRequest
> request(new GetVersionRequest(callback
));
58 AddRequest(request
, consumer
);
60 BrowserThread::PostTask(
63 NewRunnableMethod(backend_
.get(), &Backend::GetVersion
, 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
76 scoped_refptr
<GetFirmwareRequest
> request(new GetFirmwareRequest(callback
));
77 AddRequest(request
, consumer
);
79 BrowserThread::PostTask(
82 NewRunnableMethod(backend_
.get(), &Backend::GetFirmware
, request
));
83 return request
->handle();
87 std::string
VersionLoader::ParseVersion(const std::string
& contents
,
88 const std::string
& prefix
) {
89 // The file contains lines such as:
92 // Split the lines and look for the one that starts with prefix. The version
93 // file is small, which is why we don't try and be tricky.
94 std::vector
<std::string
> lines
;
95 base::SplitString(contents
, '\n', &lines
);
96 for (size_t i
= 0; i
< lines
.size(); ++i
) {
97 if (StartsWithASCII(lines
[i
], prefix
, false)) {
98 std::string version
= lines
[i
].substr(std::string(prefix
).size());
99 if (version
.size() > 1 && version
[0] == '"' &&
100 version
[version
.size() - 1] == '"') {
101 // Trim trailing and leading quotes.
102 version
= version
.substr(1, version
.size() - 2);
107 return std::string();
111 std::string
VersionLoader::ParseFirmware(const std::string
& contents
) {
112 // The file contains lines such as:
115 // release_date | ...
116 // We don't make any assumption that the spaces between "version" and "|" is
117 // fixed. So we just match kFirmwarePrefix at the start of the line and find
118 // the first character that is not "|" or space
120 std::vector
<std::string
> lines
;
121 base::SplitString(contents
, '\n', &lines
);
122 for (size_t i
= 0; i
< lines
.size(); ++i
) {
123 if (StartsWithASCII(lines
[i
], kFirmwarePrefix
, false)) {
124 std::string str
= lines
[i
].substr(std::string(kFirmwarePrefix
).size());
125 size_t found
= str
.find_first_not_of("| ");
126 if (found
!= std::string::npos
)
127 return str
.substr(found
);
130 return std::string();
133 void VersionLoader::Backend::GetVersion(
134 scoped_refptr
<GetVersionRequest
> request
,
135 VersionFormat format
) {
136 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
137 if (request
->canceled())
141 std::string contents
;
142 const FilePath
file_path(kPathVersion
);
143 if (file_util::ReadFileToString(file_path
, &contents
)) {
144 version
= ParseVersion(
146 (format
== VERSION_FULL
) ? kFullVersionPrefix
: kVersionPrefix
);
149 if (format
== VERSION_SHORT_WITH_DATE
) {
150 base::PlatformFileInfo fileinfo
;
151 if (file_util::GetFileInfo(file_path
, &fileinfo
)) {
152 base::Time::Exploded ctime
;
153 fileinfo
.creation_time
.UTCExplode(&ctime
);
154 version
+= base::StringPrintf("-%02u.%02u.%02u",
161 request
->ForwardResult(request
->handle(), version
);
164 void VersionLoader::Backend::GetFirmware(
165 scoped_refptr
<GetFirmwareRequest
> request
) {
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
167 if (request
->canceled())
170 std::string firmware
;
171 std::string contents
;
172 const FilePath
file_path(kPathFirmware
);
173 if (file_util::ReadFileToString(file_path
, &contents
)) {
174 firmware
= ParseFirmware(contents
);
177 request
->ForwardResult(request
->handle(), firmware
);
180 } // namespace chromeos