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

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

Issue 11818010: Make chrome.alarms API accept non-integers and too-short delays. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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 11282daba896015d03d6ef5d1ddb4597e1d1b0cf..a0bf73f901d8330818638ef2160e46e65e7a14b9 100644
--- a/chrome/browser/extensions/api/alarms/alarm_manager.cc
+++ b/chrome/browser/extensions/api/alarms/alarm_manager.cc
@@ -29,7 +29,7 @@ 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);
+const base::TimeDelta kDefaultMinPollPeriod = base::TimeDelta::FromDays(1);
class DefaultAlarmDelegate : public AlarmManager::Delegate {
public:
@@ -207,8 +207,7 @@ void AlarmManager::AddAlarmImpl(const std::string& extension_id,
alarms_[extension_id].push_back(alarm);
- // TODO(yoz): Is 0 really sane? There could be thrashing.
- ScheduleNextPoll(base::TimeDelta::FromMinutes(0));
+ ScheduleNextPoll();
}
void AlarmManager::WriteToStorage(const std::string& extension_id) {
@@ -237,7 +236,7 @@ void AlarmManager::ReadFromStorage(const std::string& extension_id,
}
}
-void AlarmManager::ScheduleNextPoll(base::TimeDelta min_period) {
+void AlarmManager::ScheduleNextPoll() {
// 0. If there are no alarms, stop the timer.
if (alarms_.empty()) {
timer_.Stop();
@@ -247,12 +246,12 @@ void AlarmManager::ScheduleNextPoll(base::TimeDelta min_period) {
// 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.
+ // 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.
base::Time soonest_alarm_time = base::Time::FromJsTime(
alarms_.begin()->second.begin()->js_alarm->scheduled_time);
+ base::TimeDelta min_granularity = 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();
@@ -261,14 +260,18 @@ void AlarmManager::ScheduleNextPoll(base::TimeDelta min_period) {
base::Time::FromJsTime(l_it->js_alarm->scheduled_time);
if (cur_alarm_time < soonest_alarm_time)
soonest_alarm_time = cur_alarm_time;
+ if (l_it->granularity < min_granularity)
+ min_granularity = l_it->granularity;
}
}
- // 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) {
+ base::Time next_poll(last_poll_time_ + min_granularity);
+ // If the next alarm is more than min_granularity in the future, wait for it.
+ // Otherwise, only poll as often as min_granularity.
+ // As a special case, if we've never checked for an alarm before
+ // (e.g. during startup), let alarms fire asap.
+ if (last_poll_time_.is_null() || next_poll < soonest_alarm_time)
next_poll = soonest_alarm_time;
- }
// Schedule the poll.
next_poll_time_ = next_poll;
@@ -303,19 +306,7 @@ void AlarmManager::PollAlarms() {
}
}
- // 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);
+ ScheduleNextPoll();
}
void AlarmManager::Observe(

Powered by Google App Engine
This is Rietveld 408576698