1*3ac0a46fSAndroid Build Coastguard Worker // Copyright 2017 The PDFium Authors
2*3ac0a46fSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*3ac0a46fSAndroid Build Coastguard Worker // found in the LICENSE file.
4*3ac0a46fSAndroid Build Coastguard Worker
5*3ac0a46fSAndroid Build Coastguard Worker // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6*3ac0a46fSAndroid Build Coastguard Worker
7*3ac0a46fSAndroid Build Coastguard Worker #include "fxjs/global_timer.h"
8*3ac0a46fSAndroid Build Coastguard Worker
9*3ac0a46fSAndroid Build Coastguard Worker #include <map>
10*3ac0a46fSAndroid Build Coastguard Worker
11*3ac0a46fSAndroid Build Coastguard Worker #include "core/fxcrt/cfx_timer.h"
12*3ac0a46fSAndroid Build Coastguard Worker #include "fxjs/cjs_app.h"
13*3ac0a46fSAndroid Build Coastguard Worker #include "third_party/base/check.h"
14*3ac0a46fSAndroid Build Coastguard Worker #include "third_party/base/containers/contains.h"
15*3ac0a46fSAndroid Build Coastguard Worker #include "third_party/base/no_destructor.h"
16*3ac0a46fSAndroid Build Coastguard Worker
17*3ac0a46fSAndroid Build Coastguard Worker namespace {
18*3ac0a46fSAndroid Build Coastguard Worker
19*3ac0a46fSAndroid Build Coastguard Worker using TimerMap = std::map<int32_t, GlobalTimer*>;
GetGlobalTimerMap()20*3ac0a46fSAndroid Build Coastguard Worker TimerMap& GetGlobalTimerMap() {
21*3ac0a46fSAndroid Build Coastguard Worker static pdfium::base::NoDestructor<TimerMap> timer_map;
22*3ac0a46fSAndroid Build Coastguard Worker return *timer_map;
23*3ac0a46fSAndroid Build Coastguard Worker }
24*3ac0a46fSAndroid Build Coastguard Worker
25*3ac0a46fSAndroid Build Coastguard Worker } // namespace
26*3ac0a46fSAndroid Build Coastguard Worker
GlobalTimer(CJS_App * pObj,CJS_Runtime * pRuntime,Type nType,const WideString & script,uint32_t dwElapse,uint32_t dwTimeOut)27*3ac0a46fSAndroid Build Coastguard Worker GlobalTimer::GlobalTimer(CJS_App* pObj,
28*3ac0a46fSAndroid Build Coastguard Worker CJS_Runtime* pRuntime,
29*3ac0a46fSAndroid Build Coastguard Worker Type nType,
30*3ac0a46fSAndroid Build Coastguard Worker const WideString& script,
31*3ac0a46fSAndroid Build Coastguard Worker uint32_t dwElapse,
32*3ac0a46fSAndroid Build Coastguard Worker uint32_t dwTimeOut)
33*3ac0a46fSAndroid Build Coastguard Worker : m_nType(nType),
34*3ac0a46fSAndroid Build Coastguard Worker m_nTimerID(pRuntime->GetTimerHandler()->SetTimer(dwElapse, Trigger)),
35*3ac0a46fSAndroid Build Coastguard Worker m_dwTimeOut(dwTimeOut),
36*3ac0a46fSAndroid Build Coastguard Worker m_swJScript(script),
37*3ac0a46fSAndroid Build Coastguard Worker m_pRuntime(pRuntime),
38*3ac0a46fSAndroid Build Coastguard Worker m_pEmbedApp(pObj) {
39*3ac0a46fSAndroid Build Coastguard Worker if (HasValidID()) {
40*3ac0a46fSAndroid Build Coastguard Worker DCHECK(!pdfium::Contains(GetGlobalTimerMap(), m_nTimerID));
41*3ac0a46fSAndroid Build Coastguard Worker GetGlobalTimerMap()[m_nTimerID] = this;
42*3ac0a46fSAndroid Build Coastguard Worker }
43*3ac0a46fSAndroid Build Coastguard Worker }
44*3ac0a46fSAndroid Build Coastguard Worker
~GlobalTimer()45*3ac0a46fSAndroid Build Coastguard Worker GlobalTimer::~GlobalTimer() {
46*3ac0a46fSAndroid Build Coastguard Worker if (!HasValidID())
47*3ac0a46fSAndroid Build Coastguard Worker return;
48*3ac0a46fSAndroid Build Coastguard Worker
49*3ac0a46fSAndroid Build Coastguard Worker if (m_pRuntime && m_pRuntime->GetTimerHandler())
50*3ac0a46fSAndroid Build Coastguard Worker m_pRuntime->GetTimerHandler()->KillTimer(m_nTimerID);
51*3ac0a46fSAndroid Build Coastguard Worker
52*3ac0a46fSAndroid Build Coastguard Worker DCHECK(pdfium::Contains(GetGlobalTimerMap(), m_nTimerID));
53*3ac0a46fSAndroid Build Coastguard Worker GetGlobalTimerMap().erase(m_nTimerID);
54*3ac0a46fSAndroid Build Coastguard Worker }
55*3ac0a46fSAndroid Build Coastguard Worker
56*3ac0a46fSAndroid Build Coastguard Worker // static
Trigger(int32_t nTimerID)57*3ac0a46fSAndroid Build Coastguard Worker void GlobalTimer::Trigger(int32_t nTimerID) {
58*3ac0a46fSAndroid Build Coastguard Worker auto it = GetGlobalTimerMap().find(nTimerID);
59*3ac0a46fSAndroid Build Coastguard Worker if (it == GetGlobalTimerMap().end())
60*3ac0a46fSAndroid Build Coastguard Worker return;
61*3ac0a46fSAndroid Build Coastguard Worker
62*3ac0a46fSAndroid Build Coastguard Worker GlobalTimer* pTimer = it->second;
63*3ac0a46fSAndroid Build Coastguard Worker if (pTimer->m_bProcessing)
64*3ac0a46fSAndroid Build Coastguard Worker return;
65*3ac0a46fSAndroid Build Coastguard Worker
66*3ac0a46fSAndroid Build Coastguard Worker pTimer->m_bProcessing = true;
67*3ac0a46fSAndroid Build Coastguard Worker if (pTimer->m_pEmbedApp)
68*3ac0a46fSAndroid Build Coastguard Worker pTimer->m_pEmbedApp->TimerProc(pTimer);
69*3ac0a46fSAndroid Build Coastguard Worker
70*3ac0a46fSAndroid Build Coastguard Worker // Timer proc may have destroyed timer, find it again.
71*3ac0a46fSAndroid Build Coastguard Worker it = GetGlobalTimerMap().find(nTimerID);
72*3ac0a46fSAndroid Build Coastguard Worker if (it == GetGlobalTimerMap().end())
73*3ac0a46fSAndroid Build Coastguard Worker return;
74*3ac0a46fSAndroid Build Coastguard Worker
75*3ac0a46fSAndroid Build Coastguard Worker pTimer = it->second;
76*3ac0a46fSAndroid Build Coastguard Worker pTimer->m_bProcessing = false;
77*3ac0a46fSAndroid Build Coastguard Worker if (pTimer->IsOneShot())
78*3ac0a46fSAndroid Build Coastguard Worker pTimer->m_pEmbedApp->CancelProc(pTimer);
79*3ac0a46fSAndroid Build Coastguard Worker }
80*3ac0a46fSAndroid Build Coastguard Worker
81*3ac0a46fSAndroid Build Coastguard Worker // static
Cancel(int32_t nTimerID)82*3ac0a46fSAndroid Build Coastguard Worker void GlobalTimer::Cancel(int32_t nTimerID) {
83*3ac0a46fSAndroid Build Coastguard Worker auto it = GetGlobalTimerMap().find(nTimerID);
84*3ac0a46fSAndroid Build Coastguard Worker if (it == GetGlobalTimerMap().end())
85*3ac0a46fSAndroid Build Coastguard Worker return;
86*3ac0a46fSAndroid Build Coastguard Worker
87*3ac0a46fSAndroid Build Coastguard Worker GlobalTimer* pTimer = it->second;
88*3ac0a46fSAndroid Build Coastguard Worker pTimer->m_pEmbedApp->CancelProc(pTimer);
89*3ac0a46fSAndroid Build Coastguard Worker }
90*3ac0a46fSAndroid Build Coastguard Worker
HasValidID() const91*3ac0a46fSAndroid Build Coastguard Worker bool GlobalTimer::HasValidID() const {
92*3ac0a46fSAndroid Build Coastguard Worker return m_nTimerID != CFX_Timer::HandlerIface::kInvalidTimerID;
93*3ac0a46fSAndroid Build Coastguard Worker }
94