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

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
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
17 namespace {
18
19 struct LanguageAndCodePage {
20 WORD language;
21 WORD code_page;
22 };
23
24 // Returns the \\VarFileInfo\\Translation value extracted from the
25 // VS_VERSION_INFO resource in |data|.
26 LanguageAndCodePage* GetTranslate(void* data) {
27 LanguageAndCodePage* translate;
28 uint32_t page_count;
29 if (::VerQueryValue(data, L"\\VarFileInfo\\Translation", (void**)&translate,
30 &page_count)) {
31 return translate;
32 }
33 return nullptr;
34 }
35
36 } // namespace
37
38 FileVersionInfoWin::FileVersionInfoWin(std::vector<uint8_t> data,
grt (UTC plus 2) 2016/06/17 20:34:59 nit: move ctors down in the file so that the order
fdoray 2016/06/20 14:06:41 Done.
39 WORD language,
40 WORD code_page)
41 : FileVersionInfoWin(data.data(), language, code_page) {
grt (UTC plus 2) 2016/06/17 20:34:59 , owned_data_(std::move(data)) {
fdoray 2016/06/20 14:06:41 Doesn't work error C3511: 'FileVersionInfoWin': a
grt (UTC plus 2) 2016/06/20 20:05:14 TIL...
42 owned_data_ = std::move(data);
43 }
44
19 FileVersionInfoWin::FileVersionInfoWin(void* data, 45 FileVersionInfoWin::FileVersionInfoWin(void* data,
20 WORD language, 46 WORD language,
21 WORD code_page) 47 WORD code_page)
22 : language_(language), code_page_(code_page) { 48 : data_(data), language_(language), code_page_(code_page) {
23 base::ThreadRestrictions::AssertIOAllowed(); 49 DCHECK(data_);
24 data_.reset((char*) data);
25 fixed_file_info_ = NULL;
26 UINT size; 50 UINT size;
27 ::VerQueryValue(data_.get(), L"\\", (LPVOID*)&fixed_file_info_, &size); 51 ::VerQueryValue(data_, L"\\", (LPVOID*)&fixed_file_info_, &size);
28 } 52 }
29 53
30 FileVersionInfoWin::~FileVersionInfoWin() { 54 FileVersionInfoWin::~FileVersionInfoWin() = default;
31 DCHECK(data_.get());
32 }
33
34 typedef struct {
35 WORD language;
36 WORD code_page;
37 } LanguageAndCodePage;
38 55
39 // static 56 // static
40 FileVersionInfo* FileVersionInfo::CreateFileVersionInfoForModule( 57 FileVersionInfo* FileVersionInfo::CreateFileVersionInfoForModule(
41 HMODULE module) { 58 HMODULE module) {
42 // Note that the use of MAX_PATH is basically in line with what we do for 59 void* data;
43 // all registered paths (PathProviderWin). 60 size_t version_info_length;
44 wchar_t system_buffer[MAX_PATH]; 61 const bool has_version_resource = base::win::GetResourceFromModule(
45 system_buffer[0] = 0; 62 module, VS_VERSION_INFO, RT_VERSION, &data, &version_info_length);
46 if (!GetModuleFileName(module, system_buffer, MAX_PATH)) 63 if (!has_version_resource)
47 return NULL; 64 return nullptr;
48 65
49 FilePath app_path(system_buffer); 66 const LanguageAndCodePage* translate = GetTranslate(data);
50 return CreateFileVersionInfo(app_path); 67 if (!translate)
68 return nullptr;
69
70 return new FileVersionInfoWin(data, translate->language,
71 translate->code_page);
51 } 72 }
52 73
53 // static 74 // static
54 FileVersionInfo* FileVersionInfo::CreateFileVersionInfo( 75 FileVersionInfo* FileVersionInfo::CreateFileVersionInfo(
55 const FilePath& file_path) { 76 const FilePath& file_path) {
56 base::ThreadRestrictions::AssertIOAllowed(); 77 base::ThreadRestrictions::AssertIOAllowed();
57 78
58 DWORD dummy; 79 DWORD dummy;
59 const wchar_t* path = file_path.value().c_str(); 80 const wchar_t* path = file_path.value().c_str();
60 DWORD length = ::GetFileVersionInfoSize(path, &dummy); 81 DWORD length = ::GetFileVersionInfoSize(path, &dummy);
61 if (length == 0) 82 if (length == 0)
62 return NULL; 83 return nullptr;
63 84
64 void* data = calloc(length, 1); 85 std::vector<uint8_t> data(length, 0);
65 if (!data)
66 return NULL;
67 86
68 if (!::GetFileVersionInfo(path, dummy, length, data)) { 87 if (!::GetFileVersionInfo(path, dummy, data.size(), data.data()))
69 free(data); 88 return nullptr;
70 return NULL;
71 }
72 89
73 LanguageAndCodePage* translate = NULL; 90 const LanguageAndCodePage* translate = GetTranslate(data.data());
74 uint32_t page_count; 91 if (!translate)
75 BOOL query_result = VerQueryValue(data, L"\\VarFileInfo\\Translation", 92 return nullptr;
76 (void**) &translate, &page_count);
77 93
78 if (query_result && translate) { 94 return new FileVersionInfoWin(std::move(data), translate->language,
79 return new FileVersionInfoWin(data, translate->language, 95 translate->code_page);
80 translate->code_page);
81
82 } else {
83 free(data);
84 return NULL;
85 }
86 } 96 }
87 97
88 base::string16 FileVersionInfoWin::company_name() { 98 base::string16 FileVersionInfoWin::company_name() {
89 return GetStringValue(L"CompanyName"); 99 return GetStringValue(L"CompanyName");
90 } 100 }
91 101
92 base::string16 FileVersionInfoWin::company_short_name() { 102 base::string16 FileVersionInfoWin::company_short_name() {
93 return GetStringValue(L"CompanyShortName"); 103 return GetStringValue(L"CompanyShortName");
94 } 104 }
95 105
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 178
169 i = 0; 179 i = 0;
170 while (i < arraysize(lang_codepage)) { 180 while (i < arraysize(lang_codepage)) {
171 wchar_t sub_block[MAX_PATH]; 181 wchar_t sub_block[MAX_PATH];
172 WORD language = lang_codepage[i++]; 182 WORD language = lang_codepage[i++];
173 WORD code_page = lang_codepage[i++]; 183 WORD code_page = lang_codepage[i++];
174 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH, 184 _snwprintf_s(sub_block, MAX_PATH, MAX_PATH,
175 L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name); 185 L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, name);
176 LPVOID value = NULL; 186 LPVOID value = NULL;
177 uint32_t size; 187 uint32_t size;
178 BOOL r = ::VerQueryValue(data_.get(), sub_block, &value, &size); 188 BOOL r = ::VerQueryValue(data_, sub_block, &value, &size);
179 if (r && value) { 189 if (r && value) {
180 value_str->assign(static_cast<wchar_t*>(value)); 190 value_str->assign(static_cast<wchar_t*>(value));
181 return true; 191 return true;
182 } 192 }
183 } 193 }
184 return false; 194 return false;
185 } 195 }
186 196
187 std::wstring FileVersionInfoWin::GetStringValue(const wchar_t* name) { 197 std::wstring FileVersionInfoWin::GetStringValue(const wchar_t* name) {
188 std::wstring str; 198 std::wstring str;
189 if (GetValue(name, &str)) 199 if (GetValue(name, &str))
190 return str; 200 return str;
191 else 201 else
192 return L""; 202 return L"";
193 } 203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698