Skip to content

Commit

Permalink
Remove more windows.h includes from headers
Browse files Browse the repository at this point in the history
file_enumerator.h, process_singleton.h, and clipboard_format_type.h all
include windows.h. This causes windows.h to be pulled in to many source
files that don't otherwise need it, with all of the namespace pollution
from macros that windows.h implies.

Bug: 796644
Change-Id: Ie7ddfdf51b7bd1fb4374a4988fc2dd0e75b4a26e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3025478
Reviewed-by: Peter Boström <[email protected]>
Reviewed-by: Lei Zhang <[email protected]>
Commit-Queue: Bruce Dawson <[email protected]>
Cr-Commit-Position: refs/heads/master@{#902271}
  • Loading branch information
randomascii authored and Chromium LUCI CQ committed Jul 16, 2021
1 parent e4f01e0 commit 6457cf5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
14 changes: 10 additions & 4 deletions base/files/file_enumerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "build/build_config.h"

#if defined(OS_WIN)
#include <windows.h>
#include "base/win/windows_types.h"
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
#include <unistd.h>
#include <unordered_set>
Expand Down Expand Up @@ -61,7 +61,9 @@ class BASE_EXPORT FileEnumerator {
// Note that the cAlternateFileName (used to hold the "short" 8.3 name)
// of the WIN32_FIND_DATA will be empty. Since we don't use short file
// names, we tell Windows to omit it which speeds up the query slightly.
const WIN32_FIND_DATA& find_data() const { return find_data_; }
const WIN32_FIND_DATA& find_data() const {
return *ChromeToWindowsType(&find_data_);
}
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
const stat_wrapper_t& stat() const { return stat_; }
#endif
Expand All @@ -70,7 +72,7 @@ class BASE_EXPORT FileEnumerator {
friend class FileEnumerator;

#if defined(OS_WIN)
WIN32_FIND_DATA find_data_;
CHROME_WIN32_FIND_DATA find_data_;
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
stat_wrapper_t stat_;
FilePath filename_;
Expand Down Expand Up @@ -177,9 +179,13 @@ class BASE_EXPORT FileEnumerator {
bool IsPatternMatched(const FilePath& src) const;

#if defined(OS_WIN)
const WIN32_FIND_DATA& find_data() const {
return *ChromeToWindowsType(&find_data_);
}

// True when find_data_ is valid.
bool has_find_data_ = false;
WIN32_FIND_DATA find_data_;
CHROME_WIN32_FIND_DATA find_data_;
HANDLE find_handle_ = INVALID_HANDLE_VALUE;
#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
// The files in the current directory
Expand Down
21 changes: 11 additions & 10 deletions base/files/file_enumerator_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ FileEnumerator::FileInfo::FileInfo() {
}

bool FileEnumerator::FileInfo::IsDirectory() const {
return (find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
return (find_data().dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}

FilePath FileEnumerator::FileInfo::GetName() const {
return FilePath(find_data_.cFileName);
return FilePath(find_data().cFileName);
}

int64_t FileEnumerator::FileInfo::GetSize() const {
ULARGE_INTEGER size;
size.HighPart = find_data_.nFileSizeHigh;
size.LowPart = find_data_.nFileSizeLow;
size.HighPart = find_data().nFileSizeHigh;
size.LowPart = find_data().nFileSizeLow;
DCHECK_LE(size.QuadPart,
static_cast<ULONGLONG>(std::numeric_limits<int64_t>::max()));
return static_cast<int64_t>(size.QuadPart);
}

Time FileEnumerator::FileInfo::GetLastModifiedTime() const {
return Time::FromFileTime(find_data_.ftLastWriteTime);
return Time::FromFileTime(find_data().ftLastWriteTime);
}

// FileEnumerator --------------------------------------------------------------
Expand Down Expand Up @@ -140,12 +140,13 @@ FilePath FileEnumerator::Next() {
BuildSearchFilter(folder_search_policy_, root_path_, pattern_);
find_handle_ = FindFirstFileEx(src.value().c_str(),
FindExInfoBasic, // Omit short name.
&find_data_, FindExSearchNameMatch,
nullptr, FIND_FIRST_EX_LARGE_FETCH);
ChromeToWindowsType(&find_data_),
FindExSearchNameMatch, nullptr,
FIND_FIRST_EX_LARGE_FETCH);
has_find_data_ = true;
} else {
// Search for the next file/directory.
if (!FindNextFile(find_handle_, &find_data_)) {
if (!FindNextFile(find_handle_, ChromeToWindowsType(&find_data_))) {
FindClose(find_handle_);
find_handle_ = INVALID_HANDLE_VALUE;
}
Expand Down Expand Up @@ -175,12 +176,12 @@ FilePath FileEnumerator::Next() {
return FilePath();
}

const FilePath filename(find_data_.cFileName);
const FilePath filename(find_data().cFileName);
if (ShouldSkip(filename))
continue;

const bool is_dir =
(find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
(find_data().dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
const FilePath abs_path = root_path_.Append(filename);

// Check if directory should be processed recursive.
Expand Down
23 changes: 23 additions & 0 deletions base/win/windows_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ typedef WIN32_FIND_DATAW WIN32_FIND_DATA;
// declare the Windows types so we declare our types and cast to the Windows
// types in a few places. The sizes must match the Windows types so we verify
// that with static asserts in win_includes_unittest.cc.
// ChromeToWindowsType functions are provided for pointer conversions.

struct CHROME_SRWLOCK {
PVOID Ptr;
Expand Down Expand Up @@ -285,6 +286,28 @@ WINBASEAPI HLOCAL WINAPI LocalFree(_In_ HLOCAL hMem);

#ifdef __cplusplus
}

// Helper functions for converting between Chrome and Windows native versions of
// type pointers.
// Overloaded functions must be declared outside of the extern "C" block.

inline WIN32_FIND_DATA* ChromeToWindowsType(CHROME_WIN32_FIND_DATA* p) {
return reinterpret_cast<WIN32_FIND_DATA*>(p);
}

inline const WIN32_FIND_DATA* ChromeToWindowsType(
const CHROME_WIN32_FIND_DATA* p) {
return reinterpret_cast<const WIN32_FIND_DATA*>(p);
}

inline FORMATETC* ChromeToWindowsType(CHROME_FORMATETC* p) {
return reinterpret_cast<FORMATETC*>(p);
}

inline const FORMATETC* ChromeToWindowsType(const CHROME_FORMATETC* p) {
return reinterpret_cast<const FORMATETC*>(p);
}

#endif

// These macros are all defined by windows.h and are also used as the names of
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/process_singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "build/build_config.h"

#if defined(OS_WIN)
#include <windows.h>
#include "base/win/windows_types.h"
#endif // defined(OS_WIN)

#include "base/callback.h"
Expand Down
6 changes: 3 additions & 3 deletions ui/base/clipboard/clipboard_format_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "build/build_config.h"

#if defined(OS_WIN)
#include <objidl.h>
#include "base/win/windows_types.h"
#endif

#if defined(OS_APPLE)
Expand Down Expand Up @@ -103,7 +103,7 @@ class COMPONENT_EXPORT(UI_BASE_CLIPBOARD_TYPES) ClipboardFormatType {
// if the format isn't found.
std::string GetName() const;
#if defined(OS_WIN)
const FORMATETC& ToFormatEtc() const { return data_; }
const FORMATETC& ToFormatEtc() const { return *ChromeToWindowsType(&data_); }
#elif defined(OS_APPLE)
NSString* ToNSString() const { return data_; }
// Custom copy and assignment constructor to handle NSString.
Expand Down Expand Up @@ -139,7 +139,7 @@ class COMPONENT_EXPORT(UI_BASE_CLIPBOARD_TYPES) ClipboardFormatType {

// FORMATETC:
// https://docs.microsoft.com/en-us/windows/desktop/com/the-formatetc-structure
FORMATETC data_;
CHROME_FORMATETC data_;
#elif defined(USE_AURA) || defined(OS_ANDROID) || defined(OS_FUCHSIA)
explicit ClipboardFormatType(const std::string& native_format);
std::string data_;
Expand Down

0 comments on commit 6457cf5

Please sign in to comment.