| 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_H_ | 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_ |
| 6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_ | 6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_ |
| 7 | 7 |
| 8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/threading/non_thread_safe.h" |
| 10 | 10 |
| 11 namespace base { | 11 namespace base { |
| 12 | 12 |
| 13 class TimeTicks; | 13 class TimeTicks; |
| 14 | 14 |
| 15 class BASE_EXPORT MessagePump : public RefCountedThreadSafe<MessagePump> { | 15 class BASE_EXPORT MessagePump : public NonThreadSafe { |
| 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 BASE_EXPORT Delegate { | 19 class BASE_EXPORT 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 still be called if DoWork returns | 25 // work was done. DoDelayedWork will still be called if DoWork returns |
| 26 // true, but DoIdleWork will not. | 26 // true, but DoIdleWork will not. |
| 27 virtual bool DoWork() = 0; | 27 virtual bool DoWork() = 0; |
| 28 | 28 |
| 29 // Called from within Run in response to ScheduleDelayedWork or when the | 29 // Called from within Run in response to ScheduleDelayedWork or when the |
| 30 // message pump would otherwise sleep waiting for more work. Returns true | 30 // message pump would otherwise sleep waiting for more work. Returns true |
| 31 // 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 |
| 32 // if DoDelayedWork returns true. Upon return |next_delayed_work_time| | 32 // if DoDelayedWork returns true. Upon return |next_delayed_work_time| |
| 33 // indicates the time when DoDelayedWork should be called again. If | 33 // indicates the time when DoDelayedWork should be called again. If |
| 34 // |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 |
| 35 // future delayed work (timer events) is currently empty, and no additional | 35 // future delayed work (timer events) is currently empty, and no additional |
| 36 // calls to this function need to be scheduled. | 36 // calls to this function need to be scheduled. |
| 37 virtual bool DoDelayedWork(TimeTicks* next_delayed_work_time) = 0; | 37 virtual bool DoDelayedWork(TimeTicks* next_delayed_work_time) = 0; |
| 38 | 38 |
| 39 // Called from within Run just before the message pump goes to sleep. | 39 // Called from within Run just before the message pump goes to sleep. |
| 40 // Returns true to indicate that idle work was done. | 40 // Returns true to indicate that idle work was done. |
| 41 virtual bool DoIdleWork() = 0; | 41 virtual bool DoIdleWork() = 0; |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 MessagePump(); | 44 MessagePump(); |
| 45 virtual ~MessagePump(); |
| 45 | 46 |
| 46 // The Run method is called to enter the message pump's run loop. | 47 // The Run method is called to enter the message pump's run loop. |
| 47 // | 48 // |
| 48 // Within the method, the message pump is responsible for processing native | 49 // Within the method, the message pump is responsible for processing native |
| 49 // messages as well as for giving cycles to the delegate periodically. The | 50 // messages as well as for giving cycles to the delegate periodically. The |
| 50 // message pump should take care to mix delegate callbacks with native | 51 // message pump should take care to mix delegate callbacks with native |
| 51 // message processing so neither type of event starves the other of cycles. | 52 // message processing so neither type of event starves the other of cycles. |
| 52 // | 53 // |
| 53 // The anatomy of a typical run loop: | 54 // The anatomy of a typical run loop: |
| 54 // | 55 // |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 // Schedule a DoWork callback to happen reasonably soon. Does nothing if a | 112 // Schedule a DoWork callback to happen reasonably soon. Does nothing if a |
| 112 // DoWork callback is already scheduled. This method may be called from any | 113 // DoWork callback is already scheduled. This method may be called from any |
| 113 // thread. Once this call is made, DoWork should not be "starved" at least | 114 // thread. Once this call is made, DoWork should not be "starved" at least |
| 114 // until it returns a value of false. | 115 // until it returns a value of false. |
| 115 virtual void ScheduleWork() = 0; | 116 virtual void ScheduleWork() = 0; |
| 116 | 117 |
| 117 // Schedule a DoDelayedWork callback to happen at the specified time, | 118 // Schedule a DoDelayedWork callback to happen at the specified time, |
| 118 // cancelling any pending DoDelayedWork callback. This method may only be | 119 // cancelling any pending DoDelayedWork callback. This method may only be |
| 119 // used on the thread that called Run. | 120 // used on the thread that called Run. |
| 120 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) = 0; | 121 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) = 0; |
| 121 | |
| 122 protected: | |
| 123 virtual ~MessagePump(); | |
| 124 friend class RefCountedThreadSafe<MessagePump>; | |
| 125 }; | 122 }; |
| 126 | 123 |
| 127 } // namespace base | 124 } // namespace base |
| 128 | 125 |
| 129 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_ | 126 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_ |
| OLD | NEW |