blob: 3f0fe8cb4857a5cfabd6b21176c62730a1aeef13 [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 Sinclairefbc1912016-02-17 16:54:43 -05007#include "fpdfsdk/src/javascript/JS_Object.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Lei Zhangbde53d22015-11-12 22:21:30 -08009#include "fpdfsdk/include/fsdk_mgr.h" // For CPDFDoc_Environment.
Dan Sinclair61046b92016-02-18 14:48:48 -050010#include "fpdfsdk/include/javascript/IJavaScript.h"
Dan Sinclairefbc1912016-02-17 16:54:43 -050011#include "fpdfsdk/src/javascript/JS_Context.h"
12#include "fpdfsdk/src/javascript/JS_Define.h"
Lei Zhangd607f5b2015-10-05 17:06:09 -070013
14namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016int FXJS_MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070017 const FX_WCHAR* swMsg,
18 const FX_WCHAR* swTitle,
19 FX_UINT nType,
20 FX_UINT nIcon) {
21 if (!pApp)
22 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
25 pDoc->KillFocusAnnot();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028}
29
Lei Zhangd607f5b2015-10-05 17:06:09 -070030} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
33
34CJS_EmbedObj::~CJS_EmbedObj() {
35 m_pJSObject = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036}
37
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 const FX_WCHAR* swMsg,
40 const FX_WCHAR* swTitle,
41 FX_UINT nType,
42 FX_UINT nIcon) {
Lei Zhangd607f5b2015-10-05 17:06:09 -070043 return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044}
45
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
47 CJS_Object::Alert(pContext, swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048}
49
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
51 CJS_Object* pJSObj = data.GetParameter();
52 pJSObj->ExitInstance();
53 delete pJSObj;
Tom Sepez39bfe122015-09-17 15:25:23 -070054 FXJS_FreePrivate(data.GetInternalField(0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055}
56
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
58 CJS_Object* pJSObj = data.GetParameter();
59 pJSObj->Dispose();
60 data.SetSecondPassCallback(FreeObject);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020061}
62
Tom Sepez116e4ad2015-09-21 09:22:05 -070063CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) {
Tom Sepez371379d2015-11-06 08:29:39 -080064 m_pIsolate = pObject->GetIsolate();
Tom Sepez116e4ad2015-09-21 09:22:05 -070065 m_pV8Object.Reset(m_pIsolate, pObject);
66}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
Lei Zhang2b1a2d52015-08-14 22:16:22 -070068CJS_Object::~CJS_Object() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070069}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071void CJS_Object::MakeWeak() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070072 m_pV8Object.SetWeak(this, DisposeObject,
73 v8::WeakCallbackType::kInternalFields);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020074}
75
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076void CJS_Object::Dispose() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070077 m_pV8Object.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078}
79
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080int CJS_Object::MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 const FX_WCHAR* swMsg,
82 const FX_WCHAR* swTitle,
83 FX_UINT nType,
84 FX_UINT nIcon) {
Lei Zhangd607f5b2015-10-05 17:06:09 -070085 return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086}
87
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 if (pContext->IsMsgBoxEnabled()) {
90 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
91 if (pApp)
92 pApp->JS_appAlert(swMsg, NULL, 0, 3);
93 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
Tom Sepez371c87f2015-08-13 16:56:19 -070095
Lei Zhang2d5a0e12015-10-05 17:00:03 -070096CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj,
97 CPDFDoc_Environment* pApp,
98 CJS_Runtime* pRuntime,
99 int nType,
100 const CFX_WideString& script,
101 FX_DWORD dwElapse,
102 FX_DWORD dwTimeOut)
103 : m_nTimerID(0),
104 m_pEmbedObj(pObj),
105 m_bProcessing(false),
106 m_bValid(true),
107 m_nType(nType),
108 m_dwTimeOut(dwTimeOut),
Lei Zhang79e893a2015-11-04 16:02:47 -0800109 m_swJScript(script),
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700110 m_pRuntime(pRuntime),
111 m_pApp(pApp) {
Tom Sepez371c87f2015-08-13 16:56:19 -0700112 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700113 m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc);
Tom Sepez371c87f2015-08-13 16:56:19 -0700114 (*GetGlobalTimerMap())[m_nTimerID] = this;
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700115 m_pRuntime->AddObserver(this);
116}
117
118CJS_Timer::~CJS_Timer() {
119 CJS_Runtime* pRuntime = GetRuntime();
120 if (pRuntime)
121 pRuntime->RemoveObserver(this);
122 KillJSTimer();
Tom Sepez371c87f2015-08-13 16:56:19 -0700123}
124
125void CJS_Timer::KillJSTimer() {
126 if (m_nTimerID) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700127 if (m_bValid) {
128 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
129 pHandler->KillTimer(m_nTimerID);
130 }
Tom Sepez371c87f2015-08-13 16:56:19 -0700131 GetGlobalTimerMap()->erase(m_nTimerID);
132 m_nTimerID = 0;
133 }
134}
135
136// static
137void CJS_Timer::TimerProc(int idEvent) {
138 const auto it = GetGlobalTimerMap()->find(idEvent);
139 if (it != GetGlobalTimerMap()->end()) {
140 CJS_Timer* pTimer = it->second;
141 if (!pTimer->m_bProcessing) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700142 CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing);
143 pTimer->m_bProcessing = true;
Tom Sepez371c87f2015-08-13 16:56:19 -0700144 if (pTimer->m_pEmbedObj)
145 pTimer->m_pEmbedObj->TimerProc(pTimer);
Tom Sepez371c87f2015-08-13 16:56:19 -0700146 }
147 }
148}
149
150// static
151CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
152 // Leak the timer array at shutdown.
153 static auto* s_TimerMap = new TimerMap;
154 return s_TimerMap;
155}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700156
157void CJS_Timer::OnDestroyed() {
158 m_bValid = false;
159}