1 // Copyright 2017 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #ifndef CORE_FXCRT_CFX_TIMER_H_ 8 #define CORE_FXCRT_CFX_TIMER_H_ 9 10 #include <stdint.h> 11 12 #include "core/fxcrt/observed_ptr.h" 13 #include "core/fxcrt/unowned_ptr.h" 14 15 class CFX_Timer { 16 public: 17 // HandlerIface is implemented by upper layers that actually perform 18 // the system-dependent actions of scheduling and triggering timers. 19 class HandlerIface : public Observable { 20 public: 21 static constexpr int32_t kInvalidTimerID = 0; 22 using TimerCallback = void (*)(int32_t idEvent); 23 24 virtual ~HandlerIface() = default; 25 26 virtual int32_t SetTimer(int32_t uElapse, TimerCallback lpTimerFunc) = 0; 27 virtual void KillTimer(int32_t nTimerID) = 0; 28 }; 29 30 // CallbackIface is implemented by layers that want to perform a 31 // specific action on timer expiry. 32 class CallbackIface { 33 public: 34 virtual ~CallbackIface() = default; 35 virtual void OnTimerFired() = 0; 36 }; 37 38 CFX_Timer(HandlerIface* pHandlerIface, 39 CallbackIface* pCallbackIface, 40 int32_t nInterval); 41 ~CFX_Timer(); 42 HasValidID()43 bool HasValidID() const { 44 return m_nTimerID != HandlerIface::kInvalidTimerID; 45 } 46 47 private: 48 static void TimerProc(int32_t idEvent); 49 50 int32_t m_nTimerID = HandlerIface::kInvalidTimerID; 51 ObservedPtr<HandlerIface> m_pHandlerIface; 52 UnownedPtr<CallbackIface> const m_pCallbackIface; 53 }; 54 55 #endif // CORE_FXCRT_CFX_TIMER_H_ 56