OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 |
| 5 #ifndef CHROMECAST_BASE_ALARM_MANAGER_H_ |
| 6 #define CHROMECAST_BASE_ALARM_MANAGER_H_ |
| 7 |
| 8 #include <queue> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 |
| 16 namespace base { |
| 17 class Clock; |
| 18 class SingleThreadTaskRunner; |
| 19 class Timer; |
| 20 } |
| 21 |
| 22 namespace chromecast { |
| 23 |
| 24 // Alarm manager allows setting a task for wall clock time rather than for an |
| 25 // elapsed amount of time. This is different from using long PostDelayedTasks |
| 26 // that are sensitive to time changes, clock drift, and other factors. |
| 27 // |
| 28 // Alarm manager polls the wall clock time every 5 seconds. If the clock is |
| 29 // equal or past the requested time, the alarm will fire. |
| 30 class AlarmManager { |
| 31 public: |
| 32 // Construct and start the alarm manager. The clock poller will run on the |
| 33 // caller's thread. |
| 34 AlarmManager(); |
| 35 ~AlarmManager(); |
| 36 |
| 37 // For testing only. Allows setting a fake clock and using a custom task |
| 38 // runner. |
| 39 AlarmManager(std::unique_ptr<base::Clock> clock, |
| 40 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 41 |
| 42 // Add an alarm. |
| 43 // |task| will be executed at around |time|. |
| 44 // |
| 45 // Any thread can add alarms. The alarm will be fired on the original |
| 46 // thread used to set the alarm. |
| 47 // |
| 48 // When an alarm is added to the alarm manager, the task is guaranteed to not |
| 49 // run before the clock passes the requested time. The task may not run even |
| 50 // if |
| 51 // it is past the requested time if the software is suspended. However, once |
| 52 // woken up, the event will fire within 5 seconds if the target time has |
| 53 // passed. |
| 54 void PostAlarmTask(const base::Closure& task, base::Time time); |
| 55 |
| 56 private: |
| 57 class AlarmInfo { |
| 58 public: |
| 59 AlarmInfo(const base::Closure& task, |
| 60 base::Time time, |
| 61 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 62 ~AlarmInfo(); |
| 63 |
| 64 const base::Closure task() const { return task_; } |
| 65 base::Time time() const { return time_; } |
| 66 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const { |
| 67 return task_runner_; |
| 68 } |
| 69 |
| 70 private: |
| 71 const base::Closure task_; |
| 72 const base::Time time_; |
| 73 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 74 DISALLOW_COPY_AND_ASSIGN(AlarmInfo); |
| 75 }; |
| 76 |
| 77 // Check if an alarm should fire. |
| 78 void CheckAlarm(); |
| 79 // Add the alarm to the queue. |
| 80 void AddAlarm(const base::Closure& task, |
| 81 base::Time time, |
| 82 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 83 |
| 84 // Ordering alarms by earliest time. |
| 85 struct alarm_compare |
| 86 : public std::binary_function<std::unique_ptr<AlarmInfo>&, |
| 87 std::unique_ptr<AlarmInfo>&, |
| 88 bool> { |
| 89 bool operator()(const std::unique_ptr<AlarmInfo>& lhs, |
| 90 const std::unique_ptr<AlarmInfo>& rhs) const { |
| 91 return lhs->time() > rhs->time(); |
| 92 } |
| 93 }; |
| 94 |
| 95 // Store a list of the alarms to fire with the earliest getting priority. |
| 96 std::priority_queue<std::unique_ptr<AlarmInfo>, |
| 97 std::vector<std::unique_ptr<AlarmInfo>>, |
| 98 alarm_compare> |
| 99 next_alarm_; |
| 100 |
| 101 // Poller for wall clock time. |
| 102 std::unique_ptr<base::Clock> clock_; |
| 103 std::unique_ptr<base::Timer> clock_tick_timer_; |
| 104 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 105 |
| 106 base::WeakPtrFactory<AlarmManager> weak_factory_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(AlarmManager); |
| 109 }; |
| 110 |
| 111 } // namespace chromecast |
| 112 |
| 113 #endif // CHROMECAST_BASE_ALARM_MANAGER_H_ |
OLD | NEW |