quic_alarm.cc revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
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.
4
5#include "net/quic/quic_alarm.h"
6
7#include "base/logging.h"
8
9namespace net {
10
11QuicAlarm::QuicAlarm(Delegate* delegate)
12    : delegate_(delegate),
13      deadline_(QuicTime::Zero()) {
14}
15
16QuicAlarm::~QuicAlarm() {}
17
18void QuicAlarm::Set(QuicTime deadline) {
19  DCHECK(!IsSet());
20  DCHECK(deadline.IsInitialized());
21  deadline_ = deadline;
22  SetImpl();
23}
24
25void QuicAlarm::Cancel() {
26  deadline_ = QuicTime::Zero();
27  CancelImpl();
28}
29
30bool QuicAlarm::IsSet() const {
31  return deadline_.IsInitialized();
32}
33
34void QuicAlarm::Fire() {
35  if (!deadline_.IsInitialized()) {
36    return;
37  }
38
39  deadline_ = QuicTime::Zero();
40  QuicTime deadline = delegate_->OnAlarm();
41  // delegate_->OnAlarm() might call Set(), in which case  deadline_ will
42  // already contain the new value, so don't overwrite it.
43  if (!deadline_.IsInitialized() && deadline.IsInitialized()) {
44    Set(deadline);
45  }
46}
47
48}  // namespace net
49