Allow resize-to-client to be enabled by the user.
[chromium-blink-merge.git] / base / run_loop.cc
blob991571eacdd979de1f9e07f593a1eaa88b38b776
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.
5 #include "base/run_loop.h"
7 #include "base/bind.h"
9 namespace base {
11 RunLoop::RunLoop()
12 : loop_(MessageLoop::current()),
13 weak_factory_(this),
14 previous_run_loop_(NULL),
15 run_depth_(0),
16 run_called_(false),
17 quit_called_(false),
18 running_(false),
19 quit_when_idle_received_(false) {
20 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
21 dispatcher_ = NULL;
22 #endif
25 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
26 RunLoop::RunLoop(MessageLoop::Dispatcher* dispatcher)
27 : loop_(MessageLoop::current()),
28 weak_factory_(this),
29 previous_run_loop_(NULL),
30 dispatcher_(dispatcher),
31 run_depth_(0),
32 run_called_(false),
33 quit_called_(false),
34 running_(false),
35 quit_when_idle_received_(false) {
37 #endif
39 RunLoop::~RunLoop() {
42 void RunLoop::Run() {
43 if (!BeforeRun())
44 return;
45 loop_->RunHandler();
46 AfterRun();
49 void RunLoop::RunUntilIdle() {
50 quit_when_idle_received_ = true;
51 Run();
54 void RunLoop::Quit() {
55 quit_called_ = true;
56 if (running_ && loop_->run_loop_ == this) {
57 // This is the inner-most RunLoop, so quit now.
58 loop_->QuitNow();
62 base::Closure RunLoop::QuitClosure() {
63 return base::Bind(&RunLoop::Quit, weak_factory_.GetWeakPtr());
66 bool RunLoop::BeforeRun() {
67 DCHECK(!run_called_);
68 run_called_ = true;
70 // Allow Quit to be called before Run.
71 if (quit_called_)
72 return false;
74 // Push RunLoop stack:
75 previous_run_loop_ = loop_->run_loop_;
76 run_depth_ = previous_run_loop_? previous_run_loop_->run_depth_ + 1 : 1;
77 loop_->run_loop_ = this;
79 running_ = true;
80 return true;
83 void RunLoop::AfterRun() {
84 running_ = false;
86 // Pop RunLoop stack:
87 loop_->run_loop_ = previous_run_loop_;
89 // Execute deferred QuitNow, if any:
90 if (previous_run_loop_ && previous_run_loop_->quit_called_)
91 loop_->QuitNow();
94 } // namespace base