| Index: components/timer/alarm_timer.cc
|
| diff --git a/components/timer/alarm_timer.cc b/components/timer/alarm_timer.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9f9889028a74e780d767002b8a4e394ea825a71a
|
| --- /dev/null
|
| +++ b/components/timer/alarm_timer.cc
|
| @@ -0,0 +1,123 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "components/timer/alarm_timer.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/bind_helpers.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/logging.h"
|
| +#include "base/pending_task.h"
|
| +#include "components/timer/rtc_alarm.h"
|
| +
|
| +namespace timer {
|
| +
|
| +AlarmTimer::AlarmTimer(bool retain_user_task, bool is_repeating)
|
| + : base::Timer(retain_user_task, is_repeating),
|
| + delegate_(new RtcAlarm()),
|
| + can_wake_from_suspend_(false),
|
| + origin_message_loop_(NULL),
|
| + weak_factory_(this) {
|
| + can_wake_from_suspend_ = delegate_->Init(weak_factory_.GetWeakPtr());
|
| +}
|
| +
|
| +AlarmTimer::AlarmTimer(const tracked_objects::Location& posted_from,
|
| + base::TimeDelta delay,
|
| + const base::Closure& user_task,
|
| + bool is_repeating)
|
| + : base::Timer(posted_from, delay, user_task, is_repeating),
|
| + delegate_(new RtcAlarm()),
|
| + can_wake_from_suspend_(false),
|
| + origin_message_loop_(NULL),
|
| + weak_factory_(this) {
|
| + can_wake_from_suspend_ = delegate_->Init(weak_factory_.GetWeakPtr());
|
| +}
|
| +
|
| +AlarmTimer::~AlarmTimer() {
|
| + Stop();
|
| +}
|
| +
|
| +void AlarmTimer::Stop() {
|
| + if (!can_wake_from_suspend_) {
|
| + base::Timer::Stop();
|
| + return;
|
| + }
|
| +
|
| + // Clear the running flag, stop the delegate, and delete the pending task.
|
| + is_running_ = false;
|
| + delegate_->Stop();
|
| + pending_task_.reset();
|
| +
|
| + // Stop is called when the AlarmTimer is destroyed so we need to remove
|
| + // ourselves as a MessageLoop::DestructionObserver to prevent a segfault
|
| + // later.
|
| + if (origin_message_loop_) {
|
| + origin_message_loop_->RemoveDestructionObserver(this);
|
| + origin_message_loop_ = NULL;
|
| + }
|
| +
|
| + if (!retain_user_task_)
|
| + user_task_.Reset();
|
| +}
|
| +
|
| +void AlarmTimer::Reset() {
|
| + if (!can_wake_from_suspend_) {
|
| + base::Timer::Reset();
|
| + return;
|
| + }
|
| +
|
| + DCHECK(!user_task_.is_null());
|
| + DCHECK(!origin_message_loop_ ||
|
| + origin_message_loop_->task_runner()->RunsTasksOnCurrentThread());
|
| +
|
| + // Make sure that the timer will stop if the underlying message loop is
|
| + // destroyed.
|
| + if (!origin_message_loop_) {
|
| + origin_message_loop_ = base::MessageLoop::current();
|
| + origin_message_loop_->AddDestructionObserver(this);
|
| + }
|
| +
|
| + // Set up the pending task.
|
| + if (delay_ == base::TimeDelta()) {
|
| + desired_run_time_ = base::TimeTicks();
|
| + pending_task_.reset(new base::PendingTask(posted_from_, user_task_));
|
| + } else {
|
| + desired_run_time_ = base::TimeTicks::Now() + delay_;
|
| + pending_task_.reset(new base::PendingTask(
|
| + posted_from_, user_task_, desired_run_time_, true));
|
| + }
|
| + base::MessageLoop::current()->task_annotator()->DidQueueTask(
|
| + "AlarmTimer::Reset", *pending_task_);
|
| +
|
| + // Now start up the timer.
|
| + delegate_->Reset(delay_);
|
| + is_running_ = true;
|
| +}
|
| +
|
| +void AlarmTimer::WillDestroyCurrentMessageLoop() {
|
| + Stop();
|
| +}
|
| +
|
| +void AlarmTimer::OnTimerFired() {
|
| + if (!is_running_)
|
| + return;
|
| +
|
| + DCHECK(pending_task_.get());
|
| +
|
| + // Take ownership of the pending user task, which is going to be cleared by
|
| + // the Stop() or Reset() functions below.
|
| + scoped_ptr<base::PendingTask> pending_user_task(pending_task_.Pass());
|
| +
|
| + // Re-schedule or stop the timer as requested.
|
| + if (is_repeating_)
|
| + Reset();
|
| + else
|
| + Stop();
|
| +
|
| + // Now lets run the user task.
|
| + base::MessageLoop::current()->task_annotator()->RunTask(
|
| + "AlarmTimer::Reset", "AlarmTimer::OnTimerFired", *pending_user_task);
|
| +}
|
| +
|
| +} // namespace timer
|
|
|