Chromium Code Reviews| OLD | NEW | 
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "base/win/message_window.h" | 5 #include "base/win/message_window.h" | 
| 6 | 6 | 
| 7 #include "base/lazy_instance.h" | 7 #include "base/at_exit.h" | 
| 8 #include "base/logging.h" | 8 #include "base/logging.h" | 
| 9 #include "base/macros.h" | 9 #include "base/macros.h" | 
| 10 #include "base/win/current_module.h" | 10 #include "base/win/current_module.h" | 
| 11 #include "base/win/wrapped_window_proc.h" | 11 #include "base/win/wrapped_window_proc.h" | 
| 12 | 12 | 
| 13 const wchar_t kMessageWindowClassName[] = L"Chrome_MessageWindow"; | 13 const wchar_t kMessageWindowClassName[] = L"Chrome_MessageWindow"; | 
| 14 | 14 | 
| 15 namespace base { | 15 namespace base { | 
| 16 namespace win { | 16 namespace win { | 
| 17 | 17 | 
| 18 // Used along with LazyInstance to register a window class for message-only | 18 // Used along with a threadsafe static pointer to register a window class for | 
| 19 // windows created by MessageWindow. | 19 // message-only windows created by MessageWindow. | 
| 20 class MessageWindow::WindowClass { | 20 class MessageWindow::WindowClass { | 
| 21 public: | 21 public: | 
| 22 WindowClass(); | 22 WindowClass(); | 
| 23 ~WindowClass(); | 23 ~WindowClass(); | 
| 24 | 24 | 
| 25 ATOM atom() { return atom_; } | 25 ATOM atom() { return atom_; } | 
| 26 HINSTANCE instance() { return instance_; } | 26 HINSTANCE instance() { return instance_; } | 
| 27 | 27 | 
| 28 private: | 28 private: | 
| 29 ATOM atom_; | 29 ATOM atom_; | 
| 30 HINSTANCE instance_; | 30 HINSTANCE instance_; | 
| 31 | 31 | 
| 32 DISALLOW_COPY_AND_ASSIGN(WindowClass); | 32 DISALLOW_COPY_AND_ASSIGN(WindowClass); | 
| 33 }; | 33 }; | 
| 34 | 34 | 
| 35 static LazyInstance<MessageWindow::WindowClass> g_window_class = | 35 MessageWindow::WindowClass* GetWindowClass() { | 
| 36 LAZY_INSTANCE_INITIALIZER; | 36 static MessageWindow::WindowClass* window_class = []() { | 
| 37 auto ret = new MessageWindow::WindowClass(); | |
| 38 auto deleter = [](void* p) { | |
| 39 delete reinterpret_cast<MessageWindow::WindowClass*>(p); | |
| 40 }; | |
| 41 AtExitManager::RegisterCallback(deleter, ret); | |
| 
 
scottmg
2017/01/31 21:27:43
This "quick fix" is incorrect because in tests the
 
 | |
| 42 return ret; | |
| 43 }(); | |
| 44 return window_class; | |
| 45 } | |
| 37 | 46 | 
| 38 MessageWindow::WindowClass::WindowClass() | 47 MessageWindow::WindowClass::WindowClass() | 
| 39 : atom_(0), instance_(CURRENT_MODULE()) { | 48 : atom_(0), instance_(CURRENT_MODULE()) { | 
| 40 WNDCLASSEX window_class; | 49 WNDCLASSEX window_class; | 
| 41 window_class.cbSize = sizeof(window_class); | 50 window_class.cbSize = sizeof(window_class); | 
| 42 window_class.style = 0; | 51 window_class.style = 0; | 
| 43 window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>; | 52 window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>; | 
| 44 window_class.cbClsExtra = 0; | 53 window_class.cbClsExtra = 0; | 
| 45 window_class.cbWndExtra = 0; | 54 window_class.cbWndExtra = 0; | 
| 46 window_class.hInstance = instance_; | 55 window_class.hInstance = instance_; | 
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 } | 106 } | 
| 98 | 107 | 
| 99 bool MessageWindow::DoCreate(const MessageCallback& message_callback, | 108 bool MessageWindow::DoCreate(const MessageCallback& message_callback, | 
| 100 const wchar_t* window_name) { | 109 const wchar_t* window_name) { | 
| 101 DCHECK(CalledOnValidThread()); | 110 DCHECK(CalledOnValidThread()); | 
| 102 DCHECK(message_callback_.is_null()); | 111 DCHECK(message_callback_.is_null()); | 
| 103 DCHECK(!window_); | 112 DCHECK(!window_); | 
| 104 | 113 | 
| 105 message_callback_ = message_callback; | 114 message_callback_ = message_callback; | 
| 106 | 115 | 
| 107 WindowClass& window_class = g_window_class.Get(); | 116 window_ = | 
| 108 window_ = CreateWindow(MAKEINTATOM(window_class.atom()), window_name, 0, 0, 0, | 117 CreateWindow(MAKEINTATOM(GetWindowClass()->atom()), window_name, 0, 0, 0, | 
| 109 0, 0, HWND_MESSAGE, 0, window_class.instance(), this); | 118 0, 0, HWND_MESSAGE, 0, GetWindowClass()->instance(), this); | 
| 110 if (!window_) { | 119 if (!window_) { | 
| 111 PLOG(ERROR) << "Failed to create a message-only window"; | 120 PLOG(ERROR) << "Failed to create a message-only window"; | 
| 112 return false; | 121 return false; | 
| 113 } | 122 } | 
| 114 | 123 | 
| 115 return true; | 124 return true; | 
| 116 } | 125 } | 
| 117 | 126 | 
| 118 // static | 127 // static | 
| 119 LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd, | 128 LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd, | 
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 LRESULT message_result; | 165 LRESULT message_result; | 
| 157 if (self->message_callback_.Run(message, wparam, lparam, &message_result)) | 166 if (self->message_callback_.Run(message, wparam, lparam, &message_result)) | 
| 158 return message_result; | 167 return message_result; | 
| 159 } | 168 } | 
| 160 | 169 | 
| 161 return DefWindowProc(hwnd, message, wparam, lparam); | 170 return DefWindowProc(hwnd, message, wparam, lparam); | 
| 162 } | 171 } | 
| 163 | 172 | 
| 164 } // namespace win | 173 } // namespace win | 
| 165 } // namespace base | 174 } // namespace base | 
| OLD | NEW |