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
22class Profile;
23
24namespace base {
25class Clock;
26}  // namespace base
27
28namespace extensions {
29
30class ExtensionAlarmsSchedulingTest;
31
32struct 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.
54class 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 class ProfileKeyedAPIFactory<AlarmManager>;
126
127  typedef std::string ExtensionId;
128  typedef std::map<ExtensionId, AlarmList> AlarmMap;
129
130  typedef base::Callback<void(const std::string&)> ReadyAction;
131  typedef std::queue<ReadyAction> ReadyQueue;
132  typedef std::map<ExtensionId, ReadyQueue> ReadyMap;
133
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;
137
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);
142
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);
147
148  // Part of GetAllAlarms that is executed after alarms are loaded.
149  void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback,
150                             const std::string& extension_id);
151
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);
156
157  // Part of RemoveAllAlarms that is executed after alarms are loaded.
158  void RemoveAllAlarmsWhenReady(
159      const RemoveAllAlarmsCallback& callback, const std::string& extension_id);
160
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
163  // found.
164  AlarmIterator GetAlarmIterator(const std::string& extension_id,
165                                 const std::string& name);
166
167  // Helper to cancel and remove the alarm at the given iterator. The iterator
168  // must be valid.
169  void RemoveAlarmIterator(const AlarmIterator& iter);
170
171  // Callback for when an alarm fires.
172  void OnAlarm(AlarmIterator iter);
173
174  // Internal helper to add an alarm and start the timer with the given delay.
175  void AddAlarmImpl(const std::string& extension_id,
176                    const Alarm& alarm);
177
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);
182
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();
186
187  // Polls the alarms, running any that have elapsed. After running them and
188  // rescheduling repeating alarms, schedule the next poll.
189  void PollAlarms();
190
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);
194
195  // NotificationObserver:
196  virtual void Observe(int type,
197                       const content::NotificationSource& source,
198                       const content::NotificationDetails& details) OVERRIDE;
199
200  // ProfileKeyedAPI implementation.
201  static const char* service_name() {
202    return "AlarmManager";
203  }
204  static const bool kServiceHasOwnInstanceInIncognito = true;
205
206  Profile* const profile_;
207  scoped_ptr<base::Clock> clock_;
208  content::NotificationRegistrar registrar_;
209  scoped_ptr<Delegate> delegate_;
210
211  // The timer for this alarm manager.
212  base::OneShotTimer<AlarmManager> timer_;
213
214  // A map of our pending alarms, per extension.
215  // Invariant: None of the AlarmLists are empty.
216  AlarmMap alarms_;
217
218  // A map of actions waiting for alarm data to be synced from storage, per
219  // extension.
220  ReadyMap ready_actions_;
221
222  // The previous time that alarms were run.
223  base::Time last_poll_time_;
224
225  // Next poll's time. Used only by unit tests.
226  base::Time test_next_poll_time_;
227
228  DISALLOW_COPY_AND_ASSIGN(AlarmManager);
229};
230
231}  //  namespace extensions
232
233#endif  // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__
234