[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame^] | 1 | // Copyright 2008, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #ifndef BASE_MESSAGE_PUMP_WIN_H_ |
| 31 | #define BASE_MESSAGE_PUMP_WIN_H_ |
| 32 | |
| 33 | #include <vector> |
| 34 | |
| 35 | #include <windows.h> |
| 36 | |
| 37 | #include "base/lock.h" |
| 38 | #include "base/message_pump.h" |
| 39 | #include "base/observer_list.h" |
| 40 | #include "base/time.h" |
| 41 | |
| 42 | namespace base { |
| 43 | |
| 44 | // MessagePumpWin implements a "traditional" Windows message pump. It contains |
| 45 | // a nearly infinite loop that peeks out messages, and then dispatches them. |
| 46 | // Intermixed with those peeks are callouts to DoWork for pending tasks, |
| 47 | // DoDelayedWork for pending timers, and OnObjectSignaled for signaled objects. |
| 48 | // When there are no events to be serviced, this pump goes into a wait state. |
| 49 | // In most cases, this message pump handles all processing. |
| 50 | // |
| 51 | // However, when a task, or windows event, invokes on the stack a native dialog |
| 52 | // box or such, that window typically provides a bare bones (native?) message |
| 53 | // pump. That bare-bones message pump generally supports little more than a |
| 54 | // peek of the Windows message queue, followed by a dispatch of the peeked |
| 55 | // message. MessageLoop extends that bare-bones message pump to also service |
| 56 | // Tasks, at the cost of some complexity. |
| 57 | // |
| 58 | // The basic structure of the extension (refered to as a sub-pump) is that a |
| 59 | // special message, kMsgHaveWork, is repeatedly injected into the Windows |
| 60 | // Message queue. Each time the kMsgHaveWork message is peeked, checks are |
| 61 | // made for an extended set of events, including the availability of Tasks to |
| 62 | // run. |
| 63 | // |
| 64 | // After running a task, the special message kMsgHaveWork is again posted to |
| 65 | // the Windows Message queue, ensuring a future time slice for processing a |
| 66 | // future event. To prevent flooding the Windows Message queue, care is taken |
| 67 | // to be sure that at most one kMsgHaveWork message is EVER pending in the |
| 68 | // Window's Message queue. |
| 69 | // |
| 70 | // There are a few additional complexities in this system where, when there are |
| 71 | // no Tasks to run, this otherwise infinite stream of messages which drives the |
| 72 | // sub-pump is halted. The pump is automatically re-started when Tasks are |
| 73 | // queued. |
| 74 | // |
| 75 | // A second complexity is that the presence of this stream of posted tasks may |
| 76 | // prevent a bare-bones message pump from ever peeking a WM_PAINT or WM_TIMER. |
| 77 | // Such paint and timer events always give priority to a posted message, such as |
| 78 | // kMsgHaveWork messages. As a result, care is taken to do some peeking in |
| 79 | // between the posting of each kMsgHaveWork message (i.e., after kMsgHaveWork |
| 80 | // is peeked, and before a replacement kMsgHaveWork is posted). |
| 81 | // |
| 82 | // NOTE: Although it may seem odd that messages are used to start and stop this |
| 83 | // flow (as opposed to signaling objects, etc.), it should be understood that |
| 84 | // the native message pump will *only* respond to messages. As a result, it is |
| 85 | // an excellent choice. It is also helpful that the starter messages that are |
| 86 | // placed in the queue when new task arrive also awakens DoRunLoop. |
| 87 | // |
| 88 | class MessagePumpWin : public MessagePump { |
| 89 | public: |
| 90 | // Used with WatchObject to asynchronously monitor the signaled state of a |
| 91 | // HANDLE object. |
| 92 | class Watcher { |
| 93 | public: |
| 94 | virtual ~Watcher() {} |
| 95 | // Called from MessageLoop::Run when a signalled object is detected. |
| 96 | virtual void OnObjectSignaled(HANDLE object) = 0; |
| 97 | }; |
| 98 | |
| 99 | // An Observer is an object that receives global notifications from the |
| 100 | // MessageLoop. |
| 101 | // |
| 102 | // NOTE: An Observer implementation should be extremely fast! |
| 103 | // |
| 104 | class Observer { |
| 105 | public: |
| 106 | virtual ~Observer() {} |
| 107 | |
| 108 | // This method is called before processing a message. |
| 109 | // The message may be undefined in which case msg.message is 0 |
| 110 | virtual void WillProcessMessage(const MSG& msg) = 0; |
| 111 | |
| 112 | // This method is called when control returns from processing a UI message. |
| 113 | // The message may be undefined in which case msg.message is 0 |
| 114 | virtual void DidProcessMessage(const MSG& msg) = 0; |
| 115 | }; |
| 116 | |
| 117 | // Dispatcher is used during a nested invocation of Run to dispatch events. |
| 118 | // If Run is invoked with a non-NULL Dispatcher, MessageLoop does not |
| 119 | // dispatch events (or invoke TranslateMessage), rather every message is |
| 120 | // passed to Dispatcher's Dispatch method for dispatch. It is up to the |
| 121 | // Dispatcher to dispatch, or not, the event. |
| 122 | // |
| 123 | // The nested loop is exited by either posting a quit, or returning false |
| 124 | // from Dispatch. |
| 125 | class Dispatcher { |
| 126 | public: |
| 127 | virtual ~Dispatcher() {} |
| 128 | // Dispatches the event. If true is returned processing continues as |
| 129 | // normal. If false is returned, the nested loop exits immediately. |
| 130 | virtual bool Dispatch(const MSG& msg) = 0; |
| 131 | }; |
| 132 | |
| 133 | MessagePumpWin(); |
| 134 | ~MessagePumpWin(); |
| 135 | |
| 136 | // Have the current thread's message loop watch for a signaled object. |
| 137 | // Pass a null watcher to stop watching the object. |
| 138 | void WatchObject(HANDLE, Watcher*); |
| 139 | |
| 140 | // Add an Observer, which will start receiving notifications immediately. |
| 141 | void AddObserver(Observer* observer); |
| 142 | |
| 143 | // Remove an Observer. It is safe to call this method while an Observer is |
| 144 | // receiving a notification callback. |
| 145 | void RemoveObserver(Observer* observer); |
| 146 | |
| 147 | // Give a chance to code processing additional messages to notify the |
| 148 | // message loop observers that another message has been processed. |
| 149 | void WillProcessMessage(const MSG& msg); |
| 150 | void DidProcessMessage(const MSG& msg); |
| 151 | |
| 152 | // Applications can call this to encourage us to process all pending WM_PAINT |
| 153 | // messages. This method will process all paint messages the Windows Message |
| 154 | // queue can provide, up to some fixed number (to avoid any infinite loops). |
| 155 | void PumpOutPendingPaintMessages(); |
| 156 | |
| 157 | // Like MessagePump::Run, but MSG objects are routed through dispatcher. |
| 158 | void RunWithDispatcher(Delegate* delegate, Dispatcher* dispatcher); |
| 159 | |
| 160 | // MessagePump methods: |
| 161 | virtual void Run(Delegate* delegate) { RunWithDispatcher(delegate, NULL); } |
| 162 | virtual void Quit(); |
| 163 | virtual void ScheduleWork(); |
| 164 | virtual void ScheduleDelayedWork(const TimeDelta& delay); |
| 165 | |
| 166 | private: |
| 167 | struct RunState { |
| 168 | Delegate* delegate; |
| 169 | Dispatcher* dispatcher; |
| 170 | |
| 171 | // Used to flag that the current Run() invocation should return ASAP. |
| 172 | bool should_quit; |
| 173 | |
| 174 | // Used to count how many Run() invocations are on the stack. |
| 175 | int run_depth; |
| 176 | }; |
| 177 | |
| 178 | static LRESULT CALLBACK WndProcThunk( |
| 179 | HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
| 180 | void InitMessageWnd(); |
| 181 | void HandleWorkMessage(); |
| 182 | void HandleTimerMessage(); |
| 183 | void DoRunLoop(); |
| 184 | void WaitForWork(); |
| 185 | bool ProcessNextWindowsMessage(); |
| 186 | bool ProcessMessageHelper(const MSG& msg); |
| 187 | bool ProcessPumpReplacementMessage(); |
| 188 | bool ProcessNextObject(); |
| 189 | bool SignalWatcher(size_t object_index); |
| 190 | int GetCurrentDelay() const; |
| 191 | |
| 192 | // A hidden message-only window. |
| 193 | HWND message_hwnd_; |
| 194 | |
| 195 | // A vector of objects (and corresponding watchers) that are routinely |
| 196 | // serviced by this message pump. |
| 197 | std::vector<HANDLE> objects_; |
| 198 | std::vector<Watcher*> watchers_; |
| 199 | |
| 200 | ObserverList<Observer> observers_; |
| 201 | |
| 202 | // The time at which delayed work should run. |
| 203 | Time delayed_work_time_; |
| 204 | |
| 205 | // A boolean value used to indicate if there is a kMsgDoWork message pending |
| 206 | // in the Windows Message queue. There is at most one such message, and it |
| 207 | // can drive execution of tasks when a native message pump is running. |
| 208 | LONG have_work_; |
| 209 | |
| 210 | // State for the current invocation of Run. |
| 211 | RunState* state_; |
| 212 | }; |
| 213 | |
| 214 | } // namespace base |
| 215 | |
| 216 | #endif // BASE_MESSAGE_PUMP_WIN_H_ |