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/include/fsdk_define.h"
8 #include "fpdfsdk/include/fsdk_mgr.h"
9 #include "fpdfsdk/include/fpdfxfa/fpdfxfa_util.h"
10 
11 CFX_PtrArray CXFA_FWLAdapterTimerMgr::ms_timerArray;
12 
Start(IFWL_Timer * pTimer,FX_DWORD dwElapse,FWL_HTIMER & hTimer,FX_BOOL bImmediately)13 FWL_ERR CXFA_FWLAdapterTimerMgr::Start(IFWL_Timer* pTimer,
14                                        FX_DWORD dwElapse,
15                                        FWL_HTIMER& hTimer,
16                                        FX_BOOL bImmediately /* = TRUE */) {
17   if (m_pEnv) {
18     uint32_t uIDEvent = m_pEnv->FFI_SetTimer(dwElapse, TimerProc);
19     CFWL_TimerInfo* pInfo = new CFWL_TimerInfo;
20     pInfo->uIDEvent = uIDEvent;
21     pInfo->pTimer = pTimer;
22     ms_timerArray.Add(pInfo);
23 
24     hTimer = (FWL_HTIMER)pInfo;
25     return FWL_ERR_Succeeded;
26   }
27 
28   return FWL_ERR_Indefinite;
29 }
30 
Stop(FWL_HTIMER hTimer)31 FWL_ERR CXFA_FWLAdapterTimerMgr::Stop(FWL_HTIMER hTimer) {
32   if (!hTimer)
33     return FWL_ERR_Indefinite;
34 
35   if (m_pEnv) {
36     CFWL_TimerInfo* pInfo = (CFWL_TimerInfo*)hTimer;
37 
38     m_pEnv->FFI_KillTimer(pInfo->uIDEvent);
39 
40     int32_t index = ms_timerArray.Find(pInfo);
41     if (index >= 0) {
42       ms_timerArray.RemoveAt(index);
43       delete pInfo;
44     }
45     return FWL_ERR_Succeeded;
46   }
47 
48   return FWL_ERR_Indefinite;
49 }
50 
TimerProc(int32_t idEvent)51 void CXFA_FWLAdapterTimerMgr::TimerProc(int32_t idEvent) {
52   CFWL_TimerInfo* pInfo = NULL;
53   int32_t iCount = CXFA_FWLAdapterTimerMgr::ms_timerArray.GetSize();
54   for (int32_t i = 0; i < iCount; i++) {
55     CFWL_TimerInfo* pTemp =
56         (CFWL_TimerInfo*)CXFA_FWLAdapterTimerMgr::ms_timerArray.GetAt(i);
57     if (pTemp->uIDEvent == idEvent) {
58       pInfo = pTemp;
59       break;
60     }
61   }
62   if (pInfo) {
63     pInfo->pTimer->Run((FWL_HTIMER)pInfo);
64   }
65 }
66