| OLD | NEW |
| (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 #include "base/run_loop.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 |
| 9 namespace base { |
| 10 |
| 11 RunLoop::RunLoop() |
| 12 : loop_(MessageLoop::current()), |
| 13 run_called_(false), |
| 14 quit_called_(false), |
| 15 running_(false), |
| 16 run_depth_(0), |
| 17 quit_when_idle_received_(false) { |
| 18 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 19 dispatcher_ = NULL; |
| 20 #endif |
| 21 } |
| 22 |
| 23 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 24 RunLoop::RunLoop(MessageLoop::Dispatcher* dispatcher) |
| 25 : loop_(MessageLoop::current()), |
| 26 run_called_(false), |
| 27 quit_called_(false), |
| 28 running_(false), |
| 29 run_depth_(0), |
| 30 quit_when_idle_received_(false), |
| 31 dispatcher_(dispatcher) { |
| 32 } |
| 33 #endif |
| 34 |
| 35 RunLoop::~RunLoop() { |
| 36 } |
| 37 |
| 38 void RunLoop::Run() { |
| 39 if (!BeforeRun()) |
| 40 return; |
| 41 loop_->RunHandler(); |
| 42 AfterRun(); |
| 43 } |
| 44 |
| 45 void RunLoop::RunUntilIdle() { |
| 46 quit_when_idle_received_ = true; |
| 47 Run(); |
| 48 } |
| 49 |
| 50 void RunLoop::Quit() { |
| 51 quit_called_ = true; |
| 52 if (running_ && loop_->run_loop_ == this) { |
| 53 // This is the inner-most RunLoop, so quit now. |
| 54 loop_->QuitNow(); |
| 55 } |
| 56 } |
| 57 |
| 58 base::Closure RunLoop::QuitClosure() { |
| 59 return base::Bind(&RunLoop::Quit, AsWeakPtr()); |
| 60 } |
| 61 |
| 62 bool RunLoop::BeforeRun() { |
| 63 DCHECK(!run_called_); |
| 64 run_called_ = true; |
| 65 |
| 66 // Allow Quit to be called before Run. |
| 67 if (quit_called_) |
| 68 return false; |
| 69 |
| 70 // Push RunLoop stack: |
| 71 previous_run_loop_ = loop_->run_loop_; |
| 72 run_depth_ = previous_run_loop_? previous_run_loop_->run_depth_ + 1 : 1; |
| 73 loop_->run_loop_ = AsWeakPtr(); |
| 74 |
| 75 running_ = true; |
| 76 return true; |
| 77 } |
| 78 |
| 79 void RunLoop::AfterRun() { |
| 80 running_ = false; |
| 81 |
| 82 // Pop RunLoop stack: |
| 83 loop_->run_loop_ = previous_run_loop_; |
| 84 |
| 85 // Execute deferred QuitNow, if any: |
| 86 if (previous_run_loop_ && previous_run_loop_->quit_called_) |
| 87 loop_->QuitNow(); |
| 88 } |
| 89 |
| 90 } // namespace base |
| OLD | NEW |