Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(452)

Unified Diff: components/timer/alarm_timer.cc

Issue 641943002: components: Introduce AlarmTimer class and use it for GCM heartbeat (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix gyp files Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..1c3b27cb0eb2b11787d7f0286218dee2247bd13c
--- /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()) {
Lei Zhang 2014/10/21 22:34:11 timer.cc uses "delay_ > TimeDelta::FromMicrosecond
Chirantan Ekbote 2014/10/21 22:52:21 Ah ok. Will fix in the next patch set.
Daniel Erat 2014/10/23 03:44:54 TimeDelta() and TimeDelta::FromMicroseconds(0) are
Chirantan Ekbote 2014/10/23 23:53:30 The documentation for timerfd_settime doesn't say
+ 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 run the user task.
+ base::MessageLoop::current()->task_annotator()->RunTask(
+ "AlarmTimer::Reset", "AlarmTimer::OnTimerFired", *pending_user_task);
+}
+
+} // namespace timer

Powered by Google App Engine
This is Rietveld 408576698