Index: trunk/src/base/message_pump_win.cc |
=================================================================== |
--- trunk/src/base/message_pump_win.cc (revision 201972) |
+++ trunk/src/base/message_pump_win.cc (working copy) |
@@ -9,6 +9,8 @@ |
#include "base/debug/trace_event.h" |
#include "base/message_loop.h" |
#include "base/metrics/histogram.h" |
+#include "base/process_util.h" |
+#include "base/win/wrapped_window_proc.h" |
namespace { |
@@ -23,13 +25,12 @@ |
namespace base { |
+static const wchar_t kWndClass[] = L"Chrome_MessagePumpWindow"; |
+ |
// Message sent to get an additional time slice for pumping (processing) another |
// task (a series of such messages creates a continuous task pump). |
static const int kMsgHaveWork = WM_USER + 1; |
-// Used by MessagePumpUI to wake up the thread and check any pending timers. |
-static const int kTimerId = 1; |
- |
//----------------------------------------------------------------------------- |
// MessagePumpWin public: |
@@ -95,12 +96,14 @@ |
// MessagePumpForUI public: |
MessagePumpForUI::MessagePumpForUI() |
- : message_filter_(new MessageFilter), |
- window_(new win::MessageWindow()) { |
- CHECK(window_->Create(this)); |
+ : instance_(NULL), |
+ message_filter_(new MessageFilter) { |
+ InitMessageWnd(); |
} |
MessagePumpForUI::~MessagePumpForUI() { |
+ DestroyWindow(message_hwnd_); |
+ UnregisterClass(kWndClass, instance_); |
} |
void MessagePumpForUI::ScheduleWork() { |
@@ -108,7 +111,8 @@ |
return; // Someone else continued the pumping. |
// Make sure the MessagePump does some work for us. |
- BOOL ret = PostMessage(window_->hwnd(), kMsgHaveWork, 0, 0); |
+ BOOL ret = PostMessage(message_hwnd_, kMsgHaveWork, |
+ reinterpret_cast<WPARAM>(this), 0); |
if (ret) |
return; // There was room in the Window Message queue. |
@@ -155,7 +159,8 @@ |
// Create a WM_TIMER event that will wake us up to check for any pending |
// timers (in case we are running within a nested, external sub-pump). |
- BOOL ret = SetTimer(window_->hwnd(), kTimerId, delay_msec, NULL); |
+ BOOL ret = SetTimer(message_hwnd_, reinterpret_cast<UINT_PTR>(this), |
+ delay_msec, NULL); |
if (ret) |
return; |
// If we can't set timers, we are in big trouble... but cross our fingers for |
@@ -192,23 +197,18 @@ |
//----------------------------------------------------------------------------- |
// MessagePumpForUI private: |
-bool MessagePumpForUI::HandleMessage(HWND hwnd, |
- UINT message, |
- WPARAM wparam, |
- LPARAM lparam, |
- LRESULT* result) { |
+// static |
+LRESULT CALLBACK MessagePumpForUI::WndProcThunk( |
+ HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
switch (message) { |
case kMsgHaveWork: |
- HandleWorkMessage(); |
+ reinterpret_cast<MessagePumpForUI*>(wparam)->HandleWorkMessage(); |
break; |
- |
case WM_TIMER: |
- HandleTimerMessage(); |
+ reinterpret_cast<MessagePumpForUI*>(wparam)->HandleTimerMessage(); |
break; |
} |
- |
- // Do default processing for all messages. |
- return false; |
+ return DefWindowProc(hwnd, message, wparam, lparam); |
} |
void MessagePumpForUI::DoRunLoop() { |
@@ -249,7 +249,7 @@ |
// don't want to disturb that timer if it is already in flight. However, |
// if we did do all remaining delayed work, then lets kill the WM_TIMER. |
if (more_work_is_plausible && delayed_work_time_.is_null()) |
- KillTimer(window_->hwnd(), kTimerId); |
+ KillTimer(message_hwnd_, reinterpret_cast<UINT_PTR>(this)); |
if (state_->should_quit) |
break; |
@@ -267,6 +267,20 @@ |
} |
} |
+void MessagePumpForUI::InitMessageWnd() { |
+ WNDCLASSEX wc = {0}; |
+ wc.cbSize = sizeof(wc); |
+ wc.lpfnWndProc = base::win::WrappedWindowProc<WndProcThunk>; |
+ wc.hInstance = base::GetModuleFromAddress(wc.lpfnWndProc); |
+ wc.lpszClassName = kWndClass; |
+ instance_ = wc.hInstance; |
+ RegisterClassEx(&wc); |
+ |
+ message_hwnd_ = |
+ CreateWindow(kWndClass, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, instance_, 0); |
+ DCHECK(message_hwnd_); |
+} |
+ |
void MessagePumpForUI::WaitForWork() { |
// Wait until a message is available, up to the time needed by the timer |
// manager to fire the next set of timers. |
@@ -324,7 +338,7 @@ |
} |
void MessagePumpForUI::HandleTimerMessage() { |
- KillTimer(window_->hwnd(), kTimerId); |
+ KillTimer(message_hwnd_, reinterpret_cast<UINT_PTR>(this)); |
// If we are being called outside of the context of Run, then don't do |
// anything. This could correspond to a MessageBox call or something of |
@@ -368,7 +382,7 @@ |
} |
// While running our main message pump, we discard kMsgHaveWork messages. |
- if (msg.message == kMsgHaveWork && msg.hwnd == window_->hwnd()) |
+ if (msg.message == kMsgHaveWork && msg.hwnd == message_hwnd_) |
return ProcessPumpReplacementMessage(); |
if (CallMsgFilter(const_cast<MSG*>(&msg), kMessageFilterCode)) |
@@ -415,7 +429,7 @@ |
} |
DCHECK(!have_message || kMsgHaveWork != msg.message || |
- msg.hwnd != window_->hwnd()); |
+ msg.hwnd != message_hwnd_); |
// Since we discarded a kMsgHaveWork message, we must update the flag. |
int old_have_work = InterlockedExchange(&have_work_, 0); |