Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 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 | |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 7 | #include "fxjs/global_timer.h" |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 8 | |
Lei Zhang | 4c1ced5 | 2018-10-18 21:58:21 +0000 | [diff] [blame] | 9 | #include "fxjs/cjs_app.h" |
| 10 | |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 11 | GlobalTimer::GlobalTimer(CJS_App* pObj, |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 12 | 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 Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 19 | m_pEmbedApp(pObj), |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 20 | 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 | |
| 32 | GlobalTimer::~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 |
| 43 | void 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 Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 53 | if (pTimer->m_pEmbedApp) |
| 54 | pTimer->m_pEmbedApp->TimerProc(pTimer); |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 55 | |
| 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 Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 64 | pTimer->m_pEmbedApp->CancelProc(pTimer); |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // static |
| 68 | void GlobalTimer::Cancel(int nTimerID) { |
| 69 | auto it = GetGlobalTimerMap()->find(nTimerID); |
| 70 | if (it == GetGlobalTimerMap()->end()) |
| 71 | return; |
| 72 | |
| 73 | GlobalTimer* pTimer = it->second; |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 74 | pTimer->m_pEmbedApp->CancelProc(pTimer); |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // static |
| 78 | GlobalTimer::TimerMap* GlobalTimer::GetGlobalTimerMap() { |
| 79 | // Leak the timer array at shutdown. |
| 80 | static auto* s_TimerMap = new TimerMap; |
| 81 | return s_TimerMap; |
| 82 | } |