| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/message_center/popup_timer.h" | 5 #include "ui/message_center/popup_timer.h" |
| 6 | 6 |
| 7 namespace message_center { | 7 namespace message_center { |
| 8 | 8 |
| 9 PopupTimer::PopupTimer(const std::string& id, | 9 PopupTimer::PopupTimer(const std::string& id, |
| 10 base::TimeDelta timeout, | 10 base::TimeDelta timeout, |
| 11 base::WeakPtr<Delegate> delegate) | 11 base::WeakPtr<Delegate> delegate) |
| 12 : id_(id), | 12 : id_(id), |
| 13 timeout_(timeout), | 13 timeout_(timeout), |
| 14 timer_delegate_(delegate), | 14 timer_delegate_(delegate), |
| 15 timer_(new base::OneShotTimer) {} | 15 timer_(new base::OneShotTimer) { |
| 16 DCHECK(timer_delegate_); |
| 17 } |
| 16 | 18 |
| 17 PopupTimer::~PopupTimer() { | 19 PopupTimer::~PopupTimer() { |
| 18 if (!timer_) | |
| 19 return; | |
| 20 | |
| 21 if (timer_->IsRunning()) | 20 if (timer_->IsRunning()) |
| 22 timer_->Stop(); | 21 timer_->Stop(); |
| 23 } | 22 } |
| 24 | 23 |
| 25 void PopupTimer::Start() { | 24 void PopupTimer::Start() { |
| 26 if (timer_->IsRunning()) | 25 if (timer_->IsRunning()) |
| 27 return; | 26 return; |
| 27 |
| 28 base::TimeDelta timeout_to_close = | 28 base::TimeDelta timeout_to_close = |
| 29 timeout_ <= passed_ ? base::TimeDelta() : timeout_ - passed_; | 29 timeout_ <= passed_ ? base::TimeDelta() : timeout_ - passed_; |
| 30 start_time_ = base::Time::Now(); | 30 start_time_ = base::Time::Now(); |
| 31 | 31 |
| 32 DCHECK(timer_delegate_); | |
| 33 timer_->Start( | 32 timer_->Start( |
| 34 FROM_HERE, timeout_to_close, | 33 FROM_HERE, timeout_to_close, |
| 35 base::Bind(&Delegate::TimerFinished, timer_delegate_, id_)); | 34 base::Bind(&Delegate::TimerFinished, timer_delegate_, id_)); |
| 36 } | 35 } |
| 37 | 36 |
| 38 void PopupTimer::Pause() { | 37 void PopupTimer::Pause() { |
| 39 if (!timer_ || !timer_->IsRunning()) | 38 if (!timer_->IsRunning()) |
| 40 return; | 39 return; |
| 41 | 40 |
| 42 timer_->Stop(); | 41 timer_->Stop(); |
| 43 passed_ += base::Time::Now() - start_time_; | 42 passed_ += base::Time::Now() - start_time_; |
| 44 } | 43 } |
| 45 | 44 |
| 46 void PopupTimer::Reset() { | |
| 47 if (timer_) | |
| 48 timer_->Stop(); | |
| 49 passed_ = base::TimeDelta(); | |
| 50 } | |
| 51 | |
| 52 } // namespace message_center | 45 } // namespace message_center |
| OLD | NEW |