license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_MESSAGE_PUMP_WIN_H_ |
| 6 | #define BASE_MESSAGE_PUMP_WIN_H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <windows.h> |
| 11 | |
| 12 | #include "base/lock.h" |
| 13 | #include "base/message_pump.h" |
| 14 | #include "base/observer_list.h" |
| 15 | #include "base/time.h" |
| 16 | |
| 17 | namespace base { |
| 18 | |
| 19 | // MessagePumpWin implements a "traditional" Windows message pump. It contains |
| 20 | // a nearly infinite loop that peeks out messages, and then dispatches them. |
| 21 | // Intermixed with those peeks are callouts to DoWork for pending tasks, |
| 22 | // DoDelayedWork for pending timers, and OnObjectSignaled for signaled objects. |
| 23 | // When there are no events to be serviced, this pump goes into a wait state. |
| 24 | // In most cases, this message pump handles all processing. |
| 25 | // |
| 26 | // However, when a task, or windows event, invokes on the stack a native dialog |
| 27 | // box or such, that window typically provides a bare bones (native?) message |
| 28 | // pump. That bare-bones message pump generally supports little more than a |
| 29 | // peek of the Windows message queue, followed by a dispatch of the peeked |
| 30 | // message. MessageLoop extends that bare-bones message pump to also service |
| 31 | // Tasks, at the cost of some complexity. |
| 32 | // |
| 33 | // The basic structure of the extension (refered to as a sub-pump) is that a |
| 34 | // special message, kMsgHaveWork, is repeatedly injected into the Windows |
| 35 | // Message queue. Each time the kMsgHaveWork message is peeked, checks are |
| 36 | // made for an extended set of events, including the availability of Tasks to |
| 37 | // run. |
| 38 | // |
| 39 | // After running a task, the special message kMsgHaveWork is again posted to |
| 40 | // the Windows Message queue, ensuring a future time slice for processing a |
| 41 | // future event. To prevent flooding the Windows Message queue, care is taken |
| 42 | // to be sure that at most one kMsgHaveWork message is EVER pending in the |
| 43 | // Window's Message queue. |
| 44 | // |
| 45 | // There are a few additional complexities in this system where, when there are |
| 46 | // no Tasks to run, this otherwise infinite stream of messages which drives the |
| 47 | // sub-pump is halted. The pump is automatically re-started when Tasks are |
| 48 | // queued. |
| 49 | // |
| 50 | // A second complexity is that the presence of this stream of posted tasks may |
| 51 | // prevent a bare-bones message pump from ever peeking a WM_PAINT or WM_TIMER. |
| 52 | // Such paint and timer events always give priority to a posted message, such as |
| 53 | // kMsgHaveWork messages. As a result, care is taken to do some peeking in |
| 54 | // between the posting of each kMsgHaveWork message (i.e., after kMsgHaveWork |
| 55 | // is peeked, and before a replacement kMsgHaveWork is posted). |
| 56 | // |
| 57 | // NOTE: Although it may seem odd that messages are used to start and stop this |
| 58 | // flow (as opposed to signaling objects, etc.), it should be understood that |
| 59 | // the native message pump will *only* respond to messages. As a result, it is |
| 60 | // an excellent choice. It is also helpful that the starter messages that are |
| 61 | // placed in the queue when new task arrive also awakens DoRunLoop. |
| 62 | // |
| 63 | class MessagePumpWin : public MessagePump { |
| 64 | public: |
| 65 | // Used with WatchObject to asynchronously monitor the signaled state of a |
| 66 | // HANDLE object. |
| 67 | class Watcher { |
| 68 | public: |
| 69 | virtual ~Watcher() {} |
| 70 | // Called from MessageLoop::Run when a signalled object is detected. |
| 71 | virtual void OnObjectSignaled(HANDLE object) = 0; |
| 72 | }; |
| 73 | |
| 74 | // An Observer is an object that receives global notifications from the |
| 75 | // MessageLoop. |
| 76 | // |
| 77 | // NOTE: An Observer implementation should be extremely fast! |
| 78 | // |
| 79 | class Observer { |
| 80 | public: |
| 81 | virtual ~Observer() {} |
| 82 | |
| 83 | // This method is called before processing a message. |
| 84 | // The message may be undefined in which case msg.message is 0 |
| 85 | virtual void WillProcessMessage(const MSG& msg) = 0; |
| 86 | |
| 87 | // This method is called when control returns from processing a UI message. |
| 88 | // The message may be undefined in which case msg.message is 0 |
| 89 | virtual void DidProcessMessage(const MSG& msg) = 0; |
| 90 | }; |
| 91 | |
| 92 | // Dispatcher is used during a nested invocation of Run to dispatch events. |
| 93 | // If Run is invoked with a non-NULL Dispatcher, MessageLoop does not |
| 94 | // dispatch events (or invoke TranslateMessage), rather every message is |
| 95 | // passed to Dispatcher's Dispatch method for dispatch. It is up to the |
| 96 | // Dispatcher to dispatch, or not, the event. |
| 97 | // |
| 98 | // The nested loop is exited by either posting a quit, or returning false |
| 99 | // from Dispatch. |
| 100 | class Dispatcher { |
| 101 | public: |
| 102 | virtual ~Dispatcher() {} |
| 103 | // Dispatches the event. If true is returned processing continues as |
| 104 | // normal. If false is returned, the nested loop exits immediately. |
| 105 | virtual bool Dispatch(const MSG& msg) = 0; |
| 106 | }; |
| 107 | |
| 108 | MessagePumpWin(); |
| 109 | ~MessagePumpWin(); |
| 110 | |
| 111 | // Have the current thread's message loop watch for a signaled object. |
| 112 | // Pass a null watcher to stop watching the object. |
| 113 | void WatchObject(HANDLE, Watcher*); |
| 114 | |
| 115 | // Add an Observer, which will start receiving notifications immediately. |
| 116 | void AddObserver(Observer* observer); |
| 117 | |
| 118 | // Remove an Observer. It is safe to call this method while an Observer is |
| 119 | // receiving a notification callback. |
| 120 | void RemoveObserver(Observer* observer); |
| 121 | |
| 122 | // Give a chance to code processing additional messages to notify the |
| 123 | // message loop observers that another message has been processed. |
| 124 | void WillProcessMessage(const MSG& msg); |
| 125 | void DidProcessMessage(const MSG& msg); |
| 126 | |
| 127 | // Applications can call this to encourage us to process all pending WM_PAINT |
| 128 | // messages. This method will process all paint messages the Windows Message |
| 129 | // queue can provide, up to some fixed number (to avoid any infinite loops). |
| 130 | void PumpOutPendingPaintMessages(); |
| 131 | |
| 132 | // Like MessagePump::Run, but MSG objects are routed through dispatcher. |
| 133 | void RunWithDispatcher(Delegate* delegate, Dispatcher* dispatcher); |
| 134 | |
| 135 | // MessagePump methods: |
| 136 | virtual void Run(Delegate* delegate) { RunWithDispatcher(delegate, NULL); } |
| 137 | virtual void Quit(); |
| 138 | virtual void ScheduleWork(); |
[email protected] | b24250fc | 2008-08-20 06:30:58 | [diff] [blame] | 139 | virtual void ScheduleDelayedWork(const Time& delayed_work_time); |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 140 | |
| 141 | private: |
| 142 | struct RunState { |
| 143 | Delegate* delegate; |
| 144 | Dispatcher* dispatcher; |
| 145 | |
| 146 | // Used to flag that the current Run() invocation should return ASAP. |
| 147 | bool should_quit; |
| 148 | |
| 149 | // Used to count how many Run() invocations are on the stack. |
| 150 | int run_depth; |
| 151 | }; |
| 152 | |
| 153 | static LRESULT CALLBACK WndProcThunk( |
| 154 | HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
| 155 | void InitMessageWnd(); |
| 156 | void HandleWorkMessage(); |
| 157 | void HandleTimerMessage(); |
| 158 | void DoRunLoop(); |
| 159 | void WaitForWork(); |
| 160 | bool ProcessNextWindowsMessage(); |
| 161 | bool ProcessMessageHelper(const MSG& msg); |
| 162 | bool ProcessPumpReplacementMessage(); |
| 163 | bool ProcessNextObject(); |
| 164 | bool SignalWatcher(size_t object_index); |
| 165 | int GetCurrentDelay() const; |
| 166 | |
| 167 | // A hidden message-only window. |
| 168 | HWND message_hwnd_; |
| 169 | |
| 170 | // A vector of objects (and corresponding watchers) that are routinely |
| 171 | // serviced by this message pump. |
| 172 | std::vector<HANDLE> objects_; |
| 173 | std::vector<Watcher*> watchers_; |
| 174 | |
| 175 | ObserverList<Observer> observers_; |
| 176 | |
| 177 | // The time at which delayed work should run. |
| 178 | Time delayed_work_time_; |
| 179 | |
| 180 | // A boolean value used to indicate if there is a kMsgDoWork message pending |
| 181 | // in the Windows Message queue. There is at most one such message, and it |
| 182 | // can drive execution of tasks when a native message pump is running. |
| 183 | LONG have_work_; |
| 184 | |
| 185 | // State for the current invocation of Run. |
| 186 | RunState* state_; |
| 187 | }; |
| 188 | |
| 189 | } // namespace base |
| 190 | |
| 191 | #endif // BASE_MESSAGE_PUMP_WIN_H_ |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 192 | |