blob: e59b1a77ed04b88ea6fd1d365aa3d45c9016d92d [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
Tom Sepez37458412015-10-06 11:33:46 -07007#include "JS_Context.h"
8
Tom Sepez37458412015-10-06 11:33:46 -07009#include "JS_EventHandler.h"
10#include "JS_Runtime.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080011#include "fpdfsdk/include/javascript/IJavaScript.h"
Tom Sepez37458412015-10-06 11:33:46 -070012#include "resource.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013
14/* -------------------------- CJS_Context -------------------------- */
15
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016CJS_Context::CJS_Context(CJS_Runtime* pRuntime)
17 : m_pRuntime(pRuntime), m_bBusy(FALSE), m_bMsgBoxEnable(TRUE) {
18 m_pEventHandler = new CJS_EventHandler(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019}
20
Lei Zhang2b1a2d52015-08-14 22:16:22 -070021CJS_Context::~CJS_Context() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 delete m_pEventHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023}
24
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025CPDFSDK_Document* CJS_Context::GetReaderDocument() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 return m_pRuntime->GetReaderDocument();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027}
28
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029CPDFDoc_Environment* CJS_Context::GetReaderApp() {
30 ASSERT(m_pRuntime != NULL);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 return m_pRuntime->GetReaderApp();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033}
34
Tom Sepez9ed941a2015-09-01 09:51:10 -070035FX_BOOL CJS_Context::RunScript(const CFX_WideString& script,
Tom Sepez33420902015-10-13 15:00:10 -070036 CFX_WideString* info) {
Tom Sepez9ed941a2015-09-01 09:51:10 -070037 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
Tom Sepez51da0932015-11-25 16:05:49 -080038#ifdef PDF_ENABLE_XFA
Tom Sepez9ed941a2015-09-01 09:51:10 -070039 v8::Locker locker(m_pRuntime->GetIsolate());
Tom Sepez51da0932015-11-25 16:05:49 -080040#endif
Tom Sepez9ed941a2015-09-01 09:51:10 -070041 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
42 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
43 v8::Context::Scope context_scope(context);
44
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 if (m_bBusy) {
Tom Sepez33420902015-10-13 15:00:10 -070046 *info = JSGetStringFromID(this, IDS_STRING_JSBUSY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 return FALSE;
48 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 m_bBusy = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 ASSERT(m_pEventHandler->IsValid());
Tom Sepez5d0e8432015-09-22 15:50:03 -070052 CJS_Runtime::FieldEvent event(m_pEventHandler->TargetName(),
53 m_pEventHandler->EventType());
54 if (!m_pRuntime->AddEventToSet(event)) {
Tom Sepez33420902015-10-13 15:00:10 -070055 *info = JSGetStringFromID(this, IDS_STRING_JSEVENT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 return FALSE;
57 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070058
Tom Sepez33420902015-10-13 15:00:10 -070059 CFX_WideString sErrorMessage;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 int nRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 if (script.GetLength() > 0) {
Tom Sepez33420902015-10-13 15:00:10 -070062 nRet = m_pRuntime->Execute(this, script.c_str(), &sErrorMessage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 }
Bo Xud4e406e2014-08-13 11:03:19 -070064
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 if (nRet < 0) {
Tom Sepez33420902015-10-13 15:00:10 -070066 *info += sErrorMessage;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 } else {
Tom Sepez33420902015-10-13 15:00:10 -070068 *info = JSGetStringFromID(this, IDS_STRING_RUN);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
Tom Sepez5d0e8432015-09-22 15:50:03 -070071 m_pRuntime->RemoveEventFromSet(event);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 m_pEventHandler->Destroy();
73 m_bBusy = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 return nRet >= 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076}
77
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078void CJS_Context::OnApp_Init() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 m_pEventHandler->OnApp_Init();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080}
81
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc,
83 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 m_pEventHandler->OnDoc_Open(pDoc, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085}
86
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 m_pEventHandler->OnDoc_WillPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089}
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 m_pEventHandler->OnDoc_DidPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}
94
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 m_pEventHandler->OnDoc_WillSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097}
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 m_pEventHandler->OnDoc_DidSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101}
102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 m_pEventHandler->OnDoc_WillClose(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105}
106
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 m_pEventHandler->OnPage_Open(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109}
110
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 m_pEventHandler->OnPage_Close(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700113}
114
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 m_pEventHandler->OnPage_InView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117}
118
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 m_pEventHandler->OnPage_OutView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121}
122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123void CJS_Context::OnField_MouseDown(FX_BOOL bModifier,
124 FX_BOOL bShift,
125 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127}
128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier,
130 FX_BOOL bShift,
131 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133}
134
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135void CJS_Context::OnField_MouseExit(FX_BOOL bModifier,
136 FX_BOOL bShift,
137 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700139}
140
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141void CJS_Context::OnField_MouseUp(FX_BOOL bModifier,
142 FX_BOOL bShift,
143 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145}
146
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147void CJS_Context::OnField_Focus(FX_BOOL bModifier,
148 FX_BOOL bShift,
149 CPDF_FormField* pTarget,
150 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152}
153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154void CJS_Context::OnField_Blur(FX_BOOL bModifier,
155 FX_BOOL bShift,
156 CPDF_FormField* pTarget,
157 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700159}
160
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161void CJS_Context::OnField_Calculate(CPDF_FormField* pSource,
162 CPDF_FormField* pTarget,
163 CFX_WideString& Value,
164 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700165 m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700166}
167
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168void CJS_Context::OnField_Format(CPDF_FormField* pTarget,
169 CFX_WideString& Value,
170 FX_BOOL bWillCommit) {
171 m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172}
173
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700174void CJS_Context::OnField_Keystroke(CFX_WideString& strChange,
175 const CFX_WideString& strChangeEx,
176 FX_BOOL bKeyDown,
177 FX_BOOL bModifier,
178 int& nSelEnd,
179 int& nSelStart,
180 FX_BOOL bShift,
181 CPDF_FormField* pTarget,
182 CFX_WideString& Value,
183 FX_BOOL bWillCommit,
184 FX_BOOL bFieldFull,
185 FX_BOOL& bRc) {
186 m_pEventHandler->OnField_Keystroke(
187 strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
188 pTarget, Value, bWillCommit, bFieldFull, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189}
190
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700191void CJS_Context::OnField_Validate(CFX_WideString& strChange,
192 const CFX_WideString& strChangeEx,
193 FX_BOOL bKeyDown,
194 FX_BOOL bModifier,
195 FX_BOOL bShift,
196 CPDF_FormField* pTarget,
197 CFX_WideString& Value,
198 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier,
200 bShift, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201}
202
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203void CJS_Context::OnScreen_Focus(FX_BOOL bModifier,
204 FX_BOOL bShift,
205 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209void CJS_Context::OnScreen_Blur(FX_BOOL bModifier,
210 FX_BOOL bShift,
211 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213}
214
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215void CJS_Context::OnScreen_Open(FX_BOOL bModifier,
216 FX_BOOL bShift,
217 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700219}
220
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221void CJS_Context::OnScreen_Close(FX_BOOL bModifier,
222 FX_BOOL bShift,
223 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224 m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225}
226
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier,
228 FX_BOOL bShift,
229 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230 m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700231}
232
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier,
234 FX_BOOL bShift,
235 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236 m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237}
238
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier,
240 FX_BOOL bShift,
241 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243}
244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier,
246 FX_BOOL bShift,
247 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700249}
250
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251void CJS_Context::OnScreen_InView(FX_BOOL bModifier,
252 FX_BOOL bShift,
253 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255}
256
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257void CJS_Context::OnScreen_OutView(FX_BOOL bModifier,
258 FX_BOOL bShift,
259 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260 m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700261}
262
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 m_pEventHandler->OnBookmark_MouseUp(pBookMark);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265}
266
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 m_pEventHandler->OnLink_MouseUp(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269}
270
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271void CJS_Context::OnConsole_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 m_pEventHandler->OnConsole_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273}
274
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275void CJS_Context::OnExternal_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 m_pEventHandler->OnExternal_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700277}
278
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 m_pEventHandler->OnBatchExec(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281}
282
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,
284 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286}