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 DCHECK(result); |
| 64 } |
| 65 } |
| 66 |
18 MessageWindow::MessageWindow() | 67 MessageWindow::MessageWindow() |
19 : atom_(0), | 68 : window_(NULL) { |
20 window_(NULL) { | |
21 } | 69 } |
22 | 70 |
23 MessageWindow::~MessageWindow() { | 71 MessageWindow::~MessageWindow() { |
24 DCHECK(CalledOnValidThread()); | 72 DCHECK(CalledOnValidThread()); |
25 | 73 |
26 if (window_ != NULL) { | 74 if (window_ != NULL) { |
27 BOOL result = DestroyWindow(window_); | 75 BOOL result = DestroyWindow(window_); |
28 DCHECK(result); | 76 DCHECK(result); |
29 } | 77 } |
30 | |
31 if (atom_ != 0) { | |
32 BOOL result = UnregisterClass( | |
33 MAKEINTATOM(atom_), | |
34 base::GetModuleFromAddress(&MessageWindow::WindowProc)); | |
35 DCHECK(result); | |
36 } | |
37 } | 78 } |
38 | 79 |
39 bool MessageWindow::Create(const MessageCallback& message_callback) { | 80 bool MessageWindow::Create(const MessageCallback& message_callback) { |
40 return DoCreate(message_callback, NULL); | 81 return DoCreate(message_callback, NULL); |
41 } | 82 } |
42 | 83 |
43 bool MessageWindow::CreateNamed(const MessageCallback& message_callback, | 84 bool MessageWindow::CreateNamed(const MessageCallback& message_callback, |
44 const string16& window_name) { | 85 const string16& window_name) { |
45 return DoCreate(message_callback, window_name.c_str()); | 86 return DoCreate(message_callback, window_name.c_str()); |
46 } | 87 } |
47 | 88 |
| 89 // static |
| 90 HWND MessageWindow::FindWindow(const string16& window_name) { |
| 91 return FindWindowEx(HWND_MESSAGE, NULL, kMessageWindowClassName, |
| 92 window_name.c_str()); |
| 93 } |
| 94 |
48 bool MessageWindow::DoCreate(const MessageCallback& message_callback, | 95 bool MessageWindow::DoCreate(const MessageCallback& message_callback, |
49 const wchar_t* window_name) { | 96 const wchar_t* window_name) { |
50 DCHECK(CalledOnValidThread()); | 97 DCHECK(CalledOnValidThread()); |
51 DCHECK(!atom_); | |
52 DCHECK(message_callback_.is_null()); | 98 DCHECK(message_callback_.is_null()); |
53 DCHECK(!window_); | 99 DCHECK(!window_); |
54 | 100 |
55 message_callback_ = message_callback; | 101 message_callback_ = message_callback; |
56 | 102 |
57 // Register a separate window class for each instance of |MessageWindow|. | 103 WindowClass& window_class = g_window_class.Get(); |
58 string16 class_name = base::StringPrintf(kClassNameFormat, this); | 104 window_ = CreateWindow(MAKEINTATOM(window_class.atom()), window_name, 0, 0, 0, |
59 HINSTANCE instance = base::GetModuleFromAddress(&MessageWindow::WindowProc); | 105 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_) { | 106 if (!window_) { |
84 LOG_GETLASTERROR(ERROR) << "Failed to create a message-only window"; | 107 LOG_GETLASTERROR(ERROR) << "Failed to create a message-only window"; |
85 return false; | 108 return false; |
86 } | 109 } |
87 | 110 |
88 return true; | 111 return true; |
89 } | 112 } |
90 | 113 |
91 // static | 114 // static |
92 LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd, | 115 LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 LRESULT message_result; | 152 LRESULT message_result; |
130 if (self->message_callback_.Run(message, wparam, lparam, &message_result)) | 153 if (self->message_callback_.Run(message, wparam, lparam, &message_result)) |
131 return message_result; | 154 return message_result; |
132 } | 155 } |
133 | 156 |
134 return DefWindowProc(hwnd, message, wparam, lparam); | 157 return DefWindowProc(hwnd, message, wparam, lparam); |
135 } | 158 } |
136 | 159 |
137 } // namespace win | 160 } // namespace win |
138 } // namespace base | 161 } // namespace base |
OLD | NEW |