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

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

Issue 1908103002: Landing Recent QUIC changes until 4/15/2016 17:20 UTC (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix 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
« no previous file with comments | « net/quic/quic_chromium_alarm_factory.h ('k') | net/quic/quic_chromium_alarm_factory_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "net/quic/quic_chromium_alarm_factory.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/metrics/sparse_histogram.h"
11 #include "base/task_runner.h"
12 #include "base/time/time.h"
13
14 namespace net {
15
16 namespace {
17
18 class QuicChromeAlarm : public QuicAlarm {
19 public:
20 QuicChromeAlarm(const QuicClock* clock,
21 base::TaskRunner* task_runner,
22 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate)
23 : QuicAlarm(std::move(delegate)),
24 clock_(clock),
25 task_runner_(task_runner),
26 task_deadline_(QuicTime::Zero()),
27 weak_factory_(this) {}
28
29 protected:
30 void SetImpl() override {
31 DCHECK(deadline().IsInitialized());
32 if (task_deadline_.IsInitialized()) {
33 if (task_deadline_ <= deadline()) {
34 // 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
36 // the alarm for the new deadline.
37 return;
38 }
39 // The scheduled task is after new deadline. Invalidate the weak ptrs
40 // so that task does not execute when we're not expecting it.
41 weak_factory_.InvalidateWeakPtrs();
42 }
43
44 int64_t delay_us = deadline().Subtract(clock_->Now()).ToMicroseconds();
45 if (delay_us < 0) {
46 delay_us = 0;
47 }
48 task_runner_->PostDelayedTask(
49 FROM_HERE,
50 base::Bind(&QuicChromeAlarm::OnAlarm, weak_factory_.GetWeakPtr()),
51 base::TimeDelta::FromMicroseconds(delay_us));
52 task_deadline_ = deadline();
53 }
54
55 void CancelImpl() override {
56 DCHECK(!deadline().IsInitialized());
57 // Since tasks can not be un-posted, OnAlarm will be invoked which
58 // will notice that deadline is not Initialized and will do nothing.
59 }
60
61 private:
62 void OnAlarm() {
63 DCHECK(task_deadline_.IsInitialized());
64 task_deadline_ = QuicTime::Zero();
65 // The alarm may have been cancelled.
66 if (!deadline().IsInitialized()) {
67 return;
68 }
69
70 // The alarm may have been re-set to a later time.
71 if (clock_->Now() < deadline()) {
72 SetImpl();
73 return;
74 }
75
76 Fire();
77 }
78
79 const QuicClock* clock_;
80 base::TaskRunner* task_runner_;
81 // 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
83 // 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
85 // previously posted.
86 QuicTime task_deadline_;
87 base::WeakPtrFactory<QuicChromeAlarm> weak_factory_;
88 };
89
90 } // namespace
91
92 QuicChromiumAlarmFactory::QuicChromiumAlarmFactory(
93 base::TaskRunner* task_runner,
94 const QuicClock* clock)
95 : task_runner_(task_runner), clock_(clock), weak_factory_(this) {}
96
97 QuicChromiumAlarmFactory::~QuicChromiumAlarmFactory() {}
98
99 QuicArenaScopedPtr<QuicAlarm> QuicChromiumAlarmFactory::CreateAlarm(
100 QuicArenaScopedPtr<QuicAlarm::Delegate> delegate,
101 QuicConnectionArena* arena) {
102 if (arena != nullptr) {
103 return arena->New<QuicChromeAlarm>(clock_, task_runner_,
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 }
116
117 } // namespace net
OLDNEW
« no previous file with comments | « net/quic/quic_chromium_alarm_factory.h ('k') | net/quic/quic_chromium_alarm_factory_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698