| 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 EXTENSIONS_BROWSER_API_ALARMS_ALARM_MANAGER_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_ALARMS_ALARM_MANAGER_H_ |
| 6 #define EXTENSIONS_BROWSER_API_ALARMS_ALARM_MANAGER_H_ | 6 #define EXTENSIONS_BROWSER_API_ALARMS_ALARM_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 namespace extensions { | 31 namespace extensions { |
| 32 class ExtensionAlarmsSchedulingTest; | 32 class ExtensionAlarmsSchedulingTest; |
| 33 class ExtensionRegistry; | 33 class ExtensionRegistry; |
| 34 | 34 |
| 35 struct Alarm { | 35 struct Alarm { |
| 36 Alarm(); | 36 Alarm(); |
| 37 Alarm(const std::string& name, | 37 Alarm(const std::string& name, |
| 38 const api::alarms::AlarmCreateInfo& create_info, | 38 const api::alarms::AlarmCreateInfo& create_info, |
| 39 base::TimeDelta min_granularity, | 39 base::TimeDelta min_granularity, |
| 40 base::Time now); | 40 base::Time now); |
| 41 Alarm(const Alarm& other); | 41 Alarm(Alarm&& other); |
| 42 Alarm& operator=(Alarm&& other); |
| 43 |
| 42 ~Alarm(); | 44 ~Alarm(); |
| 43 | 45 |
| 44 linked_ptr<api::alarms::Alarm> js_alarm; | 46 std::unique_ptr<api::alarms::Alarm> js_alarm; |
| 45 // The granularity isn't exposed to the extension's javascript, but we poll at | 47 // The granularity isn't exposed to the extension's javascript, but we poll at |
| 46 // least as often as the shortest alarm's granularity. It's initialized as | 48 // least as often as the shortest alarm's granularity. It's initialized as |
| 47 // the relative delay requested in creation, even if creation uses an absolute | 49 // the relative delay requested in creation, even if creation uses an absolute |
| 48 // time. This will always be at least as large as the min_granularity | 50 // time. This will always be at least as large as the min_granularity |
| 49 // constructor argument. | 51 // constructor argument. |
| 50 base::TimeDelta granularity; | 52 base::TimeDelta granularity; |
| 51 // The minimum granularity is the minimum allowed polling rate. This stops | 53 // The minimum granularity is the minimum allowed polling rate. This stops |
| 52 // alarms from polling too often. | 54 // alarms from polling too often. |
| 53 base::TimeDelta minimum_granularity; | 55 base::TimeDelta minimum_granularity; |
| 56 |
| 57 private: |
| 58 DISALLOW_COPY_AND_ASSIGN(Alarm); |
| 54 }; | 59 }; |
| 55 | 60 |
| 56 // Manages the currently pending alarms for every extension in a profile. | 61 // Manages the currently pending alarms for every extension in a profile. |
| 57 // There is one manager per virtual Profile. | 62 // There is one manager per virtual Profile. |
| 58 class AlarmManager : public BrowserContextKeyedAPI, | 63 class AlarmManager : public BrowserContextKeyedAPI, |
| 59 public ExtensionRegistryObserver, | 64 public ExtensionRegistryObserver, |
| 60 public base::SupportsWeakPtr<AlarmManager> { | 65 public base::SupportsWeakPtr<AlarmManager> { |
| 61 public: | 66 public: |
| 62 typedef std::vector<Alarm> AlarmList; | 67 typedef std::vector<std::unique_ptr<Alarm>> AlarmList; |
| 63 | 68 |
| 64 class Delegate { | 69 class Delegate { |
| 65 public: | 70 public: |
| 66 virtual ~Delegate() {} | 71 virtual ~Delegate() {} |
| 67 // Called when an alarm fires. | 72 // Called when an alarm fires. |
| 68 virtual void OnAlarm(const std::string& extension_id, | 73 virtual void OnAlarm(const std::string& extension_id, |
| 69 const Alarm& alarm) = 0; | 74 const Alarm& alarm) = 0; |
| 70 }; | 75 }; |
| 71 | 76 |
| 72 explicit AlarmManager(content::BrowserContext* context); | 77 explicit AlarmManager(content::BrowserContext* context); |
| 73 ~AlarmManager() override; | 78 ~AlarmManager() override; |
| 74 | 79 |
| 75 // Override the default delegate. Callee assumes onwership. Used for testing. | 80 // Override the default delegate. Callee assumes onwership. Used for testing. |
| 76 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); } | 81 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); } |
| 77 | 82 |
| 78 typedef base::Callback<void()> AddAlarmCallback; | 83 typedef base::Callback<void()> AddAlarmCallback; |
| 79 // Adds |alarm| for the given extension, and starts the timer. Invokes | 84 // Adds |alarm| for the given extension, and starts the timer. Invokes |
| 80 // |callback| when done. | 85 // |callback| when done. |
| 81 void AddAlarm(const std::string& extension_id, | 86 void AddAlarm(const std::string& extension_id, |
| 82 const Alarm& alarm, | 87 std::unique_ptr<Alarm> alarm, |
| 83 const AddAlarmCallback& callback); | 88 const AddAlarmCallback& callback); |
| 84 | 89 |
| 85 typedef base::Callback<void(Alarm*)> GetAlarmCallback; | 90 typedef base::Callback<void(Alarm*)> GetAlarmCallback; |
| 86 // Passes the alarm with the given name, or NULL if none exists, to | 91 // Passes the alarm with the given name, or NULL if none exists, to |
| 87 // |callback|. | 92 // |callback|. |
| 88 void GetAlarm(const std::string& extension_id, | 93 void GetAlarm(const std::string& extension_id, |
| 89 const std::string& name, | 94 const std::string& name, |
| 90 const GetAlarmCallback& callback); | 95 const GetAlarmCallback& callback); |
| 91 | 96 |
| 92 typedef base::Callback<void(const AlarmList*)> GetAllAlarmsCallback; | 97 typedef base::Callback<void(const AlarmList*)> GetAllAlarmsCallback; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 | 143 |
| 139 typedef base::Callback<void(const std::string&)> ReadyAction; | 144 typedef base::Callback<void(const std::string&)> ReadyAction; |
| 140 typedef std::queue<ReadyAction> ReadyQueue; | 145 typedef std::queue<ReadyAction> ReadyQueue; |
| 141 typedef std::map<ExtensionId, ReadyQueue> ReadyMap; | 146 typedef std::map<ExtensionId, ReadyQueue> ReadyMap; |
| 142 | 147 |
| 143 // Iterator used to identify a particular alarm within the Map/List pair. | 148 // Iterator used to identify a particular alarm within the Map/List pair. |
| 144 // "Not found" is represented by <alarms_.end(), invalid_iterator>. | 149 // "Not found" is represented by <alarms_.end(), invalid_iterator>. |
| 145 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; | 150 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; |
| 146 | 151 |
| 147 // Part of AddAlarm that is executed after alarms are loaded. | 152 // Part of AddAlarm that is executed after alarms are loaded. |
| 148 void AddAlarmWhenReady(const Alarm& alarm, | 153 void AddAlarmWhenReady(std::unique_ptr<Alarm> alarm, |
| 149 const AddAlarmCallback& callback, | 154 const AddAlarmCallback& callback, |
| 150 const std::string& extension_id); | 155 const std::string& extension_id); |
| 151 | 156 |
| 152 // Part of GetAlarm that is executed after alarms are loaded. | 157 // Part of GetAlarm that is executed after alarms are loaded. |
| 153 void GetAlarmWhenReady(const std::string& name, | 158 void GetAlarmWhenReady(const std::string& name, |
| 154 const GetAlarmCallback& callback, | 159 const GetAlarmCallback& callback, |
| 155 const std::string& extension_id); | 160 const std::string& extension_id); |
| 156 | 161 |
| 157 // Part of GetAllAlarms that is executed after alarms are loaded. | 162 // Part of GetAllAlarms that is executed after alarms are loaded. |
| 158 void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback, | 163 void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 174 const std::string& name); | 179 const std::string& name); |
| 175 | 180 |
| 176 // Helper to cancel and remove the alarm at the given iterator. The iterator | 181 // Helper to cancel and remove the alarm at the given iterator. The iterator |
| 177 // must be valid. | 182 // must be valid. |
| 178 void RemoveAlarmIterator(const AlarmIterator& iter); | 183 void RemoveAlarmIterator(const AlarmIterator& iter); |
| 179 | 184 |
| 180 // Callback for when an alarm fires. | 185 // Callback for when an alarm fires. |
| 181 void OnAlarm(AlarmIterator iter); | 186 void OnAlarm(AlarmIterator iter); |
| 182 | 187 |
| 183 // Internal helper to add an alarm and start the timer with the given delay. | 188 // Internal helper to add an alarm and start the timer with the given delay. |
| 184 void AddAlarmImpl(const std::string& extension_id, const Alarm& alarm); | 189 void AddAlarmImpl(const std::string& extension_id, |
| 190 std::unique_ptr<Alarm> alarm); |
| 185 | 191 |
| 186 // Syncs our alarm data for the given extension to/from the state storage. | 192 // Syncs our alarm data for the given extension to/from the state storage. |
| 187 void WriteToStorage(const std::string& extension_id); | 193 void WriteToStorage(const std::string& extension_id); |
| 188 void ReadFromStorage(const std::string& extension_id, | 194 void ReadFromStorage(const std::string& extension_id, |
| 189 bool is_unpacked, | 195 bool is_unpacked, |
| 190 std::unique_ptr<base::Value> value); | 196 std::unique_ptr<base::Value> value); |
| 191 | 197 |
| 192 // Set the timer to go off at the specified |time|, and set |next_poll_time| | 198 // Set the timer to go off at the specified |time|, and set |next_poll_time| |
| 193 // appropriately. | 199 // appropriately. |
| 194 void SetNextPollTime(const base::Time& time); | 200 void SetNextPollTime(const base::Time& time); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 | 246 |
| 241 // Next poll's time. | 247 // Next poll's time. |
| 242 base::Time next_poll_time_; | 248 base::Time next_poll_time_; |
| 243 | 249 |
| 244 DISALLOW_COPY_AND_ASSIGN(AlarmManager); | 250 DISALLOW_COPY_AND_ASSIGN(AlarmManager); |
| 245 }; | 251 }; |
| 246 | 252 |
| 247 } // namespace extensions | 253 } // namespace extensions |
| 248 | 254 |
| 249 #endif // EXTENSIONS_BROWSER_API_ALARMS_ALARM_MANAGER_H_ | 255 #endif // EXTENSIONS_BROWSER_API_ALARMS_ALARM_MANAGER_H_ |
| OLD | NEW |