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 #include "core/fxcrt/cfx_timer.h" 8 9 #include <map> 10 11 #include "third_party/base/check.h" 12 #include "third_party/base/no_destructor.h" 13 14 namespace { 15 16 using TimerMap = std::map<int32_t, CFX_Timer*>; GetPWLTimerMap()17TimerMap& GetPWLTimerMap() { 18 static pdfium::base::NoDestructor<TimerMap> timer_map; 19 return *timer_map; 20 } 21 22 } // namespace 23 CFX_Timer(HandlerIface * pHandlerIface,CallbackIface * pCallbackIface,int32_t nInterval)24CFX_Timer::CFX_Timer(HandlerIface* pHandlerIface, 25 CallbackIface* pCallbackIface, 26 int32_t nInterval) 27 : m_pHandlerIface(pHandlerIface), m_pCallbackIface(pCallbackIface) { 28 DCHECK(m_pCallbackIface); 29 if (m_pHandlerIface) { 30 m_nTimerID = m_pHandlerIface->SetTimer(nInterval, TimerProc); 31 if (HasValidID()) 32 GetPWLTimerMap()[m_nTimerID] = this; 33 } 34 } 35 ~CFX_Timer()36CFX_Timer::~CFX_Timer() { 37 if (HasValidID()) { 38 GetPWLTimerMap().erase(m_nTimerID); 39 if (m_pHandlerIface) 40 m_pHandlerIface->KillTimer(m_nTimerID); 41 } 42 } 43 44 // static TimerProc(int32_t idEvent)45void CFX_Timer::TimerProc(int32_t idEvent) { 46 auto it = GetPWLTimerMap().find(idEvent); 47 if (it != GetPWLTimerMap().end()) 48 it->second->m_pCallbackIface->OnTimerFired(); 49 } 50