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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__
13 #include "base/callback.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/timer/timer.h"
16 #include "chrome/common/extensions/api/alarms.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "extensions/browser/browser_context_keyed_api_factory.h"
20 #include "extensions/browser/extension_function.h"
30 } // namespace content
32 namespace extensions {
34 class ExtensionAlarmsSchedulingTest;
38 Alarm(const std::string& name,
39 const api::alarms::AlarmCreateInfo& create_info,
40 base::TimeDelta min_granularity,
44 linked_ptr<api::alarms::Alarm> js_alarm;
45 // The granularity isn't exposed to the extension's javascript, but we poll at
46 // least as often as the shortest alarm's granularity. It's initialized as
47 // the relative delay requested in creation, even if creation uses an absolute
48 // time. This will always be at least as large as the min_granularity
49 // constructor argument.
50 base::TimeDelta granularity;
51 // The minimum granularity is the minimum allowed polling rate. This stops
52 // alarms from polling too often.
53 base::TimeDelta minimum_granularity;
56 // Manages the currently pending alarms for every extension in a profile.
57 // There is one manager per virtual Profile.
58 class AlarmManager : public BrowserContextKeyedAPI,
59 public content::NotificationObserver,
60 public base::SupportsWeakPtr<AlarmManager> {
62 typedef std::vector<Alarm> AlarmList;
66 virtual ~Delegate() {}
67 // Called when an alarm fires.
68 virtual void OnAlarm(const std::string& extension_id,
69 const Alarm& alarm) = 0;
72 explicit AlarmManager(content::BrowserContext* context);
73 virtual ~AlarmManager();
75 // Override the default delegate. Callee assumes onwership. Used for testing.
76 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); }
78 typedef base::Callback<void()> AddAlarmCallback;
79 // Adds |alarm| for the given extension, and starts the timer. Invokes
80 // |callback| when done.
81 void AddAlarm(const std::string& extension_id,
83 const AddAlarmCallback& callback);
85 typedef base::Callback<void(Alarm*)> GetAlarmCallback;
86 // Passes the alarm with the given name, or NULL if none exists, to
88 void GetAlarm(const std::string& extension_id,
89 const std::string& name,
90 const GetAlarmCallback& callback);
92 typedef base::Callback<void(const AlarmList*)> GetAllAlarmsCallback;
93 // Passes the list of pending alarms for the given extension, or
94 // NULL if none exist, to |callback|.
96 const std::string& extension_id, const GetAllAlarmsCallback& callback);
98 typedef base::Callback<void(bool)> RemoveAlarmCallback;
99 // Cancels and removes the alarm with the given name. Invokes |callback| when
101 void RemoveAlarm(const std::string& extension_id,
102 const std::string& name,
103 const RemoveAlarmCallback& callback);
105 typedef base::Callback<void()> RemoveAllAlarmsCallback;
106 // Cancels and removes all alarms for the given extension. Invokes |callback|
108 void RemoveAllAlarms(
109 const std::string& extension_id, const RemoveAllAlarmsCallback& callback);
111 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it.
112 void SetClockForTesting(base::Clock* clock);
114 // BrowserContextKeyedAPI implementation.
115 static BrowserContextKeyedAPIFactory<AlarmManager>* GetFactoryInstance();
117 // Convenience method to get the AlarmManager for a profile.
118 static AlarmManager* Get(Profile* profile);
121 friend void RunScheduleNextPoll(AlarmManager*);
122 friend class ExtensionAlarmsSchedulingTest;
123 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling);
124 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
125 ReleasedExtensionPollsInfrequently);
126 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning);
127 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, MinimumGranularity);
128 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
129 DifferentMinimumGranularities);
130 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
131 RepeatingAlarmsScheduledPredictably);
132 friend class BrowserContextKeyedAPIFactory<AlarmManager>;
134 typedef std::string ExtensionId;
135 typedef std::map<ExtensionId, AlarmList> AlarmMap;
137 typedef base::Callback<void(const std::string&)> ReadyAction;
138 typedef std::queue<ReadyAction> ReadyQueue;
139 typedef std::map<ExtensionId, ReadyQueue> ReadyMap;
141 // Iterator used to identify a particular alarm within the Map/List pair.
142 // "Not found" is represented by <alarms_.end(), invalid_iterator>.
143 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator;
145 // Part of AddAlarm that is executed after alarms are loaded.
146 void AddAlarmWhenReady(const Alarm& alarm,
147 const AddAlarmCallback& callback,
148 const std::string& extension_id);
150 // Part of GetAlarm that is executed after alarms are loaded.
151 void GetAlarmWhenReady(const std::string& name,
152 const GetAlarmCallback& callback,
153 const std::string& extension_id);
155 // Part of GetAllAlarms that is executed after alarms are loaded.
156 void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback,
157 const std::string& extension_id);
159 // Part of RemoveAlarm that is executed after alarms are loaded.
160 void RemoveAlarmWhenReady(const std::string& name,
161 const RemoveAlarmCallback& callback,
162 const std::string& extension_id);
164 // Part of RemoveAllAlarms that is executed after alarms are loaded.
165 void RemoveAllAlarmsWhenReady(
166 const RemoveAllAlarmsCallback& callback, const std::string& extension_id);
168 // Helper to return the iterators within the AlarmMap and AlarmList for the
169 // matching alarm, or an iterator to the end of the AlarmMap if none were
171 AlarmIterator GetAlarmIterator(const std::string& extension_id,
172 const std::string& name);
174 // Helper to cancel and remove the alarm at the given iterator. The iterator
176 void RemoveAlarmIterator(const AlarmIterator& iter);
178 // Callback for when an alarm fires.
179 void OnAlarm(AlarmIterator iter);
181 // Internal helper to add an alarm and start the timer with the given delay.
182 void AddAlarmImpl(const std::string& extension_id,
185 // Syncs our alarm data for the given extension to/from the state storage.
186 void WriteToStorage(const std::string& extension_id);
187 void ReadFromStorage(const std::string& extension_id,
188 scoped_ptr<base::Value> value);
190 // Set the timer to go off at the specified |time|, and set |next_poll_time|
192 void SetNextPollTime(const base::Time& time);
194 // Schedules the next poll of alarms for when the next soonest alarm runs,
195 // but not more often than the minimum granularity of all alarms.
196 void ScheduleNextPoll();
198 // Polls the alarms, running any that have elapsed. After running them and
199 // rescheduling repeating alarms, schedule the next poll.
202 // Executes |action| for given extension, making sure that the extension's
203 // alarm data has been synced from the storage.
204 void RunWhenReady(const std::string& extension_id, const ReadyAction& action);
206 // NotificationObserver:
207 virtual void Observe(int type,
208 const content::NotificationSource& source,
209 const content::NotificationDetails& details) OVERRIDE;
211 // BrowserContextKeyedAPI implementation.
212 static const char* service_name() {
213 return "AlarmManager";
215 static const bool kServiceHasOwnInstanceInIncognito = true;
217 Profile* const profile_;
218 scoped_ptr<base::Clock> clock_;
219 content::NotificationRegistrar registrar_;
220 scoped_ptr<Delegate> delegate_;
222 // The timer for this alarm manager.
223 base::OneShotTimer<AlarmManager> timer_;
225 // A map of our pending alarms, per extension.
226 // Invariant: None of the AlarmLists are empty.
229 // A map of actions waiting for alarm data to be synced from storage, per
231 ReadyMap ready_actions_;
233 // The previous time that alarms were run.
234 base::Time last_poll_time_;
237 base::Time next_poll_time_;
239 DISALLOW_COPY_AND_ASSIGN(AlarmManager);
242 } // namespace extensions
244 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__