rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
Ryan Hamilton | a3ee93a7 | 2018-08-01 22:03:08 | [diff] [blame] | 5 | #include "net/quic/quic_chromium_alarm_factory.h" |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/location.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/metrics/sparse_histogram.h" |
| 11 | #include "base/task_runner.h" |
| 12 | #include "base/time/time.h" |
| 13 | |
| 14 | namespace net { |
| 15 | |
| 16 | namespace { |
| 17 | |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 18 | class QuicChromeAlarm : public quic::QuicAlarm { |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 19 | public: |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 20 | QuicChromeAlarm(const quic::QuicClock* clock, |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 21 | base::TaskRunner* task_runner, |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 22 | quic::QuicArenaScopedPtr<quic::QuicAlarm::Delegate> delegate) |
| 23 | : quic::QuicAlarm(std::move(delegate)), |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 24 | clock_(clock), |
| 25 | task_runner_(task_runner), |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 26 | task_deadline_(quic::QuicTime::Zero()), |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 27 | weak_factory_(this) {} |
| 28 | |
| 29 | protected: |
| 30 | void SetImpl() override { |
| 31 | DCHECK(deadline().IsInitialized()); |
| 32 | if (task_deadline_.IsInitialized()) { |
| 33 | if (task_deadline_ <= deadline()) { |
| 34 | // Since tasks can not be un-posted, OnAlarm will be invoked which |
| 35 | // will notice that deadline has not yet been reached, and will set |
| 36 | // the alarm for the new deadline. |
| 37 | return; |
| 38 | } |
| 39 | // The scheduled task is after new deadline. Invalidate the weak ptrs |
| 40 | // so that task does not execute when we're not expecting it. |
| 41 | weak_factory_.InvalidateWeakPtrs(); |
| 42 | } |
| 43 | |
ckrasic | 6567aa5 | 2016-07-08 09:24:35 | [diff] [blame] | 44 | int64_t delay_us = (deadline() - (clock_->Now())).ToMicroseconds(); |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 45 | if (delay_us < 0) { |
| 46 | delay_us = 0; |
| 47 | } |
| 48 | task_runner_->PostDelayedTask( |
| 49 | FROM_HERE, |
| 50 | base::Bind(&QuicChromeAlarm::OnAlarm, weak_factory_.GetWeakPtr()), |
| 51 | base::TimeDelta::FromMicroseconds(delay_us)); |
| 52 | task_deadline_ = deadline(); |
| 53 | } |
| 54 | |
| 55 | void CancelImpl() override { |
| 56 | DCHECK(!deadline().IsInitialized()); |
| 57 | // Since tasks can not be un-posted, OnAlarm will be invoked which |
| 58 | // will notice that deadline is not Initialized and will do nothing. |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | void OnAlarm() { |
| 63 | DCHECK(task_deadline_.IsInitialized()); |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 64 | task_deadline_ = quic::QuicTime::Zero(); |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 65 | // The alarm may have been cancelled. |
| 66 | if (!deadline().IsInitialized()) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | // The alarm may have been re-set to a later time. |
| 71 | if (clock_->Now() < deadline()) { |
| 72 | SetImpl(); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | Fire(); |
| 77 | } |
| 78 | |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 79 | const quic::QuicClock* clock_; |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 80 | base::TaskRunner* task_runner_; |
| 81 | // If a task has been posted to the message loop, this is the time it |
| 82 | // was scheduled to fire. Tracking this allows us to avoid posting a |
| 83 | // new tast if the new deadline is in the future, but permits us to |
| 84 | // post a new task when the new deadline now earlier than when |
| 85 | // previously posted. |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 86 | quic::QuicTime task_deadline_; |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 87 | base::WeakPtrFactory<QuicChromeAlarm> weak_factory_; |
| 88 | }; |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | QuicChromiumAlarmFactory::QuicChromiumAlarmFactory( |
| 93 | base::TaskRunner* task_runner, |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 94 | const quic::QuicClock* clock) |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 95 | : task_runner_(task_runner), clock_(clock), weak_factory_(this) {} |
| 96 | |
| 97 | QuicChromiumAlarmFactory::~QuicChromiumAlarmFactory() {} |
| 98 | |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 99 | quic::QuicArenaScopedPtr<quic::QuicAlarm> QuicChromiumAlarmFactory::CreateAlarm( |
| 100 | quic::QuicArenaScopedPtr<quic::QuicAlarm::Delegate> delegate, |
| 101 | quic::QuicConnectionArena* arena) { |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 102 | if (arena != nullptr) { |
| 103 | return arena->New<QuicChromeAlarm>(clock_, task_runner_, |
| 104 | std::move(delegate)); |
| 105 | } else { |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 106 | return quic::QuicArenaScopedPtr<quic::QuicAlarm>( |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 107 | new QuicChromeAlarm(clock_, task_runner_, std::move(delegate))); |
| 108 | } |
| 109 | } |
| 110 | |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 111 | quic::QuicAlarm* QuicChromiumAlarmFactory::CreateAlarm( |
| 112 | quic::QuicAlarm::Delegate* delegate) { |
| 113 | return new QuicChromeAlarm( |
| 114 | clock_, task_runner_, |
| 115 | quic::QuicArenaScopedPtr<quic::QuicAlarm::Delegate>(delegate)); |
rch | 16c74d1d | 2016-04-22 06:14:07 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | } // namespace net |