123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- #ifndef BASE_RUN_LOOP_H_
- #define BASE_RUN_LOOP_H_
- #include <stack>
- #include <utility>
- #include <vector>
- #include "base/base_export.h"
- #include "base/callback.h"
- #include "base/containers/stack.h"
- #include "base/gtest_prod_util.h"
- #include "base/memory/ref_counted.h"
- #include "base/memory/weak_ptr.h"
- #include "base/observer_list.h"
- #include "base/sequence_checker.h"
- #include "base/threading/thread_checker.h"
- #include "base/time/time.h"
- #include "build/build_config.h"
- namespace base {
- namespace test {
- class ScopedRunLoopTimeout;
- class ScopedDisableRunLoopTimeout;
- }
- #if defined(OS_ANDROID)
- class MessagePumpForUI;
- #endif
- #if defined(OS_IOS)
- class MessagePumpUIApplication;
- #endif
- class SingleThreadTaskRunner;
- class BASE_EXPORT RunLoop {
- public:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- enum class Type {
- kDefault,
- kNestableTasksAllowed,
- };
- explicit RunLoop(Type type = Type::kDefault);
- RunLoop(const RunLoop&) = delete;
- RunLoop& operator=(const RunLoop&) = delete;
- ~RunLoop();
-
-
- void Run();
-
-
-
-
-
-
-
-
-
-
- void RunUntilIdle();
- bool running() const {
- DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
- return running_;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void Quit();
- void QuitWhenIdle();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- RepeatingClosure QuitClosure();
- RepeatingClosure QuitWhenIdleClosure();
-
-
- static bool IsRunningOnCurrentThread();
-
-
-
- static bool IsNestedOnCurrentThread();
-
- class BASE_EXPORT NestingObserver {
- public:
-
- virtual void OnBeginNestedRunLoop() = 0;
-
- virtual void OnExitNestedRunLoop() {}
- protected:
- virtual ~NestingObserver() = default;
- };
- static void AddNestingObserverOnCurrentThread(NestingObserver* observer);
- static void RemoveNestingObserverOnCurrentThread(NestingObserver* observer);
-
-
-
-
-
-
- class BASE_EXPORT Delegate {
- public:
- Delegate();
- Delegate(const Delegate&) = delete;
- Delegate& operator=(const Delegate&) = delete;
- virtual ~Delegate();
-
-
-
-
-
-
-
-
-
-
-
-
-
- virtual void Run(bool application_tasks_allowed, TimeDelta timeout) = 0;
- virtual void Quit() = 0;
-
-
-
-
-
-
-
- virtual void EnsureWorkScheduled() = 0;
- protected:
-
-
- bool ShouldQuitWhenIdle();
- private:
-
-
- friend class RunLoop;
-
-
-
- using RunLoopStack = stack<RunLoop*, std::vector<RunLoop*>>;
- RunLoopStack active_run_loops_;
- ObserverList<RunLoop::NestingObserver>::Unchecked nesting_observers_;
- #if DCHECK_IS_ON()
- bool allow_running_for_testing_ = true;
- #endif
-
-
- bool bound_ = false;
-
- THREAD_CHECKER(bound_thread_checker_);
- };
-
-
-
- static void RegisterDelegateForCurrentThread(Delegate* delegate);
-
-
-
-
-
-
- static void QuitCurrentDeprecated();
- static void QuitCurrentWhenIdleDeprecated();
- static RepeatingClosure QuitCurrentWhenIdleClosureDeprecated();
-
-
-
-
-
-
-
-
- class BASE_EXPORT ScopedDisallowRunningForTesting {
- public:
- ScopedDisallowRunningForTesting();
- ScopedDisallowRunningForTesting(const ScopedDisallowRunningForTesting&) =
- delete;
- ScopedDisallowRunningForTesting& operator=(
- const ScopedDisallowRunningForTesting&) = delete;
- ~ScopedDisallowRunningForTesting();
- private:
- #if DCHECK_IS_ON()
- Delegate* current_delegate_;
- const bool previous_run_allowance_;
- #endif
- };
-
-
- struct BASE_EXPORT RunLoopTimeout {
- RunLoopTimeout();
- ~RunLoopTimeout();
- TimeDelta timeout;
- RepeatingClosure on_timeout;
- };
- private:
- FRIEND_TEST_ALL_PREFIXES(SingleThreadTaskExecutorTypedTest,
- RunLoopQuitOrderAfter);
- #if defined(OS_ANDROID)
-
-
- friend class MessagePumpForUI;
- #endif
- #if defined(OS_IOS)
-
-
- friend class MessagePumpUIApplication;
- #endif
-
- friend class test::ScopedRunLoopTimeout;
- friend class test::ScopedDisableRunLoopTimeout;
- static void SetTimeoutForCurrentThread(const RunLoopTimeout* timeout);
- static const RunLoopTimeout* GetTimeoutForCurrentThread();
-
- bool BeforeRun();
- void AfterRun();
-
-
-
- Delegate* const delegate_;
- const Type type_;
- #if DCHECK_IS_ON()
- bool run_called_ = false;
- #endif
- bool quit_called_ = false;
- bool running_ = false;
-
-
-
-
- bool quit_when_idle_received_ = false;
-
-
-
- bool allow_quit_current_deprecated_ = true;
-
-
-
-
-
-
- SEQUENCE_CHECKER(sequence_checker_);
- const scoped_refptr<SingleThreadTaskRunner> origin_task_runner_;
-
- WeakPtrFactory<RunLoop> weak_factory_{this};
- };
- }
- #endif
|