blob: 9bfa857cb95add0d635e5c71ae7e15886db35a1e [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
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 Sinclairf766ad22016-03-14 13:51:24 -040011#include "fpdfsdk/javascript/JS_Context.h"
12#include "fpdfsdk/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
Dan Sinclairf766ad22016-03-14 13:51:24 -040068CJS_Object::~CJS_Object() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070069
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070void CJS_Object::MakeWeak() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070071 m_pV8Object.SetWeak(this, DisposeObject,
72 v8::WeakCallbackType::kInternalFields);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020073}
74
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075void CJS_Object::Dispose() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070076 m_pV8Object.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077}
78
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079int CJS_Object::MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 const FX_WCHAR* swMsg,
81 const FX_WCHAR* swTitle,
82 FX_UINT nType,
83 FX_UINT nIcon) {
Lei Zhangd607f5b2015-10-05 17:06:09 -070084 return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085}
86
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 if (pContext->IsMsgBoxEnabled()) {
89 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
90 if (pApp)
91 pApp->JS_appAlert(swMsg, NULL, 0, 3);
92 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}
Tom Sepez371c87f2015-08-13 16:56:19 -070094
Lei Zhang2d5a0e12015-10-05 17:00:03 -070095CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj,
96 CPDFDoc_Environment* pApp,
97 CJS_Runtime* pRuntime,
98 int nType,
99 const CFX_WideString& script,
tsepezc3255f52016-03-25 14:52:27 -0700100 uint32_t dwElapse,
101 uint32_t dwTimeOut)
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700102 : m_nTimerID(0),
103 m_pEmbedObj(pObj),
104 m_bProcessing(false),
105 m_bValid(true),
106 m_nType(nType),
107 m_dwTimeOut(dwTimeOut),
Lei Zhang79e893a2015-11-04 16:02:47 -0800108 m_swJScript(script),
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700109 m_pRuntime(pRuntime),
110 m_pApp(pApp) {
Tom Sepez371c87f2015-08-13 16:56:19 -0700111 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700112 m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc);
Tom Sepez371c87f2015-08-13 16:56:19 -0700113 (*GetGlobalTimerMap())[m_nTimerID] = this;
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700114 m_pRuntime->AddObserver(this);
115}
116
117CJS_Timer::~CJS_Timer() {
118 CJS_Runtime* pRuntime = GetRuntime();
119 if (pRuntime)
120 pRuntime->RemoveObserver(this);
121 KillJSTimer();
Tom Sepez371c87f2015-08-13 16:56:19 -0700122}
123
124void CJS_Timer::KillJSTimer() {
125 if (m_nTimerID) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700126 if (m_bValid) {
127 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
128 pHandler->KillTimer(m_nTimerID);
129 }
Tom Sepez371c87f2015-08-13 16:56:19 -0700130 GetGlobalTimerMap()->erase(m_nTimerID);
131 m_nTimerID = 0;
132 }
133}
134
135// static
136void CJS_Timer::TimerProc(int idEvent) {
137 const auto it = GetGlobalTimerMap()->find(idEvent);
138 if (it != GetGlobalTimerMap()->end()) {
139 CJS_Timer* pTimer = it->second;
140 if (!pTimer->m_bProcessing) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700141 CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing);
142 pTimer->m_bProcessing = true;
Tom Sepez371c87f2015-08-13 16:56:19 -0700143 if (pTimer->m_pEmbedObj)
144 pTimer->m_pEmbedObj->TimerProc(pTimer);
Tom Sepez371c87f2015-08-13 16:56:19 -0700145 }
146 }
147}
148
149// static
150CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
151 // Leak the timer array at shutdown.
152 static auto* s_TimerMap = new TimerMap;
153 return s_TimerMap;
154}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700155
156void CJS_Timer::OnDestroyed() {
157 m_bValid = false;
158}