Lei Zhang | fac46f8 | 2017-06-02 14:20:04 -0700 | [diff] [blame] | 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 | |
Dan Sinclair | c411eb9 | 2017-07-25 09:39:30 -0400 | [diff] [blame] | 7 | #include "fpdfsdk/pwl/cpwl_timer.h" |
Lei Zhang | fac46f8 | 2017-06-02 14:20:04 -0700 | [diff] [blame] | 8 | |
| 9 | #include <map> |
| 10 | |
| 11 | #include "fpdfsdk/cfx_systemhandler.h" |
Dan Sinclair | c411eb9 | 2017-07-25 09:39:30 -0400 | [diff] [blame] | 12 | #include "fpdfsdk/pwl/cpwl_timer_handler.h" |
Lei Zhang | fac46f8 | 2017-06-02 14:20:04 -0700 | [diff] [blame] | 13 | |
| 14 | namespace { |
| 15 | |
| 16 | std::map<int32_t, CPWL_Timer*>& GetPWLTimeMap() { |
| 17 | // Leak the object at shutdown. |
| 18 | static auto* timeMap = new std::map<int32_t, CPWL_Timer*>; |
| 19 | return *timeMap; |
| 20 | } |
| 21 | |
| 22 | } // namespace |
| 23 | |
| 24 | CPWL_Timer::CPWL_Timer(CPWL_TimerHandler* pAttached, |
| 25 | CFX_SystemHandler* pSystemHandler) |
| 26 | : m_nTimerID(0), m_pAttached(pAttached), m_pSystemHandler(pSystemHandler) { |
| 27 | ASSERT(m_pAttached); |
| 28 | ASSERT(m_pSystemHandler); |
| 29 | } |
| 30 | |
| 31 | CPWL_Timer::~CPWL_Timer() { |
| 32 | KillPWLTimer(); |
| 33 | } |
| 34 | |
| 35 | int32_t CPWL_Timer::SetPWLTimer(int32_t nElapse) { |
| 36 | if (m_nTimerID != 0) |
| 37 | KillPWLTimer(); |
| 38 | m_nTimerID = m_pSystemHandler->SetTimer(nElapse, TimerProc); |
| 39 | |
| 40 | GetPWLTimeMap()[m_nTimerID] = this; |
| 41 | return m_nTimerID; |
| 42 | } |
| 43 | |
| 44 | void CPWL_Timer::KillPWLTimer() { |
| 45 | if (m_nTimerID == 0) |
| 46 | return; |
| 47 | |
| 48 | m_pSystemHandler->KillTimer(m_nTimerID); |
| 49 | GetPWLTimeMap().erase(m_nTimerID); |
| 50 | m_nTimerID = 0; |
| 51 | } |
| 52 | |
| 53 | // static |
| 54 | void CPWL_Timer::TimerProc(int32_t idEvent) { |
| 55 | auto it = GetPWLTimeMap().find(idEvent); |
| 56 | if (it == GetPWLTimeMap().end()) |
| 57 | return; |
| 58 | |
| 59 | CPWL_Timer* pTimer = it->second; |
| 60 | if (pTimer->m_pAttached) |
| 61 | pTimer->m_pAttached->TimerProc(); |
| 62 | } |