xref: /aosp_15_r20/external/libchrome/base/message_loop/message_loop.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <memory>
9*635a8641SAndroid Build Coastguard Worker #include <queue>
10*635a8641SAndroid Build Coastguard Worker #include <string>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/callback_forward.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/gtest_prod_util.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/memory/scoped_refptr.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/incoming_task_queue.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/message_loop_current.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/message_loop_task_runner.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/message_pump.h"
21*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/timer_slack.h"
22*635a8641SAndroid Build Coastguard Worker #include "base/observer_list.h"
23*635a8641SAndroid Build Coastguard Worker #include "base/pending_task.h"
24*635a8641SAndroid Build Coastguard Worker #include "base/run_loop.h"
25*635a8641SAndroid Build Coastguard Worker #include "base/synchronization/lock.h"
26*635a8641SAndroid Build Coastguard Worker #include "base/threading/sequence_local_storage_map.h"
27*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_checker.h"
28*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h"
29*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker // Just in libchrome
32*635a8641SAndroid Build Coastguard Worker namespace brillo {
33*635a8641SAndroid Build Coastguard Worker class BaseMessageLoop;
34*635a8641SAndroid Build Coastguard Worker }
35*635a8641SAndroid Build Coastguard Worker 
36*635a8641SAndroid Build Coastguard Worker namespace base {
37*635a8641SAndroid Build Coastguard Worker 
38*635a8641SAndroid Build Coastguard Worker class ThreadTaskRunnerHandle;
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker // A MessageLoop is used to process events for a particular thread.  There is
41*635a8641SAndroid Build Coastguard Worker // at most one MessageLoop instance per thread.
42*635a8641SAndroid Build Coastguard Worker //
43*635a8641SAndroid Build Coastguard Worker // Events include at a minimum Task instances submitted to the MessageLoop's
44*635a8641SAndroid Build Coastguard Worker // TaskRunner. Depending on the type of message pump used by the MessageLoop
45*635a8641SAndroid Build Coastguard Worker // other events such as UI messages may be processed.  On Windows APC calls (as
46*635a8641SAndroid Build Coastguard Worker // time permits) and signals sent to a registered set of HANDLEs may also be
47*635a8641SAndroid Build Coastguard Worker // processed.
48*635a8641SAndroid Build Coastguard Worker //
49*635a8641SAndroid Build Coastguard Worker // The MessageLoop's API should only be used directly by its owner (and users
50*635a8641SAndroid Build Coastguard Worker // which the owner opts to share a MessageLoop* with). Other ways to access
51*635a8641SAndroid Build Coastguard Worker // subsets of the MessageLoop API:
52*635a8641SAndroid Build Coastguard Worker //   - base::RunLoop : Drive the MessageLoop from the thread it's bound to.
53*635a8641SAndroid Build Coastguard Worker //   - base::Thread/SequencedTaskRunnerHandle : Post back to the MessageLoop
54*635a8641SAndroid Build Coastguard Worker //     from a task running on it.
55*635a8641SAndroid Build Coastguard Worker //   - SequenceLocalStorageSlot : Bind external state to this MessageLoop.
56*635a8641SAndroid Build Coastguard Worker //   - base::MessageLoopCurrent : Access statically exposed APIs of this
57*635a8641SAndroid Build Coastguard Worker //     MessageLoop.
58*635a8641SAndroid Build Coastguard Worker //   - Embedders may provide their own static accessors to post tasks on
59*635a8641SAndroid Build Coastguard Worker //     specific loops (e.g. content::BrowserThreads).
60*635a8641SAndroid Build Coastguard Worker //
61*635a8641SAndroid Build Coastguard Worker // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called
62*635a8641SAndroid Build Coastguard Worker // on the thread where the MessageLoop's Run method executes.
63*635a8641SAndroid Build Coastguard Worker //
64*635a8641SAndroid Build Coastguard Worker // NOTE: MessageLoop has task reentrancy protection.  This means that if a
65*635a8641SAndroid Build Coastguard Worker // task is being processed, a second task cannot start until the first task is
66*635a8641SAndroid Build Coastguard Worker // finished.  Reentrancy can happen when processing a task, and an inner
67*635a8641SAndroid Build Coastguard Worker // message pump is created.  That inner pump then processes native messages
68*635a8641SAndroid Build Coastguard Worker // which could implicitly start an inner task.  Inner message pumps are created
69*635a8641SAndroid Build Coastguard Worker // with dialogs (DialogBox), common dialogs (GetOpenFileName), OLE functions
70*635a8641SAndroid Build Coastguard Worker // (DoDragDrop), printer functions (StartDoc) and *many* others.
71*635a8641SAndroid Build Coastguard Worker //
72*635a8641SAndroid Build Coastguard Worker // Sample workaround when inner task processing is needed:
73*635a8641SAndroid Build Coastguard Worker //   HRESULT hr;
74*635a8641SAndroid Build Coastguard Worker //   {
75*635a8641SAndroid Build Coastguard Worker //     MessageLoopCurrent::ScopedNestableTaskAllower allow;
76*635a8641SAndroid Build Coastguard Worker //     hr = DoDragDrop(...); // Implicitly runs a modal message loop.
77*635a8641SAndroid Build Coastguard Worker //   }
78*635a8641SAndroid Build Coastguard Worker //   // Process |hr| (the result returned by DoDragDrop()).
79*635a8641SAndroid Build Coastguard Worker //
80*635a8641SAndroid Build Coastguard Worker // Please be SURE your task is reentrant (nestable) and all global variables
81*635a8641SAndroid Build Coastguard Worker // are stable and accessible before calling SetNestableTasksAllowed(true).
82*635a8641SAndroid Build Coastguard Worker //
83*635a8641SAndroid Build Coastguard Worker // TODO(gab): MessageLoop doesn't need to be a MessageLoopCurrent once callers
84*635a8641SAndroid Build Coastguard Worker // that store MessageLoop::current() in a MessageLoop* variable have been
85*635a8641SAndroid Build Coastguard Worker // updated to use a MessageLoopCurrent variable.
86*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT MessageLoop : public MessagePump::Delegate,
87*635a8641SAndroid Build Coastguard Worker                                 public RunLoop::Delegate,
88*635a8641SAndroid Build Coastguard Worker                                 public MessageLoopCurrent {
89*635a8641SAndroid Build Coastguard Worker  public:
90*635a8641SAndroid Build Coastguard Worker   // TODO(gab): Migrate usage of this class to MessageLoopCurrent and remove
91*635a8641SAndroid Build Coastguard Worker   // this forwarded declaration.
92*635a8641SAndroid Build Coastguard Worker   using DestructionObserver = MessageLoopCurrent::DestructionObserver;
93*635a8641SAndroid Build Coastguard Worker 
94*635a8641SAndroid Build Coastguard Worker   // A MessageLoop has a particular type, which indicates the set of
95*635a8641SAndroid Build Coastguard Worker   // asynchronous events it may process in addition to tasks and timers.
96*635a8641SAndroid Build Coastguard Worker   //
97*635a8641SAndroid Build Coastguard Worker   // TYPE_DEFAULT
98*635a8641SAndroid Build Coastguard Worker   //   This type of ML only supports tasks and timers.
99*635a8641SAndroid Build Coastguard Worker   //
100*635a8641SAndroid Build Coastguard Worker   // TYPE_UI
101*635a8641SAndroid Build Coastguard Worker   //   This type of ML also supports native UI events (e.g., Windows messages).
102*635a8641SAndroid Build Coastguard Worker   //   See also MessageLoopForUI.
103*635a8641SAndroid Build Coastguard Worker   //
104*635a8641SAndroid Build Coastguard Worker   // TYPE_IO
105*635a8641SAndroid Build Coastguard Worker   //   This type of ML also supports asynchronous IO.  See also
106*635a8641SAndroid Build Coastguard Worker   //   MessageLoopForIO.
107*635a8641SAndroid Build Coastguard Worker   //
108*635a8641SAndroid Build Coastguard Worker   // TYPE_JAVA
109*635a8641SAndroid Build Coastguard Worker   //   This type of ML is backed by a Java message handler which is responsible
110*635a8641SAndroid Build Coastguard Worker   //   for running the tasks added to the ML. This is only for use on Android.
111*635a8641SAndroid Build Coastguard Worker   //   TYPE_JAVA behaves in essence like TYPE_UI, except during construction
112*635a8641SAndroid Build Coastguard Worker   //   where it does not use the main thread specific pump factory.
113*635a8641SAndroid Build Coastguard Worker   //
114*635a8641SAndroid Build Coastguard Worker   // TYPE_CUSTOM
115*635a8641SAndroid Build Coastguard Worker   //   MessagePump was supplied to constructor.
116*635a8641SAndroid Build Coastguard Worker   //
117*635a8641SAndroid Build Coastguard Worker   enum Type {
118*635a8641SAndroid Build Coastguard Worker     TYPE_DEFAULT,
119*635a8641SAndroid Build Coastguard Worker     TYPE_UI,
120*635a8641SAndroid Build Coastguard Worker     TYPE_CUSTOM,
121*635a8641SAndroid Build Coastguard Worker     TYPE_IO,
122*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
123*635a8641SAndroid Build Coastguard Worker     TYPE_JAVA,
124*635a8641SAndroid Build Coastguard Worker #endif  // defined(OS_ANDROID)
125*635a8641SAndroid Build Coastguard Worker   };
126*635a8641SAndroid Build Coastguard Worker 
127*635a8641SAndroid Build Coastguard Worker   // Normally, it is not necessary to instantiate a MessageLoop.  Instead, it
128*635a8641SAndroid Build Coastguard Worker   // is typical to make use of the current thread's MessageLoop instance.
129*635a8641SAndroid Build Coastguard Worker   explicit MessageLoop(Type type = TYPE_DEFAULT);
130*635a8641SAndroid Build Coastguard Worker   // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must
131*635a8641SAndroid Build Coastguard Worker   // be non-NULL.
132*635a8641SAndroid Build Coastguard Worker   explicit MessageLoop(std::unique_ptr<MessagePump> pump);
133*635a8641SAndroid Build Coastguard Worker 
134*635a8641SAndroid Build Coastguard Worker   ~MessageLoop() override;
135*635a8641SAndroid Build Coastguard Worker 
136*635a8641SAndroid Build Coastguard Worker   // TODO(gab): Mass migrate callers to MessageLoopCurrent::Get().
137*635a8641SAndroid Build Coastguard Worker   static MessageLoopCurrent current();
138*635a8641SAndroid Build Coastguard Worker 
139*635a8641SAndroid Build Coastguard Worker   using MessagePumpFactory = std::unique_ptr<MessagePump>();
140*635a8641SAndroid Build Coastguard Worker   // Uses the given base::MessagePumpForUIFactory to override the default
141*635a8641SAndroid Build Coastguard Worker   // MessagePump implementation for 'TYPE_UI'. Returns true if the factory
142*635a8641SAndroid Build Coastguard Worker   // was successfully registered.
143*635a8641SAndroid Build Coastguard Worker   static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory);
144*635a8641SAndroid Build Coastguard Worker 
145*635a8641SAndroid Build Coastguard Worker   // Creates the default MessagePump based on |type|. Caller owns return
146*635a8641SAndroid Build Coastguard Worker   // value.
147*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<MessagePump> CreateMessagePumpForType(Type type);
148*635a8641SAndroid Build Coastguard Worker 
149*635a8641SAndroid Build Coastguard Worker   // Set the timer slack for this message loop.
SetTimerSlack(TimerSlack timer_slack)150*635a8641SAndroid Build Coastguard Worker   void SetTimerSlack(TimerSlack timer_slack) {
151*635a8641SAndroid Build Coastguard Worker     pump_->SetTimerSlack(timer_slack);
152*635a8641SAndroid Build Coastguard Worker   }
153*635a8641SAndroid Build Coastguard Worker 
154*635a8641SAndroid Build Coastguard Worker   // Returns true if this loop is |type|. This allows subclasses (especially
155*635a8641SAndroid Build Coastguard Worker   // those in tests) to specialize how they are identified.
156*635a8641SAndroid Build Coastguard Worker   virtual bool IsType(Type type) const;
157*635a8641SAndroid Build Coastguard Worker 
158*635a8641SAndroid Build Coastguard Worker   // Returns the type passed to the constructor.
type()159*635a8641SAndroid Build Coastguard Worker   Type type() const { return type_; }
160*635a8641SAndroid Build Coastguard Worker 
161*635a8641SAndroid Build Coastguard Worker   // Returns the name of the thread this message loop is bound to. This function
162*635a8641SAndroid Build Coastguard Worker   // is only valid when this message loop is running, BindToCurrentThread has
163*635a8641SAndroid Build Coastguard Worker   // already been called and has an "happens-before" relationship with this call
164*635a8641SAndroid Build Coastguard Worker   // (this relationship is obtained implicitly by the MessageLoop's task posting
165*635a8641SAndroid Build Coastguard Worker   // system unless calling this very early).
166*635a8641SAndroid Build Coastguard Worker   std::string GetThreadName() const;
167*635a8641SAndroid Build Coastguard Worker 
168*635a8641SAndroid Build Coastguard Worker   // Gets the TaskRunner associated with this message loop.
task_runner()169*635a8641SAndroid Build Coastguard Worker   const scoped_refptr<SingleThreadTaskRunner>& task_runner() const {
170*635a8641SAndroid Build Coastguard Worker     return task_runner_;
171*635a8641SAndroid Build Coastguard Worker   }
172*635a8641SAndroid Build Coastguard Worker 
173*635a8641SAndroid Build Coastguard Worker   // Sets a new TaskRunner for this message loop. The message loop must already
174*635a8641SAndroid Build Coastguard Worker   // have been bound to a thread prior to this call, and the task runner must
175*635a8641SAndroid Build Coastguard Worker   // belong to that thread. Note that changing the task runner will also affect
176*635a8641SAndroid Build Coastguard Worker   // the ThreadTaskRunnerHandle for the target thread. Must be called on the
177*635a8641SAndroid Build Coastguard Worker   // thread to which the message loop is bound.
178*635a8641SAndroid Build Coastguard Worker   void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner);
179*635a8641SAndroid Build Coastguard Worker 
180*635a8641SAndroid Build Coastguard Worker   // Clears task_runner() and the ThreadTaskRunnerHandle for the target thread.
181*635a8641SAndroid Build Coastguard Worker   // Must be called on the thread to which the message loop is bound.
182*635a8641SAndroid Build Coastguard Worker   void ClearTaskRunnerForTesting();
183*635a8641SAndroid Build Coastguard Worker 
184*635a8641SAndroid Build Coastguard Worker   // TODO(https://crbug.com/825327): Remove users of TaskObservers through
185*635a8641SAndroid Build Coastguard Worker   // MessageLoop::current() and migrate the type back here.
186*635a8641SAndroid Build Coastguard Worker   using TaskObserver = MessageLoopCurrent::TaskObserver;
187*635a8641SAndroid Build Coastguard Worker 
188*635a8641SAndroid Build Coastguard Worker   // These functions can only be called on the same thread that |this| is
189*635a8641SAndroid Build Coastguard Worker   // running on.
190*635a8641SAndroid Build Coastguard Worker   void AddTaskObserver(TaskObserver* task_observer);
191*635a8641SAndroid Build Coastguard Worker   void RemoveTaskObserver(TaskObserver* task_observer);
192*635a8641SAndroid Build Coastguard Worker 
193*635a8641SAndroid Build Coastguard Worker   // Returns true if the message loop is idle (ignoring delayed tasks). This is
194*635a8641SAndroid Build Coastguard Worker   // the same condition which triggers DoWork() to return false: i.e.
195*635a8641SAndroid Build Coastguard Worker   // out of tasks which can be processed at the current run-level -- there might
196*635a8641SAndroid Build Coastguard Worker   // be deferred non-nestable tasks remaining if currently in a nested run
197*635a8641SAndroid Build Coastguard Worker   // level.
198*635a8641SAndroid Build Coastguard Worker   bool IsIdleForTesting();
199*635a8641SAndroid Build Coastguard Worker 
200*635a8641SAndroid Build Coastguard Worker   // Runs the specified PendingTask.
201*635a8641SAndroid Build Coastguard Worker   void RunTask(PendingTask* pending_task);
202*635a8641SAndroid Build Coastguard Worker 
203*635a8641SAndroid Build Coastguard Worker   //----------------------------------------------------------------------------
204*635a8641SAndroid Build Coastguard Worker  protected:
205*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<MessagePump> pump_;
206*635a8641SAndroid Build Coastguard Worker 
207*635a8641SAndroid Build Coastguard Worker   using MessagePumpFactoryCallback =
208*635a8641SAndroid Build Coastguard Worker       OnceCallback<std::unique_ptr<MessagePump>()>;
209*635a8641SAndroid Build Coastguard Worker 
210*635a8641SAndroid Build Coastguard Worker   // Common protected constructor. Other constructors delegate the
211*635a8641SAndroid Build Coastguard Worker   // initialization to this constructor.
212*635a8641SAndroid Build Coastguard Worker   // A subclass can invoke this constructor to create a message_loop of a
213*635a8641SAndroid Build Coastguard Worker   // specific type with a custom loop. The implementation does not call
214*635a8641SAndroid Build Coastguard Worker   // BindToCurrentThread. If this constructor is invoked directly by a subclass,
215*635a8641SAndroid Build Coastguard Worker   // then the subclass must subsequently bind the message loop.
216*635a8641SAndroid Build Coastguard Worker   MessageLoop(Type type, MessagePumpFactoryCallback pump_factory);
217*635a8641SAndroid Build Coastguard Worker 
218*635a8641SAndroid Build Coastguard Worker   // Configure various members and bind this message loop to the current thread.
219*635a8641SAndroid Build Coastguard Worker   void BindToCurrentThread();
220*635a8641SAndroid Build Coastguard Worker 
221*635a8641SAndroid Build Coastguard Worker  private:
222*635a8641SAndroid Build Coastguard Worker   //only in libchrome
223*635a8641SAndroid Build Coastguard Worker   friend class brillo::BaseMessageLoop;
224*635a8641SAndroid Build Coastguard Worker   friend class internal::IncomingTaskQueue;
225*635a8641SAndroid Build Coastguard Worker   friend class MessageLoopCurrent;
226*635a8641SAndroid Build Coastguard Worker   friend class MessageLoopCurrentForIO;
227*635a8641SAndroid Build Coastguard Worker   friend class MessageLoopCurrentForUI;
228*635a8641SAndroid Build Coastguard Worker   friend class ScheduleWorkTest;
229*635a8641SAndroid Build Coastguard Worker   friend class Thread;
230*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, DeleteUnboundLoop);
231*635a8641SAndroid Build Coastguard Worker 
232*635a8641SAndroid Build Coastguard Worker   class Controller;
233*635a8641SAndroid Build Coastguard Worker 
234*635a8641SAndroid Build Coastguard Worker   // Creates a MessageLoop without binding to a thread.
235*635a8641SAndroid Build Coastguard Worker   // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given
236*635a8641SAndroid Build Coastguard Worker   // to create a message pump for this message loop.  Otherwise a default
237*635a8641SAndroid Build Coastguard Worker   // message pump for the |type| is created.
238*635a8641SAndroid Build Coastguard Worker   //
239*635a8641SAndroid Build Coastguard Worker   // It is valid to call this to create a new message loop on one thread,
240*635a8641SAndroid Build Coastguard Worker   // and then pass it to the thread where the message loop actually runs.
241*635a8641SAndroid Build Coastguard Worker   // The message loop's BindToCurrentThread() method must be called on the
242*635a8641SAndroid Build Coastguard Worker   // thread the message loop runs on, before calling Run().
243*635a8641SAndroid Build Coastguard Worker   // Before BindToCurrentThread() is called, only Post*Task() functions can
244*635a8641SAndroid Build Coastguard Worker   // be called on the message loop.
245*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<MessageLoop> CreateUnbound(
246*635a8641SAndroid Build Coastguard Worker       Type type,
247*635a8641SAndroid Build Coastguard Worker       MessagePumpFactoryCallback pump_factory);
248*635a8641SAndroid Build Coastguard Worker 
249*635a8641SAndroid Build Coastguard Worker   // Sets the ThreadTaskRunnerHandle for the current thread to point to the
250*635a8641SAndroid Build Coastguard Worker   // task runner for this message loop.
251*635a8641SAndroid Build Coastguard Worker   void SetThreadTaskRunnerHandle();
252*635a8641SAndroid Build Coastguard Worker 
253*635a8641SAndroid Build Coastguard Worker   // RunLoop::Delegate:
254*635a8641SAndroid Build Coastguard Worker   void Run(bool application_tasks_allowed) override;
255*635a8641SAndroid Build Coastguard Worker   void Quit() override;
256*635a8641SAndroid Build Coastguard Worker   void EnsureWorkScheduled() override;
257*635a8641SAndroid Build Coastguard Worker 
258*635a8641SAndroid Build Coastguard Worker   // Called to process any delayed non-nestable tasks.
259*635a8641SAndroid Build Coastguard Worker   bool ProcessNextDelayedNonNestableTask();
260*635a8641SAndroid Build Coastguard Worker 
261*635a8641SAndroid Build Coastguard Worker   // Calls RunTask or queues the pending_task on the deferred task list if it
262*635a8641SAndroid Build Coastguard Worker   // cannot be run right now.  Returns true if the task was run.
263*635a8641SAndroid Build Coastguard Worker   bool DeferOrRunPendingTask(PendingTask pending_task);
264*635a8641SAndroid Build Coastguard Worker 
265*635a8641SAndroid Build Coastguard Worker   // Delete tasks that haven't run yet without running them.  Used in the
266*635a8641SAndroid Build Coastguard Worker   // destructor to make sure all the task's destructors get called.
267*635a8641SAndroid Build Coastguard Worker   void DeletePendingTasks();
268*635a8641SAndroid Build Coastguard Worker 
269*635a8641SAndroid Build Coastguard Worker   // Wakes up the message pump. Can be called on any thread. The caller is
270*635a8641SAndroid Build Coastguard Worker   // responsible for synchronizing ScheduleWork() calls.
271*635a8641SAndroid Build Coastguard Worker   void ScheduleWork();
272*635a8641SAndroid Build Coastguard Worker 
273*635a8641SAndroid Build Coastguard Worker   // MessagePump::Delegate methods:
274*635a8641SAndroid Build Coastguard Worker   bool DoWork() override;
275*635a8641SAndroid Build Coastguard Worker   bool DoDelayedWork(TimeTicks* next_delayed_work_time) override;
276*635a8641SAndroid Build Coastguard Worker   bool DoIdleWork() override;
277*635a8641SAndroid Build Coastguard Worker 
278*635a8641SAndroid Build Coastguard Worker   const Type type_;
279*635a8641SAndroid Build Coastguard Worker 
280*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
281*635a8641SAndroid Build Coastguard Worker   // Tracks if we have requested high resolution timers. Its only use is to
282*635a8641SAndroid Build Coastguard Worker   // turn off the high resolution timer upon loop destruction.
283*635a8641SAndroid Build Coastguard Worker   bool in_high_res_mode_ = false;
284*635a8641SAndroid Build Coastguard Worker #endif
285*635a8641SAndroid Build Coastguard Worker 
286*635a8641SAndroid Build Coastguard Worker   // A recent snapshot of Time::Now(), used to check delayed_work_queue_.
287*635a8641SAndroid Build Coastguard Worker   TimeTicks recent_time_;
288*635a8641SAndroid Build Coastguard Worker 
289*635a8641SAndroid Build Coastguard Worker   // Non-null when the last thing this MessageLoop did is become idle with
290*635a8641SAndroid Build Coastguard Worker   // pending delayed tasks. Used to report metrics on the following wake up.
291*635a8641SAndroid Build Coastguard Worker   struct ScheduledWakeup {
292*635a8641SAndroid Build Coastguard Worker     // The scheduled time of the next delayed task when this loop became idle.
293*635a8641SAndroid Build Coastguard Worker     TimeTicks next_run_time;
294*635a8641SAndroid Build Coastguard Worker     // The delta until |next_run_time| when this loop became idle.
295*635a8641SAndroid Build Coastguard Worker     TimeDelta intended_sleep;
296*635a8641SAndroid Build Coastguard Worker   } scheduled_wakeup_;
297*635a8641SAndroid Build Coastguard Worker 
298*635a8641SAndroid Build Coastguard Worker   ObserverList<DestructionObserver> destruction_observers_;
299*635a8641SAndroid Build Coastguard Worker 
300*635a8641SAndroid Build Coastguard Worker   // A boolean which prevents unintentional reentrant task execution (e.g. from
301*635a8641SAndroid Build Coastguard Worker   // induced nested message loops). As such, nested message loops will only
302*635a8641SAndroid Build Coastguard Worker   // process system messages (not application tasks) by default. A nested loop
303*635a8641SAndroid Build Coastguard Worker   // layer must have been explicitly granted permission to be able to execute
304*635a8641SAndroid Build Coastguard Worker   // application tasks. This is granted either by
305*635a8641SAndroid Build Coastguard Worker   // RunLoop::Type::kNestableTasksAllowed when the loop is driven by the
306*635a8641SAndroid Build Coastguard Worker   // application or by a ScopedNestableTaskAllower preceding a system call that
307*635a8641SAndroid Build Coastguard Worker   // is known to generate a system-driven nested loop.
308*635a8641SAndroid Build Coastguard Worker   bool task_execution_allowed_ = true;
309*635a8641SAndroid Build Coastguard Worker 
310*635a8641SAndroid Build Coastguard Worker   // pump_factory_.Run() is called to create a message pump for this loop
311*635a8641SAndroid Build Coastguard Worker   // if type_ is TYPE_CUSTOM and pump_ is null.
312*635a8641SAndroid Build Coastguard Worker   MessagePumpFactoryCallback pump_factory_;
313*635a8641SAndroid Build Coastguard Worker 
314*635a8641SAndroid Build Coastguard Worker   ObserverList<TaskObserver> task_observers_;
315*635a8641SAndroid Build Coastguard Worker 
316*635a8641SAndroid Build Coastguard Worker   // Pointer to this MessageLoop's Controller, valid until the reference to
317*635a8641SAndroid Build Coastguard Worker   // |incoming_task_queue_| is dropped below.
318*635a8641SAndroid Build Coastguard Worker   Controller* const message_loop_controller_;
319*635a8641SAndroid Build Coastguard Worker   scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_;
320*635a8641SAndroid Build Coastguard Worker 
321*635a8641SAndroid Build Coastguard Worker   // A task runner which we haven't bound to a thread yet.
322*635a8641SAndroid Build Coastguard Worker   scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_;
323*635a8641SAndroid Build Coastguard Worker 
324*635a8641SAndroid Build Coastguard Worker   // The task runner associated with this message loop.
325*635a8641SAndroid Build Coastguard Worker   scoped_refptr<SingleThreadTaskRunner> task_runner_;
326*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_;
327*635a8641SAndroid Build Coastguard Worker 
328*635a8641SAndroid Build Coastguard Worker   // Id of the thread this message loop is bound to. Initialized once when the
329*635a8641SAndroid Build Coastguard Worker   // MessageLoop is bound to its thread and constant forever after.
330*635a8641SAndroid Build Coastguard Worker   PlatformThreadId thread_id_ = kInvalidThreadId;
331*635a8641SAndroid Build Coastguard Worker 
332*635a8641SAndroid Build Coastguard Worker   // Holds data stored through the SequenceLocalStorageSlot API.
333*635a8641SAndroid Build Coastguard Worker   internal::SequenceLocalStorageMap sequence_local_storage_map_;
334*635a8641SAndroid Build Coastguard Worker 
335*635a8641SAndroid Build Coastguard Worker   // Enables the SequenceLocalStorageSlot API within its scope.
336*635a8641SAndroid Build Coastguard Worker   // Instantiated in BindToCurrentThread().
337*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<internal::ScopedSetSequenceLocalStorageMapForCurrentThread>
338*635a8641SAndroid Build Coastguard Worker       scoped_set_sequence_local_storage_map_for_current_thread_;
339*635a8641SAndroid Build Coastguard Worker 
340*635a8641SAndroid Build Coastguard Worker   // Verifies that calls are made on the thread on which BindToCurrentThread()
341*635a8641SAndroid Build Coastguard Worker   // was invoked.
342*635a8641SAndroid Build Coastguard Worker   THREAD_CHECKER(bound_thread_checker_);
343*635a8641SAndroid Build Coastguard Worker 
344*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(MessageLoop);
345*635a8641SAndroid Build Coastguard Worker };
346*635a8641SAndroid Build Coastguard Worker 
347*635a8641SAndroid Build Coastguard Worker // MessageLoopForUI is unsupported in libchrome for android target.
348*635a8641SAndroid Build Coastguard Worker // The Android UI thread is tied to the Android framework and is not used.
349*635a8641SAndroid Build Coastguard Worker // This is to avoid dependency on libandroid ALooper.
350*635a8641SAndroid Build Coastguard Worker #if !defined(OS_NACL) && !defined(OS_ANDROID)
351*635a8641SAndroid Build Coastguard Worker 
352*635a8641SAndroid Build Coastguard Worker //-----------------------------------------------------------------------------
353*635a8641SAndroid Build Coastguard Worker // MessageLoopForUI extends MessageLoop with methods that are particular to a
354*635a8641SAndroid Build Coastguard Worker // MessageLoop instantiated with TYPE_UI.
355*635a8641SAndroid Build Coastguard Worker //
356*635a8641SAndroid Build Coastguard Worker // By instantiating a MessageLoopForUI on the current thread, the owner enables
357*635a8641SAndroid Build Coastguard Worker // native UI message pumping.
358*635a8641SAndroid Build Coastguard Worker //
359*635a8641SAndroid Build Coastguard Worker // MessageLoopCurrentForUI is exposed statically on its thread via
360*635a8641SAndroid Build Coastguard Worker // MessageLoopCurrentForUI::Get() to provide additional functionality.
361*635a8641SAndroid Build Coastguard Worker //
362*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT MessageLoopForUI : public MessageLoop {
363*635a8641SAndroid Build Coastguard Worker  public:
364*635a8641SAndroid Build Coastguard Worker   explicit MessageLoopForUI(Type type = TYPE_UI);
365*635a8641SAndroid Build Coastguard Worker 
366*635a8641SAndroid Build Coastguard Worker   // TODO(gab): Mass migrate callers to MessageLoopCurrentForUI::Get()/IsSet().
367*635a8641SAndroid Build Coastguard Worker   static MessageLoopCurrentForUI current();
368*635a8641SAndroid Build Coastguard Worker   static bool IsCurrent();
369*635a8641SAndroid Build Coastguard Worker 
370*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
371*635a8641SAndroid Build Coastguard Worker   // On iOS, the main message loop cannot be Run().  Instead call Attach(),
372*635a8641SAndroid Build Coastguard Worker   // which connects this MessageLoop to the UI thread's CFRunLoop and allows
373*635a8641SAndroid Build Coastguard Worker   // PostTask() to work.
374*635a8641SAndroid Build Coastguard Worker   void Attach();
375*635a8641SAndroid Build Coastguard Worker #endif
376*635a8641SAndroid Build Coastguard Worker 
377*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
378*635a8641SAndroid Build Coastguard Worker   // On Android there are cases where we want to abort immediately without
379*635a8641SAndroid Build Coastguard Worker   // calling Quit(), in these cases we call Abort().
380*635a8641SAndroid Build Coastguard Worker   void Abort();
381*635a8641SAndroid Build Coastguard Worker 
382*635a8641SAndroid Build Coastguard Worker   // True if this message pump has been aborted.
383*635a8641SAndroid Build Coastguard Worker   bool IsAborted();
384*635a8641SAndroid Build Coastguard Worker 
385*635a8641SAndroid Build Coastguard Worker   // Since Run() is never called on Android, and the message loop is run by the
386*635a8641SAndroid Build Coastguard Worker   // java Looper, quitting the RunLoop won't join the thread, so we need a
387*635a8641SAndroid Build Coastguard Worker   // callback to run when the RunLoop goes idle to let the Java thread know when
388*635a8641SAndroid Build Coastguard Worker   // it can safely quit.
389*635a8641SAndroid Build Coastguard Worker   void QuitWhenIdle(base::OnceClosure callback);
390*635a8641SAndroid Build Coastguard Worker #endif
391*635a8641SAndroid Build Coastguard Worker 
392*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
393*635a8641SAndroid Build Coastguard Worker   // See method of the same name in the Windows MessagePumpForUI implementation.
394*635a8641SAndroid Build Coastguard Worker   void EnableWmQuit();
395*635a8641SAndroid Build Coastguard Worker #endif
396*635a8641SAndroid Build Coastguard Worker };
397*635a8641SAndroid Build Coastguard Worker 
398*635a8641SAndroid Build Coastguard Worker // Do not add any member variables to MessageLoopForUI!  This is important b/c
399*635a8641SAndroid Build Coastguard Worker // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI).  Any extra
400*635a8641SAndroid Build Coastguard Worker // data that you need should be stored on the MessageLoop's pump_ instance.
401*635a8641SAndroid Build Coastguard Worker static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForUI),
402*635a8641SAndroid Build Coastguard Worker               "MessageLoopForUI should not have extra member variables");
403*635a8641SAndroid Build Coastguard Worker 
404*635a8641SAndroid Build Coastguard Worker #endif  // !defined(OS_NACL) && !defined(OS_ANDROID)
405*635a8641SAndroid Build Coastguard Worker 
406*635a8641SAndroid Build Coastguard Worker //-----------------------------------------------------------------------------
407*635a8641SAndroid Build Coastguard Worker // MessageLoopForIO extends MessageLoop with methods that are particular to a
408*635a8641SAndroid Build Coastguard Worker // MessageLoop instantiated with TYPE_IO.
409*635a8641SAndroid Build Coastguard Worker //
410*635a8641SAndroid Build Coastguard Worker // By instantiating a MessageLoopForIO on the current thread, the owner enables
411*635a8641SAndroid Build Coastguard Worker // native async IO message pumping.
412*635a8641SAndroid Build Coastguard Worker //
413*635a8641SAndroid Build Coastguard Worker // MessageLoopCurrentForIO is exposed statically on its thread via
414*635a8641SAndroid Build Coastguard Worker // MessageLoopCurrentForIO::Get() to provide additional functionality.
415*635a8641SAndroid Build Coastguard Worker //
416*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT MessageLoopForIO : public MessageLoop {
417*635a8641SAndroid Build Coastguard Worker  public:
MessageLoopForIO()418*635a8641SAndroid Build Coastguard Worker   MessageLoopForIO() : MessageLoop(TYPE_IO) {}
419*635a8641SAndroid Build Coastguard Worker 
420*635a8641SAndroid Build Coastguard Worker   // TODO(gab): Mass migrate callers to MessageLoopCurrentForIO::Get()/IsSet().
421*635a8641SAndroid Build Coastguard Worker   static MessageLoopCurrentForIO current();
422*635a8641SAndroid Build Coastguard Worker   static bool IsCurrent();
423*635a8641SAndroid Build Coastguard Worker };
424*635a8641SAndroid Build Coastguard Worker 
425*635a8641SAndroid Build Coastguard Worker // Do not add any member variables to MessageLoopForIO!  This is important b/c
426*635a8641SAndroid Build Coastguard Worker // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO).  Any extra
427*635a8641SAndroid Build Coastguard Worker // data that you need should be stored on the MessageLoop's pump_ instance.
428*635a8641SAndroid Build Coastguard Worker static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
429*635a8641SAndroid Build Coastguard Worker               "MessageLoopForIO should not have extra member variables");
430*635a8641SAndroid Build Coastguard Worker 
431*635a8641SAndroid Build Coastguard Worker }  // namespace base
432*635a8641SAndroid Build Coastguard Worker 
433*635a8641SAndroid Build Coastguard Worker #endif  // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_
434