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. 4 5 #include "chrome/test/remoting/waiter.h" 6 7 #include "content/public/test/test_utils.h" 8 9 namespace remoting { 10 11 TimeoutWaiter::TimeoutWaiter(base::TimeDelta timeout) 12 : timeout_(timeout) { 13 DCHECK(timeout > base::TimeDelta::FromSeconds(0)); 14 } 15 16 TimeoutWaiter::~TimeoutWaiter() {} 17 18 bool TimeoutWaiter::Wait() { 19 DCHECK(!timeout_timer_.IsRunning()); 20 21 timeout_timer_.Start( 22 FROM_HERE, 23 timeout_, 24 base::Bind(&TimeoutWaiter::CancelWaitCallback, base::Unretained(this))); 25 26 message_loop_runner_ = new content::MessageLoopRunner; 27 message_loop_runner_->Run(); 28 29 return true; 30 } 31 32 void TimeoutWaiter::CancelWait() { 33 message_loop_runner_->Quit(); 34 } 35 36 void TimeoutWaiter::CancelWaitCallback() { 37 CancelWait(); 38 } 39 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); 48 } 49 50 ConditionalTimeoutWaiter::~ConditionalTimeoutWaiter() {} 51 52 bool ConditionalTimeoutWaiter::Wait() { 53 DCHECK(!condition_timer_.IsRunning()); 54 55 condition_timer_.Start( 56 FROM_HERE, 57 interval_, 58 base::Bind(&ConditionalTimeoutWaiter::CancelWaitCallback, 59 base::Unretained(this))); 60 61 // Also call the base class Wait() to start the timeout timer. 62 TimeoutWaiter::Wait(); 63 64 return success_; 65 } 66 67 void ConditionalTimeoutWaiter::CancelWait() { 68 condition_timer_.Stop(); 69 70 // Also call the base class CancelWait() to stop the timeout timer. 71 TimeoutWaiter::CancelWait(); 72 } 73 74 void ConditionalTimeoutWaiter::CancelWaitCallback() { 75 if (callback_.Run()) { 76 success_ = true; 77 CancelWait(); 78 } 79 } 80 81 } // namespace remoting 82