1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/win/message_window.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";
namespace base {
namespace win {
MessageWindow::MessageWindow()
: atom_(0),
window_(NULL) {
}
MessageWindow::~MessageWindow() {
DCHECK(CalledOnValidThread());
if (window_ != NULL) {
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) {
return DoCreate(message_callback, NULL);
}
bool MessageWindow::CreateNamed(const MessageCallback& message_callback,
const string16& window_name) {
return DoCreate(message_callback, window_name.c_str());
}
bool MessageWindow::DoCreate(const MessageCallback& message_callback,
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);
if (!window_) {
LOG_GETLASTERROR(ERROR) << "Failed to create a message-only window";
return false;
}
return true;
}
// static
LRESULT CALLBACK MessageWindow::WindowProc(HWND hwnd,
UINT message,
WPARAM wparam,
LPARAM lparam) {
MessageWindow* self = reinterpret_cast<MessageWindow*>(
GetWindowLongPtr(hwnd, GWLP_USERDATA));
switch (message) {
// Set up the self before handling WM_CREATE.
case WM_CREATE: {
CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lparam);
self = reinterpret_cast<MessageWindow*>(cs->lpCreateParams);
// Make |hwnd| available to the message handler. At this point the control
// hasn't returned from CreateWindow() yet.
self->window_ = hwnd;
// Store pointer to the self to the window's user data.
SetLastError(ERROR_SUCCESS);
LONG_PTR result = SetWindowLongPtr(
hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(self));
CHECK(result != 0 || GetLastError() == ERROR_SUCCESS);
break;
}
// Clear the pointer to stop calling the self once WM_DESTROY is
// received.
case WM_DESTROY: {
SetLastError(ERROR_SUCCESS);
LONG_PTR result = SetWindowLongPtr(hwnd, GWLP_USERDATA, NULL);
CHECK(result != 0 || GetLastError() == ERROR_SUCCESS);
break;
}
}
// Handle the message.
if (self) {
LRESULT message_result;
if (self->message_callback_.Run(message, wparam, lparam, &message_result))
return message_result;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
} // namespace win
} // namespace base
|