Index: chrome/browser/extensions/api/alarms/alarm_manager.h |
diff --git a/chrome/browser/extensions/api/alarms/alarm_manager.h b/chrome/browser/extensions/api/alarms/alarm_manager.h |
index 47884f9ece729fd5ae7957981029f7d04418f13b..b2e9e00c8b1c5518c5979faa56b6f5a92f654654 100644 |
--- a/chrome/browser/extensions/api/alarms/alarm_manager.h |
+++ b/chrome/browser/extensions/api/alarms/alarm_manager.h |
@@ -22,12 +22,36 @@ namespace extensions { |
class ExtensionAlarmsSchedulingTest; |
+ |
+struct Alarm { |
+ typedef base::Time (*TimeProvider)(); |
+ |
+ Alarm(); |
+ Alarm(const std::string& name, |
+ const extensions::api::alarms::AlarmOneShotCreateInfo& create_info, |
Matt Perry
2012/06/11 22:32:44
extensions prefix is unnecessary
Jeffrey Yasskin
2012/06/12 19:12:31
Whoops, removed everywhere.
|
+ base::TimeDelta min_granularity, |
+ TimeProvider now); |
+ Alarm(const std::string& name, |
+ const extensions::api::alarms::AlarmRepeatingCreateInfo& create_info, |
+ base::TimeDelta min_granularity, |
+ TimeProvider now); |
+ ~Alarm(); |
+ |
+ linked_ptr<extensions::api::alarms::Alarm> js_alarm; |
+ // The granularity isn't exposed to the extension's javascript, but we poll at |
+ // least as often as the shortest alarm's granularity. It's initialized as |
+ // the relative delay requested in creation, even if creation uses an absolute |
+ // time. This will always be at least as large as the min_granularity |
+ // constructor argument. |
+ base::TimeDelta granularity; |
+}; |
+ |
// Manages the currently pending alarms for every extension in a profile. |
// There is one manager per virtual Profile. |
class AlarmManager : public content::NotificationObserver { |
public: |
- typedef extensions::api::alarms::Alarm Alarm; |
- typedef std::vector<linked_ptr<Alarm> > AlarmList; |
+ typedef base::Time (*TimeProvider)(); |
+ typedef std::vector<Alarm> AlarmList; |
class Delegate { |
public: |
@@ -37,7 +61,8 @@ class AlarmManager : public content::NotificationObserver { |
const Alarm& alarm) = 0; |
}; |
- explicit AlarmManager(Profile* profile); |
+ // 'now' is usually &base::Time::Now. |
+ explicit AlarmManager(Profile* profile, TimeProvider now); |
virtual ~AlarmManager(); |
// Override the default delegate. Callee assumes onwership. Used for testing. |
@@ -45,7 +70,7 @@ class AlarmManager : public content::NotificationObserver { |
// Adds |alarm| for the given extension, and starts the timer. |
void AddAlarm(const std::string& extension_id, |
- const linked_ptr<Alarm>& alarm); |
+ const Alarm& alarm); |
// Returns the alarm with the given name, or NULL if none exists. |
const Alarm* GetAlarm(const std::string& extension_id, |
@@ -67,6 +92,8 @@ class AlarmManager : public content::NotificationObserver { |
FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, Clear); |
friend class ExtensionAlarmsSchedulingTest; |
FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); |
+ FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, |
+ ReleasedExtensionPollsInfrequently); |
FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); |
typedef std::string ExtensionId; |
@@ -76,12 +103,6 @@ class AlarmManager : public content::NotificationObserver { |
// "Not found" is represented by <alarms_.end(), invalid_iterator>. |
typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; |
- struct AlarmRuntimeInfo { |
- std::string extension_id; |
- base::Time time; |
- }; |
- typedef std::map<const Alarm*, AlarmRuntimeInfo> AlarmRuntimeInfoMap; |
- |
// Helper to return the iterators within the AlarmMap and AlarmList for the |
// matching alarm, or an iterator to the end of the AlarmMap if none were |
// found. |
@@ -93,12 +114,11 @@ class AlarmManager : public content::NotificationObserver { |
void RemoveAlarmIterator(const AlarmIterator& iter); |
// Callback for when an alarm fires. |
- void OnAlarm(const std::string& extension_id, const std::string& name); |
+ void OnAlarm(AlarmIterator iter); |
// Internal helper to add an alarm and start the timer with the given delay. |
void AddAlarmImpl(const std::string& extension_id, |
- const linked_ptr<Alarm>& alarm, |
- base::TimeDelta time_delay); |
+ const Alarm& alarm); |
// Syncs our alarm data for the given extension to/from the prefs file. |
void WriteToPrefs(const std::string& extension_id); |
@@ -118,6 +138,7 @@ class AlarmManager : public content::NotificationObserver { |
const content::NotificationDetails& details) OVERRIDE; |
Profile* profile_; |
+ const TimeProvider now_; |
content::NotificationRegistrar registrar_; |
scoped_ptr<Delegate> delegate_; |
@@ -125,25 +146,14 @@ class AlarmManager : public content::NotificationObserver { |
base::OneShotTimer<AlarmManager> timer_; |
// A map of our pending alarms, per extension. |
+ // Invariant: None of the AlarmLists are empty. |
AlarmMap alarms_; |
- // A map of the next scheduled times associated with each alarm. |
- AlarmRuntimeInfoMap scheduled_times_; |
- |
// The previous and next time that alarms were and will be run. |
base::Time last_poll_time_; |
base::Time next_poll_time_; |
}; |
-// Contains the data we store in the extension prefs for each alarm. |
-struct AlarmPref { |
- linked_ptr<AlarmManager::Alarm> alarm; |
- base::Time scheduled_run_time; |
- |
- AlarmPref(); |
- ~AlarmPref(); |
-}; |
- |
} // namespace extensions |
#endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |