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_H_ | 5 #ifndef BASE_MESSAGE_PUMP_H_ |
6 #define BASE_MESSAGE_PUMP_H_ | 6 #define BASE_MESSAGE_PUMP_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 class TimeTicks; | 13 class TimeTicks; |
14 | 14 |
15 class MessagePump : public RefCountedThreadSafe<MessagePump> { | 15 class MessagePump : public RefCountedThreadSafe<MessagePump> { |
16 public: | 16 public: |
17 // Please see the comments above the Run method for an illustration of how | 17 // Please see the comments above the Run method for an illustration of how |
18 // these delegate methods are used. | 18 // these delegate methods are used. |
19 class Delegate { | 19 class Delegate { |
20 public: | 20 public: |
21 virtual ~Delegate() {} | 21 virtual ~Delegate() {} |
22 | 22 |
23 // Called from within Run in response to ScheduleWork or when the message | 23 // Called from within Run in response to ScheduleWork or when the message |
24 // pump would otherwise call DoDelayedWork. Returns true to indicate that | 24 // pump would otherwise call DoDelayedWork. Returns true to indicate that |
25 // work was done. DoDelayedWork will not be called if DoWork returns true. | 25 // work was done. DoDelayedWork will still be called if DoWork returns |
| 26 // true, but DoIdleWork will not. |
26 virtual bool DoWork() = 0; | 27 virtual bool DoWork() = 0; |
27 | 28 |
28 // Called from within Run in response to ScheduleDelayedWork or when the | 29 // Called from within Run in response to ScheduleDelayedWork or when the |
29 // message pump would otherwise sleep waiting for more work. Returns true | 30 // message pump would otherwise sleep waiting for more work. Returns true |
30 // to indicate that delayed work was done. DoIdleWork will not be called | 31 // to indicate that delayed work was done. DoIdleWork will not be called |
31 // if DoDelayedWork returns true. Upon return |next_delayed_work_time| | 32 // if DoDelayedWork returns true. Upon return |next_delayed_work_time| |
32 // indicates the time when DoDelayedWork should be called again. If | 33 // indicates the time when DoDelayedWork should be called again. If |
33 // |next_delayed_work_time| is null (per Time::is_null), then the queue of | 34 // |next_delayed_work_time| is null (per Time::is_null), then the queue of |
34 // future delayed work (timer events) is currently empty, and no additional | 35 // future delayed work (timer events) is currently empty, and no additional |
35 // calls to this function need to be scheduled. | 36 // calls to this function need to be scheduled. |
(...skipping 18 matching lines...) Expand all Loading... |
54 // | 55 // |
55 // for (;;) { | 56 // for (;;) { |
56 // bool did_work = DoInternalWork(); | 57 // bool did_work = DoInternalWork(); |
57 // if (should_quit_) | 58 // if (should_quit_) |
58 // break; | 59 // break; |
59 // | 60 // |
60 // did_work |= delegate_->DoWork(); | 61 // did_work |= delegate_->DoWork(); |
61 // if (should_quit_) | 62 // if (should_quit_) |
62 // break; | 63 // break; |
63 // | 64 // |
64 // did_work |= delegate_->DoDelayedWork(); | 65 // TimeTicks next_time; |
| 66 // did_work |= delegate_->DoDelayedWork(&next_time); |
65 // if (should_quit_) | 67 // if (should_quit_) |
66 // break; | 68 // break; |
67 // | 69 // |
68 // if (did_work) | 70 // if (did_work) |
69 // continue; | 71 // continue; |
70 // | 72 // |
71 // did_work = delegate_->DoIdleWork(); | 73 // did_work = delegate_->DoIdleWork(); |
72 // if (should_quit_) | 74 // if (should_quit_) |
73 // break; | 75 // break; |
74 // | 76 // |
75 // if (did_work) | 77 // if (did_work) |
76 // continue; | 78 // continue; |
77 // | 79 // |
78 // WaitForWork(); | 80 // WaitForWork(); |
79 // } | 81 // } |
80 // | 82 // |
81 // Here, DoInternalWork is some private method of the message pump that is | 83 // Here, DoInternalWork is some private method of the message pump that is |
82 // responsible for dispatching the next UI message or notifying the next IO | 84 // responsible for dispatching the next UI message or notifying the next IO |
83 // completion (for example). WaitForWork is a private method that simply | 85 // completion (for example). WaitForWork is a private method that simply |
84 // blocks until there is more work of any type to do. | 86 // blocks until there is more work of any type to do. |
85 // | 87 // |
86 // Notice that the run loop cycles between calling DoInternalWork, DoWork, | 88 // Notice that the run loop cycles between calling DoInternalWork, DoWork, |
87 // and DoDelayedWork methods. This helps ensure that neither work queue | 89 // and DoDelayedWork methods. This helps ensure that none of these work |
88 // starves the other. This is important for message pumps that are used to | 90 // queues starve the others. This is important for message pumps that are |
89 // drive animations, for example. | 91 // used to drive animations, for example. |
90 // | 92 // |
91 // Notice also that after each callout to foreign code, the run loop checks | 93 // Notice also that after each callout to foreign code, the run loop checks |
92 // to see if it should quit. The Quit method is responsible for setting this | 94 // to see if it should quit. The Quit method is responsible for setting this |
93 // flag. No further work is done once the quit flag is set. | 95 // flag. No further work is done once the quit flag is set. |
94 // | 96 // |
95 // NOTE: Care must be taken to handle Run being called again from within any | 97 // NOTE: Care must be taken to handle Run being called again from within any |
96 // of the callouts to foreign code. Native message pumps may also need to | 98 // of the callouts to foreign code. Native message pumps may also need to |
97 // deal with other native message pumps being run outside their control | 99 // deal with other native message pumps being run outside their control |
98 // (e.g., the MessageBox API on Windows pumps UI messages!). To be specific, | 100 // (e.g., the MessageBox API on Windows pumps UI messages!). To be specific, |
99 // the callouts (DoWork and DoDelayedWork) MUST still be provided even in | 101 // the callouts (DoWork and DoDelayedWork) MUST still be provided even in |
(...skipping 15 matching lines...) Expand all Loading... |
115 | 117 |
116 // Schedule a DoDelayedWork callback to happen at the specified time, | 118 // Schedule a DoDelayedWork callback to happen at the specified time, |
117 // cancelling any pending DoDelayedWork callback. This method may only be | 119 // cancelling any pending DoDelayedWork callback. This method may only be |
118 // used on the thread that called Run. | 120 // used on the thread that called Run. |
119 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) = 0; | 121 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) = 0; |
120 }; | 122 }; |
121 | 123 |
122 } // namespace base | 124 } // namespace base |
123 | 125 |
124 #endif // BASE_MESSAGE_PUMP_H_ | 126 #endif // BASE_MESSAGE_PUMP_H_ |
OLD | NEW |