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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
// 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/gfx/win/window_impl.h"
#include <list>
#include "base/at_exit.h"
#include "base/compiler_specific.h"
#include "base/debug/alias.h"
#include "base/debug/dump_without_crashing.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "base/win/resource_exhaustion.h"
#include "base/win/win_util.h"
#include "base/win/wrapped_window_proc.h"
#include "ui/gfx/win/crash_id_helper.h"
#include "ui/gfx/win/hwnd_util.h"
namespace gfx {
static constexpr DWORD kWindowDefaultChildStyle =
WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
static constexpr DWORD kWindowDefaultStyle =
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
///////////////////////////////////////////////////////////////////////////////
// WindowImpl class tracking.
// Several external scripts rely explicitly on this base class name for
// acquiring the window handle and will break if this is modified!
// static
const wchar_t* const WindowImpl::kBaseClassName = L"Chrome_WidgetWin_";
// WindowImpl class information used for registering unique windows.
struct ClassInfo {
ClassInfo(int style, HICON icon, HICON small_icon)
: style(style), icon(icon), small_icon(small_icon) {}
// Compares two ClassInfos. Returns true if all members match.
bool Equals(const ClassInfo& other) const {
return (other.style == style && other.icon == icon &&
other.small_icon == small_icon);
}
UINT style;
HICON icon;
HICON small_icon;
};
// WARNING: this class may be used on multiple threads.
class ClassRegistrar {
public:
ClassRegistrar(const ClassRegistrar&) = delete;
ClassRegistrar& operator=(const ClassRegistrar&) = delete;
~ClassRegistrar();
static ClassRegistrar* GetInstance();
void UnregisterClasses();
// Returns the atom identifying the class matching |class_info|,
// creating and registering a new class if the class is not yet known.
ATOM RetrieveClassAtom(const ClassInfo& class_info);
private:
// Represents a registered window class.
struct RegisteredClass {
RegisteredClass(const ClassInfo& info,
const std::wstring& name,
ATOM atom,
HINSTANCE instance);
// Info used to create the class.
ClassInfo info;
// The name given to the window class
std::wstring name;
// The atom identifying the window class.
ATOM atom;
// The handle of the module containing the window proceedure.
HMODULE instance;
};
ClassRegistrar();
friend struct base::DefaultSingletonTraits<ClassRegistrar>;
using RegisteredClasses = std::list<RegisteredClass>;
RegisteredClasses registered_classes_;
// Counter of how many classes have been registered so far.
int registered_count_ = 0;
base::Lock lock_;
};
ClassRegistrar::~ClassRegistrar() = default;
// static
ClassRegistrar* ClassRegistrar::GetInstance() {
return base::Singleton<ClassRegistrar,
base::LeakySingletonTraits<ClassRegistrar>>::get();
}
void ClassRegistrar::UnregisterClasses() {
for (RegisteredClasses::iterator i = registered_classes_.begin();
i != registered_classes_.end(); ++i) {
if (UnregisterClass(MAKEINTATOM(i->atom), i->instance)) {
registered_classes_.erase(i);
} else {
LOG(ERROR) << "Failed to unregister class " << i->name
<< ". Error = " << GetLastError();
}
}
}
ATOM ClassRegistrar::RetrieveClassAtom(const ClassInfo& class_info) {
base::AutoLock auto_lock(lock_);
for (const auto& registered_class : registered_classes_) {
if (class_info.Equals(registered_class.info)) {
return registered_class.atom;
}
}
// No class found, need to register one.
std::wstring name = std::wstring(WindowImpl::kBaseClassName) +
base::NumberToWString(registered_count_++);
// We're not supposed to have many window classes, so if registered_count_
// gets above a certain small threshold, we may as well have a resource leak
// caused by repeatedly registering a class with auto-generated name, which
// would eventually lead to user atom table exhaustion.
// TODO(crbug.com/40925772): remove when source of ATOM leak is found.
if (registered_count_ == 128) {
base::debug::DumpWithoutCrashing();
}
WNDCLASSEX window_class;
base::win::InitializeWindowClass(
name.c_str(), &base::win::WrappedWindowProc<WindowImpl::WndProc>,
class_info.style, 0, 0, nullptr,
reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)), nullptr,
class_info.icon, class_info.small_icon, &window_class);
HMODULE instance = window_class.hInstance;
ATOM atom = RegisterClassEx(&window_class);
if (!atom) {
// Perhaps the Window session has run out of atoms; see
// https://crbug.com/653493.
base::win::OnResourceExhausted();
}
registered_classes_.push_back(RegisteredClass(
class_info, name, atom, instance));
return atom;
}
ClassRegistrar::RegisteredClass::RegisteredClass(const ClassInfo& info,
const std::wstring& name,
ATOM atom,
HMODULE instance)
: info(info), name(name), atom(atom), instance(instance) {}
ClassRegistrar::ClassRegistrar() = default;
///////////////////////////////////////////////////////////////////////////////
// WindowImpl, public
WindowImpl::WindowImpl(const std::string& debugging_id)
: debugging_id_(debugging_id), class_style_(CS_DBLCLKS) {}
WindowImpl::~WindowImpl() {
ClearUserData();
}
// static
void WindowImpl::UnregisterClassesAtExit() {
base::AtExitManager::RegisterTask(
base::BindOnce(&ClassRegistrar::UnregisterClasses,
base::Unretained(ClassRegistrar::GetInstance())));
}
void WindowImpl::Init(HWND parent, const Rect& bounds) {
if (window_style_ == 0)
window_style_ = parent ? kWindowDefaultChildStyle : kWindowDefaultStyle;
if (parent == HWND_DESKTOP) {
// Only non-child windows can have HWND_DESKTOP (0) as their parent.
CHECK((window_style_ & WS_CHILD) == 0);
parent = GetWindowToParentTo(false);
} else if (parent == ::GetDesktopWindow()) {
// Any type of window can have the "Desktop Window" as their parent.
parent = GetWindowToParentTo(true);
} else if (parent != HWND_MESSAGE) {
CHECK(::IsWindow(parent));
}
int x, y, width, height;
if (bounds.IsEmpty()) {
x = y = width = height = CW_USEDEFAULT;
} else {
x = bounds.x();
y = bounds.y();
width = bounds.width();
height = bounds.height();
}
ATOM atom = GetWindowClassAtom();
auto weak_this = weak_factory_.GetWeakPtr();
HWND hwnd = CreateWindowEx(window_ex_style_, reinterpret_cast<wchar_t*>(atom),
nullptr, window_style_, x, y, width, height,
parent, nullptr, nullptr, this);
const DWORD create_window_error = ::GetLastError();
// First nccalcsize (during CreateWindow) for captioned windows is
// deliberately ignored so force a second one here to get the right
// non-client set up.
if (hwnd && (window_style_ & WS_CAPTION)) {
SetWindowPos(hwnd, nullptr, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
SWP_NOACTIVATE | SWP_NOREDRAW);
}
if (!hwnd_ && create_window_error == 0) {
bool still_alive = !!weak_this;
base::debug::Alias(&still_alive);
base::debug::Alias(&hwnd);
base::debug::Alias(&atom);
bool got_create = got_create_;
base::debug::Alias(&got_create);
bool got_valid_hwnd = got_valid_hwnd_;
base::debug::Alias(&got_valid_hwnd);
WNDCLASSEX class_info;
UNSAFE_TODO(memset(&class_info, 0, sizeof(WNDCLASSEX)));
class_info.cbSize = sizeof(WNDCLASSEX);
BOOL got_class =
GetClassInfoEx(GetModuleHandle(nullptr),
reinterpret_cast<wchar_t*>(atom), &class_info);
base::debug::Alias(&got_class);
bool procs_match = got_class && class_info.lpfnWndProc ==
base::win::WrappedWindowProc<&WindowImpl::WndProc>;
base::debug::Alias(&procs_match);
NOTREACHED();
}
CheckWindowCreated(hwnd_, create_window_error);
// The window procedure should have set the data for us.
CHECK_EQ(this, GetWindowUserData(hwnd));
}
HICON WindowImpl::GetDefaultWindowIcon() const {
return nullptr;
}
HICON WindowImpl::GetSmallWindowIcon() const {
return nullptr;
}
LRESULT WindowImpl::OnWndProc(UINT message, WPARAM w_param, LPARAM l_param) {
LRESULT result = 0;
HWND hwnd = hwnd_;
if (message == WM_NCDESTROY)
hwnd_ = nullptr;
// Handle the message if it's in our message map; otherwise, let the system
// handle it.
if (!ProcessWindowMessage(hwnd, message, w_param, l_param, result))
result = DefWindowProc(hwnd, message, w_param, l_param);
return result;
}
void WindowImpl::ClearUserData() {
if (::IsWindow(hwnd_)) {
SetWindowUserData(hwnd_, nullptr);
}
}
// static
LRESULT CALLBACK WindowImpl::WndProc(HWND hwnd,
UINT message,
WPARAM w_param,
LPARAM l_param) {
WindowImpl* window = nullptr;
if (message == WM_NCCREATE) {
CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(l_param);
window = reinterpret_cast<WindowImpl*>(cs->lpCreateParams);
DCHECK(window);
SetWindowUserData(hwnd, window);
window->hwnd_ = hwnd;
window->got_create_ = true;
if (hwnd)
window->got_valid_hwnd_ = true;
} else {
window = reinterpret_cast<WindowImpl*>(GetWindowUserData(hwnd));
}
if (!window)
return 0;
auto logger =
CrashIdHelper::Get()->OnWillProcessMessages(window->debugging_id_);
return window->OnWndProc(message, w_param, l_param);
}
ATOM WindowImpl::GetWindowClassAtom() {
HICON icon = GetDefaultWindowIcon();
HICON small_icon = GetSmallWindowIcon();
ClassInfo class_info(initial_class_style(), icon, small_icon);
return ClassRegistrar::GetInstance()->RetrieveClassAtom(class_info);
}
} // namespace gfx
|