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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 AlarmIterator GetAlarmIterator(const std::string& extension_id, | 74 AlarmIterator GetAlarmIterator(const std::string& extension_id, |
73 const std::string& name); | 75 const std::string& name); |
74 | 76 |
75 // Helper to cancel and remove the alarm at the given iterator. The iterator | 77 // Helper to cancel and remove the alarm at the given iterator. The iterator |
76 // must be valid. | 78 // must be valid. |
77 void RemoveAlarmIterator(const AlarmIterator& iter); | 79 void RemoveAlarmIterator(const AlarmIterator& iter); |
78 | 80 |
79 // Callback for when an alarm fires. | 81 // Callback for when an alarm fires. |
80 void OnAlarm(const std::string& extension_id, const std::string& name); | 82 void OnAlarm(const std::string& extension_id, const std::string& name); |
81 | 83 |
| 84 // Internal helper to add an alarm and start the timer with the given delay. |
| 85 void AddAlarmImpl(const std::string& extension_id, |
| 86 const linked_ptr<Alarm>& alarm, |
| 87 base::TimeDelta timer_delay); |
| 88 |
| 89 // Syncs our alarm data for the given extension to/from the prefs file. |
| 90 void WriteToPrefs(const std::string& extension_id); |
| 91 void ReadFromPrefs(const std::string& extension_id); |
| 92 |
| 93 // NotificationObserver: |
| 94 virtual void Observe(int type, |
| 95 const content::NotificationSource& source, |
| 96 const content::NotificationDetails& details) OVERRIDE; |
| 97 |
82 Profile* profile_; | 98 Profile* profile_; |
| 99 content::NotificationRegistrar registrar_; |
83 scoped_ptr<Delegate> delegate_; | 100 scoped_ptr<Delegate> delegate_; |
84 | 101 |
85 // A map of our pending alarms, per extension. | 102 // A map of our pending alarms, per extension. |
86 AlarmMap alarms_; | 103 AlarmMap alarms_; |
87 | 104 |
88 // A map of the timer associated with each alarm. | 105 // A map of the timer associated with each alarm. |
89 std::map<const Alarm*, linked_ptr<base::Timer> > timers_; | 106 std::map<const Alarm*, linked_ptr<base::Timer> > timers_; |
90 }; | 107 }; |
91 | 108 |
| 109 // Contains the data we store in the extension prefs for each alarm. |
| 110 struct AlarmPref { |
| 111 linked_ptr<AlarmManager::Alarm> alarm; |
| 112 base::Time scheduled_run_time; |
| 113 |
| 114 AlarmPref(); |
| 115 ~AlarmPref(); |
| 116 }; |
| 117 |
92 } // namespace extensions | 118 } // namespace extensions |
93 | 119 |
94 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 120 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
OLD | NEW |