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

Unified Diff: chrome/browser/extensions/api/alarms/alarm_manager.cc

Issue 145353013: Do not reschedule all alarms when adding a new alarm (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Yoyo's Created 6 years, 11 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 side-by-side diff with in-line comments
Download patch
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..eecc662128287bcdd09245ba9c33fa9792b5da4b 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_)
+ SetNextPollTime(alarm_time);
}
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::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.
@@ -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);
+ SetNextPollTime(next_poll);
}
void AlarmManager::PollAlarms() {
« no previous file with comments | « chrome/browser/extensions/api/alarms/alarm_manager.h ('k') | chrome/browser/extensions/api/alarms/alarms_api_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698