Chromium Code Reviews| Index: chrome/browser/extensions/api/alarms/alarm_manager.cc |
| diff --git a/chrome/browser/extensions/api/alarms/alarm_manager.cc b/chrome/browser/extensions/api/alarms/alarm_manager.cc |
| index 1f260a5f52b32e0fca68b7b475147fe4420b390a..06bfbcb2d3b56f05e0e0947df1163f59c048a202 100644 |
| --- a/chrome/browser/extensions/api/alarms/alarm_manager.cc |
| +++ b/chrome/browser/extensions/api/alarms/alarm_manager.cc |
| @@ -236,8 +236,10 @@ void AlarmManager::RemoveAlarmIterator(const AlarmIterator& iter) { |
| // 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) { |
| @@ -278,8 +280,10 @@ void AlarmManager::AddAlarmImpl(const std::string& extension_id, |
| 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_) |
| + ScheduleTimer(alarm_time); |
|
Yoyo Zhou
2014/01/29 21:30:16
This is a shortcut to setting the time (does it st
Devlin
2014/01/29 21:46:16
Discussed offline - granularity isn't really an is
|
| } |
| void AlarmManager::WriteToStorage(const std::string& extension_id) { |
| @@ -313,16 +317,22 @@ void AlarmManager::ReadFromStorage(const std::string& extension_id, |
| ready_actions_.erase(extension_id); |
| } |
| +void AlarmManager::ScheduleTimer(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. |
| @@ -356,13 +366,7 @@ void AlarmManager::ScheduleNextPoll() { |
| 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); |
| + ScheduleTimer(next_poll); |
| } |
| void AlarmManager::PollAlarms() { |