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/browser/extensions/api/profile_keyed_api_factory.h"
17 #include "chrome/browser/extensions/extension_function.h"
18 #include "chrome/common/extensions/api/alarms.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
28 namespace extensions {
30 class ExtensionAlarmsSchedulingTest;
34 Alarm(const std::string& name,
35 const api::alarms::AlarmCreateInfo& create_info,
36 base::TimeDelta min_granularity,
40 linked_ptr<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 // The minimum granularity is the minimum allowed polling rate. This stops
48 // alarms from polling too often.
49 base::TimeDelta minimum_granularity;
52 // Manages the currently pending alarms for every extension in a profile.
53 // There is one manager per virtual Profile.
55 : public ProfileKeyedAPI,
56 public content::NotificationObserver,
57 public base::SupportsWeakPtr<AlarmManager> {
59 typedef std::vector<Alarm> AlarmList;
63 virtual ~Delegate() {}
64 // Called when an alarm fires.
65 virtual void OnAlarm(const std::string& extension_id,
66 const Alarm& alarm) = 0;
69 explicit AlarmManager(Profile* profile);
70 virtual ~AlarmManager();
72 // Override the default delegate. Callee assumes onwership. Used for testing.
73 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); }
75 typedef base::Callback<void()> AddAlarmCallback;
76 // Adds |alarm| for the given extension, and starts the timer. Invokes
77 // |callback| when done.
78 void AddAlarm(const std::string& extension_id,
80 const AddAlarmCallback& callback);
82 typedef base::Callback<void(Alarm*)> GetAlarmCallback;
83 // Passes the alarm with the given name, or NULL if none exists, to
85 void GetAlarm(const std::string& extension_id,
86 const std::string& name,
87 const GetAlarmCallback& callback);
89 typedef base::Callback<void(const AlarmList*)> GetAllAlarmsCallback;
90 // Passes the list of pending alarms for the given extension, or
91 // NULL if none exist, to |callback|.
93 const std::string& extension_id, const GetAllAlarmsCallback& callback);
95 typedef base::Callback<void(bool)> RemoveAlarmCallback;
96 // Cancels and removes the alarm with the given name. Invokes |callback| when
98 void RemoveAlarm(const std::string& extension_id,
99 const std::string& name,
100 const RemoveAlarmCallback& callback);
102 typedef base::Callback<void()> RemoveAllAlarmsCallback;
103 // Cancels and removes all alarms for the given extension. Invokes |callback|
105 void RemoveAllAlarms(
106 const std::string& extension_id, const RemoveAllAlarmsCallback& callback);
108 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it.
109 void SetClockForTesting(base::Clock* clock);
111 // ProfileKeyedAPI implementation.
112 static ProfileKeyedAPIFactory<AlarmManager>* GetFactoryInstance();
114 // Convenience method to get the AlarmManager for a profile.
115 static AlarmManager* Get(Profile* profile);
118 friend void RunScheduleNextPoll(AlarmManager*);
119 friend class ExtensionAlarmsSchedulingTest;
120 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling);
121 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
122 ReleasedExtensionPollsInfrequently);
123 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning);
124 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, MinimumGranularity);
125 friend class ProfileKeyedAPIFactory<AlarmManager>;
127 typedef std::string ExtensionId;
128 typedef std::map<ExtensionId, AlarmList> AlarmMap;
130 typedef base::Callback<void(const std::string&)> ReadyAction;
131 typedef std::queue<ReadyAction> ReadyQueue;
132 typedef std::map<ExtensionId, ReadyQueue> ReadyMap;
134 // Iterator used to identify a particular alarm within the Map/List pair.
135 // "Not found" is represented by <alarms_.end(), invalid_iterator>.
136 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator;
138 // Part of AddAlarm that is executed after alarms are loaded.
139 void AddAlarmWhenReady(const Alarm& alarm,
140 const AddAlarmCallback& callback,
141 const std::string& extension_id);
143 // Part of GetAlarm that is executed after alarms are loaded.
144 void GetAlarmWhenReady(const std::string& name,
145 const GetAlarmCallback& callback,
146 const std::string& extension_id);
148 // Part of GetAllAlarms that is executed after alarms are loaded.
149 void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback,
150 const std::string& extension_id);
152 // Part of RemoveAlarm that is executed after alarms are loaded.
153 void RemoveAlarmWhenReady(const std::string& name,
154 const RemoveAlarmCallback& callback,
155 const std::string& extension_id);
157 // Part of RemoveAllAlarms that is executed after alarms are loaded.
158 void RemoveAllAlarmsWhenReady(
159 const RemoveAllAlarmsCallback& callback, const std::string& extension_id);
161 // Helper to return the iterators within the AlarmMap and AlarmList for the
162 // matching alarm, or an iterator to the end of the AlarmMap if none were
164 AlarmIterator GetAlarmIterator(const std::string& extension_id,
165 const std::string& name);
167 // Helper to cancel and remove the alarm at the given iterator. The iterator
169 void RemoveAlarmIterator(const AlarmIterator& iter);
171 // Callback for when an alarm fires.
172 void OnAlarm(AlarmIterator iter);
174 // Internal helper to add an alarm and start the timer with the given delay.
175 void AddAlarmImpl(const std::string& extension_id,
178 // Syncs our alarm data for the given extension to/from the state storage.
179 void WriteToStorage(const std::string& extension_id);
180 void ReadFromStorage(const std::string& extension_id,
181 scoped_ptr<base::Value> value);
183 // Schedules the next poll of alarms for when the next soonest alarm runs,
184 // but not more often than the minimum granularity of all alarms.
185 void ScheduleNextPoll();
187 // Polls the alarms, running any that have elapsed. After running them and
188 // rescheduling repeating alarms, schedule the next poll.
191 // Executes |action| for given extension, making sure that the extension's
192 // alarm data has been synced from the storage.
193 void RunWhenReady(const std::string& extension_id, const ReadyAction& action);
195 // NotificationObserver:
196 virtual void Observe(int type,
197 const content::NotificationSource& source,
198 const content::NotificationDetails& details) OVERRIDE;
200 // ProfileKeyedAPI implementation.
201 static const char* service_name() {
202 return "AlarmManager";
204 static const bool kServiceHasOwnInstanceInIncognito = true;
206 Profile* const profile_;
207 scoped_ptr<base::Clock> clock_;
208 content::NotificationRegistrar registrar_;
209 scoped_ptr<Delegate> delegate_;
211 // The timer for this alarm manager.
212 base::OneShotTimer<AlarmManager> timer_;
214 // A map of our pending alarms, per extension.
215 // Invariant: None of the AlarmLists are empty.
218 // A map of actions waiting for alarm data to be synced from storage, per
220 ReadyMap ready_actions_;
222 // The previous time that alarms were run.
223 base::Time last_poll_time_;
225 // Next poll's time. Used only by unit tests.
226 base::Time test_next_poll_time_;
228 DISALLOW_COPY_AND_ASSIGN(AlarmManager);
231 } // namespace extensions
233 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__