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/memory/scoped_ptr.h" |
15 #include "base/message_loop/message_pump.h" | 15 #include "base/message_loop/message_pump.h" |
16 #include "base/message_loop/message_pump_dispatcher.h" | 16 #include "base/message_loop/message_pump_dispatcher.h" |
17 #include "base/message_loop/message_pump_observer.h" | 17 #include "base/message_loop/message_pump_observer.h" |
18 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
| 19 #include "base/synchronization/lock.h" |
19 #include "base/time.h" | 20 #include "base/time.h" |
20 #include "base/win/scoped_handle.h" | 21 #include "base/win/scoped_handle.h" |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 | 24 |
24 // MessagePumpWin serves as the base for specialized versions of the MessagePump | 25 // MessagePumpWin serves as the base for specialized versions of the MessagePump |
25 // for Windows. It provides basic functionality like handling of observers and | 26 // for Windows. It provides basic functionality like handling of observers and |
26 // controlling the lifetime of the message pump. | 27 // controlling the lifetime of the message pump. |
27 class BASE_EXPORT MessagePumpWin : public MessagePump { | 28 class BASE_EXPORT MessagePumpWin : public MessagePump { |
28 public: | 29 public: |
29 MessagePumpWin() : have_work_(0), state_(NULL) {} | 30 MessagePumpWin() : have_work_(0), state_(NULL) {} |
30 virtual ~MessagePumpWin() {} | 31 virtual ~MessagePumpWin() {} |
31 | 32 |
32 // Add an Observer, which will start receiving notifications immediately. | 33 // Add an Observer, which will start receiving notifications immediately. |
33 void AddObserver(MessagePumpObserver* observer); | 34 void AddObserver(MessagePumpObserver* observer); |
34 | 35 |
35 // Remove an Observer. It is safe to call this method while an Observer is | 36 // Remove an Observer. It is safe to call this method while an Observer is |
36 // receiving a notification callback. | 37 // receiving a notification callback. |
37 void RemoveObserver(MessagePumpObserver* observer); | 38 void RemoveObserver(MessagePumpObserver* observer); |
38 | 39 |
39 // Give a chance to code processing additional messages to notify the | 40 // Give a chance to code processing additional messages to notify the |
40 // message loop observers that another message has been processed. | 41 // message loop observers that another message has been processed. |
41 void WillProcessMessage(const MSG& msg); | 42 void WillProcessMessage(const MSG& msg); |
42 void DidProcessMessage(const MSG& msg); | 43 void DidProcessMessage(const MSG& msg); |
43 | 44 |
44 // Like MessagePump::Run, but MSG objects are routed through dispatcher. | 45 // Like MessagePump::Run, but MSG objects are routed through dispatcher. |
45 void RunWithDispatcher(Delegate* delegate, MessagePumpDispatcher* dispatcher); | 46 void RunWithDispatcher(Delegate* delegate, MessagePumpDispatcher* dispatcher); |
46 | 47 |
47 // MessagePump methods: | 48 // MessagePump methods: |
48 virtual void Run(Delegate* delegate) { RunWithDispatcher(delegate, NULL); } | 49 virtual void Run(Delegate* delegate) OVERRIDE; |
49 virtual void Quit(); | 50 virtual void Quit() OVERRIDE; |
50 | 51 |
51 protected: | 52 protected: |
52 struct RunState { | 53 struct RunState { |
53 Delegate* delegate; | 54 Delegate* delegate; |
54 MessagePumpDispatcher* dispatcher; | 55 MessagePumpDispatcher* dispatcher; |
55 | 56 |
56 // Used to flag that the current Run() invocation should return ASAP. | 57 // Used to flag that the current Run() invocation should return ASAP. |
57 bool should_quit; | 58 bool should_quit; |
58 | 59 |
59 // Used to count how many Run() invocations are on the stack. | 60 // Used to count how many Run() invocations are on the stack. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 | 160 |
160 MessagePumpForUI(); | 161 MessagePumpForUI(); |
161 virtual ~MessagePumpForUI(); | 162 virtual ~MessagePumpForUI(); |
162 | 163 |
163 // Sets a new MessageFilter. MessagePumpForUI takes ownership of | 164 // Sets a new MessageFilter. MessagePumpForUI takes ownership of |
164 // |message_filter|. When SetMessageFilter is called, old MessageFilter is | 165 // |message_filter|. When SetMessageFilter is called, old MessageFilter is |
165 // deleted. | 166 // deleted. |
166 void SetMessageFilter(scoped_ptr<MessageFilter> message_filter); | 167 void SetMessageFilter(scoped_ptr<MessageFilter> message_filter); |
167 | 168 |
168 // MessagePump methods: | 169 // MessagePump methods: |
169 virtual void ScheduleWork(); | 170 virtual void ScheduleWork() OVERRIDE; |
170 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time); | 171 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) OVERRIDE; |
| 172 virtual void Shutdown() OVERRIDE; |
171 | 173 |
172 // Applications can call this to encourage us to process all pending WM_PAINT | 174 // Applications can call this to encourage us to process all pending WM_PAINT |
173 // messages. This method will process all paint messages the Windows Message | 175 // messages. This method will process all paint messages the Windows Message |
174 // queue can provide, up to some fixed number (to avoid any infinite loops). | 176 // queue can provide, up to some fixed number (to avoid any infinite loops). |
175 void PumpOutPendingPaintMessages(); | 177 void PumpOutPendingPaintMessages(); |
176 | 178 |
177 private: | 179 private: |
178 static LRESULT CALLBACK WndProcThunk(HWND window_handle, | 180 static LRESULT CALLBACK WndProcThunk(HWND window_handle, |
179 UINT message, | 181 UINT message, |
180 WPARAM wparam, | 182 WPARAM wparam, |
181 LPARAM lparam); | 183 LPARAM lparam); |
182 virtual void DoRunLoop(); | 184 virtual void DoRunLoop(); |
183 void InitMessageWnd(); | 185 void InitMessageWnd(); |
184 void WaitForWork(); | 186 void WaitForWork(); |
185 void HandleWorkMessage(); | 187 void HandleWorkMessage(); |
186 void HandleTimerMessage(); | 188 void HandleTimerMessage(); |
187 bool ProcessNextWindowsMessage(); | 189 bool ProcessNextWindowsMessage(); |
188 bool ProcessMessageHelper(const MSG& msg); | 190 bool ProcessMessageHelper(const MSG& msg); |
189 bool ProcessPumpReplacementMessage(); | 191 bool ProcessPumpReplacementMessage(); |
190 | 192 |
191 // Atom representing the registered window class. | 193 // Atom representing the registered window class. |
192 ATOM atom_; | 194 ATOM atom_; |
193 | 195 |
194 // A hidden message-only window. | 196 // A hidden message-only window. |
195 HWND message_hwnd_; | 197 HWND message_hwnd_; |
196 | 198 |
| 199 // Protectes access to |message_hwnd_|. |
| 200 base::Lock message_hwnd_lock_; |
| 201 |
197 scoped_ptr<MessageFilter> message_filter_; | 202 scoped_ptr<MessageFilter> message_filter_; |
198 }; | 203 }; |
199 | 204 |
200 //----------------------------------------------------------------------------- | 205 //----------------------------------------------------------------------------- |
201 // MessagePumpForIO extends MessagePumpWin with methods that are particular to a | 206 // MessagePumpForIO extends MessagePumpWin with methods that are particular to a |
202 // MessageLoop instantiated with TYPE_IO. This version of MessagePump does not | 207 // MessageLoop instantiated with TYPE_IO. This version of MessagePump does not |
203 // deal with Windows mesagges, and instead has a Run loop based on Completion | 208 // deal with Windows mesagges, and instead has a Run loop based on Completion |
204 // Ports so it is better suited for IO operations. | 209 // Ports so it is better suited for IO operations. |
205 // | 210 // |
206 class BASE_EXPORT MessagePumpForIO : public MessagePumpWin { | 211 class BASE_EXPORT MessagePumpForIO : public MessagePumpWin { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 OVERLAPPED overlapped; | 325 OVERLAPPED overlapped; |
321 IOHandler* handler; | 326 IOHandler* handler; |
322 }; | 327 }; |
323 | 328 |
324 MessagePumpForIO(); | 329 MessagePumpForIO(); |
325 virtual ~MessagePumpForIO() {} | 330 virtual ~MessagePumpForIO() {} |
326 | 331 |
327 // MessagePump methods: | 332 // MessagePump methods: |
328 virtual void ScheduleWork(); | 333 virtual void ScheduleWork(); |
329 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time); | 334 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time); |
| 335 virtual void Shutdown() OVERRIDE; |
330 | 336 |
331 // Register the handler to be used when asynchronous IO for the given file | 337 // Register the handler to be used when asynchronous IO for the given file |
332 // completes. The registration persists as long as |file_handle| is valid, so | 338 // completes. The registration persists as long as |file_handle| is valid, so |
333 // |handler| must be valid as long as there is pending IO for the given file. | 339 // |handler| must be valid as long as there is pending IO for the given file. |
334 void RegisterIOHandler(HANDLE file_handle, IOHandler* handler); | 340 void RegisterIOHandler(HANDLE file_handle, IOHandler* handler); |
335 | 341 |
336 // Register the handler to be used to process job events. The registration | 342 // Register the handler to be used to process job events. The registration |
337 // persists as long as the job object is live, so |handler| must be valid | 343 // persists as long as the job object is live, so |handler| must be valid |
338 // until the job object is destroyed. Returns true if the registration | 344 // until the job object is destroyed. Returns true if the registration |
339 // succeeded, and false otherwise. | 345 // succeeded, and false otherwise. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 // This list will be empty almost always. It stores IO completions that have | 393 // This list will be empty almost always. It stores IO completions that have |
388 // not been delivered yet because somebody was doing cleanup. | 394 // not been delivered yet because somebody was doing cleanup. |
389 std::list<IOItem> completed_io_; | 395 std::list<IOItem> completed_io_; |
390 | 396 |
391 ObserverList<IOObserver> io_observers_; | 397 ObserverList<IOObserver> io_observers_; |
392 }; | 398 }; |
393 | 399 |
394 } // namespace base | 400 } // namespace base |
395 | 401 |
396 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_WIN_H_ | 402 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_WIN_H_ |
OLD | NEW |