WebKit roll 109146:109201
[chromium-blink-merge.git] / base / file_version_info_win.cc
blob6528ca3dfbb1237e4679dc4347270373360bac70
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_path.h"
10 #include "base/file_version_info.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "base/threading/thread_restrictions.h"
15 FileVersionInfoWin::FileVersionInfoWin(void* data, int language, int code_page)
16 : language_(language), code_page_(code_page) {
17 base::ThreadRestrictions::AssertIOAllowed();
18 data_.reset((char*) data);
19 fixed_file_info_ = NULL;
20 UINT size;
21 ::VerQueryValue(data_.get(), L"\\", (LPVOID*)&fixed_file_info_, &size);
24 FileVersionInfoWin::~FileVersionInfoWin() {
25 DCHECK(data_.get());
28 typedef struct {
29 WORD language;
30 WORD code_page;
31 } LanguageAndCodePage;
33 // static
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))
41 return NULL;
43 FilePath app_path(system_buffer);
44 return CreateFileVersionInfo(app_path);
47 // static
48 FileVersionInfo* FileVersionInfo::CreateFileVersionInfo(
49 const FilePath& file_path) {
50 base::ThreadRestrictions::AssertIOAllowed();
52 DWORD dummy;
53 const wchar_t* path = file_path.value().c_str();
54 DWORD length = ::GetFileVersionInfoSize(path, &dummy);
55 if (length == 0)
56 return NULL;
58 void* data = calloc(length, 1);
59 if (!data)
60 return NULL;
62 if (!::GetFileVersionInfo(path, dummy, length, data)) {
63 free(data);
64 return NULL;
67 LanguageAndCodePage* translate = NULL;
68 uint32 page_count;
69 BOOL query_result = VerQueryValue(data, L"\\VarFileInfo\\Translation",
70 (void**) &translate, &page_count);
72 if (query_result && translate) {
73 return new FileVersionInfoWin(data, translate->language,
74 translate->code_page);
76 } else {
77 free(data);
78 return NULL;
82 string16 FileVersionInfoWin::company_name() {
83 return GetStringValue(L"CompanyName");
86 string16 FileVersionInfoWin::company_short_name() {
87 return GetStringValue(L"CompanyShortName");
90 string16 FileVersionInfoWin::internal_name() {
91 return GetStringValue(L"InternalName");
94 string16 FileVersionInfoWin::product_name() {
95 return GetStringValue(L"ProductName");
98 string16 FileVersionInfoWin::product_short_name() {
99 return GetStringValue(L"ProductShortName");
102 string16 FileVersionInfoWin::comments() {
103 return GetStringValue(L"Comments");
106 string16 FileVersionInfoWin::legal_copyright() {
107 return GetStringValue(L"LegalCopyright");
110 string16 FileVersionInfoWin::product_version() {
111 return GetStringValue(L"ProductVersion");
114 string16 FileVersionInfoWin::file_description() {
115 return GetStringValue(L"FileDescription");
118 string16 FileVersionInfoWin::legal_trademarks() {
119 return GetStringValue(L"LegalTrademarks");
122 string16 FileVersionInfoWin::private_build() {
123 return GetStringValue(L"PrivateBuild");
126 string16 FileVersionInfoWin::file_version() {
127 return GetStringValue(L"FileVersion");
130 string16 FileVersionInfoWin::original_filename() {
131 return GetStringValue(L"OriginalFilename");
134 string16 FileVersionInfoWin::special_build() {
135 return GetStringValue(L"SpecialBuild");
138 string16 FileVersionInfoWin::last_change() {
139 return GetStringValue(L"LastChange");
142 bool FileVersionInfoWin::is_official_build() {
143 return (GetStringValue(L"Official Build").compare(L"1") == 0);
146 bool FileVersionInfoWin::GetValue(const wchar_t* name,
147 std::wstring* value_str) {
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;
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,
169 L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name);
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;
178 return false;
181 std::wstring FileVersionInfoWin::GetStringValue(const wchar_t* name) {
182 std::wstring str;
183 if (GetValue(name, &str))
184 return str;
185 else
186 return L"";