1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/win/hwnd_subclass.h"
#include <algorithm>
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/singleton.h"
#include "ui/base/win/touch_input.h"
#include "ui/gfx/win/hwnd_util.h"
namespace {
const char kHWNDSubclassKey[] = "__UI_BASE_WIN_HWND_SUBCLASS_PROC__";
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM w_param,
LPARAM l_param) {
ui::HWNDSubclass* wrapped_wnd_proc =
reinterpret_cast<ui::HWNDSubclass*>(
ui::ViewProp::GetValue(hwnd, kHWNDSubclassKey));
return wrapped_wnd_proc ? wrapped_wnd_proc->OnWndProc(hwnd,
message,
w_param,
l_param)
: DefWindowProc(hwnd, message, w_param, l_param);
}
WNDPROC GetCurrentWndProc(HWND target) {
return reinterpret_cast<WNDPROC>(GetWindowLongPtr(target, GWLP_WNDPROC));
}
} // namespace
namespace ui {
// Singleton factory that creates and manages the lifetime of all
// ui::HWNDSubclass objects.
class HWNDSubclass::HWNDSubclassFactory {
public:
static HWNDSubclassFactory* GetInstance() {
return base::Singleton<
HWNDSubclassFactory,
base::LeakySingletonTraits<HWNDSubclassFactory>>::get();
}
HWNDSubclassFactory(const HWNDSubclassFactory&) = delete;
HWNDSubclassFactory& operator=(const HWNDSubclassFactory&) = delete;
// Returns a non-null HWNDSubclass corresponding to the HWND |target|. Creates
// one if none exists. Retains ownership of the returned pointer.
HWNDSubclass* GetHwndSubclassForTarget(HWND target) {
DCHECK(target);
HWNDSubclass* subclass = reinterpret_cast<HWNDSubclass*>(
ui::ViewProp::GetValue(target, kHWNDSubclassKey));
if (!subclass) {
subclass = new ui::HWNDSubclass(target);
hwnd_subclasses_.push_back(base::WrapUnique(subclass));
}
return subclass;
}
const std::vector<std::unique_ptr<HWNDSubclass>>& hwnd_subclasses() {
return hwnd_subclasses_;
}
private:
friend struct base::DefaultSingletonTraits<HWNDSubclassFactory>;
HWNDSubclassFactory() {}
std::vector<std::unique_ptr<HWNDSubclass>> hwnd_subclasses_;
};
// static
void HWNDSubclass::AddFilterToTarget(HWND target, HWNDMessageFilter* filter) {
HWNDSubclassFactory::GetInstance()->GetHwndSubclassForTarget(
target)->AddFilter(filter);
}
// static
void HWNDSubclass::RemoveFilterFromAllTargets(HWNDMessageFilter* filter) {
HWNDSubclassFactory* factory = HWNDSubclassFactory::GetInstance();
for (const auto& subclass : factory->hwnd_subclasses())
subclass->RemoveFilter(filter);
}
// static
HWNDSubclass* HWNDSubclass::GetHwndSubclassForTarget(HWND target) {
return HWNDSubclassFactory::GetInstance()->GetHwndSubclassForTarget(target);
}
void HWNDSubclass::AddFilter(HWNDMessageFilter* filter) {
DCHECK(filter);
if (!base::Contains(filters_, filter))
filters_.push_back(filter);
}
void HWNDSubclass::RemoveFilter(HWNDMessageFilter* filter) {
std::vector<raw_ptr<HWNDMessageFilter, VectorExperimental>>::iterator it =
std::ranges::find(filters_, filter);
if (it != filters_.end())
filters_.erase(it);
}
HWNDSubclass::HWNDSubclass(HWND target)
: target_(target),
original_wnd_proc_(GetCurrentWndProc(target)),
prop_(target, kHWNDSubclassKey, this) {
gfx::SetWindowProc(target_, &WndProc);
}
HWNDSubclass::~HWNDSubclass() {
}
LRESULT HWNDSubclass::OnWndProc(HWND hwnd,
UINT message,
WPARAM w_param,
LPARAM l_param) {
// Touch messages are always passed in screen coordinates. If the OS is
// scaled, but the app is not DPI aware, then then WM_TOUCH might be
// intended for a different window.
if (message == WM_TOUCH) {
TOUCHINPUT point;
if (ui::GetTouchInputInfoWrapper(reinterpret_cast<HTOUCHINPUT>(l_param), 1,
&point, sizeof(TOUCHINPUT))) {
POINT touch_location = {TOUCH_COORD_TO_PIXEL(point.x),
TOUCH_COORD_TO_PIXEL(point.y)};
HWND actual_target = WindowFromPoint(touch_location);
if (actual_target != hwnd) {
return SendMessage(actual_target, message, w_param, l_param);
}
}
}
for (std::vector<raw_ptr<HWNDMessageFilter, VectorExperimental>>::iterator
it = filters_.begin();
it != filters_.end(); ++it) {
LRESULT l_result = 0;
if ((*it)->FilterMessage(hwnd, message, w_param, l_param, &l_result))
return l_result;
}
// In most cases, |original_wnd_proc_| will take care of calling
// DefWindowProc.
return CallWindowProc(original_wnd_proc_, hwnd, message, w_param, l_param);
}
HWNDMessageFilter::~HWNDMessageFilter() {
HWNDSubclass::RemoveFilterFromAllTargets(this);
}
} // namespace ui
|