| OLD | NEW |
| (Empty) |
| 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 <memory> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "net/base/net_export.h" | |
| 12 #include "net/quic/quic_arena_scoped_ptr.h" | |
| 13 #include "net/quic/quic_time.h" | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 // Abstract class which represents an alarm which will go off at a | |
| 18 // scheduled time, and execute the |OnAlarm| method of the delegate. | |
| 19 // An alarm may be cancelled, in which case it may or may not be | |
| 20 // removed from the underlying scheduling system, but in either case | |
| 21 // the task will not be executed. | |
| 22 class NET_EXPORT_PRIVATE QuicAlarm { | |
| 23 public: | |
| 24 class NET_EXPORT_PRIVATE Delegate { | |
| 25 public: | |
| 26 virtual ~Delegate() {} | |
| 27 | |
| 28 // Invoked when the alarm fires. | |
| 29 virtual void OnAlarm() = 0; | |
| 30 }; | |
| 31 | |
| 32 explicit QuicAlarm(QuicArenaScopedPtr<Delegate> delegate); | |
| 33 virtual ~QuicAlarm(); | |
| 34 | |
| 35 // Sets the alarm to fire at |deadline|. Must not be called while | |
| 36 // the alarm is set. To reschedule an alarm, call Cancel() first, | |
| 37 // then Set(). | |
| 38 void Set(QuicTime new_deadline); | |
| 39 | |
| 40 // Cancels the alarm. May be called repeatedly. Does not | |
| 41 // guarantee that the underlying scheduling system will remove | |
| 42 // the alarm's associated task, but guarantees that the | |
| 43 // delegates OnAlarm method will not be called. | |
| 44 void Cancel(); | |
| 45 | |
| 46 // Cancels and sets the alarm if the |deadline| is farther from the current | |
| 47 // deadline than |granularity|, and otherwise does nothing. If |deadline| is | |
| 48 // not initialized, the alarm is cancelled. | |
| 49 void Update(QuicTime new_deadline, QuicTime::Delta granularity); | |
| 50 | |
| 51 // Returns true if |deadline_| has been set to a non-zero time. | |
| 52 bool IsSet() const; | |
| 53 | |
| 54 QuicTime deadline() const { return deadline_; } | |
| 55 | |
| 56 protected: | |
| 57 // Subclasses implement this method to perform the platform-specific | |
| 58 // scheduling of the alarm. Is called from Set() or Fire(), after the | |
| 59 // deadline has been updated. | |
| 60 virtual void SetImpl() = 0; | |
| 61 | |
| 62 // Subclasses implement this method to perform the platform-specific | |
| 63 // cancelation of the alarm. | |
| 64 virtual void CancelImpl() = 0; | |
| 65 | |
| 66 // Subclasses implement this method to perform the platform-specific update of | |
| 67 // the alarm if there exists a more optimal implementation than calling | |
| 68 // CancelImpl() and SetImpl(). | |
| 69 virtual void UpdateImpl(); | |
| 70 | |
| 71 // Called by subclasses when the alarm fires. Invokes the | |
| 72 // delegates |OnAlarm| if a delegate is set, and if the deadline | |
| 73 // has been exceeded. Implementations which do not remove the | |
| 74 // alarm from the underlying scheduler on Cancel() may need to handle | |
| 75 // the situation where the task executes before the deadline has been | |
| 76 // reached, in which case they need to reschedule the task and must not | |
| 77 // call invoke this method. | |
| 78 void Fire(); | |
| 79 | |
| 80 private: | |
| 81 QuicArenaScopedPtr<Delegate> delegate_; | |
| 82 QuicTime deadline_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(QuicAlarm); | |
| 85 }; | |
| 86 | |
| 87 } // namespace net | |
| 88 | |
| 89 #endif // NET_QUIC_QUIC_ALARM_H_ | |
| OLD | NEW |