OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
6 #define CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 6 #define CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <map> | 10 #include <map> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/timer.h" | 13 #include "base/timer.h" |
14 #include "chrome/browser/extensions/extension_function.h" | 14 #include "chrome/browser/extensions/extension_function.h" |
15 #include "chrome/common/extensions/api/alarms.h" | 15 #include "chrome/common/extensions/api/alarms.h" |
16 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
17 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
18 | 18 |
19 class Profile; | 19 class Profile; |
20 | 20 |
21 namespace extensions { | 21 namespace extensions { |
22 | 22 |
23 class ExtensionAlarmsSchedulingTest; | 23 class ExtensionAlarmsSchedulingTest; |
24 | 24 |
25 | |
26 struct Alarm { | |
27 typedef base::Time (*TimeProvider)(); | |
28 | |
29 Alarm(); | |
30 Alarm(const std::string& name, | |
31 const extensions::api::alarms::AlarmOneShotCreateInfo& create_info, | |
Matt Perry
2012/06/11 22:32:44
extensions prefix is unnecessary
Jeffrey Yasskin
2012/06/12 19:12:31
Whoops, removed everywhere.
| |
32 base::TimeDelta min_granularity, | |
33 TimeProvider now); | |
34 Alarm(const std::string& name, | |
35 const extensions::api::alarms::AlarmRepeatingCreateInfo& create_info, | |
36 base::TimeDelta min_granularity, | |
37 TimeProvider now); | |
38 ~Alarm(); | |
39 | |
40 linked_ptr<extensions::api::alarms::Alarm> js_alarm; | |
41 // The granularity isn't exposed to the extension's javascript, but we poll at | |
42 // least as often as the shortest alarm's granularity. It's initialized as | |
43 // the relative delay requested in creation, even if creation uses an absolute | |
44 // time. This will always be at least as large as the min_granularity | |
45 // constructor argument. | |
46 base::TimeDelta granularity; | |
47 }; | |
48 | |
25 // Manages the currently pending alarms for every extension in a profile. | 49 // Manages the currently pending alarms for every extension in a profile. |
26 // There is one manager per virtual Profile. | 50 // There is one manager per virtual Profile. |
27 class AlarmManager : public content::NotificationObserver { | 51 class AlarmManager : public content::NotificationObserver { |
28 public: | 52 public: |
29 typedef extensions::api::alarms::Alarm Alarm; | 53 typedef base::Time (*TimeProvider)(); |
30 typedef std::vector<linked_ptr<Alarm> > AlarmList; | 54 typedef std::vector<Alarm> AlarmList; |
31 | 55 |
32 class Delegate { | 56 class Delegate { |
33 public: | 57 public: |
34 virtual ~Delegate() {} | 58 virtual ~Delegate() {} |
35 // Called when an alarm fires. | 59 // Called when an alarm fires. |
36 virtual void OnAlarm(const std::string& extension_id, | 60 virtual void OnAlarm(const std::string& extension_id, |
37 const Alarm& alarm) = 0; | 61 const Alarm& alarm) = 0; |
38 }; | 62 }; |
39 | 63 |
40 explicit AlarmManager(Profile* profile); | 64 // 'now' is usually &base::Time::Now. |
65 explicit AlarmManager(Profile* profile, TimeProvider now); | |
41 virtual ~AlarmManager(); | 66 virtual ~AlarmManager(); |
42 | 67 |
43 // Override the default delegate. Callee assumes onwership. Used for testing. | 68 // Override the default delegate. Callee assumes onwership. Used for testing. |
44 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); } | 69 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); } |
45 | 70 |
46 // Adds |alarm| for the given extension, and starts the timer. | 71 // Adds |alarm| for the given extension, and starts the timer. |
47 void AddAlarm(const std::string& extension_id, | 72 void AddAlarm(const std::string& extension_id, |
48 const linked_ptr<Alarm>& alarm); | 73 const Alarm& alarm); |
49 | 74 |
50 // Returns the alarm with the given name, or NULL if none exists. | 75 // Returns the alarm with the given name, or NULL if none exists. |
51 const Alarm* GetAlarm(const std::string& extension_id, | 76 const Alarm* GetAlarm(const std::string& extension_id, |
52 const std::string& name); | 77 const std::string& name); |
53 | 78 |
54 // Returns the list of pending alarms for the given extension, or NULL | 79 // Returns the list of pending alarms for the given extension, or NULL |
55 // if none exist. | 80 // if none exist. |
56 const AlarmList* GetAllAlarms(const std::string& extension_id); | 81 const AlarmList* GetAllAlarms(const std::string& extension_id); |
57 | 82 |
58 // Cancels and removes the alarm with the given name. | 83 // Cancels and removes the alarm with the given name. |
59 bool RemoveAlarm(const std::string& extension_id, | 84 bool RemoveAlarm(const std::string& extension_id, |
60 const std::string& name); | 85 const std::string& name); |
61 | 86 |
62 // Cancels and removes all alarms for the given extension. | 87 // Cancels and removes all alarms for the given extension. |
63 void RemoveAllAlarms(const std::string& extension_id); | 88 void RemoveAllAlarms(const std::string& extension_id); |
64 | 89 |
65 private: | 90 private: |
66 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, CreateRepeating); | 91 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, CreateRepeating); |
67 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, Clear); | 92 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, Clear); |
68 friend class ExtensionAlarmsSchedulingTest; | 93 friend class ExtensionAlarmsSchedulingTest; |
69 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); | 94 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); |
95 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, | |
96 ReleasedExtensionPollsInfrequently); | |
70 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); | 97 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); |
71 | 98 |
72 typedef std::string ExtensionId; | 99 typedef std::string ExtensionId; |
73 typedef std::map<ExtensionId, AlarmList> AlarmMap; | 100 typedef std::map<ExtensionId, AlarmList> AlarmMap; |
74 | 101 |
75 // Iterator used to identify a particular alarm within the Map/List pair. | 102 // Iterator used to identify a particular alarm within the Map/List pair. |
76 // "Not found" is represented by <alarms_.end(), invalid_iterator>. | 103 // "Not found" is represented by <alarms_.end(), invalid_iterator>. |
77 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; | 104 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; |
78 | 105 |
79 struct AlarmRuntimeInfo { | |
80 std::string extension_id; | |
81 base::Time time; | |
82 }; | |
83 typedef std::map<const Alarm*, AlarmRuntimeInfo> AlarmRuntimeInfoMap; | |
84 | |
85 // Helper to return the iterators within the AlarmMap and AlarmList for the | 106 // Helper to return the iterators within the AlarmMap and AlarmList for the |
86 // matching alarm, or an iterator to the end of the AlarmMap if none were | 107 // matching alarm, or an iterator to the end of the AlarmMap if none were |
87 // found. | 108 // found. |
88 AlarmIterator GetAlarmIterator(const std::string& extension_id, | 109 AlarmIterator GetAlarmIterator(const std::string& extension_id, |
89 const std::string& name); | 110 const std::string& name); |
90 | 111 |
91 // Helper to cancel and remove the alarm at the given iterator. The iterator | 112 // Helper to cancel and remove the alarm at the given iterator. The iterator |
92 // must be valid. | 113 // must be valid. |
93 void RemoveAlarmIterator(const AlarmIterator& iter); | 114 void RemoveAlarmIterator(const AlarmIterator& iter); |
94 | 115 |
95 // Callback for when an alarm fires. | 116 // Callback for when an alarm fires. |
96 void OnAlarm(const std::string& extension_id, const std::string& name); | 117 void OnAlarm(AlarmIterator iter); |
97 | 118 |
98 // Internal helper to add an alarm and start the timer with the given delay. | 119 // Internal helper to add an alarm and start the timer with the given delay. |
99 void AddAlarmImpl(const std::string& extension_id, | 120 void AddAlarmImpl(const std::string& extension_id, |
100 const linked_ptr<Alarm>& alarm, | 121 const Alarm& alarm); |
101 base::TimeDelta time_delay); | |
102 | 122 |
103 // Syncs our alarm data for the given extension to/from the prefs file. | 123 // Syncs our alarm data for the given extension to/from the prefs file. |
104 void WriteToPrefs(const std::string& extension_id); | 124 void WriteToPrefs(const std::string& extension_id); |
105 void ReadFromPrefs(const std::string& extension_id); | 125 void ReadFromPrefs(const std::string& extension_id); |
106 | 126 |
107 // Schedules the next poll of alarms for when the next soonest alarm runs, | 127 // Schedules the next poll of alarms for when the next soonest alarm runs, |
108 // but do not more often than min_period. | 128 // but do not more often than min_period. |
109 void ScheduleNextPoll(base::TimeDelta min_period); | 129 void ScheduleNextPoll(base::TimeDelta min_period); |
110 | 130 |
111 // Polls the alarms, running any that have elapsed. After running them and | 131 // Polls the alarms, running any that have elapsed. After running them and |
112 // rescheduling repeating alarms, schedule the next poll. | 132 // rescheduling repeating alarms, schedule the next poll. |
113 void PollAlarms(); | 133 void PollAlarms(); |
114 | 134 |
115 // NotificationObserver: | 135 // NotificationObserver: |
116 virtual void Observe(int type, | 136 virtual void Observe(int type, |
117 const content::NotificationSource& source, | 137 const content::NotificationSource& source, |
118 const content::NotificationDetails& details) OVERRIDE; | 138 const content::NotificationDetails& details) OVERRIDE; |
119 | 139 |
120 Profile* profile_; | 140 Profile* profile_; |
141 const TimeProvider now_; | |
121 content::NotificationRegistrar registrar_; | 142 content::NotificationRegistrar registrar_; |
122 scoped_ptr<Delegate> delegate_; | 143 scoped_ptr<Delegate> delegate_; |
123 | 144 |
124 // The timer for this alarm manager. | 145 // The timer for this alarm manager. |
125 base::OneShotTimer<AlarmManager> timer_; | 146 base::OneShotTimer<AlarmManager> timer_; |
126 | 147 |
127 // A map of our pending alarms, per extension. | 148 // A map of our pending alarms, per extension. |
149 // Invariant: None of the AlarmLists are empty. | |
128 AlarmMap alarms_; | 150 AlarmMap alarms_; |
129 | 151 |
130 // A map of the next scheduled times associated with each alarm. | |
131 AlarmRuntimeInfoMap scheduled_times_; | |
132 | |
133 // The previous and next time that alarms were and will be run. | 152 // The previous and next time that alarms were and will be run. |
134 base::Time last_poll_time_; | 153 base::Time last_poll_time_; |
135 base::Time next_poll_time_; | 154 base::Time next_poll_time_; |
136 }; | 155 }; |
137 | 156 |
138 // Contains the data we store in the extension prefs for each alarm. | |
139 struct AlarmPref { | |
140 linked_ptr<AlarmManager::Alarm> alarm; | |
141 base::Time scheduled_run_time; | |
142 | |
143 AlarmPref(); | |
144 ~AlarmPref(); | |
145 }; | |
146 | |
147 } // namespace extensions | 157 } // namespace extensions |
148 | 158 |
149 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 159 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
OLD | NEW |