Home | History | Annotate | Download | only in common
      1 // Copyright (c) 2012 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/common/chrome_version_info.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/file_version_info.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "base/threading/thread_restrictions.h"
     11 #include "build/build_config.h"
     12 #include "grit/chromium_strings.h"
     13 #include "grit/generated_resources.h"
     14 #include "ui/base/l10n/l10n_util.h"
     15 #include "ui/base/resource/resource_bundle.h"
     16 
     17 namespace chrome {
     18 
     19 std::string VersionInfo::ProductNameAndVersionForUserAgent() const {
     20   if (!is_valid())
     21     return std::string();
     22   return "Chrome/" + Version();
     23 }
     24 
     25 #if defined(OS_WIN) || defined(OS_MACOSX)
     26 // On Windows and Mac, we get the Chrome version info by querying
     27 // FileVersionInfo for the current module.
     28 
     29 VersionInfo::VersionInfo() {
     30   // The current module is already loaded in memory, so this will be cheap.
     31   base::ThreadRestrictions::ScopedAllowIO allow_io;
     32   version_info_.reset(FileVersionInfo::CreateFileVersionInfoForCurrentModule());
     33 }
     34 
     35 VersionInfo::~VersionInfo() {
     36 }
     37 
     38 bool VersionInfo::is_valid() const {
     39   return version_info_.get() != NULL;
     40 }
     41 
     42 std::string VersionInfo::Name() const {
     43   if (!is_valid())
     44     return std::string();
     45   return base::UTF16ToUTF8(version_info_->product_name());
     46 }
     47 
     48 std::string VersionInfo::Version() const {
     49   if (!is_valid())
     50     return std::string();
     51   return base::UTF16ToUTF8(version_info_->product_version());
     52 }
     53 
     54 std::string VersionInfo::LastChange() const {
     55   if (!is_valid())
     56     return std::string();
     57   return base::UTF16ToUTF8(version_info_->last_change());
     58 }
     59 
     60 bool VersionInfo::IsOfficialBuild() const {
     61   if (!is_valid())
     62     return false;
     63   return version_info_->is_official_build();
     64 }
     65 
     66 #elif defined(OS_POSIX)
     67 // We get chrome version information from chrome_version_info_posix.h,
     68 // a generated header.
     69 
     70 #include "chrome/common/chrome_version_info_posix.h"
     71 
     72 VersionInfo::VersionInfo() {
     73 }
     74 
     75 VersionInfo::~VersionInfo() {
     76 }
     77 
     78 bool VersionInfo::is_valid() const {
     79   return true;
     80 }
     81 
     82 std::string VersionInfo::Name() const {
     83   return PRODUCT_NAME;
     84 }
     85 
     86 std::string VersionInfo::Version() const {
     87   return PRODUCT_VERSION;
     88 }
     89 
     90 std::string VersionInfo::LastChange() const {
     91   return LAST_CHANGE;
     92 }
     93 
     94 bool VersionInfo::IsOfficialBuild() const {
     95   return IS_OFFICIAL_BUILD;
     96 }
     97 
     98 #endif
     99 
    100 std::string VersionInfo::CreateVersionString() const {
    101   std::string current_version;
    102   if (is_valid()) {
    103     current_version += Version();
    104 #if !defined(GOOGLE_CHROME_BUILD)
    105     current_version += " (";
    106     current_version += l10n_util::GetStringUTF8(IDS_ABOUT_VERSION_UNOFFICIAL);
    107     current_version += " ";
    108     current_version += LastChange();
    109     current_version += " ";
    110     current_version += OSType();
    111     current_version += ")";
    112 #endif
    113     std::string modifier = GetVersionStringModifier();
    114     if (!modifier.empty())
    115       current_version += " " + modifier;
    116   }
    117   return current_version;
    118 }
    119 
    120 std::string VersionInfo::OSType() const {
    121 #if defined(OS_WIN)
    122   return "Windows";
    123 #elif defined(OS_MACOSX)
    124   return "Mac OS X";
    125 #elif defined(OS_CHROMEOS)
    126   #if defined(GOOGLE_CHROME_BUILD)
    127     return "Chrome OS";
    128   #else
    129     return "Chromium OS";
    130   #endif
    131 #elif defined(OS_ANDROID)
    132   return "Android";
    133 #elif defined(OS_LINUX)
    134   return "Linux";
    135 #elif defined(OS_FREEBSD)
    136   return "FreeBSD";
    137 #elif defined(OS_OPENBSD)
    138   return "OpenBSD";
    139 #elif defined(OS_SOLARIS)
    140   return "Solaris";
    141 #else
    142   return "Unknown";
    143 #endif
    144 }
    145 
    146 }  // namespace chrome
    147