version_loader.cc revision dc0f95d653279beabeb9817299e2902918ba123e
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 3// found in the LICENSE file. 4 5#include "chrome/browser/chromeos/version_loader.h" 6 7#include <vector> 8 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/threading/thread.h" 15#include "base/time.h" 16#include "chrome/browser/browser_process.h" 17#include "content/browser/browser_thread.h" 18 19namespace chromeos { 20 21// File to look for version number in. 22static const char kPath[] = "/etc/lsb-release"; 23 24VersionLoader::VersionLoader() : backend_(new Backend()) { 25} 26 27// Beginning of line we look for that gives full version number. 28// Format: x.x.xx.x (Developer|Official build extra info) board info 29// static 30const char VersionLoader::kFullVersionPrefix[] = 31 "CHROMEOS_RELEASE_DESCRIPTION="; 32 33// Same but for short version (x.x.xx.x). 34// static 35const char VersionLoader::kVersionPrefix[] = "CHROMEOS_RELEASE_VERSION="; 36 37VersionLoader::Handle VersionLoader::GetVersion( 38 CancelableRequestConsumerBase* consumer, 39 VersionLoader::GetVersionCallback* callback, 40 VersionFormat format) { 41 if (!g_browser_process->file_thread()) { 42 // This should only happen if Chrome is shutting down, so we don't do 43 // anything. 44 return 0; 45 } 46 47 scoped_refptr<GetVersionRequest> request(new GetVersionRequest(callback)); 48 AddRequest(request, consumer); 49 50 g_browser_process->file_thread()->message_loop()->PostTask( 51 FROM_HERE, 52 NewRunnableMethod(backend_.get(), &Backend::GetVersion, request, format)); 53 return request->handle(); 54} 55 56// static 57std::string VersionLoader::ParseVersion(const std::string& contents, 58 const std::string& prefix) { 59 // The file contains lines such as: 60 // XXX=YYY 61 // AAA=ZZZ 62 // 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. 64 std::vector<std::string> lines; 65 base::SplitString(contents, '\n', &lines); 66 for (size_t i = 0; i < lines.size(); ++i) { 67 if (StartsWithASCII(lines[i], prefix, false)) { 68 std::string version = lines[i].substr(std::string(prefix).size()); 69 if (version.size() > 1 && version[0] == '"' && 70 version[version.size() - 1] == '"') { 71 // Trim trailing and leading quotes. 72 version = version.substr(1, version.size() - 2); 73 } 74 return version; 75 } 76 } 77 return std::string(); 78} 79 80void VersionLoader::Backend::GetVersion( 81 scoped_refptr<GetVersionRequest> request, 82 VersionFormat format) { 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 84 if (request->canceled()) 85 return; 86 87 std::string version; 88 std::string contents; 89 const FilePath file_path(kPath); 90 if (file_util::ReadFileToString(file_path, &contents)) { 91 version = ParseVersion( 92 contents, 93 (format == VERSION_FULL) ? kFullVersionPrefix : kVersionPrefix); 94 } 95 96 if (format == VERSION_SHORT_WITH_DATE) { 97 base::PlatformFileInfo fileinfo; 98 if (file_util::GetFileInfo(file_path, &fileinfo)) { 99 base::Time::Exploded ctime; 100 fileinfo.creation_time.UTCExplode(&ctime); 101 version += StringPrintf("-%02u.%02u.%02u", 102 ctime.year % 100, 103 ctime.month, 104 ctime.day_of_month); 105 } 106 } 107 108 request->ForwardResult(GetVersionCallback::TupleType(request->handle(), 109 version)); 110} 111 112} // namespace chromeos 113