Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: chrome/browser/extensions/api/alarms/alarm_manager.h

Issue 14319002: Change AlarmManager to use ProfileKeyedAPI. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/extensions/api/alarms/alarm_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <string> 8 #include <string>
9 #include <map> 9 #include <map>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
13 #include "base/timer.h" 13 #include "base/timer.h"
14 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
14 #include "chrome/browser/extensions/extension_function.h" 15 #include "chrome/browser/extensions/extension_function.h"
15 #include "chrome/common/extensions/api/alarms.h" 16 #include "chrome/common/extensions/api/alarms.h"
16 #include "content/public/browser/notification_observer.h" 17 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h" 18 #include "content/public/browser/notification_registrar.h"
18 19
19 class Profile; 20 class Profile;
20 21
21 namespace base { 22 namespace base {
22 class Clock; 23 class Clock;
23 } // namespace base 24 } // namespace base
(...skipping 15 matching lines...) Expand all
39 // least as often as the shortest alarm's granularity. It's initialized as 40 // least as often as the shortest alarm's granularity. It's initialized as
40 // the relative delay requested in creation, even if creation uses an absolute 41 // the relative delay requested in creation, even if creation uses an absolute
41 // time. This will always be at least as large as the min_granularity 42 // time. This will always be at least as large as the min_granularity
42 // constructor argument. 43 // constructor argument.
43 base::TimeDelta granularity; 44 base::TimeDelta granularity;
44 }; 45 };
45 46
46 // Manages the currently pending alarms for every extension in a profile. 47 // Manages the currently pending alarms for every extension in a profile.
47 // There is one manager per virtual Profile. 48 // There is one manager per virtual Profile.
48 class AlarmManager 49 class AlarmManager
49 : public content::NotificationObserver, 50 : public ProfileKeyedAPI,
51 public content::NotificationObserver,
50 public base::SupportsWeakPtr<AlarmManager> { 52 public base::SupportsWeakPtr<AlarmManager> {
51 public: 53 public:
52 typedef std::vector<Alarm> AlarmList; 54 typedef std::vector<Alarm> AlarmList;
53 55
54 class Delegate { 56 class Delegate {
55 public: 57 public:
56 virtual ~Delegate() {} 58 virtual ~Delegate() {}
57 // Called when an alarm fires. 59 // Called when an alarm fires.
58 virtual void OnAlarm(const std::string& extension_id, 60 virtual void OnAlarm(const std::string& extension_id,
59 const Alarm& alarm) = 0; 61 const Alarm& alarm) = 0;
60 }; 62 };
61 63
62 // |clock| is usually a base::DefaultClock, but can be something 64 explicit AlarmManager(Profile* profile);
63 // else for testing.
64 explicit AlarmManager(Profile* profile, base::Clock* clock);
65 virtual ~AlarmManager(); 65 virtual ~AlarmManager();
66 66
67 // Override the default delegate. Callee assumes onwership. Used for testing. 67 // Override the default delegate. Callee assumes onwership. Used for testing.
68 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); } 68 void set_delegate(Delegate* delegate) { delegate_.reset(delegate); }
69 69
70 // Adds |alarm| for the given extension, and starts the timer. 70 // Adds |alarm| for the given extension, and starts the timer.
71 void AddAlarm(const std::string& extension_id, 71 void AddAlarm(const std::string& extension_id,
72 const Alarm& alarm); 72 const Alarm& alarm);
73 73
74 // Returns the alarm with the given name, or NULL if none exists. 74 // Returns the alarm with the given name, or NULL if none exists.
75 const Alarm* GetAlarm(const std::string& extension_id, 75 const Alarm* GetAlarm(const std::string& extension_id,
76 const std::string& name); 76 const std::string& name);
77 77
78 // Returns the list of pending alarms for the given extension, or NULL 78 // Returns the list of pending alarms for the given extension, or NULL
79 // if none exist. 79 // if none exist.
80 const AlarmList* GetAllAlarms(const std::string& extension_id); 80 const AlarmList* GetAllAlarms(const std::string& extension_id);
81 81
82 // Cancels and removes the alarm with the given name. 82 // Cancels and removes the alarm with the given name.
83 bool RemoveAlarm(const std::string& extension_id, 83 bool RemoveAlarm(const std::string& extension_id,
84 const std::string& name); 84 const std::string& name);
85 85
86 // Cancels and removes all alarms for the given extension. 86 // Cancels and removes all alarms for the given extension.
87 void RemoveAllAlarms(const std::string& extension_id); 87 void RemoveAllAlarms(const std::string& extension_id);
88 88
89 // Replaces AlarmManager's owned clock with |clock| and takes ownership of it.
90 void SetClockForTesting(base::Clock* clock);
91
92 // ProfileKeyedAPI implementation.
93 static ProfileKeyedAPIFactory<AlarmManager>* GetFactoryInstance();
94
95 // Convenience method to get the AlarmManager for a profile.
96 static AlarmManager* Get(Profile* profile);
97
89 private: 98 private:
90 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, CreateRepeating); 99 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, CreateRepeating);
91 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, Clear); 100 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsTest, Clear);
92 friend class ExtensionAlarmsSchedulingTest; 101 friend class ExtensionAlarmsSchedulingTest;
93 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling); 102 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, PollScheduling);
94 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, 103 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest,
95 ReleasedExtensionPollsInfrequently); 104 ReleasedExtensionPollsInfrequently);
96 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning); 105 FRIEND_TEST_ALL_PREFIXES(ExtensionAlarmsSchedulingTest, TimerRunning);
106 friend class ProfileKeyedAPIFactory<AlarmManager>;
97 107
98 typedef std::string ExtensionId; 108 typedef std::string ExtensionId;
99 typedef std::map<ExtensionId, AlarmList> AlarmMap; 109 typedef std::map<ExtensionId, AlarmList> AlarmMap;
100 110
101 // Iterator used to identify a particular alarm within the Map/List pair. 111 // Iterator used to identify a particular alarm within the Map/List pair.
102 // "Not found" is represented by <alarms_.end(), invalid_iterator>. 112 // "Not found" is represented by <alarms_.end(), invalid_iterator>.
103 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator; 113 typedef std::pair<AlarmMap::iterator, AlarmList::iterator> AlarmIterator;
104 114
105 // Helper to return the iterators within the AlarmMap and AlarmList for the 115 // Helper to return the iterators within the AlarmMap and AlarmList for the
106 // matching alarm, or an iterator to the end of the AlarmMap if none were 116 // matching alarm, or an iterator to the end of the AlarmMap if none were
(...skipping 23 matching lines...) Expand all
130 140
131 // Polls the alarms, running any that have elapsed. After running them and 141 // Polls the alarms, running any that have elapsed. After running them and
132 // rescheduling repeating alarms, schedule the next poll. 142 // rescheduling repeating alarms, schedule the next poll.
133 void PollAlarms(); 143 void PollAlarms();
134 144
135 // NotificationObserver: 145 // NotificationObserver:
136 virtual void Observe(int type, 146 virtual void Observe(int type,
137 const content::NotificationSource& source, 147 const content::NotificationSource& source,
138 const content::NotificationDetails& details) OVERRIDE; 148 const content::NotificationDetails& details) OVERRIDE;
139 149
150 // ProfileKeyedAPI implementation.
151 static const char* service_name() {
152 return "AlarmManager";
153 }
154
140 Profile* const profile_; 155 Profile* const profile_;
141 base::Clock* const clock_; 156 scoped_ptr<base::Clock> clock_;
142 content::NotificationRegistrar registrar_; 157 content::NotificationRegistrar registrar_;
143 scoped_ptr<Delegate> delegate_; 158 scoped_ptr<Delegate> delegate_;
144 159
145 // The timer for this alarm manager. 160 // The timer for this alarm manager.
146 base::OneShotTimer<AlarmManager> timer_; 161 base::OneShotTimer<AlarmManager> timer_;
147 162
148 // A map of our pending alarms, per extension. 163 // A map of our pending alarms, per extension.
149 // Invariant: None of the AlarmLists are empty. 164 // Invariant: None of the AlarmLists are empty.
150 AlarmMap alarms_; 165 AlarmMap alarms_;
151 166
152 // The previous and next time that alarms were and will be run. 167 // The previous and next time that alarms were and will be run.
153 base::Time last_poll_time_; 168 base::Time last_poll_time_;
154 base::Time next_poll_time_; 169 base::Time next_poll_time_;
155 170
156 DISALLOW_COPY_AND_ASSIGN(AlarmManager); 171 DISALLOW_COPY_AND_ASSIGN(AlarmManager);
157 }; 172 };
158 173
159 } // namespace extensions 174 } // namespace extensions
160 175
161 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__ 176 #endif // CHROME_BROWSER_EXTENSIONS_API_ALARMS_ALARM_MANAGER_H__
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/api/alarms/alarm_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698