Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / remoting / waiter.cc
blob7296068209302d1f634d6b26abbb9477a87907a1
1 // Copyright 2013 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 "chrome/test/remoting/waiter.h"
7 #include "content/public/test/test_utils.h"
9 namespace remoting {
11 TimeoutWaiter::TimeoutWaiter(base::TimeDelta timeout)
12 : timeout_(timeout) {
13 DCHECK(timeout > base::TimeDelta::FromSeconds(0));
16 TimeoutWaiter::~TimeoutWaiter() {}
18 bool TimeoutWaiter::Wait() {
19 DCHECK(!timeout_timer_.IsRunning());
21 timeout_timer_.Start(
22 FROM_HERE,
23 timeout_,
24 base::Bind(&TimeoutWaiter::CancelWaitCallback, base::Unretained(this)));
26 message_loop_runner_ = new content::MessageLoopRunner;
27 message_loop_runner_->Run();
29 return true;
32 void TimeoutWaiter::CancelWait() {
33 message_loop_runner_->Quit();
36 void TimeoutWaiter::CancelWaitCallback() {
37 CancelWait();
40 ConditionalTimeoutWaiter::ConditionalTimeoutWaiter(base::TimeDelta timeout,
41 base::TimeDelta interval,
42 const Predicate& callback)
43 : TimeoutWaiter(timeout),
44 interval_(interval),
45 callback_(callback),
46 success_(false) {
47 DCHECK(timeout > interval);
50 ConditionalTimeoutWaiter::~ConditionalTimeoutWaiter() {}
52 bool ConditionalTimeoutWaiter::Wait() {
53 DCHECK(!condition_timer_.IsRunning());
55 condition_timer_.Start(
56 FROM_HERE,
57 interval_,
58 base::Bind(&ConditionalTimeoutWaiter::CancelWaitCallback,
59 base::Unretained(this)));
61 // Also call the base class Wait() to start the timeout timer.
62 TimeoutWaiter::Wait();
64 return success_;
67 void ConditionalTimeoutWaiter::CancelWait() {
68 condition_timer_.Stop();
70 // Also call the base class CancelWait() to stop the timeout timer.
71 TimeoutWaiter::CancelWait();
74 void ConditionalTimeoutWaiter::CancelWaitCallback() {
75 if (callback_.Run()) {
76 success_ = true;
77 CancelWait();
81 } // namespace remoting