| 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/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 16 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" | |
| 17 #include "chrome/common/extensions/api/alarms.h" | 16 #include "chrome/common/extensions/api/alarms.h" |
| 18 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 19 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 20 #include "extensions/browser/extension_function.h" | 20 #include "extensions/browser/extension_function.h" |
| 21 | 21 |
| 22 class Profile; | 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; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 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 | 58 class AlarmManager : public BrowserContextKeyedAPI, |
| 59 : public ProfileKeyedAPI, | 59 public content::NotificationObserver, |
| 60 public content::NotificationObserver, | 60 public base::SupportsWeakPtr<AlarmManager> { |
| 61 public base::SupportsWeakPtr<AlarmManager> { | |
| 62 public: | 61 public: |
| 63 typedef std::vector<Alarm> AlarmList; | 62 typedef std::vector<Alarm> AlarmList; |
| 64 | 63 |
| 65 class Delegate { | 64 class Delegate { |
| 66 public: | 65 public: |
| 67 virtual ~Delegate() {} | 66 virtual ~Delegate() {} |
| 68 // Called when an alarm fires. | 67 // Called when an alarm fires. |
| 69 virtual void OnAlarm(const std::string& extension_id, | 68 virtual void OnAlarm(const std::string& extension_id, |
| 70 const Alarm& alarm) = 0; | 69 const Alarm& alarm) = 0; |
| 71 }; | 70 }; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 104 |
| 106 typedef base::Callback<void()> RemoveAllAlarmsCallback; | 105 typedef base::Callback<void()> RemoveAllAlarmsCallback; |
| 107 // Cancels and removes all alarms for the given extension. Invokes |callback| | 106 // Cancels and removes all alarms for the given extension. Invokes |callback| |
| 108 // when done. | 107 // when done. |
| 109 void RemoveAllAlarms( | 108 void RemoveAllAlarms( |
| 110 const std::string& extension_id, const RemoveAllAlarmsCallback& callback); | 109 const std::string& extension_id, const RemoveAllAlarmsCallback& callback); |
| 111 | 110 |
| 112 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it. | 111 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it. |
| 113 void SetClockForTesting(base::Clock* clock); | 112 void SetClockForTesting(base::Clock* clock); |
| 114 | 113 |
| 115 // ProfileKeyedAPI implementation. | 114 // BrowserContextKeyedAPI implementation. |
| 116 static ProfileKeyedAPIFactory<AlarmManager>* GetFactoryInstance(); | 115 static BrowserContextKeyedAPIFactory<AlarmManager>* GetFactoryInstance(); |
| 117 | 116 |
| 118 // Convenience method to get the AlarmManager for a profile. | 117 // Convenience method to get the AlarmManager for a profile. |
| 119 static AlarmManager* Get(Profile* profile); | 118 static AlarmManager* Get(Profile* profile); |
| 120 | 119 |
| 121 private: | 120 private: |
| 122 friend void RunScheduleNextPoll(AlarmManager*); | 121 friend void RunScheduleNextPoll(AlarmManager*); |
| 123 friend class ExtensionAlarmsSchedulingTest; | 122 friend class ExtensionAlarmsSchedulingTest; |
| 124 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); | 123 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); |
| 125 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, | 124 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, |
| 126 ReleasedExtensionPollsInfrequently); | 125 ReleasedExtensionPollsInfrequently); |
| 127 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); | 126 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); |
| 128 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, MinimumGranularity); | 127 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, MinimumGranularity); |
| 129 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, | 128 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, |
| 130 DifferentMinimumGranularities); | 129 DifferentMinimumGranularities); |
| 131 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, | 130 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, |
| 132 RepeatingAlarmsScheduledPredictably); | 131 RepeatingAlarmsScheduledPredictably); |
| 133 friend class ProfileKeyedAPIFactory<AlarmManager>; | 132 friend class BrowserContextKeyedAPIFactory<AlarmManager>; |
| 134 | 133 |
| 135 typedef std::string ExtensionId; | 134 typedef std::string ExtensionId; |
| 136 typedef std::map<ExtensionId, AlarmList> AlarmMap; | 135 typedef std::map<ExtensionId, AlarmList> AlarmMap; |
| 137 | 136 |
| 138 typedef base::Callback<void(const std::string&)> ReadyAction; | 137 typedef base::Callback<void(const std::string&)> ReadyAction; |
| 139 typedef std::queue<ReadyAction> ReadyQueue; | 138 typedef std::queue<ReadyAction> ReadyQueue; |
| 140 typedef std::map<ExtensionId, ReadyQueue> ReadyMap; | 139 typedef std::map<ExtensionId, ReadyQueue> ReadyMap; |
| 141 | 140 |
| 142 // Iterator used to identify a particular alarm within the Map/List pair. | 141 // Iterator used to identify a particular alarm within the Map/List pair. |
| 143 // "Not found" is represented by <alarms_.end(), invalid_iterator>. | 142 // "Not found" is represented by <alarms_.end(), invalid_iterator>. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 | 201 |
| 203 // Executes |action| for given extension, making sure that the extension's | 202 // Executes |action| for given extension, making sure that the extension's |
| 204 // alarm data has been synced from the storage. | 203 // alarm data has been synced from the storage. |
| 205 void RunWhenReady(const std::string& extension_id, const ReadyAction& action); | 204 void RunWhenReady(const std::string& extension_id, const ReadyAction& action); |
| 206 | 205 |
| 207 // NotificationObserver: | 206 // NotificationObserver: |
| 208 virtual void Observe(int type, | 207 virtual void Observe(int type, |
| 209 const content::NotificationSource& source, | 208 const content::NotificationSource& source, |
| 210 const content::NotificationDetails& details) OVERRIDE; | 209 const content::NotificationDetails& details) OVERRIDE; |
| 211 | 210 |
| 212 // ProfileKeyedAPI implementation. | 211 // BrowserContextKeyedAPI implementation. |
| 213 static const char* service_name() { | 212 static const char* service_name() { |
| 214 return "AlarmManager"; | 213 return "AlarmManager"; |
| 215 } | 214 } |
| 216 static const bool kServiceHasOwnInstanceInIncognito = true; | 215 static const bool kServiceHasOwnInstanceInIncognito = true; |
| 217 | 216 |
| 218 Profile* const profile_; | 217 Profile* const profile_; |
| 219 scoped_ptr<base::Clock> clock_; | 218 scoped_ptr<base::Clock> clock_; |
| 220 content::NotificationRegistrar registrar_; | 219 content::NotificationRegistrar registrar_; |
| 221 scoped_ptr<Delegate> delegate_; | 220 scoped_ptr<Delegate> delegate_; |
| 222 | 221 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 236 | 235 |
| 237 // Next poll's time. | 236 // Next poll's time. |
| 238 base::Time next_poll_time_; | 237 base::Time next_poll_time_; |
| 239 | 238 |
| 240 DISALLOW_COPY_AND_ASSIGN(AlarmManager); | 239 DISALLOW_COPY_AND_ASSIGN(AlarmManager); |
| 241 }; | 240 }; |
| 242 | 241 |
| 243 } // namespace extensions | 242 } // namespace extensions |
| 244 | 243 |
| 245 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ | 244 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ |
| OLD | NEW |