blob: a4d3c041cafcb94ffe28e682d954e9c9ef64a723 [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 Zhangde72ac42019-01-10 19:40:20 +00009#include <map>
10
Lei Zhangcb13a082019-01-10 19:34:49 +000011#include "fpdfsdk/cfx_systemhandler.h"
Lei Zhang4c1ced52018-10-18 21:58:21 +000012#include "fxjs/cjs_app.h"
13
Lei Zhangde72ac42019-01-10 19:40:20 +000014namespace {
15
16using TimerMap = std::map<int32_t, GlobalTimer*>;
17TimerMap* GetGlobalTimerMap() {
18 // Leak the timer array at shutdown.
19 static auto* s_TimerMap = new TimerMap;
20 return s_TimerMap;
21}
22
23} // namespace
24
Dan Sinclairf7435522018-02-05 22:27:22 +000025GlobalTimer::GlobalTimer(CJS_App* pObj,
Dan Sinclairc0858352017-10-30 17:49:52 +000026 CPDFSDK_FormFillEnvironment* pFormFillEnv,
27 CJS_Runtime* pRuntime,
Lei Zhangde72ac42019-01-10 19:40:20 +000028 Type nType,
Dan Sinclairc0858352017-10-30 17:49:52 +000029 const WideString& script,
30 uint32_t dwElapse,
31 uint32_t dwTimeOut)
Lei Zhange3f18b22019-01-09 22:10:25 +000032 : m_nTimerID(pFormFillEnv->GetSysHandler()->SetTimer(dwElapse, Trigger)),
Dan Sinclairf7435522018-02-05 22:27:22 +000033 m_pEmbedApp(pObj),
Dan Sinclairc0858352017-10-30 17:49:52 +000034 m_nType(nType),
35 m_dwTimeOut(dwTimeOut),
36 m_swJScript(script),
37 m_pRuntime(pRuntime),
38 m_pFormFillEnv(pFormFillEnv) {
Lei Zhangcb13a082019-01-10 19:34:49 +000039 if (HasValidID())
Dan Sinclairc0858352017-10-30 17:49:52 +000040 (*GetGlobalTimerMap())[m_nTimerID] = this;
41}
42
43GlobalTimer::~GlobalTimer() {
Lei Zhangcb13a082019-01-10 19:34:49 +000044 if (!HasValidID())
Dan Sinclairc0858352017-10-30 17:49:52 +000045 return;
46
47 if (GetRuntime())
48 m_pFormFillEnv->GetSysHandler()->KillTimer(m_nTimerID);
49
50 GetGlobalTimerMap()->erase(m_nTimerID);
51}
52
53// static
Lei Zhang5b166c02019-01-10 19:31:19 +000054void GlobalTimer::Trigger(int32_t nTimerID) {
Dan Sinclairc0858352017-10-30 17:49:52 +000055 auto it = GetGlobalTimerMap()->find(nTimerID);
56 if (it == GetGlobalTimerMap()->end())
57 return;
58
59 GlobalTimer* pTimer = it->second;
60 if (pTimer->m_bProcessing)
61 return;
62
63 pTimer->m_bProcessing = true;
Dan Sinclairf7435522018-02-05 22:27:22 +000064 if (pTimer->m_pEmbedApp)
65 pTimer->m_pEmbedApp->TimerProc(pTimer);
Dan Sinclairc0858352017-10-30 17:49:52 +000066
67 // Timer proc may have destroyed timer, find it again.
68 it = GetGlobalTimerMap()->find(nTimerID);
69 if (it == GetGlobalTimerMap()->end())
70 return;
71
72 pTimer = it->second;
73 pTimer->m_bProcessing = false;
74 if (pTimer->IsOneShot())
Dan Sinclairf7435522018-02-05 22:27:22 +000075 pTimer->m_pEmbedApp->CancelProc(pTimer);
Dan Sinclairc0858352017-10-30 17:49:52 +000076}
77
78// static
Lei Zhang5b166c02019-01-10 19:31:19 +000079void GlobalTimer::Cancel(int32_t nTimerID) {
Dan Sinclairc0858352017-10-30 17:49:52 +000080 auto it = GetGlobalTimerMap()->find(nTimerID);
81 if (it == GetGlobalTimerMap()->end())
82 return;
83
84 GlobalTimer* pTimer = it->second;
Dan Sinclairf7435522018-02-05 22:27:22 +000085 pTimer->m_pEmbedApp->CancelProc(pTimer);
Dan Sinclairc0858352017-10-30 17:49:52 +000086}
87
Lei Zhangcb13a082019-01-10 19:34:49 +000088bool GlobalTimer::HasValidID() const {
89 return m_nTimerID != CFX_SystemHandler::kInvalidTimerID;
90}