Standardize usage of virtual/override/final in chrome/browser/prerender/
[chromium-blink-merge.git] / base / file_version_info_win.cc
blob46b0581db97ff1fd41c621fce352af14527e56a0
1 // Copyright (c) 2011 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.
5 #include "base/file_version_info_win.h"
7 #include <windows.h>
9 #include "base/file_version_info.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "base/threading/thread_restrictions.h"
15 using base::FilePath;
17 FileVersionInfoWin::FileVersionInfoWin(void* data,
18 WORD language,
19 WORD code_page)
20 : language_(language), code_page_(code_page) {
21 base::ThreadRestrictions::AssertIOAllowed();
22 data_.reset((char*) data);
23 fixed_file_info_ = NULL;
24 UINT size;
25 ::VerQueryValue(data_.get(), L"\\", (LPVOID*)&fixed_file_info_, &size);
28 FileVersionInfoWin::~FileVersionInfoWin() {
29 DCHECK(data_.get());
32 typedef struct {
33 WORD language;
34 WORD code_page;
35 } LanguageAndCodePage;
37 // static
38 FileVersionInfo* FileVersionInfo::CreateFileVersionInfoForModule(
39 HMODULE module) {
40 // Note that the use of MAX_PATH is basically in line with what we do for
41 // all registered paths (PathProviderWin).
42 wchar_t system_buffer[MAX_PATH];
43 system_buffer[0] = 0;
44 if (!GetModuleFileName(module, system_buffer, MAX_PATH))
45 return NULL;
47 FilePath app_path(system_buffer);
48 return CreateFileVersionInfo(app_path);
51 // static
52 FileVersionInfo* FileVersionInfo::CreateFileVersionInfo(
53 const FilePath& file_path) {
54 base::ThreadRestrictions::AssertIOAllowed();
56 DWORD dummy;
57 const wchar_t* path = file_path.value().c_str();
58 DWORD length = ::GetFileVersionInfoSize(path, &dummy);
59 if (length == 0)
60 return NULL;
62 void* data = calloc(length, 1);
63 if (!data)
64 return NULL;
66 if (!::GetFileVersionInfo(path, dummy, length, data)) {
67 free(data);
68 return NULL;
71 LanguageAndCodePage* translate = NULL;
72 uint32 page_count;
73 BOOL query_result = VerQueryValue(data, L"\\VarFileInfo\\Translation",
74 (void**) &translate, &page_count);
76 if (query_result && translate) {
77 return new FileVersionInfoWin(data, translate->language,
78 translate->code_page);
80 } else {
81 free(data);
82 return NULL;
86 base::string16 FileVersionInfoWin::company_name() {
87 return GetStringValue(L"CompanyName");
90 base::string16 FileVersionInfoWin::company_short_name() {
91 return GetStringValue(L"CompanyShortName");
94 base::string16 FileVersionInfoWin::internal_name() {
95 return GetStringValue(L"InternalName");
98 base::string16 FileVersionInfoWin::product_name() {
99 return GetStringValue(L"ProductName");
102 base::string16 FileVersionInfoWin::product_short_name() {
103 return GetStringValue(L"ProductShortName");
106 base::string16 FileVersionInfoWin::comments() {
107 return GetStringValue(L"Comments");
110 base::string16 FileVersionInfoWin::legal_copyright() {
111 return GetStringValue(L"LegalCopyright");
114 base::string16 FileVersionInfoWin::product_version() {
115 return GetStringValue(L"ProductVersion");
118 base::string16 FileVersionInfoWin::file_description() {
119 return GetStringValue(L"FileDescription");
122 base::string16 FileVersionInfoWin::legal_trademarks() {
123 return GetStringValue(L"LegalTrademarks");
126 base::string16 FileVersionInfoWin::private_build() {
127 return GetStringValue(L"PrivateBuild");
130 base::string16 FileVersionInfoWin::file_version() {
131 return GetStringValue(L"FileVersion");
134 base::string16 FileVersionInfoWin::original_filename() {
135 return GetStringValue(L"OriginalFilename");
138 base::string16 FileVersionInfoWin::special_build() {
139 return GetStringValue(L"SpecialBuild");
142 base::string16 FileVersionInfoWin::last_change() {
143 return GetStringValue(L"LastChange");
146 bool FileVersionInfoWin::is_official_build() {
147 return (GetStringValue(L"Official Build").compare(L"1") == 0);
150 bool FileVersionInfoWin::GetValue(const wchar_t* name,
151 std::wstring* value_str) {
152 WORD lang_codepage[8];
153 int i = 0;
154 // Use the language and codepage from the DLL.
155 lang_codepage[i++] = language_;
156 lang_codepage[i++] = code_page_;
157 // Use the default language and codepage from the DLL.
158 lang_codepage[i++] = ::GetUserDefaultLangID();
159 lang_codepage[i++] = code_page_;
160 // Use the language from the DLL and Latin codepage (most common).
161 lang_codepage[i++] = language_;
162 lang_codepage[i++] = 1252;
163 // Use the default language and Latin codepage (most common).
164 lang_codepage[i++] = ::GetUserDefaultLangID();
165 lang_codepage[i++] = 1252;
167 i = 0;
168 while (i < arraysize(lang_codepage)) {
169 wchar_t sub_block[MAX_PATH];
170 WORD language = lang_codepage[i++];
171 WORD code_page = lang_codepage[i++];
172 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH,
173 L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name);
174 LPVOID value = NULL;
175 uint32 size;
176 BOOL r = ::VerQueryValue(data_.get(), sub_block, &value, &size);
177 if (r && value) {
178 value_str->assign(static_cast<wchar_t*>(value));
179 return true;
182 return false;
185 std::wstring FileVersionInfoWin::GetStringValue(const wchar_t* name) {
186 std::wstring str;
187 if (GetValue(name, &str))
188 return str;
189 else
190 return L"";