blob: 8a4dc0fa33c98223ffb4da535289865665230490 [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() {
thestig1cd352e2016-06-07 17:53:06 -070034 m_pJSObject = nullptr;
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
weili625ad662016-06-15 11:21:33 -070078void CJS_Object::InitInstance(IJS_Runtime* pIRuntime) {}
79
80void CJS_Object::ExitInstance() {}
81
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 if (pContext->IsMsgBoxEnabled()) {
84 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
85 if (pApp)
thestig1cd352e2016-06-07 17:53:06 -070086 pApp->JS_appAlert(swMsg, nullptr, 0, 3);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
Tom Sepez371c87f2015-08-13 16:56:19 -070089
Lei Zhang2d5a0e12015-10-05 17:00:03 -070090CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj,
91 CPDFDoc_Environment* pApp,
92 CJS_Runtime* pRuntime,
93 int nType,
94 const CFX_WideString& script,
tsepezc3255f52016-03-25 14:52:27 -070095 uint32_t dwElapse,
96 uint32_t dwTimeOut)
Lei Zhang2d5a0e12015-10-05 17:00:03 -070097 : m_nTimerID(0),
98 m_pEmbedObj(pObj),
99 m_bProcessing(false),
100 m_bValid(true),
101 m_nType(nType),
102 m_dwTimeOut(dwTimeOut),
Lei Zhang79e893a2015-11-04 16:02:47 -0800103 m_swJScript(script),
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700104 m_pRuntime(pRuntime),
105 m_pApp(pApp) {
dsinclairb9590102016-04-27 06:38:59 -0700106 CFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700107 m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc);
Tom Sepez371c87f2015-08-13 16:56:19 -0700108 (*GetGlobalTimerMap())[m_nTimerID] = this;
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700109 m_pRuntime->AddObserver(this);
110}
111
112CJS_Timer::~CJS_Timer() {
113 CJS_Runtime* pRuntime = GetRuntime();
114 if (pRuntime)
115 pRuntime->RemoveObserver(this);
116 KillJSTimer();
Tom Sepez371c87f2015-08-13 16:56:19 -0700117}
118
119void CJS_Timer::KillJSTimer() {
120 if (m_nTimerID) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700121 if (m_bValid) {
dsinclairb9590102016-04-27 06:38:59 -0700122 CFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700123 pHandler->KillTimer(m_nTimerID);
124 }
Tom Sepez371c87f2015-08-13 16:56:19 -0700125 GetGlobalTimerMap()->erase(m_nTimerID);
126 m_nTimerID = 0;
127 }
128}
129
130// static
131void CJS_Timer::TimerProc(int idEvent) {
132 const auto it = GetGlobalTimerMap()->find(idEvent);
133 if (it != GetGlobalTimerMap()->end()) {
134 CJS_Timer* pTimer = it->second;
135 if (!pTimer->m_bProcessing) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700136 CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing);
137 pTimer->m_bProcessing = true;
Tom Sepez371c87f2015-08-13 16:56:19 -0700138 if (pTimer->m_pEmbedObj)
139 pTimer->m_pEmbedObj->TimerProc(pTimer);
Tom Sepez371c87f2015-08-13 16:56:19 -0700140 }
141 }
142}
143
144// static
145CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
146 // Leak the timer array at shutdown.
147 static auto* s_TimerMap = new TimerMap;
148 return s_TimerMap;
149}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700150
151void CJS_Timer::OnDestroyed() {
152 m_bValid = false;
153}