Chromium Code Reviews| 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 | 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/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/scoped_observer.h" | |
| 15 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
| 16 #include "chrome/common/extensions/api/alarms.h" | 17 #include "chrome/common/extensions/api/alarms.h" |
| 17 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 19 #include "extensions/browser/browser_context_keyed_api_factory.h" | 20 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 20 #include "extensions/browser/extension_function.h" | 21 #include "extensions/browser/extension_function.h" |
| 21 | 22 #include "extensions/browser/extension_registry_observer.h" |
| 22 class Profile; | |
| 23 | 23 |
| 24 namespace base { | 24 namespace base { |
| 25 class Clock; | 25 class Clock; |
| 26 } // namespace base | 26 } // namespace base |
| 27 | 27 |
| 28 namespace content { | 28 namespace content { |
| 29 class BrowserContext; | 29 class BrowserContext; |
| 30 } // namespace content | 30 } // namespace content |
| 31 | 31 |
| 32 namespace extensions { | 32 namespace extensions { |
| 33 | 33 class ExtensionRegistry; |
| 34 class ExtensionAlarmsSchedulingTest; | 34 class ExtensionAlarmsSchedulingTest; |
|
not at google - send to devlin
2014/05/12 17:53:27
alphabetic
limasdf
2014/05/12 23:19:50
Done.
| |
| 35 | 35 |
| 36 struct Alarm { | 36 struct Alarm { |
| 37 Alarm(); | 37 Alarm(); |
| 38 Alarm(const std::string& name, | 38 Alarm(const std::string& name, |
| 39 const api::alarms::AlarmCreateInfo& create_info, | 39 const api::alarms::AlarmCreateInfo& create_info, |
| 40 base::TimeDelta min_granularity, | 40 base::TimeDelta min_granularity, |
| 41 base::Time now); | 41 base::Time now); |
| 42 ~Alarm(); | 42 ~Alarm(); |
| 43 | 43 |
| 44 linked_ptr<api::alarms::Alarm> js_alarm; | 44 linked_ptr<api::alarms::Alarm> js_alarm; |
| 45 // The granularity isn't exposed to the extension's javascript, but we poll at | 45 // 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 | 46 // 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 | 47 // 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 | 48 // time. This will always be at least as large as the min_granularity |
| 49 // constructor argument. | 49 // constructor argument. |
| 50 base::TimeDelta granularity; | 50 base::TimeDelta granularity; |
| 51 // The minimum granularity is the minimum allowed polling rate. This stops | 51 // The minimum granularity is the minimum allowed polling rate. This stops |
| 52 // alarms from polling too often. | 52 // alarms from polling too often. |
| 53 base::TimeDelta minimum_granularity; | 53 base::TimeDelta minimum_granularity; |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 // Manages the currently pending alarms for every extension in a profile. | 56 // Manages the currently pending alarms for every extension in a profile. |
| 57 // There is one manager per virtual Profile. | 57 // There is one manager per virtual Profile. |
| 58 class AlarmManager : public BrowserContextKeyedAPI, | 58 class AlarmManager : public BrowserContextKeyedAPI, |
| 59 public content::NotificationObserver, | 59 public content::NotificationObserver, |
| 60 public ExtensionRegistryObserver, | |
| 60 public base::SupportsWeakPtr<AlarmManager> { | 61 public base::SupportsWeakPtr<AlarmManager> { |
| 61 public: | 62 public: |
| 62 typedef std::vector<Alarm> AlarmList; | 63 typedef std::vector<Alarm> AlarmList; |
| 63 | 64 |
| 64 class Delegate { | 65 class Delegate { |
| 65 public: | 66 public: |
| 66 virtual ~Delegate() {} | 67 virtual ~Delegate() {} |
| 67 // Called when an alarm fires. | 68 // Called when an alarm fires. |
| 68 virtual void OnAlarm(const std::string& extension_id, | 69 virtual void OnAlarm(const std::string& extension_id, |
| 69 const Alarm& alarm) = 0; | 70 const Alarm& alarm) = 0; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 // when done. | 108 // when done. |
| 108 void RemoveAllAlarms( | 109 void RemoveAllAlarms( |
| 109 const std::string& extension_id, const RemoveAllAlarmsCallback& callback); | 110 const std::string& extension_id, const RemoveAllAlarmsCallback& callback); |
| 110 | 111 |
| 111 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it. | 112 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it. |
| 112 void SetClockForTesting(base::Clock* clock); | 113 void SetClockForTesting(base::Clock* clock); |
| 113 | 114 |
| 114 // BrowserContextKeyedAPI implementation. | 115 // BrowserContextKeyedAPI implementation. |
| 115 static BrowserContextKeyedAPIFactory<AlarmManager>* GetFactoryInstance(); | 116 static BrowserContextKeyedAPIFactory<AlarmManager>* GetFactoryInstance(); |
| 116 | 117 |
| 117 // Convenience method to get the AlarmManager for a profile. | 118 // Convenience method to get the AlarmManager for a content::BrowserContext. |
| 118 static AlarmManager* Get(Profile* profile); | 119 static AlarmManager* Get(content::BrowserContext* browser_context); |
| 119 | 120 |
| 120 private: | 121 private: |
| 121 friend void RunScheduleNextPoll(AlarmManager*); | 122 friend void RunScheduleNextPoll(AlarmManager*); |
| 122 friend class ExtensionAlarmsSchedulingTest; | 123 friend class ExtensionAlarmsSchedulingTest; |
| 123 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); | 124 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); |
| 124 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, | 125 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, |
| 125 ReleasedExtensionPollsInfrequently); | 126 ReleasedExtensionPollsInfrequently); |
| 126 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); | 127 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); |
| 127 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, MinimumGranularity); | 128 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, MinimumGranularity); |
| 128 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, | 129 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 | 202 |
| 202 // Executes |action| for given extension, making sure that the extension's | 203 // Executes |action| for given extension, making sure that the extension's |
| 203 // alarm data has been synced from the storage. | 204 // alarm data has been synced from the storage. |
| 204 void RunWhenReady(const std::string& extension_id, const ReadyAction& action); | 205 void RunWhenReady(const std::string& extension_id, const ReadyAction& action); |
| 205 | 206 |
| 206 // NotificationObserver: | 207 // NotificationObserver: |
| 207 virtual void Observe(int type, | 208 virtual void Observe(int type, |
| 208 const content::NotificationSource& source, | 209 const content::NotificationSource& source, |
| 209 const content::NotificationDetails& details) OVERRIDE; | 210 const content::NotificationDetails& details) OVERRIDE; |
| 210 | 211 |
| 212 // Overridden from extensions::ExtensionRegistryObserver. | |
| 213 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, | |
| 214 const Extension* extension) OVERRIDE; | |
| 215 | |
| 211 // BrowserContextKeyedAPI implementation. | 216 // BrowserContextKeyedAPI implementation. |
| 212 static const char* service_name() { | 217 static const char* service_name() { |
| 213 return "AlarmManager"; | 218 return "AlarmManager"; |
| 214 } | 219 } |
| 215 static const bool kServiceHasOwnInstanceInIncognito = true; | 220 static const bool kServiceHasOwnInstanceInIncognito = true; |
| 216 | 221 |
| 217 Profile* const profile_; | 222 content::BrowserContext* const browser_context_; |
| 218 scoped_ptr<base::Clock> clock_; | 223 scoped_ptr<base::Clock> clock_; |
| 219 content::NotificationRegistrar registrar_; | 224 content::NotificationRegistrar registrar_; |
| 220 scoped_ptr<Delegate> delegate_; | 225 scoped_ptr<Delegate> delegate_; |
| 221 | 226 |
| 227 // Listen to extension load notifications. | |
| 228 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | |
| 229 extension_registry_observer_; | |
| 230 | |
| 222 // The timer for this alarm manager. | 231 // The timer for this alarm manager. |
| 223 base::OneShotTimer<AlarmManager> timer_; | 232 base::OneShotTimer<AlarmManager> timer_; |
| 224 | 233 |
| 225 // A map of our pending alarms, per extension. | 234 // A map of our pending alarms, per extension. |
| 226 // Invariant: None of the AlarmLists are empty. | 235 // Invariant: None of the AlarmLists are empty. |
| 227 AlarmMap alarms_; | 236 AlarmMap alarms_; |
| 228 | 237 |
| 229 // A map of actions waiting for alarm data to be synced from storage, per | 238 // A map of actions waiting for alarm data to be synced from storage, per |
| 230 // extension. | 239 // extension. |
| 231 ReadyMap ready_actions_; | 240 ReadyMap ready_actions_; |
| 232 | 241 |
| 233 // The previous time that alarms were run. | 242 // The previous time that alarms were run. |
| 234 base::Time last_poll_time_; | 243 base::Time last_poll_time_; |
| 235 | 244 |
| 236 // Next poll's time. | 245 // Next poll's time. |
| 237 base::Time next_poll_time_; | 246 base::Time next_poll_time_; |
| 238 | 247 |
| 239 DISALLOW_COPY_AND_ASSIGN(AlarmManager); | 248 DISALLOW_COPY_AND_ASSIGN(AlarmManager); |
| 240 }; | 249 }; |
| 241 | 250 |
| 242 } // namespace extensions | 251 } // namespace extensions |
| 243 | 252 |
| 244 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 253 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
| OLD | NEW |