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 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 8 | #include <windows.h> |
| 9 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 10 | #include <list> |
| 11 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 12 | #include "base/lock.h" |
| 13 | #include "base/message_pump.h" |
| 14 | #include "base/observer_list.h" |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 15 | #include "base/scoped_handle.h" |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 16 | #include "base/time.h" |
| 17 | |
| 18 | namespace base { |
| 19 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 20 | // MessagePumpWin serves as the base for specialized versions of the MessagePump |
| 21 | // for Windows. It provides basic functionality like handling of observers and |
| 22 | // controlling the lifetime of the message pump. |
| 23 | class MessagePumpWin : public MessagePump { |
| 24 | public: |
| 25 | // An Observer is an object that receives global notifications from the |
| 26 | // MessageLoop. |
| 27 | // |
| 28 | // NOTE: An Observer implementation should be extremely fast! |
| 29 | // |
| 30 | class Observer { |
| 31 | public: |
| 32 | virtual ~Observer() {} |
| 33 | |
| 34 | // This method is called before processing a message. |
| 35 | // The message may be undefined in which case msg.message is 0 |
| 36 | virtual void WillProcessMessage(const MSG& msg) = 0; |
| 37 | |
| 38 | // This method is called when control returns from processing a UI message. |
| 39 | // The message may be undefined in which case msg.message is 0 |
| 40 | virtual void DidProcessMessage(const MSG& msg) = 0; |
| 41 | }; |
| 42 | |
| 43 | // Dispatcher is used during a nested invocation of Run to dispatch events. |
| 44 | // If Run is invoked with a non-NULL Dispatcher, MessageLoop does not |
| 45 | // dispatch events (or invoke TranslateMessage), rather every message is |
| 46 | // passed to Dispatcher's Dispatch method for dispatch. It is up to the |
| 47 | // Dispatcher to dispatch, or not, the event. |
| 48 | // |
| 49 | // The nested loop is exited by either posting a quit, or returning false |
| 50 | // from Dispatch. |
| 51 | class Dispatcher { |
| 52 | public: |
| 53 | virtual ~Dispatcher() {} |
| 54 | // Dispatches the event. If true is returned processing continues as |
| 55 | // normal. If false is returned, the nested loop exits immediately. |
| 56 | virtual bool Dispatch(const MSG& msg) = 0; |
| 57 | }; |
| 58 | |
| 59 | MessagePumpWin() : have_work_(0), state_(NULL) {} |
| 60 | virtual ~MessagePumpWin() {} |
| 61 | |
| 62 | // Add an Observer, which will start receiving notifications immediately. |
| 63 | void AddObserver(Observer* observer); |
| 64 | |
| 65 | // Remove an Observer. It is safe to call this method while an Observer is |
| 66 | // receiving a notification callback. |
| 67 | void RemoveObserver(Observer* observer); |
| 68 | |
| 69 | // Give a chance to code processing additional messages to notify the |
| 70 | // message loop observers that another message has been processed. |
| 71 | void WillProcessMessage(const MSG& msg); |
| 72 | void DidProcessMessage(const MSG& msg); |
| 73 | |
| 74 | // Like MessagePump::Run, but MSG objects are routed through dispatcher. |
| 75 | void RunWithDispatcher(Delegate* delegate, Dispatcher* dispatcher); |
| 76 | |
| 77 | // MessagePump methods: |
| 78 | virtual void Run(Delegate* delegate) { RunWithDispatcher(delegate, NULL); } |
| 79 | virtual void Quit(); |
| 80 | |
| 81 | protected: |
| 82 | struct RunState { |
| 83 | Delegate* delegate; |
| 84 | Dispatcher* dispatcher; |
| 85 | |
| 86 | // Used to flag that the current Run() invocation should return ASAP. |
| 87 | bool should_quit; |
| 88 | |
| 89 | // Used to count how many Run() invocations are on the stack. |
| 90 | int run_depth; |
| 91 | }; |
| 92 | |
| 93 | virtual void DoRunLoop() = 0; |
| 94 | int GetCurrentDelay() const; |
| 95 | |
| 96 | ObserverList<Observer> observers_; |
| 97 | |
| 98 | // The time at which delayed work should run. |
| 99 | Time delayed_work_time_; |
| 100 | |
| 101 | // A boolean value used to indicate if there is a kMsgDoWork message pending |
| 102 | // in the Windows Message queue. There is at most one such message, and it |
| 103 | // can drive execution of tasks when a native message pump is running. |
| 104 | LONG have_work_; |
| 105 | |
| 106 | // State for the current invocation of Run. |
| 107 | RunState* state_; |
| 108 | }; |
| 109 | |
| 110 | //----------------------------------------------------------------------------- |
| 111 | // MessagePumpForUI extends MessagePumpWin with methods that are particular to a |
| 112 | // MessageLoop instantiated with TYPE_UI. |
| 113 | // |
| 114 | // MessagePumpForUI implements a "traditional" Windows message pump. It contains |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 115 | // a nearly infinite loop that peeks out messages, and then dispatches them. |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 116 | // Intermixed with those peeks are callouts to DoWork for pending tasks, and |
| 117 | // DoDelayedWork for pending timers. When there are no events to be serviced, |
| 118 | // this pump goes into a wait state. In most cases, this message pump handles |
| 119 | // all processing. |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 120 | // |
| 121 | // However, when a task, or windows event, invokes on the stack a native dialog |
| 122 | // box or such, that window typically provides a bare bones (native?) message |
| 123 | // pump. That bare-bones message pump generally supports little more than a |
| 124 | // peek of the Windows message queue, followed by a dispatch of the peeked |
| 125 | // message. MessageLoop extends that bare-bones message pump to also service |
| 126 | // Tasks, at the cost of some complexity. |
| 127 | // |
| 128 | // The basic structure of the extension (refered to as a sub-pump) is that a |
| 129 | // special message, kMsgHaveWork, is repeatedly injected into the Windows |
| 130 | // Message queue. Each time the kMsgHaveWork message is peeked, checks are |
| 131 | // made for an extended set of events, including the availability of Tasks to |
| 132 | // run. |
| 133 | // |
| 134 | // After running a task, the special message kMsgHaveWork is again posted to |
| 135 | // the Windows Message queue, ensuring a future time slice for processing a |
| 136 | // future event. To prevent flooding the Windows Message queue, care is taken |
| 137 | // to be sure that at most one kMsgHaveWork message is EVER pending in the |
| 138 | // Window's Message queue. |
| 139 | // |
| 140 | // There are a few additional complexities in this system where, when there are |
| 141 | // no Tasks to run, this otherwise infinite stream of messages which drives the |
| 142 | // sub-pump is halted. The pump is automatically re-started when Tasks are |
| 143 | // queued. |
| 144 | // |
| 145 | // A second complexity is that the presence of this stream of posted tasks may |
| 146 | // prevent a bare-bones message pump from ever peeking a WM_PAINT or WM_TIMER. |
| 147 | // Such paint and timer events always give priority to a posted message, such as |
| 148 | // kMsgHaveWork messages. As a result, care is taken to do some peeking in |
| 149 | // between the posting of each kMsgHaveWork message (i.e., after kMsgHaveWork |
| 150 | // is peeked, and before a replacement kMsgHaveWork is posted). |
| 151 | // |
| 152 | // NOTE: Although it may seem odd that messages are used to start and stop this |
| 153 | // flow (as opposed to signaling objects, etc.), it should be understood that |
| 154 | // the native message pump will *only* respond to messages. As a result, it is |
| 155 | // an excellent choice. It is also helpful that the starter messages that are |
| 156 | // placed in the queue when new task arrive also awakens DoRunLoop. |
| 157 | // |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 158 | class MessagePumpForUI : public MessagePumpWin { |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 159 | public: |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 160 | MessagePumpForUI(); |
| 161 | virtual ~MessagePumpForUI(); |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 162 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 163 | // MessagePump methods: |
| 164 | virtual void ScheduleWork(); |
| 165 | virtual void ScheduleDelayedWork(const Time& delayed_work_time); |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 166 | |
| 167 | // Applications can call this to encourage us to process all pending WM_PAINT |
| 168 | // messages. This method will process all paint messages the Windows Message |
| 169 | // queue can provide, up to some fixed number (to avoid any infinite loops). |
| 170 | void PumpOutPendingPaintMessages(); |
| 171 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 172 | private: |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 173 | static LRESULT CALLBACK WndProcThunk( |
| 174 | HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 175 | virtual void DoRunLoop(); |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 176 | void InitMessageWnd(); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 177 | void WaitForWork(); |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 178 | void HandleWorkMessage(); |
| 179 | void HandleTimerMessage(); |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 180 | bool ProcessNextWindowsMessage(); |
| 181 | bool ProcessMessageHelper(const MSG& msg); |
| 182 | bool ProcessPumpReplacementMessage(); |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 183 | |
| 184 | // A hidden message-only window. |
| 185 | HWND message_hwnd_; |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | //----------------------------------------------------------------------------- |
| 189 | // MessagePumpForIO extends MessagePumpWin with methods that are particular to a |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 190 | // MessageLoop instantiated with TYPE_IO. This version of MessagePump does not |
| 191 | // deal with Windows mesagges, and instead has a Run loop based on Completion |
| 192 | // Ports so it is better suited for IO operations. |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 193 | // |
| 194 | class MessagePumpForIO : public MessagePumpWin { |
| 195 | public: |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 196 | struct IOContext; |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 197 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 198 | // Clients interested in receiving OS notifications when asynchronous IO |
| 199 | // operations complete should implement this interface and register themselves |
| 200 | // with the message pump. |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 201 | // |
| 202 | // Typical use #1: |
| 203 | // // Use only when there are no user's buffers involved on the actual IO, |
| 204 | // // so that all the cleanup can be done by the message pump. |
| 205 | // class MyFile : public IOHandler { |
| 206 | // MyFile() { |
| 207 | // ... |
| 208 | // context_ = new IOContext; |
| 209 | // context_->handler = this; |
| 210 | // message_pump->RegisterIOHandler(file_, this); |
| 211 | // } |
| 212 | // ~MyFile() { |
| 213 | // if (pending_) { |
| 214 | // // By setting the handler to NULL, we're asking for this context |
| 215 | // // to be deleted when received, without calling back to us. |
| 216 | // context_->handler = NULL; |
| 217 | // } else { |
| 218 | // delete context_; |
| 219 | // } |
| 220 | // } |
| 221 | // virtual void OnIOCompleted(IOContext* context, DWORD bytes_transfered, |
| 222 | // DWORD error) { |
| 223 | // pending_ = false; |
| 224 | // } |
| 225 | // void DoSomeIo() { |
| 226 | // ... |
| 227 | // // The only buffer required for this operation is the overlapped |
| 228 | // // structure. |
| 229 | // ConnectNamedPipe(file_, &context_->overlapped); |
| 230 | // pending_ = true; |
| 231 | // } |
| 232 | // bool pending_; |
| 233 | // IOContext* context_; |
| 234 | // HANDLE file_; |
| 235 | // }; |
| 236 | // |
| 237 | // Typical use #2: |
| 238 | // class MyFile : public IOHandler { |
| 239 | // MyFile() { |
| 240 | // ... |
| 241 | // message_pump->RegisterIOHandler(file_, this); |
| 242 | // } |
| 243 | // // Plus some code to make sure that this destructor is not called |
| 244 | // // while there are pending IO operations. |
| 245 | // ~MyFile() { |
| 246 | // } |
| 247 | // virtual void OnIOCompleted(IOContext* context, DWORD bytes_transfered, |
| 248 | // DWORD error) { |
| 249 | // ... |
| 250 | // delete context; |
| 251 | // } |
| 252 | // void DoSomeIo() { |
| 253 | // ... |
| 254 | // IOContext* context = new IOContext; |
| 255 | // // This is not used for anything. It just prevents the context from |
| 256 | // // being considered "abandoned". |
| 257 | // context->handler = this; |
| 258 | // ReadFile(file_, buffer, num_bytes, &read, &context->overlapped); |
| 259 | // } |
| 260 | // HANDLE file_; |
| 261 | // }; |
| 262 | // |
| 263 | // Typical use #3: |
| 264 | // Same as the previous example, except that in order to deal with the |
| 265 | // requirement stated for the destructor, the class calls WaitForIOCompletion |
| 266 | // from the destructor to block until all IO finishes. |
| 267 | // ~MyFile() { |
| 268 | // while(pending_) |
| 269 | // message_pump->WaitForIOCompletion(INFINITE, this); |
| 270 | // } |
| 271 | // |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 272 | class IOHandler { |
| 273 | public: |
| 274 | virtual ~IOHandler() {} |
| 275 | // This will be called once the pending IO operation associated with |
| 276 | // |context| completes. |error| is the Win32 error code of the IO operation |
| 277 | // (ERROR_SUCCESS if there was no error). |bytes_transfered| will be zero |
| 278 | // on error. |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 279 | virtual void OnIOCompleted(IOContext* context, DWORD bytes_transfered, |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 280 | DWORD error) = 0; |
| 281 | }; |
| 282 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 283 | // The extended context that should be used as the base structure on every |
| 284 | // overlapped IO operation. |handler| must be set to the registered IOHandler |
| 285 | // for the given file when the operation is started, and it can be set to NULL |
| 286 | // before the operation completes to indicate that the handler should not be |
| 287 | // called anymore, and instead, the IOContext should be deleted when the OS |
| 288 | // notifies the completion of this operation. Please remember that any buffers |
| 289 | // involved with an IO operation should be around until the callback is |
| 290 | // received, so this technique can only be used for IO that do not involve |
| 291 | // additional buffers (other than the overlapped structure itself). |
| 292 | struct IOContext { |
| 293 | OVERLAPPED overlapped; |
| 294 | IOHandler* handler; |
| 295 | }; |
| 296 | |
| 297 | MessagePumpForIO(); |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 298 | virtual ~MessagePumpForIO() {} |
| 299 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 300 | // MessagePump methods: |
| 301 | virtual void ScheduleWork(); |
| 302 | virtual void ScheduleDelayedWork(const Time& delayed_work_time); |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 303 | |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 304 | // Register the handler to be used when asynchronous IO for the given file |
| 305 | // completes. The registration persists as long as |file_handle| is valid, so |
| 306 | // |handler| must be valid as long as there is pending IO for the given file. |
| 307 | void RegisterIOHandler(HANDLE file_handle, IOHandler* handler); |
| 308 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 309 | // Waits for the next IO completion that should be processed by |filter|, for |
| 310 | // up to |timeout| milliseconds. Return true if any IO operation completed, |
| 311 | // regardless of the involved handler, and false if the timeout expired. If |
| 312 | // the completion port received any message and the involved IO handler |
| 313 | // matches |filter|, the callback is called before returning from this code; |
| 314 | // if the handler is not the one that we are looking for, the callback will |
| 315 | // be postponed for another time, so reentrancy problems can be avoided. |
| 316 | // External use of this method should be reserved for the rare case when the |
| 317 | // caller is willing to allow pausing regular task dispatching on this thread. |
| 318 | bool WaitForIOCompletion(DWORD timeout, IOHandler* filter); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 319 | |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 320 | private: |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 321 | struct IOItem { |
| 322 | IOHandler* handler; |
| 323 | IOContext* context; |
| 324 | DWORD bytes_transfered; |
| 325 | DWORD error; |
| 326 | }; |
| 327 | |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 328 | virtual void DoRunLoop(); |
| 329 | void WaitForWork(); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 330 | bool MatchCompletedIOItem(IOHandler* filter, IOItem* item); |
| 331 | bool GetIOItem(DWORD timeout, IOItem* item); |
| 332 | bool ProcessInternalIOItem(const IOItem& item); |
[email protected] | 32cda29d | 2008-10-09 23:58:43 | [diff] [blame] | 333 | |
| 334 | // The completion port associated with this thread. |
| 335 | ScopedHandle port_; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame^] | 336 | // This list will be empty almost always. It stores IO completions that have |
| 337 | // not been delivered yet because somebody was doing cleanup. |
| 338 | std::list<IOItem> completed_io_; |
[email protected] | 1a8f5d1d | 2008-09-25 20:33:04 | [diff] [blame] | 339 | }; |
| 340 | |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 341 | } // namespace base |
| 342 | |
| 343 | #endif // BASE_MESSAGE_PUMP_WIN_H_ |