| 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 EXTENSIONS_BROWSER_API_ALARMS_ALARM_MANAGER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/scoped_observer.h" | 16 #include "base/scoped_observer.h" |
| 17 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
| 18 #include "chrome/common/extensions/api/alarms.h" | |
| 19 #include "extensions/browser/browser_context_keyed_api_factory.h" | 18 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 20 #include "extensions/browser/extension_registry_observer.h" | 19 #include "extensions/browser/extension_registry_observer.h" |
| 20 #include "extensions/common/api/alarms.h" |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 class Clock; | 23 class Clock; |
| 24 } // namespace base | 24 } // namespace base |
| 25 | 25 |
| 26 namespace content { | 26 namespace content { |
| 27 class BrowserContext; | 27 class BrowserContext; |
| 28 } // namespace content | 28 } // namespace content |
| 29 | 29 |
| 30 namespace extensions { | 30 namespace extensions { |
| 31 class ExtensionAlarmsSchedulingTest; | 31 class ExtensionAlarmsSchedulingTest; |
| 32 class ExtensionRegistry; | 32 class ExtensionRegistry; |
| 33 | 33 |
| 34 struct Alarm { | 34 struct Alarm { |
| 35 Alarm(); | 35 Alarm(); |
| 36 Alarm(const std::string& name, | 36 Alarm(const std::string& name, |
| 37 const api::alarms::AlarmCreateInfo& create_info, | 37 const core_api::alarms::AlarmCreateInfo& create_info, |
| 38 base::TimeDelta min_granularity, | 38 base::TimeDelta min_granularity, |
| 39 base::Time now); | 39 base::Time now); |
| 40 ~Alarm(); | 40 ~Alarm(); |
| 41 | 41 |
| 42 linked_ptr<api::alarms::Alarm> js_alarm; | 42 linked_ptr<core_api::alarms::Alarm> js_alarm; |
| 43 // The granularity isn't exposed to the extension's javascript, but we poll at | 43 // The granularity isn't exposed to the extension's javascript, but we poll at |
| 44 // least as often as the shortest alarm's granularity. It's initialized as | 44 // least as often as the shortest alarm's granularity. It's initialized as |
| 45 // the relative delay requested in creation, even if creation uses an absolute | 45 // the relative delay requested in creation, even if creation uses an absolute |
| 46 // time. This will always be at least as large as the min_granularity | 46 // time. This will always be at least as large as the min_granularity |
| 47 // constructor argument. | 47 // constructor argument. |
| 48 base::TimeDelta granularity; | 48 base::TimeDelta granularity; |
| 49 // The minimum granularity is the minimum allowed polling rate. This stops | 49 // The minimum granularity is the minimum allowed polling rate. This stops |
| 50 // alarms from polling too often. | 50 // alarms from polling too often. |
| 51 base::TimeDelta minimum_granularity; | 51 base::TimeDelta minimum_granularity; |
| 52 }; | 52 }; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 83 typedef base::Callback<void(Alarm*)> GetAlarmCallback; | 83 typedef base::Callback<void(Alarm*)> GetAlarmCallback; |
| 84 // Passes the alarm with the given name, or NULL if none exists, to | 84 // Passes the alarm with the given name, or NULL if none exists, to |
| 85 // |callback|. | 85 // |callback|. |
| 86 void GetAlarm(const std::string& extension_id, | 86 void GetAlarm(const std::string& extension_id, |
| 87 const std::string& name, | 87 const std::string& name, |
| 88 const GetAlarmCallback& callback); | 88 const GetAlarmCallback& callback); |
| 89 | 89 |
| 90 typedef base::Callback<void(const AlarmList*)> GetAllAlarmsCallback; | 90 typedef base::Callback<void(const AlarmList*)> GetAllAlarmsCallback; |
| 91 // Passes the list of pending alarms for the given extension, or | 91 // Passes the list of pending alarms for the given extension, or |
| 92 // NULL if none exist, to |callback|. | 92 // NULL if none exist, to |callback|. |
| 93 void GetAllAlarms( | 93 void GetAllAlarms(const std::string& extension_id, |
| 94 const std::string& extension_id, const GetAllAlarmsCallback& callback); | 94 const GetAllAlarmsCallback& callback); |
| 95 | 95 |
| 96 typedef base::Callback<void(bool)> RemoveAlarmCallback; | 96 typedef base::Callback<void(bool)> RemoveAlarmCallback; |
| 97 // Cancels and removes the alarm with the given name. Invokes |callback| when | 97 // Cancels and removes the alarm with the given name. Invokes |callback| when |
| 98 // done. | 98 // done. |
| 99 void RemoveAlarm(const std::string& extension_id, | 99 void RemoveAlarm(const std::string& extension_id, |
| 100 const std::string& name, | 100 const std::string& name, |
| 101 const RemoveAlarmCallback& callback); | 101 const RemoveAlarmCallback& callback); |
| 102 | 102 |
| 103 typedef base::Callback<void()> RemoveAllAlarmsCallback; | 103 typedef base::Callback<void()> RemoveAllAlarmsCallback; |
| 104 // Cancels and removes all alarms for the given extension. Invokes |callback| | 104 // Cancels and removes all alarms for the given extension. Invokes |callback| |
| 105 // when done. | 105 // when done. |
| 106 void RemoveAllAlarms( | 106 void RemoveAllAlarms(const std::string& extension_id, |
| 107 const std::string& extension_id, const RemoveAllAlarmsCallback& callback); | 107 const RemoveAllAlarmsCallback& callback); |
| 108 | 108 |
| 109 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it. | 109 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it. |
| 110 void SetClockForTesting(base::Clock* clock); | 110 void SetClockForTesting(base::Clock* clock); |
| 111 | 111 |
| 112 // BrowserContextKeyedAPI implementation. | 112 // BrowserContextKeyedAPI implementation. |
| 113 static BrowserContextKeyedAPIFactory<AlarmManager>* GetFactoryInstance(); | 113 static BrowserContextKeyedAPIFactory<AlarmManager>* GetFactoryInstance(); |
| 114 | 114 |
| 115 // Convenience method to get the AlarmManager for a content::BrowserContext. | 115 // Convenience method to get the AlarmManager for a content::BrowserContext. |
| 116 static AlarmManager* Get(content::BrowserContext* browser_context); | 116 static AlarmManager* Get(content::BrowserContext* browser_context); |
| 117 | 117 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 // Part of GetAllAlarms that is executed after alarms are loaded. | 153 // Part of GetAllAlarms that is executed after alarms are loaded. |
| 154 void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback, | 154 void GetAllAlarmsWhenReady(const GetAllAlarmsCallback& callback, |
| 155 const std::string& extension_id); | 155 const std::string& extension_id); |
| 156 | 156 |
| 157 // Part of RemoveAlarm that is executed after alarms are loaded. | 157 // Part of RemoveAlarm that is executed after alarms are loaded. |
| 158 void RemoveAlarmWhenReady(const std::string& name, | 158 void RemoveAlarmWhenReady(const std::string& name, |
| 159 const RemoveAlarmCallback& callback, | 159 const RemoveAlarmCallback& callback, |
| 160 const std::string& extension_id); | 160 const std::string& extension_id); |
| 161 | 161 |
| 162 // Part of RemoveAllAlarms that is executed after alarms are loaded. | 162 // Part of RemoveAllAlarms that is executed after alarms are loaded. |
| 163 void RemoveAllAlarmsWhenReady( | 163 void RemoveAllAlarmsWhenReady(const RemoveAllAlarmsCallback& callback, |
| 164 const RemoveAllAlarmsCallback& callback, const std::string& extension_id); | 164 const std::string& extension_id); |
| 165 | 165 |
| 166 // Helper to return the iterators within the AlarmMap and AlarmList for the | 166 // Helper to return the iterators within the AlarmMap and AlarmList for the |
| 167 // matching alarm, or an iterator to the end of the AlarmMap if none were | 167 // matching alarm, or an iterator to the end of the AlarmMap if none were |
| 168 // found. | 168 // found. |
| 169 AlarmIterator GetAlarmIterator(const std::string& extension_id, | 169 AlarmIterator GetAlarmIterator(const std::string& extension_id, |
| 170 const std::string& name); | 170 const std::string& name); |
| 171 | 171 |
| 172 // Helper to cancel and remove the alarm at the given iterator. The iterator | 172 // Helper to cancel and remove the alarm at the given iterator. The iterator |
| 173 // must be valid. | 173 // must be valid. |
| 174 void RemoveAlarmIterator(const AlarmIterator& iter); | 174 void RemoveAlarmIterator(const AlarmIterator& iter); |
| 175 | 175 |
| 176 // Callback for when an alarm fires. | 176 // Callback for when an alarm fires. |
| 177 void OnAlarm(AlarmIterator iter); | 177 void OnAlarm(AlarmIterator iter); |
| 178 | 178 |
| 179 // Internal helper to add an alarm and start the timer with the given delay. | 179 // Internal helper to add an alarm and start the timer with the given delay. |
| 180 void AddAlarmImpl(const std::string& extension_id, | 180 void AddAlarmImpl(const std::string& extension_id, const Alarm& alarm); |
| 181 const Alarm& alarm); | |
| 182 | 181 |
| 183 // Syncs our alarm data for the given extension to/from the state storage. | 182 // Syncs our alarm data for the given extension to/from the state storage. |
| 184 void WriteToStorage(const std::string& extension_id); | 183 void WriteToStorage(const std::string& extension_id); |
| 185 void ReadFromStorage(const std::string& extension_id, | 184 void ReadFromStorage(const std::string& extension_id, |
| 186 scoped_ptr<base::Value> value); | 185 scoped_ptr<base::Value> value); |
| 187 | 186 |
| 188 // Set the timer to go off at the specified |time|, and set |next_poll_time| | 187 // Set the timer to go off at the specified |time|, and set |next_poll_time| |
| 189 // appropriately. | 188 // appropriately. |
| 190 void SetNextPollTime(const base::Time& time); | 189 void SetNextPollTime(const base::Time& time); |
| 191 | 190 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 202 void RunWhenReady(const std::string& extension_id, const ReadyAction& action); | 201 void RunWhenReady(const std::string& extension_id, const ReadyAction& action); |
| 203 | 202 |
| 204 // ExtensionRegistryObserver implementation. | 203 // ExtensionRegistryObserver implementation. |
| 205 void OnExtensionLoaded(content::BrowserContext* browser_context, | 204 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 206 const Extension* extension) override; | 205 const Extension* extension) override; |
| 207 void OnExtensionUninstalled(content::BrowserContext* browser_context, | 206 void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 208 const Extension* extension, | 207 const Extension* extension, |
| 209 extensions::UninstallReason reason) override; | 208 extensions::UninstallReason reason) override; |
| 210 | 209 |
| 211 // BrowserContextKeyedAPI implementation. | 210 // BrowserContextKeyedAPI implementation. |
| 212 static const char* service_name() { | 211 static const char* service_name() { return "AlarmManager"; } |
| 213 return "AlarmManager"; | |
| 214 } | |
| 215 static const bool kServiceHasOwnInstanceInIncognito = true; | 212 static const bool kServiceHasOwnInstanceInIncognito = true; |
| 216 | 213 |
| 217 content::BrowserContext* const browser_context_; | 214 content::BrowserContext* const browser_context_; |
| 218 scoped_ptr<base::Clock> clock_; | 215 scoped_ptr<base::Clock> clock_; |
| 219 scoped_ptr<Delegate> delegate_; | 216 scoped_ptr<Delegate> delegate_; |
| 220 | 217 |
| 221 // Listen to extension load notifications. | 218 // Listen to extension load notifications. |
| 222 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 219 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 223 extension_registry_observer_; | 220 extension_registry_observer_; |
| 224 | 221 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 237 base::Time last_poll_time_; | 234 base::Time last_poll_time_; |
| 238 | 235 |
| 239 // Next poll's time. | 236 // Next poll's time. |
| 240 base::Time next_poll_time_; | 237 base::Time next_poll_time_; |
| 241 | 238 |
| 242 DISALLOW_COPY_AND_ASSIGN(AlarmManager); | 239 DISALLOW_COPY_AND_ASSIGN(AlarmManager); |
| 243 }; | 240 }; |
| 244 | 241 |
| 245 } // namespace extensions | 242 } // namespace extensions |
| 246 | 243 |
| 247 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 244 #endif // EXTENSIONS_BROWSER_API_ALARMS_ALARM_MANAGER_H_ |
| OLD | NEW |