Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: base/run_loop.h

Issue 10479018: Add base::RunLoop and update ui_test_utils to use it to reduce flakiness (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: darin feedback Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // 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_
7 #pragma once
8
9 #include "base/base_export.h"
10 #include "base/callback.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop.h"
13
14 namespace base {
15 #if defined(OS_ANDROID)
16 class MessagePumpForUI;
17 #endif
18
19 // Helper class to Run a nested MessageLoop. Please do not use nested
20 // MessageLoops in production code! If you must, use this class instead of
21 // calling MessageLoop::Run/Quit directly. RunLoop::Run can only be called once
22 // per RunLoop lifetime. Create a RunLoop on the stack and call Run/Quit to run
23 // a nested MessageLoop.
24 class BASE_EXPORT RunLoop : public base::SupportsWeakPtr<RunLoop> {
25 public:
26 RunLoop();
27 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
28 explicit RunLoop(MessageLoop::Dispatcher* dispatcher);
29 #endif
30 ~RunLoop();
31
32 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
33 void set_dispatcher(MessageLoop::Dispatcher* dispatcher) {
34 dispatcher_ = dispatcher;
35 }
36 #endif
37
38 // Run the current MessageLoop. This blocks until Quit is called. Before
39 // calling Run, be sure to grab an AsWeakPtr or the QuitClosure in order to
40 // stop the MessageLoop asynchronously. MessageLoop::Quit and QuitNow will
41 // also trigger a return from Run, but those are deprecated.
42 void Run();
43
44 // Run the current MessageLoop until it doesn't find any tasks or messages in
45 // the queue (it goes idle). WARNING: This may never return! Only use this
46 // when repeating tasks such as animated web pages have been shut down.
47 void RunUntilIdle();
48
49 // Quit an earlier call to this->Run(). There can be other nested RunLoops
50 // servicing the same task queue (MessageLoop); Quitting one RunLoop has no
51 // bearing on the others. Quit can be called before, during or after Run. If
52 // called before Run, Run will return immediately when called.
53 //
54 // WARNING: You must NEVER assume that a call to Quit will terminate the
55 // targetted message loop. If a nested message loop continues running, the
56 // target may NEVER terminate. It is very easy to livelock (run forever) in
57 // such a case.
58 void Quit();
59
60 // Hand this closure off to code that asynchronously notifies of completion.
61 // The returned Closure is safe to run after the RunLoop has been destructed.
62 // Example:
63 // RunLoop run_loop;
64 // kick_off_some_procedure(run_loop.QuitClosure());
65 // run_loop.Run();
66 base::Closure QuitClosure();
67
68 private:
69 friend class ::MessageLoop;
70 #if defined(OS_ANDROID)
71 // Android doesn't support the blocking MessageLoop::Run, so it calls
72 // BeforeRun and AfterRun directly.
73 friend class base::MessagePumpForUI;
74 #endif
75
76 // Return false to abort the Run.
77 bool BeforeRun();
78 void AfterRun();
79
80 MessageLoop* loop_;
81
82 base::WeakPtr<RunLoop> previous_run_loop_;
83
84 bool run_called_;
85 bool quit_called_;
86 bool running_;
87
88 // Used to count how many nested Run() invocations are on the stack.
89 int run_depth_;
90
91 // Used to record that Quit() was called on the MessageLoop, meaning that we
92 // should quit Run once it becomes idle.
93 bool quit_when_idle_received_;
94
95 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
96 MessageLoop::Dispatcher* dispatcher_;
97 #endif
98
99 DISALLOW_COPY_AND_ASSIGN(RunLoop);
100 };
101
102 } // namespace base
103
104 #endif // BASE_RUN_LOOP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698