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 "fxjs/cjs_event_context.h"
8
9 #include "core/fxcrt/autorestorer.h"
10 #include "fxjs/cjs_eventrecorder.h"
11 #include "fxjs/cjs_field.h"
12 #include "fxjs/cjs_runtime.h"
13 #include "fxjs/js_define.h"
14 #include "fxjs/js_resources.h"
15 #include "third_party/base/ptr_util.h"
16
CJS_EventContext(CJS_Runtime * pRuntime)17 CJS_EventContext::CJS_EventContext(CJS_Runtime* pRuntime)
18 : m_pRuntime(pRuntime),
19 m_pEventRecorder(pdfium::MakeUnique<CJS_EventRecorder>()) {
20 ASSERT(pRuntime);
21 }
22
23 CJS_EventContext::~CJS_EventContext() = default;
24
GetFormFillEnv()25 CPDFSDK_FormFillEnvironment* CJS_EventContext::GetFormFillEnv() {
26 return m_pRuntime->GetFormFillEnv();
27 }
28
RunScript(const WideString & script)29 Optional<IJS_Runtime::JS_Error> CJS_EventContext::RunScript(
30 const WideString& script) {
31 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
32 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
33 v8::Local<v8::Context> context = m_pRuntime->GetV8Context();
34 v8::Context::Scope context_scope(context);
35
36 if (m_bBusy) {
37 return IJS_Runtime::JS_Error(1, 1,
38 JSGetStringFromID(JSMessage::kBusyError));
39 }
40
41 AutoRestorer<bool> restorer(&m_bBusy);
42 m_bBusy = true;
43
44 ASSERT(m_pEventRecorder->IsValid());
45 CJS_Runtime::FieldEvent event(m_pEventRecorder->TargetName(),
46 m_pEventRecorder->EventType());
47 if (!m_pRuntime->AddEventToSet(event)) {
48 return IJS_Runtime::JS_Error(
49 1, 1, JSGetStringFromID(JSMessage::kDuplicateEventError));
50 }
51
52 Optional<IJS_Runtime::JS_Error> err;
53 if (script.GetLength() > 0)
54 err = m_pRuntime->ExecuteScript(script);
55
56 m_pRuntime->RemoveEventFromSet(event);
57 m_pEventRecorder->Destroy();
58 return err;
59 }
60
SourceField()61 CJS_Field* CJS_EventContext::SourceField() {
62 v8::Local<v8::Object> pDocObj = m_pRuntime->NewFXJSBoundObject(
63 CJS_Document::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
64 if (pDocObj.IsEmpty())
65 return nullptr;
66
67 v8::Local<v8::Object> pFieldObj = m_pRuntime->NewFXJSBoundObject(
68 CJS_Field::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
69 if (pFieldObj.IsEmpty())
70 return nullptr;
71
72 auto* pFormFillEnv = m_pEventRecorder->GetFormFillEnvironment();
73 if (!pFormFillEnv)
74 pFormFillEnv = GetFormFillEnv();
75
76 auto* pJSDocument =
77 static_cast<CJS_Document*>(CFXJS_Engine::GetObjectPrivate(pDocObj));
78 pJSDocument->SetFormFillEnv(pFormFillEnv);
79
80 auto* pJSField =
81 static_cast<CJS_Field*>(CFXJS_Engine::GetObjectPrivate(pFieldObj));
82 pJSField->AttachField(pJSDocument, m_pEventRecorder->SourceName());
83 return pJSField;
84 }
85
TargetField()86 CJS_Field* CJS_EventContext::TargetField() {
87 v8::Local<v8::Object> pDocObj = m_pRuntime->NewFXJSBoundObject(
88 CJS_Document::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
89 if (pDocObj.IsEmpty())
90 return nullptr;
91
92 v8::Local<v8::Object> pFieldObj = m_pRuntime->NewFXJSBoundObject(
93 CJS_Field::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
94 if (pFieldObj.IsEmpty())
95 return nullptr;
96
97 auto* pFormFillEnv = m_pEventRecorder->GetFormFillEnvironment();
98 if (!pFormFillEnv)
99 pFormFillEnv = GetFormFillEnv();
100
101 auto* pJSDocument =
102 static_cast<CJS_Document*>(CFXJS_Engine::GetObjectPrivate(pDocObj));
103 pJSDocument->SetFormFillEnv(pFormFillEnv);
104
105 auto* pJSField =
106 static_cast<CJS_Field*>(CFXJS_Engine::GetObjectPrivate(pFieldObj));
107 pJSField->AttachField(pJSDocument, m_pEventRecorder->TargetName());
108 return pJSField;
109 }
110
OnApp_Init()111 void CJS_EventContext::OnApp_Init() {
112 m_pEventRecorder->OnApp_Init();
113 }
114
OnDoc_Open(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & strTargetName)115 void CJS_EventContext::OnDoc_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv,
116 const WideString& strTargetName) {
117 m_pEventRecorder->OnDoc_Open(pFormFillEnv, strTargetName);
118 }
119
OnDoc_WillPrint(CPDFSDK_FormFillEnvironment * pFormFillEnv)120 void CJS_EventContext::OnDoc_WillPrint(
121 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
122 m_pEventRecorder->OnDoc_WillPrint(pFormFillEnv);
123 }
124
OnDoc_DidPrint(CPDFSDK_FormFillEnvironment * pFormFillEnv)125 void CJS_EventContext::OnDoc_DidPrint(
126 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
127 m_pEventRecorder->OnDoc_DidPrint(pFormFillEnv);
128 }
129
OnDoc_WillSave(CPDFSDK_FormFillEnvironment * pFormFillEnv)130 void CJS_EventContext::OnDoc_WillSave(
131 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
132 m_pEventRecorder->OnDoc_WillSave(pFormFillEnv);
133 }
134
OnDoc_DidSave(CPDFSDK_FormFillEnvironment * pFormFillEnv)135 void CJS_EventContext::OnDoc_DidSave(
136 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
137 m_pEventRecorder->OnDoc_DidSave(pFormFillEnv);
138 }
139
OnDoc_WillClose(CPDFSDK_FormFillEnvironment * pFormFillEnv)140 void CJS_EventContext::OnDoc_WillClose(
141 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
142 m_pEventRecorder->OnDoc_WillClose(pFormFillEnv);
143 }
144
OnPage_Open(CPDFSDK_FormFillEnvironment * pFormFillEnv)145 void CJS_EventContext::OnPage_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
146 m_pEventRecorder->OnPage_Open(pFormFillEnv);
147 }
148
OnPage_Close(CPDFSDK_FormFillEnvironment * pFormFillEnv)149 void CJS_EventContext::OnPage_Close(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
150 m_pEventRecorder->OnPage_Close(pFormFillEnv);
151 }
152
OnPage_InView(CPDFSDK_FormFillEnvironment * pFormFillEnv)153 void CJS_EventContext::OnPage_InView(
154 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
155 m_pEventRecorder->OnPage_InView(pFormFillEnv);
156 }
157
OnPage_OutView(CPDFSDK_FormFillEnvironment * pFormFillEnv)158 void CJS_EventContext::OnPage_OutView(
159 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
160 m_pEventRecorder->OnPage_OutView(pFormFillEnv);
161 }
162
OnField_MouseDown(bool bModifier,bool bShift,CPDF_FormField * pTarget)163 void CJS_EventContext::OnField_MouseDown(bool bModifier,
164 bool bShift,
165 CPDF_FormField* pTarget) {
166 m_pEventRecorder->OnField_MouseDown(bModifier, bShift, pTarget);
167 }
168
OnField_MouseEnter(bool bModifier,bool bShift,CPDF_FormField * pTarget)169 void CJS_EventContext::OnField_MouseEnter(bool bModifier,
170 bool bShift,
171 CPDF_FormField* pTarget) {
172 m_pEventRecorder->OnField_MouseEnter(bModifier, bShift, pTarget);
173 }
174
OnField_MouseExit(bool bModifier,bool bShift,CPDF_FormField * pTarget)175 void CJS_EventContext::OnField_MouseExit(bool bModifier,
176 bool bShift,
177 CPDF_FormField* pTarget) {
178 m_pEventRecorder->OnField_MouseExit(bModifier, bShift, pTarget);
179 }
180
OnField_MouseUp(bool bModifier,bool bShift,CPDF_FormField * pTarget)181 void CJS_EventContext::OnField_MouseUp(bool bModifier,
182 bool bShift,
183 CPDF_FormField* pTarget) {
184 m_pEventRecorder->OnField_MouseUp(bModifier, bShift, pTarget);
185 }
186
OnField_Focus(bool bModifier,bool bShift,CPDF_FormField * pTarget,WideString * Value)187 void CJS_EventContext::OnField_Focus(bool bModifier,
188 bool bShift,
189 CPDF_FormField* pTarget,
190 WideString* Value) {
191 m_pEventRecorder->OnField_Focus(bModifier, bShift, pTarget, Value);
192 }
193
OnField_Blur(bool bModifier,bool bShift,CPDF_FormField * pTarget,WideString * Value)194 void CJS_EventContext::OnField_Blur(bool bModifier,
195 bool bShift,
196 CPDF_FormField* pTarget,
197 WideString* Value) {
198 m_pEventRecorder->OnField_Blur(bModifier, bShift, pTarget, Value);
199 }
200
OnField_Calculate(CPDF_FormField * pSource,CPDF_FormField * pTarget,WideString * pValue,bool * pRc)201 void CJS_EventContext::OnField_Calculate(CPDF_FormField* pSource,
202 CPDF_FormField* pTarget,
203 WideString* pValue,
204 bool* pRc) {
205 m_pEventRecorder->OnField_Calculate(pSource, pTarget, pValue, pRc);
206 }
207
OnField_Format(CPDF_FormField * pTarget,WideString * Value)208 void CJS_EventContext::OnField_Format(CPDF_FormField* pTarget,
209 WideString* Value) {
210 m_pEventRecorder->OnField_Format(pTarget, Value);
211 }
212
OnField_Keystroke(WideString * strChange,const WideString & strChangeEx,bool bKeyDown,bool bModifier,int * nSelEnd,int * nSelStart,bool bShift,CPDF_FormField * pTarget,WideString * Value,bool bWillCommit,bool bFieldFull,bool * bRc)213 void CJS_EventContext::OnField_Keystroke(WideString* strChange,
214 const WideString& strChangeEx,
215 bool bKeyDown,
216 bool bModifier,
217 int* nSelEnd,
218 int* nSelStart,
219 bool bShift,
220 CPDF_FormField* pTarget,
221 WideString* Value,
222 bool bWillCommit,
223 bool bFieldFull,
224 bool* bRc) {
225 m_pEventRecorder->OnField_Keystroke(
226 strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
227 pTarget, Value, bWillCommit, bFieldFull, bRc);
228 }
229
OnField_Validate(WideString * strChange,const WideString & strChangeEx,bool bKeyDown,bool bModifier,bool bShift,CPDF_FormField * pTarget,WideString * Value,bool * bRc)230 void CJS_EventContext::OnField_Validate(WideString* strChange,
231 const WideString& strChangeEx,
232 bool bKeyDown,
233 bool bModifier,
234 bool bShift,
235 CPDF_FormField* pTarget,
236 WideString* Value,
237 bool* bRc) {
238 m_pEventRecorder->OnField_Validate(strChange, strChangeEx, bKeyDown,
239 bModifier, bShift, pTarget, Value, bRc);
240 }
241
OnScreen_Focus(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)242 void CJS_EventContext::OnScreen_Focus(bool bModifier,
243 bool bShift,
244 CPDFSDK_Annot* pScreen) {
245 m_pEventRecorder->OnScreen_Focus(bModifier, bShift, pScreen);
246 }
247
OnScreen_Blur(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)248 void CJS_EventContext::OnScreen_Blur(bool bModifier,
249 bool bShift,
250 CPDFSDK_Annot* pScreen) {
251 m_pEventRecorder->OnScreen_Blur(bModifier, bShift, pScreen);
252 }
253
OnScreen_Open(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)254 void CJS_EventContext::OnScreen_Open(bool bModifier,
255 bool bShift,
256 CPDFSDK_Annot* pScreen) {
257 m_pEventRecorder->OnScreen_Open(bModifier, bShift, pScreen);
258 }
259
OnScreen_Close(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)260 void CJS_EventContext::OnScreen_Close(bool bModifier,
261 bool bShift,
262 CPDFSDK_Annot* pScreen) {
263 m_pEventRecorder->OnScreen_Close(bModifier, bShift, pScreen);
264 }
265
OnScreen_MouseDown(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)266 void CJS_EventContext::OnScreen_MouseDown(bool bModifier,
267 bool bShift,
268 CPDFSDK_Annot* pScreen) {
269 m_pEventRecorder->OnScreen_MouseDown(bModifier, bShift, pScreen);
270 }
271
OnScreen_MouseUp(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)272 void CJS_EventContext::OnScreen_MouseUp(bool bModifier,
273 bool bShift,
274 CPDFSDK_Annot* pScreen) {
275 m_pEventRecorder->OnScreen_MouseUp(bModifier, bShift, pScreen);
276 }
277
OnScreen_MouseEnter(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)278 void CJS_EventContext::OnScreen_MouseEnter(bool bModifier,
279 bool bShift,
280 CPDFSDK_Annot* pScreen) {
281 m_pEventRecorder->OnScreen_MouseEnter(bModifier, bShift, pScreen);
282 }
283
OnScreen_MouseExit(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)284 void CJS_EventContext::OnScreen_MouseExit(bool bModifier,
285 bool bShift,
286 CPDFSDK_Annot* pScreen) {
287 m_pEventRecorder->OnScreen_MouseExit(bModifier, bShift, pScreen);
288 }
289
OnScreen_InView(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)290 void CJS_EventContext::OnScreen_InView(bool bModifier,
291 bool bShift,
292 CPDFSDK_Annot* pScreen) {
293 m_pEventRecorder->OnScreen_InView(bModifier, bShift, pScreen);
294 }
295
OnScreen_OutView(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)296 void CJS_EventContext::OnScreen_OutView(bool bModifier,
297 bool bShift,
298 CPDFSDK_Annot* pScreen) {
299 m_pEventRecorder->OnScreen_OutView(bModifier, bShift, pScreen);
300 }
301
OnBookmark_MouseUp(CPDF_Bookmark * pBookMark)302 void CJS_EventContext::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
303 m_pEventRecorder->OnBookmark_MouseUp(pBookMark);
304 }
305
OnLink_MouseUp(CPDFSDK_FormFillEnvironment * pFormFillEnv)306 void CJS_EventContext::OnLink_MouseUp(
307 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
308 m_pEventRecorder->OnLink_MouseUp(pFormFillEnv);
309 }
310
OnConsole_Exec()311 void CJS_EventContext::OnConsole_Exec() {
312 m_pEventRecorder->OnConsole_Exec();
313 }
314
OnExternal_Exec()315 void CJS_EventContext::OnExternal_Exec() {
316 m_pEventRecorder->OnExternal_Exec();
317 }
318
OnBatchExec(CPDFSDK_FormFillEnvironment * pFormFillEnv)319 void CJS_EventContext::OnBatchExec(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
320 m_pEventRecorder->OnBatchExec(pFormFillEnv);
321 }
322
OnMenu_Exec(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & strTargetName)323 void CJS_EventContext::OnMenu_Exec(CPDFSDK_FormFillEnvironment* pFormFillEnv,
324 const WideString& strTargetName) {
325 m_pEventRecorder->OnMenu_Exec(pFormFillEnv, strTargetName);
326 }
327