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 #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/experimental.alarms.h" | 15 #include "chrome/common/extensions/api/experimental.alarms.h" |
| 16 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" |
16 | 18 |
17 class Profile; | 19 class Profile; |
18 | 20 |
19 namespace extensions { | 21 namespace extensions { |
20 | 22 |
21 // Manages the currently pending alarms for every extension in a profile. | 23 // Manages the currently pending alarms for every extension in a profile. |
22 // There is one manager per virtual Profile. | 24 // There is one manager per virtual Profile. |
23 class AlarmManager { | 25 class AlarmManager : public content::NotificationObserver { |
24 public: | 26 public: |
25 typedef extensions::api::experimental_alarms::Alarm Alarm; | 27 typedef extensions::api::experimental_alarms::Alarm Alarm; |
26 typedef std::vector<linked_ptr<Alarm> > AlarmList; | 28 typedef std::vector<linked_ptr<Alarm> > AlarmList; |
27 | 29 |
28 class Delegate { | 30 class Delegate { |
29 public: | 31 public: |
30 virtual ~Delegate() {} | 32 virtual ~Delegate() {} |
31 // Called when an alarm fires. | 33 // Called when an alarm fires. |
32 virtual void OnAlarm(const std::string& extension_id, | 34 virtual void OnAlarm(const std::string& extension_id, |
33 const Alarm& alarm) = 0; | 35 const Alarm& alarm) = 0; |
(...skipping 25 matching lines...) Expand all Loading... |
59 void RemoveAllAlarms(const std::string& extension_id); | 61 void RemoveAllAlarms(const std::string& extension_id); |
60 | 62 |
61 private: | 63 private: |
62 typedef std::string ExtensionId; | 64 typedef std::string ExtensionId; |
63 typedef std::map<ExtensionId, AlarmList> AlarmMap; | 65 typedef std::map<ExtensionId, AlarmList> AlarmMap; |
64 | 66 |
65 // Iterator used to identify a particular alarm within the Map/List pair. | 67 // Iterator used to identify a particular alarm within the Map/List pair. |
66 // "Not found" is represented by <alarms_.end(), invalid_iterator>. | 68 // "Not found" is represented by <alarms_.end(), invalid_iterator>. |
67 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; | 69 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; |
68 | 70 |
| 71 // The timer associated with an alarm. |
| 72 struct TimerInfo; |
| 73 |
69 // Helper to return the iterators within the AlarmMap and AlarmList for the | 74 // Helper to return the iterators within the AlarmMap and AlarmList for the |
70 // matching alarm, or an iterator to the end of the AlarmMap if none were | 75 // matching alarm, or an iterator to the end of the AlarmMap if none were |
71 // found. | 76 // found. |
72 AlarmIterator GetAlarmIterator(const std::string& extension_id, | 77 AlarmIterator GetAlarmIterator(const std::string& extension_id, |
73 const std::string& name); | 78 const std::string& name); |
74 | 79 |
75 // Helper to cancel and remove the alarm at the given iterator. The iterator | 80 // Helper to cancel and remove the alarm at the given iterator. The iterator |
76 // must be valid. | 81 // must be valid. |
77 void RemoveAlarmIterator(const AlarmIterator& iter); | 82 void RemoveAlarmIterator(const AlarmIterator& iter); |
78 | 83 |
79 // Callback for when an alarm fires. | 84 // Callback for when an alarm fires. |
80 void OnAlarm(const std::string& extension_id, const std::string& name); | 85 void OnAlarm(const std::string& extension_id, const std::string& name); |
81 | 86 |
| 87 // Internal helper to add an alarm and start the timer with the given delay. |
| 88 void AddAlarmImpl(const std::string& extension_id, |
| 89 const linked_ptr<Alarm>& alarm, |
| 90 base::TimeDelta timer_delay); |
| 91 |
| 92 // Syncs our alarm data for the given extension to/from the prefs file. |
| 93 void WriteToPrefs(const std::string& extension_id); |
| 94 void ReadFromPrefs(const std::string& extension_id); |
| 95 |
| 96 // NotificationObserver: |
| 97 virtual void Observe(int type, |
| 98 const content::NotificationSource& source, |
| 99 const content::NotificationDetails& details) OVERRIDE; |
| 100 |
82 Profile* profile_; | 101 Profile* profile_; |
| 102 content::NotificationRegistrar registrar_; |
83 scoped_ptr<Delegate> delegate_; | 103 scoped_ptr<Delegate> delegate_; |
84 | 104 |
85 // A map of our pending alarms, per extension. | 105 // A map of our pending alarms, per extension. |
86 AlarmMap alarms_; | 106 AlarmMap alarms_; |
87 | 107 |
88 // A map of the timer associated with each alarm. | 108 // A map of the timer associated with each alarm. |
89 std::map<const Alarm*, linked_ptr<base::Timer> > timers_; | 109 std::map<const Alarm*, linked_ptr<TimerInfo> > timers_; |
| 110 }; |
| 111 |
| 112 // Contains the data we store in the extension prefs for each alarm. |
| 113 struct AlarmPref { |
| 114 linked_ptr<AlarmManager::Alarm> alarm; |
| 115 base::Time scheduled_run_time; |
| 116 |
| 117 AlarmPref(); |
| 118 ~AlarmPref(); |
90 }; | 119 }; |
91 | 120 |
92 } // namespace extensions | 121 } // namespace extensions |
93 | 122 |
94 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 123 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
OLD | NEW |