OLD | NEW |
| (Empty) |
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 "chrome/browser/chromeos/chromeos_version_loader.h" | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/file_util.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/string_util.h" | |
11 #include "base/thread.h" | |
12 #include "chrome/browser/browser_process.h" | |
13 | |
14 // Beginning of line we look for that gives version number. | |
15 static const char kPrefix[] = "CHROMEOS_RELEASE_DESCRIPTION="; | |
16 | |
17 // File to look for version number in. | |
18 static const char kPath[] = "/etc/lsb-release"; | |
19 | |
20 ChromeOSVersionLoader::ChromeOSVersionLoader() : backend_(new Backend()) { | |
21 } | |
22 | |
23 ChromeOSVersionLoader::Handle ChromeOSVersionLoader::GetVersion( | |
24 CancelableRequestConsumerBase* consumer, | |
25 ChromeOSVersionLoader::GetVersionCallback* callback) { | |
26 if (!g_browser_process->file_thread()) { | |
27 // This should only happen if Chrome is shutting down, so we don't do | |
28 // anything. | |
29 return 0; | |
30 } | |
31 | |
32 scoped_refptr<CancelableRequest<GetVersionCallback> > request( | |
33 new CancelableRequest<GetVersionCallback>(callback)); | |
34 AddRequest(request, consumer); | |
35 | |
36 g_browser_process->file_thread()->message_loop()->PostTask( | |
37 FROM_HERE, | |
38 NewRunnableMethod(backend_.get(), &Backend::GetVersion, request)); | |
39 return request->handle(); | |
40 } | |
41 | |
42 // static | |
43 std::string ChromeOSVersionLoader::ParseVersion(const std::string& contents) { | |
44 // The file contains lines such as: | |
45 // XXX=YYY | |
46 // AAA=ZZZ | |
47 // Split the lines and look for the one that starts with kPrefix. The version | |
48 // file is small, which is why we don't try and be tricky. | |
49 std::vector<std::string> lines; | |
50 SplitString(contents, '\n', &lines); | |
51 for (size_t i = 0; i < lines.size(); ++i) { | |
52 if (StartsWithASCII(lines[i], kPrefix, false)) { | |
53 std::string version = lines[i].substr(std::string(kPrefix).size()); | |
54 if (version.size() > 1 && version[0] == '"' && | |
55 version[version.size() - 1] == '"') { | |
56 // Trim trailing and leading quotes. | |
57 version = version.substr(1, version.size() - 2); | |
58 } | |
59 return version; | |
60 } | |
61 } | |
62 return std::string(); | |
63 } | |
64 | |
65 void ChromeOSVersionLoader::Backend::GetVersion( | |
66 scoped_refptr<GetVersionRequest> request) { | |
67 if (request->canceled()) | |
68 return; | |
69 | |
70 std::string version; | |
71 std::string contents; | |
72 if (file_util::ReadFileToString(FilePath(kPath), &contents)) | |
73 version = ParseVersion(contents); | |
74 request->ForwardResult(GetVersionCallback::TupleType(request->handle(), | |
75 version)); | |
76 } | |
OLD | NEW |