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