blob: 34f81fc433aabdd3f9b6e3f2b49beb8d24161e27 [file] [log] [blame]
Lei Zhangfac46f82017-06-02 14:20:04 -07001// 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 Sinclairc411eb92017-07-25 09:39:30 -04007#include "fpdfsdk/pwl/cpwl_timer.h"
Lei Zhangfac46f82017-06-02 14:20:04 -07008
9#include <map>
10
11#include "fpdfsdk/cfx_systemhandler.h"
Dan Sinclairc411eb92017-07-25 09:39:30 -040012#include "fpdfsdk/pwl/cpwl_timer_handler.h"
Lei Zhangfac46f82017-06-02 14:20:04 -070013
14namespace {
15
16std::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
24CPWL_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
31CPWL_Timer::~CPWL_Timer() {
32 KillPWLTimer();
33}
34
35int32_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
44void 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
54void 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}