#include "base/values.h"
#include "chrome/browser/chrome_notification_types.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/extensions/api/alarms.h"
#include "content/public/browser/notification_service.h"
#include "extensions/browser/event_router.h"
+#include "extensions/browser/extension_system.h"
namespace extensions {
// 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())
+ if (alarms_.empty()) {
timer_.Stop();
+ next_poll_time_ = base::Time();
+ }
}
void AlarmManager::OnAlarm(AlarmIterator it) {
RemoveAlarmIterator(old_alarm);
alarms_[extension_id].push_back(alarm);
-
- ScheduleNextPoll();
+ base::Time alarm_time =
+ base::Time::FromJsTime(alarm.js_alarm->scheduled_time);
+ if (next_poll_time_.is_null() || alarm_time < next_poll_time_)
+ SetNextPollTime(alarm_time);
}
void AlarmManager::WriteToStorage(const std::string& extension_id) {
ready_actions_.erase(extension_id);
}
+void AlarmManager::SetNextPollTime(const base::Time& time) {
+ next_poll_time_ = time;
+ timer_.Start(FROM_HERE,
+ std::max(base::TimeDelta::FromSeconds(0), time - clock_->Now()),
+ this,
+ &AlarmManager::PollAlarms);
+}
+
void AlarmManager::ScheduleNextPoll() {
// If there are no alarms, stop the timer.
if (alarms_.empty()) {
timer_.Stop();
+ next_poll_time_ = base::Time();
return;
}
- // TODO(yoz): Try not to reschedule every single time if we're adding
- // a lot of alarms.
-
// Find the soonest alarm that is scheduled to run and the smallest
// granularity of any alarm.
// alarms_ guarantees that none of its contained lists are empty.
next_poll = soonest_alarm_time;
// Schedule the poll.
- test_next_poll_time_ = next_poll;
- base::TimeDelta delay = std::max(base::TimeDelta::FromSeconds(0),
- next_poll - clock_->Now());
- timer_.Start(FROM_HERE,
- delay,
- this,
- &AlarmManager::PollAlarms);
+ SetNextPollTime(next_poll);
}
void AlarmManager::PollAlarms() {