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