• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The PDFium Authors
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 #ifndef FXJS_CJS_EVENT_CONTEXT_H_
8 #define FXJS_CJS_EVENT_CONTEXT_H_
9 
10 #include "core/fxcrt/fx_string.h"
11 #include "core/fxcrt/observed_ptr.h"
12 #include "core/fxcrt/unowned_ptr.h"
13 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
14 #include "fxjs/ijs_event_context.h"
15 
16 class CJS_Field;
17 class CJS_Runtime;
18 
19 class CJS_EventContext final : public IJS_EventContext {
20  public:
21   enum class Kind : uint8_t {
22     kUnknown,
23     kDocOpen,
24     kDocWillPrint,
25     kDocDidPrint,
26     kDocWillSave,
27     kDocDidSave,
28     kDocWillClose,
29     kPageOpen,
30     kPageClose,
31     kPageInView,
32     kPageOutView,
33     kFieldMouseDown,
34     kFieldMouseUp,
35     kFieldMouseEnter,
36     kFieldMouseExit,
37     kFieldFocus,
38     kFieldBlur,
39     kFieldKeystroke,
40     kFieldValidate,
41     kFieldCalculate,
42     kFieldFormat,
43     kExternalExec,
44   };
45 
46   explicit CJS_EventContext(CJS_Runtime* pRuntime);
47   ~CJS_EventContext() override;
48 
49   // IJS_EventContext
50   absl::optional<IJS_Runtime::JS_Error> RunScript(
51       const WideString& script) override;
52   void OnDoc_Open(const WideString& strTargetName) override;
53   void OnDoc_WillPrint() override;
54   void OnDoc_DidPrint() override;
55   void OnDoc_WillSave() override;
56   void OnDoc_DidSave() override;
57   void OnDoc_WillClose() override;
58   void OnPage_Open() override;
59   void OnPage_Close() override;
60   void OnPage_InView() override;
61   void OnPage_OutView() override;
62   void OnField_MouseDown(bool bModifier,
63                          bool bShift,
64                          CPDF_FormField* pTarget) override;
65   void OnField_MouseEnter(bool bModifier,
66                           bool bShift,
67                           CPDF_FormField* pTarget) override;
68   void OnField_MouseExit(bool bModifier,
69                          bool bShift,
70                          CPDF_FormField* pTarget) override;
71   void OnField_MouseUp(bool bModifier,
72                        bool bShift,
73                        CPDF_FormField* pTarget) override;
74   void OnField_Focus(bool bModifier,
75                      bool bShift,
76                      CPDF_FormField* pTarget,
77                      WideString* Value) override;
78   void OnField_Blur(bool bModifier,
79                     bool bShift,
80                     CPDF_FormField* pTarget,
81                     WideString* Value) override;
82   void OnField_Calculate(CPDF_FormField* pSource,
83                          CPDF_FormField* pTarget,
84                          WideString* pValue,
85                          bool* pRc) override;
86   void OnField_Format(CPDF_FormField* pTarget, WideString* Value) override;
87   void OnField_Keystroke(WideString* strChange,
88                          const WideString& strChangeEx,
89                          bool bKeyDown,
90                          bool bModifier,
91                          int* nSelEnd,
92                          int* nSelStart,
93                          bool bShift,
94                          CPDF_FormField* pTarget,
95                          WideString* Value,
96                          bool bWillCommit,
97                          bool bFieldFull,
98                          bool* bRc) override;
99   void OnField_Validate(WideString* strChange,
100                         const WideString& strChangeEx,
101                         bool bKeyDown,
102                         bool bModifier,
103                         bool bShift,
104                         CPDF_FormField* pTarget,
105                         WideString* Value,
106                         bool* bRc) override;
107   void OnExternal_Exec() override;
108 
GetJSRuntime()109   CJS_Runtime* GetJSRuntime() const { return m_pRuntime; }
GetFormFillEnv()110   CPDFSDK_FormFillEnvironment* GetFormFillEnv() const {
111     return m_pFormFillEnv.Get();
112   }
113   CJS_Field* SourceField();
114   CJS_Field* TargetField();
115 
EventKind()116   Kind EventKind() const { return m_eKind; }
IsValid()117   bool IsValid() const { return m_bValid; }
118   bool IsUserGesture() const;
119   WideString& Change();
ChangeEx()120   WideString ChangeEx() const { return m_WideStrChangeEx; }
SourceName()121   WideString SourceName() const { return m_strSourceName; }
TargetName()122   WideString TargetName() const { return m_strTargetName; }
CommitKey()123   int CommitKey() const { return m_nCommitKey; }
FieldFull()124   bool FieldFull() const { return m_bFieldFull; }
KeyDown()125   bool KeyDown() const { return m_bKeyDown; }
Modifier()126   bool Modifier() const { return m_bModifier; }
127   ByteStringView Name() const;
128   ByteStringView Type() const;
129   bool& Rc();
130   int SelEnd() const;
131   int SelStart() const;
132   void SetSelEnd(int value);
133   void SetSelStart(int value);
Shift()134   bool Shift() const { return m_bShift; }
HasValue()135   bool HasValue() const { return !!m_pValue; }
Value()136   WideString& Value() { return *m_pValue; }
WillCommit()137   bool WillCommit() const { return m_bWillCommit; }
138 
SetValueForTest(WideString * pStr)139   void SetValueForTest(WideString* pStr) { m_pValue = pStr; }
SetRCForTest(bool * pRC)140   void SetRCForTest(bool* pRC) { m_pbRc = pRC; }
SetStrChangeForTest(WideString * pStrChange)141   void SetStrChangeForTest(WideString* pStrChange) {
142     m_pWideStrChange = pStrChange;
143   }
ResetWillCommitForTest()144   void ResetWillCommitForTest() { m_bWillCommit = false; }
145 
146  private:
147   void Initialize(Kind kind);
148   void Destroy();
149 
150   UnownedPtr<CJS_Runtime> const m_pRuntime;
151   ObservedPtr<CPDFSDK_FormFillEnvironment> m_pFormFillEnv;
152   Kind m_eKind = Kind::kUnknown;
153   bool m_bBusy = false;
154   bool m_bValid = false;
155   UnownedPtr<WideString> m_pValue;
156   WideString m_strSourceName;
157   WideString m_strTargetName;
158   WideString m_WideStrChangeDu;
159   WideString m_WideStrChangeEx;
160   UnownedPtr<WideString> m_pWideStrChange;
161   int m_nCommitKey = -1;
162   bool m_bKeyDown = false;
163   bool m_bModifier = false;
164   bool m_bShift = false;
165   int m_nSelEndDu = 0;
166   int m_nSelStartDu = 0;
167   UnownedPtr<int> m_pISelEnd;
168   UnownedPtr<int> m_pISelStart;
169   bool m_bWillCommit = false;
170   bool m_bFieldFull = false;
171   bool m_bRcDu = false;
172   UnownedPtr<bool> m_pbRc;
173 };
174 
175 #endif  // FXJS_CJS_EVENT_CONTEXT_H_
176