blob: b65699a18bffd43c8ff59b5af42e6fc64f1a131e [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#include "fpdfsdk/javascript/JS_Object.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
dsinclair64376be2016-03-31 20:03:24 -07009#include "fpdfsdk/include/fsdk_mgr.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040010#include "fpdfsdk/javascript/JS_Define.h"
dsinclair64376be2016-03-31 20:03:24 -070011#include "fpdfsdk/javascript/cjs_context.h"
Lei Zhangd607f5b2015-10-05 17:06:09 -070012
Nico Weber9d8ec5a2015-08-04 13:00:21 -070013CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
14
15CJS_EmbedObj::~CJS_EmbedObj() {
thestig1cd352e2016-06-07 17:53:06 -070016 m_pJSObject = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017}
18
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020 const FX_WCHAR* swMsg,
21 const FX_WCHAR* swTitle,
22 FX_UINT nType,
23 FX_UINT nIcon) {
tsepez1d3348c2016-07-21 13:29:55 -070024 if (!pApp)
25 return 0;
26
27 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
28 pDoc->KillFocusAnnot();
29
30 return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031}
32
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
34 CJS_Object::Alert(pContext, swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035}
36
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
38 CJS_Object* pJSObj = data.GetParameter();
39 pJSObj->ExitInstance();
40 delete pJSObj;
Tom Sepez39bfe122015-09-17 15:25:23 -070041 FXJS_FreePrivate(data.GetInternalField(0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042}
43
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
45 CJS_Object* pJSObj = data.GetParameter();
46 pJSObj->Dispose();
47 data.SetSecondPassCallback(FreeObject);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020048}
49
Tom Sepez116e4ad2015-09-21 09:22:05 -070050CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) {
Tom Sepez371379d2015-11-06 08:29:39 -080051 m_pIsolate = pObject->GetIsolate();
Tom Sepez116e4ad2015-09-21 09:22:05 -070052 m_pV8Object.Reset(m_pIsolate, pObject);
53}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054
Dan Sinclairf766ad22016-03-14 13:51:24 -040055CJS_Object::~CJS_Object() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057void CJS_Object::MakeWeak() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070058 m_pV8Object.SetWeak(this, DisposeObject,
59 v8::WeakCallbackType::kInternalFields);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020060}
61
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062void CJS_Object::Dispose() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070063 m_pV8Object.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064}
65
weili625ad662016-06-15 11:21:33 -070066void CJS_Object::InitInstance(IJS_Runtime* pIRuntime) {}
67
68void CJS_Object::ExitInstance() {}
69
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
tsepez1d3348c2016-07-21 13:29:55 -070071 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
72 if (pApp)
73 pApp->JS_appAlert(swMsg, nullptr, 0, 3);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074}
Tom Sepez371c87f2015-08-13 16:56:19 -070075
Lei Zhang2d5a0e12015-10-05 17:00:03 -070076CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj,
77 CPDFDoc_Environment* pApp,
78 CJS_Runtime* pRuntime,
79 int nType,
80 const CFX_WideString& script,
tsepezc3255f52016-03-25 14:52:27 -070081 uint32_t dwElapse,
82 uint32_t dwTimeOut)
Lei Zhang2d5a0e12015-10-05 17:00:03 -070083 : m_nTimerID(0),
84 m_pEmbedObj(pObj),
85 m_bProcessing(false),
86 m_bValid(true),
87 m_nType(nType),
88 m_dwTimeOut(dwTimeOut),
Lei Zhang79e893a2015-11-04 16:02:47 -080089 m_swJScript(script),
Lei Zhang2d5a0e12015-10-05 17:00:03 -070090 m_pRuntime(pRuntime),
91 m_pApp(pApp) {
dsinclairb9590102016-04-27 06:38:59 -070092 CFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
tsepez8ca63de2016-08-05 17:12:27 -070093 m_nTimerID = pHandler->SetTimer(dwElapse, Trigger);
Tom Sepez371c87f2015-08-13 16:56:19 -070094 (*GetGlobalTimerMap())[m_nTimerID] = this;
Lei Zhang2d5a0e12015-10-05 17:00:03 -070095 m_pRuntime->AddObserver(this);
96}
97
98CJS_Timer::~CJS_Timer() {
99 CJS_Runtime* pRuntime = GetRuntime();
100 if (pRuntime)
101 pRuntime->RemoveObserver(this);
Tom Sepez371c87f2015-08-13 16:56:19 -0700102
tsepez8ca63de2016-08-05 17:12:27 -0700103 if (!m_nTimerID)
104 return;
105
106 if (m_bValid)
107 m_pApp->GetSysHandler()->KillTimer(m_nTimerID);
108
109 GetGlobalTimerMap()->erase(m_nTimerID);
Tom Sepez371c87f2015-08-13 16:56:19 -0700110}
111
112// static
tsepez8ca63de2016-08-05 17:12:27 -0700113void CJS_Timer::Trigger(int nTimerID) {
114 auto it = GetGlobalTimerMap()->find(nTimerID);
tsepez32e693f2016-08-04 12:47:42 -0700115 if (it == GetGlobalTimerMap()->end())
116 return;
117
118 CJS_Timer* pTimer = it->second;
119 if (pTimer->m_bProcessing)
120 return;
121
122 pTimer->m_bProcessing = true;
123 if (pTimer->m_pEmbedObj)
124 pTimer->m_pEmbedObj->TimerProc(pTimer);
125
126 // Timer proc may have destroyed timer, find it again.
tsepez8ca63de2016-08-05 17:12:27 -0700127 it = GetGlobalTimerMap()->find(nTimerID);
tsepez32e693f2016-08-04 12:47:42 -0700128 if (it == GetGlobalTimerMap()->end())
129 return;
130
131 pTimer = it->second;
132 pTimer->m_bProcessing = false;
tsepez8ca63de2016-08-05 17:12:27 -0700133 if (pTimer->IsOneShot())
134 pTimer->m_pEmbedObj->CancelProc(pTimer);
135}
136
137// static
138void CJS_Timer::Cancel(int nTimerID) {
139 auto it = GetGlobalTimerMap()->find(nTimerID);
140 if (it == GetGlobalTimerMap()->end())
141 return;
142
143 CJS_Timer* pTimer = it->second;
144 pTimer->m_pEmbedObj->CancelProc(pTimer);
Tom Sepez371c87f2015-08-13 16:56:19 -0700145}
146
147// static
148CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
149 // Leak the timer array at shutdown.
150 static auto* s_TimerMap = new TimerMap;
151 return s_TimerMap;
152}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700153
154void CJS_Timer::OnDestroyed() {
155 m_bValid = false;
156}