Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Side by Side Diff: chrome/browser/extensions/api/alarms/alarm_manager.h

Issue 789643004: Move chrome.alarms API from chrome/ to extensions/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__
7
8 #include <map>
9 #include <queue>
10 #include <string>
11 #include <vector>
12
13 #include "base/callback.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/scoped_observer.h"
17 #include "base/timer/timer.h"
18 #include "chrome/common/extensions/api/alarms.h"
19 #include "extensions/browser/browser_context_keyed_api_factory.h"
20 #include "extensions/browser/extension_registry_observer.h"
21
22 namespace base {
23 class Clock;
24 } // namespace base
25
26 namespace content {
27 class BrowserContext;
28 } // namespace content
29
30 namespace extensions {
31 class ExtensionAlarmsSchedulingTest;
32 class ExtensionRegistry;
33
34 struct Alarm {
35 Alarm();
36 Alarm(const std::string& name,
37 const api::alarms::AlarmCreateInfo& create_info,
38 base::TimeDelta min_granularity,
39 base::Time now);
40 ~Alarm();
41
42 linked_ptr<api::alarms::Alarm> js_alarm;
43 // The granularity isn't exposed to the extension's javascript, but we poll at
44 // least as often as the shortest alarm's granularity. It's initialized as
45 // the relative delay requested in creation, even if creation uses an absolute
46 // time. This will always be at least as large as the min_granularity
47 // constructor argument.
48 base::TimeDelta granularity;
49 // The minimum granularity is the minimum allowed polling rate. This stops
50 // alarms from polling too often.
51 base::TimeDelta minimum_granularity;
52 };
53
54 // Manages the currently pending alarms for every extension in a profile.
55 // There is one manager per virtual Profile.
56 class AlarmManager : public BrowserContextKeyedAPI,
57 public ExtensionRegistryObserver,
58 public base::SupportsWeakPtr<AlarmManager> {
59 public:
60 typedef std::vector<Alarm> AlarmList;
61
62 class Delegate {
63 public:
64 virtual ~Delegate() {}
65 // Called when an alarm fires.
66 virtual void OnAlarm(const std::string& extension_id,
67 const Alarm& alarm) = 0;
68 };
69
70 explicit AlarmManager(content::BrowserContext* context);
71 ~AlarmManager() override;
72
73 // Override the default delegate. Callee assumes onwership. Used for testing.
74 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); }
75
76 typedef base::Callback<void()> AddAlarmCallback;
77 // Adds |alarm| for the given extension, and starts the timer. Invokes
78 // |callback| when done.
79 void AddAlarm(const std::string& extension_id,
80 const Alarm& alarm,
81 const AddAlarmCallback& callback);
82
83 typedef base::Callback<void(Alarm*)> GetAlarmCallback;
84 // Passes the alarm with the given name, or NULL if none exists, to
85 // |callback|.
86 void GetAlarm(const std::string& extension_id,
87 const std::string& name,
88 const GetAlarmCallback& callback);
89
90 typedef base::Callback<void(const AlarmList*)> GetAllAlarmsCallback;
91 // Passes the list of pending alarms for the given extension, or
92 // NULL if none exist, to |callback|.
93 void GetAllAlarms(
94 const std::string& extension_id, const GetAllAlarmsCallback& callback);
95
96 typedef base::Callback<void(bool)> RemoveAlarmCallback;
97 // Cancels and removes the alarm with the given name. Invokes |callback| when
98 // done.
99 void RemoveAlarm(const std::string& extension_id,
100 const std::string& name,
101 const RemoveAlarmCallback& callback);
102
103 typedef base::Callback<void()> RemoveAllAlarmsCallback;
104 // Cancels and removes all alarms for the given extension. Invokes |callback|
105 // when done.
106 void RemoveAllAlarms(
107 const std::string& extension_id, const RemoveAllAlarmsCallback& callback);
108
109 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it.
110 void SetClockForTesting(base::Clock* clock);
111
112 // BrowserContextKeyedAPI implementation.
113 static BrowserContextKeyedAPIFactory<AlarmManager>* GetFactoryInstance();
114
115 // Convenience method to get the AlarmManager for a content::BrowserContext.
116 static AlarmManager* Get(content::BrowserContext* browser_context);
117
118 private:
119 friend void RunScheduleNextPoll(AlarmManager*);
120 friend class ExtensionAlarmsSchedulingTest;
121 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling);
122 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
123 ReleasedExtensionPollsInfrequently);
124 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning);
125 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, MinimumGranularity);
126 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
127 DifferentMinimumGranularities);
128 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
129 RepeatingAlarmsScheduledPredictably);
130 friend class BrowserContextKeyedAPIFactory<AlarmManager>;
131
132 typedef std::string ExtensionId;
133 typedef std::map<ExtensionId, AlarmList> AlarmMap;
134
135 typedef base::Callback<void(const std::string&)> ReadyAction;
136 typedef std::queue<ReadyAction> ReadyQueue;
137 typedef std::map<ExtensionId, ReadyQueue> ReadyMap;
138
139 // Iterator used to identify a particular alarm within the Map/List pair.
140 // "Not found" is represented by <alarms_.end(), invalid_iterator>.
141 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator;
142
143 // Part of AddAlarm that is executed after alarms are loaded.
144 void AddAlarmWhenReady(const Alarm& alarm,
145 const AddAlarmCallback& callback,
146 const std::string& extension_id);
147
148 // Part of GetAlarm that is executed after alarms are loaded.
149 void GetAlarmWhenReady(const std::string& name,
150 const GetAlarmCallback& callback,
151 const std::string& extension_id);
152
153 // Part of GetAllAlarms that is executed after alarms are loaded.
154 void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback,
155 const std::string& extension_id);
156
157 // Part of RemoveAlarm that is executed after alarms are loaded.
158 void RemoveAlarmWhenReady(const std::string& name,
159 const RemoveAlarmCallback& callback,
160 const std::string& extension_id);
161
162 // Part of RemoveAllAlarms that is executed after alarms are loaded.
163 void RemoveAllAlarmsWhenReady(
164 const RemoveAllAlarmsCallback& callback, const std::string& extension_id);
165
166 // Helper to return the iterators within the AlarmMap and AlarmList for the
167 // matching alarm, or an iterator to the end of the AlarmMap if none were
168 // found.
169 AlarmIterator GetAlarmIterator(const std::string& extension_id,
170 const std::string& name);
171
172 // Helper to cancel and remove the alarm at the given iterator. The iterator
173 // must be valid.
174 void RemoveAlarmIterator(const AlarmIterator& iter);
175
176 // Callback for when an alarm fires.
177 void OnAlarm(AlarmIterator iter);
178
179 // Internal helper to add an alarm and start the timer with the given delay.
180 void AddAlarmImpl(const std::string& extension_id,
181 const Alarm& alarm);
182
183 // Syncs our alarm data for the given extension to/from the state storage.
184 void WriteToStorage(const std::string& extension_id);
185 void ReadFromStorage(const std::string& extension_id,
186 scoped_ptr<base::Value> value);
187
188 // Set the timer to go off at the specified |time|, and set |next_poll_time|
189 // appropriately.
190 void SetNextPollTime(const base::Time& time);
191
192 // Schedules the next poll of alarms for when the next soonest alarm runs,
193 // but not more often than the minimum granularity of all alarms.
194 void ScheduleNextPoll();
195
196 // Polls the alarms, running any that have elapsed. After running them and
197 // rescheduling repeating alarms, schedule the next poll.
198 void PollAlarms();
199
200 // Executes |action| for given extension, making sure that the extension's
201 // alarm data has been synced from the storage.
202 void RunWhenReady(const std::string& extension_id, const ReadyAction& action);
203
204 // ExtensionRegistryObserver implementation.
205 void OnExtensionLoaded(content::BrowserContext* browser_context,
206 const Extension* extension) override;
207 void OnExtensionUninstalled(content::BrowserContext* browser_context,
208 const Extension* extension,
209 extensions::UninstallReason reason) override;
210
211 // BrowserContextKeyedAPI implementation.
212 static const char* service_name() {
213 return "AlarmManager";
214 }
215 static const bool kServiceHasOwnInstanceInIncognito = true;
216
217 content::BrowserContext* const browser_context_;
218 scoped_ptr<base::Clock> clock_;
219 scoped_ptr<Delegate> delegate_;
220
221 // Listen to extension load notifications.
222 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
223 extension_registry_observer_;
224
225 // The timer for this alarm manager.
226 base::OneShotTimer<AlarmManager> timer_;
227
228 // A map of our pending alarms, per extension.
229 // Invariant: None of the AlarmLists are empty.
230 AlarmMap alarms_;
231
232 // A map of actions waiting for alarm data to be synced from storage, per
233 // extension.
234 ReadyMap ready_actions_;
235
236 // The previous time that alarms were run.
237 base::Time last_poll_time_;
238
239 // Next poll's time.
240 base::Time next_poll_time_;
241
242 DISALLOW_COPY_AND_ASSIGN(AlarmManager);
243 };
244
245 } // namespace extensions
246
247 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/alarms/OWNERS ('k') | chrome/browser/extensions/api/alarms/alarm_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698