blob: a3f972c1cd3f175350b774a712bccb1e3ed00097 [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
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07007#include "../../include/javascript/IJavaScript.h"
8#include "../../include/javascript/JS_Define.h"
9#include "../../include/javascript/JS_Object.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070010#include "../../include/javascript/JS_Context.h"
11
Nico Weber9d8ec5a2015-08-04 13:00:21 -070012int FXJS_MsgBox(CPDFDoc_Environment* pApp,
13 CPDFSDK_PageView* pPageView,
14 const FX_WCHAR* swMsg,
15 const FX_WCHAR* swTitle,
16 FX_UINT nType,
17 FX_UINT nIcon) {
18 if (!pApp)
19 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
22 pDoc->KillFocusAnnot();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025}
26
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027CPDFSDK_PageView* FXJS_GetPageView(IFXJS_Context* cc) {
28 if (CJS_Context* pContext = (CJS_Context*)cc) {
29 if (pContext->GetReaderDocument())
30 return NULL;
31 }
32 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033}
34
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
36
37CJS_EmbedObj::~CJS_EmbedObj() {
38 m_pJSObject = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039}
40
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041CPDFSDK_PageView* CJS_EmbedObj::JSGetPageView(IFXJS_Context* cc) {
42 return FXJS_GetPageView(cc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043}
44
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp,
46 CPDFSDK_PageView* pPageView,
47 const FX_WCHAR* swMsg,
48 const FX_WCHAR* swTitle,
49 FX_UINT nType,
50 FX_UINT nIcon) {
51 return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052}
53
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
55 CJS_Object::Alert(pContext, swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056}
57
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
59 CJS_Object* pJSObj = data.GetParameter();
60 pJSObj->ExitInstance();
61 delete pJSObj;
Tom Sepez39bfe122015-09-17 15:25:23 -070062 FXJS_FreePrivate(data.GetInternalField(0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063}
64
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
66 CJS_Object* pJSObj = data.GetParameter();
67 pJSObj->Dispose();
68 data.SetSecondPassCallback(FreeObject);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020069}
70
Tom Sepez116e4ad2015-09-21 09:22:05 -070071CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) {
72 m_pIsolate = pObject->CreationContext()->GetIsolate();
73 m_pV8Object.Reset(m_pIsolate, pObject);
74}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075
Lei Zhang2b1a2d52015-08-14 22:16:22 -070076CJS_Object::~CJS_Object() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070077}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079void CJS_Object::MakeWeak() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070080 m_pV8Object.SetWeak(this, DisposeObject,
81 v8::WeakCallbackType::kInternalFields);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020082}
83
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084void CJS_Object::Dispose() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070085 m_pV8Object.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086}
87
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088CPDFSDK_PageView* CJS_Object::JSGetPageView(IFXJS_Context* cc) {
89 return FXJS_GetPageView(cc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090}
91
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092int CJS_Object::MsgBox(CPDFDoc_Environment* pApp,
93 CPDFSDK_PageView* pPageView,
94 const FX_WCHAR* swMsg,
95 const FX_WCHAR* swTitle,
96 FX_UINT nType,
97 FX_UINT nIcon) {
98 return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099}
100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
102 ASSERT(pContext != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 if (pContext->IsMsgBoxEnabled()) {
105 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
106 if (pApp)
107 pApp->JS_appAlert(swMsg, NULL, 0, 3);
108 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109}
Tom Sepez371c87f2015-08-13 16:56:19 -0700110
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700111CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj,
112 CPDFDoc_Environment* pApp,
113 CJS_Runtime* pRuntime,
114 int nType,
115 const CFX_WideString& script,
116 FX_DWORD dwElapse,
117 FX_DWORD dwTimeOut)
118 : m_nTimerID(0),
119 m_pEmbedObj(pObj),
120 m_bProcessing(false),
121 m_bValid(true),
122 m_nType(nType),
123 m_dwTimeOut(dwTimeOut),
124 m_pRuntime(pRuntime),
125 m_pApp(pApp) {
Tom Sepez371c87f2015-08-13 16:56:19 -0700126 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700127 m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc);
Tom Sepez371c87f2015-08-13 16:56:19 -0700128 (*GetGlobalTimerMap())[m_nTimerID] = this;
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700129 m_pRuntime->AddObserver(this);
130}
131
132CJS_Timer::~CJS_Timer() {
133 CJS_Runtime* pRuntime = GetRuntime();
134 if (pRuntime)
135 pRuntime->RemoveObserver(this);
136 KillJSTimer();
Tom Sepez371c87f2015-08-13 16:56:19 -0700137}
138
139void CJS_Timer::KillJSTimer() {
140 if (m_nTimerID) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700141 if (m_bValid) {
142 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
143 pHandler->KillTimer(m_nTimerID);
144 }
Tom Sepez371c87f2015-08-13 16:56:19 -0700145 GetGlobalTimerMap()->erase(m_nTimerID);
146 m_nTimerID = 0;
147 }
148}
149
150// static
151void CJS_Timer::TimerProc(int idEvent) {
152 const auto it = GetGlobalTimerMap()->find(idEvent);
153 if (it != GetGlobalTimerMap()->end()) {
154 CJS_Timer* pTimer = it->second;
155 if (!pTimer->m_bProcessing) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700156 CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing);
157 pTimer->m_bProcessing = true;
Tom Sepez371c87f2015-08-13 16:56:19 -0700158 if (pTimer->m_pEmbedObj)
159 pTimer->m_pEmbedObj->TimerProc(pTimer);
Tom Sepez371c87f2015-08-13 16:56:19 -0700160 }
161 }
162}
163
164// static
165CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
166 // Leak the timer array at shutdown.
167 static auto* s_TimerMap = new TimerMap;
168 return s_TimerMap;
169}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700170
171void CJS_Timer::OnDestroyed() {
172 m_bValid = false;
173}