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

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

Issue 10545104: Refactor chrome.alarms interface to support absolute alarm deadlines. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Matt's comments, and re-merge create() Created 8 years, 6 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 | Annotate | Revision Log
OLDNEW
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 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 #include <map> 10 #include <map>
11 #include <vector> 11 #include <vector>
12 12
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 extensions { 21 namespace extensions {
22 22
23 class ExtensionAlarmsSchedulingTest; 23 class ExtensionAlarmsSchedulingTest;
24 24
25
26 struct Alarm {
27 typedef base::Time (*TimeProvider)();
28
29 Alarm();
30 Alarm(const std::string& name,
31 const api::alarms::AlarmCreateInfo& create_info,
32 base::TimeDelta min_granularity,
33 TimeProvider now);
34 ~Alarm();
35
36 linked_ptr<api::alarms::Alarm> js_alarm;
37 // The granularity isn't exposed to the extension's javascript, but we poll at
38 // least as often as the shortest alarm's granularity. It's initialized as
39 // the relative delay requested in creation, even if creation uses an absolute
40 // time. This will always be at least as large as the min_granularity
41 // constructor argument.
42 base::TimeDelta granularity;
43 };
44
25 // Manages the currently pending alarms for every extension in a profile. 45 // Manages the currently pending alarms for every extension in a profile.
26 // There is one manager per virtual Profile. 46 // There is one manager per virtual Profile.
27 class AlarmManager : public content::NotificationObserver { 47 class AlarmManager : public content::NotificationObserver {
28 public: 48 public:
29 typedef extensions::api::alarms::Alarm Alarm; 49 typedef base::Time (*TimeProvider)();
30 typedef std::vector<linked_ptr<Alarm> > AlarmList; 50 typedef std::vector<Alarm> AlarmList;
31 51
32 class Delegate { 52 class Delegate {
33 public: 53 public:
34 virtual ~Delegate() {} 54 virtual ~Delegate() {}
35 // Called when an alarm fires. 55 // Called when an alarm fires.
36 virtual void OnAlarm(const std::string& extension_id, 56 virtual void OnAlarm(const std::string& extension_id,
37 const Alarm& alarm) = 0; 57 const Alarm& alarm) = 0;
38 }; 58 };
39 59
40 explicit AlarmManager(Profile* profile); 60 // 'now' is usually &base::Time::Now.
61 explicit AlarmManager(Profile* profile, TimeProvider now);
41 virtual ~AlarmManager(); 62 virtual ~AlarmManager();
42 63
43 // Override the default delegate. Callee assumes onwership. Used for testing. 64 // Override the default delegate. Callee assumes onwership. Used for testing.
44 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); } 65 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); }
45 66
46 // Adds |alarm| for the given extension, and starts the timer. 67 // Adds |alarm| for the given extension, and starts the timer.
47 void AddAlarm(const std::string& extension_id, 68 void AddAlarm(const std::string& extension_id,
48 const linked_ptr<Alarm>& alarm); 69 const Alarm& alarm);
49 70
50 // Returns the alarm with the given name, or NULL if none exists. 71 // Returns the alarm with the given name, or NULL if none exists.
51 const Alarm* GetAlarm(const std::string& extension_id, 72 const Alarm* GetAlarm(const std::string& extension_id,
52 const std::string& name); 73 const std::string& name);
53 74
54 // Returns the list of pending alarms for the given extension, or NULL 75 // Returns the list of pending alarms for the given extension, or NULL
55 // if none exist. 76 // if none exist.
56 const AlarmList* GetAllAlarms(const std::string& extension_id); 77 const AlarmList* GetAllAlarms(const std::string& extension_id);
57 78
58 // Cancels and removes the alarm with the given name. 79 // Cancels and removes the alarm with the given name.
59 bool RemoveAlarm(const std::string& extension_id, 80 bool RemoveAlarm(const std::string& extension_id,
60 const std::string& name); 81 const std::string& name);
61 82
62 // Cancels and removes all alarms for the given extension. 83 // Cancels and removes all alarms for the given extension.
63 void RemoveAllAlarms(const std::string& extension_id); 84 void RemoveAllAlarms(const std::string& extension_id);
64 85
65 private: 86 private:
66 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, CreateRepeating); 87 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, CreateRepeating);
67 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, Clear); 88 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, Clear);
68 friend class ExtensionAlarmsSchedulingTest; 89 friend class ExtensionAlarmsSchedulingTest;
69 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); 90 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling);
91 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
92 ReleasedExtensionPollsInfrequently);
70 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); 93 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning);
71 94
72 typedef std::string ExtensionId; 95 typedef std::string ExtensionId;
73 typedef std::map<ExtensionId, AlarmList> AlarmMap; 96 typedef std::map<ExtensionId, AlarmList> AlarmMap;
74 97
75 // Iterator used to identify a particular alarm within the Map/List pair. 98 // Iterator used to identify a particular alarm within the Map/List pair.
76 // "Not found" is represented by <alarms_.end(), invalid_iterator>. 99 // "Not found" is represented by <alarms_.end(), invalid_iterator>.
77 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; 100 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator;
78 101
79 struct AlarmRuntimeInfo {
80 std::string extension_id;
81 base::Time time;
82 };
83 typedef std::map<const Alarm*, AlarmRuntimeInfo> AlarmRuntimeInfoMap;
84
85 // Helper to return the iterators within the AlarmMap and AlarmList for the 102 // Helper to return the iterators within the AlarmMap and AlarmList for the
86 // matching alarm, or an iterator to the end of the AlarmMap if none were 103 // matching alarm, or an iterator to the end of the AlarmMap if none were
87 // found. 104 // found.
88 AlarmIterator GetAlarmIterator(const std::string& extension_id, 105 AlarmIterator GetAlarmIterator(const std::string& extension_id,
89 const std::string& name); 106 const std::string& name);
90 107
91 // Helper to cancel and remove the alarm at the given iterator. The iterator 108 // Helper to cancel and remove the alarm at the given iterator. The iterator
92 // must be valid. 109 // must be valid.
93 void RemoveAlarmIterator(const AlarmIterator& iter); 110 void RemoveAlarmIterator(const AlarmIterator& iter);
94 111
95 // Callback for when an alarm fires. 112 // Callback for when an alarm fires.
96 void OnAlarm(const std::string& extension_id, const std::string& name); 113 void OnAlarm(AlarmIterator iter);
97 114
98 // Internal helper to add an alarm and start the timer with the given delay. 115 // Internal helper to add an alarm and start the timer with the given delay.
99 void AddAlarmImpl(const std::string& extension_id, 116 void AddAlarmImpl(const std::string& extension_id,
100 const linked_ptr<Alarm>& alarm, 117 const Alarm& alarm);
101 base::TimeDelta time_delay);
102 118
103 // Syncs our alarm data for the given extension to/from the prefs file. 119 // Syncs our alarm data for the given extension to/from the prefs file.
104 void WriteToPrefs(const std::string& extension_id); 120 void WriteToPrefs(const std::string& extension_id);
105 void ReadFromPrefs(const std::string& extension_id); 121 void ReadFromPrefs(const std::string& extension_id);
106 122
107 // Schedules the next poll of alarms for when the next soonest alarm runs, 123 // Schedules the next poll of alarms for when the next soonest alarm runs,
108 // but do not more often than min_period. 124 // but do not more often than min_period.
109 void ScheduleNextPoll(base::TimeDelta min_period); 125 void ScheduleNextPoll(base::TimeDelta min_period);
110 126
111 // Polls the alarms, running any that have elapsed. After running them and 127 // Polls the alarms, running any that have elapsed. After running them and
112 // rescheduling repeating alarms, schedule the next poll. 128 // rescheduling repeating alarms, schedule the next poll.
113 void PollAlarms(); 129 void PollAlarms();
114 130
115 // NotificationObserver: 131 // NotificationObserver:
116 virtual void Observe(int type, 132 virtual void Observe(int type,
117 const content::NotificationSource& source, 133 const content::NotificationSource& source,
118 const content::NotificationDetails& details) OVERRIDE; 134 const content::NotificationDetails& details) OVERRIDE;
119 135
120 Profile* profile_; 136 Profile* profile_;
137 const TimeProvider now_;
121 content::NotificationRegistrar registrar_; 138 content::NotificationRegistrar registrar_;
122 scoped_ptr<Delegate> delegate_; 139 scoped_ptr<Delegate> delegate_;
123 140
124 // The timer for this alarm manager. 141 // The timer for this alarm manager.
125 base::OneShotTimer<AlarmManager> timer_; 142 base::OneShotTimer<AlarmManager> timer_;
126 143
127 // A map of our pending alarms, per extension. 144 // A map of our pending alarms, per extension.
145 // Invariant: None of the AlarmLists are empty.
128 AlarmMap alarms_; 146 AlarmMap alarms_;
129 147
130 // A map of the next scheduled times associated with each alarm.
131 AlarmRuntimeInfoMap scheduled_times_;
132
133 // The previous and next time that alarms were and will be run. 148 // The previous and next time that alarms were and will be run.
134 base::Time last_poll_time_; 149 base::Time last_poll_time_;
135 base::Time next_poll_time_; 150 base::Time next_poll_time_;
136 }; 151 };
137 152
138 // Contains the data we store in the extension prefs for each alarm.
139 struct AlarmPref {
140 linked_ptr<AlarmManager::Alarm> alarm;
141 base::Time scheduled_run_time;
142
143 AlarmPref();
144 ~AlarmPref();
145 };
146
147 } // namespace extensions 153 } // namespace extensions
148 154
149 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ 155 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698