blob: d85cc1dbe496c798b857a8d82a0ada89c40e60e1 [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
13namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070014
Nico Weber9d8ec5a2015-08-04 13:00:21 -070015int FXJS_MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016 const FX_WCHAR* swMsg,
17 const FX_WCHAR* swTitle,
18 FX_UINT nType,
19 FX_UINT nIcon) {
20 if (!pApp)
21 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
24 pDoc->KillFocusAnnot();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027}
28
Lei Zhangd607f5b2015-10-05 17:06:09 -070029} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
32
33CJS_EmbedObj::~CJS_EmbedObj() {
34 m_pJSObject = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035}
36
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038 const FX_WCHAR* swMsg,
39 const FX_WCHAR* swTitle,
40 FX_UINT nType,
41 FX_UINT nIcon) {
Lei Zhangd607f5b2015-10-05 17:06:09 -070042 return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043}
44
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
46 CJS_Object::Alert(pContext, swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047}
48
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
50 CJS_Object* pJSObj = data.GetParameter();
51 pJSObj->ExitInstance();
52 delete pJSObj;
Tom Sepez39bfe122015-09-17 15:25:23 -070053 FXJS_FreePrivate(data.GetInternalField(0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054}
55
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
57 CJS_Object* pJSObj = data.GetParameter();
58 pJSObj->Dispose();
59 data.SetSecondPassCallback(FreeObject);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020060}
61
Tom Sepez116e4ad2015-09-21 09:22:05 -070062CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) {
Tom Sepez371379d2015-11-06 08:29:39 -080063 m_pIsolate = pObject->GetIsolate();
Tom Sepez116e4ad2015-09-21 09:22:05 -070064 m_pV8Object.Reset(m_pIsolate, pObject);
65}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066
Dan Sinclairf766ad22016-03-14 13:51:24 -040067CJS_Object::~CJS_Object() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069void CJS_Object::MakeWeak() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070070 m_pV8Object.SetWeak(this, DisposeObject,
71 v8::WeakCallbackType::kInternalFields);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020072}
73
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074void CJS_Object::Dispose() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070075 m_pV8Object.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076}
77
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078int CJS_Object::MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 const FX_WCHAR* swMsg,
80 const FX_WCHAR* swTitle,
81 FX_UINT nType,
82 FX_UINT nIcon) {
Lei Zhangd607f5b2015-10-05 17:06:09 -070083 return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084}
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 if (pContext->IsMsgBoxEnabled()) {
88 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
89 if (pApp)
90 pApp->JS_appAlert(swMsg, NULL, 0, 3);
91 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
Tom Sepez371c87f2015-08-13 16:56:19 -070093
Lei Zhang2d5a0e12015-10-05 17:00:03 -070094CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj,
95 CPDFDoc_Environment* pApp,
96 CJS_Runtime* pRuntime,
97 int nType,
98 const CFX_WideString& script,
tsepezc3255f52016-03-25 14:52:27 -070099 uint32_t dwElapse,
100 uint32_t dwTimeOut)
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700101 : m_nTimerID(0),
102 m_pEmbedObj(pObj),
103 m_bProcessing(false),
104 m_bValid(true),
105 m_nType(nType),
106 m_dwTimeOut(dwTimeOut),
Lei Zhang79e893a2015-11-04 16:02:47 -0800107 m_swJScript(script),
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700108 m_pRuntime(pRuntime),
109 m_pApp(pApp) {
Tom Sepez371c87f2015-08-13 16:56:19 -0700110 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700111 m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc);
Tom Sepez371c87f2015-08-13 16:56:19 -0700112 (*GetGlobalTimerMap())[m_nTimerID] = this;
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700113 m_pRuntime->AddObserver(this);
114}
115
116CJS_Timer::~CJS_Timer() {
117 CJS_Runtime* pRuntime = GetRuntime();
118 if (pRuntime)
119 pRuntime->RemoveObserver(this);
120 KillJSTimer();
Tom Sepez371c87f2015-08-13 16:56:19 -0700121}
122
123void CJS_Timer::KillJSTimer() {
124 if (m_nTimerID) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700125 if (m_bValid) {
126 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
127 pHandler->KillTimer(m_nTimerID);
128 }
Tom Sepez371c87f2015-08-13 16:56:19 -0700129 GetGlobalTimerMap()->erase(m_nTimerID);
130 m_nTimerID = 0;
131 }
132}
133
134// static
135void CJS_Timer::TimerProc(int idEvent) {
136 const auto it = GetGlobalTimerMap()->find(idEvent);
137 if (it != GetGlobalTimerMap()->end()) {
138 CJS_Timer* pTimer = it->second;
139 if (!pTimer->m_bProcessing) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700140 CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing);
141 pTimer->m_bProcessing = true;
Tom Sepez371c87f2015-08-13 16:56:19 -0700142 if (pTimer->m_pEmbedObj)
143 pTimer->m_pEmbedObj->TimerProc(pTimer);
Tom Sepez371c87f2015-08-13 16:56:19 -0700144 }
145 }
146}
147
148// static
149CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
150 // Leak the timer array at shutdown.
151 static auto* s_TimerMap = new TimerMap;
152 return s_TimerMap;
153}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700154
155void CJS_Timer::OnDestroyed() {
156 m_bValid = false;
157}