| 6 | #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_ |
| 7 |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/message_loop/message_pump_type.h" |
| 11 | #include "base/message_loop/timer_slack.h" |
| 12 | #include "base/sequence_checker.h" |
| 13 | #include "base/time/time.h" |
| 14 | #include "build/build_config.h" |
| 15 |
| 16 | namespace base { |
| 17 |
| 18 | class TimeTicks; |
| 19 |
| 20 | class BASE_EXPORT MessagePump { |
| 21 | public: |
| 22 | using MessagePumpFactory = std::unique_ptr<MessagePump>(); |
| 23 | // Uses the given base::MessagePumpFactory to override the default MessagePump |
| 24 | // implementation for 'MessagePumpType::UI'. May only be called once. |
| 25 | static void OverrideMessagePumpForUIFactory(MessagePumpFactory* factory); |
| 26 |
| 27 | // Returns true if the MessagePumpForUI has been overidden. |
| 28 | static bool IsMessagePumpForUIFactoryOveridden(); |
| 29 |
| 30 | // Creates the default MessagePump based on |type|. Caller owns return value. |
| 31 | static std::unique_ptr<MessagePump> Create(MessagePumpType type); |
| 32 |
| 33 | // Please see the comments above the Run method for an illustration of how |
| 34 | // these delegate methods are used. |
| 35 | class BASE_EXPORT Delegate { |
| 36 | public: |
| 37 | virtual ~Delegate() = default; |
| 38 |
| 39 | // Called before a unit of work internal to the message pump is executed. |
| 40 | // This allows reports about individual units of work to be produced. |
| 41 | // The unit of work ends when BeforeDoInternalWork() is called again, or |
| 42 | // when BeforeWait(), DoSomeWork(), or DoIdleWork() is called. |
| 43 | // TODO(crbug.com/851163): Place calls for all platforms. |
| 44 | virtual void BeforeDoInternalWork() = 0; |
| 45 |
| 46 | // Called before the message pump starts waiting for work. |
| 47 | // This indicates the end of the current unit of work, which is required |
| 48 | // to produce reports about individual units of work. |
| 49 | virtual void BeforeWait() = 0; |
| 50 |
| 51 | struct NextWorkInfo { |
| 52 | // Helper to extract a TimeDelta for pumps that need a |
| 53 | // timeout-till-next-task. |
| 54 | TimeDelta remaining_delay() const { |
| 55 | DCHECK(!delayed_run_time.is_null() && !delayed_run_time.is_max()); |
| 56 | DCHECK_GE(TimeTicks::Now(), recent_now); |
| 57 | return delayed_run_time - recent_now; |
| 58 | } |
| 59 |
| 60 | // Helper to verify if the next task is ready right away. |
| 61 | bool is_immediate() const { return delayed_run_time.is_null(); } |
| 62 |
| 63 | // The next PendingTask's |delayed_run_time|. is_null() if there's extra |
| 64 | // work to run immediately. is_max() if there are no more immediate nor |
| 65 | // delayed tasks. |
| 66 | TimeTicks delayed_run_time; |
| 67 |
| 68 | // A recent view of TimeTicks::Now(). Only valid if |next_task_run_time| |
| 69 | // isn't null nor max. MessagePump impls should use remaining_delay() |
| 70 | // instead of resampling Now() if they wish to sleep for a TimeDelta. |
| 71 | TimeTicks recent_now; |
| 72 | }; |
| 73 |
| 74 | // Executes an immediate task or a ripe delayed task. Returns information |
| 75 | // about when DoSomeWork() should be called again. If the returned |
| 76 | // NextWorkInfo is_immediate(), DoSomeWork() must be invoked again shortly. |
| 77 | // Else, DoSomeWork() must be invoked at |NextWorkInfo::delayed_run_time| or |
| 78 | // when ScheduleWork() is invoked, whichever comes first. Redundant/spurious |
| 79 | // invocations of DoSomeWork() outside of those requirements are tolerated. |
| 80 | // DoIdleWork() will not be called so long as this returns a NextWorkInfo |
| 81 | // which is_immediate(). |
| 82 | virtual NextWorkInfo DoSomeWork() = 0; |
| 83 |
| 84 | // Called from within Run just before the message pump goes to sleep. |
| 85 | // Returns true to indicate that idle work was done. Returning false means |
| 86 | // the pump will now wait. |
| 87 | virtual bool DoIdleWork() = 0; |
| 88 | }; |
| 89 |
| 90 | MessagePump(); |
| 91 | virtual ~MessagePump(); |
| 92 |
| 93 | // The Run method is called to enter the message pump's run loop. |
| 94 | // |
| 95 | // Within the method, the message pump is responsible for processing native |
| 96 | // messages as well as for giving cycles to the delegate periodically. The |
| 97 | // message pump should take care to mix delegate callbacks with native message |
| 98 | // processing so neither type of event starves the other of cycles. Each call |
| 99 | // to a delegate function or DoInternalWork() is considered the beginning of a |
| 100 | // new "unit of work". |
| 101 | // |
| 102 | // The anatomy of a typical run loop: |
| 103 | // |
| 104 | // for (;;) { |
| 105 | // bool did_internal_work = DoInternalWork(); |
| 106 | // if (should_quit_) |
| 107 | // break; |
| 108 | // |
| 109 | // Delegate::NextWorkInfo next_work_info = delegate->DoSomeWork(); |
| 110 | // if (should_quit_) |
| 111 | // break; |
| 112 | // |
| 113 | // if (did_internal_work || next_work_info.is_immediate()) |
| 114 | // continue; |
| 115 | // |
| 116 | // bool did_idle_work = delegate_->DoIdleWork(); |
| 117 | // if (should_quit_) |
| 118 | // break; |
| 119 | // |
| 120 | // if (did_idle_work) |
| 121 | // continue; |
| 122 | // |
| 123 | // WaitForWork(); |
| 124 | // } |
| 125 | // |
| 126 |
| 127 | // Here, DoInternalWork is some private method of the message pump that is |
| 128 | // responsible for dispatching the next UI message or notifying the next IO |
| 129 | // completion (for example). WaitForWork is a private method that simply |
| 130 | // blocks until there is more work of any type to do. |
| 131 | // |
| 132 | // Notice that the run loop cycles between calling DoInternalWork and |
| 133 | // DoSomeWork methods. This helps ensure that none of these work queues starve |
| 134 | // the others. This is important for message pumps that are used to drive |
| 135 | // animations, for example. |
| 136 | // |
| 137 | // Notice also that after each callout to foreign code, the run loop checks to |
| 138 | // see if it should quit. The Quit method is responsible for setting this |
| 139 | // flag. No further work is done once the quit flag is set. |
| 140 | // |
| 141 | // NOTE 1: Run may be called reentrantly from any of the callouts to foreign |
| 142 | // code (internal work, DoSomeWork, DoIdleWork). As a result, DoSomeWork and |
| 143 | // DoIdleWork must be reentrant. |
| 144 | // |
| 145 | // NOTE 2: Run implementations must arrange for DoSomeWork to be invoked as |
| 146 | // expected if a callout to foreign code enters a message pump outside their |
| 147 | // control. For example, the MessageBox API on Windows pumps UI messages. If |
| 148 | // the MessageBox API is called (indirectly) from within Run, it is expected |
| 149 | // that DoSomeWork will be invoked from within that call in response to |
| 150 | // ScheduleWork or as requested by the last NextWorkInfo returned by |
| 151 | // DoSomeWork. The MessagePump::Delegate may then elect to do nested work or |
| 152 | // not depending on its policy in that context. Regardless of that decision |
| 153 | // (and return value of the nested DoSomeWork() call), DoSomeWork() will be |
| 154 | // invoked again when the nested loop unwinds. |
| 155 | virtual void Run(Delegate* delegate) = 0; |
| 156 |
| 157 | // Quit immediately from the most recently entered run loop. This method may |
| 158 | // only be used on the thread that called Run. |
| 159 | virtual void Quit() = 0; |
| 160 |
| 161 | // Schedule a DoSomeWork callback to happen reasonably soon. Does nothing if |
| 162 | // a DoSomeWork callback is already scheduled. Once this call is made, |
| 163 | // DoSomeWork is guaranteed to be called repeatedly at least until it returns |
| 164 | // a non-immediate NextWorkInfo. This call can be expensive and callers should |
| 165 | // attempt not to invoke it again before a non-immediate NextWorkInfo was |
| 166 | // returned from DoSomeWork(). Thread-safe (and callers should avoid holding a |
| 167 | // Lock at all cost while making this call as some platforms' priority |
| 168 | // boosting features have been observed to cause the caller to get descheduled |
| 169 | // : https://crbug.com/890978). |
| 170 | virtual void ScheduleWork() = 0; |
| 171 |
| 172 | // Schedule a DoSomeWork callback to happen at the specified time, cancelling |
| 173 | // any pending callback scheduled by this method. This method may only be used |
| 174 | // on the thread that called Run. |
| 175 | // |
| 176 | // It isn't necessary to call this during normal execution, as the pump wakes |
| 177 | // up as requested by the return value of DoSomeWork(). |
| 178 | // TODO(crbug.com/885371): Determine if this must be called to ensure that |
| 179 | // delayed tasks run when a message pump outside the control of Run is |
| 180 | // entered. |
| 181 | virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) = 0; |
| 182 |
| 183 | // Sets the timer slack to the specified value. |
| 184 | virtual void SetTimerSlack(TimerSlack timer_slack); |
| 185 | }; |
| 186 |
| 187 | } // namespace base |
| 188 |
| 189 | #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_H_ |
| 190 |