[Windows] Do not include string terminator in GetWindowText
WindowEnumerator::GetWindowText was including the trailing string
terminator in the body of the returned string. This CL fixes that as
well as makes several other changes:
- WindowEnumeratorTest.EnumerateTopLevelWindows had the same bug in its
independent implementation of GetWindowText. It is also fixed.
- WindowEnumerator has been whittled down to a single function, so
there's no need for a class anymore. It's now simply
base::win::EnumerateChildWindows, which is a little easier to use.
- base/win/window_enumerator.h no longer includes windows.h.
- GetWindowText has been renamed to GetWindowTextString so that it
doesn't accidentally get renamed to GetWindowTextW by windows.h.
- GetWindowTextString also handles the case where the actual text is
fewer characters than reported by GetWindowTextLength. MSDN explains a
few cases where this could happen.
Bug: 1482637
Change-Id: I8cb9b5706fb43bdb10e79afc9c832d9a4e5ee6af
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4868390
Auto-Submit: Greg Thompson <grt@chromium.org>
Reviewed-by: S Ganesh <ganesh@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1197669}
diff --git a/base/win/window_enumerator.h b/base/win/window_enumerator.h
index ee68ce40..663df52 100644
--- a/base/win/window_enumerator.h
+++ b/base/win/window_enumerator.h
@@ -5,54 +5,34 @@
#ifndef BASE_WIN_WINDOW_ENUMERATOR_H_
#define BASE_WIN_WINDOW_ENUMERATOR_H_
-#include <windows.h>
-
#include <string>
#include "base/base_export.h"
#include "base/functional/callback.h"
+#include "base/win/windows_types.h"
namespace base::win {
-// Enumerates immediate child windows of `parent`, and calls `filter.Run` for
-// each window:
-// * If `filter.Run` returns `false`, continues enumerating.
-// * If `filter.Run` returns `true`, stops enumerating.
-class BASE_EXPORT WindowEnumerator {
- public:
- WindowEnumerator(HWND parent,
- base::RepeatingCallback<bool(HWND hwnd)> filter);
- WindowEnumerator(const WindowEnumerator&) = delete;
- WindowEnumerator& operator=(const WindowEnumerator&) = delete;
- ~WindowEnumerator();
+// Enumerates immediate child windows of `parent`, running `filter` for each
+// window until `filter` returns true.
+using WindowEnumeratorCallback = base::RepeatingCallback<bool(HWND hwnd)>;
+BASE_EXPORT void EnumerateChildWindows(HWND parent,
+ WindowEnumeratorCallback filter);
- void Run() const;
+// Returns true if `hwnd` is an always-on-top window.
+BASE_EXPORT bool IsTopmostWindow(HWND hwnd);
- // Returns true if `hwnd` is an always-on-top window.
- static bool IsTopmostWindow(HWND hwnd);
+// Returns true if `hwnd` is a system dialog.
+BASE_EXPORT bool IsSystemDialog(HWND hwnd);
- // Returns true if `hwnd` is a system dialog.
- static bool IsSystemDialog(HWND hwnd);
+// Returns true if `hwnd` is a window owned by the Windows shell.
+BASE_EXPORT bool IsShellWindow(HWND hwnd);
- // Returns true if `hwnd` is a window owned by the Windows shell.
- static bool IsShellWindow(HWND hwnd);
+// Returns the class name of `hwnd` or an empty string on error.
+BASE_EXPORT std::wstring GetWindowClass(HWND hwnd);
- // Returns the class name of `hwnd` or an empty string on error.
- static std::wstring GetWindowClass(HWND hwnd);
-
- // Returns the window text for `hwnd`, or an empty string on error.
- static std::wstring GetWindowText(HWND hwnd);
-
- private:
- // Main processing function run for each window.
- bool OnWindow(HWND hwnd) const;
-
- // An EnumWindowsProc invoked by EnumWindows once for each window.
- static BOOL CALLBACK OnWindowProc(HWND hwnd, LPARAM lparam);
-
- const HWND parent_;
- base::RepeatingCallback<bool(HWND hwnd)> filter_;
-};
+// Returns the window text for `hwnd`, or an empty string on error.
+BASE_EXPORT std::wstring GetWindowTextString(HWND hwnd);
} // namespace base::win