xref: /aosp_15_r20/external/libchrome/base/run_loop.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 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_RUN_LOOP_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_RUN_LOOP_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <utility>
9*635a8641SAndroid Build Coastguard Worker #include <vector>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/callback.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/containers/stack.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
16*635a8641SAndroid Build Coastguard Worker #include "base/memory/weak_ptr.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/observer_list.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/sequence_checker.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_checker.h"
20*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker namespace base {
23*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
24*635a8641SAndroid Build Coastguard Worker class MessagePumpForUI;
25*635a8641SAndroid Build Coastguard Worker #endif
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
28*635a8641SAndroid Build Coastguard Worker class MessagePumpUIApplication;
29*635a8641SAndroid Build Coastguard Worker #endif
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker class SingleThreadTaskRunner;
32*635a8641SAndroid Build Coastguard Worker 
33*635a8641SAndroid Build Coastguard Worker // Helper class to run the RunLoop::Delegate associated with the current thread.
34*635a8641SAndroid Build Coastguard Worker // A RunLoop::Delegate must have been bound to this thread (ref.
35*635a8641SAndroid Build Coastguard Worker // RunLoop::RegisterDelegateForCurrentThread()) prior to using any of RunLoop's
36*635a8641SAndroid Build Coastguard Worker // member and static methods unless explicitly indicated otherwise (e.g.
37*635a8641SAndroid Build Coastguard Worker // IsRunning/IsNestedOnCurrentThread()). RunLoop::Run can only be called once
38*635a8641SAndroid Build Coastguard Worker // per RunLoop lifetime. Create a RunLoop on the stack and call Run/Quit to run
39*635a8641SAndroid Build Coastguard Worker // a nested RunLoop but please do not use nested loops in production code!
40*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT RunLoop {
41*635a8641SAndroid Build Coastguard Worker  public:
42*635a8641SAndroid Build Coastguard Worker   // The type of RunLoop: a kDefault RunLoop at the top-level (non-nested) will
43*635a8641SAndroid Build Coastguard Worker   // process system and application tasks assigned to its Delegate. When nested
44*635a8641SAndroid Build Coastguard Worker   // however a kDefault RunLoop will only process system tasks while a
45*635a8641SAndroid Build Coastguard Worker   // kNestableTasksAllowed RunLoop will continue to process application tasks
46*635a8641SAndroid Build Coastguard Worker   // even if nested.
47*635a8641SAndroid Build Coastguard Worker   //
48*635a8641SAndroid Build Coastguard Worker   // This is relevant in the case of recursive RunLoops. Some unwanted run loops
49*635a8641SAndroid Build Coastguard Worker   // may occur when using common controls or printer functions. By default,
50*635a8641SAndroid Build Coastguard Worker   // recursive task processing is disabled.
51*635a8641SAndroid Build Coastguard Worker   //
52*635a8641SAndroid Build Coastguard Worker   // In general, nestable RunLoops are to be avoided. They are dangerous and
53*635a8641SAndroid Build Coastguard Worker   // difficult to get right, so please use with extreme caution.
54*635a8641SAndroid Build Coastguard Worker   //
55*635a8641SAndroid Build Coastguard Worker   // A specific example where this makes a difference is:
56*635a8641SAndroid Build Coastguard Worker   // - The thread is running a RunLoop.
57*635a8641SAndroid Build Coastguard Worker   // - It receives a task #1 and executes it.
58*635a8641SAndroid Build Coastguard Worker   // - The task #1 implicitly starts a RunLoop, like a MessageBox in the unit
59*635a8641SAndroid Build Coastguard Worker   //   test. This can also be StartDoc or GetSaveFileName.
60*635a8641SAndroid Build Coastguard Worker   // - The thread receives a task #2 before or while in this second RunLoop.
61*635a8641SAndroid Build Coastguard Worker   // - With a kNestableTasksAllowed RunLoop, the task #2 will run right away.
62*635a8641SAndroid Build Coastguard Worker   //   Otherwise, it will get executed right after task #1 completes in the main
63*635a8641SAndroid Build Coastguard Worker   //   RunLoop.
64*635a8641SAndroid Build Coastguard Worker   enum class Type {
65*635a8641SAndroid Build Coastguard Worker     kDefault,
66*635a8641SAndroid Build Coastguard Worker     kNestableTasksAllowed,
67*635a8641SAndroid Build Coastguard Worker   };
68*635a8641SAndroid Build Coastguard Worker 
69*635a8641SAndroid Build Coastguard Worker   RunLoop(Type type = Type::kDefault);
70*635a8641SAndroid Build Coastguard Worker   ~RunLoop();
71*635a8641SAndroid Build Coastguard Worker 
72*635a8641SAndroid Build Coastguard Worker   // Run the current RunLoop::Delegate. This blocks until Quit is called. Before
73*635a8641SAndroid Build Coastguard Worker   // calling Run, be sure to grab the QuitClosure in order to stop the
74*635a8641SAndroid Build Coastguard Worker   // RunLoop::Delegate asynchronously.
75*635a8641SAndroid Build Coastguard Worker   void Run();
76*635a8641SAndroid Build Coastguard Worker 
77*635a8641SAndroid Build Coastguard Worker   // Run the current RunLoop::Delegate until it doesn't find any tasks or
78*635a8641SAndroid Build Coastguard Worker   // messages in its queue (it goes idle). WARNING: This may never return! Only
79*635a8641SAndroid Build Coastguard Worker   // use this when repeating tasks such as animated web pages have been shut
80*635a8641SAndroid Build Coastguard Worker   // down.
81*635a8641SAndroid Build Coastguard Worker   void RunUntilIdle();
82*635a8641SAndroid Build Coastguard Worker 
running()83*635a8641SAndroid Build Coastguard Worker   bool running() const {
84*635a8641SAndroid Build Coastguard Worker     DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
85*635a8641SAndroid Build Coastguard Worker     return running_;
86*635a8641SAndroid Build Coastguard Worker   }
87*635a8641SAndroid Build Coastguard Worker 
88*635a8641SAndroid Build Coastguard Worker   // Quit() quits an earlier call to Run() immediately. QuitWhenIdle() quits an
89*635a8641SAndroid Build Coastguard Worker   // earlier call to Run() when there aren't any tasks or messages in the queue.
90*635a8641SAndroid Build Coastguard Worker   //
91*635a8641SAndroid Build Coastguard Worker   // These methods are thread-safe but note that Quit() is best-effort when
92*635a8641SAndroid Build Coastguard Worker   // called from another thread (will quit soon but tasks that were already
93*635a8641SAndroid Build Coastguard Worker   // queued on this RunLoop will get to run first).
94*635a8641SAndroid Build Coastguard Worker   //
95*635a8641SAndroid Build Coastguard Worker   // There can be other nested RunLoops servicing the same task queue. Quitting
96*635a8641SAndroid Build Coastguard Worker   // one RunLoop has no bearing on the others. Quit() and QuitWhenIdle() can be
97*635a8641SAndroid Build Coastguard Worker   // called before, during or after Run(). If called before Run(), Run() will
98*635a8641SAndroid Build Coastguard Worker   // return immediately when called. Calling Quit() or QuitWhenIdle() after the
99*635a8641SAndroid Build Coastguard Worker   // RunLoop has already finished running has no effect.
100*635a8641SAndroid Build Coastguard Worker   //
101*635a8641SAndroid Build Coastguard Worker   // WARNING: You must NEVER assume that a call to Quit() or QuitWhenIdle() will
102*635a8641SAndroid Build Coastguard Worker   // terminate the targetted message loop. If a nested RunLoop continues
103*635a8641SAndroid Build Coastguard Worker   // running, the target may NEVER terminate. It is very easy to livelock (run
104*635a8641SAndroid Build Coastguard Worker   // forever) in such a case.
105*635a8641SAndroid Build Coastguard Worker   void Quit();
106*635a8641SAndroid Build Coastguard Worker   void QuitWhenIdle();
107*635a8641SAndroid Build Coastguard Worker 
108*635a8641SAndroid Build Coastguard Worker   // Convenience methods to get a closure that safely calls Quit() or
109*635a8641SAndroid Build Coastguard Worker   // QuitWhenIdle() (has no effect if the RunLoop instance is gone).
110*635a8641SAndroid Build Coastguard Worker   //
111*635a8641SAndroid Build Coastguard Worker   // The resulting Closure is thread-safe (note however that invoking the
112*635a8641SAndroid Build Coastguard Worker   // QuitClosure() from another thread than this RunLoop's will result in an
113*635a8641SAndroid Build Coastguard Worker   // asynchronous rather than immediate Quit()).
114*635a8641SAndroid Build Coastguard Worker   //
115*635a8641SAndroid Build Coastguard Worker   // Example:
116*635a8641SAndroid Build Coastguard Worker   //   RunLoop run_loop;
117*635a8641SAndroid Build Coastguard Worker   //   PostTask(run_loop.QuitClosure());
118*635a8641SAndroid Build Coastguard Worker   //   run_loop.Run();
119*635a8641SAndroid Build Coastguard Worker   base::Closure QuitClosure();
120*635a8641SAndroid Build Coastguard Worker   base::Closure QuitWhenIdleClosure();
121*635a8641SAndroid Build Coastguard Worker 
122*635a8641SAndroid Build Coastguard Worker   // Returns true if there is an active RunLoop on this thread.
123*635a8641SAndroid Build Coastguard Worker   // Safe to call before RegisterDelegateForCurrentThread().
124*635a8641SAndroid Build Coastguard Worker   static bool IsRunningOnCurrentThread();
125*635a8641SAndroid Build Coastguard Worker 
126*635a8641SAndroid Build Coastguard Worker   // Returns true if there is an active RunLoop on this thread and it's nested
127*635a8641SAndroid Build Coastguard Worker   // within another active RunLoop.
128*635a8641SAndroid Build Coastguard Worker   // Safe to call before RegisterDelegateForCurrentThread().
129*635a8641SAndroid Build Coastguard Worker   static bool IsNestedOnCurrentThread();
130*635a8641SAndroid Build Coastguard Worker 
131*635a8641SAndroid Build Coastguard Worker   // A NestingObserver is notified when a nested RunLoop begins and ends.
132*635a8641SAndroid Build Coastguard Worker   class BASE_EXPORT NestingObserver {
133*635a8641SAndroid Build Coastguard Worker    public:
134*635a8641SAndroid Build Coastguard Worker     // Notified before a nested loop starts running work on the current thread.
135*635a8641SAndroid Build Coastguard Worker     virtual void OnBeginNestedRunLoop() = 0;
136*635a8641SAndroid Build Coastguard Worker     // Notified after a nested loop is done running work on the current thread.
OnExitNestedRunLoop()137*635a8641SAndroid Build Coastguard Worker     virtual void OnExitNestedRunLoop() {}
138*635a8641SAndroid Build Coastguard Worker 
139*635a8641SAndroid Build Coastguard Worker    protected:
140*635a8641SAndroid Build Coastguard Worker     virtual ~NestingObserver() = default;
141*635a8641SAndroid Build Coastguard Worker   };
142*635a8641SAndroid Build Coastguard Worker 
143*635a8641SAndroid Build Coastguard Worker   static void AddNestingObserverOnCurrentThread(NestingObserver* observer);
144*635a8641SAndroid Build Coastguard Worker   static void RemoveNestingObserverOnCurrentThread(NestingObserver* observer);
145*635a8641SAndroid Build Coastguard Worker 
146*635a8641SAndroid Build Coastguard Worker   // A RunLoop::Delegate is a generic interface that allows RunLoop to be
147*635a8641SAndroid Build Coastguard Worker   // separate from the underlying implementation of the message loop for this
148*635a8641SAndroid Build Coastguard Worker   // thread. It holds private state used by RunLoops on its associated thread.
149*635a8641SAndroid Build Coastguard Worker   // One and only one RunLoop::Delegate must be registered on a given thread
150*635a8641SAndroid Build Coastguard Worker   // via RunLoop::RegisterDelegateForCurrentThread() before RunLoop instances
151*635a8641SAndroid Build Coastguard Worker   // and RunLoop static methods can be used on it.
152*635a8641SAndroid Build Coastguard Worker   class BASE_EXPORT Delegate {
153*635a8641SAndroid Build Coastguard Worker    public:
154*635a8641SAndroid Build Coastguard Worker     Delegate();
155*635a8641SAndroid Build Coastguard Worker     virtual ~Delegate();
156*635a8641SAndroid Build Coastguard Worker 
157*635a8641SAndroid Build Coastguard Worker     // Used by RunLoop to inform its Delegate to Run/Quit. Implementations are
158*635a8641SAndroid Build Coastguard Worker     // expected to keep on running synchronously from the Run() call until the
159*635a8641SAndroid Build Coastguard Worker     // eventual matching Quit() call. Upon receiving a Quit() call it should
160*635a8641SAndroid Build Coastguard Worker     // return from the Run() call as soon as possible without executing
161*635a8641SAndroid Build Coastguard Worker     // remaining tasks/messages. Run() calls can nest in which case each Quit()
162*635a8641SAndroid Build Coastguard Worker     // call should result in the topmost active Run() call returning. The only
163*635a8641SAndroid Build Coastguard Worker     // other trigger for Run() to return is the
164*635a8641SAndroid Build Coastguard Worker     // |should_quit_when_idle_callback_| which the Delegate should probe before
165*635a8641SAndroid Build Coastguard Worker     // sleeping when it becomes idle. |application_tasks_allowed| is true if
166*635a8641SAndroid Build Coastguard Worker     // this is the first Run() call on the stack or it was made from a nested
167*635a8641SAndroid Build Coastguard Worker     // RunLoop of Type::kNestableTasksAllowed (otherwise this Run() level should
168*635a8641SAndroid Build Coastguard Worker     // only process system tasks).
169*635a8641SAndroid Build Coastguard Worker     virtual void Run(bool application_tasks_allowed) = 0;
170*635a8641SAndroid Build Coastguard Worker     virtual void Quit() = 0;
171*635a8641SAndroid Build Coastguard Worker 
172*635a8641SAndroid Build Coastguard Worker     // Invoked right before a RunLoop enters a nested Run() call on this
173*635a8641SAndroid Build Coastguard Worker     // Delegate iff this RunLoop is of type kNestableTasksAllowed. The Delegate
174*635a8641SAndroid Build Coastguard Worker     // should ensure that the upcoming Run() call will result in processing
175*635a8641SAndroid Build Coastguard Worker     // application tasks queued ahead of it without further probing. e.g.
176*635a8641SAndroid Build Coastguard Worker     // message pumps on some platforms, like Mac, need an explicit request to
177*635a8641SAndroid Build Coastguard Worker     // process application tasks when nested, otherwise they'll only wait for
178*635a8641SAndroid Build Coastguard Worker     // system messages.
179*635a8641SAndroid Build Coastguard Worker     virtual void EnsureWorkScheduled() = 0;
180*635a8641SAndroid Build Coastguard Worker 
181*635a8641SAndroid Build Coastguard Worker    protected:
182*635a8641SAndroid Build Coastguard Worker     // Returns the result of this Delegate's |should_quit_when_idle_callback_|.
183*635a8641SAndroid Build Coastguard Worker     // "protected" so it can be invoked only by the Delegate itself.
184*635a8641SAndroid Build Coastguard Worker     bool ShouldQuitWhenIdle();
185*635a8641SAndroid Build Coastguard Worker 
186*635a8641SAndroid Build Coastguard Worker    private:
187*635a8641SAndroid Build Coastguard Worker     // While the state is owned by the Delegate subclass, only RunLoop can use
188*635a8641SAndroid Build Coastguard Worker     // it.
189*635a8641SAndroid Build Coastguard Worker     friend class RunLoop;
190*635a8641SAndroid Build Coastguard Worker 
191*635a8641SAndroid Build Coastguard Worker     // A vector-based stack is more memory efficient than the default
192*635a8641SAndroid Build Coastguard Worker     // deque-based stack as the active RunLoop stack isn't expected to ever
193*635a8641SAndroid Build Coastguard Worker     // have more than a few entries.
194*635a8641SAndroid Build Coastguard Worker     using RunLoopStack = base::stack<RunLoop*, std::vector<RunLoop*>>;
195*635a8641SAndroid Build Coastguard Worker 
196*635a8641SAndroid Build Coastguard Worker     RunLoopStack active_run_loops_;
197*635a8641SAndroid Build Coastguard Worker     ObserverList<RunLoop::NestingObserver> nesting_observers_;
198*635a8641SAndroid Build Coastguard Worker 
199*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
200*635a8641SAndroid Build Coastguard Worker     bool allow_running_for_testing_ = true;
201*635a8641SAndroid Build Coastguard Worker #endif
202*635a8641SAndroid Build Coastguard Worker 
203*635a8641SAndroid Build Coastguard Worker     // True once this Delegate is bound to a thread via
204*635a8641SAndroid Build Coastguard Worker     // RegisterDelegateForCurrentThread().
205*635a8641SAndroid Build Coastguard Worker     bool bound_ = false;
206*635a8641SAndroid Build Coastguard Worker 
207*635a8641SAndroid Build Coastguard Worker     // Thread-affine per its use of TLS.
208*635a8641SAndroid Build Coastguard Worker     THREAD_CHECKER(bound_thread_checker_);
209*635a8641SAndroid Build Coastguard Worker 
210*635a8641SAndroid Build Coastguard Worker     DISALLOW_COPY_AND_ASSIGN(Delegate);
211*635a8641SAndroid Build Coastguard Worker   };
212*635a8641SAndroid Build Coastguard Worker 
213*635a8641SAndroid Build Coastguard Worker   // Registers |delegate| on the current thread. Must be called once and only
214*635a8641SAndroid Build Coastguard Worker   // once per thread before using RunLoop methods on it. |delegate| is from then
215*635a8641SAndroid Build Coastguard Worker   // on forever bound to that thread (including its destruction).
216*635a8641SAndroid Build Coastguard Worker   static void RegisterDelegateForCurrentThread(Delegate* delegate);
217*635a8641SAndroid Build Coastguard Worker 
218*635a8641SAndroid Build Coastguard Worker   // Quits the active RunLoop (when idle) -- there must be one. These were
219*635a8641SAndroid Build Coastguard Worker   // introduced as prefered temporary replacements to the long deprecated
220*635a8641SAndroid Build Coastguard Worker   // MessageLoop::Quit(WhenIdle)(Closure) methods. Callers should properly plumb
221*635a8641SAndroid Build Coastguard Worker   // a reference to the appropriate RunLoop instance (or its QuitClosure)
222*635a8641SAndroid Build Coastguard Worker   // instead of using these in order to link Run()/Quit() to a single RunLoop
223*635a8641SAndroid Build Coastguard Worker   // instance and increase readability.
224*635a8641SAndroid Build Coastguard Worker   static void QuitCurrentDeprecated();
225*635a8641SAndroid Build Coastguard Worker   static void QuitCurrentWhenIdleDeprecated();
226*635a8641SAndroid Build Coastguard Worker   static Closure QuitCurrentWhenIdleClosureDeprecated();
227*635a8641SAndroid Build Coastguard Worker 
228*635a8641SAndroid Build Coastguard Worker   // Run() will DCHECK if called while there's a ScopedDisallowRunningForTesting
229*635a8641SAndroid Build Coastguard Worker   // in scope on its thread. This is useful to add safety to some test
230*635a8641SAndroid Build Coastguard Worker   // constructs which allow multiple task runners to share the main thread in
231*635a8641SAndroid Build Coastguard Worker   // unit tests. While the main thread can be shared by multiple runners to
232*635a8641SAndroid Build Coastguard Worker   // deterministically fake multi threading, there can still only be a single
233*635a8641SAndroid Build Coastguard Worker   // RunLoop::Delegate per thread and RunLoop::Run() should only be invoked from
234*635a8641SAndroid Build Coastguard Worker   // it (or it would result in incorrectly driving TaskRunner A while in
235*635a8641SAndroid Build Coastguard Worker   // TaskRunner B's context).
236*635a8641SAndroid Build Coastguard Worker   class BASE_EXPORT ScopedDisallowRunningForTesting {
237*635a8641SAndroid Build Coastguard Worker    public:
238*635a8641SAndroid Build Coastguard Worker     ScopedDisallowRunningForTesting();
239*635a8641SAndroid Build Coastguard Worker     ~ScopedDisallowRunningForTesting();
240*635a8641SAndroid Build Coastguard Worker 
241*635a8641SAndroid Build Coastguard Worker    private:
242*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
243*635a8641SAndroid Build Coastguard Worker     Delegate* current_delegate_;
244*635a8641SAndroid Build Coastguard Worker     const bool previous_run_allowance_;
245*635a8641SAndroid Build Coastguard Worker #endif  // DCHECK_IS_ON()
246*635a8641SAndroid Build Coastguard Worker 
247*635a8641SAndroid Build Coastguard Worker     DISALLOW_COPY_AND_ASSIGN(ScopedDisallowRunningForTesting);
248*635a8641SAndroid Build Coastguard Worker   };
249*635a8641SAndroid Build Coastguard Worker 
250*635a8641SAndroid Build Coastguard Worker  private:
251*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(MessageLoopTypedTest, RunLoopQuitOrderAfter);
252*635a8641SAndroid Build Coastguard Worker 
253*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID) && 0
254*635a8641SAndroid Build Coastguard Worker   // Android doesn't support the blocking RunLoop::Run, so it calls
255*635a8641SAndroid Build Coastguard Worker   // BeforeRun and AfterRun directly.
256*635a8641SAndroid Build Coastguard Worker   friend class base::MessagePumpForUI;
257*635a8641SAndroid Build Coastguard Worker #endif
258*635a8641SAndroid Build Coastguard Worker 
259*635a8641SAndroid Build Coastguard Worker #if defined(OS_IOS)
260*635a8641SAndroid Build Coastguard Worker   // iOS doesn't support the blocking RunLoop::Run, so it calls
261*635a8641SAndroid Build Coastguard Worker   // BeforeRun directly.
262*635a8641SAndroid Build Coastguard Worker   friend class base::MessagePumpUIApplication;
263*635a8641SAndroid Build Coastguard Worker #endif
264*635a8641SAndroid Build Coastguard Worker 
265*635a8641SAndroid Build Coastguard Worker   // Return false to abort the Run.
266*635a8641SAndroid Build Coastguard Worker   bool BeforeRun();
267*635a8641SAndroid Build Coastguard Worker   void AfterRun();
268*635a8641SAndroid Build Coastguard Worker 
269*635a8641SAndroid Build Coastguard Worker   // A copy of RunLoop::Delegate for the thread driven by tis RunLoop for quick
270*635a8641SAndroid Build Coastguard Worker   // access without using TLS (also allows access to state from another sequence
271*635a8641SAndroid Build Coastguard Worker   // during Run(), ref. |sequence_checker_| below).
272*635a8641SAndroid Build Coastguard Worker   Delegate* delegate_;
273*635a8641SAndroid Build Coastguard Worker 
274*635a8641SAndroid Build Coastguard Worker   const Type type_;
275*635a8641SAndroid Build Coastguard Worker 
276*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
277*635a8641SAndroid Build Coastguard Worker   bool run_called_ = false;
278*635a8641SAndroid Build Coastguard Worker #endif
279*635a8641SAndroid Build Coastguard Worker 
280*635a8641SAndroid Build Coastguard Worker   bool quit_called_ = false;
281*635a8641SAndroid Build Coastguard Worker   bool running_ = false;
282*635a8641SAndroid Build Coastguard Worker   // Used to record that QuitWhenIdle() was called on this RunLoop, meaning that
283*635a8641SAndroid Build Coastguard Worker   // the Delegate should quit Run() once it becomes idle (it's responsible for
284*635a8641SAndroid Build Coastguard Worker   // probing this state via ShouldQuitWhenIdle()). This state is stored here
285*635a8641SAndroid Build Coastguard Worker   // rather than pushed to Delegate to support nested RunLoops.
286*635a8641SAndroid Build Coastguard Worker   bool quit_when_idle_received_ = false;
287*635a8641SAndroid Build Coastguard Worker 
288*635a8641SAndroid Build Coastguard Worker   // True if use of QuitCurrent*Deprecated() is allowed. Taking a Quit*Closure()
289*635a8641SAndroid Build Coastguard Worker   // from a RunLoop implicitly sets this to false, so QuitCurrent*Deprecated()
290*635a8641SAndroid Build Coastguard Worker   // cannot be used while that RunLoop is being Run().
291*635a8641SAndroid Build Coastguard Worker   bool allow_quit_current_deprecated_ = true;
292*635a8641SAndroid Build Coastguard Worker 
293*635a8641SAndroid Build Coastguard Worker   // RunLoop is not thread-safe. Its state/methods, unless marked as such, may
294*635a8641SAndroid Build Coastguard Worker   // not be accessed from any other sequence than the thread it was constructed
295*635a8641SAndroid Build Coastguard Worker   // on. Exception: RunLoop can be safely accessed from one other sequence (or
296*635a8641SAndroid Build Coastguard Worker   // single parallel task) during Run() -- e.g. to Quit() without having to
297*635a8641SAndroid Build Coastguard Worker   // plumb ThreatTaskRunnerHandle::Get() throughout a test to repost QuitClosure
298*635a8641SAndroid Build Coastguard Worker   // to origin thread.
299*635a8641SAndroid Build Coastguard Worker   SEQUENCE_CHECKER(sequence_checker_);
300*635a8641SAndroid Build Coastguard Worker 
301*635a8641SAndroid Build Coastguard Worker   const scoped_refptr<SingleThreadTaskRunner> origin_task_runner_;
302*635a8641SAndroid Build Coastguard Worker 
303*635a8641SAndroid Build Coastguard Worker   // WeakPtrFactory for QuitClosure safety.
304*635a8641SAndroid Build Coastguard Worker   base::WeakPtrFactory<RunLoop> weak_factory_;
305*635a8641SAndroid Build Coastguard Worker 
306*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(RunLoop);
307*635a8641SAndroid Build Coastguard Worker };
308*635a8641SAndroid Build Coastguard Worker 
309*635a8641SAndroid Build Coastguard Worker }  // namespace base
310*635a8641SAndroid Build Coastguard Worker 
311*635a8641SAndroid Build Coastguard Worker #endif  // BASE_RUN_LOOP_H_
312