| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/version_loader.h" | 5 #include "chrome/browser/chromeos/version_loader.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/string_split.h" | 12 #include "base/string_split.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "content/browser/browser_thread.h" | 17 #include "content/browser/browser_thread.h" |
| 18 | 18 |
| 19 namespace chromeos { | 19 namespace chromeos { |
| 20 | 20 |
| 21 // File to look for version number in. | 21 // File to look for version number in. |
| 22 static const char kPath[] = "/etc/lsb-release"; | 22 static const char kPathVersion[] = "/etc/lsb-release"; |
| 23 |
| 24 // File to look for firmware number in. |
| 25 static const char kPathFirmware[] = "/var/log/bios_info.txt"; |
| 23 | 26 |
| 24 VersionLoader::VersionLoader() : backend_(new Backend()) { | 27 VersionLoader::VersionLoader() : backend_(new Backend()) { |
| 25 } | 28 } |
| 26 | 29 |
| 27 // Beginning of line we look for that gives full version number. | 30 // Beginning of line we look for that gives full version number. |
| 28 // Format: x.x.xx.x (Developer|Official build extra info) board info | 31 // Format: x.x.xx.x (Developer|Official build extra info) board info |
| 29 // static | 32 // static |
| 30 const char VersionLoader::kFullVersionPrefix[] = | 33 const char VersionLoader::kFullVersionPrefix[] = |
| 31 "CHROMEOS_RELEASE_DESCRIPTION="; | 34 "CHROMEOS_RELEASE_DESCRIPTION="; |
| 32 | 35 |
| 33 // Same but for short version (x.x.xx.x). | 36 // Same but for short version (x.x.xx.x). |
| 34 // static | 37 // static |
| 35 const char VersionLoader::kVersionPrefix[] = "CHROMEOS_RELEASE_VERSION="; | 38 const char VersionLoader::kVersionPrefix[] = "CHROMEOS_RELEASE_VERSION="; |
| 36 | 39 |
| 40 // Beginning of line we look for that gives the firmware version. |
| 41 const char VersionLoader::kFirmwarePrefix[] = "version"; |
| 42 |
| 37 VersionLoader::Handle VersionLoader::GetVersion( | 43 VersionLoader::Handle VersionLoader::GetVersion( |
| 38 CancelableRequestConsumerBase* consumer, | 44 CancelableRequestConsumerBase* consumer, |
| 39 VersionLoader::GetVersionCallback* callback, | 45 VersionLoader::GetVersionCallback* callback, |
| 40 VersionFormat format) { | 46 VersionFormat format) { |
| 41 if (!g_browser_process->file_thread()) { | 47 if (!g_browser_process->file_thread()) { |
| 42 // This should only happen if Chrome is shutting down, so we don't do | 48 // This should only happen if Chrome is shutting down, so we don't do |
| 43 // anything. | 49 // anything. |
| 44 return 0; | 50 return 0; |
| 45 } | 51 } |
| 46 | 52 |
| 47 scoped_refptr<GetVersionRequest> request(new GetVersionRequest(callback)); | 53 scoped_refptr<GetVersionRequest> request(new GetVersionRequest(callback)); |
| 48 AddRequest(request, consumer); | 54 AddRequest(request, consumer); |
| 49 | 55 |
| 50 g_browser_process->file_thread()->message_loop()->PostTask( | 56 g_browser_process->file_thread()->message_loop()->PostTask( |
| 51 FROM_HERE, | 57 FROM_HERE, |
| 52 NewRunnableMethod(backend_.get(), &Backend::GetVersion, request, format)); | 58 NewRunnableMethod(backend_.get(), &Backend::GetVersion, request, format)); |
| 53 return request->handle(); | 59 return request->handle(); |
| 54 } | 60 } |
| 55 | 61 |
| 62 VersionLoader::Handle VersionLoader::GetFirmware( |
| 63 CancelableRequestConsumerBase* consumer, |
| 64 VersionLoader::GetFirmwareCallback* callback) { |
| 65 if (!g_browser_process->file_thread()) { |
| 66 // This should only happen if Chrome is shutting down, so we don't do |
| 67 // anything. |
| 68 return 0; |
| 69 } |
| 70 |
| 71 scoped_refptr<GetFirmwareRequest> request(new GetFirmwareRequest(callback)); |
| 72 AddRequest(request, consumer); |
| 73 |
| 74 g_browser_process->file_thread()->message_loop()->PostTask( |
| 75 FROM_HERE, |
| 76 NewRunnableMethod(backend_.get(), &Backend::GetFirmware, request)); |
| 77 return request->handle(); |
| 78 } |
| 79 |
| 56 // static | 80 // static |
| 57 std::string VersionLoader::ParseVersion(const std::string& contents, | 81 std::string VersionLoader::ParseVersion(const std::string& contents, |
| 58 const std::string& prefix) { | 82 const std::string& prefix) { |
| 59 // The file contains lines such as: | 83 // The file contains lines such as: |
| 60 // XXX=YYY | 84 // XXX=YYY |
| 61 // AAA=ZZZ | 85 // AAA=ZZZ |
| 62 // Split the lines and look for the one that starts with prefix. The version | 86 // Split the lines and look for the one that starts with prefix. The version |
| 63 // file is small, which is why we don't try and be tricky. | 87 // file is small, which is why we don't try and be tricky. |
| 64 std::vector<std::string> lines; | 88 std::vector<std::string> lines; |
| 65 base::SplitString(contents, '\n', &lines); | 89 base::SplitString(contents, '\n', &lines); |
| 66 for (size_t i = 0; i < lines.size(); ++i) { | 90 for (size_t i = 0; i < lines.size(); ++i) { |
| 67 if (StartsWithASCII(lines[i], prefix, false)) { | 91 if (StartsWithASCII(lines[i], prefix, false)) { |
| 68 std::string version = lines[i].substr(std::string(prefix).size()); | 92 std::string version = lines[i].substr(std::string(prefix).size()); |
| 69 if (version.size() > 1 && version[0] == '"' && | 93 if (version.size() > 1 && version[0] == '"' && |
| 70 version[version.size() - 1] == '"') { | 94 version[version.size() - 1] == '"') { |
| 71 // Trim trailing and leading quotes. | 95 // Trim trailing and leading quotes. |
| 72 version = version.substr(1, version.size() - 2); | 96 version = version.substr(1, version.size() - 2); |
| 73 } | 97 } |
| 74 return version; | 98 return version; |
| 75 } | 99 } |
| 76 } | 100 } |
| 77 return std::string(); | 101 return std::string(); |
| 78 } | 102 } |
| 79 | 103 |
| 104 // static |
| 105 std::string VersionLoader::ParseFirmware(const std::string& contents) { |
| 106 // The file contains lines such as: |
| 107 // vendor | ... |
| 108 // version | ... |
| 109 // release_date | ... |
| 110 // We don't make any assumption that the spaces between "version" and "|" is |
| 111 // fixed. So we just match kFirmwarePrefix at the start of the line and find |
| 112 // the first character that is not "|" or space |
| 113 |
| 114 std::vector<std::string> lines; |
| 115 base::SplitString(contents, '\n', &lines); |
| 116 for (size_t i = 0; i < lines.size(); ++i) { |
| 117 if (StartsWithASCII(lines[i], kFirmwarePrefix, false)) { |
| 118 std::string str = lines[i].substr(std::string(kFirmwarePrefix).size()); |
| 119 size_t found = str.find_first_not_of("| "); |
| 120 if (found != std::string::npos) |
| 121 return str.substr(found); |
| 122 } |
| 123 } |
| 124 return std::string(); |
| 125 } |
| 126 |
| 80 void VersionLoader::Backend::GetVersion( | 127 void VersionLoader::Backend::GetVersion( |
| 81 scoped_refptr<GetVersionRequest> request, | 128 scoped_refptr<GetVersionRequest> request, |
| 82 VersionFormat format) { | 129 VersionFormat format) { |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 84 if (request->canceled()) | 131 if (request->canceled()) |
| 85 return; | 132 return; |
| 86 | 133 |
| 87 std::string version; | 134 std::string version; |
| 88 std::string contents; | 135 std::string contents; |
| 89 const FilePath file_path(kPath); | 136 const FilePath file_path(kPathVersion); |
| 90 if (file_util::ReadFileToString(file_path, &contents)) { | 137 if (file_util::ReadFileToString(file_path, &contents)) { |
| 91 version = ParseVersion( | 138 version = ParseVersion( |
| 92 contents, | 139 contents, |
| 93 (format == VERSION_FULL) ? kFullVersionPrefix : kVersionPrefix); | 140 (format == VERSION_FULL) ? kFullVersionPrefix : kVersionPrefix); |
| 94 } | 141 } |
| 95 | 142 |
| 96 if (format == VERSION_SHORT_WITH_DATE) { | 143 if (format == VERSION_SHORT_WITH_DATE) { |
| 97 base::PlatformFileInfo fileinfo; | 144 base::PlatformFileInfo fileinfo; |
| 98 if (file_util::GetFileInfo(file_path, &fileinfo)) { | 145 if (file_util::GetFileInfo(file_path, &fileinfo)) { |
| 99 base::Time::Exploded ctime; | 146 base::Time::Exploded ctime; |
| 100 fileinfo.creation_time.UTCExplode(&ctime); | 147 fileinfo.creation_time.UTCExplode(&ctime); |
| 101 version += StringPrintf("-%02u.%02u.%02u", | 148 version += StringPrintf("-%02u.%02u.%02u", |
| 102 ctime.year % 100, | 149 ctime.year % 100, |
| 103 ctime.month, | 150 ctime.month, |
| 104 ctime.day_of_month); | 151 ctime.day_of_month); |
| 105 } | 152 } |
| 106 } | 153 } |
| 107 | 154 |
| 108 request->ForwardResult(GetVersionCallback::TupleType(request->handle(), | 155 request->ForwardResult(GetVersionCallback::TupleType(request->handle(), |
| 109 version)); | 156 version)); |
| 110 } | 157 } |
| 111 | 158 |
| 159 void VersionLoader::Backend::GetFirmware( |
| 160 scoped_refptr<GetFirmwareRequest> request) { |
| 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 162 if (request->canceled()) |
| 163 return; |
| 164 |
| 165 std::string firmware; |
| 166 std::string contents; |
| 167 const FilePath file_path(kPathFirmware); |
| 168 if (file_util::ReadFileToString(file_path, &contents)) { |
| 169 firmware = ParseFirmware(contents); |
| 170 } |
| 171 |
| 172 request->ForwardResult(GetFirmwareCallback::TupleType(request->handle(), |
| 173 firmware)); |
| 174 } |
| 175 |
| 112 } // namespace chromeos | 176 } // namespace chromeos |
| OLD | NEW |