| 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/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/process_util.h" | 9 #include "base/process_util.h" |
| 9 #include "base/strings/string16.h" | |
| 10 #include "base/strings/stringprintf.h" | |
| 11 #include "base/win/wrapped_window_proc.h" | 10 #include "base/win/wrapped_window_proc.h" |
| 12 | 11 |
| 13 const wchar_t kClassNameFormat[] = L"Chrome_MessageWindow_%p"; | 12 const wchar_t kMessageWindowClassName[] = L"Chrome_MessageWindow"; |
| 14 | 13 |
| 15 namespace base { | 14 namespace base { |
| 16 namespace win { | 15 namespace win { |
| 17 | 16 |
| 17 // Used along with LazyInstance to register a window class for message-only |
| 18 // windows created by MessageWindow. |
| 19 class MessageWindow::WindowClass { |
| 20 public: |
| 21 WindowClass(); |
| 22 ~WindowClass(); |
| 23 |
| 24 ATOM atom() { return atom_; } |
| 25 HINSTANCE instance() { return instance_; } |
| 26 |
| 27 private: |
| 28 ATOM atom_; |
| 29 HINSTANCE instance_; |
| 30 |
| 31 DISALLOW_COPY_AND_ASSIGN(WindowClass); |
| 32 }; |
| 33 |
| 34 static LazyInstance<MessageWindow::WindowClass> g_window_class = |
| 35 LAZY_INSTANCE_INITIALIZER; |
| 36 |
| 37 MessageWindow::WindowClass::WindowClass() |
| 38 : atom_(0), |
| 39 instance_(base::GetModuleFromAddress(&MessageWindow::WindowProc)) { |
| 40 WNDCLASSEX window_class; |
| 41 window_class.cbSize = sizeof(window_class); |
| 42 window_class.style = 0; |
| 43 window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>; |
| 44 window_class.cbClsExtra = 0; |
| 45 window_class.cbWndExtra = 0; |
| 46 window_class.hInstance = instance_; |
| 47 window_class.hIcon = NULL; |
| 48 window_class.hCursor = NULL; |
| 49 window_class.hbrBackground = NULL; |
| 50 window_class.lpszMenuName = NULL; |
| 51 window_class.lpszClassName = kMessageWindowClassName; |
| 52 window_class.hIconSm = NULL; |
| 53 atom_ = RegisterClassEx(&window_class); |
| 54 if (atom_ == 0) { |
| 55 LOG_GETLASTERROR(ERROR) |
| 56 << "Failed to register the window class for a message-only window"; |
| 57 } |
| 58 } |
| 59 |
| 60 MessageWindow::WindowClass::~WindowClass() { |
| 61 if (atom_ != 0) { |
| 62 BOOL result = UnregisterClass(MAKEINTATOM(atom_), instance_); |
| 63 // Hitting this DCHECK usually means that some MessageWindow objects were |
| 64 // leaked. For example not calling |
| 65 // ui::Clipboard::DestroyClipboardForCurrentThread() results in a leaked |
| 66 // MessageWindow. |
| 67 DCHECK(result); |
| 68 } |
| 69 } |
| 70 |
| 18 MessageWindow::MessageWindow() | 71 MessageWindow::MessageWindow() |
| 19 : atom_(0), | 72 : window_(NULL) { |
| 20 window_(NULL) { | |
| 21 } | 73 } |
| 22 | 74 |
| 23 MessageWindow::~MessageWindow() { | 75 MessageWindow::~MessageWindow() { |
| 24 DCHECK(CalledOnValidThread()); | 76 DCHECK(CalledOnValidThread()); |
| 25 | 77 |
| 26 if (window_ != NULL) { | 78 if (window_ != NULL) { |
| 27 BOOL result = DestroyWindow(window_); | 79 BOOL result = DestroyWindow(window_); |
| 28 DCHECK(result); | 80 DCHECK(result); |
| 29 } | 81 } |
| 30 | |
| 31 if (atom_ != 0) { | |
| 32 BOOL result = UnregisterClass( | |
| 33 MAKEINTATOM(atom_), | |
| 34 base::GetModuleFromAddress(&MessageWindow::WindowProc)); | |
| 35 DCHECK(result); | |
| 36 } | |
| 37 } | 82 } |
| 38 | 83 |
| 39 bool MessageWindow::Create(const MessageCallback& message_callback) { | 84 bool MessageWindow::Create(const MessageCallback& message_callback) { |
| 40 return DoCreate(message_callback, NULL); | 85 return DoCreate(message_callback, NULL); |
| 41 } | 86 } |
| 42 | 87 |
| 43 bool MessageWindow::CreateNamed(const MessageCallback& message_callback, | 88 bool MessageWindow::CreateNamed(const MessageCallback& message_callback, |
| 44 const string16& window_name) { | 89 const string16& window_name) { |
| 45 return DoCreate(message_callback, window_name.c_str()); | 90 return DoCreate(message_callback, window_name.c_str()); |
| 46 } | 91 } |
| 47 | 92 |
| 93 // static |
| 94 HWND MessageWindow::FindWindow(const string16& window_name) { |
| 95 return FindWindowEx(HWND_MESSAGE, NULL, kMessageWindowClassName, |
| 96 window_name.c_str()); |
| 97 } |
| 98 |
| 48 bool MessageWindow::DoCreate(const MessageCallback& message_callback, | 99 bool MessageWindow::DoCreate(const MessageCallback& message_callback, |
| 49 const wchar_t* window_name) { | 100 const wchar_t* window_name) { |
| 50 DCHECK(CalledOnValidThread()); | 101 DCHECK(CalledOnValidThread()); |
| 51 DCHECK(!atom_); | |
| 52 DCHECK(message_callback_.is_null()); | 102 DCHECK(message_callback_.is_null()); |
| 53 DCHECK(!window_); | 103 DCHECK(!window_); |
| 54 | 104 |
| 55 message_callback_ = message_callback; | 105 message_callback_ = message_callback; |
| 56 | 106 |
| 57 // Register a separate window class for each instance of |MessageWindow|. | 107 WindowClass& window_class = g_window_class.Get(); |
| 58 string16 class_name = base::StringPrintf(kClassNameFormat, this); | 108 window_ = CreateWindow(MAKEINTATOM(window_class.atom()), window_name, 0, 0, 0, |
| 59 HINSTANCE instance = base::GetModuleFromAddress(&MessageWindow::WindowProc); | 109 0, 0, HWND_MESSAGE, 0, window_class.instance(), this); |
| 60 | |
| 61 WNDCLASSEX window_class; | |
| 62 window_class.cbSize = sizeof(window_class); | |
| 63 window_class.style = 0; | |
| 64 window_class.lpfnWndProc = &base::win::WrappedWindowProc<WindowProc>; | |
| 65 window_class.cbClsExtra = 0; | |
| 66 window_class.cbWndExtra = 0; | |
| 67 window_class.hInstance = instance; | |
| 68 window_class.hIcon = NULL; | |
| 69 window_class.hCursor = NULL; | |
| 70 window_class.hbrBackground = NULL; | |
| 71 window_class.lpszMenuName = NULL; | |
| 72 window_class.lpszClassName = class_name.c_str(); | |
| 73 window_class.hIconSm = NULL; | |
| 74 atom_ = RegisterClassEx(&window_class); | |
| 75 if (atom_ == 0) { | |
| 76 LOG_GETLASTERROR(ERROR) | |
| 77 << "Failed to register the window class for a message-only window"; | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 window_ = CreateWindow(MAKEINTATOM(atom_), window_name, 0, 0, 0, 0, 0, | |
| 82 HWND_MESSAGE, 0, instance, this); | |
| 83 if (!window_) { | 110 if (!window_) { |
| 84 LOG_GETLASTERROR(ERROR) << "Failed to create a message-only window"; | 111 LOG_GETLASTERROR(ERROR) << "Failed to create a message-only window"; |
| 85 return false; | 112 return false; |
| 86 } | 113 } |
| 87 | 114 |
| 88 return true; | 115 return true; |
| 89 } | 116 } |
| 90 | 117 |
| 91 // static | 118 // static |
| 92 LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd, | 119 LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 LRESULT message_result; | 156 LRESULT message_result; |
| 130 if (self->message_callback_.Run(message, wparam, lparam, &message_result)) | 157 if (self->message_callback_.Run(message, wparam, lparam, &message_result)) |
| 131 return message_result; | 158 return message_result; |
| 132 } | 159 } |
| 133 | 160 |
| 134 return DefWindowProc(hwnd, message, wparam, lparam); | 161 return DefWindowProc(hwnd, message, wparam, lparam); |
| 135 } | 162 } |
| 136 | 163 |
| 137 } // namespace win | 164 } // namespace win |
| 138 } // namespace base | 165 } // namespace base |
| OLD | NEW |