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.
5 #include "net/quic/quic_connection_helper.h"
7 #include "base/location.h"
8 #include "base/logging.h"
9 #include "base/metrics/sparse_histogram.h"
10 #include "base/task_runner.h"
11 #include "base/time/time.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
14 #include "net/quic/quic_utils.h"
20 class QuicChromeAlarm
: public QuicAlarm
{
22 QuicChromeAlarm(const QuicClock
* clock
,
23 base::TaskRunner
* task_runner
,
24 QuicAlarm::Delegate
* delegate
)
25 : QuicAlarm(delegate
),
27 task_runner_(task_runner
),
28 task_deadline_(QuicTime::Zero()),
29 weak_factory_(this) {}
32 void SetImpl() override
{
33 DCHECK(deadline().IsInitialized());
34 if (task_deadline_
.IsInitialized()) {
35 if (task_deadline_
<= deadline()) {
36 // Since tasks can not be un-posted, OnAlarm will be invoked which
37 // will notice that deadline has not yet been reached, and will set
38 // the alarm for the new deadline.
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();
46 int64 delay_us
= deadline().Subtract(clock_
->Now()).ToMicroseconds();
50 task_runner_
->PostDelayedTask(
52 base::Bind(&QuicChromeAlarm::OnAlarm
, weak_factory_
.GetWeakPtr()),
53 base::TimeDelta::FromMicroseconds(delay_us
));
54 task_deadline_
= deadline();
57 void CancelImpl() override
{
58 DCHECK(!deadline().IsInitialized());
59 // Since tasks can not be un-posted, OnAlarm will be invoked which
60 // will notice that deadline is not Initialized and will do nothing.
65 DCHECK(task_deadline_
.IsInitialized());
66 task_deadline_
= QuicTime::Zero();
67 // The alarm may have been cancelled.
68 if (!deadline().IsInitialized()) {
72 // The alarm may have been re-set to a later time.
73 if (clock_
->Now() < deadline()) {
81 const QuicClock
* clock_
;
82 base::TaskRunner
* task_runner_
;
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
88 QuicTime task_deadline_
;
89 base::WeakPtrFactory
<QuicChromeAlarm
> weak_factory_
;
94 QuicConnectionHelper::QuicConnectionHelper(base::TaskRunner
* task_runner
,
95 const QuicClock
* clock
,
96 QuicRandom
* random_generator
)
97 : task_runner_(task_runner
),
99 random_generator_(random_generator
),
100 weak_factory_(this) {
103 QuicConnectionHelper::~QuicConnectionHelper() {
106 const QuicClock
* QuicConnectionHelper::GetClock() const {
110 QuicRandom
* QuicConnectionHelper::GetRandomGenerator() {
111 return random_generator_
;
114 QuicAlarm
* QuicConnectionHelper::CreateAlarm(QuicAlarm::Delegate
* delegate
) {
115 return new QuicChromeAlarm(clock_
, task_runner_
, delegate
);