Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / net / quic / quic_alarm.cc
blobb5aca8caa175aae288f0d6f7c23f39b61bfed29a
1 // Copyright 2013 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_alarm.h"
7 #include "base/logging.h"
9 namespace net {
11 QuicAlarm::QuicAlarm(Delegate* delegate)
12 : delegate_(delegate),
13 deadline_(QuicTime::Zero()) {
16 QuicAlarm::~QuicAlarm() {}
18 void QuicAlarm::Set(QuicTime deadline) {
19 DCHECK(!IsSet());
20 DCHECK(deadline.IsInitialized());
21 deadline_ = deadline;
22 SetImpl();
25 void QuicAlarm::Cancel() {
26 deadline_ = QuicTime::Zero();
27 CancelImpl();
30 void QuicAlarm::Update(QuicTime deadline, QuicTime::Delta granularity) {
31 if (!deadline.IsInitialized()) {
32 Cancel();
33 return;
35 if (std::abs(deadline.Subtract(deadline_).ToMicroseconds()) <
36 granularity.ToMicroseconds()) {
37 return;
39 Cancel();
40 Set(deadline);
43 bool QuicAlarm::IsSet() const {
44 return deadline_.IsInitialized();
47 void QuicAlarm::Fire() {
48 if (!deadline_.IsInitialized()) {
49 return;
52 deadline_ = QuicTime::Zero();
53 QuicTime deadline = delegate_->OnAlarm();
54 // delegate_->OnAlarm() might call Set(), in which case deadline_ will
55 // already contain the new value, so don't overwrite it.
56 if (!deadline_.IsInitialized() && deadline.IsInitialized()) {
57 Set(deadline);
61 } // namespace net