1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/api/alarms/alarm_manager.h"
#include "base/bind.h"
#include "base/json/json_writer.h"
#include "base/message_loop.h"
#include "base/time.h"
#include "base/value_conversions.h"
#include "base/values.h"
#include "chrome/browser/extensions/event_router.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/state_store.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_service.h"
namespace extensions {
namespace {
const char kOnAlarmEvent[] = "alarms.onAlarm";
// A list of alarms that this extension has set.
const char kRegisteredAlarms[] = "alarms";
const char kAlarmGranularity[] = "granularity";
// The minimum period between polling for alarms to run.
const base::TimeDelta kDefaultMinPollPeriod = base::TimeDelta::FromMinutes(1);
class DefaultAlarmDelegate : public AlarmManager::Delegate {
public:
explicit DefaultAlarmDelegate(Profile* profile) : profile_(profile) {}
virtual ~DefaultAlarmDelegate() {}
virtual void OnAlarm(const std::string& extension_id,
const Alarm& alarm) {
scoped_ptr<ListValue> args(new ListValue());
args->Append(alarm.js_alarm->ToValue().release());
scoped_ptr<Event> event(new Event(kOnAlarmEvent, args.Pass()));
ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension(
extension_id, event.Pass());
}
private:
Profile* profile_;
};
// Creates a TimeDelta from a delay as specified in the API.
base::TimeDelta TimeDeltaFromDelay(double delay_in_minutes) {
return base::TimeDelta::FromMicroseconds(
delay_in_minutes * base::Time::kMicrosecondsPerMinute);
}
std::vector<Alarm> AlarmsFromValue(const base::ListValue* list) {
std::vector<Alarm> alarms;
for (size_t i = 0; i < list->GetSize(); ++i) {
const base::DictionaryValue* alarm_dict = NULL;
Alarm alarm;
if (list->GetDictionary(i, &alarm_dict) &&
api::alarms::Alarm::Populate(*alarm_dict, alarm.js_alarm.get())) {
const base::Value* time_value = NULL;
if (alarm_dict->Get(kAlarmGranularity, &time_value))
base::GetValueAsTimeDelta(*time_value, &alarm.granularity);
alarms.push_back(alarm);
}
}
return alarms;
}
scoped_ptr<base::ListValue> AlarmsToValue(const std::vector<Alarm>& alarms) {
scoped_ptr<base::ListValue> list(new ListValue());
for (size_t i = 0; i < alarms.size(); ++i) {
scoped_ptr<base::DictionaryValue> alarm =
alarms[i].js_alarm->ToValue().Pass();
alarm->Set(kAlarmGranularity,
base::CreateTimeDeltaValue(alarms[i].granularity));
list->Append(alarm.release());
}
return list.Pass();
}
} // namespace
// AlarmManager
AlarmManager::AlarmManager(Profile* profile, TimeProvider now)
: profile_(profile),
now_(now),
delegate_(new DefaultAlarmDelegate(profile)),
last_poll_time_(base::Time()) {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
content::Source<Profile>(profile_));
StateStore* storage = ExtensionSystem::Get(profile_)->state_store();
if (storage)
storage->RegisterKey(kRegisteredAlarms);
}
AlarmManager::~AlarmManager() {
}
void AlarmManager::AddAlarm(const std::string& extension_id,
const Alarm& alarm) {
AddAlarmImpl(extension_id, alarm);
WriteToStorage(extension_id);
}
const Alarm* AlarmManager::GetAlarm(
const std::string& extension_id, const std::string& name) {
AlarmIterator it = GetAlarmIterator(extension_id, name);
if (it.first == alarms_.end())
return NULL;
return &*it.second;
}
const AlarmManager::AlarmList* AlarmManager::GetAllAlarms(
const std::string& extension_id) {
AlarmMap::iterator list = alarms_.find(extension_id);
if (list == alarms_.end())
return NULL;
return &list->second;
}
AlarmManager::AlarmIterator AlarmManager::GetAlarmIterator(
const std::string& extension_id, const std::string& name) {
AlarmMap::iterator list = alarms_.find(extension_id);
if (list == alarms_.end())
return make_pair(alarms_.end(), AlarmList::iterator());
for (AlarmList::iterator it = list->second.begin();
it != list->second.end(); ++it) {
if (it->js_alarm->name == name)
return make_pair(list, it);
}
return make_pair(alarms_.end(), AlarmList::iterator());
}
bool AlarmManager::RemoveAlarm(const std::string& extension_id,
const std::string& name) {
AlarmIterator it = GetAlarmIterator(extension_id, name);
if (it.first == alarms_.end())
return false;
RemoveAlarmIterator(it);
WriteToStorage(extension_id);
return true;
}
void AlarmManager::RemoveAllAlarms(const std::string& extension_id) {
AlarmMap::iterator list = alarms_.find(extension_id);
if (list == alarms_.end())
return;
// Note: I'm using indices rather than iterators here because
// RemoveAlarmIterator will delete the list when it becomes empty.
for (size_t i = 0, size = list->second.size(); i < size; ++i)
RemoveAlarmIterator(AlarmIterator(list, list->second.begin()));
CHECK(alarms_.find(extension_id) == alarms_.end());
WriteToStorage(extension_id);
}
void AlarmManager::RemoveAlarmIterator(const AlarmIterator& iter) {
AlarmList& list = iter.first->second;
list.erase(iter.second);
if (list.empty())
alarms_.erase(iter.first);
// Cancel the timer if there are no more alarms.
// We don't need to reschedule the poll otherwise, because in
// the worst case we would just poll one extra time.
if (alarms_.empty())
timer_.Stop();
}
void AlarmManager::OnAlarm(AlarmIterator it) {
CHECK(it.first != alarms_.end());
Alarm& alarm = *it.second;
std::string extension_id_copy(it.first->first);
delegate_->OnAlarm(extension_id_copy, alarm);
// Update our scheduled time for the next alarm.
if (double* period_in_minutes =
alarm.js_alarm->period_in_minutes.get()) {
alarm.js_alarm->scheduled_time =
(last_poll_time_ +
TimeDeltaFromDelay(*period_in_minutes)).ToJsTime();
} else {
RemoveAlarmIterator(it);
}
WriteToStorage(extension_id_copy);
}
void AlarmManager::AddAlarmImpl(const std::string& extension_id,
const Alarm& alarm) {
// Override any old alarm with the same name.
AlarmIterator old_alarm = GetAlarmIterator(extension_id,
alarm.js_alarm->name);
if (old_alarm.first != alarms_.end())
RemoveAlarmIterator(old_alarm);
alarms_[extension_id].push_back(alarm);
// TODO(yoz): Is 0 really sane? There could be thrashing.
ScheduleNextPoll(base::TimeDelta::FromMinutes(0));
}
void AlarmManager::WriteToStorage(const std::string& extension_id) {
StateStore* storage = ExtensionSystem::Get(profile_)->state_store();
if (!storage)
return;
scoped_ptr<Value> alarms;
AlarmMap::iterator list = alarms_.find(extension_id);
if (list != alarms_.end())
alarms.reset(AlarmsToValue(list->second).release());
else
alarms.reset(AlarmsToValue(std::vector<Alarm>()).release());
storage->SetExtensionValue(extension_id, kRegisteredAlarms, alarms.Pass());
}
void AlarmManager::ReadFromStorage(const std::string& extension_id,
scoped_ptr<base::Value> value) {
base::ListValue* list = NULL;
if (!value.get() || !value->GetAsList(&list))
return;
std::vector<Alarm> alarm_states = AlarmsFromValue(list);
for (size_t i = 0; i < alarm_states.size(); ++i) {
AddAlarmImpl(extension_id, alarm_states[i]);
}
}
void AlarmManager::ScheduleNextPoll(base::TimeDelta min_period) {
// 0. If there are no alarms, stop the timer.
if (alarms_.empty()) {
timer_.Stop();
return;
}
// TODO(yoz): Try not to reschedule every single time if we're adding
// a lot of alarms.
base::Time next_poll(last_poll_time_ + min_period);
// Find the soonest alarm that is scheduled to run.
// alarms_ guarantees that none of its contained lists are empty.
base::Time soonest_alarm_time = base::Time::FromJsTime(
alarms_.begin()->second.begin()->js_alarm->scheduled_time);
for (AlarmMap::const_iterator m_it = alarms_.begin(), m_end = alarms_.end();
m_it != m_end; ++m_it) {
for (AlarmList::const_iterator l_it = m_it->second.begin();
l_it != m_it->second.end(); ++l_it) {
base::Time cur_alarm_time =
base::Time::FromJsTime(l_it->js_alarm->scheduled_time);
if (cur_alarm_time < soonest_alarm_time)
soonest_alarm_time = cur_alarm_time;
}
}
// If the next alarm is more than min_period in the future, wait for it.
// Otherwise, only poll as often as min_period.
if (last_poll_time_.is_null() || next_poll < soonest_alarm_time) {
next_poll = soonest_alarm_time;
}
// Schedule the poll.
next_poll_time_ = next_poll;
base::TimeDelta delay = std::max(base::TimeDelta::FromSeconds(0),
next_poll - now_());
timer_.Start(FROM_HERE,
delay,
this,
&AlarmManager::PollAlarms);
}
void AlarmManager::PollAlarms() {
last_poll_time_ = now_();
// Run any alarms scheduled in the past. OnAlarm uses vector::erase to remove
// elements from the AlarmList, and map::erase to remove AlarmLists from the
// AlarmMap.
for (AlarmMap::iterator m_it = alarms_.begin(), m_end = alarms_.end();
m_it != m_end;) {
AlarmMap::iterator cur_extension = m_it++;
// Iterate (a) backwards so that removing elements doesn't affect
// upcoming iterations, and (b) with indices so that if the last
// iteration destroys the AlarmList, I'm not about to use the end
// iterator that the destruction invalidates.
for (size_t i = cur_extension->second.size(); i > 0; --i) {
AlarmList::iterator cur_alarm = cur_extension->second.begin() + i - 1;
if (base::Time::FromJsTime(cur_alarm->js_alarm->scheduled_time) <=
next_poll_time_) {
OnAlarm(make_pair(cur_extension, cur_alarm));
}
}
}
// Schedule the next poll. The soonest it may happen is after
// kDefaultMinPollPeriod or after the shortest granularity of any alarm,
// whichever comes sooner.
base::TimeDelta min_poll_period = kDefaultMinPollPeriod;
for (AlarmMap::const_iterator m_it = alarms_.begin(), m_end = alarms_.end();
m_it != m_end; ++m_it) {
for (AlarmList::const_iterator l_it = m_it->second.begin();
l_it != m_it->second.end(); ++l_it) {
if (l_it->granularity < min_poll_period)
min_poll_period = l_it->granularity;
}
}
ScheduleNextPoll(min_poll_period);
}
void AlarmManager::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_EXTENSION_LOADED: {
const Extension* extension =
content::Details<const Extension>(details).ptr();
StateStore* storage = ExtensionSystem::Get(profile_)->state_store();
if (storage) {
storage->GetExtensionValue(extension->id(), kRegisteredAlarms,
base::Bind(&AlarmManager::ReadFromStorage,
AsWeakPtr(), extension->id()));
}
break;
}
default:
NOTREACHED();
break;
}
}
// AlarmManager::Alarm
Alarm::Alarm()
: js_alarm(new api::alarms::Alarm()) {
}
Alarm::Alarm(const std::string& name,
const api::alarms::AlarmCreateInfo& create_info,
base::TimeDelta min_granularity,
TimeProvider now)
: js_alarm(new api::alarms::Alarm()) {
js_alarm->name = name;
if (create_info.when.get()) {
// Absolute scheduling.
js_alarm->scheduled_time = *create_info.when;
granularity = base::Time::FromJsTime(js_alarm->scheduled_time) - now();
} else {
// Relative scheduling.
double* delay_in_minutes = create_info.delay_in_minutes.get();
if (delay_in_minutes == NULL)
delay_in_minutes = create_info.period_in_minutes.get();
CHECK(delay_in_minutes != NULL)
<< "ValidateAlarmCreateInfo in alarms_api.cc should have "
<< "prevented this call.";
base::TimeDelta delay = TimeDeltaFromDelay(*delay_in_minutes);
js_alarm->scheduled_time = (now() + delay).ToJsTime();
granularity = delay;
}
if (granularity < min_granularity)
granularity = min_granularity;
// Check for repetition.
if (create_info.period_in_minutes.get()) {
js_alarm->period_in_minutes.reset(
new double(*create_info.period_in_minutes));
}
}
Alarm::~Alarm() {
}
} // namespace extensions
|