| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 "net/quic/chromium/quic_chromium_alarm_factory.h" | 5 #include "net/quic/quartc/quartc_alarm_factory.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | |
| 9 #include "base/logging.h" | 8 #include "base/logging.h" |
| 10 #include "base/metrics/sparse_histogram.h" | |
| 11 #include "base/task_runner.h" | 9 #include "base/task_runner.h" |
| 12 #include "base/time/time.h" | |
| 13 | 10 |
| 14 namespace net { | 11 namespace net { |
| 15 | 12 |
| 16 namespace { | 13 namespace { |
| 17 | 14 |
| 18 class QuicChromeAlarm : public QuicAlarm { | 15 class QuartcAlarm : public QuicAlarm { |
| 19 public: | 16 public: |
| 20 QuicChromeAlarm(const QuicClock* clock, | 17 QuartcAlarm(const QuicClock* clock, |
| 21 base::TaskRunner* task_runner, | 18 base::TaskRunner* task_runner, |
| 22 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate) | 19 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate) |
| 23 : QuicAlarm(std::move(delegate)), | 20 : QuicAlarm(std::move(delegate)), |
| 24 clock_(clock), | 21 clock_(clock), |
| 25 task_runner_(task_runner), | 22 task_runner_(task_runner), |
| 26 task_deadline_(QuicTime::Zero()), | 23 task_deadline_(QuicTime::Zero()), |
| 27 weak_factory_(this) {} | 24 weak_factory_(this) {} |
| 28 | 25 |
| 29 protected: | 26 protected: |
| 30 void SetImpl() override { | 27 void SetImpl() override { |
| 31 DCHECK(deadline().IsInitialized()); | 28 DCHECK(deadline().IsInitialized()); |
| 32 if (task_deadline_.IsInitialized()) { | 29 if (task_deadline_.IsInitialized()) { |
| 33 if (task_deadline_ <= deadline()) { | 30 if (task_deadline_ <= deadline()) { |
| 34 // Since tasks can not be un-posted, OnAlarm will be invoked which | 31 // 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 | 32 // will notice that deadline has not yet been reached, and will set |
| 36 // the alarm for the new deadline. | 33 // the alarm for the new deadline. |
| 37 return; | 34 return; |
| 38 } | 35 } |
| 39 // The scheduled task is after new deadline. Invalidate the weak ptrs | 36 // The scheduled task is after new deadline. Invalidate the weak ptrs |
| 40 // so that task does not execute when we're not expecting it. | 37 // so that task does not execute when we're not expecting it. |
| 41 weak_factory_.InvalidateWeakPtrs(); | 38 weak_factory_.InvalidateWeakPtrs(); |
| 42 } | 39 } |
| 43 | 40 |
| 44 int64_t delay_us = (deadline() - (clock_->Now())).ToMicroseconds(); | 41 int64_t delay_us = (deadline() - (clock_->Now())).ToMicroseconds(); |
| 45 if (delay_us < 0) { | 42 if (delay_us < 0) { |
| 46 delay_us = 0; | 43 delay_us = 0; |
| 47 } | 44 } |
| 48 task_runner_->PostDelayedTask( | 45 task_runner_->PostDelayedTask( |
| 49 FROM_HERE, | 46 FROM_HERE, |
| 50 base::Bind(&QuicChromeAlarm::OnAlarm, weak_factory_.GetWeakPtr()), | 47 base::Bind(&QuartcAlarm::OnAlarm, weak_factory_.GetWeakPtr()), |
| 51 base::TimeDelta::FromMicroseconds(delay_us)); | 48 base::TimeDelta::FromMicroseconds(delay_us)); |
| 52 task_deadline_ = deadline(); | 49 task_deadline_ = deadline(); |
| 53 } | 50 } |
| 54 | 51 |
| 55 void CancelImpl() override { | 52 void CancelImpl() override { |
| 56 DCHECK(!deadline().IsInitialized()); | 53 DCHECK(!deadline().IsInitialized()); |
| 57 // Since tasks can not be un-posted, OnAlarm will be invoked which | 54 // Since tasks can not be un-posted, OnAlarm will be invoked which |
| 58 // will notice that deadline is not Initialized and will do nothing. | 55 // will notice that deadline is not Initialized and will do nothing. |
| 59 } | 56 } |
| 60 | 57 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 77 } | 74 } |
| 78 | 75 |
| 79 const QuicClock* clock_; | 76 const QuicClock* clock_; |
| 80 base::TaskRunner* task_runner_; | 77 base::TaskRunner* task_runner_; |
| 81 // If a task has been posted to the message loop, this is the time it | 78 // 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 | 79 // 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 | 80 // 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 | 81 // post a new task when the new deadline now earlier than when |
| 85 // previously posted. | 82 // previously posted. |
| 86 QuicTime task_deadline_; | 83 QuicTime task_deadline_; |
| 87 base::WeakPtrFactory<QuicChromeAlarm> weak_factory_; | 84 base::WeakPtrFactory<QuartcAlarm> weak_factory_; |
| 88 }; | 85 }; |
| 89 | 86 |
| 90 } // namespace | 87 } // namespace |
| 91 | 88 |
| 92 QuicChromiumAlarmFactory::QuicChromiumAlarmFactory( | 89 QuartcAlarmFactory::QuartcAlarmFactory(base::TaskRunner* task_runner, |
| 93 base::TaskRunner* task_runner, | 90 const QuicClock* clock) |
| 94 const QuicClock* clock) | 91 : task_runner_(task_runner), clock_(clock) {} |
| 95 : task_runner_(task_runner), clock_(clock), weak_factory_(this) {} | |
| 96 | 92 |
| 97 QuicChromiumAlarmFactory::~QuicChromiumAlarmFactory() {} | 93 QuartcAlarmFactory::~QuartcAlarmFactory() {} |
| 98 | 94 |
| 99 QuicArenaScopedPtr<QuicAlarm> QuicChromiumAlarmFactory::CreateAlarm( | 95 QuicAlarm* QuartcAlarmFactory::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 96 return new QuartcAlarm(clock_, task_runner_, |
| 97 QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)); |
| 98 } |
| 99 |
| 100 QuicArenaScopedPtr<QuicAlarm> QuartcAlarmFactory::CreateAlarm( |
| 100 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, | 101 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate, |
| 101 QuicConnectionArena* arena) { | 102 QuicConnectionArena* arena) { |
| 102 if (arena != nullptr) { | 103 return QuicArenaScopedPtr<QuicAlarm>( |
| 103 return arena->New<QuicChromeAlarm>(clock_, task_runner_, | 104 new QuartcAlarm(clock_, task_runner_, std::move(delegate))); |
| 104 std::move(delegate)); | |
| 105 } else { | |
| 106 return QuicArenaScopedPtr<QuicAlarm>( | |
| 107 new QuicChromeAlarm(clock_, task_runner_, std::move(delegate))); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 QuicAlarm* QuicChromiumAlarmFactory::CreateAlarm( | |
| 112 QuicAlarm::Delegate* delegate) { | |
| 113 return new QuicChromeAlarm(clock_, task_runner_, | |
| 114 QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate)); | |
| 115 } | 105 } |
| 116 | 106 |
| 117 } // namespace net | 107 } // namespace net |
| OLD | NEW |