popup_timer.cc 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #include "ui/message_center/popup_timer.h"
  5. #include "base/bind.h"
  6. namespace message_center {
  7. PopupTimer::PopupTimer(const std::string& id,
  8. base::TimeDelta timeout,
  9. base::WeakPtr<Delegate> delegate)
  10. : id_(id),
  11. timeout_(timeout),
  12. timer_delegate_(delegate),
  13. timer_(new base::OneShotTimer) {
  14. DCHECK(timer_delegate_);
  15. }
  16. PopupTimer::~PopupTimer() {
  17. if (timer_->IsRunning())
  18. timer_->Stop();
  19. }
  20. void PopupTimer::Start() {
  21. if (timer_->IsRunning())
  22. return;
  23. base::TimeDelta timeout_to_close =
  24. timeout_ <= passed_ ? base::TimeDelta() : timeout_ - passed_;
  25. start_time_ = base::Time::Now();
  26. timer_->Start(FROM_HERE, timeout_to_close,
  27. base::BindOnce(&Delegate::TimerFinished, timer_delegate_, id_));
  28. }
  29. void PopupTimer::Pause() {
  30. if (!timer_->IsRunning())
  31. return;
  32. timer_->Stop();
  33. passed_ += base::Time::Now() - start_time_;
  34. }
  35. } // namespace message_center