dewittj | ddb90228 | 2015-09-22 21:10:14 | [diff] [blame] | 1 | // Copyright (c) 2015 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 "ui/message_center/popup_timer.h" |
| 6 | |
johnme | 07e31a3 | 2015-10-07 19:14:06 | [diff] [blame] | 7 | namespace message_center { |
| 8 | |
dewittj | ddb90228 | 2015-09-22 21:10:14 | [diff] [blame] | 9 | PopupTimer::PopupTimer(const std::string& id, |
| 10 | base::TimeDelta timeout, |
miguelg | dc180892 | 2016-01-29 21:43:21 | [diff] [blame] | 11 | base::WeakPtr<Delegate> delegate) |
dewittj | ddb90228 | 2015-09-22 21:10:14 | [diff] [blame] | 12 | : id_(id), |
| 13 | timeout_(timeout), |
miguelg | dc180892 | 2016-01-29 21:43:21 | [diff] [blame] | 14 | timer_delegate_(delegate), |
peter | 90faa18 | 2016-09-12 19:08:26 | [diff] [blame] | 15 | timer_(new base::OneShotTimer) { |
| 16 | DCHECK(timer_delegate_); |
| 17 | } |
dewittj | ddb90228 | 2015-09-22 21:10:14 | [diff] [blame] | 18 | |
| 19 | PopupTimer::~PopupTimer() { |
dewittj | ddb90228 | 2015-09-22 21:10:14 | [diff] [blame] | 20 | if (timer_->IsRunning()) |
| 21 | timer_->Stop(); |
| 22 | } |
| 23 | |
| 24 | void PopupTimer::Start() { |
| 25 | if (timer_->IsRunning()) |
| 26 | return; |
peter | 90faa18 | 2016-09-12 19:08:26 | [diff] [blame] | 27 | |
dewittj | ddb90228 | 2015-09-22 21:10:14 | [diff] [blame] | 28 | base::TimeDelta timeout_to_close = |
| 29 | timeout_ <= passed_ ? base::TimeDelta() : timeout_ - passed_; |
| 30 | start_time_ = base::Time::Now(); |
miguelg | dc180892 | 2016-01-29 21:43:21 | [diff] [blame] | 31 | |
miguelg | dc180892 | 2016-01-29 21:43:21 | [diff] [blame] | 32 | timer_->Start( |
| 33 | FROM_HERE, timeout_to_close, |
| 34 | base::Bind(&Delegate::TimerFinished, timer_delegate_, id_)); |
dewittj | ddb90228 | 2015-09-22 21:10:14 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void PopupTimer::Pause() { |
peter | 90faa18 | 2016-09-12 19:08:26 | [diff] [blame] | 38 | if (!timer_->IsRunning()) |
dewittj | ddb90228 | 2015-09-22 21:10:14 | [diff] [blame] | 39 | return; |
| 40 | |
| 41 | timer_->Stop(); |
| 42 | passed_ += base::Time::Now() - start_time_; |
| 43 | } |
| 44 | |
dewittj | ddb90228 | 2015-09-22 21:10:14 | [diff] [blame] | 45 | } // namespace message_center |