[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 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 | #include "base/run_loop.h" |
| 6 | |
| 7 | #include "base/bind.h" |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 8 | #include "base/callback.h" |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 9 | #include "base/lazy_instance.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame^] | 10 | #include "base/message_loop/message_loop.h" |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 11 | #include "base/single_thread_task_runner.h" |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 12 | #include "base/threading/thread_local.h" |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 13 | #include "base/threading/thread_task_runner_handle.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 18 | namespace { |
| 19 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 20 | LazyInstance<ThreadLocalPointer<RunLoop::Delegate>>::Leaky tls_delegate = |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 21 | LAZY_INSTANCE_INITIALIZER; |
| 22 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 23 | // Runs |closure| immediately if this is called on |task_runner|, otherwise |
| 24 | // forwards |closure| to it. |
| 25 | void ProxyToTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner, |
| 26 | OnceClosure closure) { |
| 27 | if (task_runner->RunsTasksInCurrentSequence()) { |
| 28 | std::move(closure).Run(); |
| 29 | return; |
| 30 | } |
| 31 | task_runner->PostTask(FROM_HERE, std::move(closure)); |
| 32 | } |
| 33 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 34 | } // namespace |
| 35 | |
Gabriel Charette | 50518fc | 2018-05-22 17:53:22 | [diff] [blame] | 36 | RunLoop::Delegate::Delegate() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 37 | // The Delegate can be created on another thread. It is only bound in |
| 38 | // RegisterDelegateForCurrentThread(). |
| 39 | DETACH_FROM_THREAD(bound_thread_checker_); |
| 40 | } |
| 41 | |
| 42 | RunLoop::Delegate::~Delegate() { |
| 43 | DCHECK_CALLED_ON_VALID_THREAD(bound_thread_checker_); |
| 44 | // A RunLoop::Delegate may be destroyed before it is bound, if so it may still |
| 45 | // be on its creation thread (e.g. a Thread that fails to start) and |
| 46 | // shouldn't disrupt that thread's state. |
| 47 | if (bound_) |
| 48 | tls_delegate.Get().Set(nullptr); |
| 49 | } |
| 50 | |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 51 | bool RunLoop::Delegate::ShouldQuitWhenIdle() { |
Gabriel Charette | 50518fc | 2018-05-22 17:53:22 | [diff] [blame] | 52 | return active_run_loops_.top()->quit_when_idle_received_; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 53 | } |
| 54 | |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 55 | // static |
Gabriel Charette | a3ec961 | 2017-12-14 17:22:40 | [diff] [blame] | 56 | void RunLoop::RegisterDelegateForCurrentThread(Delegate* delegate) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 57 | // Bind |delegate| to this thread. |
| 58 | DCHECK(!delegate->bound_); |
| 59 | DCHECK_CALLED_ON_VALID_THREAD(delegate->bound_thread_checker_); |
| 60 | |
| 61 | // There can only be one RunLoop::Delegate per thread. |
Gabriel Charette | fa5e7f0d | 2018-01-29 12:08:21 | [diff] [blame] | 62 | DCHECK(!tls_delegate.Get().Get()) |
| 63 | << "Error: Multiple RunLoop::Delegates registered on the same thread.\n\n" |
| 64 | "Hint: You perhaps instantiated a second " |
| 65 | "MessageLoop/ScopedTaskEnvironment on a thread that already had one?"; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 66 | tls_delegate.Get().Set(delegate); |
| 67 | delegate->bound_ = true; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 68 | } |
| 69 | |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 70 | RunLoop::RunLoop(Type type) |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 71 | : delegate_(tls_delegate.Get().Get()), |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 72 | type_(type), |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 73 | origin_task_runner_(ThreadTaskRunnerHandle::Get()), |
| 74 | weak_factory_(this) { |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 75 | DCHECK(delegate_) << "A RunLoop::Delegate must be bound to this thread prior " |
| 76 | "to using RunLoop."; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 77 | DCHECK(origin_task_runner_); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 80 | RunLoop::~RunLoop() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 81 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 82 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void RunLoop::Run() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 86 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 87 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 88 | if (!BeforeRun()) |
| 89 | return; |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 90 | |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 91 | // It is okay to access this RunLoop from another sequence while Run() is |
| 92 | // active as this RunLoop won't touch its state until after that returns (if |
| 93 | // the RunLoop's state is accessed while processing Run(), it will be re-bound |
| 94 | // to the accessing sequence for the remainder of that Run() -- accessing from |
| 95 | // multiple sequences is still disallowed). |
| 96 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 97 | |
Gabriel Charette | b030a4a | 2017-10-26 01:04:40 | [diff] [blame] | 98 | DCHECK_EQ(this, delegate_->active_run_loops_.top()); |
| 99 | const bool application_tasks_allowed = |
| 100 | delegate_->active_run_loops_.size() == 1U || |
| 101 | type_ == Type::kNestableTasksAllowed; |
| 102 | delegate_->Run(application_tasks_allowed); |
vadimt | 12f0f7d | 2014-09-15 19:19:38 | [diff] [blame] | 103 | |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 104 | // Rebind this RunLoop to the current thread after Run(). |
| 105 | DETACH_FROM_SEQUENCE(sequence_checker_); |
| 106 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 107 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 108 | AfterRun(); |
| 109 | } |
| 110 | |
| 111 | void RunLoop::RunUntilIdle() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 112 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 113 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 114 | quit_when_idle_received_ = true; |
| 115 | Run(); |
| 116 | } |
| 117 | |
| 118 | void RunLoop::Quit() { |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 119 | // Thread-safe. |
| 120 | |
| 121 | // This can only be hit if run_loop->Quit() is called directly (QuitClosure() |
| 122 | // proxies through ProxyToTaskRunner() as it can only deref its WeakPtr on |
| 123 | // |origin_task_runner_|). |
| 124 | if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
tzik | 0c100dc | 2017-06-26 06:13:17 | [diff] [blame] | 125 | origin_task_runner_->PostTask( |
| 126 | FROM_HERE, base::BindOnce(&RunLoop::Quit, Unretained(this))); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 127 | return; |
| 128 | } |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 129 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 130 | quit_called_ = true; |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 131 | if (running_ && delegate_->active_run_loops_.top() == this) { |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 132 | // This is the inner-most RunLoop, so quit now. |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 133 | delegate_->Quit(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 137 | void RunLoop::QuitWhenIdle() { |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 138 | // Thread-safe. |
| 139 | |
| 140 | // This can only be hit if run_loop->QuitWhenIdle() is called directly |
| 141 | // (QuitWhenIdleClosure() proxies through ProxyToTaskRunner() as it can only |
| 142 | // deref its WeakPtr on |origin_task_runner_|). |
| 143 | if (!origin_task_runner_->RunsTasksInCurrentSequence()) { |
| 144 | origin_task_runner_->PostTask( |
tzik | 0c100dc | 2017-06-26 06:13:17 | [diff] [blame] | 145 | FROM_HERE, base::BindOnce(&RunLoop::QuitWhenIdle, Unretained(this))); |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
fdoray | a4f28ec | 2016-06-10 00:08:58 | [diff] [blame] | 149 | quit_when_idle_received_ = true; |
| 150 | } |
| 151 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 152 | base::Closure RunLoop::QuitClosure() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 153 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 154 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 155 | allow_quit_current_deprecated_ = false; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 156 | |
| 157 | // Need to use ProxyToTaskRunner() as WeakPtrs vended from |
| 158 | // |weak_factory_| may only be accessed on |origin_task_runner_|. |
| 159 | // TODO(gab): It feels wrong that QuitClosure() is bound to a WeakPtr. |
| 160 | return base::Bind(&ProxyToTaskRunner, origin_task_runner_, |
| 161 | base::Bind(&RunLoop::Quit, weak_factory_.GetWeakPtr())); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 162 | } |
| 163 | |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 164 | base::Closure RunLoop::QuitWhenIdleClosure() { |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 165 | // TODO(gab): Fix bad usage and enable this check, http://crbug.com/715235. |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 166 | // DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 167 | allow_quit_current_deprecated_ = false; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 168 | |
| 169 | // Need to use ProxyToTaskRunner() as WeakPtrs vended from |
| 170 | // |weak_factory_| may only be accessed on |origin_task_runner_|. |
| 171 | // TODO(gab): It feels wrong that QuitWhenIdleClosure() is bound to a WeakPtr. |
| 172 | return base::Bind( |
| 173 | &ProxyToTaskRunner, origin_task_runner_, |
| 174 | base::Bind(&RunLoop::QuitWhenIdle, weak_factory_.GetWeakPtr())); |
fdoray | a365860 | 2016-06-10 18:23:15 | [diff] [blame] | 175 | } |
| 176 | |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 177 | // static |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 178 | bool RunLoop::IsRunningOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 179 | Delegate* delegate = tls_delegate.Get().Get(); |
| 180 | return delegate && !delegate->active_run_loops_.empty(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // static |
| 184 | bool RunLoop::IsNestedOnCurrentThread() { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 185 | Delegate* delegate = tls_delegate.Get().Get(); |
| 186 | return delegate && delegate->active_run_loops_.size() > 1; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // static |
| 190 | void RunLoop::AddNestingObserverOnCurrentThread(NestingObserver* observer) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 191 | Delegate* delegate = tls_delegate.Get().Get(); |
| 192 | DCHECK(delegate); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 193 | delegate->nesting_observers_.AddObserver(observer); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // static |
| 197 | void RunLoop::RemoveNestingObserverOnCurrentThread(NestingObserver* observer) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 198 | Delegate* delegate = tls_delegate.Get().Get(); |
| 199 | DCHECK(delegate); |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 200 | delegate->nesting_observers_.RemoveObserver(observer); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // static |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 204 | void RunLoop::QuitCurrentDeprecated() { |
| 205 | DCHECK(IsRunningOnCurrentThread()); |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 206 | Delegate* delegate = tls_delegate.Get().Get(); |
| 207 | DCHECK(delegate->active_run_loops_.top()->allow_quit_current_deprecated_) |
| 208 | << "Please migrate off QuitCurrentDeprecated(), e.g. to QuitClosure()."; |
| 209 | delegate->active_run_loops_.top()->Quit(); |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // static |
| 213 | void RunLoop::QuitCurrentWhenIdleDeprecated() { |
| 214 | DCHECK(IsRunningOnCurrentThread()); |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 215 | Delegate* delegate = tls_delegate.Get().Get(); |
| 216 | DCHECK(delegate->active_run_loops_.top()->allow_quit_current_deprecated_) |
| 217 | << "Please migrate off QuitCurrentWhenIdleDeprecated(), e.g. to " |
| 218 | "QuitWhenIdleClosure()."; |
| 219 | delegate->active_run_loops_.top()->QuitWhenIdle(); |
Gabriel Charette | 0592c3a | 2017-07-26 12:02:04 | [diff] [blame] | 220 | } |
| 221 | |
Gabriel Charette | 5bc8841 | 2018-05-16 03:28:25 | [diff] [blame] | 222 | // static |
| 223 | Closure RunLoop::QuitCurrentWhenIdleClosureDeprecated() { |
Wez | 325eafc | 2018-07-17 17:01:49 | [diff] [blame] | 224 | // TODO(844016): Fix callsites and enable this check, or remove the API. |
| 225 | // Delegate* delegate = tls_delegate.Get().Get(); |
| 226 | // DCHECK(delegate->active_run_loops_.top()->allow_quit_current_deprecated_) |
| 227 | // << "Please migrate off QuitCurrentWhenIdleClosureDeprecated(), e.g to " |
| 228 | // "QuitWhenIdleClosure()."; |
Gabriel Charette | 5bc8841 | 2018-05-16 03:28:25 | [diff] [blame] | 229 | return Bind(&RunLoop::QuitCurrentWhenIdleDeprecated); |
| 230 | } |
| 231 | |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 232 | #if DCHECK_IS_ON() |
| 233 | RunLoop::ScopedDisallowRunningForTesting::ScopedDisallowRunningForTesting() |
| 234 | : current_delegate_(tls_delegate.Get().Get()), |
| 235 | previous_run_allowance_( |
| 236 | current_delegate_ ? current_delegate_->allow_running_for_testing_ |
| 237 | : false) { |
| 238 | if (current_delegate_) |
| 239 | current_delegate_->allow_running_for_testing_ = false; |
| 240 | } |
| 241 | |
| 242 | RunLoop::ScopedDisallowRunningForTesting::~ScopedDisallowRunningForTesting() { |
| 243 | DCHECK_EQ(current_delegate_, tls_delegate.Get().Get()); |
| 244 | if (current_delegate_) |
| 245 | current_delegate_->allow_running_for_testing_ = previous_run_allowance_; |
| 246 | } |
| 247 | #else // DCHECK_IS_ON() |
| 248 | // Defined out of line so that the compiler doesn't inline these and realize |
| 249 | // the scope has no effect and then throws an "unused variable" warning in |
| 250 | // non-dcheck builds. |
| 251 | RunLoop::ScopedDisallowRunningForTesting::ScopedDisallowRunningForTesting() = |
| 252 | default; |
| 253 | RunLoop::ScopedDisallowRunningForTesting::~ScopedDisallowRunningForTesting() = |
| 254 | default; |
| 255 | #endif // DCHECK_IS_ON() |
| 256 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 257 | bool RunLoop::BeforeRun() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 258 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 259 | |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 260 | #if DCHECK_IS_ON() |
Gabriel Charette | a4497505 | 2017-08-21 23:14:04 | [diff] [blame] | 261 | DCHECK(delegate_->allow_running_for_testing_) |
| 262 | << "RunLoop::Run() isn't allowed in the scope of a " |
| 263 | "ScopedDisallowRunningForTesting. Hint: if mixing " |
| 264 | "TestMockTimeTaskRunners on same thread, use TestMockTimeTaskRunner's " |
| 265 | "API instead of RunLoop to drive individual task runners."; |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 266 | DCHECK(!run_called_); |
| 267 | run_called_ = true; |
gab | cf5e4ce | 2017-05-19 22:56:57 | [diff] [blame] | 268 | #endif // DCHECK_IS_ON() |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 269 | |
| 270 | // Allow Quit to be called before Run. |
| 271 | if (quit_called_) |
| 272 | return false; |
| 273 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 274 | auto& active_run_loops_ = delegate_->active_run_loops_; |
| 275 | active_run_loops_.push(this); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 276 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 277 | const bool is_nested = active_run_loops_.size() > 1; |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 278 | |
| 279 | if (is_nested) { |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 280 | for (auto& observer : delegate_->nesting_observers_) |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 281 | observer.OnBeginNestedRunLoop(); |
Gabriel Charette | 3ff403e | 2017-08-07 04:22:48 | [diff] [blame] | 282 | if (type_ == Type::kNestableTasksAllowed) |
| 283 | delegate_->EnsureWorkScheduled(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 284 | } |
jamescook | aacdfd0 | 2016-04-28 00:50:03 | [diff] [blame] | 285 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 286 | running_ = true; |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | void RunLoop::AfterRun() { |
gab | 980a5271 | 2017-05-18 16:20:16 | [diff] [blame] | 291 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 292 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 293 | running_ = false; |
| 294 | |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 295 | auto& active_run_loops_ = delegate_->active_run_loops_; |
| 296 | DCHECK_EQ(active_run_loops_.top(), this); |
| 297 | active_run_loops_.pop(); |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 298 | |
| 299 | RunLoop* previous_run_loop = |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 300 | active_run_loops_.empty() ? nullptr : active_run_loops_.top(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 301 | |
Francois Doray | 80bdddf | 2018-01-04 16:17:32 | [diff] [blame] | 302 | if (previous_run_loop) { |
| 303 | for (auto& observer : delegate_->nesting_observers_) |
| 304 | observer.OnExitNestedRunLoop(); |
| 305 | } |
| 306 | |
Gabriel Charette | e2b632b | 2017-08-02 03:52:16 | [diff] [blame] | 307 | // Execute deferred Quit, if any: |
gab | 7af9dc0 | 2017-05-05 13:38:54 | [diff] [blame] | 308 | if (previous_run_loop && previous_run_loop->quit_called_) |
gab | 27355196 | 2017-05-18 06:01:10 | [diff] [blame] | 309 | delegate_->Quit(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | } // namespace base |