blob: 543bf09f801ccb17d47a3eb45d75545519257a60 [file] [log] [blame]
[email protected]1d197ef52012-11-07 20:41:291// 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
5#include "net/quic/quic_connection_helper.h"
6
7#include "base/location.h"
8#include "base/logging.h"
[email protected]120350b2013-09-03 22:03:199#include "base/metrics/sparse_histogram.h"
[email protected]1d197ef52012-11-07 20:41:2910#include "base/task_runner.h"
[email protected]f002abb2013-06-28 02:30:2111#include "base/time/time.h"
[email protected]acf24742012-11-14 07:28:2412#include "net/base/io_buffer.h"
[email protected]ccc66e8a2013-03-26 08:26:1413#include "net/base/net_errors.h"
[email protected]1d197ef52012-11-07 20:41:2914#include "net/quic/quic_utils.h"
15
16namespace net {
17
[email protected]965dbe62013-08-09 21:34:3118namespace {
19
20class QuicChromeAlarm : public QuicAlarm {
21 public:
22 QuicChromeAlarm(const QuicClock* clock,
23 base::TaskRunner* task_runner,
24 QuicAlarm::Delegate* delegate)
25 : QuicAlarm(delegate),
26 clock_(clock),
27 task_runner_(task_runner),
[email protected]9d0e0d12013-10-15 06:58:1128 task_deadline_(QuicTime::Zero()),
[email protected]965dbe62013-08-09 21:34:3129 weak_factory_(this) {}
30
31 protected:
32 virtual void SetImpl() OVERRIDE {
33 DCHECK(deadline().IsInitialized());
[email protected]9d0e0d12013-10-15 06:58:1134 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.
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();
[email protected]965dbe62013-08-09 21:34:3144 }
45
46 int64 delay_us = deadline().Subtract(clock_->Now()).ToMicroseconds();
47 if (delay_us < 0) {
48 delay_us = 0;
49 }
50 task_runner_->PostDelayedTask(
51 FROM_HERE,
52 base::Bind(&QuicChromeAlarm::OnAlarm, weak_factory_.GetWeakPtr()),
53 base::TimeDelta::FromMicroseconds(delay_us));
[email protected]9d0e0d12013-10-15 06:58:1154 task_deadline_ = deadline();
[email protected]965dbe62013-08-09 21:34:3155 }
56
57 virtual 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.
61 }
62
63 private:
64 void OnAlarm() {
[email protected]9d0e0d12013-10-15 06:58:1165 DCHECK(task_deadline_.IsInitialized());
66 task_deadline_ = QuicTime::Zero();
[email protected]965dbe62013-08-09 21:34:3167 // The alarm may have been cancelled.
68 if (!deadline().IsInitialized()) {
69 return;
70 }
71
72 // The alarm may have been re-set to a later time.
73 if (clock_->Now() < deadline()) {
74 SetImpl();
75 return;
76 }
77
78 Fire();
79 }
80
81 const QuicClock* clock_;
82 base::TaskRunner* task_runner_;
[email protected]9d0e0d12013-10-15 06:58:1183 // 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_;
[email protected]965dbe62013-08-09 21:34:3189 base::WeakPtrFactory<QuicChromeAlarm> weak_factory_;
90};
91
92} // namespace
93
[email protected]1d197ef52012-11-07 20:41:2994QuicConnectionHelper::QuicConnectionHelper(base::TaskRunner* task_runner,
[email protected]97693d12012-11-16 16:05:0095 const QuicClock* clock,
[email protected]cbd731e2013-10-24 00:20:3996 QuicRandom* random_generator)
[email protected]aa249b52013-04-30 01:04:3297 : weak_factory_(this),
[email protected]1d197ef52012-11-07 20:41:2998 task_runner_(task_runner),
[email protected]1d197ef52012-11-07 20:41:2999 clock_(clock),
[email protected]965dbe62013-08-09 21:34:31100 random_generator_(random_generator) {
[email protected]1d197ef52012-11-07 20:41:29101}
102
103QuicConnectionHelper::~QuicConnectionHelper() {
104}
105
[email protected]97693d12012-11-16 16:05:00106const QuicClock* QuicConnectionHelper::GetClock() const {
[email protected]1d197ef52012-11-07 20:41:29107 return clock_;
108}
109
[email protected]9558c5d32012-12-22 00:08:14110QuicRandom* QuicConnectionHelper::GetRandomGenerator() {
111 return random_generator_;
112}
113
[email protected]965dbe62013-08-09 21:34:31114QuicAlarm* QuicConnectionHelper::CreateAlarm(QuicAlarm::Delegate* delegate) {
115 return new QuicChromeAlarm(clock_, task_runner_, delegate);
[email protected]89503262013-01-17 02:08:55116}
117
[email protected]1d197ef52012-11-07 20:41:29118} // namespace net