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 "fxjs/cjs_eventrecorder.h"
8
9 #include "core/fpdfdoc/cpdf_bookmark.h"
10 #include "core/fpdfdoc/cpdf_formfield.h"
11
12 CJS_EventRecorder::CJS_EventRecorder() = default;
13
14 CJS_EventRecorder::~CJS_EventRecorder() = default;
15
OnApp_Init()16 void CJS_EventRecorder::OnApp_Init() {
17 Initialize(JET_APP_INIT);
18 }
19
OnDoc_Open(CPDFSDK_FormFillEnvironment * pFormFillEnv,const WideString & strTargetName)20 void CJS_EventRecorder::OnDoc_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv,
21 const WideString& strTargetName) {
22 Initialize(JET_DOC_OPEN);
23 m_pTargetFormFillEnv.Reset(pFormFillEnv);
24 m_strTargetName = strTargetName;
25 }
26
OnDoc_WillPrint(CPDFSDK_FormFillEnvironment * pFormFillEnv)27 void CJS_EventRecorder::OnDoc_WillPrint(
28 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
29 Initialize(JET_DOC_WILLPRINT);
30 m_pTargetFormFillEnv.Reset(pFormFillEnv);
31 }
32
OnDoc_DidPrint(CPDFSDK_FormFillEnvironment * pFormFillEnv)33 void CJS_EventRecorder::OnDoc_DidPrint(
34 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
35 Initialize(JET_DOC_DIDPRINT);
36 m_pTargetFormFillEnv.Reset(pFormFillEnv);
37 }
38
OnDoc_WillSave(CPDFSDK_FormFillEnvironment * pFormFillEnv)39 void CJS_EventRecorder::OnDoc_WillSave(
40 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
41 Initialize(JET_DOC_WILLSAVE);
42 m_pTargetFormFillEnv.Reset(pFormFillEnv);
43 }
44
OnDoc_DidSave(CPDFSDK_FormFillEnvironment * pFormFillEnv)45 void CJS_EventRecorder::OnDoc_DidSave(
46 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
47 Initialize(JET_DOC_DIDSAVE);
48 m_pTargetFormFillEnv.Reset(pFormFillEnv);
49 }
50
OnDoc_WillClose(CPDFSDK_FormFillEnvironment * pFormFillEnv)51 void CJS_EventRecorder::OnDoc_WillClose(
52 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
53 Initialize(JET_DOC_WILLCLOSE);
54 m_pTargetFormFillEnv.Reset(pFormFillEnv);
55 }
56
OnPage_Open(CPDFSDK_FormFillEnvironment * pFormFillEnv)57 void CJS_EventRecorder::OnPage_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
58 Initialize(JET_PAGE_OPEN);
59 m_pTargetFormFillEnv.Reset(pFormFillEnv);
60 }
61
OnPage_Close(CPDFSDK_FormFillEnvironment * pFormFillEnv)62 void CJS_EventRecorder::OnPage_Close(
63 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
64 Initialize(JET_PAGE_CLOSE);
65 m_pTargetFormFillEnv.Reset(pFormFillEnv);
66 }
67
OnPage_InView(CPDFSDK_FormFillEnvironment * pFormFillEnv)68 void CJS_EventRecorder::OnPage_InView(
69 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
70 Initialize(JET_PAGE_INVIEW);
71 m_pTargetFormFillEnv.Reset(pFormFillEnv);
72 }
73
OnPage_OutView(CPDFSDK_FormFillEnvironment * pFormFillEnv)74 void CJS_EventRecorder::OnPage_OutView(
75 CPDFSDK_FormFillEnvironment* pFormFillEnv) {
76 Initialize(JET_PAGE_OUTVIEW);
77 m_pTargetFormFillEnv.Reset(pFormFillEnv);
78 }
79
OnField_MouseEnter(bool bModifier,bool bShift,CPDF_FormField * pTarget)80 void CJS_EventRecorder::OnField_MouseEnter(bool bModifier,
81 bool bShift,
82 CPDF_FormField* pTarget) {
83 Initialize(JET_FIELD_MOUSEENTER);
84
85 m_bModifier = bModifier;
86 m_bShift = bShift;
87
88 m_strTargetName = pTarget->GetFullName();
89 }
90
OnField_MouseExit(bool bModifier,bool bShift,CPDF_FormField * pTarget)91 void CJS_EventRecorder::OnField_MouseExit(bool bModifier,
92 bool bShift,
93 CPDF_FormField* pTarget) {
94 Initialize(JET_FIELD_MOUSEEXIT);
95
96 m_bModifier = bModifier;
97 m_bShift = bShift;
98 m_strTargetName = pTarget->GetFullName();
99 }
100
OnField_MouseDown(bool bModifier,bool bShift,CPDF_FormField * pTarget)101 void CJS_EventRecorder::OnField_MouseDown(bool bModifier,
102 bool bShift,
103 CPDF_FormField* pTarget) {
104 Initialize(JET_FIELD_MOUSEDOWN);
105 m_eEventType = JET_FIELD_MOUSEDOWN;
106
107 m_bModifier = bModifier;
108 m_bShift = bShift;
109 m_strTargetName = pTarget->GetFullName();
110 }
111
OnField_MouseUp(bool bModifier,bool bShift,CPDF_FormField * pTarget)112 void CJS_EventRecorder::OnField_MouseUp(bool bModifier,
113 bool bShift,
114 CPDF_FormField* pTarget) {
115 Initialize(JET_FIELD_MOUSEUP);
116
117 m_bModifier = bModifier;
118 m_bShift = bShift;
119 m_strTargetName = pTarget->GetFullName();
120 }
121
OnField_Focus(bool bModifier,bool bShift,CPDF_FormField * pTarget,WideString * pValue)122 void CJS_EventRecorder::OnField_Focus(bool bModifier,
123 bool bShift,
124 CPDF_FormField* pTarget,
125 WideString* pValue) {
126 ASSERT(pValue);
127 Initialize(JET_FIELD_FOCUS);
128
129 m_bModifier = bModifier;
130 m_bShift = bShift;
131 m_strTargetName = pTarget->GetFullName();
132 m_pValue = pValue;
133 }
134
OnField_Blur(bool bModifier,bool bShift,CPDF_FormField * pTarget,WideString * pValue)135 void CJS_EventRecorder::OnField_Blur(bool bModifier,
136 bool bShift,
137 CPDF_FormField* pTarget,
138 WideString* pValue) {
139 ASSERT(pValue);
140 Initialize(JET_FIELD_BLUR);
141
142 m_bModifier = bModifier;
143 m_bShift = bShift;
144 m_strTargetName = pTarget->GetFullName();
145 m_pValue = pValue;
146 }
147
OnField_Keystroke(WideString * strChange,const WideString & strChangeEx,bool KeyDown,bool bModifier,int * pSelEnd,int * pSelStart,bool bShift,CPDF_FormField * pTarget,WideString * pValue,bool bWillCommit,bool bFieldFull,bool * pbRc)148 void CJS_EventRecorder::OnField_Keystroke(WideString* strChange,
149 const WideString& strChangeEx,
150 bool KeyDown,
151 bool bModifier,
152 int* pSelEnd,
153 int* pSelStart,
154 bool bShift,
155 CPDF_FormField* pTarget,
156 WideString* pValue,
157 bool bWillCommit,
158 bool bFieldFull,
159 bool* pbRc) {
160 ASSERT(pValue);
161 ASSERT(pbRc);
162 ASSERT(pSelStart);
163 ASSERT(pSelEnd);
164
165 Initialize(JET_FIELD_KEYSTROKE);
166
167 m_nCommitKey = 0;
168 m_pWideStrChange = strChange;
169 m_WideStrChangeEx = strChangeEx;
170 m_bKeyDown = KeyDown;
171 m_bModifier = bModifier;
172 m_pISelEnd = pSelEnd;
173 m_pISelStart = pSelStart;
174 m_bShift = bShift;
175 m_strTargetName = pTarget->GetFullName();
176 m_pValue = pValue;
177 m_bWillCommit = bWillCommit;
178 m_pbRc = pbRc;
179 m_bFieldFull = bFieldFull;
180 }
181
OnField_Validate(WideString * strChange,const WideString & strChangeEx,bool bKeyDown,bool bModifier,bool bShift,CPDF_FormField * pTarget,WideString * pValue,bool * pbRc)182 void CJS_EventRecorder::OnField_Validate(WideString* strChange,
183 const WideString& strChangeEx,
184 bool bKeyDown,
185 bool bModifier,
186 bool bShift,
187 CPDF_FormField* pTarget,
188 WideString* pValue,
189 bool* pbRc) {
190 ASSERT(pValue);
191 ASSERT(pbRc);
192
193 Initialize(JET_FIELD_VALIDATE);
194
195 m_pWideStrChange = strChange;
196 m_WideStrChangeEx = strChangeEx;
197 m_bKeyDown = bKeyDown;
198 m_bModifier = bModifier;
199 m_bShift = bShift;
200 m_strTargetName = pTarget->GetFullName();
201 m_pValue = pValue;
202 m_pbRc = pbRc;
203 }
204
OnField_Calculate(CPDF_FormField * pSource,CPDF_FormField * pTarget,WideString * pValue,bool * pRc)205 void CJS_EventRecorder::OnField_Calculate(CPDF_FormField* pSource,
206 CPDF_FormField* pTarget,
207 WideString* pValue,
208 bool* pRc) {
209 ASSERT(pValue);
210 ASSERT(pRc);
211
212 Initialize(JET_FIELD_CALCULATE);
213
214 if (pSource)
215 m_strSourceName = pSource->GetFullName();
216 m_strTargetName = pTarget->GetFullName();
217 m_pValue = pValue;
218 m_pbRc = pRc;
219 }
220
OnField_Format(CPDF_FormField * pTarget,WideString * pValue)221 void CJS_EventRecorder::OnField_Format(CPDF_FormField* pTarget,
222 WideString* pValue) {
223 ASSERT(pValue);
224 Initialize(JET_FIELD_FORMAT);
225
226 m_nCommitKey = 0;
227 m_strTargetName = pTarget->GetFullName();
228 m_pValue = pValue;
229 m_bWillCommit = true;
230 }
231
OnScreen_Focus(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)232 void CJS_EventRecorder::OnScreen_Focus(bool bModifier,
233 bool bShift,
234 CPDFSDK_Annot* pScreen) {
235 Initialize(JET_SCREEN_FOCUS);
236
237 m_bModifier = bModifier;
238 m_bShift = bShift;
239 m_pTargetAnnot.Reset(pScreen);
240 }
241
OnScreen_Blur(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)242 void CJS_EventRecorder::OnScreen_Blur(bool bModifier,
243 bool bShift,
244 CPDFSDK_Annot* pScreen) {
245 Initialize(JET_SCREEN_BLUR);
246
247 m_bModifier = bModifier;
248 m_bShift = bShift;
249 m_pTargetAnnot.Reset(pScreen);
250 }
251
OnScreen_Open(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)252 void CJS_EventRecorder::OnScreen_Open(bool bModifier,
253 bool bShift,
254 CPDFSDK_Annot* pScreen) {
255 Initialize(JET_SCREEN_OPEN);
256
257 m_bModifier = bModifier;
258 m_bShift = bShift;
259 m_pTargetAnnot.Reset(pScreen);
260 }
261
OnScreen_Close(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)262 void CJS_EventRecorder::OnScreen_Close(bool bModifier,
263 bool bShift,
264 CPDFSDK_Annot* pScreen) {
265 Initialize(JET_SCREEN_CLOSE);
266
267 m_bModifier = bModifier;
268 m_bShift = bShift;
269 m_pTargetAnnot.Reset(pScreen);
270 }
271
OnScreen_MouseDown(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)272 void CJS_EventRecorder::OnScreen_MouseDown(bool bModifier,
273 bool bShift,
274 CPDFSDK_Annot* pScreen) {
275 Initialize(JET_SCREEN_MOUSEDOWN);
276
277 m_bModifier = bModifier;
278 m_bShift = bShift;
279 m_pTargetAnnot.Reset(pScreen);
280 }
281
OnScreen_MouseUp(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)282 void CJS_EventRecorder::OnScreen_MouseUp(bool bModifier,
283 bool bShift,
284 CPDFSDK_Annot* pScreen) {
285 Initialize(JET_SCREEN_MOUSEUP);
286
287 m_bModifier = bModifier;
288 m_bShift = bShift;
289 m_pTargetAnnot.Reset(pScreen);
290 }
291
OnScreen_MouseEnter(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)292 void CJS_EventRecorder::OnScreen_MouseEnter(bool bModifier,
293 bool bShift,
294 CPDFSDK_Annot* pScreen) {
295 Initialize(JET_SCREEN_MOUSEENTER);
296
297 m_bModifier = bModifier;
298 m_bShift = bShift;
299 m_pTargetAnnot.Reset(pScreen);
300 }
301
OnScreen_MouseExit(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)302 void CJS_EventRecorder::OnScreen_MouseExit(bool bModifier,
303 bool bShift,
304 CPDFSDK_Annot* pScreen) {
305 Initialize(JET_SCREEN_MOUSEEXIT);
306
307 m_bModifier = bModifier;
308 m_bShift = bShift;
309 m_pTargetAnnot.Reset(pScreen);
310 }
311
OnScreen_InView(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)312 void CJS_EventRecorder::OnScreen_InView(bool bModifier,
313 bool bShift,
314 CPDFSDK_Annot* pScreen) {
315 Initialize(JET_SCREEN_INVIEW);
316 m_bModifier = bModifier;
317 m_bShift = bShift;
318 m_pTargetAnnot.Reset(pScreen);
319 }
320
OnScreen_OutView(bool bModifier,bool bShift,CPDFSDK_Annot * pScreen)321 void CJS_EventRecorder::OnScreen_OutView(bool bModifier,
322 bool bShift,
323 CPDFSDK_Annot* pScreen) {
324 Initialize(JET_SCREEN_OUTVIEW);
325 m_bModifier = bModifier;
326 m_bShift = bShift;
327 m_pTargetAnnot.Reset(pScreen);
328 }
329
OnLink_MouseUp(CPDFSDK_FormFillEnvironment * pTargetFormFillEnv)330 void CJS_EventRecorder::OnLink_MouseUp(
331 CPDFSDK_FormFillEnvironment* pTargetFormFillEnv) {
332 Initialize(JET_LINK_MOUSEUP);
333 m_pTargetFormFillEnv.Reset(pTargetFormFillEnv);
334 }
335
OnBookmark_MouseUp(CPDF_Bookmark * pBookMark)336 void CJS_EventRecorder::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
337 Initialize(JET_BOOKMARK_MOUSEUP);
338 m_pTargetBookMark = pBookMark;
339 }
340
OnMenu_Exec(CPDFSDK_FormFillEnvironment * pTargetFormFillEnv,const WideString & strTargetName)341 void CJS_EventRecorder::OnMenu_Exec(
342 CPDFSDK_FormFillEnvironment* pTargetFormFillEnv,
343 const WideString& strTargetName) {
344 Initialize(JET_MENU_EXEC);
345 m_pTargetFormFillEnv.Reset(pTargetFormFillEnv);
346 m_strTargetName = strTargetName;
347 }
348
OnExternal_Exec()349 void CJS_EventRecorder::OnExternal_Exec() {
350 Initialize(JET_EXTERNAL_EXEC);
351 }
352
OnBatchExec(CPDFSDK_FormFillEnvironment * pTargetFormFillEnv)353 void CJS_EventRecorder::OnBatchExec(
354 CPDFSDK_FormFillEnvironment* pTargetFormFillEnv) {
355 Initialize(JET_BATCH_EXEC);
356 m_pTargetFormFillEnv.Reset(pTargetFormFillEnv);
357 }
358
OnConsole_Exec()359 void CJS_EventRecorder::OnConsole_Exec() {
360 Initialize(JET_CONSOLE_EXEC);
361 }
362
Initialize(JS_EVENT_T type)363 void CJS_EventRecorder::Initialize(JS_EVENT_T type) {
364 m_eEventType = type;
365 m_strTargetName.clear();
366 m_strSourceName.clear();
367 m_pWideStrChange = nullptr;
368 m_WideStrChangeDu.clear();
369 m_WideStrChangeEx.clear();
370 m_nCommitKey = -1;
371 m_bKeyDown = false;
372 m_bModifier = false;
373 m_bShift = false;
374 m_pISelEnd = nullptr;
375 m_nSelEndDu = 0;
376 m_pISelStart = nullptr;
377 m_nSelStartDu = 0;
378 m_bWillCommit = false;
379 m_pValue = nullptr;
380 m_bFieldFull = false;
381 m_pbRc = nullptr;
382 m_bRcDu = false;
383 m_pTargetBookMark = nullptr;
384 m_pTargetFormFillEnv.Reset();
385 m_pTargetAnnot.Reset();
386 m_bValid = true;
387 }
388
Destroy()389 void CJS_EventRecorder::Destroy() {
390 m_bValid = false;
391 }
392
IsUserGesture() const393 bool CJS_EventRecorder::IsUserGesture() const {
394 switch (m_eEventType) {
395 case JET_FIELD_MOUSEDOWN:
396 case JET_FIELD_MOUSEUP:
397 case JET_SCREEN_MOUSEDOWN:
398 case JET_SCREEN_MOUSEUP:
399 case JET_BOOKMARK_MOUSEUP:
400 case JET_LINK_MOUSEUP:
401 case JET_FIELD_KEYSTROKE:
402 return true;
403 default:
404 return false;
405 }
406 }
407
Change()408 WideString& CJS_EventRecorder::Change() {
409 return m_pWideStrChange ? *m_pWideStrChange : m_WideStrChangeDu;
410 }
411
Name() const412 ByteStringView CJS_EventRecorder::Name() const {
413 switch (m_eEventType) {
414 case JET_APP_INIT:
415 return "Init";
416 case JET_BATCH_EXEC:
417 return "Exec";
418 case JET_BOOKMARK_MOUSEUP:
419 return "Mouse Up";
420 case JET_CONSOLE_EXEC:
421 return "Exec";
422 case JET_DOC_DIDPRINT:
423 return "DidPrint";
424 case JET_DOC_DIDSAVE:
425 return "DidSave";
426 case JET_DOC_OPEN:
427 return "Open";
428 case JET_DOC_WILLCLOSE:
429 return "WillClose";
430 case JET_DOC_WILLPRINT:
431 return "WillPrint";
432 case JET_DOC_WILLSAVE:
433 return "WillSave";
434 case JET_EXTERNAL_EXEC:
435 return "Exec";
436 case JET_FIELD_FOCUS:
437 case JET_SCREEN_FOCUS:
438 return "Focus";
439 case JET_FIELD_BLUR:
440 case JET_SCREEN_BLUR:
441 return "Blur";
442 case JET_FIELD_MOUSEDOWN:
443 case JET_SCREEN_MOUSEDOWN:
444 return "Mouse Down";
445 case JET_FIELD_MOUSEUP:
446 case JET_SCREEN_MOUSEUP:
447 return "Mouse Up";
448 case JET_FIELD_MOUSEENTER:
449 case JET_SCREEN_MOUSEENTER:
450 return "Mouse Enter";
451 case JET_FIELD_MOUSEEXIT:
452 case JET_SCREEN_MOUSEEXIT:
453 return "Mouse Exit";
454 case JET_FIELD_CALCULATE:
455 return "Calculate";
456 case JET_FIELD_FORMAT:
457 return "Format";
458 case JET_FIELD_KEYSTROKE:
459 return "Keystroke";
460 case JET_FIELD_VALIDATE:
461 return "Validate";
462 case JET_LINK_MOUSEUP:
463 return "Mouse Up";
464 case JET_MENU_EXEC:
465 return "Exec";
466 case JET_PAGE_OPEN:
467 case JET_SCREEN_OPEN:
468 return "Open";
469 case JET_PAGE_CLOSE:
470 case JET_SCREEN_CLOSE:
471 return "Close";
472 case JET_SCREEN_INVIEW:
473 case JET_PAGE_INVIEW:
474 return "InView";
475 case JET_PAGE_OUTVIEW:
476 case JET_SCREEN_OUTVIEW:
477 return "OutView";
478 default:
479 return "";
480 }
481 }
482
Type() const483 ByteStringView CJS_EventRecorder::Type() const {
484 switch (m_eEventType) {
485 case JET_APP_INIT:
486 return "App";
487 case JET_BATCH_EXEC:
488 return "Batch";
489 case JET_BOOKMARK_MOUSEUP:
490 return "BookMark";
491 case JET_CONSOLE_EXEC:
492 return "Console";
493 case JET_DOC_DIDPRINT:
494 case JET_DOC_DIDSAVE:
495 case JET_DOC_OPEN:
496 case JET_DOC_WILLCLOSE:
497 case JET_DOC_WILLPRINT:
498 case JET_DOC_WILLSAVE:
499 return "Doc";
500 case JET_EXTERNAL_EXEC:
501 return "External";
502 case JET_FIELD_BLUR:
503 case JET_FIELD_FOCUS:
504 case JET_FIELD_MOUSEDOWN:
505 case JET_FIELD_MOUSEENTER:
506 case JET_FIELD_MOUSEEXIT:
507 case JET_FIELD_MOUSEUP:
508 case JET_FIELD_CALCULATE:
509 case JET_FIELD_FORMAT:
510 case JET_FIELD_KEYSTROKE:
511 case JET_FIELD_VALIDATE:
512 return "Field";
513 case JET_SCREEN_FOCUS:
514 case JET_SCREEN_BLUR:
515 case JET_SCREEN_OPEN:
516 case JET_SCREEN_CLOSE:
517 case JET_SCREEN_MOUSEDOWN:
518 case JET_SCREEN_MOUSEUP:
519 case JET_SCREEN_MOUSEENTER:
520 case JET_SCREEN_MOUSEEXIT:
521 case JET_SCREEN_INVIEW:
522 case JET_SCREEN_OUTVIEW:
523 return "Screen";
524 case JET_LINK_MOUSEUP:
525 return "Link";
526 case JET_MENU_EXEC:
527 return "Menu";
528 case JET_PAGE_OPEN:
529 case JET_PAGE_CLOSE:
530 case JET_PAGE_INVIEW:
531 case JET_PAGE_OUTVIEW:
532 return "Page";
533 default:
534 return "";
535 }
536 }
537
Rc()538 bool& CJS_EventRecorder::Rc() {
539 return m_pbRc ? *m_pbRc : m_bRcDu;
540 }
541
SelEnd() const542 int CJS_EventRecorder::SelEnd() const {
543 return m_pISelEnd ? *m_pISelEnd : m_nSelEndDu;
544 }
545
SelStart() const546 int CJS_EventRecorder::SelStart() const {
547 return m_pISelStart ? *m_pISelStart : m_nSelStartDu;
548 }
549
SetSelEnd(int value)550 void CJS_EventRecorder::SetSelEnd(int value) {
551 int& target = m_pISelEnd ? *m_pISelEnd : m_nSelEndDu;
552 target = value;
553 }
554
SetSelStart(int value)555 void CJS_EventRecorder::SetSelStart(int value) {
556 int& target = m_pISelStart ? *m_pISelStart : m_nSelStartDu;
557 target = value;
558 }
559