• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 PDFium 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fxcrt/cfx_timer.h"
8 
9 #include <map>
10 
11 #include "third_party/base/no_destructor.h"
12 
13 namespace {
14 
15 using TimerMap = std::map<int32_t, CFX_Timer*>;
GetPWLTimerMap()16 TimerMap& GetPWLTimerMap() {
17   static pdfium::base::NoDestructor<TimerMap> timer_map;
18   return *timer_map;
19 }
20 
21 }  // namespace
22 
CFX_Timer(TimerHandlerIface * pTimerHandler,CallbackIface * pCallbackIface,int32_t nInterval)23 CFX_Timer::CFX_Timer(TimerHandlerIface* pTimerHandler,
24                      CallbackIface* pCallbackIface,
25                      int32_t nInterval)
26     : m_nTimerID(pTimerHandler->SetTimer(nInterval, TimerProc)),
27       m_pTimerHandler(pTimerHandler),
28       m_pCallbackIface(pCallbackIface) {
29   ASSERT(m_pCallbackIface);
30   if (HasValidID())
31     GetPWLTimerMap()[m_nTimerID] = this;
32 }
33 
~CFX_Timer()34 CFX_Timer::~CFX_Timer() {
35   if (HasValidID()) {
36     m_pTimerHandler->KillTimer(m_nTimerID);
37     GetPWLTimerMap().erase(m_nTimerID);
38   }
39 }
40 
41 // static
TimerProc(int32_t idEvent)42 void CFX_Timer::TimerProc(int32_t idEvent) {
43   auto it = GetPWLTimerMap().find(idEvent);
44   if (it != GetPWLTimerMap().end())
45     it->second->m_pCallbackIface->OnTimerFired();
46 }
47