Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(156)

Side by Side Diff: base/file_version_info_win.cc

Issue 2046583002: Don't use ::GetFileVersionInfo() in CreateFileVersionInfoForModule() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self-review Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/file_version_info_win.h ('k') | base/file_version_info_win_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_version_info_win.h" 5 #include "base/file_version_info_win.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h>
10 9
11 #include "base/file_version_info.h"
12 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
13 #include "base/logging.h" 11 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "base/threading/thread_restrictions.h" 12 #include "base/threading/thread_restrictions.h"
13 #include "base/win/resource_util.h"
16 14
17 using base::FilePath; 15 using base::FilePath;
18 16
19 FileVersionInfoWin::FileVersionInfoWin(void* data, 17 namespace {
20 WORD language, 18
21 WORD code_page) 19 struct LanguageAndCodePage {
22 : language_(language), code_page_(code_page) { 20 WORD language;
23 base::ThreadRestrictions::AssertIOAllowed(); 21 WORD code_page;
24 data_.reset((char*) data); 22 };
25 fixed_file_info_ = NULL; 23
26 UINT size; 24 // Returns the \\VarFileInfo\\Translation value extracted from the
27 ::VerQueryValue(data_.get(), L"\\", (LPVOID*)&fixed_file_info_, &size); 25 // VS_VERSION_INFO resource in |data|.
26 LanguageAndCodePage* GetTranslate(const void* data) {
27 LanguageAndCodePage* translate = nullptr;
28 UINT length;
29 if (::VerQueryValue(data, L"\\VarFileInfo\\Translation",
30 reinterpret_cast<void**>(&translate), &length)) {
31 return translate;
32 }
33 return nullptr;
28 } 34 }
29 35
30 FileVersionInfoWin::~FileVersionInfoWin() { 36 VS_FIXEDFILEINFO* GetVsFixedFileInfo(const void* data) {
31 DCHECK(data_.get()); 37 VS_FIXEDFILEINFO* fixed_file_info = nullptr;
38 UINT length;
39 if (::VerQueryValue(data, L"\\", reinterpret_cast<void**>(&fixed_file_info),
40 &length)) {
41 return fixed_file_info;
42 }
43 return nullptr;
32 } 44 }
33 45
34 typedef struct { 46 } // namespace
35 WORD language; 47
36 WORD code_page; 48 FileVersionInfoWin::~FileVersionInfoWin() = default;
37 } LanguageAndCodePage;
38 49
39 // static 50 // static
40 FileVersionInfo* FileVersionInfo::CreateFileVersionInfoForModule( 51 FileVersionInfo* FileVersionInfo::CreateFileVersionInfoForModule(
41 HMODULE module) { 52 HMODULE module) {
42 // Note that the use of MAX_PATH is basically in line with what we do for 53 void* data;
43 // all registered paths (PathProviderWin). 54 size_t version_info_length;
44 wchar_t system_buffer[MAX_PATH]; 55 const bool has_version_resource = base::win::GetResourceFromModule(
45 system_buffer[0] = 0; 56 module, VS_VERSION_INFO, RT_VERSION, &data, &version_info_length);
46 if (!GetModuleFileName(module, system_buffer, MAX_PATH)) 57 if (!has_version_resource)
47 return NULL; 58 return nullptr;
48 59
49 FilePath app_path(system_buffer); 60 const LanguageAndCodePage* translate = GetTranslate(data);
50 return CreateFileVersionInfo(app_path); 61 if (!translate)
62 return nullptr;
63
64 return new FileVersionInfoWin(data, translate->language,
65 translate->code_page);
51 } 66 }
52 67
53 // static 68 // static
54 FileVersionInfo* FileVersionInfo::CreateFileVersionInfo( 69 FileVersionInfo* FileVersionInfo::CreateFileVersionInfo(
55 const FilePath& file_path) { 70 const FilePath& file_path) {
56 base::ThreadRestrictions::AssertIOAllowed(); 71 base::ThreadRestrictions::AssertIOAllowed();
57 72
58 DWORD dummy; 73 DWORD dummy;
59 const wchar_t* path = file_path.value().c_str(); 74 const wchar_t* path = file_path.value().c_str();
60 DWORD length = ::GetFileVersionInfoSize(path, &dummy); 75 DWORD length = ::GetFileVersionInfoSize(path, &dummy);
61 if (length == 0) 76 if (length == 0)
62 return NULL; 77 return nullptr;
63 78
64 void* data = calloc(length, 1); 79 std::vector<uint8_t> data(length, 0);
65 if (!data)
66 return NULL;
67 80
68 if (!::GetFileVersionInfo(path, dummy, length, data)) { 81 if (!::GetFileVersionInfo(path, dummy, data.size(), data.data()))
69 free(data); 82 return nullptr;
70 return NULL;
71 }
72 83
73 LanguageAndCodePage* translate = NULL; 84 const LanguageAndCodePage* translate = GetTranslate(data.data());
74 uint32_t page_count; 85 if (!translate)
75 BOOL query_result = VerQueryValue(data, L"\\VarFileInfo\\Translation", 86 return nullptr;
76 (void**) &translate, &page_count);
77 87
78 if (query_result && translate) { 88 return new FileVersionInfoWin(std::move(data), translate->language,
79 return new FileVersionInfoWin(data, translate->language, 89 translate->code_page);
80 translate->code_page);
81
82 } else {
83 free(data);
84 return NULL;
85 }
86 } 90 }
87 91
88 base::string16 FileVersionInfoWin::company_name() { 92 base::string16 FileVersionInfoWin::company_name() {
89 return GetStringValue(L"CompanyName"); 93 return GetStringValue(L"CompanyName");
90 } 94 }
91 95
92 base::string16 FileVersionInfoWin::company_short_name() { 96 base::string16 FileVersionInfoWin::company_short_name() {
93 return GetStringValue(L"CompanyShortName"); 97 return GetStringValue(L"CompanyShortName");
94 } 98 }
95 99
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 172
169 i = 0; 173 i = 0;
170 while (i < arraysize(lang_codepage)) { 174 while (i < arraysize(lang_codepage)) {
171 wchar_t sub_block[MAX_PATH]; 175 wchar_t sub_block[MAX_PATH];
172 WORD language = lang_codepage[i++]; 176 WORD language = lang_codepage[i++];
173 WORD code_page = lang_codepage[i++]; 177 WORD code_page = lang_codepage[i++];
174 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH, 178 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH,
175 L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name); 179 L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name);
176 LPVOID value = NULL; 180 LPVOID value = NULL;
177 uint32_t size; 181 uint32_t size;
178 BOOL r = ::VerQueryValue(data_.get(), sub_block, &value, &size); 182 BOOL r = ::VerQueryValue(data_, sub_block, &value, &size);
179 if (r && value) { 183 if (r && value) {
180 value_str->assign(static_cast<wchar_t*>(value)); 184 value_str->assign(static_cast<wchar_t*>(value));
181 return true; 185 return true;
182 } 186 }
183 } 187 }
184 return false; 188 return false;
185 } 189 }
186 190
187 std::wstring FileVersionInfoWin::GetStringValue(const wchar_t* name) { 191 std::wstring FileVersionInfoWin::GetStringValue(const wchar_t* name) {
188 std::wstring str; 192 std::wstring str;
189 if (GetValue(name, &str)) 193 if (GetValue(name, &str))
190 return str; 194 return str;
191 else 195 else
192 return L""; 196 return L"";
193 } 197 }
198
199 FileVersionInfoWin::FileVersionInfoWin(std::vector<uint8_t> data,
200 WORD language,
201 WORD code_page)
202 : owned_data_(std::move(data)),
203 data_(owned_data_.data()),
204 language_(language),
205 code_page_(code_page),
206 fixed_file_info_(GetVsFixedFileInfo(data_)) {}
grt (UTC plus 2) 2016/06/22 14:09:43 should this DCHECK(!owned_data_.empty());?
fdoray 2016/06/22 17:20:19 Done.
207
208 FileVersionInfoWin::FileVersionInfoWin(void* data,
209 WORD language,
210 WORD code_page)
211 : data_(data),
212 language_(language),
213 code_page_(code_page),
214 fixed_file_info_(GetVsFixedFileInfo(data)) {
215 DCHECK(data_);
216 }
OLDNEW
« no previous file with comments | « base/file_version_info_win.h ('k') | base/file_version_info_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698