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 #ifndef NET_QUIC_QUIC_ALARM_H_ 6 #define NET_QUIC_QUIC_ALARM_H_ 7 8 #include "base/memory/scoped_ptr.h" 9 #include "net/base/net_export.h" 10 #include "net/quic/quic_time.h" 11 12 namespace net { 13 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 { 20 public: 21 class NET_EXPORT_PRIVATE Delegate { 22 public: 23 virtual ~Delegate() {} 24 25 // Invoked when the alarm fires. If the return value is not 26 // infinite, then the alarm will be rescheduled at the 27 // specified time. 28 virtual QuicTime OnAlarm() = 0; 29 }; 30 31 explicit QuicAlarm(Delegate* delegate); 32 virtual ~QuicAlarm(); 33 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, 36 // then Set(). 37 void Set(QuicTime deadline); 38 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. 43 void Cancel(); 44 45 bool IsSet() const; 46 47 QuicTime deadline() const { return deadline_; } 48 49 protected: 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; 54 55 // Subclasses implement this method to perform the platform-specific 56 // cancelation of the alarm. 57 virtual void CancelImpl() = 0; 58 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. 66 void Fire(); 67 68 private: 69 scoped_ptr<Delegate> delegate_; 70 QuicTime deadline_; 71 72 DISALLOW_COPY_AND_ASSIGN(QuicAlarm); 73 }; 74 75 } // namespace net 76 77 #endif // NET_QUIC_QUIC_ALARM_H_ 78