| 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" | |
| 8 #include "base/logging.h" | 7 #include "base/logging.h" |
| 9 #include "base/macros.h" | 8 #include "base/macros.h" |
| 10 #include "base/win/current_module.h" | 9 #include "base/win/current_module.h" |
| 11 #include "base/win/wrapped_window_proc.h" | 10 #include "base/win/wrapped_window_proc.h" |
| 12 | 11 |
| 13 const wchar_t kMessageWindowClassName[] = L"Chrome_MessageWindow"; | 12 const wchar_t kMessageWindowClassName[] = L"Chrome_MessageWindow"; |
| 14 | 13 |
| 15 namespace base { | 14 namespace base { |
| 16 namespace win { | 15 namespace win { |
| 17 | 16 |
| 18 // Used along with LazyInstance to register a window class for message-only | 17 // Used along with a threadsafe static pointer to register a window class for |
| 19 // windows created by MessageWindow. | 18 // message-only windows created by MessageWindow. |
| 20 class MessageWindow::WindowClass { | 19 class MessageWindow::WindowClass { |
| 21 public: | 20 public: |
| 22 WindowClass(); | 21 WindowClass(); |
| 23 ~WindowClass(); | 22 ~WindowClass(); |
| 24 | 23 |
| 25 ATOM atom() { return atom_; } | 24 ATOM atom() { return atom_; } |
| 26 HINSTANCE instance() { return instance_; } | 25 HINSTANCE instance() { return instance_; } |
| 27 | 26 |
| 28 private: | 27 private: |
| 29 ATOM atom_; | 28 ATOM atom_; |
| 30 HINSTANCE instance_; | 29 HINSTANCE instance_; |
| 31 | 30 |
| 32 DISALLOW_COPY_AND_ASSIGN(WindowClass); | 31 DISALLOW_COPY_AND_ASSIGN(WindowClass); |
| 33 }; | 32 }; |
| 34 | 33 |
| 35 static LazyInstance<MessageWindow::WindowClass> g_window_class = | |
| 36 LAZY_INSTANCE_INITIALIZER; | |
| 37 | |
| 38 MessageWindow::WindowClass::WindowClass() | 34 MessageWindow::WindowClass::WindowClass() |
| 39 : atom_(0), instance_(CURRENT_MODULE()) { | 35 : atom_(0), instance_(CURRENT_MODULE()) { |
| 40 WNDCLASSEX window_class; | 36 WNDCLASSEX window_class; |
| 41 window_class.cbSize = sizeof(window_class); | 37 window_class.cbSize = sizeof(window_class); |
| 42 window_class.style = 0; | 38 window_class.style = 0; |
| 43 window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>; | 39 window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>; |
| 44 window_class.cbClsExtra = 0; | 40 window_class.cbClsExtra = 0; |
| 45 window_class.cbWndExtra = 0; | 41 window_class.cbWndExtra = 0; |
| 46 window_class.hInstance = instance_; | 42 window_class.hInstance = instance_; |
| 47 window_class.hIcon = NULL; | 43 window_class.hIcon = NULL; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 } | 93 } |
| 98 | 94 |
| 99 bool MessageWindow::DoCreate(const MessageCallback& message_callback, | 95 bool MessageWindow::DoCreate(const MessageCallback& message_callback, |
| 100 const wchar_t* window_name) { | 96 const wchar_t* window_name) { |
| 101 DCHECK(CalledOnValidThread()); | 97 DCHECK(CalledOnValidThread()); |
| 102 DCHECK(message_callback_.is_null()); | 98 DCHECK(message_callback_.is_null()); |
| 103 DCHECK(!window_); | 99 DCHECK(!window_); |
| 104 | 100 |
| 105 message_callback_ = message_callback; | 101 message_callback_ = message_callback; |
| 106 | 102 |
| 107 WindowClass& window_class = g_window_class.Get(); | 103 static auto window_class = new WindowClass(); |
| 108 window_ = CreateWindow(MAKEINTATOM(window_class.atom()), window_name, 0, 0, 0, | 104 window_ = |
| 109 0, 0, HWND_MESSAGE, 0, window_class.instance(), this); | 105 CreateWindow(MAKEINTATOM(window_class->atom()), window_name, 0, 0, 0, 0, |
| 106 0, HWND_MESSAGE, 0, window_class->instance(), this); |
| 110 if (!window_) { | 107 if (!window_) { |
| 111 PLOG(ERROR) << "Failed to create a message-only window"; | 108 PLOG(ERROR) << "Failed to create a message-only window"; |
| 112 return false; | 109 return false; |
| 113 } | 110 } |
| 114 | 111 |
| 115 return true; | 112 return true; |
| 116 } | 113 } |
| 117 | 114 |
| 118 // static | 115 // static |
| 119 LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd, | 116 LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 LRESULT message_result; | 153 LRESULT message_result; |
| 157 if (self->message_callback_.Run(message, wparam, lparam, &message_result)) | 154 if (self->message_callback_.Run(message, wparam, lparam, &message_result)) |
| 158 return message_result; | 155 return message_result; |
| 159 } | 156 } |
| 160 | 157 |
| 161 return DefWindowProc(hwnd, message, wparam, lparam); | 158 return DefWindowProc(hwnd, message, wparam, lparam); |
| 162 } | 159 } |
| 163 | 160 |
| 164 } // namespace win | 161 } // namespace win |
| 165 } // namespace base | 162 } // namespace base |
| OLD | NEW |