• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   static void InitializeGlobals();
39   static void DestroyGlobals();
40 
41   CFX_Timer(HandlerIface* pHandlerIface,
42             CallbackIface* pCallbackIface,
43             int32_t nInterval);
44   ~CFX_Timer();
45 
HasValidID()46   bool HasValidID() const {
47     return m_nTimerID != HandlerIface::kInvalidTimerID;
48   }
49 
50  private:
51   static void TimerProc(int32_t idEvent);
52 
53   int32_t m_nTimerID = HandlerIface::kInvalidTimerID;
54   ObservedPtr<HandlerIface> m_pHandlerIface;
55   UnownedPtr<CallbackIface> const m_pCallbackIface;
56 };
57 
58 #endif  // CORE_FXCRT_CFX_TIMER_H_
59