Index: components/timers/alarm_timer.h |
diff --git a/components/timers/alarm_timer.h b/components/timers/alarm_timer.h |
index d2f93335d61c518c4516b523b94058df949a1577..a855a9df1a027212680e9fd57294f9a392ee3f8a 100644 |
--- a/components/timers/alarm_timer.h |
+++ b/components/timers/alarm_timer.h |
@@ -27,38 +27,6 @@ namespace timers { |
class AlarmTimer : public base::Timer, |
public base::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 base::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(base::WeakPtr<AlarmTimer> timer) = 0; |
- |
- // Stops the currently running timer. It should be safe to call this more |
- // than once. |
- virtual void Stop() = 0; |
- |
- // Resets the timer to fire after |delay| has passed. Cancels any |
- // pre-existing delay. |
- virtual void Reset(base::TimeDelta delay) = 0; |
- |
- protected: |
- virtual ~Delegate() {} |
- |
- private: |
- friend class base::RefCountedThreadSafe<Delegate>; |
- }; |
- |
- AlarmTimer(bool retain_user_task, bool is_repeating); |
- |
- AlarmTimer(const tracked_objects::Location& posted_from, |
- base::TimeDelta delay, |
- const base::Closure& user_task, |
- bool is_repeating); |
- |
~AlarmTimer() override; |
bool can_wake_from_suspend() const { return can_wake_from_suspend_; } |
@@ -74,11 +42,21 @@ class AlarmTimer : public base::Timer, |
// MessageLoop::DestructionObserver overrides. |
void WillDestroyCurrentMessageLoop() override; |
+ protected: |
+ // The constructors for this class are protected because consumers should |
+ // instantiate one of the specialized sub-classes defined below instead. |
+ AlarmTimer(bool retain_user_task, bool is_repeating); |
+ AlarmTimer(const tracked_objects::Location& posted_from, |
+ base::TimeDelta delay, |
+ const base::Closure& user_task, |
+ bool is_repeating); |
+ |
private: |
// Initializes the timer with the appropriate delegate. |
void Init(); |
// Delegate that will manage actually setting the timer. |
+ class Delegate; |
scoped_refptr<Delegate> delegate_; |
// Keeps track of the user task we want to run. A new one is constructed |
@@ -100,6 +78,55 @@ class AlarmTimer : public base::Timer, |
DISALLOW_COPY_AND_ASSIGN(AlarmTimer); |
}; |
+// As its name suggests, a OneShotAlarmTimer runs a given task once. It does |
+// not remember the task that was given to it after it has fired and does not |
+// repeat. Useful for fire-and-forget tasks. |
+class OneShotAlarmTimer : public AlarmTimer { |
+ public: |
+ // Constructs a basic OneShotAlarmTimer. An AlarmTimer constructed this way |
+ // requires that Start() is called before Reset() is called. |
+ OneShotAlarmTimer() : AlarmTimer(false, false) {} |
+}; |
+ |
+// A RepeatingAlarmTimer takes a task and delay and repeatedly runs the task |
+// using the specified delay as an interval between the runs until it is |
+// explicitly stopped. It remembers both the task and the delay it was given |
+// after it fires. |
+class RepeatingAlarmTimer : public AlarmTimer { |
+ public: |
+ // Constructs a basic RepeatingAlarmTimer. An AlarmTimer constructed this way |
+ // requires that Start() is called before Reset() is called. |
+ RepeatingAlarmTimer() : AlarmTimer(true, true) {} |
+ |
+ // Constructs a RepeatingAlarmTimer with pre-populated parameters but does not |
+ // start it. Useful if |user_task| or |delay| are not going to change. |
+ // Reset() can be called immediately after constructing an AlarmTimer in this |
+ // way. |
+ RepeatingAlarmTimer(const tracked_objects::Location& posted_from, |
+ base::TimeDelta delay, |
+ const base::Closure& user_task) |
+ : AlarmTimer(posted_from, delay, user_task, true) {} |
+}; |
+ |
+// A SimpleAlarmTimer only fires once but remembers the task that it was given |
+// even after it has fired. Useful if you want to run the same task multiple |
+// times but not at a regular interval. |
+class SimpleAlarmTimer : public AlarmTimer { |
+ public: |
+ // Constructs a basic SimpleAlarmTimer. An AlarmTimer constructed this way |
+ // requires that Start() is called before Reset() is called. |
+ SimpleAlarmTimer() : AlarmTimer(true, false) {} |
+ |
+ // Constructs a SimpleAlarmTimer with pre-populated parameters but does not |
+ // start it. Useful if |user_task| or |delay| are not going to change. |
+ // Reset() can be called immediately after constructing an AlarmTimer in this |
+ // way. |
+ SimpleAlarmTimer(const tracked_objects::Location& posted_from, |
+ base::TimeDelta delay, |
+ const base::Closure& user_task) |
+ : AlarmTimer(posted_from, delay, user_task, false) {} |
+}; |
+ |
} // namespace timers |
#endif // COMPONENTS_TIMER_ALARM_TIMER_H_ |