• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
8 
9 #include <utility>
10 #include <vector>
11 
12 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
13 #include "fpdfsdk/fsdk_define.h"
14 
15 namespace {
16 
17 class CFWL_FWLAdapterTimerInfo : public CFWL_TimerInfo {
18  public:
CFWL_FWLAdapterTimerInfo(IFWL_AdapterTimerMgr * mgr,int32_t event,CFWL_Timer * timer)19   CFWL_FWLAdapterTimerInfo(IFWL_AdapterTimerMgr* mgr,
20                            int32_t event,
21                            CFWL_Timer* timer)
22       : CFWL_TimerInfo(mgr), idEvent(event), pTimer(timer) {}
23 
24   int32_t idEvent;
25   CFWL_Timer* pTimer;
26 };
27 
28 }  // namespace
29 
30 std::vector<CFWL_TimerInfo*>* CXFA_FWLAdapterTimerMgr::s_TimerArray = nullptr;
31 
CXFA_FWLAdapterTimerMgr(CPDFSDK_FormFillEnvironment * pFormFillEnv)32 CXFA_FWLAdapterTimerMgr::CXFA_FWLAdapterTimerMgr(
33     CPDFSDK_FormFillEnvironment* pFormFillEnv)
34     : m_pFormFillEnv(pFormFillEnv) {}
35 
~CXFA_FWLAdapterTimerMgr()36 CXFA_FWLAdapterTimerMgr::~CXFA_FWLAdapterTimerMgr() {}
37 
Start(CFWL_Timer * pTimer,uint32_t dwElapse,bool bImmediately,CFWL_TimerInfo ** pTimerInfo)38 void CXFA_FWLAdapterTimerMgr::Start(CFWL_Timer* pTimer,
39                                     uint32_t dwElapse,
40                                     bool bImmediately,
41                                     CFWL_TimerInfo** pTimerInfo) {
42   if (!m_pFormFillEnv)
43     return;
44 
45   int32_t id_event = m_pFormFillEnv->SetTimer(dwElapse, TimerProc);
46   if (!s_TimerArray)
47     s_TimerArray = new std::vector<CFWL_TimerInfo*>;
48 
49   *pTimerInfo = new CFWL_FWLAdapterTimerInfo(this, id_event, pTimer);
50   s_TimerArray->push_back(*pTimerInfo);
51 }
52 
Stop(CFWL_TimerInfo * pTimerInfo)53 void CXFA_FWLAdapterTimerMgr::Stop(CFWL_TimerInfo* pTimerInfo) {
54   if (!pTimerInfo || !m_pFormFillEnv)
55     return;
56 
57   CFWL_FWLAdapterTimerInfo* pInfo =
58       static_cast<CFWL_FWLAdapterTimerInfo*>(pTimerInfo);
59   m_pFormFillEnv->KillTimer(pInfo->idEvent);
60   if (!s_TimerArray)
61     return;
62 
63   auto it = std::find(s_TimerArray->begin(), s_TimerArray->end(), pInfo);
64   if (it != s_TimerArray->end()) {
65     s_TimerArray->erase(it);
66     delete pInfo;
67   }
68 }
69 
70 // static
TimerProc(int32_t idEvent)71 void CXFA_FWLAdapterTimerMgr::TimerProc(int32_t idEvent) {
72   if (!s_TimerArray)
73     return;
74 
75   for (auto* info : *s_TimerArray) {
76     CFWL_FWLAdapterTimerInfo* pInfo =
77         static_cast<CFWL_FWLAdapterTimerInfo*>(info);
78     if (pInfo->idEvent == idEvent) {
79       pInfo->pTimer->Run(pInfo);
80       break;
81     }
82   }
83 }
84