Upstream version 5.34.104.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/browser/extensions/api/profile_keyed_api_factory.h"
17 #include "chrome/common/extensions/api/alarms.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.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 extensions {
29
30 class ExtensionAlarmsSchedulingTest;
31
32 struct Alarm {
33   Alarm();
34   Alarm(const std::string& name,
35         const api::alarms::AlarmCreateInfo& create_info,
36         base::TimeDelta min_granularity,
37         base::Time now);
38   ~Alarm();
39
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;
50 };
51
52 // Manages the currently pending alarms for every extension in a profile.
53 // There is one manager per virtual Profile.
54 class AlarmManager
55     : public ProfileKeyedAPI,
56       public content::NotificationObserver,
57       public base::SupportsWeakPtr<AlarmManager> {
58  public:
59   typedef std::vector<Alarm> AlarmList;
60
61   class Delegate {
62    public:
63     virtual ~Delegate() {}
64     // Called when an alarm fires.
65     virtual void OnAlarm(const std::string& extension_id,
66                          const Alarm& alarm) = 0;
67   };
68
69   explicit AlarmManager(Profile* profile);
70   virtual ~AlarmManager();
71
72   // Override the default delegate. Callee assumes onwership. Used for testing.
73   void set_delegate(Delegate* delegate) { delegate_.reset(delegate); }
74
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,
79                 const Alarm& alarm,
80                 const AddAlarmCallback& callback);
81
82   typedef base::Callback<void(Alarm*)> GetAlarmCallback;
83   // Passes the alarm with the given name, or NULL if none exists, to
84   // |callback|.
85   void GetAlarm(const std::string& extension_id,
86                 const std::string& name,
87                 const GetAlarmCallback& callback);
88
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|.
92   void GetAllAlarms(
93       const std::string& extension_id, const GetAllAlarmsCallback& callback);
94
95   typedef base::Callback<void(bool)> RemoveAlarmCallback;
96   // Cancels and removes the alarm with the given name. Invokes |callback| when
97   // done.
98   void RemoveAlarm(const std::string& extension_id,
99                    const std::string& name,
100                    const RemoveAlarmCallback& callback);
101
102   typedef base::Callback<void()> RemoveAllAlarmsCallback;
103   // Cancels and removes all alarms for the given extension. Invokes |callback|
104   // when done.
105   void RemoveAllAlarms(
106       const std::string& extension_id, const RemoveAllAlarmsCallback& callback);
107
108   // Replaces AlarmManager's owned clock with |clock| and takes ownership of it.
109   void SetClockForTesting(base::Clock* clock);
110
111   // ProfileKeyedAPI implementation.
112   static ProfileKeyedAPIFactory<AlarmManager>* GetFactoryInstance();
113
114   // Convenience method to get the AlarmManager for a profile.
115   static AlarmManager* Get(Profile* profile);
116
117  private:
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_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
126                            DifferentMinimumGranularities);
127   FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
128                            RepeatingAlarmsScheduledPredictably);
129   friend class ProfileKeyedAPIFactory<AlarmManager>;
130
131   typedef std::string ExtensionId;
132   typedef std::map<ExtensionId, AlarmList> AlarmMap;
133
134   typedef base::Callback<void(const std::string&)> ReadyAction;
135   typedef std::queue<ReadyAction> ReadyQueue;
136   typedef std::map<ExtensionId, ReadyQueue> ReadyMap;
137
138   // Iterator used to identify a particular alarm within the Map/List pair.
139   // "Not found" is represented by <alarms_.end(), invalid_iterator>.
140   typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator;
141
142   // Part of AddAlarm that is executed after alarms are loaded.
143   void AddAlarmWhenReady(const Alarm& alarm,
144                          const AddAlarmCallback& callback,
145                          const std::string& extension_id);
146
147   // Part of GetAlarm that is executed after alarms are loaded.
148   void GetAlarmWhenReady(const std::string& name,
149                          const GetAlarmCallback& callback,
150                          const std::string& extension_id);
151
152   // Part of GetAllAlarms that is executed after alarms are loaded.
153   void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback,
154                              const std::string& extension_id);
155
156   // Part of RemoveAlarm that is executed after alarms are loaded.
157   void RemoveAlarmWhenReady(const std::string& name,
158                             const RemoveAlarmCallback& callback,
159                             const std::string& extension_id);
160
161   // Part of RemoveAllAlarms that is executed after alarms are loaded.
162   void RemoveAllAlarmsWhenReady(
163       const RemoveAllAlarmsCallback& callback, const std::string& extension_id);
164
165   // Helper to return the iterators within the AlarmMap and AlarmList for the
166   // matching alarm, or an iterator to the end of the AlarmMap if none were
167   // found.
168   AlarmIterator GetAlarmIterator(const std::string& extension_id,
169                                  const std::string& name);
170
171   // Helper to cancel and remove the alarm at the given iterator. The iterator
172   // must be valid.
173   void RemoveAlarmIterator(const AlarmIterator& iter);
174
175   // Callback for when an alarm fires.
176   void OnAlarm(AlarmIterator iter);
177
178   // Internal helper to add an alarm and start the timer with the given delay.
179   void AddAlarmImpl(const std::string& extension_id,
180                     const Alarm& alarm);
181
182   // Syncs our alarm data for the given extension to/from the state storage.
183   void WriteToStorage(const std::string& extension_id);
184   void ReadFromStorage(const std::string& extension_id,
185                        scoped_ptr<base::Value> value);
186
187   // Set the timer to go off at the specified |time|, and set |next_poll_time|
188   // appropriately.
189   void SetNextPollTime(const base::Time& time);
190
191   // Schedules the next poll of alarms for when the next soonest alarm runs,
192   // but not more often than the minimum granularity of all alarms.
193   void ScheduleNextPoll();
194
195   // Polls the alarms, running any that have elapsed. After running them and
196   // rescheduling repeating alarms, schedule the next poll.
197   void PollAlarms();
198
199   // Executes |action| for given extension, making sure that the extension's
200   // alarm data has been synced from the storage.
201   void RunWhenReady(const std::string& extension_id, const ReadyAction& action);
202
203   // NotificationObserver:
204   virtual void Observe(int type,
205                        const content::NotificationSource& source,
206                        const content::NotificationDetails& details) OVERRIDE;
207
208   // ProfileKeyedAPI implementation.
209   static const char* service_name() {
210     return "AlarmManager";
211   }
212   static const bool kServiceHasOwnInstanceInIncognito = true;
213
214   Profile* const profile_;
215   scoped_ptr<base::Clock> clock_;
216   content::NotificationRegistrar registrar_;
217   scoped_ptr<Delegate> delegate_;
218
219   // The timer for this alarm manager.
220   base::OneShotTimer<AlarmManager> timer_;
221
222   // A map of our pending alarms, per extension.
223   // Invariant: None of the AlarmLists are empty.
224   AlarmMap alarms_;
225
226   // A map of actions waiting for alarm data to be synced from storage, per
227   // extension.
228   ReadyMap ready_actions_;
229
230   // The previous time that alarms were run.
231   base::Time last_poll_time_;
232
233   // Next poll's time.
234   base::Time next_poll_time_;
235
236   DISALLOW_COPY_AND_ASSIGN(AlarmManager);
237 };
238
239 }  //  namespace extensions
240
241 #endif  // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__