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