[email protected] | f48122119 | 2011-04-07 22:15:34 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | bcff05a | 2010-04-14 01:46:43 | [diff] [blame] | 5 | #include "base/file_version_info_win.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | |
[email protected] | f539333 | 2009-06-03 15:01:29 | [diff] [blame] | 7 | #include <windows.h> |
| 8 | |
| 9 | #include "base/file_path.h" |
[email protected] | bcff05a | 2010-04-14 01:46:43 | [diff] [blame] | 10 | #include "base/file_version_info.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | #include "base/path_service.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 13 | #include "base/threading/thread_restrictions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
[email protected] | bcff05a | 2010-04-14 01:46:43 | [diff] [blame] | 15 | FileVersionInfoWin::FileVersionInfoWin(void* data, int language, int code_page) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 16 | : language_(language), code_page_(code_page) { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 17 | base::ThreadRestrictions::AssertIOAllowed(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 18 | data_.reset((char*) data); |
| 19 | fixed_file_info_ = NULL; |
| 20 | UINT size; |
| 21 | ::VerQueryValue(data_.get(), L"\\", (LPVOID*)&fixed_file_info_, &size); |
| 22 | } |
| 23 | |
[email protected] | bcff05a | 2010-04-14 01:46:43 | [diff] [blame] | 24 | FileVersionInfoWin::~FileVersionInfoWin() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 25 | DCHECK(data_.get()); |
| 26 | } |
| 27 | |
| 28 | typedef struct { |
| 29 | WORD language; |
| 30 | WORD code_page; |
| 31 | } LanguageAndCodePage; |
| 32 | |
| 33 | // static |
[email protected] | 70910be | 2011-04-22 23:55:45 | [diff] [blame] | 34 | FileVersionInfo* FileVersionInfo::CreateFileVersionInfoForModule( |
| 35 | HMODULE module) { |
| 36 | // Note that the use of MAX_PATH is basically in line with what we do for |
| 37 | // all registered paths (PathProviderWin). |
| 38 | wchar_t system_buffer[MAX_PATH]; |
| 39 | system_buffer[0] = 0; |
| 40 | if (!GetModuleFileName(module, system_buffer, MAX_PATH)) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 41 | return NULL; |
| 42 | |
[email protected] | 70910be | 2011-04-22 23:55:45 | [diff] [blame] | 43 | FilePath app_path(system_buffer); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 44 | return CreateFileVersionInfo(app_path); |
| 45 | } |
| 46 | |
| 47 | // static |
| 48 | FileVersionInfo* FileVersionInfo::CreateFileVersionInfo( |
[email protected] | 0956223 | 2008-12-23 16:57:36 | [diff] [blame] | 49 | const FilePath& file_path) { |
[email protected] | 45446a5 | 2010-11-04 17:41:00 | [diff] [blame] | 50 | base::ThreadRestrictions::AssertIOAllowed(); |
| 51 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 52 | DWORD dummy; |
[email protected] | 0956223 | 2008-12-23 16:57:36 | [diff] [blame] | 53 | const wchar_t* path = file_path.value().c_str(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 54 | DWORD length = ::GetFileVersionInfoSize(path, &dummy); |
| 55 | if (length == 0) |
| 56 | return NULL; |
| 57 | |
| 58 | void* data = calloc(length, 1); |
| 59 | if (!data) |
| 60 | return NULL; |
| 61 | |
| 62 | if (!::GetFileVersionInfo(path, dummy, length, data)) { |
| 63 | free(data); |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | LanguageAndCodePage* translate = NULL; |
| 68 | uint32 page_count; |
| 69 | BOOL query_result = VerQueryValue(data, L"\\VarFileInfo\\Translation", |
| 70 | (void**) &translate, &page_count); |
| 71 | |
| 72 | if (query_result && translate) { |
[email protected] | bcff05a | 2010-04-14 01:46:43 | [diff] [blame] | 73 | return new FileVersionInfoWin(data, translate->language, |
| 74 | translate->code_page); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 75 | |
| 76 | } else { |
| 77 | free(data); |
| 78 | return NULL; |
| 79 | } |
| 80 | } |
| 81 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 82 | string16 FileVersionInfoWin::company_name() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 83 | return GetStringValue(L"CompanyName"); |
| 84 | } |
| 85 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 86 | string16 FileVersionInfoWin::company_short_name() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 87 | return GetStringValue(L"CompanyShortName"); |
| 88 | } |
| 89 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 90 | string16 FileVersionInfoWin::internal_name() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 91 | return GetStringValue(L"InternalName"); |
| 92 | } |
| 93 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 94 | string16 FileVersionInfoWin::product_name() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 95 | return GetStringValue(L"ProductName"); |
| 96 | } |
| 97 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 98 | string16 FileVersionInfoWin::product_short_name() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 99 | return GetStringValue(L"ProductShortName"); |
| 100 | } |
| 101 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 102 | string16 FileVersionInfoWin::comments() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 103 | return GetStringValue(L"Comments"); |
| 104 | } |
| 105 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 106 | string16 FileVersionInfoWin::legal_copyright() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 107 | return GetStringValue(L"LegalCopyright"); |
| 108 | } |
| 109 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 110 | string16 FileVersionInfoWin::product_version() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 111 | return GetStringValue(L"ProductVersion"); |
| 112 | } |
| 113 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 114 | string16 FileVersionInfoWin::file_description() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 115 | return GetStringValue(L"FileDescription"); |
| 116 | } |
| 117 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 118 | string16 FileVersionInfoWin::legal_trademarks() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 119 | return GetStringValue(L"LegalTrademarks"); |
| 120 | } |
| 121 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 122 | string16 FileVersionInfoWin::private_build() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 123 | return GetStringValue(L"PrivateBuild"); |
| 124 | } |
| 125 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 126 | string16 FileVersionInfoWin::file_version() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 127 | return GetStringValue(L"FileVersion"); |
| 128 | } |
| 129 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 130 | string16 FileVersionInfoWin::original_filename() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 131 | return GetStringValue(L"OriginalFilename"); |
| 132 | } |
| 133 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 134 | string16 FileVersionInfoWin::special_build() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 135 | return GetStringValue(L"SpecialBuild"); |
| 136 | } |
| 137 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 138 | string16 FileVersionInfoWin::last_change() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 139 | return GetStringValue(L"LastChange"); |
| 140 | } |
| 141 | |
[email protected] | bcff05a | 2010-04-14 01:46:43 | [diff] [blame] | 142 | bool FileVersionInfoWin::is_official_build() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 143 | return (GetStringValue(L"Official Build").compare(L"1") == 0); |
| 144 | } |
| 145 | |
[email protected] | bcff05a | 2010-04-14 01:46:43 | [diff] [blame] | 146 | bool FileVersionInfoWin::GetValue(const wchar_t* name, |
| 147 | std::wstring* value_str) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 148 | WORD lang_codepage[8]; |
| 149 | int i = 0; |
| 150 | // Use the language and codepage from the DLL. |
| 151 | lang_codepage[i++] = language_; |
| 152 | lang_codepage[i++] = code_page_; |
| 153 | // Use the default language and codepage from the DLL. |
| 154 | lang_codepage[i++] = ::GetUserDefaultLangID(); |
| 155 | lang_codepage[i++] = code_page_; |
| 156 | // Use the language from the DLL and Latin codepage (most common). |
| 157 | lang_codepage[i++] = language_; |
| 158 | lang_codepage[i++] = 1252; |
| 159 | // Use the default language and Latin codepage (most common). |
| 160 | lang_codepage[i++] = ::GetUserDefaultLangID(); |
| 161 | lang_codepage[i++] = 1252; |
| 162 | |
| 163 | i = 0; |
| 164 | while (i < arraysize(lang_codepage)) { |
| 165 | wchar_t sub_block[MAX_PATH]; |
| 166 | WORD language = lang_codepage[i++]; |
| 167 | WORD code_page = lang_codepage[i++]; |
| 168 | _snwprintf_s(sub_block, MAX_PATH, MAX_PATH, |
[email protected] | 5115474 | 2008-08-14 00:41:45 | [diff] [blame] | 169 | L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 170 | LPVOID value = NULL; |
| 171 | uint32 size; |
| 172 | BOOL r = ::VerQueryValue(data_.get(), sub_block, &value, &size); |
| 173 | if (r && value) { |
| 174 | value_str->assign(static_cast<wchar_t*>(value)); |
| 175 | return true; |
| 176 | } |
| 177 | } |
| 178 | return false; |
| 179 | } |
| 180 | |
[email protected] | bcff05a | 2010-04-14 01:46:43 | [diff] [blame] | 181 | std::wstring FileVersionInfoWin::GetStringValue(const wchar_t* name) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 182 | std::wstring str; |
| 183 | if (GetValue(name, &str)) |
| 184 | return str; |
| 185 | else |
| 186 | return L""; |
| 187 | } |