OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_WIN_H_ | 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_WIN_H_ |
6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_WIN_H_ | 6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_WIN_H_ |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include <list> | 10 #include <list> |
11 | 11 |
12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/message_loop/message_pump.h" | 15 #include "base/message_loop/message_pump.h" |
15 #include "base/message_loop/message_pump_dispatcher.h" | 16 #include "base/message_loop/message_pump_dispatcher.h" |
16 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "base/win/scoped_handle.h" | 19 #include "base/win/scoped_handle.h" |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 | 22 |
| 23 class Thread; |
| 24 |
22 // MessagePumpWin serves as the base for specialized versions of the MessagePump | 25 // MessagePumpWin serves as the base for specialized versions of the MessagePump |
23 // for Windows. It provides basic functionality like handling of observers and | 26 // for Windows. It provides basic functionality like handling of observers and |
24 // controlling the lifetime of the message pump. | 27 // controlling the lifetime of the message pump. |
25 class BASE_EXPORT MessagePumpWin : public MessagePump { | 28 class BASE_EXPORT MessagePumpWin : public MessagePump { |
26 public: | 29 public: |
27 MessagePumpWin() : have_work_(0), state_(NULL) {} | 30 MessagePumpWin() : have_work_(0), state_(NULL) {} |
28 | 31 |
29 // Like MessagePump::Run, but MSG objects are routed through dispatcher. | 32 // Like MessagePump::Run, but MSG objects are routed through dispatcher. |
30 void RunWithDispatcher(Delegate* delegate, MessagePumpDispatcher* dispatcher); | 33 void RunWithDispatcher(Delegate* delegate, MessagePumpDispatcher* dispatcher); |
31 | 34 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 void DoRunLoop() override; | 131 void DoRunLoop() override; |
129 void InitMessageWnd(); | 132 void InitMessageWnd(); |
130 void WaitForWork(); | 133 void WaitForWork(); |
131 void HandleWorkMessage(); | 134 void HandleWorkMessage(); |
132 void HandleTimerMessage(); | 135 void HandleTimerMessage(); |
133 void RescheduleTimer(); | 136 void RescheduleTimer(); |
134 bool ProcessNextWindowsMessage(); | 137 bool ProcessNextWindowsMessage(); |
135 bool ProcessMessageHelper(const MSG& msg); | 138 bool ProcessMessageHelper(const MSG& msg); |
136 bool ProcessPumpReplacementMessage(); | 139 bool ProcessPumpReplacementMessage(); |
137 | 140 |
| 141 // We spawn a worker thread to periodically post (3 ms) the kMsgHaveWork |
| 142 // message to the UI message pump. This is to ensure that the main thread |
| 143 // gets to process tasks and delayed tasks when there is no activity in the |
| 144 // Windows message pump or when there is a nested modal loop (sizing/moving/ |
| 145 // drag drop/message boxes) etc. |
| 146 void DoWorkerThreadRunLoop(); |
| 147 |
| 148 // This function is posted as part of a user mode APC to shutdown the worker |
| 149 // thread when the main message pump is shutting down. |
| 150 static void CALLBACK ShutdownWorkerThread(ULONG_PTR param); |
| 151 |
| 152 // Helper function for posting the kMsgHaveWork message to wake up the pump |
| 153 // for processing tasks. |
| 154 void PostWorkMessage(); |
| 155 |
| 156 // Helper function to set the waitable timer used to wake up the UI worker |
| 157 // thread for processing delayed tasks. |
| 158 // |delay_ms| : The delay in milliseconds. |
| 159 void SetWakeupTimer(int64 delay_ms); |
| 160 |
138 // Atom representing the registered window class. | 161 // Atom representing the registered window class. |
139 ATOM atom_; | 162 ATOM atom_; |
140 | 163 |
141 // A hidden message-only window. | 164 // A hidden message-only window. |
142 HWND message_hwnd_; | 165 HWND message_hwnd_; |
| 166 |
| 167 // This thread is used to periodically wake up the main thread to process |
| 168 // tasks. |
| 169 scoped_ptr<base::Thread> ui_worker_thread_; |
| 170 |
| 171 // The UI worker thread waits on this timer indefinitely. When the main |
| 172 // thread has tasks ready to be processed it sets the timer. |
| 173 base::win::ScopedHandle ui_worker_thread_timer_; |
143 }; | 174 }; |
144 | 175 |
145 //----------------------------------------------------------------------------- | 176 //----------------------------------------------------------------------------- |
146 // MessagePumpForIO extends MessagePumpWin with methods that are particular to a | 177 // MessagePumpForIO extends MessagePumpWin with methods that are particular to a |
147 // MessageLoop instantiated with TYPE_IO. This version of MessagePump does not | 178 // MessageLoop instantiated with TYPE_IO. This version of MessagePump does not |
148 // deal with Windows mesagges, and instead has a Run loop based on Completion | 179 // deal with Windows mesagges, and instead has a Run loop based on Completion |
149 // Ports so it is better suited for IO operations. | 180 // Ports so it is better suited for IO operations. |
150 // | 181 // |
151 class BASE_EXPORT MessagePumpForIO : public MessagePumpWin { | 182 class BASE_EXPORT MessagePumpForIO : public MessagePumpWin { |
152 public: | 183 public: |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 // This list will be empty almost always. It stores IO completions that have | 363 // This list will be empty almost always. It stores IO completions that have |
333 // not been delivered yet because somebody was doing cleanup. | 364 // not been delivered yet because somebody was doing cleanup. |
334 std::list<IOItem> completed_io_; | 365 std::list<IOItem> completed_io_; |
335 | 366 |
336 ObserverList<IOObserver> io_observers_; | 367 ObserverList<IOObserver> io_observers_; |
337 }; | 368 }; |
338 | 369 |
339 } // namespace base | 370 } // namespace base |
340 | 371 |
341 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_WIN_H_ | 372 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_WIN_H_ |
OLD | NEW |