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/message_loop/message_pump.h" | 13 #include "base/message_loop/message_pump.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "base/win/scoped_handle.h" | 16 #include "base/win/scoped_handle.h" |
17 | 17 |
18 namespace base { | 18 namespace base { |
19 | 19 |
20 // MessagePumpWin serves as the base for specialized versions of the MessagePump | 20 // MessagePumpWin serves as the base for specialized versions of the MessagePump |
21 // for Windows. It provides basic functionality like handling of observers and | 21 // for Windows. It provides basic functionality like handling of observers and |
22 // controlling the lifetime of the message pump. | 22 // controlling the lifetime of the message pump. |
23 class BASE_EXPORT MessagePumpWin : public MessagePump { | 23 class BASE_EXPORT MessagePumpWin : public MessagePump { |
24 public: | 24 public: |
25 MessagePumpWin() : have_work_(0), state_(NULL) {} | 25 MessagePumpWin() : work_state_(READY), state_(NULL) {} |
26 | 26 |
27 // MessagePump methods: | 27 // MessagePump methods: |
28 void Run(Delegate* delegate) override; | 28 void Run(Delegate* delegate) override; |
29 void Quit() override; | 29 void Quit() override; |
30 | 30 |
31 protected: | 31 protected: |
32 struct RunState { | 32 struct RunState { |
33 Delegate* delegate; | 33 Delegate* delegate; |
34 | 34 |
35 // Used to flag that the current Run() invocation should return ASAP. | 35 // Used to flag that the current Run() invocation should return ASAP. |
36 bool should_quit; | 36 bool should_quit; |
37 | 37 |
38 // Used to count how many Run() invocations are on the stack. | 38 // Used to count how many Run() invocations are on the stack. |
39 int run_depth; | 39 int run_depth; |
40 }; | 40 }; |
41 | 41 |
| 42 // State used with work_state_ variable. |
| 43 enum WorkState { |
| 44 READY = 0, // Ready to accept new work. |
| 45 HAVE_WORK = 1, // New work has been signalled. |
| 46 WORKING = 2 // Handling the work. |
| 47 }; |
| 48 |
42 virtual void DoRunLoop() = 0; | 49 virtual void DoRunLoop() = 0; |
43 int GetCurrentDelay() const; | 50 int GetCurrentDelay() const; |
44 | 51 |
45 // The time at which delayed work should run. | 52 // The time at which delayed work should run. |
46 TimeTicks delayed_work_time_; | 53 TimeTicks delayed_work_time_; |
47 | 54 |
48 // A boolean value used to indicate if there is a kMsgDoWork message pending | 55 // A value used to indicate if there is a kMsgDoWork message pending |
49 // in the Windows Message queue. There is at most one such message, and it | 56 // in the Windows Message queue. There is at most one such message, and it |
50 // can drive execution of tasks when a native message pump is running. | 57 // can drive execution of tasks when a native message pump is running. |
51 LONG have_work_; | 58 LONG work_state_; |
52 | 59 |
53 // State for the current invocation of Run. | 60 // State for the current invocation of Run. |
54 RunState* state_; | 61 RunState* state_; |
55 }; | 62 }; |
56 | 63 |
57 //----------------------------------------------------------------------------- | 64 //----------------------------------------------------------------------------- |
| 65 // MessagePumpForUIBase serves as a base class for for specialized versions of |
| 66 // MessagePump instantiated with TYPE_UI. It provides basic functionality for |
| 67 // creating a hidden window with a message queue and handling window messages, |
| 68 // including the special kMsgHaveWork message that indicates the scheduled work, |
| 69 // handling timers, etc. |
| 70 // |
| 71 class BASE_EXPORT MessagePumpForUIBase : public MessagePumpWin { |
| 72 public: |
| 73 // The application-defined code passed to the hook procedure. |
| 74 static const int kMessageFilterCode = 0x5001; |
| 75 |
| 76 MessagePumpForUIBase(); |
| 77 ~MessagePumpForUIBase() override; |
| 78 |
| 79 // MessagePump methods: |
| 80 void ScheduleWork() override; |
| 81 |
| 82 protected: |
| 83 virtual void HandleWorkMessage() = 0; |
| 84 virtual void HandleTimerMessage() = 0; |
| 85 |
| 86 bool IsWorkMessage(const MSG& msg) const; |
| 87 void SetTimer(int delay_msec); |
| 88 void KillTimer(); |
| 89 |
| 90 private: |
| 91 static LRESULT CALLBACK WndProcThunk(HWND window_handle, |
| 92 UINT message, |
| 93 WPARAM wparam, |
| 94 LPARAM lparam); |
| 95 void InitMessageWnd(); |
| 96 |
| 97 // Atom representing the registered window class. |
| 98 ATOM atom_; |
| 99 |
| 100 // A hidden message-only window. |
| 101 HWND message_hwnd_; |
| 102 }; |
| 103 |
| 104 //----------------------------------------------------------------------------- |
58 // MessagePumpForUI extends MessagePumpWin with methods that are particular to a | 105 // MessagePumpForUI extends MessagePumpWin with methods that are particular to a |
59 // MessageLoop instantiated with TYPE_UI. | 106 // MessageLoop instantiated with TYPE_UI. |
60 // | 107 // |
61 // MessagePumpForUI implements a "traditional" Windows message pump. It contains | 108 // MessagePumpForUI implements a "traditional" Windows message pump. It contains |
62 // a nearly infinite loop that peeks out messages, and then dispatches them. | 109 // a nearly infinite loop that peeks out messages, and then dispatches them. |
63 // Intermixed with those peeks are callouts to DoWork for pending tasks, and | 110 // Intermixed with those peeks are callouts to DoWork for pending tasks, and |
64 // DoDelayedWork for pending timers. When there are no events to be serviced, | 111 // DoDelayedWork for pending timers. When there are no events to be serviced, |
65 // this pump goes into a wait state. In most cases, this message pump handles | 112 // this pump goes into a wait state. In most cases, this message pump handles |
66 // all processing. | 113 // all processing. |
67 // | 114 // |
(...skipping 27 matching lines...) Expand all Loading... |
95 // kMsgHaveWork messages. As a result, care is taken to do some peeking in | 142 // kMsgHaveWork messages. As a result, care is taken to do some peeking in |
96 // between the posting of each kMsgHaveWork message (i.e., after kMsgHaveWork | 143 // between the posting of each kMsgHaveWork message (i.e., after kMsgHaveWork |
97 // is peeked, and before a replacement kMsgHaveWork is posted). | 144 // is peeked, and before a replacement kMsgHaveWork is posted). |
98 // | 145 // |
99 // NOTE: Although it may seem odd that messages are used to start and stop this | 146 // NOTE: Although it may seem odd that messages are used to start and stop this |
100 // flow (as opposed to signaling objects, etc.), it should be understood that | 147 // flow (as opposed to signaling objects, etc.), it should be understood that |
101 // the native message pump will *only* respond to messages. As a result, it is | 148 // the native message pump will *only* respond to messages. As a result, it is |
102 // an excellent choice. It is also helpful that the starter messages that are | 149 // an excellent choice. It is also helpful that the starter messages that are |
103 // placed in the queue when new task arrive also awakens DoRunLoop. | 150 // placed in the queue when new task arrive also awakens DoRunLoop. |
104 // | 151 // |
105 class BASE_EXPORT MessagePumpForUI : public MessagePumpWin { | 152 class BASE_EXPORT MessagePumpForUI : public MessagePumpForUIBase { |
106 public: | 153 public: |
107 // The application-defined code passed to the hook procedure. | |
108 static const int kMessageFilterCode = 0x5001; | |
109 | |
110 MessagePumpForUI(); | 154 MessagePumpForUI(); |
111 ~MessagePumpForUI() override; | 155 ~MessagePumpForUI() override; |
112 | 156 |
| 157 private: |
113 // MessagePump methods: | 158 // MessagePump methods: |
114 void ScheduleWork() override; | 159 void DoRunLoop() override; |
115 void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override; | 160 void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override; |
116 | 161 |
117 private: | 162 void HandleWorkMessage() override; |
118 static LRESULT CALLBACK WndProcThunk(HWND window_handle, | 163 void HandleTimerMessage() override; |
119 UINT message, | 164 |
120 WPARAM wparam, | 165 bool ProcessNextWindowsMessage(); |
121 LPARAM lparam); | |
122 void DoRunLoop() override; | |
123 void InitMessageWnd(); | |
124 void WaitForWork(); | 166 void WaitForWork(); |
125 void HandleWorkMessage(); | 167 |
126 void HandleTimerMessage(); | |
127 void RescheduleTimer(); | |
128 bool ProcessNextWindowsMessage(); | |
129 bool ProcessMessageHelper(const MSG& msg); | 168 bool ProcessMessageHelper(const MSG& msg); |
130 bool ProcessPumpReplacementMessage(); | 169 bool ProcessPumpReplacementMessage(); |
131 | 170 void RescheduleTimer(); |
132 // Atom representing the registered window class. | |
133 ATOM atom_; | |
134 | |
135 // A hidden message-only window. | |
136 HWND message_hwnd_; | |
137 }; | 171 }; |
138 | 172 |
139 //----------------------------------------------------------------------------- | 173 //----------------------------------------------------------------------------- |
| 174 // MessagePumpForGpu is a simplified version of MessagePumpForUI that is |
| 175 // optimized for the GPU process. Unlike MessagePumpForUI it doesn't handle a |
| 176 // situation where a native message pump might be used by modal dialogs and |
| 177 // message boxes. That allows to skip some system calls like setting and killing |
| 178 // timers, getting status of the message queue, etc. |
| 179 |
| 180 class BASE_EXPORT MessagePumpForGpu : public MessagePumpForUIBase { |
| 181 public: |
| 182 MessagePumpForGpu(); |
| 183 ~MessagePumpForGpu() override; |
| 184 |
| 185 // Factory methods. |
| 186 static void InitFactory(); |
| 187 static scoped_ptr<MessagePump> CreateMessagePumpForGpu(); |
| 188 |
| 189 private: |
| 190 // MessagePump methods: |
| 191 void DoRunLoop() override; |
| 192 void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override; |
| 193 |
| 194 void HandleWorkMessage() override; |
| 195 void HandleTimerMessage() override; |
| 196 |
| 197 void ProcessWindowsMessages(); |
| 198 void WaitForWork(); |
| 199 }; |
| 200 |
| 201 //----------------------------------------------------------------------------- |
140 // MessagePumpForIO extends MessagePumpWin with methods that are particular to a | 202 // MessagePumpForIO extends MessagePumpWin with methods that are particular to a |
141 // MessageLoop instantiated with TYPE_IO. This version of MessagePump does not | 203 // MessageLoop instantiated with TYPE_IO. This version of MessagePump does not |
142 // deal with Windows mesagges, and instead has a Run loop based on Completion | 204 // deal with Windows mesagges, and instead has a Run loop based on Completion |
143 // Ports so it is better suited for IO operations. | 205 // Ports so it is better suited for IO operations. |
144 // | 206 // |
145 class BASE_EXPORT MessagePumpForIO : public MessagePumpWin { | 207 class BASE_EXPORT MessagePumpForIO : public MessagePumpWin { |
146 public: | 208 public: |
147 struct IOContext; | 209 struct IOContext; |
148 | 210 |
149 // Clients interested in receiving OS notifications when asynchronous IO | 211 // Clients interested in receiving OS notifications when asynchronous IO |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 // This list will be empty almost always. It stores IO completions that have | 388 // This list will be empty almost always. It stores IO completions that have |
327 // not been delivered yet because somebody was doing cleanup. | 389 // not been delivered yet because somebody was doing cleanup. |
328 std::list<IOItem> completed_io_; | 390 std::list<IOItem> completed_io_; |
329 | 391 |
330 ObserverList<IOObserver> io_observers_; | 392 ObserverList<IOObserver> io_observers_; |
331 }; | 393 }; |
332 | 394 |
333 } // namespace base | 395 } // namespace base |
334 | 396 |
335 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_WIN_H_ | 397 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_WIN_H_ |
OLD | NEW |