| 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 "net/quic/quic_connection_helper.h" | 5 #include "net/quic/quic_connection_helper.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/sparse_histogram.h" | 9 #include "base/metrics/sparse_histogram.h" |
| 10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/quic/quic_utils.h" | 14 #include "net/quic/quic_utils.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class QuicChromeAlarm : public QuicAlarm { | 20 class QuicChromeAlarm : public QuicAlarm { |
| 21 public: | 21 public: |
| 22 QuicChromeAlarm(const QuicClock* clock, | 22 QuicChromeAlarm(const QuicClock* clock, |
| 23 base::TaskRunner* task_runner, | 23 base::TaskRunner* task_runner, |
| 24 QuicAlarm::Delegate* delegate) | 24 QuicAlarm::Delegate* delegate) |
| 25 : QuicAlarm(delegate), | 25 : QuicAlarm(delegate), |
| 26 clock_(clock), | 26 clock_(clock), |
| 27 task_runner_(task_runner), | 27 task_runner_(task_runner), |
| 28 task_posted_(false), | 28 task_deadline_(QuicTime::Zero()), |
| 29 weak_factory_(this) {} | 29 weak_factory_(this) {} |
| 30 | 30 |
| 31 protected: | 31 protected: |
| 32 virtual void SetImpl() OVERRIDE { | 32 virtual void SetImpl() OVERRIDE { |
| 33 DCHECK(deadline().IsInitialized()); | 33 DCHECK(deadline().IsInitialized()); |
| 34 if (task_posted_) { | 34 if (task_deadline_.IsInitialized()) { |
| 35 // Since tasks can not be un-posted, OnAlarm will be invoked which | 35 if (task_deadline_ <= deadline()) { |
| 36 // will notice that deadline has not yet been reached, and will set | 36 // Since tasks can not be un-posted, OnAlarm will be invoked which |
| 37 // the alarm for the new deadline. | 37 // will notice that deadline has not yet been reached, and will set |
| 38 return; | 38 // the alarm for the new deadline. |
| 39 return; |
| 40 } |
| 41 // The scheduled task is after new deadline. Invalidate the weak ptrs |
| 42 // so that task does not execute when we're not expecting it. |
| 43 weak_factory_.InvalidateWeakPtrs(); |
| 39 } | 44 } |
| 40 | 45 |
| 41 int64 delay_us = deadline().Subtract(clock_->Now()).ToMicroseconds(); | 46 int64 delay_us = deadline().Subtract(clock_->Now()).ToMicroseconds(); |
| 42 if (delay_us < 0) { | 47 if (delay_us < 0) { |
| 43 delay_us = 0; | 48 delay_us = 0; |
| 44 } | 49 } |
| 45 task_runner_->PostDelayedTask( | 50 task_runner_->PostDelayedTask( |
| 46 FROM_HERE, | 51 FROM_HERE, |
| 47 base::Bind(&QuicChromeAlarm::OnAlarm, weak_factory_.GetWeakPtr()), | 52 base::Bind(&QuicChromeAlarm::OnAlarm, weak_factory_.GetWeakPtr()), |
| 48 base::TimeDelta::FromMicroseconds(delay_us)); | 53 base::TimeDelta::FromMicroseconds(delay_us)); |
| 49 task_posted_ = true; | 54 task_deadline_ = deadline(); |
| 50 } | 55 } |
| 51 | 56 |
| 52 virtual void CancelImpl() OVERRIDE { | 57 virtual void CancelImpl() OVERRIDE { |
| 53 DCHECK(!deadline().IsInitialized()); | 58 DCHECK(!deadline().IsInitialized()); |
| 54 // Since tasks can not be un-posted, OnAlarm will be invoked which | 59 // Since tasks can not be un-posted, OnAlarm will be invoked which |
| 55 // will notice that deadline is not Initialized and will do nothing. | 60 // will notice that deadline is not Initialized and will do nothing. |
| 56 } | 61 } |
| 57 | 62 |
| 58 private: | 63 private: |
| 59 void OnAlarm() { | 64 void OnAlarm() { |
| 60 DCHECK(task_posted_); | 65 DCHECK(task_deadline_.IsInitialized()); |
| 61 task_posted_ = false; | 66 task_deadline_ = QuicTime::Zero(); |
| 62 // The alarm may have been cancelled. | 67 // The alarm may have been cancelled. |
| 63 if (!deadline().IsInitialized()) { | 68 if (!deadline().IsInitialized()) { |
| 64 return; | 69 return; |
| 65 } | 70 } |
| 66 | 71 |
| 67 // The alarm may have been re-set to a later time. | 72 // The alarm may have been re-set to a later time. |
| 68 if (clock_->Now() < deadline()) { | 73 if (clock_->Now() < deadline()) { |
| 69 SetImpl(); | 74 SetImpl(); |
| 70 return; | 75 return; |
| 71 } | 76 } |
| 72 | 77 |
| 73 Fire(); | 78 Fire(); |
| 74 } | 79 } |
| 75 | 80 |
| 76 const QuicClock* clock_; | 81 const QuicClock* clock_; |
| 77 base::TaskRunner* task_runner_; | 82 base::TaskRunner* task_runner_; |
| 78 bool task_posted_; | 83 // If a task has been posted to the message loop, this is the time it |
| 84 // was scheduled to fire. Tracking this allows us to avoid posting a |
| 85 // new tast if the new deadline is in the future, but permits us to |
| 86 // post a new task when the new deadline now earlier than when |
| 87 // previously posted. |
| 88 QuicTime task_deadline_; |
| 79 base::WeakPtrFactory<QuicChromeAlarm> weak_factory_; | 89 base::WeakPtrFactory<QuicChromeAlarm> weak_factory_; |
| 80 }; | 90 }; |
| 81 | 91 |
| 82 } // namespace | 92 } // namespace |
| 83 | 93 |
| 84 QuicConnectionHelper::QuicConnectionHelper(base::TaskRunner* task_runner, | 94 QuicConnectionHelper::QuicConnectionHelper(base::TaskRunner* task_runner, |
| 85 const QuicClock* clock, | 95 const QuicClock* clock, |
| 86 QuicRandom* random_generator, | 96 QuicRandom* random_generator, |
| 87 DatagramClientSocket* socket) | 97 DatagramClientSocket* socket) |
| 88 : weak_factory_(this), | 98 : weak_factory_(this), |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 QuicAlarm* QuicConnectionHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { | 158 QuicAlarm* QuicConnectionHelper::CreateAlarm(QuicAlarm::Delegate* delegate) { |
| 149 return new QuicChromeAlarm(clock_, task_runner_, delegate); | 159 return new QuicChromeAlarm(clock_, task_runner_, delegate); |
| 150 } | 160 } |
| 151 | 161 |
| 152 void QuicConnectionHelper::OnWriteComplete(int result) { | 162 void QuicConnectionHelper::OnWriteComplete(int result) { |
| 153 // TODO(rch): Inform the connection about the result. | 163 // TODO(rch): Inform the connection about the result. |
| 154 connection_->OnCanWrite(); | 164 connection_->OnCanWrite(); |
| 155 } | 165 } |
| 156 | 166 |
| 157 } // namespace net | 167 } // namespace net |
| OLD | NEW |