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
7 #include "fxjs/global_timer.h"
8
GlobalTimer(app * pObj,CPDFSDK_FormFillEnvironment * pFormFillEnv,CJS_Runtime * pRuntime,int nType,const WideString & script,uint32_t dwElapse,uint32_t dwTimeOut)9 GlobalTimer::GlobalTimer(app* pObj,
10 CPDFSDK_FormFillEnvironment* pFormFillEnv,
11 CJS_Runtime* pRuntime,
12 int nType,
13 const WideString& script,
14 uint32_t dwElapse,
15 uint32_t dwTimeOut)
16 : m_nTimerID(0),
17 m_pEmbedObj(pObj),
18 m_bProcessing(false),
19 m_nType(nType),
20 m_dwTimeOut(dwTimeOut),
21 m_swJScript(script),
22 m_pRuntime(pRuntime),
23 m_pFormFillEnv(pFormFillEnv) {
24 CFX_SystemHandler* pHandler = m_pFormFillEnv->GetSysHandler();
25 m_nTimerID = pHandler->SetTimer(dwElapse, Trigger);
26 if (m_nTimerID)
27 (*GetGlobalTimerMap())[m_nTimerID] = this;
28 }
29
~GlobalTimer()30 GlobalTimer::~GlobalTimer() {
31 if (!m_nTimerID)
32 return;
33
34 if (GetRuntime())
35 m_pFormFillEnv->GetSysHandler()->KillTimer(m_nTimerID);
36
37 GetGlobalTimerMap()->erase(m_nTimerID);
38 }
39
40 // static
Trigger(int nTimerID)41 void GlobalTimer::Trigger(int nTimerID) {
42 auto it = GetGlobalTimerMap()->find(nTimerID);
43 if (it == GetGlobalTimerMap()->end())
44 return;
45
46 GlobalTimer* pTimer = it->second;
47 if (pTimer->m_bProcessing)
48 return;
49
50 pTimer->m_bProcessing = true;
51 if (pTimer->m_pEmbedObj)
52 pTimer->m_pEmbedObj->TimerProc(pTimer);
53
54 // Timer proc may have destroyed timer, find it again.
55 it = GetGlobalTimerMap()->find(nTimerID);
56 if (it == GetGlobalTimerMap()->end())
57 return;
58
59 pTimer = it->second;
60 pTimer->m_bProcessing = false;
61 if (pTimer->IsOneShot())
62 pTimer->m_pEmbedObj->CancelProc(pTimer);
63 }
64
65 // static
Cancel(int nTimerID)66 void GlobalTimer::Cancel(int nTimerID) {
67 auto it = GetGlobalTimerMap()->find(nTimerID);
68 if (it == GetGlobalTimerMap()->end())
69 return;
70
71 GlobalTimer* pTimer = it->second;
72 pTimer->m_pEmbedObj->CancelProc(pTimer);
73 }
74
75 // static
GetGlobalTimerMap()76 GlobalTimer::TimerMap* GlobalTimer::GetGlobalTimerMap() {
77 // Leak the timer array at shutdown.
78 static auto* s_TimerMap = new TimerMap;
79 return s_TimerMap;
80 }
81