Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / alarms / alarm_manager.h
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/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"
21
22 class Profile;
23
24 namespace base {
25 class Clock;
26 }  // namespace base
27
28 namespace content {
29 class BrowserContext;
30 }  // namespace content
31
32 namespace extensions {
33
34 class ExtensionAlarmsSchedulingTest;
35
36 struct Alarm {
37   Alarm();
38   Alarm(const std::string& name,
39         const api::alarms::AlarmCreateInfo& create_info,
40         base::TimeDelta min_granularity,
41         base::Time now);
42   ~Alarm();
43
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;
54 };
55
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> {
61  public:
62   typedef std::vector<Alarm> AlarmList;
63
64   class Delegate {
65    public:
66     virtual ~Delegate() {}
67     // Called when an alarm fires.
68     virtual void OnAlarm(const std::string& extension_id,
69                          const Alarm& alarm) = 0;
70   };
71
72   explicit AlarmManager(content::BrowserContext* context);
73   virtual ~AlarmManager();
74
75   // Override the default delegate. Callee assumes onwership. Used for testing.
76   void set_delegate(Delegate* delegate) { delegate_.reset(delegate); }
77
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,
82                 const Alarm& alarm,
83                 const AddAlarmCallback& callback);
84
85   typedef base::Callback<void(Alarm*)> GetAlarmCallback;
86   // Passes the alarm with the given name, or NULL if none exists, to
87   // |callback|.
88   void GetAlarm(const std::string& extension_id,
89                 const std::string& name,
90                 const GetAlarmCallback& callback);
91
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|.
95   void GetAllAlarms(
96       const std::string& extension_id, const GetAllAlarmsCallback& callback);
97
98   typedef base::Callback<void(bool)> RemoveAlarmCallback;
99   // Cancels and removes the alarm with the given name. Invokes |callback| when
100   // done.
101   void RemoveAlarm(const std::string& extension_id,
102                    const std::string& name,
103                    const RemoveAlarmCallback& callback);
104
105   typedef base::Callback<void()> RemoveAllAlarmsCallback;
106   // Cancels and removes all alarms for the given extension. Invokes |callback|
107   // when done.
108   void RemoveAllAlarms(
109       const std::string& extension_id, const RemoveAllAlarmsCallback& callback);
110
111   // Replaces AlarmManager's owned clock with |clock| and takes ownership of it.
112   void SetClockForTesting(base::Clock* clock);
113
114   // BrowserContextKeyedAPI implementation.
115   static BrowserContextKeyedAPIFactory<AlarmManager>* GetFactoryInstance();
116
117   // Convenience method to get the AlarmManager for a profile.
118   static AlarmManager* Get(Profile* profile);
119
120  private:
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>;
133
134   typedef std::string ExtensionId;
135   typedef std::map<ExtensionId, AlarmList> AlarmMap;
136
137   typedef base::Callback<void(const std::string&)> ReadyAction;
138   typedef std::queue<ReadyAction> ReadyQueue;
139   typedef std::map<ExtensionId, ReadyQueue> ReadyMap;
140
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;
144
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);
149
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);
154
155   // Part of GetAllAlarms that is executed after alarms are loaded.
156   void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback,
157                              const std::string& extension_id);
158
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);
163
164   // Part of RemoveAllAlarms that is executed after alarms are loaded.
165   void RemoveAllAlarmsWhenReady(
166       const RemoveAllAlarmsCallback& callback, const std::string& extension_id);
167
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
170   // found.
171   AlarmIterator GetAlarmIterator(const std::string& extension_id,
172                                  const std::string& name);
173
174   // Helper to cancel and remove the alarm at the given iterator. The iterator
175   // must be valid.
176   void RemoveAlarmIterator(const AlarmIterator& iter);
177
178   // Callback for when an alarm fires.
179   void OnAlarm(AlarmIterator iter);
180
181   // Internal helper to add an alarm and start the timer with the given delay.
182   void AddAlarmImpl(const std::string& extension_id,
183                     const Alarm& alarm);
184
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);
189
190   // Set the timer to go off at the specified |time|, and set |next_poll_time|
191   // appropriately.
192   void SetNextPollTime(const base::Time& time);
193
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();
197
198   // Polls the alarms, running any that have elapsed. After running them and
199   // rescheduling repeating alarms, schedule the next poll.
200   void PollAlarms();
201
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);
205
206   // NotificationObserver:
207   virtual void Observe(int type,
208                        const content::NotificationSource& source,
209                        const content::NotificationDetails& details) OVERRIDE;
210
211   // BrowserContextKeyedAPI implementation.
212   static const char* service_name() {
213     return "AlarmManager";
214   }
215   static const bool kServiceHasOwnInstanceInIncognito = true;
216
217   Profile* const profile_;
218   scoped_ptr<base::Clock> clock_;
219   content::NotificationRegistrar registrar_;
220   scoped_ptr<Delegate> delegate_;
221
222   // The timer for this alarm manager.
223   base::OneShotTimer<AlarmManager> timer_;
224
225   // A map of our pending alarms, per extension.
226   // Invariant: None of the AlarmLists are empty.
227   AlarmMap alarms_;
228
229   // A map of actions waiting for alarm data to be synced from storage, per
230   // extension.
231   ReadyMap ready_actions_;
232
233   // The previous time that alarms were run.
234   base::Time last_poll_time_;
235
236   // Next poll's time.
237   base::Time next_poll_time_;
238
239   DISALLOW_COPY_AND_ASSIGN(AlarmManager);
240 };
241
242 }  //  namespace extensions
243
244 #endif  // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__