Commit 4bb23ded authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Tidy FileVersionInfoWin.

- Refactor GetValue() to use an existing struct.
- Make the parameter names in the .cc / .h files match for GetValue().
- Mark GetValue() and GetStringValue() const.
- Make all ::VerQueryValue() callers consistent.
- Add a comment to note |fixed_file_info_| is never nullptr.
- Fix various nits.

Change-Id: I4e73bf5791045005c2cfdeb6a039815c39e953ce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1837490
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: default avatarGreg Thompson <grt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702890}
parent 302a0869
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include <windows.h> #include <windows.h>
#include <stddef.h> #include <stddef.h>
#include <utility>
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
...@@ -15,8 +17,6 @@ ...@@ -15,8 +17,6 @@
#include "base/threading/scoped_blocking_call.h" #include "base/threading/scoped_blocking_call.h"
#include "base/win/resource_util.h" #include "base/win/resource_util.h"
using base::FilePath;
namespace { namespace {
struct LanguageAndCodePage { struct LanguageAndCodePage {
...@@ -24,26 +24,23 @@ struct LanguageAndCodePage { ...@@ -24,26 +24,23 @@ struct LanguageAndCodePage {
WORD code_page; WORD code_page;
}; };
// Returns the \\VarFileInfo\\Translation value extracted from the // Returns the \VarFileInfo\Translation value extracted from the
// VS_VERSION_INFO resource in |data|. // VS_VERSION_INFO resource in |data|.
LanguageAndCodePage* GetTranslate(const void* data) { LanguageAndCodePage* GetTranslate(const void* data) {
LanguageAndCodePage* translate = nullptr; static constexpr wchar_t kTranslation[] = L"\\VarFileInfo\\Translation";
UINT length; LPVOID translate = nullptr;
if (::VerQueryValue(data, L"\\VarFileInfo\\Translation", UINT dummy_size;
reinterpret_cast<void**>(&translate), &length)) { if (::VerQueryValue(data, kTranslation, &translate, &dummy_size))
return translate; return static_cast<LanguageAndCodePage*>(translate);
}
return nullptr; return nullptr;
} }
VS_FIXEDFILEINFO* GetVsFixedFileInfo(const void* data) { const VS_FIXEDFILEINFO& GetVsFixedFileInfo(const void* data) {
VS_FIXEDFILEINFO* fixed_file_info = nullptr; static constexpr wchar_t kRoot[] = L"\\";
UINT length; LPVOID fixed_file_info = nullptr;
if (::VerQueryValue(data, L"\\", reinterpret_cast<void**>(&fixed_file_info), UINT dummy_size;
&length)) { CHECK(::VerQueryValue(data, kRoot, &fixed_file_info, &dummy_size));
return fixed_file_info; return *static_cast<VS_FIXEDFILEINFO*>(fixed_file_info);
}
return nullptr;
} }
} // namespace } // namespace
...@@ -70,13 +67,13 @@ FileVersionInfo::CreateFileVersionInfoForModule(HMODULE module) { ...@@ -70,13 +67,13 @@ FileVersionInfo::CreateFileVersionInfoForModule(HMODULE module) {
// static // static
std::unique_ptr<FileVersionInfo> FileVersionInfo::CreateFileVersionInfo( std::unique_ptr<FileVersionInfo> FileVersionInfo::CreateFileVersionInfo(
const FilePath& file_path) { const base::FilePath& file_path) {
return FileVersionInfoWin::CreateFileVersionInfoWin(file_path); return FileVersionInfoWin::CreateFileVersionInfoWin(file_path);
} }
// static // static
std::unique_ptr<FileVersionInfoWin> std::unique_ptr<FileVersionInfoWin>
FileVersionInfoWin::CreateFileVersionInfoWin(const FilePath& file_path) { FileVersionInfoWin::CreateFileVersionInfoWin(const base::FilePath& file_path) {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK); base::BlockingType::MAY_BLOCK);
...@@ -140,55 +137,46 @@ base::string16 FileVersionInfoWin::special_build() { ...@@ -140,55 +137,46 @@ base::string16 FileVersionInfoWin::special_build() {
} }
bool FileVersionInfoWin::GetValue(const base::char16* name, bool FileVersionInfoWin::GetValue(const base::char16* name,
base::string16* value_str) { base::string16* value) const {
WORD lang_codepage[8]; const struct LanguageAndCodePage lang_codepages[] = {
size_t i = 0;
// Use the language and codepage from the DLL. // Use the language and codepage from the DLL.
lang_codepage[i++] = language_; {language_, code_page_},
lang_codepage[i++] = code_page_;
// Use the default language and codepage from the DLL. // Use the default language and codepage from the DLL.
lang_codepage[i++] = ::GetUserDefaultLangID(); {::GetUserDefaultLangID(), code_page_},
lang_codepage[i++] = code_page_;
// Use the language from the DLL and Latin codepage (most common). // Use the language from the DLL and Latin codepage (most common).
lang_codepage[i++] = language_; {language_, 1252},
lang_codepage[i++] = 1252;
// Use the default language and Latin codepage (most common). // Use the default language and Latin codepage (most common).
lang_codepage[i++] = ::GetUserDefaultLangID(); {::GetUserDefaultLangID(), 1252},
lang_codepage[i++] = 1252; };
i = 0; for (const auto& lang_codepage : lang_codepages) {
while (i < base::size(lang_codepage)) {
wchar_t sub_block[MAX_PATH]; wchar_t sub_block[MAX_PATH];
WORD language = lang_codepage[i++];
WORD code_page = lang_codepage[i++];
_snwprintf_s(sub_block, MAX_PATH, MAX_PATH, _snwprintf_s(sub_block, MAX_PATH, MAX_PATH,
L"\\StringFileInfo\\%04x%04x\\%ls", language, code_page, L"\\StringFileInfo\\%04x%04x\\%ls", lang_codepage.language,
base::as_wcstr(name)); lang_codepage.code_page, base::as_wcstr(name));
LPVOID value = NULL; LPVOID value_ptr = nullptr;
uint32_t size; uint32_t size;
BOOL r = ::VerQueryValue(data_, sub_block, &value, &size); BOOL r = ::VerQueryValue(data_, sub_block, &value_ptr, &size);
if (r && value) { if (r && value_ptr && size) {
value_str->assign(static_cast<base::char16*>(value)); value->assign(static_cast<base::char16*>(value_ptr), size - 1);
return true; return true;
} }
} }
return false; return false;
} }
base::string16 FileVersionInfoWin::GetStringValue(const base::char16* name) { base::string16 FileVersionInfoWin::GetStringValue(
const base::char16* name) const {
base::string16 str; base::string16 str;
if (GetValue(name, &str)) GetValue(name, &str);
return str; return str;
else
return base::string16();
} }
base::Version FileVersionInfoWin::GetFileVersion() const { base::Version FileVersionInfoWin::GetFileVersion() const {
return base::Version( return base::Version({HIWORD(fixed_file_info_.dwFileVersionMS),
std::vector<uint32_t>{HIWORD(fixed_file_info_->dwFileVersionMS), LOWORD(fixed_file_info_.dwFileVersionMS),
LOWORD(fixed_file_info_->dwFileVersionMS), HIWORD(fixed_file_info_.dwFileVersionLS),
HIWORD(fixed_file_info_->dwFileVersionLS), LOWORD(fixed_file_info_.dwFileVersionLS)});
LOWORD(fixed_file_info_->dwFileVersionLS)});
} }
FileVersionInfoWin::FileVersionInfoWin(std::vector<uint8_t>&& data, FileVersionInfoWin::FileVersionInfoWin(std::vector<uint8_t>&& data,
......
...@@ -38,12 +38,13 @@ class BASE_EXPORT FileVersionInfoWin : public FileVersionInfo { ...@@ -38,12 +38,13 @@ class BASE_EXPORT FileVersionInfoWin : public FileVersionInfo {
base::string16 file_description() override; base::string16 file_description() override;
base::string16 file_version() override; base::string16 file_version() override;
// Lets you access other properties not covered above. // Lets you access other properties not covered above. |value| is only
bool GetValue(const base::char16* name, base::string16* value); // modified if GetValue() returns true.
bool GetValue(const base::char16* name, base::string16* value) const;
// Similar to GetValue but returns a string16 (empty string if the property // Similar to GetValue but returns a string16 (empty string if the property
// does not exist). // does not exist).
base::string16 GetStringValue(const base::char16* name); base::string16 GetStringValue(const base::char16* name) const;
// Get file version number in dotted version format. // Get file version number in dotted version format.
base::Version GetFileVersion() const; base::Version GetFileVersion() const;
...@@ -67,8 +68,8 @@ class BASE_EXPORT FileVersionInfoWin : public FileVersionInfo { ...@@ -67,8 +68,8 @@ class BASE_EXPORT FileVersionInfoWin : public FileVersionInfo {
const WORD language_; const WORD language_;
const WORD code_page_; const WORD code_page_;
// This is a pointer into |data_| if it exists. Otherwise nullptr. // This is a reference for a portion of |data_|.
const VS_FIXEDFILEINFO* const fixed_file_info_; const VS_FIXEDFILEINFO& fixed_file_info_;
DISALLOW_COPY_AND_ASSIGN(FileVersionInfoWin); DISALLOW_COPY_AND_ASSIGN(FileVersionInfoWin);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment