| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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_PUMP_WIN_H_ | 5 #ifndef BASE_MESSAGE_PUMP_WIN_H_ |
| 6 #define BASE_MESSAGE_PUMP_WIN_H_ | 6 #define BASE_MESSAGE_PUMP_WIN_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include <windows.h> | 10 #include <windows.h> |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 // is peeked, and before a replacement kMsgHaveWork is posted). | 55 // is peeked, and before a replacement kMsgHaveWork is posted). |
| 56 // | 56 // |
| 57 // NOTE: Although it may seem odd that messages are used to start and stop this | 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 | 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 | 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 | 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. | 61 // placed in the queue when new task arrive also awakens DoRunLoop. |
| 62 // | 62 // |
| 63 class MessagePumpWin : public MessagePump { | 63 class MessagePumpWin : public MessagePump { |
| 64 public: | 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 | 65 // An Observer is an object that receives global notifications from the |
| 75 // MessageLoop. | 66 // MessageLoop. |
| 76 // | 67 // |
| 77 // NOTE: An Observer implementation should be extremely fast! | 68 // NOTE: An Observer implementation should be extremely fast! |
| 78 // | 69 // |
| 79 class Observer { | 70 class Observer { |
| 80 public: | 71 public: |
| 81 virtual ~Observer() {} | 72 virtual ~Observer() {} |
| 82 | 73 |
| 83 // This method is called before processing a message. | 74 // This method is called before processing a message. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 99 // from Dispatch. | 90 // from Dispatch. |
| 100 class Dispatcher { | 91 class Dispatcher { |
| 101 public: | 92 public: |
| 102 virtual ~Dispatcher() {} | 93 virtual ~Dispatcher() {} |
| 103 // Dispatches the event. If true is returned processing continues as | 94 // Dispatches the event. If true is returned processing continues as |
| 104 // normal. If false is returned, the nested loop exits immediately. | 95 // normal. If false is returned, the nested loop exits immediately. |
| 105 virtual bool Dispatch(const MSG& msg) = 0; | 96 virtual bool Dispatch(const MSG& msg) = 0; |
| 106 }; | 97 }; |
| 107 | 98 |
| 108 MessagePumpWin(); | 99 MessagePumpWin(); |
| 109 ~MessagePumpWin(); | 100 virtual ~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 | 101 |
| 115 // Add an Observer, which will start receiving notifications immediately. | 102 // Add an Observer, which will start receiving notifications immediately. |
| 116 void AddObserver(Observer* observer); | 103 void AddObserver(Observer* observer); |
| 117 | 104 |
| 118 // Remove an Observer. It is safe to call this method while an Observer is | 105 // Remove an Observer. It is safe to call this method while an Observer is |
| 119 // receiving a notification callback. | 106 // receiving a notification callback. |
| 120 void RemoveObserver(Observer* observer); | 107 void RemoveObserver(Observer* observer); |
| 121 | 108 |
| 122 // Give a chance to code processing additional messages to notify the | 109 // Give a chance to code processing additional messages to notify the |
| 123 // message loop observers that another message has been processed. | 110 // message loop observers that another message has been processed. |
| 124 void WillProcessMessage(const MSG& msg); | 111 void WillProcessMessage(const MSG& msg); |
| 125 void DidProcessMessage(const MSG& msg); | 112 void DidProcessMessage(const MSG& msg); |
| 126 | 113 |
| 127 // Applications can call this to encourage us to process all pending WM_PAINT | 114 // 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 | 115 // 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). | 116 // queue can provide, up to some fixed number (to avoid any infinite loops). |
| 130 void PumpOutPendingPaintMessages(); | 117 void PumpOutPendingPaintMessages(); |
| 131 | 118 |
| 132 // Like MessagePump::Run, but MSG objects are routed through dispatcher. | 119 // Like MessagePump::Run, but MSG objects are routed through dispatcher. |
| 133 void RunWithDispatcher(Delegate* delegate, Dispatcher* dispatcher); | 120 void RunWithDispatcher(Delegate* delegate, Dispatcher* dispatcher); |
| 134 | 121 |
| 135 // MessagePump methods: | 122 // MessagePump methods: |
| 136 virtual void Run(Delegate* delegate) { RunWithDispatcher(delegate, NULL); } | 123 virtual void Run(Delegate* delegate) { RunWithDispatcher(delegate, NULL); } |
| 137 virtual void Quit(); | 124 virtual void Quit(); |
| 138 virtual void ScheduleWork(); | 125 virtual void ScheduleWork(); |
| 139 virtual void ScheduleDelayedWork(const Time& delayed_work_time); | 126 virtual void ScheduleDelayedWork(const Time& delayed_work_time); |
| 140 | 127 |
| 141 private: | 128 protected: |
| 142 struct RunState { | 129 struct RunState { |
| 143 Delegate* delegate; | 130 Delegate* delegate; |
| 144 Dispatcher* dispatcher; | 131 Dispatcher* dispatcher; |
| 145 | 132 |
| 146 // Used to flag that the current Run() invocation should return ASAP. | 133 // Used to flag that the current Run() invocation should return ASAP. |
| 147 bool should_quit; | 134 bool should_quit; |
| 148 | 135 |
| 149 // Used to count how many Run() invocations are on the stack. | 136 // Used to count how many Run() invocations are on the stack. |
| 150 int run_depth; | 137 int run_depth; |
| 151 }; | 138 }; |
| 152 | 139 |
| 153 static LRESULT CALLBACK WndProcThunk( | 140 static LRESULT CALLBACK WndProcThunk( |
| 154 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); | 141 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); |
| 142 virtual void DoRunLoop() = 0; |
| 155 void InitMessageWnd(); | 143 void InitMessageWnd(); |
| 156 void HandleWorkMessage(); | 144 void HandleWorkMessage(); |
| 157 void HandleTimerMessage(); | 145 void HandleTimerMessage(); |
| 158 void DoRunLoop(); | |
| 159 void WaitForWork(); | |
| 160 bool ProcessNextWindowsMessage(); | 146 bool ProcessNextWindowsMessage(); |
| 161 bool ProcessMessageHelper(const MSG& msg); | 147 bool ProcessMessageHelper(const MSG& msg); |
| 162 bool ProcessPumpReplacementMessage(); | 148 bool ProcessPumpReplacementMessage(); |
| 163 bool ProcessNextObject(); | |
| 164 bool SignalWatcher(size_t object_index); | |
| 165 int GetCurrentDelay() const; | 149 int GetCurrentDelay() const; |
| 166 | 150 |
| 167 // A hidden message-only window. | 151 // A hidden message-only window. |
| 168 HWND message_hwnd_; | 152 HWND message_hwnd_; |
| 169 | 153 |
| 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_; | 154 ObserverList<Observer> observers_; |
| 176 | 155 |
| 177 // The time at which delayed work should run. | 156 // The time at which delayed work should run. |
| 178 Time delayed_work_time_; | 157 Time delayed_work_time_; |
| 179 | 158 |
| 180 // A boolean value used to indicate if there is a kMsgDoWork message pending | 159 // 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 | 160 // 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. | 161 // can drive execution of tasks when a native message pump is running. |
| 183 LONG have_work_; | 162 LONG have_work_; |
| 184 | 163 |
| 185 // State for the current invocation of Run. | 164 // State for the current invocation of Run. |
| 186 RunState* state_; | 165 RunState* state_; |
| 187 }; | 166 }; |
| 188 | 167 |
| 168 //----------------------------------------------------------------------------- |
| 169 // MessagePumpForUI extends MessagePumpWin with methods that are particular to a |
| 170 // MessageLoop instantiated with TYPE_UI. |
| 171 // |
| 172 class MessagePumpForUI : public MessagePumpWin { |
| 173 public: |
| 174 MessagePumpForUI() {} |
| 175 virtual ~MessagePumpForUI() {} |
| 176 private: |
| 177 virtual void DoRunLoop(); |
| 178 void WaitForWork(); |
| 179 }; |
| 180 |
| 181 //----------------------------------------------------------------------------- |
| 182 // MessagePumpForIO extends MessagePumpWin with methods that are particular to a |
| 183 // MessageLoop instantiated with TYPE_IO. |
| 184 // |
| 185 class MessagePumpForIO : public MessagePumpWin { |
| 186 public: |
| 187 // Used with WatchObject to asynchronously monitor the signaled state of a |
| 188 // HANDLE object. |
| 189 class Watcher { |
| 190 public: |
| 191 virtual ~Watcher() {} |
| 192 // Called from MessageLoop::Run when a signalled object is detected. |
| 193 virtual void OnObjectSignaled(HANDLE object) = 0; |
| 194 }; |
| 195 |
| 196 MessagePumpForIO() {} |
| 197 virtual ~MessagePumpForIO() {} |
| 198 |
| 199 // Have the current thread's message loop watch for a signaled object. |
| 200 // Pass a null watcher to stop watching the object. |
| 201 void WatchObject(HANDLE, Watcher*); |
| 202 |
| 203 private: |
| 204 virtual void DoRunLoop(); |
| 205 void WaitForWork(); |
| 206 bool ProcessNextObject(); |
| 207 bool SignalWatcher(size_t object_index); |
| 208 |
| 209 // A vector of objects (and corresponding watchers) that are routinely |
| 210 // serviced by this message pump. |
| 211 std::vector<HANDLE> objects_; |
| 212 std::vector<Watcher*> watchers_; |
| 213 }; |
| 214 |
| 189 } // namespace base | 215 } // namespace base |
| 190 | 216 |
| 191 #endif // BASE_MESSAGE_PUMP_WIN_H_ | 217 #endif // BASE_MESSAGE_PUMP_WIN_H_ |
| 192 | 218 |
| OLD | NEW |