blob: 66e9b20c34ac8e3ab05f650ae7d50cb9871bf703 [file] [log] [blame]
dewittjddb902282015-09-22 21:10:141// 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
johnme07e31a32015-10-07 19:14:067namespace message_center {
8
dewittjddb902282015-09-22 21:10:149PopupTimer::PopupTimer(const std::string& id,
10 base::TimeDelta timeout,
miguelgdc1808922016-01-29 21:43:2111 base::WeakPtr<Delegate> delegate)
dewittjddb902282015-09-22 21:10:1412 : id_(id),
13 timeout_(timeout),
miguelgdc1808922016-01-29 21:43:2114 timer_delegate_(delegate),
peter90faa182016-09-12 19:08:2615 timer_(new base::OneShotTimer) {
16 DCHECK(timer_delegate_);
17}
dewittjddb902282015-09-22 21:10:1418
19PopupTimer::~PopupTimer() {
dewittjddb902282015-09-22 21:10:1420 if (timer_->IsRunning())
21 timer_->Stop();
22}
23
24void PopupTimer::Start() {
25 if (timer_->IsRunning())
26 return;
peter90faa182016-09-12 19:08:2627
dewittjddb902282015-09-22 21:10:1428 base::TimeDelta timeout_to_close =
29 timeout_ <= passed_ ? base::TimeDelta() : timeout_ - passed_;
30 start_time_ = base::Time::Now();
miguelgdc1808922016-01-29 21:43:2131
miguelgdc1808922016-01-29 21:43:2132 timer_->Start(
33 FROM_HERE, timeout_to_close,
34 base::Bind(&Delegate::TimerFinished, timer_delegate_, id_));
dewittjddb902282015-09-22 21:10:1435}
36
37void PopupTimer::Pause() {
peter90faa182016-09-12 19:08:2638 if (!timer_->IsRunning())
dewittjddb902282015-09-22 21:10:1439 return;
40
41 timer_->Stop();
42 passed_ += base::Time::Now() - start_time_;
43}
44
dewittjddb902282015-09-22 21:10:1445} // namespace message_center