| 6 | #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| 7 |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 |
| 11 | #include "base/base_export.h" |
| 12 | #include "base/callback_forward.h" |
| 13 | #include "base/gtest_prod_util.h" |
| 14 | #include "base/macros.h" |
| 15 | #include "base/memory/scoped_refptr.h" |
| 16 | #include "base/message_loop/message_pump_type.h" |
| 17 | #include "base/message_loop/timer_slack.h" |
| 18 | #include "base/pending_task.h" |
| 19 | #include "base/run_loop.h" |
| 20 | #include "base/threading/thread_checker.h" |
| 21 | #include "base/time/time.h" |
| 22 | #include "build/build_config.h" |
| 23 |
| 24 | namespace base { |
| 25 |
| 26 | class MessagePump; |
| 27 | class TaskObserver; |
| 28 |
| 29 | namespace sequence_manager { |
| 30 | class TaskQueue; |
| 31 | namespace internal { |
| 32 | class SequenceManagerImpl; |
| 33 | } // namespace internal |
| 34 | } // namespace sequence_manager |
| 35 |
| 36 | // A MessageLoop is used to process events for a particular thread. There is |
| 37 | // at most one MessageLoop instance per thread. |
| 38 | // |
| 39 | // Events include at a minimum Task instances submitted to the MessageLoop's |
| 40 | // TaskRunner. Depending on the Type of message pump used by the MessageLoop |
| 41 | // other events such as UI messages may be processed. On Windows APC calls (as |
| 42 | // time permits) and signals sent to a registered set of HANDLEs may also be |
| 43 | // processed. |
| 44 | // |
| 45 | // The MessageLoop's API should only be used directly by its owner (and users |
| 46 | // which the owner opts to share a MessageLoop* with). Other ways to access |
| 47 | // subsets of the MessageLoop API: |
| 48 | // - base::RunLoop : Drive the MessageLoop from the thread it's bound to. |
| 49 | // - base::Thread/SequencedTaskRunnerHandle : Post back to the MessageLoop |
| 50 | // from a task running on it. |
| 51 | // - SequenceLocalStorageSlot : Bind external state to this MessageLoop. |
| 52 | // - base::MessageLoopCurrent : Access statically exposed APIs of this |
| 53 | // MessageLoop. |
| 54 | // - Embedders may provide their own static accessors to post tasks on |
| 55 | // specific loops (e.g. content::BrowserThreads). |
| 56 | // |
| 57 | // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called |
| 58 | // on the thread where the MessageLoop's Run method executes. |
| 59 | // |
| 60 | // NOTE: MessageLoop has task reentrancy protection. This means that if a |
| 61 | // task is being processed, a second task cannot start until the first task is |
| 62 | // finished. Reentrancy can happen when processing a task, and an inner |
| 63 | // message pump is created. That inner pump then processes native messages |
| 64 | // which could implicitly start an inner task. Inner message pumps are created |
| 65 | // with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions |
| 66 | // (DoDragDrop), printer functions (StartDoc) and *many* others. |
| 67 | // |
| 68 | // Sample workaround when inner task processing is needed: |
| 69 | // HRESULT hr; |
| 70 | // { |
| 71 | // MessageLoopCurrent::ScopedNestableTaskAllower allow; |
| 72 | // hr = DoDragDrop(...); // Implicitly runs a modal message loop. |
| 73 | // } |
| 74 | // // Process |hr| (the result returned by DoDragDrop()). |
| 75 | // |
| 76 | // Please be SURE your task is reentrant (nestable) and all global variables |
| 77 | // are stable and accessible before calling SetNestableTasksAllowed(true). |
| 78 | // |
| 79 | // DEPRECATED: Use a SingleThreadTaskExecutor instead or TaskEnvironment |
| 80 | // for tests. TODO(https://crbug.com/891670/) remove this class. |
| 81 | class BASE_EXPORT MessageLoop { |
| 82 | public: |
| 83 | // Normally, it is not necessary to instantiate a MessageLoop. Instead, it |
| 84 | // is typical to make use of the current thread's MessageLoop instance. |
| 85 | explicit MessageLoop(MessagePumpType type = MessagePumpType::DEFAULT); |
| 86 | // Creates a MessageLoop with the supplied MessagePump, which must be |
| 87 | // non-null. |
| 88 | explicit MessageLoop(std::unique_ptr<MessagePump> custom_pump); |
| 89 |
| 90 | virtual ~MessageLoop(); |
| 91 |
| 92 | // Set the timer slack for this message loop. |
| 93 | void SetTimerSlack(TimerSlack timer_slack); |
| 94 |
| 95 | // Returns true if this loop's pump is |type|. This allows subclasses |
| 96 | // (especially those in tests) to specialize how they are identified. |
| 97 | virtual bool IsType(MessagePumpType type) const; |
| 98 |
| 99 | // Returns the type passed to the constructor. |
| 100 | MessagePumpType type() const { return type_; } |
| 101 |
| 102 | // Sets a new TaskRunner for this message loop. If the message loop was |
| 103 | // already bound, this must be called on the thread to which it is bound. |
| 104 | void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner); |
| 105 |
| 106 | // Gets the TaskRunner associated with this message loop. |
| 107 | scoped_refptr<SingleThreadTaskRunner> task_runner() const; |
| 108 |
| 109 | // These functions can only be called on the same thread that |this| is |
| 110 | // running on. |
| 111 | // These functions must not be called from a TaskObserver callback. |
| 112 | void AddTaskObserver(TaskObserver* task_observer); |
| 113 | void RemoveTaskObserver(TaskObserver* task_observer); |
| 114 |
| 115 | // Returns true if the message loop is idle (ignoring delayed tasks). This is |
| 116 | // the same condition which triggers DoWork() to return false: i.e. |
| 117 | // out of tasks which can be processed at the current run-level -- there might |
| 118 | // be deferred non-nestable tasks remaining if currently in a nested run |
| 119 | // level. |
| 120 | // TODO(alexclarke): Make this const when MessageLoopImpl goes away. |
| 121 | bool IsIdleForTesting(); |
| 122 |
| 123 | //---------------------------------------------------------------------------- |
| 124 | protected: |
| 125 | // Returns true if this is the active MessageLoop for the current thread. |
| 126 | bool IsBoundToCurrentThread() const; |
| 127 |
| 128 | using MessagePumpFactoryCallback = |
| 129 | OnceCallback<std::unique_ptr<MessagePump>()>; |
| 130 |
| 131 | // Common protected constructor. Other constructors delegate the |
| 132 | // initialization to this constructor. |
| 133 | // A subclass can invoke this constructor to create a message_loop of a |
| 134 | // specific type with a custom loop. The implementation does not call |
| 135 | // BindToCurrentThread. If this constructor is invoked directly by a subclass, |
| 136 | // then the subclass must subsequently bind the message loop. |
| 137 | MessageLoop(MessagePumpType type, std::unique_ptr<MessagePump> pump); |
| 138 |
| 139 | // Configure various members and bind this message loop to the current thread. |
| 140 | void BindToCurrentThread(); |
| 141 |
| 142 | // A raw pointer to the MessagePump handed-off to |sequence_manager_|. |
| 143 | // Valid for the lifetime of |sequence_manager_|. |
| 144 | MessagePump* pump_ = nullptr; |
| 145 |
| 146 | // TODO(crbug.com/891670): We shouldn't publicly expose all of |
| 147 | // SequenceManagerImpl. |
| 148 | const std::unique_ptr<sequence_manager::internal::SequenceManagerImpl> |
| 149 | sequence_manager_; |
| 150 | // SequenceManager requires an explicit initialisation of the default task |
| 151 | // queue. |
| 152 | const scoped_refptr<sequence_manager::TaskQueue> default_task_queue_; |
| 153 |
| 154 | private: |
| 155 | friend class MessageLoopTypedTest; |
| 156 | friend class ScheduleWorkTest; |
| 157 | friend class Thread; |
| 158 | friend class sequence_manager::internal::SequenceManagerImpl; |
| 159 | FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, DeleteUnboundLoop); |
| 160 |
| 161 | // Creates a MessageLoop without binding to a thread. |
| 162 | // |
| 163 | // It is valid to call this to create a new message loop on one thread, |
| 164 | // and then pass it to the thread where the message loop actually runs. |
| 165 | // The message loop's BindToCurrentThread() method must be called on the |
| 166 | // thread the message loop runs on, before calling Run(). |
| 167 | // Before BindToCurrentThread() is called, only Post*Task() functions can |
| 168 | // be called on the message loop. |
| 169 | static std::unique_ptr<MessageLoop> CreateUnbound(MessagePumpType type); |
| 170 | static std::unique_ptr<MessageLoop> CreateUnbound( |
| 171 | std::unique_ptr<MessagePump> pump); |
| 172 |
| 173 | scoped_refptr<sequence_manager::TaskQueue> CreateDefaultTaskQueue(); |
| 174 |
| 175 | std::unique_ptr<MessagePump> CreateMessagePump(); |
| 176 |
| 177 | sequence_manager::internal::SequenceManagerImpl* GetSequenceManagerImpl() |
| 178 | const { |
| 179 | return sequence_manager_.get(); |
| 180 | } |
| 181 |
| 182 | const MessagePumpType type_; |
| 183 |
| 184 | // If set this will be returned by the next call to CreateMessagePump(). |
| 185 | // This is only set if |type_| is TYPE_CUSTOM and |pump_| is null. |
| 186 | std::unique_ptr<MessagePump> custom_pump_; |
| 187 |
| 188 | // Id of the thread this message loop is bound to. Initialized once when the |
| 189 | // MessageLoop is bound to its thread and constant forever after. |
| 190 | PlatformThreadId thread_id_ = kInvalidThreadId; |
| 191 |
| 192 | // Verifies that calls are made on the thread on which BindToCurrentThread() |
| 193 | // was invoked. |
| 194 | THREAD_CHECKER(bound_thread_checker_); |
| 195 |
| 196 | DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
| 197 | }; |
| 198 |
| 199 | } // namespace base |
| 200 |
| 201 | #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| 202 |