OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/alarms/alarm_manager.h" | 5 #include "chrome/browser/extensions/api/alarms/alarm_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 | 292 |
293 ReadyQueue& extension_ready_queue = ready_actions_[extension_id]; | 293 ReadyQueue& extension_ready_queue = ready_actions_[extension_id]; |
294 while (!extension_ready_queue.empty()) { | 294 while (!extension_ready_queue.empty()) { |
295 extension_ready_queue.front().Run(extension_id); | 295 extension_ready_queue.front().Run(extension_id); |
296 extension_ready_queue.pop(); | 296 extension_ready_queue.pop(); |
297 } | 297 } |
298 ready_actions_.erase(extension_id); | 298 ready_actions_.erase(extension_id); |
299 } | 299 } |
300 | 300 |
301 void AlarmManager::ScheduleNextPoll() { | 301 void AlarmManager::ScheduleNextPoll() { |
302 // 0. If there are no alarms, stop the timer. | 302 // If there are no alarms, stop the timer. |
303 if (alarms_.empty()) { | 303 if (alarms_.empty()) { |
304 timer_.Stop(); | 304 timer_.Stop(); |
305 return; | 305 return; |
306 } | 306 } |
307 | 307 |
308 // TODO(yoz): Try not to reschedule every single time if we're adding | 308 // TODO(yoz): Try not to reschedule every single time if we're adding |
309 // a lot of alarms. | 309 // a lot of alarms. |
310 | 310 |
311 // Find the soonest alarm that is scheduled to run and the smallest | 311 // Find the soonest alarm that is scheduled to run and the smallest |
312 // granularity of any alarm. | 312 // granularity of any alarm. |
313 // alarms_ guarantees that none of its contained lists are empty. | 313 // alarms_ guarantees that none of its contained lists are empty. |
314 base::Time soonest_alarm_time = base::Time::FromJsTime( | 314 base::Time soonest_alarm_time = base::Time::FromJsTime( |
315 alarms_.begin()->second.begin()->js_alarm->scheduled_time); | 315 alarms_.begin()->second.begin()->js_alarm->scheduled_time); |
316 base::TimeDelta min_granularity = kDefaultMinPollPeriod; | 316 base::TimeDelta min_granularity = kDefaultMinPollPeriod; |
317 for (AlarmMap::const_iterator m_it = alarms_.begin(), m_end = alarms_.end(); | 317 for (AlarmMap::const_iterator m_it = alarms_.begin(), m_end = alarms_.end(); |
318 m_it != m_end; ++m_it) { | 318 m_it != m_end; ++m_it) { |
319 for (AlarmList::const_iterator l_it = m_it->second.begin(); | 319 for (AlarmList::const_iterator l_it = m_it->second.begin(); |
320 l_it != m_it->second.end(); ++l_it) { | 320 l_it != m_it->second.end(); ++l_it) { |
321 base::Time cur_alarm_time = | 321 base::Time cur_alarm_time = |
322 base::Time::FromJsTime(l_it->js_alarm->scheduled_time); | 322 base::Time::FromJsTime(l_it->js_alarm->scheduled_time); |
323 if (cur_alarm_time < soonest_alarm_time) | 323 if (cur_alarm_time < soonest_alarm_time) |
324 soonest_alarm_time = cur_alarm_time; | 324 soonest_alarm_time = cur_alarm_time; |
325 if (l_it->granularity < min_granularity) | 325 if (l_it->granularity < min_granularity) |
326 min_granularity = l_it->granularity; | 326 min_granularity = l_it->granularity; |
327 base::TimeDelta cur_alarm_delta = cur_alarm_time - clock_->Now(); | |
328 if (cur_alarm_delta < min_granularity) | |
329 min_granularity = cur_alarm_delta; | |
330 if (min_granularity < l_it->minimum_granularity) | |
Matt Perry
2014/01/16 22:58:34
I think these 2 ifs are reversed. The idea here is
Yoyo Zhou
2014/01/16 23:34:32
This lg to me.
iclelland
2014/01/17 14:54:32
I don't think it's reversed -- it should _increase
Matt Perry
2014/01/17 18:43:52
That's not the intended calculation. Each alarm's
| |
331 min_granularity = l_it->minimum_granularity; | |
327 } | 332 } |
328 } | 333 } |
329 | 334 |
330 base::Time next_poll(last_poll_time_ + min_granularity); | 335 base::Time next_poll(last_poll_time_ + min_granularity); |
331 // If the next alarm is more than min_granularity in the future, wait for it. | 336 // If the next alarm is more than min_granularity in the future, wait for it. |
332 // Otherwise, only poll as often as min_granularity. | 337 // Otherwise, only poll as often as min_granularity. |
333 // As a special case, if we've never checked for an alarm before | 338 // As a special case, if we've never checked for an alarm before |
334 // (e.g. during startup), let alarms fire asap. | 339 // (e.g. during startup), let alarms fire asap. |
335 if (last_poll_time_.is_null() || next_poll < soonest_alarm_time) | 340 if (last_poll_time_.is_null() || next_poll < soonest_alarm_time) |
336 next_poll = soonest_alarm_time; | 341 next_poll = soonest_alarm_time; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
419 Alarm::Alarm() | 424 Alarm::Alarm() |
420 : js_alarm(new api::alarms::Alarm()) { | 425 : js_alarm(new api::alarms::Alarm()) { |
421 } | 426 } |
422 | 427 |
423 Alarm::Alarm(const std::string& name, | 428 Alarm::Alarm(const std::string& name, |
424 const api::alarms::AlarmCreateInfo& create_info, | 429 const api::alarms::AlarmCreateInfo& create_info, |
425 base::TimeDelta min_granularity, | 430 base::TimeDelta min_granularity, |
426 base::Time now) | 431 base::Time now) |
427 : js_alarm(new api::alarms::Alarm()) { | 432 : js_alarm(new api::alarms::Alarm()) { |
428 js_alarm->name = name; | 433 js_alarm->name = name; |
434 minimum_granularity = min_granularity; | |
429 | 435 |
430 if (create_info.when.get()) { | 436 if (create_info.when.get()) { |
431 // Absolute scheduling. | 437 // Absolute scheduling. |
432 js_alarm->scheduled_time = *create_info.when; | 438 js_alarm->scheduled_time = *create_info.when; |
433 granularity = base::Time::FromJsTime(js_alarm->scheduled_time) - now; | 439 granularity = base::Time::FromJsTime(js_alarm->scheduled_time) - now; |
434 } else { | 440 } else { |
435 // Relative scheduling. | 441 // Relative scheduling. |
436 double* delay_in_minutes = create_info.delay_in_minutes.get(); | 442 double* delay_in_minutes = create_info.delay_in_minutes.get(); |
437 if (delay_in_minutes == NULL) | 443 if (delay_in_minutes == NULL) |
438 delay_in_minutes = create_info.period_in_minutes.get(); | 444 delay_in_minutes = create_info.period_in_minutes.get(); |
(...skipping 12 matching lines...) Expand all Loading... | |
451 if (create_info.period_in_minutes.get()) { | 457 if (create_info.period_in_minutes.get()) { |
452 js_alarm->period_in_minutes.reset( | 458 js_alarm->period_in_minutes.reset( |
453 new double(*create_info.period_in_minutes)); | 459 new double(*create_info.period_in_minutes)); |
454 } | 460 } |
455 } | 461 } |
456 | 462 |
457 Alarm::~Alarm() { | 463 Alarm::~Alarm() { |
458 } | 464 } |
459 | 465 |
460 } // namespace extensions | 466 } // namespace extensions |
OLD | NEW |