Chromium Code Reviews| Index: base/win/message_window.cc |
| diff --git a/base/win/message_window.cc b/base/win/message_window.cc |
| index 8a9c2874d8990abeb1736258da6603da6dce8135..697915a1cb9a80e7f54480530e456def78dc6a5a 100644 |
| --- a/base/win/message_window.cc |
| +++ b/base/win/message_window.cc |
| @@ -4,20 +4,68 @@ |
| #include "base/win/message_window.h" |
| +#include "base/lazy_instance.h" |
| #include "base/logging.h" |
| #include "base/process_util.h" |
| -#include "base/strings/string16.h" |
| -#include "base/strings/stringprintf.h" |
| #include "base/win/wrapped_window_proc.h" |
| -const wchar_t kClassNameFormat[] = L"Chrome_MessageWindow_%p"; |
| +const wchar_t kMessageWindowClassName[] = L"Chrome_MessageWindow"; |
| namespace base { |
| namespace win { |
| -MessageWindow::MessageWindow() |
| +// Used along with LazyInstance to register a window class for message-only |
| +// windows created by MessageWindow. |
| +class MessageWindow::WindowClass { |
| + public: |
| + WindowClass(); |
| + ~WindowClass(); |
| + |
| + ATOM atom() const { return atom_; } |
|
cpu_(ooo_6.6-7.5)
2013/06/27 15:15:38
member constness but not logical constness. I woul
alexeypa (please no reviews)
2013/06/27 17:10:58
Done.
|
| + HINSTANCE instance() const { return instance_; } |
| + |
| + private: |
| + ATOM atom_; |
| + HINSTANCE instance_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WindowClass); |
| +}; |
| + |
| +static LazyInstance<MessageWindow::WindowClass> g_window_class = |
| + LAZY_INSTANCE_INITIALIZER; |
| + |
| +MessageWindow::WindowClass::WindowClass() |
| : atom_(0), |
| - window_(NULL) { |
| + instance_(base::GetModuleFromAddress(&MessageWindow::WindowProc)) { |
| + WNDCLASSEX window_class; |
| + window_class.cbSize = sizeof(window_class); |
| + window_class.style = 0; |
| + window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>; |
| + window_class.cbClsExtra = 0; |
| + window_class.cbWndExtra = 0; |
| + window_class.hInstance = instance_; |
| + window_class.hIcon = NULL; |
| + window_class.hCursor = NULL; |
| + window_class.hbrBackground = NULL; |
| + window_class.lpszMenuName = NULL; |
| + window_class.lpszClassName = kMessageWindowClassName; |
| + window_class.hIconSm = NULL; |
| + atom_ = RegisterClassEx(&window_class); |
| + if (atom_ == 0) { |
| + LOG_GETLASTERROR(ERROR) |
| + << "Failed to register the window class for a message-only window"; |
| + } |
| +} |
| + |
| +MessageWindow::WindowClass::~WindowClass() { |
| + if (atom_ != 0) { |
| + BOOL result = UnregisterClass(MAKEINTATOM(atom_), instance_); |
| + DCHECK(result); |
| + } |
| +} |
| + |
| +MessageWindow::MessageWindow() |
| + : window_(NULL) { |
| } |
| MessageWindow::~MessageWindow() { |
| @@ -27,13 +75,6 @@ MessageWindow::~MessageWindow() { |
| BOOL result = DestroyWindow(window_); |
| DCHECK(result); |
| } |
| - |
| - if (atom_ != 0) { |
| - BOOL result = UnregisterClass( |
| - MAKEINTATOM(atom_), |
| - base::GetModuleFromAddress(&MessageWindow::WindowProc)); |
| - DCHECK(result); |
| - } |
| } |
| bool MessageWindow::Create(const MessageCallback& message_callback) { |
| @@ -45,41 +86,23 @@ bool MessageWindow::CreateNamed(const MessageCallback& message_callback, |
| return DoCreate(message_callback, window_name.c_str()); |
| } |
| +// static |
| +HWND MessageWindow::FindWindow(const string16& window_name) { |
| + return FindWindowEx(HWND_MESSAGE, NULL, kMessageWindowClassName, |
| + window_name.c_str()); |
| +} |
| + |
| bool MessageWindow::DoCreate(const MessageCallback& message_callback, |
| - const wchar_t* window_name) { |
| + const wchar_t* window_name) { |
| DCHECK(CalledOnValidThread()); |
| - DCHECK(!atom_); |
| DCHECK(message_callback_.is_null()); |
| DCHECK(!window_); |
| message_callback_ = message_callback; |
| - // Register a separate window class for each instance of |MessageWindow|. |
| - string16 class_name = base::StringPrintf(kClassNameFormat, this); |
| - HINSTANCE instance = base::GetModuleFromAddress(&MessageWindow::WindowProc); |
| - |
| - WNDCLASSEX window_class; |
| - window_class.cbSize = sizeof(window_class); |
| - window_class.style = 0; |
| - window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>; |
| - window_class.cbClsExtra = 0; |
| - window_class.cbWndExtra = 0; |
| - window_class.hInstance = instance; |
| - window_class.hIcon = NULL; |
| - window_class.hCursor = NULL; |
| - window_class.hbrBackground = NULL; |
| - window_class.lpszMenuName = NULL; |
| - window_class.lpszClassName = class_name.c_str(); |
| - window_class.hIconSm = NULL; |
| - atom_ = RegisterClassEx(&window_class); |
| - if (atom_ == 0) { |
| - LOG_GETLASTERROR(ERROR) |
| - << "Failed to register the window class for a message-only window"; |
| - return false; |
| - } |
| - |
| - window_ = CreateWindow(MAKEINTATOM(atom_), window_name, 0, 0, 0, 0, 0, |
| - HWND_MESSAGE, 0, instance, this); |
| + const WindowClass& window_class = g_window_class.Get(); |
| + window_ = CreateWindow(MAKEINTATOM(window_class.atom()), window_name, 0, 0, 0, |
| + 0, 0, HWND_MESSAGE, 0, window_class.instance(), this); |
| if (!window_) { |
| LOG_GETLASTERROR(ERROR) << "Failed to create a message-only window"; |
| return false; |