blob: 66a3e0783da1be1fa2a16a588c16d91eff0620af [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
dsinclair64376be2016-03-31 20:03:24 -07007#include "fpdfsdk/javascript/cjs_context.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
Dan Sinclairf766ad22016-03-14 13:51:24 -04009#include "fpdfsdk/javascript/JS_EventHandler.h"
dsinclair64376be2016-03-31 20:03:24 -070010#include "fpdfsdk/javascript/cjs_runtime.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040011#include "fpdfsdk/javascript/resource.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012
Nico Weber9d8ec5a2015-08-04 13:00:21 -070013CJS_Context::CJS_Context(CJS_Runtime* pRuntime)
14 : m_pRuntime(pRuntime), m_bBusy(FALSE), m_bMsgBoxEnable(TRUE) {
15 m_pEventHandler = new CJS_EventHandler(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016}
17
Lei Zhang2b1a2d52015-08-14 22:16:22 -070018CJS_Context::~CJS_Context() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019 delete m_pEventHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020}
21
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022CPDFSDK_Document* CJS_Context::GetReaderDocument() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070023 return m_pRuntime->GetReaderDocument();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024}
25
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026CPDFDoc_Environment* CJS_Context::GetReaderApp() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 return m_pRuntime->GetReaderApp();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028}
29
Tom Sepez9ed941a2015-09-01 09:51:10 -070030FX_BOOL CJS_Context::RunScript(const CFX_WideString& script,
Tom Sepez33420902015-10-13 15:00:10 -070031 CFX_WideString* info) {
Tom Sepez9ed941a2015-09-01 09:51:10 -070032 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
Tom Sepez51da0932015-11-25 16:05:49 -080033#ifdef PDF_ENABLE_XFA
Tom Sepez9ed941a2015-09-01 09:51:10 -070034 v8::Locker locker(m_pRuntime->GetIsolate());
Tom Sepez40e9ff32015-11-30 12:39:54 -080035#endif // PDF_ENABLE_XFA
Tom Sepez9ed941a2015-09-01 09:51:10 -070036 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
37 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
38 v8::Context::Scope context_scope(context);
39
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040 if (m_bBusy) {
Tom Sepez33420902015-10-13 15:00:10 -070041 *info = JSGetStringFromID(this, IDS_STRING_JSBUSY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 return FALSE;
43 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 m_bBusy = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 ASSERT(m_pEventHandler->IsValid());
Tom Sepez5d0e8432015-09-22 15:50:03 -070047 CJS_Runtime::FieldEvent event(m_pEventHandler->TargetName(),
48 m_pEventHandler->EventType());
49 if (!m_pRuntime->AddEventToSet(event)) {
Tom Sepez33420902015-10-13 15:00:10 -070050 *info = JSGetStringFromID(this, IDS_STRING_JSEVENT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 return FALSE;
52 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053
Tom Sepez33420902015-10-13 15:00:10 -070054 CFX_WideString sErrorMessage;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 int nRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 if (script.GetLength() > 0) {
Tom Sepez33420902015-10-13 15:00:10 -070057 nRet = m_pRuntime->Execute(this, script.c_str(), &sErrorMessage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 }
Bo Xud4e406e2014-08-13 11:03:19 -070059
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 if (nRet < 0) {
Tom Sepez33420902015-10-13 15:00:10 -070061 *info += sErrorMessage;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 } else {
Tom Sepez33420902015-10-13 15:00:10 -070063 *info = JSGetStringFromID(this, IDS_STRING_RUN);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065
Tom Sepez5d0e8432015-09-22 15:50:03 -070066 m_pRuntime->RemoveEventFromSet(event);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 m_pEventHandler->Destroy();
68 m_bBusy = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070069
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 return nRet >= 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071}
72
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073void CJS_Context::OnApp_Init() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 m_pEventHandler->OnApp_Init();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075}
76
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc,
78 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 m_pEventHandler->OnDoc_Open(pDoc, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080}
81
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 m_pEventHandler->OnDoc_WillPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084}
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 m_pEventHandler->OnDoc_DidPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
89
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 m_pEventHandler->OnDoc_WillSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 m_pEventHandler->OnDoc_DidSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096}
97
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 m_pEventHandler->OnDoc_WillClose(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100}
101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 m_pEventHandler->OnPage_Open(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104}
105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 m_pEventHandler->OnPage_Close(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108}
109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 m_pEventHandler->OnPage_InView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112}
113
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 m_pEventHandler->OnPage_OutView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118void CJS_Context::OnField_MouseDown(FX_BOOL bModifier,
119 FX_BOOL bShift,
120 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122}
123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier,
125 FX_BOOL bShift,
126 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128}
129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130void CJS_Context::OnField_MouseExit(FX_BOOL bModifier,
131 FX_BOOL bShift,
132 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134}
135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136void CJS_Context::OnField_MouseUp(FX_BOOL bModifier,
137 FX_BOOL bShift,
138 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140}
141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142void CJS_Context::OnField_Focus(FX_BOOL bModifier,
143 FX_BOOL bShift,
144 CPDF_FormField* pTarget,
145 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147}
148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149void CJS_Context::OnField_Blur(FX_BOOL bModifier,
150 FX_BOOL bShift,
151 CPDF_FormField* pTarget,
152 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154}
155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156void CJS_Context::OnField_Calculate(CPDF_FormField* pSource,
157 CPDF_FormField* pTarget,
158 CFX_WideString& Value,
159 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161}
162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163void CJS_Context::OnField_Format(CPDF_FormField* pTarget,
164 CFX_WideString& Value,
165 FX_BOOL bWillCommit) {
166 m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167}
168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169void CJS_Context::OnField_Keystroke(CFX_WideString& strChange,
170 const CFX_WideString& strChangeEx,
171 FX_BOOL bKeyDown,
172 FX_BOOL bModifier,
173 int& nSelEnd,
174 int& nSelStart,
175 FX_BOOL bShift,
176 CPDF_FormField* pTarget,
177 CFX_WideString& Value,
178 FX_BOOL bWillCommit,
179 FX_BOOL bFieldFull,
180 FX_BOOL& bRc) {
181 m_pEventHandler->OnField_Keystroke(
182 strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
183 pTarget, Value, bWillCommit, bFieldFull, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184}
185
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186void CJS_Context::OnField_Validate(CFX_WideString& strChange,
187 const CFX_WideString& strChangeEx,
188 FX_BOOL bKeyDown,
189 FX_BOOL bModifier,
190 FX_BOOL bShift,
191 CPDF_FormField* pTarget,
192 CFX_WideString& Value,
193 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier,
195 bShift, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700196}
197
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198void CJS_Context::OnScreen_Focus(FX_BOOL bModifier,
199 FX_BOOL bShift,
200 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204void CJS_Context::OnScreen_Blur(FX_BOOL bModifier,
205 FX_BOOL bShift,
206 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208}
209
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210void CJS_Context::OnScreen_Open(FX_BOOL bModifier,
211 FX_BOOL bShift,
212 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700214}
215
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216void CJS_Context::OnScreen_Close(FX_BOOL bModifier,
217 FX_BOOL bShift,
218 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220}
221
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier,
223 FX_BOOL bShift,
224 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226}
227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier,
229 FX_BOOL bShift,
230 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232}
233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier,
235 FX_BOOL bShift,
236 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238}
239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier,
241 FX_BOOL bShift,
242 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700244}
245
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246void CJS_Context::OnScreen_InView(FX_BOOL bModifier,
247 FX_BOOL bShift,
248 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250}
251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252void CJS_Context::OnScreen_OutView(FX_BOOL bModifier,
253 FX_BOOL bShift,
254 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256}
257
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259 m_pEventHandler->OnBookmark_MouseUp(pBookMark);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700260}
261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 m_pEventHandler->OnLink_MouseUp(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266void CJS_Context::OnConsole_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 m_pEventHandler->OnConsole_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268}
269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270void CJS_Context::OnExternal_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 m_pEventHandler->OnExternal_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272}
273
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 m_pEventHandler->OnBatchExec(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276}
277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,
279 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281}