| 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 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 | 329 |
| 330 base::Time next_poll(last_poll_time_ + min_granularity); | 330 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. | 331 // If the next alarm is more than min_granularity in the future, wait for it. |
| 332 // Otherwise, only poll as often as min_granularity. | 332 // Otherwise, only poll as often as min_granularity. |
| 333 // As a special case, if we've never checked for an alarm before | 333 // As a special case, if we've never checked for an alarm before |
| 334 // (e.g. during startup), let alarms fire asap. | 334 // (e.g. during startup), let alarms fire asap. |
| 335 if (last_poll_time_.is_null() || next_poll < soonest_alarm_time) | 335 if (last_poll_time_.is_null() || next_poll < soonest_alarm_time) |
| 336 next_poll = soonest_alarm_time; | 336 next_poll = soonest_alarm_time; |
| 337 | 337 |
| 338 // Schedule the poll. | 338 // Schedule the poll. |
| 339 next_poll_time_ = next_poll; | 339 test_next_poll_time_ = next_poll; |
| 340 base::TimeDelta delay = std::max(base::TimeDelta::FromSeconds(0), | 340 base::TimeDelta delay = std::max(base::TimeDelta::FromSeconds(0), |
| 341 next_poll - clock_->Now()); | 341 next_poll - clock_->Now()); |
| 342 timer_.Start(FROM_HERE, | 342 timer_.Start(FROM_HERE, |
| 343 delay, | 343 delay, |
| 344 this, | 344 this, |
| 345 &AlarmManager::PollAlarms); | 345 &AlarmManager::PollAlarms); |
| 346 } | 346 } |
| 347 | 347 |
| 348 void AlarmManager::PollAlarms() { | 348 void AlarmManager::PollAlarms() { |
| 349 last_poll_time_ = clock_->Now(); | 349 last_poll_time_ = clock_->Now(); |
| 350 | 350 |
| 351 // Run any alarms scheduled in the past. OnAlarm uses vector::erase to remove | 351 // Run any alarms scheduled in the past. OnAlarm uses vector::erase to remove |
| 352 // elements from the AlarmList, and map::erase to remove AlarmLists from the | 352 // elements from the AlarmList, and map::erase to remove AlarmLists from the |
| 353 // AlarmMap. | 353 // AlarmMap. |
| 354 for (AlarmMap::iterator m_it = alarms_.begin(), m_end = alarms_.end(); | 354 for (AlarmMap::iterator m_it = alarms_.begin(), m_end = alarms_.end(); |
| 355 m_it != m_end;) { | 355 m_it != m_end;) { |
| 356 AlarmMap::iterator cur_extension = m_it++; | 356 AlarmMap::iterator cur_extension = m_it++; |
| 357 | 357 |
| 358 // Iterate (a) backwards so that removing elements doesn't affect | 358 // Iterate (a) backwards so that removing elements doesn't affect |
| 359 // upcoming iterations, and (b) with indices so that if the last | 359 // upcoming iterations, and (b) with indices so that if the last |
| 360 // iteration destroys the AlarmList, I'm not about to use the end | 360 // iteration destroys the AlarmList, I'm not about to use the end |
| 361 // iterator that the destruction invalidates. | 361 // iterator that the destruction invalidates. |
| 362 for (size_t i = cur_extension->second.size(); i > 0; --i) { | 362 for (size_t i = cur_extension->second.size(); i > 0; --i) { |
| 363 AlarmList::iterator cur_alarm = cur_extension->second.begin() + i - 1; | 363 AlarmList::iterator cur_alarm = cur_extension->second.begin() + i - 1; |
| 364 if (base::Time::FromJsTime(cur_alarm->js_alarm->scheduled_time) <= | 364 if (base::Time::FromJsTime(cur_alarm->js_alarm->scheduled_time) <= |
| 365 next_poll_time_) { | 365 last_poll_time_) { |
| 366 OnAlarm(make_pair(cur_extension, cur_alarm)); | 366 OnAlarm(make_pair(cur_extension, cur_alarm)); |
| 367 } | 367 } |
| 368 } | 368 } |
| 369 } | 369 } |
| 370 | 370 |
| 371 ScheduleNextPoll(); | 371 ScheduleNextPoll(); |
| 372 } | 372 } |
| 373 | 373 |
| 374 static void RemoveAllOnUninstallCallback() {} | 374 static void RemoveAllOnUninstallCallback() {} |
| 375 | 375 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 if (create_info.period_in_minutes.get()) { | 451 if (create_info.period_in_minutes.get()) { |
| 452 js_alarm->period_in_minutes.reset( | 452 js_alarm->period_in_minutes.reset( |
| 453 new double(*create_info.period_in_minutes)); | 453 new double(*create_info.period_in_minutes)); |
| 454 } | 454 } |
| 455 } | 455 } |
| 456 | 456 |
| 457 Alarm::~Alarm() { | 457 Alarm::~Alarm() { |
| 458 } | 458 } |
| 459 | 459 |
| 460 } // namespace extensions | 460 } // namespace extensions |
| OLD | NEW |