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 | 7 |
8 #include <map> | 8 #include <map> |
| 9 #include <queue> |
9 #include <string> | 10 #include <string> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
| 13 #include "base/callback.h" |
12 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
13 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
14 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" | 16 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" |
15 #include "chrome/browser/extensions/extension_function.h" | 17 #include "chrome/browser/extensions/extension_function.h" |
16 #include "chrome/common/extensions/api/alarms.h" | 18 #include "chrome/common/extensions/api/alarms.h" |
17 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
18 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
19 | 21 |
20 class Profile; | 22 class Profile; |
21 | 23 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 virtual void OnAlarm(const std::string& extension_id, | 62 virtual void OnAlarm(const std::string& extension_id, |
61 const Alarm& alarm) = 0; | 63 const Alarm& alarm) = 0; |
62 }; | 64 }; |
63 | 65 |
64 explicit AlarmManager(Profile* profile); | 66 explicit AlarmManager(Profile* profile); |
65 virtual ~AlarmManager(); | 67 virtual ~AlarmManager(); |
66 | 68 |
67 // Override the default delegate. Callee assumes onwership. Used for testing. | 69 // Override the default delegate. Callee assumes onwership. Used for testing. |
68 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); } | 70 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); } |
69 | 71 |
70 // Adds |alarm| for the given extension, and starts the timer. | 72 typedef base::Callback<void()> AddAlarmCallback; |
| 73 // Adds |alarm| for the given extension, and starts the timer. Invokes |
| 74 // |callback| when done. |
71 void AddAlarm(const std::string& extension_id, | 75 void AddAlarm(const std::string& extension_id, |
72 const Alarm& alarm); | 76 const Alarm& alarm, |
| 77 const AddAlarmCallback& callback); |
73 | 78 |
74 // Returns the alarm with the given name, or NULL if none exists. | 79 typedef base::Callback<void(Alarm*)> GetAlarmCallback; |
75 const Alarm* GetAlarm(const std::string& extension_id, | 80 // Passes the alarm with the given name, or NULL if none exists, to |
76 const std::string& name); | 81 // |callback|. |
| 82 void GetAlarm(const std::string& extension_id, |
| 83 const std::string& name, |
| 84 const GetAlarmCallback& callback); |
77 | 85 |
78 // Returns the list of pending alarms for the given extension, or NULL | 86 typedef base::Callback<void(const AlarmList*)> GetAllAlarmsCallback; |
79 // if none exist. | 87 // Passes the list of pending alarms for the given extension, or |
80 const AlarmList* GetAllAlarms(const std::string& extension_id); | 88 // NULL if none exist, to |callback|. |
| 89 void GetAllAlarms( |
| 90 const std::string& extension_id, const GetAllAlarmsCallback& callback); |
81 | 91 |
82 // Cancels and removes the alarm with the given name. | 92 typedef base::Callback<void(bool)> RemoveAlarmCallback; |
83 bool RemoveAlarm(const std::string& extension_id, | 93 // Cancels and removes the alarm with the given name. Invokes |callback| when |
84 const std::string& name); | 94 // done. |
| 95 void RemoveAlarm(const std::string& extension_id, |
| 96 const std::string& name, |
| 97 const RemoveAlarmCallback& callback); |
85 | 98 |
86 // Cancels and removes all alarms for the given extension. | 99 typedef base::Callback<void()> RemoveAllAlarmsCallback; |
87 void RemoveAllAlarms(const std::string& extension_id); | 100 // Cancels and removes all alarms for the given extension. Invokes |callback| |
| 101 // when done. |
| 102 void RemoveAllAlarms( |
| 103 const std::string& extension_id, const RemoveAllAlarmsCallback& callback); |
88 | 104 |
89 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it. | 105 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it. |
90 void SetClockForTesting(base::Clock* clock); | 106 void SetClockForTesting(base::Clock* clock); |
91 | 107 |
92 // ProfileKeyedAPI implementation. | 108 // ProfileKeyedAPI implementation. |
93 static ProfileKeyedAPIFactory<AlarmManager>* GetFactoryInstance(); | 109 static ProfileKeyedAPIFactory<AlarmManager>* GetFactoryInstance(); |
94 | 110 |
95 // Convenience method to get the AlarmManager for a profile. | 111 // Convenience method to get the AlarmManager for a profile. |
96 static AlarmManager* Get(Profile* profile); | 112 static AlarmManager* Get(Profile* profile); |
97 | 113 |
98 private: | 114 private: |
99 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, CreateRepeating); | 115 friend void RunScheduleNextPoll(AlarmManager*); |
100 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, Clear); | |
101 friend class ExtensionAlarmsSchedulingTest; | 116 friend class ExtensionAlarmsSchedulingTest; |
102 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); | 117 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); |
103 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, | 118 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, |
104 ReleasedExtensionPollsInfrequently); | 119 ReleasedExtensionPollsInfrequently); |
105 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); | 120 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); |
106 friend class ProfileKeyedAPIFactory<AlarmManager>; | 121 friend class ProfileKeyedAPIFactory<AlarmManager>; |
107 | 122 |
108 typedef std::string ExtensionId; | 123 typedef std::string ExtensionId; |
109 typedef std::map<ExtensionId, AlarmList> AlarmMap; | 124 typedef std::map<ExtensionId, AlarmList> AlarmMap; |
110 | 125 |
| 126 typedef base::Callback<void(const std::string&)> ReadyAction; |
| 127 typedef std::queue<ReadyAction> ReadyQueue; |
| 128 typedef std::map<ExtensionId, ReadyQueue> ReadyMap; |
| 129 |
111 // Iterator used to identify a particular alarm within the Map/List pair. | 130 // Iterator used to identify a particular alarm within the Map/List pair. |
112 // "Not found" is represented by <alarms_.end(), invalid_iterator>. | 131 // "Not found" is represented by <alarms_.end(), invalid_iterator>. |
113 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; | 132 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; |
114 | 133 |
| 134 // Part of AddAlarm that is executed after alarms are loaded. |
| 135 void AddAlarmWhenReady(const Alarm& alarm, |
| 136 const AddAlarmCallback& callback, |
| 137 const std::string& extension_id); |
| 138 |
| 139 // Part of GetAlarm that is executed after alarms are loaded. |
| 140 void GetAlarmWhenReady(const std::string& name, |
| 141 const GetAlarmCallback& callback, |
| 142 const std::string& extension_id); |
| 143 |
| 144 // Part of GetAllAlarms that is executed after alarms are loaded. |
| 145 void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback, |
| 146 const std::string& extension_id); |
| 147 |
| 148 // Part of RemoveAlarm that is executed after alarms are loaded. |
| 149 void RemoveAlarmWhenReady(const std::string& name, |
| 150 const RemoveAlarmCallback& callback, |
| 151 const std::string& extension_id); |
| 152 |
| 153 // Part of RemoveAllAlarms that is executed after alarms are loaded. |
| 154 void RemoveAllAlarmsWhenReady( |
| 155 const RemoveAllAlarmsCallback& callback, const std::string& extension_id); |
| 156 |
115 // Helper to return the iterators within the AlarmMap and AlarmList for the | 157 // Helper to return the iterators within the AlarmMap and AlarmList for the |
116 // matching alarm, or an iterator to the end of the AlarmMap if none were | 158 // matching alarm, or an iterator to the end of the AlarmMap if none were |
117 // found. | 159 // found. |
118 AlarmIterator GetAlarmIterator(const std::string& extension_id, | 160 AlarmIterator GetAlarmIterator(const std::string& extension_id, |
119 const std::string& name); | 161 const std::string& name); |
120 | 162 |
121 // Helper to cancel and remove the alarm at the given iterator. The iterator | 163 // Helper to cancel and remove the alarm at the given iterator. The iterator |
122 // must be valid. | 164 // must be valid. |
123 void RemoveAlarmIterator(const AlarmIterator& iter); | 165 void RemoveAlarmIterator(const AlarmIterator& iter); |
124 | 166 |
(...skipping 10 matching lines...) Expand all Loading... |
135 scoped_ptr<base::Value> value); | 177 scoped_ptr<base::Value> value); |
136 | 178 |
137 // Schedules the next poll of alarms for when the next soonest alarm runs, | 179 // Schedules the next poll of alarms for when the next soonest alarm runs, |
138 // but not more often than the minimum granularity of all alarms. | 180 // but not more often than the minimum granularity of all alarms. |
139 void ScheduleNextPoll(); | 181 void ScheduleNextPoll(); |
140 | 182 |
141 // Polls the alarms, running any that have elapsed. After running them and | 183 // Polls the alarms, running any that have elapsed. After running them and |
142 // rescheduling repeating alarms, schedule the next poll. | 184 // rescheduling repeating alarms, schedule the next poll. |
143 void PollAlarms(); | 185 void PollAlarms(); |
144 | 186 |
| 187 // Executes |action| for given extension, making sure that the extension's |
| 188 // alarm data has been synced from the storage. |
| 189 void RunWhenReady(const std::string& extension_id, const ReadyAction& action); |
| 190 |
145 // NotificationObserver: | 191 // NotificationObserver: |
146 virtual void Observe(int type, | 192 virtual void Observe(int type, |
147 const content::NotificationSource& source, | 193 const content::NotificationSource& source, |
148 const content::NotificationDetails& details) OVERRIDE; | 194 const content::NotificationDetails& details) OVERRIDE; |
149 | 195 |
150 // ProfileKeyedAPI implementation. | 196 // ProfileKeyedAPI implementation. |
151 static const char* service_name() { | 197 static const char* service_name() { |
152 return "AlarmManager"; | 198 return "AlarmManager"; |
153 } | 199 } |
154 static const bool kServiceHasOwnInstanceInIncognito = true; | 200 static const bool kServiceHasOwnInstanceInIncognito = true; |
155 | 201 |
156 Profile* const profile_; | 202 Profile* const profile_; |
157 scoped_ptr<base::Clock> clock_; | 203 scoped_ptr<base::Clock> clock_; |
158 content::NotificationRegistrar registrar_; | 204 content::NotificationRegistrar registrar_; |
159 scoped_ptr<Delegate> delegate_; | 205 scoped_ptr<Delegate> delegate_; |
160 | 206 |
161 // The timer for this alarm manager. | 207 // The timer for this alarm manager. |
162 base::OneShotTimer<AlarmManager> timer_; | 208 base::OneShotTimer<AlarmManager> timer_; |
163 | 209 |
164 // A map of our pending alarms, per extension. | 210 // A map of our pending alarms, per extension. |
165 // Invariant: None of the AlarmLists are empty. | 211 // Invariant: None of the AlarmLists are empty. |
166 AlarmMap alarms_; | 212 AlarmMap alarms_; |
167 | 213 |
168 // The previous and next time that alarms were and will be run. | 214 // A map of actions waiting for alarm data to be synced from storage, per |
| 215 // extension. |
| 216 ReadyMap ready_actions_; |
| 217 |
| 218 // The previous time that alarms were run. |
169 base::Time last_poll_time_; | 219 base::Time last_poll_time_; |
170 base::Time next_poll_time_; | 220 |
| 221 // Next poll's time. Used only by unit tests. |
| 222 base::Time test_next_poll_time_; |
171 | 223 |
172 DISALLOW_COPY_AND_ASSIGN(AlarmManager); | 224 DISALLOW_COPY_AND_ASSIGN(AlarmManager); |
173 }; | 225 }; |
174 | 226 |
175 } // namespace extensions | 227 } // namespace extensions |
176 | 228 |
177 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 229 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
OLD | NEW |