blob: 380c8bf7401bcddc7141a9333ff7b616941fbd34 [file] [log] [blame]
jbates@chromium.org5d5b2812012-06-29 07:57:30 +09001// 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
3// found in the LICENSE file.
4
5#ifndef BASE_RUN_LOOP_H_
6#define BASE_RUN_LOOP_H_
jbates@chromium.org5d5b2812012-06-29 07:57:30 +09007
8#include "base/base_export.h"
9#include "base/callback.h"
10#include "base/memory/weak_ptr.h"
11#include "base/message_loop.h"
12
13namespace base {
14#if defined(OS_ANDROID)
15class MessagePumpForUI;
16#endif
17
rohitrao@chromium.orgdcc49022012-07-13 20:02:57 +090018#if defined(OS_IOS)
19class MessagePumpUIApplication;
20#endif
21
jbates@chromium.org5d5b2812012-06-29 07:57:30 +090022// Helper class to Run a nested MessageLoop. Please do not use nested
23// MessageLoops in production code! If you must, use this class instead of
24// calling MessageLoop::Run/Quit directly. RunLoop::Run can only be called once
25// per RunLoop lifetime. Create a RunLoop on the stack and call Run/Quit to run
26// a nested MessageLoop.
27class BASE_EXPORT RunLoop {
28 public:
29 RunLoop();
30#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
31 explicit RunLoop(MessageLoop::Dispatcher* dispatcher);
32#endif
33 ~RunLoop();
34
35#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
36 void set_dispatcher(MessageLoop::Dispatcher* dispatcher) {
37 dispatcher_ = dispatcher;
38 }
39#endif
40
41 // Run the current MessageLoop. This blocks until Quit is called. Before
42 // calling Run, be sure to grab an AsWeakPtr or the QuitClosure in order to
43 // stop the MessageLoop asynchronously. MessageLoop::Quit and QuitNow will
44 // also trigger a return from Run, but those are deprecated.
45 void Run();
46
47 // Run the current MessageLoop until it doesn't find any tasks or messages in
48 // the queue (it goes idle). WARNING: This may never return! Only use this
49 // when repeating tasks such as animated web pages have been shut down.
50 void RunUntilIdle();
51
52 bool running() const { return running_; }
53
54 // Quit an earlier call to Run(). There can be other nested RunLoops servicing
55 // the same task queue (MessageLoop); Quitting one RunLoop has no bearing on
56 // the others. Quit can be called before, during or after Run. If called
57 // before Run, Run will return immediately when called. Calling Quit after the
58 // RunLoop has already finished running has no effect.
59 //
60 // WARNING: You must NEVER assume that a call to Quit will terminate the
61 // targetted message loop. If a nested message loop continues running, the
62 // target may NEVER terminate. It is very easy to livelock (run forever) in
63 // such a case.
64 void Quit();
65
66 // Convenience method to get a closure that safely calls Quit (has no effect
67 // if the RunLoop instance is gone).
68 //
69 // Example:
70 // RunLoop run_loop;
71 // PostTask(run_loop.QuitClosure());
72 // run_loop.Run();
73 base::Closure QuitClosure();
74
75 private:
xhwang@chromium.orgff47fa52013-05-04 22:57:01 +090076 friend class MessageLoop;
jbates@chromium.org5d5b2812012-06-29 07:57:30 +090077#if defined(OS_ANDROID)
78 // Android doesn't support the blocking MessageLoop::Run, so it calls
79 // BeforeRun and AfterRun directly.
80 friend class base::MessagePumpForUI;
81#endif
82
rohitrao@chromium.orgdcc49022012-07-13 20:02:57 +090083#if defined(OS_IOS)
84 // iOS doesn't support the blocking MessageLoop::Run, so it calls
85 // BeforeRun directly.
86 friend class base::MessagePumpUIApplication;
87#endif
88
jbates@chromium.org5d5b2812012-06-29 07:57:30 +090089 // Return false to abort the Run.
90 bool BeforeRun();
91 void AfterRun();
92
93 MessageLoop* loop_;
94
95 // WeakPtrFactory for QuitClosure safety.
96 base::WeakPtrFactory<RunLoop> weak_factory_;
97
98 // Parent RunLoop or NULL if this is the top-most RunLoop.
99 RunLoop* previous_run_loop_;
100
101#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
102 MessageLoop::Dispatcher* dispatcher_;
103#endif
104
105 // Used to count how many nested Run() invocations are on the stack.
106 int run_depth_;
107
108 bool run_called_;
109 bool quit_called_;
110 bool running_;
111
112 // Used to record that QuitWhenIdle() was called on the MessageLoop, meaning
113 // that we should quit Run once it becomes idle.
114 bool quit_when_idle_received_;
115
116 DISALLOW_COPY_AND_ASSIGN(RunLoop);
117};
118
119} // namespace base
120
121#endif // BASE_RUN_LOOP_H_