| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/timers/alarm_timer.h" | 5 #include "components/timers/alarm_timer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 can_wake_from_suspend_(false), | 31 can_wake_from_suspend_(false), |
| 32 origin_message_loop_(NULL), | 32 origin_message_loop_(NULL), |
| 33 weak_factory_(this) { | 33 weak_factory_(this) { |
| 34 can_wake_from_suspend_ = delegate_->Init(weak_factory_.GetWeakPtr()); | 34 can_wake_from_suspend_ = delegate_->Init(weak_factory_.GetWeakPtr()); |
| 35 } | 35 } |
| 36 | 36 |
| 37 AlarmTimer::~AlarmTimer() { | 37 AlarmTimer::~AlarmTimer() { |
| 38 Stop(); | 38 Stop(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 void AlarmTimer::OnTimerFired() { |
| 42 if (!base::Timer::IsRunning()) |
| 43 return; |
| 44 |
| 45 DCHECK(pending_task_.get()); |
| 46 |
| 47 // Take ownership of the pending user task, which is going to be cleared by |
| 48 // the Stop() or Reset() functions below. |
| 49 scoped_ptr<base::PendingTask> pending_user_task(pending_task_.Pass()); |
| 50 |
| 51 // Re-schedule or stop the timer as requested. |
| 52 if (base::Timer::is_repeating()) |
| 53 Reset(); |
| 54 else |
| 55 Stop(); |
| 56 |
| 57 // Now run the user task. |
| 58 base::MessageLoop::current()->task_annotator()->RunTask( |
| 59 "AlarmTimer::Reset", "AlarmTimer::OnTimerFired", *pending_user_task); |
| 60 } |
| 61 |
| 41 void AlarmTimer::Stop() { | 62 void AlarmTimer::Stop() { |
| 42 if (!can_wake_from_suspend_) { | 63 if (!can_wake_from_suspend_) { |
| 43 base::Timer::Stop(); | 64 base::Timer::Stop(); |
| 44 return; | 65 return; |
| 45 } | 66 } |
| 46 | 67 |
| 47 // Clear the running flag, stop the delegate, and delete the pending task. | 68 // Clear the running flag, stop the delegate, and delete the pending task. |
| 48 base::Timer::set_is_running(false); | 69 base::Timer::set_is_running(false); |
| 49 delegate_->Stop(); | 70 delegate_->Stop(); |
| 50 pending_task_.reset(); | 71 pending_task_.reset(); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 | 117 |
| 97 // Now start up the timer. | 118 // Now start up the timer. |
| 98 delegate_->Reset(base::Timer::GetCurrentDelay()); | 119 delegate_->Reset(base::Timer::GetCurrentDelay()); |
| 99 base::Timer::set_is_running(true); | 120 base::Timer::set_is_running(true); |
| 100 } | 121 } |
| 101 | 122 |
| 102 void AlarmTimer::WillDestroyCurrentMessageLoop() { | 123 void AlarmTimer::WillDestroyCurrentMessageLoop() { |
| 103 Stop(); | 124 Stop(); |
| 104 } | 125 } |
| 105 | 126 |
| 106 void AlarmTimer::OnTimerFired() { | |
| 107 if (!base::Timer::is_running()) | |
| 108 return; | |
| 109 | |
| 110 DCHECK(pending_task_.get()); | |
| 111 | |
| 112 // Take ownership of the pending user task, which is going to be cleared by | |
| 113 // the Stop() or Reset() functions below. | |
| 114 scoped_ptr<base::PendingTask> pending_user_task(pending_task_.Pass()); | |
| 115 | |
| 116 // Re-schedule or stop the timer as requested. | |
| 117 if (base::Timer::is_repeating()) | |
| 118 Reset(); | |
| 119 else | |
| 120 Stop(); | |
| 121 | |
| 122 // Now run the user task. | |
| 123 base::MessageLoop::current()->task_annotator()->RunTask( | |
| 124 "AlarmTimer::Reset", "AlarmTimer::OnTimerFired", *pending_user_task); | |
| 125 } | |
| 126 | |
| 127 } // namespace timers | 127 } // namespace timers |
| OLD | NEW |