Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(435)

Side by Side Diff: net/quic/quic_chromium_connection_helper.cc

Issue 1898793003: Make QuicDispatcher's helper and alarm factory arguments unique_ptrs to make ownership clear. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@119871679
Patch Set: fixing rebase Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_chromium_connection_helper.h" 5 #include "net/quic/quic_chromium_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 {
19
20 class QuicChromeAlarm : public QuicAlarm {
21 public:
22 QuicChromeAlarm(const QuicClock* clock,
23 base::TaskRunner* task_runner,
24 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate)
25 : QuicAlarm(std::move(delegate)),
26 clock_(clock),
27 task_runner_(task_runner),
28 task_deadline_(QuicTime::Zero()),
29 weak_factory_(this) {}
30
31 protected:
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.
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();
44 }
45
46 int64_t 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));
54 task_deadline_ = deadline();
55 }
56
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.
61 }
62
63 private:
64 void OnAlarm() {
65 DCHECK(task_deadline_.IsInitialized());
66 task_deadline_ = QuicTime::Zero();
67 // 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_;
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_;
89 base::WeakPtrFactory<QuicChromeAlarm> weak_factory_;
90 };
91
92 } // namespace
93
94 QuicChromiumConnectionHelper::QuicChromiumConnectionHelper( 18 QuicChromiumConnectionHelper::QuicChromiumConnectionHelper(
95 base::TaskRunner* task_runner,
96 const QuicClock* clock, 19 const QuicClock* clock,
97 QuicRandom* random_generator) 20 QuicRandom* random_generator)
98 : task_runner_(task_runner), 21 : clock_(clock), random_generator_(random_generator), weak_factory_(this) {}
99 clock_(clock),
100 random_generator_(random_generator),
101 weak_factory_(this) {}
102 22
103 QuicChromiumConnectionHelper::~QuicChromiumConnectionHelper() {} 23 QuicChromiumConnectionHelper::~QuicChromiumConnectionHelper() {}
104 24
105 const QuicClock* QuicChromiumConnectionHelper::GetClock() const { 25 const QuicClock* QuicChromiumConnectionHelper::GetClock() const {
106 return clock_; 26 return clock_;
107 } 27 }
108 28
109 QuicRandom* QuicChromiumConnectionHelper::GetRandomGenerator() { 29 QuicRandom* QuicChromiumConnectionHelper::GetRandomGenerator() {
110 return random_generator_; 30 return random_generator_;
111 } 31 }
112 32
113 QuicArenaScopedPtr<QuicAlarm> QuicChromiumConnectionHelper::CreateAlarm(
114 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate,
115 QuicConnectionArena* arena) {
116 if (arena != nullptr) {
117 return arena->New<QuicChromeAlarm>(clock_, task_runner_,
118 std::move(delegate));
119 } else {
120 return QuicArenaScopedPtr<QuicAlarm>(
121 new QuicChromeAlarm(clock_, task_runner_, std::move(delegate)));
122 }
123 }
124
125 QuicAlarm* QuicChromiumConnectionHelper::CreateAlarm(
126 QuicAlarm::Delegate* delegate) {
127 return new QuicChromeAlarm(clock_, task_runner_,
128 QuicArenaScopedPtr<QuicAlarm::Delegate>(delegate));
129 }
130
131 QuicBufferAllocator* QuicChromiumConnectionHelper::GetBufferAllocator() { 33 QuicBufferAllocator* QuicChromiumConnectionHelper::GetBufferAllocator() {
132 return &buffer_allocator_; 34 return &buffer_allocator_;
133 } 35 }
134 36
135 } // namespace net 37 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/quic_chromium_connection_helper.h ('k') | net/quic/quic_chromium_connection_helper_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698