1 // 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.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "JS_Context.h"
8
9 #include "JS_EventHandler.h"
10 #include "JS_Runtime.h"
11 #include "fpdfsdk/include/javascript/IJavaScript.h"
12 #include "resource.h"
13
14 /* -------------------------- CJS_Context -------------------------- */
15
CJS_Context(CJS_Runtime * pRuntime)16 CJS_Context::CJS_Context(CJS_Runtime* pRuntime)
17 : m_pRuntime(pRuntime), m_bBusy(FALSE), m_bMsgBoxEnable(TRUE) {
18 m_pEventHandler = new CJS_EventHandler(this);
19 }
20
~CJS_Context()21 CJS_Context::~CJS_Context() {
22 delete m_pEventHandler;
23 }
24
GetReaderDocument()25 CPDFSDK_Document* CJS_Context::GetReaderDocument() {
26 return m_pRuntime->GetReaderDocument();
27 }
28
GetReaderApp()29 CPDFDoc_Environment* CJS_Context::GetReaderApp() {
30 return m_pRuntime->GetReaderApp();
31 }
32
RunScript(const CFX_WideString & script,CFX_WideString * info)33 FX_BOOL CJS_Context::RunScript(const CFX_WideString& script,
34 CFX_WideString* info) {
35 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
36 #ifdef PDF_ENABLE_XFA
37 v8::Locker locker(m_pRuntime->GetIsolate());
38 #endif // PDF_ENABLE_XFA
39 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
40 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
41 v8::Context::Scope context_scope(context);
42
43 if (m_bBusy) {
44 *info = JSGetStringFromID(this, IDS_STRING_JSBUSY);
45 return FALSE;
46 }
47 m_bBusy = TRUE;
48
49 ASSERT(m_pEventHandler->IsValid());
50 CJS_Runtime::FieldEvent event(m_pEventHandler->TargetName(),
51 m_pEventHandler->EventType());
52 if (!m_pRuntime->AddEventToSet(event)) {
53 *info = JSGetStringFromID(this, IDS_STRING_JSEVENT);
54 return FALSE;
55 }
56
57 CFX_WideString sErrorMessage;
58 int nRet = 0;
59 if (script.GetLength() > 0) {
60 nRet = m_pRuntime->Execute(this, script.c_str(), &sErrorMessage);
61 }
62
63 if (nRet < 0) {
64 *info += sErrorMessage;
65 } else {
66 *info = JSGetStringFromID(this, IDS_STRING_RUN);
67 }
68
69 m_pRuntime->RemoveEventFromSet(event);
70 m_pEventHandler->Destroy();
71 m_bBusy = FALSE;
72
73 return nRet >= 0;
74 }
75
OnApp_Init()76 void CJS_Context::OnApp_Init() {
77 m_pEventHandler->OnApp_Init();
78 }
79
OnDoc_Open(CPDFSDK_Document * pDoc,const CFX_WideString & strTargetName)80 void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc,
81 const CFX_WideString& strTargetName) {
82 m_pEventHandler->OnDoc_Open(pDoc, strTargetName);
83 }
84
OnDoc_WillPrint(CPDFSDK_Document * pDoc)85 void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc) {
86 m_pEventHandler->OnDoc_WillPrint(pDoc);
87 }
88
OnDoc_DidPrint(CPDFSDK_Document * pDoc)89 void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc) {
90 m_pEventHandler->OnDoc_DidPrint(pDoc);
91 }
92
OnDoc_WillSave(CPDFSDK_Document * pDoc)93 void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc) {
94 m_pEventHandler->OnDoc_WillSave(pDoc);
95 }
96
OnDoc_DidSave(CPDFSDK_Document * pDoc)97 void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc) {
98 m_pEventHandler->OnDoc_DidSave(pDoc);
99 }
100
OnDoc_WillClose(CPDFSDK_Document * pDoc)101 void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc) {
102 m_pEventHandler->OnDoc_WillClose(pDoc);
103 }
104
OnPage_Open(CPDFSDK_Document * pTarget)105 void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget) {
106 m_pEventHandler->OnPage_Open(pTarget);
107 }
108
OnPage_Close(CPDFSDK_Document * pTarget)109 void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget) {
110 m_pEventHandler->OnPage_Close(pTarget);
111 }
112
OnPage_InView(CPDFSDK_Document * pTarget)113 void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget) {
114 m_pEventHandler->OnPage_InView(pTarget);
115 }
116
OnPage_OutView(CPDFSDK_Document * pTarget)117 void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget) {
118 m_pEventHandler->OnPage_OutView(pTarget);
119 }
120
OnField_MouseDown(FX_BOOL bModifier,FX_BOOL bShift,CPDF_FormField * pTarget)121 void CJS_Context::OnField_MouseDown(FX_BOOL bModifier,
122 FX_BOOL bShift,
123 CPDF_FormField* pTarget) {
124 m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
125 }
126
OnField_MouseEnter(FX_BOOL bModifier,FX_BOOL bShift,CPDF_FormField * pTarget)127 void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier,
128 FX_BOOL bShift,
129 CPDF_FormField* pTarget) {
130 m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
131 }
132
OnField_MouseExit(FX_BOOL bModifier,FX_BOOL bShift,CPDF_FormField * pTarget)133 void CJS_Context::OnField_MouseExit(FX_BOOL bModifier,
134 FX_BOOL bShift,
135 CPDF_FormField* pTarget) {
136 m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
137 }
138
OnField_MouseUp(FX_BOOL bModifier,FX_BOOL bShift,CPDF_FormField * pTarget)139 void CJS_Context::OnField_MouseUp(FX_BOOL bModifier,
140 FX_BOOL bShift,
141 CPDF_FormField* pTarget) {
142 m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
143 }
144
OnField_Focus(FX_BOOL bModifier,FX_BOOL bShift,CPDF_FormField * pTarget,const CFX_WideString & Value)145 void CJS_Context::OnField_Focus(FX_BOOL bModifier,
146 FX_BOOL bShift,
147 CPDF_FormField* pTarget,
148 const CFX_WideString& Value) {
149 m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
150 }
151
OnField_Blur(FX_BOOL bModifier,FX_BOOL bShift,CPDF_FormField * pTarget,const CFX_WideString & Value)152 void CJS_Context::OnField_Blur(FX_BOOL bModifier,
153 FX_BOOL bShift,
154 CPDF_FormField* pTarget,
155 const CFX_WideString& Value) {
156 m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
157 }
158
OnField_Calculate(CPDF_FormField * pSource,CPDF_FormField * pTarget,CFX_WideString & Value,FX_BOOL & bRc)159 void CJS_Context::OnField_Calculate(CPDF_FormField* pSource,
160 CPDF_FormField* pTarget,
161 CFX_WideString& Value,
162 FX_BOOL& bRc) {
163 m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
164 }
165
OnField_Format(CPDF_FormField * pTarget,CFX_WideString & Value,FX_BOOL bWillCommit)166 void CJS_Context::OnField_Format(CPDF_FormField* pTarget,
167 CFX_WideString& Value,
168 FX_BOOL bWillCommit) {
169 m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
170 }
171
OnField_Keystroke(CFX_WideString & strChange,const CFX_WideString & strChangeEx,FX_BOOL bKeyDown,FX_BOOL bModifier,int & nSelEnd,int & nSelStart,FX_BOOL bShift,CPDF_FormField * pTarget,CFX_WideString & Value,FX_BOOL bWillCommit,FX_BOOL bFieldFull,FX_BOOL & bRc)172 void CJS_Context::OnField_Keystroke(CFX_WideString& strChange,
173 const CFX_WideString& strChangeEx,
174 FX_BOOL bKeyDown,
175 FX_BOOL bModifier,
176 int& nSelEnd,
177 int& nSelStart,
178 FX_BOOL bShift,
179 CPDF_FormField* pTarget,
180 CFX_WideString& Value,
181 FX_BOOL bWillCommit,
182 FX_BOOL bFieldFull,
183 FX_BOOL& bRc) {
184 m_pEventHandler->OnField_Keystroke(
185 strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
186 pTarget, Value, bWillCommit, bFieldFull, bRc);
187 }
188
OnField_Validate(CFX_WideString & strChange,const CFX_WideString & strChangeEx,FX_BOOL bKeyDown,FX_BOOL bModifier,FX_BOOL bShift,CPDF_FormField * pTarget,CFX_WideString & Value,FX_BOOL & bRc)189 void CJS_Context::OnField_Validate(CFX_WideString& strChange,
190 const CFX_WideString& strChangeEx,
191 FX_BOOL bKeyDown,
192 FX_BOOL bModifier,
193 FX_BOOL bShift,
194 CPDF_FormField* pTarget,
195 CFX_WideString& Value,
196 FX_BOOL& bRc) {
197 m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier,
198 bShift, pTarget, Value, bRc);
199 }
200
OnScreen_Focus(FX_BOOL bModifier,FX_BOOL bShift,CPDFSDK_Annot * pScreen)201 void CJS_Context::OnScreen_Focus(FX_BOOL bModifier,
202 FX_BOOL bShift,
203 CPDFSDK_Annot* pScreen) {
204 m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
205 }
206
OnScreen_Blur(FX_BOOL bModifier,FX_BOOL bShift,CPDFSDK_Annot * pScreen)207 void CJS_Context::OnScreen_Blur(FX_BOOL bModifier,
208 FX_BOOL bShift,
209 CPDFSDK_Annot* pScreen) {
210 m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
211 }
212
OnScreen_Open(FX_BOOL bModifier,FX_BOOL bShift,CPDFSDK_Annot * pScreen)213 void CJS_Context::OnScreen_Open(FX_BOOL bModifier,
214 FX_BOOL bShift,
215 CPDFSDK_Annot* pScreen) {
216 m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
217 }
218
OnScreen_Close(FX_BOOL bModifier,FX_BOOL bShift,CPDFSDK_Annot * pScreen)219 void CJS_Context::OnScreen_Close(FX_BOOL bModifier,
220 FX_BOOL bShift,
221 CPDFSDK_Annot* pScreen) {
222 m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
223 }
224
OnScreen_MouseDown(FX_BOOL bModifier,FX_BOOL bShift,CPDFSDK_Annot * pScreen)225 void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier,
226 FX_BOOL bShift,
227 CPDFSDK_Annot* pScreen) {
228 m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
229 }
230
OnScreen_MouseUp(FX_BOOL bModifier,FX_BOOL bShift,CPDFSDK_Annot * pScreen)231 void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier,
232 FX_BOOL bShift,
233 CPDFSDK_Annot* pScreen) {
234 m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
235 }
236
OnScreen_MouseEnter(FX_BOOL bModifier,FX_BOOL bShift,CPDFSDK_Annot * pScreen)237 void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier,
238 FX_BOOL bShift,
239 CPDFSDK_Annot* pScreen) {
240 m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
241 }
242
OnScreen_MouseExit(FX_BOOL bModifier,FX_BOOL bShift,CPDFSDK_Annot * pScreen)243 void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier,
244 FX_BOOL bShift,
245 CPDFSDK_Annot* pScreen) {
246 m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
247 }
248
OnScreen_InView(FX_BOOL bModifier,FX_BOOL bShift,CPDFSDK_Annot * pScreen)249 void CJS_Context::OnScreen_InView(FX_BOOL bModifier,
250 FX_BOOL bShift,
251 CPDFSDK_Annot* pScreen) {
252 m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
253 }
254
OnScreen_OutView(FX_BOOL bModifier,FX_BOOL bShift,CPDFSDK_Annot * pScreen)255 void CJS_Context::OnScreen_OutView(FX_BOOL bModifier,
256 FX_BOOL bShift,
257 CPDFSDK_Annot* pScreen) {
258 m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
259 }
260
OnBookmark_MouseUp(CPDF_Bookmark * pBookMark)261 void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
262 m_pEventHandler->OnBookmark_MouseUp(pBookMark);
263 }
264
OnLink_MouseUp(CPDFSDK_Document * pTarget)265 void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget) {
266 m_pEventHandler->OnLink_MouseUp(pTarget);
267 }
268
OnConsole_Exec()269 void CJS_Context::OnConsole_Exec() {
270 m_pEventHandler->OnConsole_Exec();
271 }
272
OnExternal_Exec()273 void CJS_Context::OnExternal_Exec() {
274 m_pEventHandler->OnExternal_Exec();
275 }
276
OnBatchExec(CPDFSDK_Document * pTarget)277 void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget) {
278 m_pEventHandler->OnBatchExec(pTarget);
279 }
280
OnMenu_Exec(CPDFSDK_Document * pTarget,const CFX_WideString & strTargetName)281 void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,
282 const CFX_WideString& strTargetName) {
283 m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
284 }
285