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