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 | de72ac4 | 2019-01-10 19:40:20 +0000 | [diff] [blame] | 9 | #include <map> |
| 10 | |
Lei Zhang | cb13a08 | 2019-01-10 19:34:49 +0000 | [diff] [blame] | 11 | #include "fpdfsdk/cfx_systemhandler.h" |
Lei Zhang | 4c1ced5 | 2018-10-18 21:58:21 +0000 | [diff] [blame] | 12 | #include "fxjs/cjs_app.h" |
| 13 | |
Lei Zhang | de72ac4 | 2019-01-10 19:40:20 +0000 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | using TimerMap = std::map<int32_t, GlobalTimer*>; |
| 17 | TimerMap* 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 Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 25 | GlobalTimer::GlobalTimer(CJS_App* pObj, |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 26 | CPDFSDK_FormFillEnvironment* pFormFillEnv, |
| 27 | CJS_Runtime* pRuntime, |
Lei Zhang | de72ac4 | 2019-01-10 19:40:20 +0000 | [diff] [blame] | 28 | Type nType, |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 29 | const WideString& script, |
| 30 | uint32_t dwElapse, |
| 31 | uint32_t dwTimeOut) |
Lei Zhang | e3f18b2 | 2019-01-09 22:10:25 +0000 | [diff] [blame] | 32 | : m_nTimerID(pFormFillEnv->GetSysHandler()->SetTimer(dwElapse, Trigger)), |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 33 | m_pEmbedApp(pObj), |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 34 | m_nType(nType), |
| 35 | m_dwTimeOut(dwTimeOut), |
| 36 | m_swJScript(script), |
| 37 | m_pRuntime(pRuntime), |
| 38 | m_pFormFillEnv(pFormFillEnv) { |
Lei Zhang | cb13a08 | 2019-01-10 19:34:49 +0000 | [diff] [blame] | 39 | if (HasValidID()) |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 40 | (*GetGlobalTimerMap())[m_nTimerID] = this; |
| 41 | } |
| 42 | |
| 43 | GlobalTimer::~GlobalTimer() { |
Lei Zhang | cb13a08 | 2019-01-10 19:34:49 +0000 | [diff] [blame] | 44 | if (!HasValidID()) |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 45 | return; |
| 46 | |
| 47 | if (GetRuntime()) |
| 48 | m_pFormFillEnv->GetSysHandler()->KillTimer(m_nTimerID); |
| 49 | |
| 50 | GetGlobalTimerMap()->erase(m_nTimerID); |
| 51 | } |
| 52 | |
| 53 | // static |
Lei Zhang | 5b166c0 | 2019-01-10 19:31:19 +0000 | [diff] [blame] | 54 | void GlobalTimer::Trigger(int32_t nTimerID) { |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 55 | 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 Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 64 | if (pTimer->m_pEmbedApp) |
| 65 | pTimer->m_pEmbedApp->TimerProc(pTimer); |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 66 | |
| 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 Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 75 | pTimer->m_pEmbedApp->CancelProc(pTimer); |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // static |
Lei Zhang | 5b166c0 | 2019-01-10 19:31:19 +0000 | [diff] [blame] | 79 | void GlobalTimer::Cancel(int32_t nTimerID) { |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 80 | auto it = GetGlobalTimerMap()->find(nTimerID); |
| 81 | if (it == GetGlobalTimerMap()->end()) |
| 82 | return; |
| 83 | |
| 84 | GlobalTimer* pTimer = it->second; |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 85 | pTimer->m_pEmbedApp->CancelProc(pTimer); |
Dan Sinclair | c085835 | 2017-10-30 17:49:52 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Lei Zhang | cb13a08 | 2019-01-10 19:34:49 +0000 | [diff] [blame] | 88 | bool GlobalTimer::HasValidID() const { |
| 89 | return m_nTimerID != CFX_SystemHandler::kInvalidTimerID; |
| 90 | } |