OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
6 #define CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 6 #define CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <map> | 9 #include <map> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/timer.h" | 13 #include "base/timer.h" |
14 #include "chrome/browser/extensions/extension_function.h" | 14 #include "chrome/browser/extensions/extension_function.h" |
15 #include "chrome/common/extensions/api/alarms.h" | 15 #include "chrome/common/extensions/api/alarms.h" |
16 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
17 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
18 | 18 |
19 class Profile; | 19 class Profile; |
20 | 20 |
| 21 namespace base { |
| 22 class Clock; |
| 23 } // namespace base |
| 24 |
21 namespace extensions { | 25 namespace extensions { |
22 | 26 |
23 class ExtensionAlarmsSchedulingTest; | 27 class ExtensionAlarmsSchedulingTest; |
24 | 28 |
25 struct Alarm { | 29 struct Alarm { |
26 typedef base::Time (*TimeProvider)(); | |
27 | |
28 Alarm(); | 30 Alarm(); |
29 Alarm(const std::string& name, | 31 Alarm(const std::string& name, |
30 const api::alarms::AlarmCreateInfo& create_info, | 32 const api::alarms::AlarmCreateInfo& create_info, |
31 base::TimeDelta min_granularity, | 33 base::TimeDelta min_granularity, |
32 TimeProvider now); | 34 base::Time now); |
33 ~Alarm(); | 35 ~Alarm(); |
34 | 36 |
35 linked_ptr<api::alarms::Alarm> js_alarm; | 37 linked_ptr<api::alarms::Alarm> js_alarm; |
36 // The granularity isn't exposed to the extension's javascript, but we poll at | 38 // The granularity isn't exposed to the extension's javascript, but we poll at |
37 // least as often as the shortest alarm's granularity. It's initialized as | 39 // least as often as the shortest alarm's granularity. It's initialized as |
38 // the relative delay requested in creation, even if creation uses an absolute | 40 // the relative delay requested in creation, even if creation uses an absolute |
39 // time. This will always be at least as large as the min_granularity | 41 // time. This will always be at least as large as the min_granularity |
40 // constructor argument. | 42 // constructor argument. |
41 base::TimeDelta granularity; | 43 base::TimeDelta granularity; |
42 }; | 44 }; |
43 | 45 |
44 // Manages the currently pending alarms for every extension in a profile. | 46 // Manages the currently pending alarms for every extension in a profile. |
45 // There is one manager per virtual Profile. | 47 // There is one manager per virtual Profile. |
46 class AlarmManager | 48 class AlarmManager |
47 : public content::NotificationObserver, | 49 : public content::NotificationObserver, |
48 public base::SupportsWeakPtr<AlarmManager> { | 50 public base::SupportsWeakPtr<AlarmManager> { |
49 public: | 51 public: |
50 typedef base::Time (*TimeProvider)(); | |
51 typedef std::vector<Alarm> AlarmList; | 52 typedef std::vector<Alarm> AlarmList; |
52 | 53 |
53 class Delegate { | 54 class Delegate { |
54 public: | 55 public: |
55 virtual ~Delegate() {} | 56 virtual ~Delegate() {} |
56 // Called when an alarm fires. | 57 // Called when an alarm fires. |
57 virtual void OnAlarm(const std::string& extension_id, | 58 virtual void OnAlarm(const std::string& extension_id, |
58 const Alarm& alarm) = 0; | 59 const Alarm& alarm) = 0; |
59 }; | 60 }; |
60 | 61 |
61 // 'now' is usually &base::Time::Now. | 62 // |clock| is usually a base::DefaultClock, but can be something |
62 explicit AlarmManager(Profile* profile, TimeProvider now); | 63 // else for testing. |
| 64 explicit AlarmManager(Profile* profile, base::Clock* clock); |
63 virtual ~AlarmManager(); | 65 virtual ~AlarmManager(); |
64 | 66 |
65 // Override the default delegate. Callee assumes onwership. Used for testing. | 67 // Override the default delegate. Callee assumes onwership. Used for testing. |
66 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); } | 68 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); } |
67 | 69 |
68 // Adds |alarm| for the given extension, and starts the timer. | 70 // Adds |alarm| for the given extension, and starts the timer. |
69 void AddAlarm(const std::string& extension_id, | 71 void AddAlarm(const std::string& extension_id, |
70 const Alarm& alarm); | 72 const Alarm& alarm); |
71 | 73 |
72 // Returns the alarm with the given name, or NULL if none exists. | 74 // Returns the alarm with the given name, or NULL if none exists. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 130 |
129 // Polls the alarms, running any that have elapsed. After running them and | 131 // Polls the alarms, running any that have elapsed. After running them and |
130 // rescheduling repeating alarms, schedule the next poll. | 132 // rescheduling repeating alarms, schedule the next poll. |
131 void PollAlarms(); | 133 void PollAlarms(); |
132 | 134 |
133 // NotificationObserver: | 135 // NotificationObserver: |
134 virtual void Observe(int type, | 136 virtual void Observe(int type, |
135 const content::NotificationSource& source, | 137 const content::NotificationSource& source, |
136 const content::NotificationDetails& details) OVERRIDE; | 138 const content::NotificationDetails& details) OVERRIDE; |
137 | 139 |
138 Profile* profile_; | 140 Profile* const profile_; |
139 const TimeProvider now_; | 141 base::Clock* const clock_; |
140 content::NotificationRegistrar registrar_; | 142 content::NotificationRegistrar registrar_; |
141 scoped_ptr<Delegate> delegate_; | 143 scoped_ptr<Delegate> delegate_; |
142 | 144 |
143 // The timer for this alarm manager. | 145 // The timer for this alarm manager. |
144 base::OneShotTimer<AlarmManager> timer_; | 146 base::OneShotTimer<AlarmManager> timer_; |
145 | 147 |
146 // A map of our pending alarms, per extension. | 148 // A map of our pending alarms, per extension. |
147 // Invariant: None of the AlarmLists are empty. | 149 // Invariant: None of the AlarmLists are empty. |
148 AlarmMap alarms_; | 150 AlarmMap alarms_; |
149 | 151 |
150 // The previous and next time that alarms were and will be run. | 152 // The previous and next time that alarms were and will be run. |
151 base::Time last_poll_time_; | 153 base::Time last_poll_time_; |
152 base::Time next_poll_time_; | 154 base::Time next_poll_time_; |
153 | 155 |
154 DISALLOW_COPY_AND_ASSIGN(AlarmManager); | 156 DISALLOW_COPY_AND_ASSIGN(AlarmManager); |
155 }; | 157 }; |
156 | 158 |
157 } // namespace extensions | 159 } // namespace extensions |
158 | 160 |
159 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 161 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
OLD | NEW |