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 #ifndef NET_QUIC_QUIC_ALARM_H_
6 #define NET_QUIC_QUIC_ALARM_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "net/base/net_export.h"
10 #include "net/quic/quic_time.h"
14 // Abstract class which represents an alarm which will go off at a
15 // scheduled time, and execute the |OnAlarm| method of the delegate.
16 // An alarm may be cancelled, in which case it may or may not be
17 // removed from the underlying scheduling system, but in either case
18 // the task will not be executed.
19 class NET_EXPORT_PRIVATE QuicAlarm
{
21 class NET_EXPORT_PRIVATE Delegate
{
23 virtual ~Delegate() {}
25 // Invoked when the alarm fires. If the return value is not
26 // infinite, then the alarm will be rescheduled at the
28 virtual QuicTime
OnAlarm() = 0;
31 explicit QuicAlarm(Delegate
* delegate
);
34 // Sets the alarm to fire at |deadline|. Must not be called while
35 // the alarm is set. To reschedule an alarm, call Cancel() first,
37 void Set(QuicTime deadline
);
39 // Cancels the alarm. May be called repeatedly. Does not
40 // guarantee that the underlying scheduling system will remove
41 // the alarm's associated task, but guarantees that the
42 // delegates OnAlarm method will not be called.
47 QuicTime
deadline() const { return deadline_
; }
50 // Subclasses implement this method to perform the platform-specific
51 // scheduling of the alarm. Is called from Set() or Fire(), after the
52 // deadline has been updated.
53 virtual void SetImpl() = 0;
55 // Subclasses implement this method to perform the platform-specific
56 // cancelation of the alarm.
57 virtual void CancelImpl() = 0;
59 // Called by subclasses when the alarm fires. Invokes the
60 // delegates |OnAlarm| if a delegate is set, and if the deadline
61 // has been exceeded. Implementations which do not remove the
62 // alarm from the underlying scheduler on Cancel() may need to handle
63 // the situation where the task executes before the deadline has been
64 // reached, in which case they need to reschedule the task and must not
65 // call invoke this method.
69 scoped_ptr
<Delegate
> delegate_
;
72 DISALLOW_COPY_AND_ASSIGN(QuicAlarm
);
77 #endif // NET_QUIC_QUIC_ALARM_H_