blob: 5475bfb8b19de1258dd7f2a9e147383ec1a45478 [file] [log] [blame]
rch16c74d1d2016-04-22 06:14:071// 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
Ryan Hamiltona3ee93a72018-08-01 22:03:085#include "net/quic/quic_chromium_alarm_factory.h"
rch16c74d1d2016-04-22 06:14:076
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
14namespace net {
15
16namespace {
17
Ryan Hamilton8d9ee76e2018-05-29 23:52:5218class QuicChromeAlarm : public quic::QuicAlarm {
rch16c74d1d2016-04-22 06:14:0719 public:
Ryan Hamilton8d9ee76e2018-05-29 23:52:5220 QuicChromeAlarm(const quic::QuicClock* clock,
rch16c74d1d2016-04-22 06:14:0721 base::TaskRunner* task_runner,
Ryan Hamilton8d9ee76e2018-05-29 23:52:5222 quic::QuicArenaScopedPtr<quic::QuicAlarm::Delegate> delegate)
23 : quic::QuicAlarm(std::move(delegate)),
rch16c74d1d2016-04-22 06:14:0724 clock_(clock),
25 task_runner_(task_runner),
Ryan Hamilton8d9ee76e2018-05-29 23:52:5226 task_deadline_(quic::QuicTime::Zero()),
rch16c74d1d2016-04-22 06:14:0727 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
ckrasic6567aa52016-07-08 09:24:3544 int64_t delay_us = (deadline() - (clock_->Now())).ToMicroseconds();
rch16c74d1d2016-04-22 06:14:0745 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());
Ryan Hamilton8d9ee76e2018-05-29 23:52:5264 task_deadline_ = quic::QuicTime::Zero();
rch16c74d1d2016-04-22 06:14:0765 // 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
Ryan Hamilton8d9ee76e2018-05-29 23:52:5279 const quic::QuicClock* clock_;
rch16c74d1d2016-04-22 06:14:0780 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.
Ryan Hamilton8d9ee76e2018-05-29 23:52:5286 quic::QuicTime task_deadline_;
rch16c74d1d2016-04-22 06:14:0787 base::WeakPtrFactory<QuicChromeAlarm> weak_factory_;
88};
89
90} // namespace
91
92QuicChromiumAlarmFactory::QuicChromiumAlarmFactory(
93 base::TaskRunner* task_runner,
Ryan Hamilton8d9ee76e2018-05-29 23:52:5294 const quic::QuicClock* clock)
rch16c74d1d2016-04-22 06:14:0795 : task_runner_(task_runner), clock_(clock), weak_factory_(this) {}
96
97QuicChromiumAlarmFactory::~QuicChromiumAlarmFactory() {}
98
Ryan Hamilton8d9ee76e2018-05-29 23:52:5299quic::QuicArenaScopedPtr<quic::QuicAlarm> QuicChromiumAlarmFactory::CreateAlarm(
100 quic::QuicArenaScopedPtr<quic::QuicAlarm::Delegate> delegate,
101 quic::QuicConnectionArena* arena) {
rch16c74d1d2016-04-22 06:14:07102 if (arena != nullptr) {
103 return arena->New<QuicChromeAlarm>(clock_, task_runner_,
104 std::move(delegate));
105 } else {
Ryan Hamilton8d9ee76e2018-05-29 23:52:52106 return quic::QuicArenaScopedPtr<quic::QuicAlarm>(
rch16c74d1d2016-04-22 06:14:07107 new QuicChromeAlarm(clock_, task_runner_, std::move(delegate)));
108 }
109}
110
Ryan Hamilton8d9ee76e2018-05-29 23:52:52111quic::QuicAlarm* QuicChromiumAlarmFactory::CreateAlarm(
112 quic::QuicAlarm::Delegate* delegate) {
113 return new QuicChromeAlarm(
114 clock_, task_runner_,
115 quic::QuicArenaScopedPtr<quic::QuicAlarm::Delegate>(delegate));
rch16c74d1d2016-04-22 06:14:07116}
117
118} // namespace net