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

Unified Diff: base/timer/alarm_timer.h

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: Make some Timer variables protected to reduce duplication, clean up comments 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: base/timer/alarm_timer.h
diff --git a/base/timer/alarm_timer.h b/base/timer/alarm_timer.h
new file mode 100644
index 0000000000000000000000000000000000000000..8ba818301c848d0848bf1533379c1c8d0cd50faf
--- /dev/null
+++ b/base/timer/alarm_timer.h
@@ -0,0 +1,104 @@
+// 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.
+
+#ifndef BASE_TIMER_ALARM_TIMER_CHROMEOS_H_
+#define BASE_TIMER_ALARM_TIMER_CHROMEOS_H_
+
+#include "base/base_export.h"
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/weak_ptr.h"
+#include "base/message_loop/message_loop.h"
+#include "base/time/time.h"
+#include "base/timer/timer.h"
+
+namespace base {
+
+struct PendingTask;
+
+// The class implements a timer that is capable of waking the system up from a
+// suspended state. For example, this is useful for running tasks that are
+// needed for maintaining network connectivity, like sending heartbeat messages.
+// Currently, this feature is only available on Chrome OS systems running linux
+// version 3.11 or higher. On all other platforms, the AlarmTimer behaves
+// exactly the same way as a regular Timer.
+class BASE_EXPORT AlarmTimer : public Timer,
+ public MessageLoop::DestructionObserver {
+ public:
+ // The delegate is responsible for managing the system level details for
+ // actually setting up and monitoring a timer that is capable of waking the
+ // system from suspend. This class is reference counted because it may need
+ // to outlive the timer in order to clean up after itself.
+ class Delegate : public RefCountedThreadSafe<Delegate> {
+ public:
+ // Initializes the timer. Should return true iff the system has timers that
+ // can wake it up from suspend. Will only be called once.
+ virtual bool Init(WeakPtr<AlarmTimer> timer) = 0;
+
+ // Stop the currently running timer. It should be safe to call this more
Daniel Erat 2014/10/15 15:04:22 nit: s/Stop/Stops/
Chirantan Ekbote 2014/10/16 21:26:38 Done.
+ // than once.
+ virtual void Stop() = 0;
+
+ // Reset the timer to fire after |delay| amount of time has passed. Cancels
Daniel Erat 2014/10/15 15:04:22 nit: s/Reset/Resets/; can also remove "amount of t
Chirantan Ekbote 2014/10/16 21:26:38 Done.
+ // any pre-existing delay.
+ virtual void Reset(TimeDelta delay) = 0;
+
+ protected:
+ virtual ~Delegate() {};
+
+ private:
+ friend class RefCountedThreadSafe<Delegate>;
+ };
+
+ AlarmTimer(bool retain_user_task, bool is_repeating);
+
+ AlarmTimer(const tracked_objects::Location& posted_from,
+ TimeDelta delay,
+ const Closure& user_task,
+ bool is_repeating);
+
+ virtual ~AlarmTimer();
+
+ // Timer overrides.
+ virtual bool IsRunning() const override;
+ virtual TimeDelta GetCurrentDelay() const override;
+ virtual void Start(const tracked_objects::Location& posted_from,
+ TimeDelta delay,
+ const Closure& user_task) override;
+ virtual void Stop() override;
+ virtual void Reset() override;
+
+ // MessageLoop::DestructionObserver overrides.
+ virtual void WillDestroyCurrentMessageLoop() override;
+
+ // Must be called by the delegate to indicate that the timer has fired and
+ // that the user task should be run.
+ void OnTimerFired();
+
+ private:
+ // Delegate that will manage actually setting the timer.
+ scoped_refptr<Delegate> delegate_;
+
+ // Keeps track of the user task we want to run. A new one is constructed
+ // every timer Reset() is called.
+ scoped_ptr<PendingTask> pending_task_;
+
+ // Tracks whether the timer has the ability to wake the system up from
+ // suspend. This is a runtime check because we won't know if the system
+ // supports being woken up from suspend until the delegate actually tries to
+ // set it up.
+ bool can_wake_from_suspend_;
+
+ // Pointer to the message loop that started the timer. Used to track the
+ // destruction of that message loop.
+ MessageLoop* origin_message_loop_;
+
+ WeakPtrFactory<AlarmTimer> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(AlarmTimer);
+};
+
+} // namespace base
Daniel Erat 2014/10/15 15:04:22 nit: add blank line after this one
Chirantan Ekbote 2014/10/16 21:26:38 Done.
+#endif // BASE_TIMER_ALARM_TIMER_CHROMEOS_H_

Powered by Google App Engine
This is Rietveld 408576698