blob: 80278208ad0fa35b4157f2d31beb7f178a7e49d3 [file] [log] [blame]
Dan Sinclairc0858352017-10-30 17:49:52 +00001// 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 Sinclaire0345a42017-10-30 20:20:42 +00007#include "fxjs/global_timer.h"
Dan Sinclairc0858352017-10-30 17:49:52 +00008
Lei Zhang4c1ced52018-10-18 21:58:21 +00009#include "fxjs/cjs_app.h"
10
Dan Sinclairf7435522018-02-05 22:27:22 +000011GlobalTimer::GlobalTimer(CJS_App* pObj,
Dan Sinclairc0858352017-10-30 17:49:52 +000012 CPDFSDK_FormFillEnvironment* pFormFillEnv,
13 CJS_Runtime* pRuntime,
14 int nType,
15 const WideString& script,
16 uint32_t dwElapse,
17 uint32_t dwTimeOut)
18 : m_nTimerID(0),
Dan Sinclairf7435522018-02-05 22:27:22 +000019 m_pEmbedApp(pObj),
Dan Sinclairc0858352017-10-30 17:49:52 +000020 m_bProcessing(false),
21 m_nType(nType),
22 m_dwTimeOut(dwTimeOut),
23 m_swJScript(script),
24 m_pRuntime(pRuntime),
25 m_pFormFillEnv(pFormFillEnv) {
26 CFX_SystemHandler* pHandler = m_pFormFillEnv->GetSysHandler();
27 m_nTimerID = pHandler->SetTimer(dwElapse, Trigger);
28 if (m_nTimerID)
29 (*GetGlobalTimerMap())[m_nTimerID] = this;
30}
31
32GlobalTimer::~GlobalTimer() {
33 if (!m_nTimerID)
34 return;
35
36 if (GetRuntime())
37 m_pFormFillEnv->GetSysHandler()->KillTimer(m_nTimerID);
38
39 GetGlobalTimerMap()->erase(m_nTimerID);
40}
41
42// static
43void GlobalTimer::Trigger(int nTimerID) {
44 auto it = GetGlobalTimerMap()->find(nTimerID);
45 if (it == GetGlobalTimerMap()->end())
46 return;
47
48 GlobalTimer* pTimer = it->second;
49 if (pTimer->m_bProcessing)
50 return;
51
52 pTimer->m_bProcessing = true;
Dan Sinclairf7435522018-02-05 22:27:22 +000053 if (pTimer->m_pEmbedApp)
54 pTimer->m_pEmbedApp->TimerProc(pTimer);
Dan Sinclairc0858352017-10-30 17:49:52 +000055
56 // Timer proc may have destroyed timer, find it again.
57 it = GetGlobalTimerMap()->find(nTimerID);
58 if (it == GetGlobalTimerMap()->end())
59 return;
60
61 pTimer = it->second;
62 pTimer->m_bProcessing = false;
63 if (pTimer->IsOneShot())
Dan Sinclairf7435522018-02-05 22:27:22 +000064 pTimer->m_pEmbedApp->CancelProc(pTimer);
Dan Sinclairc0858352017-10-30 17:49:52 +000065}
66
67// static
68void GlobalTimer::Cancel(int nTimerID) {
69 auto it = GetGlobalTimerMap()->find(nTimerID);
70 if (it == GetGlobalTimerMap()->end())
71 return;
72
73 GlobalTimer* pTimer = it->second;
Dan Sinclairf7435522018-02-05 22:27:22 +000074 pTimer->m_pEmbedApp->CancelProc(pTimer);
Dan Sinclairc0858352017-10-30 17:49:52 +000075}
76
77// static
78GlobalTimer::TimerMap* GlobalTimer::GetGlobalTimerMap() {
79 // Leak the timer array at shutdown.
80 static auto* s_TimerMap = new TimerMap;
81 return s_TimerMap;
82}