Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cjs_event_context.cpp
Go to the documentation of this file.
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#include "fxjs/cjs_event_context.h"
8
9#include "core/fpdfdoc/cpdf_formfield.h"
10#include "core/fxcrt/autorestorer.h"
11#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
12#include "fxjs/cjs_field.h"
13#include "fxjs/cjs_runtime.h"
14#include "fxjs/js_define.h"
15#include "fxjs/js_resources.h"
16#include "third_party/base/check.h"
17#include "v8/include/v8-context.h"
18#include "v8/include/v8-isolate.h"
19
20CJS_EventContext::CJS_EventContext(CJS_Runtime* pRuntime)
22
23CJS_EventContext::~CJS_EventContext() = default;
24
26 const WideString& script) {
27 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
28 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
29 v8::Local<v8::Context> context = m_pRuntime->GetV8Context();
30 v8::Context::Scope context_scope(context);
31
32 if (m_bBusy) {
35 }
36
37 AutoRestorer<bool> restorer(&m_bBusy);
38 m_bBusy = true;
39
40 DCHECK(IsValid());
41 CJS_Runtime::FieldEvent event(TargetName(), EventKind());
42 if (!m_pRuntime->AddEventToSet(event)) {
45 }
46
47 absl::optional<IJS_Runtime::JS_Error> err;
48 if (script.GetLength() > 0)
49 err = m_pRuntime->ExecuteScript(script);
50
51 m_pRuntime->RemoveEventFromSet(event);
52 Destroy();
53 return err;
54}
55
56CJS_Field* CJS_EventContext::SourceField() {
57 v8::Local<v8::Object> pDocObj = m_pRuntime->NewFXJSBoundObject(
58 CJS_Document::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
59 if (pDocObj.IsEmpty())
60 return nullptr;
61
62 v8::Local<v8::Object> pFieldObj = m_pRuntime->NewFXJSBoundObject(
63 CJS_Field::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
64 if (pFieldObj.IsEmpty())
65 return nullptr;
66
67 auto* pFormFillEnv = GetFormFillEnv();
68 auto* pJSDocument = static_cast<CJS_Document*>(
69 CFXJS_Engine::GetObjectPrivate(m_pRuntime->GetIsolate(), pDocObj));
70 pJSDocument->SetFormFillEnv(pFormFillEnv);
71
72 auto* pJSField = static_cast<CJS_Field*>(
73 CFXJS_Engine::GetObjectPrivate(m_pRuntime->GetIsolate(), pFieldObj));
74 pJSField->AttachField(pJSDocument, SourceName());
75 return pJSField;
76}
77
78CJS_Field* CJS_EventContext::TargetField() {
79 v8::Local<v8::Object> pDocObj = m_pRuntime->NewFXJSBoundObject(
80 CJS_Document::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
81 if (pDocObj.IsEmpty())
82 return nullptr;
83
84 v8::Local<v8::Object> pFieldObj = m_pRuntime->NewFXJSBoundObject(
85 CJS_Field::GetObjDefnID(), FXJSOBJTYPE_DYNAMIC);
86 if (pFieldObj.IsEmpty())
87 return nullptr;
88
89 auto* pFormFillEnv = GetFormFillEnv();
90 auto* pJSDocument = static_cast<CJS_Document*>(
91 CFXJS_Engine::GetObjectPrivate(m_pRuntime->GetIsolate(), pDocObj));
92 pJSDocument->SetFormFillEnv(pFormFillEnv);
93
94 auto* pJSField = static_cast<CJS_Field*>(
95 CFXJS_Engine::GetObjectPrivate(m_pRuntime->GetIsolate(), pFieldObj));
96 pJSField->AttachField(pJSDocument, TargetName());
97 return pJSField;
98}
99
100void CJS_EventContext::OnDoc_Open(const WideString& strTargetName) {
101 Initialize(Kind::kDocOpen);
102 m_strTargetName = strTargetName;
103}
104
105void CJS_EventContext::OnDoc_WillPrint() {
106 Initialize(Kind::kDocWillPrint);
107}
108
109void CJS_EventContext::OnDoc_DidPrint() {
110 Initialize(Kind::kDocDidPrint);
111}
112
113void CJS_EventContext::OnDoc_WillSave() {
114 Initialize(Kind::kDocWillSave);
115}
116
117void CJS_EventContext::OnDoc_DidSave() {
118 Initialize(Kind::kDocDidSave);
119}
120
121void CJS_EventContext::OnDoc_WillClose() {
122 Initialize(Kind::kDocWillClose);
123}
124
125void CJS_EventContext::OnPage_Open() {
126 Initialize(Kind::kPageOpen);
127}
128
129void CJS_EventContext::OnPage_Close() {
130 Initialize(Kind::kPageClose);
131}
132
133void CJS_EventContext::OnPage_InView() {
134 Initialize(Kind::kPageInView);
135}
136
137void CJS_EventContext::OnPage_OutView() {
138 Initialize(Kind::kPageOutView);
139}
140
141void CJS_EventContext::OnField_MouseEnter(bool bModifier,
142 bool bShift,
143 CPDF_FormField* pTarget) {
144 Initialize(Kind::kFieldMouseEnter);
145 m_bModifier = bModifier;
146 m_bShift = bShift;
147 m_strTargetName = pTarget->GetFullName();
148}
149
150void CJS_EventContext::OnField_MouseExit(bool bModifier,
151 bool bShift,
152 CPDF_FormField* pTarget) {
153 Initialize(Kind::kFieldMouseExit);
154 m_bModifier = bModifier;
155 m_bShift = bShift;
156 m_strTargetName = pTarget->GetFullName();
157}
158
159void CJS_EventContext::OnField_MouseDown(bool bModifier,
160 bool bShift,
161 CPDF_FormField* pTarget) {
162 Initialize(Kind::kFieldMouseDown);
163 m_bModifier = bModifier;
164 m_bShift = bShift;
165 m_strTargetName = pTarget->GetFullName();
166}
167
168void CJS_EventContext::OnField_MouseUp(bool bModifier,
169 bool bShift,
170 CPDF_FormField* pTarget) {
171 Initialize(Kind::kFieldMouseUp);
172 m_bModifier = bModifier;
173 m_bShift = bShift;
174 m_strTargetName = pTarget->GetFullName();
175}
176
177void CJS_EventContext::OnField_Focus(bool bModifier,
178 bool bShift,
179 CPDF_FormField* pTarget,
180 WideString* pValue) {
181 DCHECK(pValue);
182 Initialize(Kind::kFieldFocus);
183 m_bModifier = bModifier;
184 m_bShift = bShift;
185 m_strTargetName = pTarget->GetFullName();
186 m_pValue = pValue;
187}
188
189void CJS_EventContext::OnField_Blur(bool bModifier,
190 bool bShift,
191 CPDF_FormField* pTarget,
192 WideString* pValue) {
193 DCHECK(pValue);
194 Initialize(Kind::kFieldBlur);
195 m_bModifier = bModifier;
196 m_bShift = bShift;
197 m_strTargetName = pTarget->GetFullName();
198 m_pValue = pValue;
199}
200
201void CJS_EventContext::OnField_Keystroke(WideString* strChange,
202 const WideString& strChangeEx,
203 bool KeyDown,
204 bool bModifier,
205 int* pSelEnd,
206 int* pSelStart,
207 bool bShift,
208 CPDF_FormField* pTarget,
209 WideString* pValue,
210 bool bWillCommit,
211 bool bFieldFull,
212 bool* pbRc) {
213 DCHECK(pValue);
214 DCHECK(pbRc);
215 DCHECK(pSelStart);
216 DCHECK(pSelEnd);
217
218 Initialize(Kind::kFieldKeystroke);
219 m_nCommitKey = 0;
220 m_pWideStrChange = strChange;
221 m_WideStrChangeEx = strChangeEx;
222 m_bKeyDown = KeyDown;
223 m_bModifier = bModifier;
224 m_pISelEnd = pSelEnd;
225 m_pISelStart = pSelStart;
226 m_bShift = bShift;
227 m_strTargetName = pTarget->GetFullName();
228 m_pValue = pValue;
229 m_bWillCommit = bWillCommit;
230 m_pbRc = pbRc;
231 m_bFieldFull = bFieldFull;
232}
233
234void CJS_EventContext::OnField_Validate(WideString* strChange,
235 const WideString& strChangeEx,
236 bool bKeyDown,
237 bool bModifier,
238 bool bShift,
239 CPDF_FormField* pTarget,
240 WideString* pValue,
241 bool* pbRc) {
242 DCHECK(pValue);
243 DCHECK(pbRc);
244 Initialize(Kind::kFieldValidate);
245 m_pWideStrChange = strChange;
246 m_WideStrChangeEx = strChangeEx;
247 m_bKeyDown = bKeyDown;
248 m_bModifier = bModifier;
249 m_bShift = bShift;
250 m_strTargetName = pTarget->GetFullName();
251 m_pValue = pValue;
252 m_pbRc = pbRc;
253}
254
255void CJS_EventContext::OnField_Calculate(CPDF_FormField* pSource,
256 CPDF_FormField* pTarget,
257 WideString* pValue,
258 bool* pRc) {
259 DCHECK(pValue);
260 DCHECK(pRc);
261 Initialize(Kind::kFieldCalculate);
262 if (pSource)
263 m_strSourceName = pSource->GetFullName();
264 m_strTargetName = pTarget->GetFullName();
265 m_pValue = pValue;
266 m_pbRc = pRc;
267}
268
269void CJS_EventContext::OnField_Format(CPDF_FormField* pTarget,
270 WideString* pValue) {
271 DCHECK(pValue);
272 Initialize(Kind::kFieldFormat);
273 m_nCommitKey = 0;
274 m_strTargetName = pTarget->GetFullName();
275 m_pValue = pValue;
276 m_bWillCommit = true;
277}
278
279void CJS_EventContext::OnExternal_Exec() {
280 Initialize(Kind::kExternalExec);
281}
282
283void CJS_EventContext::Initialize(Kind kind) {
284 m_eKind = kind;
285 m_strTargetName.clear();
286 m_strSourceName.clear();
287 m_pWideStrChange = nullptr;
288 m_WideStrChangeDu.clear();
289 m_WideStrChangeEx.clear();
290 m_nCommitKey = -1;
291 m_bKeyDown = false;
292 m_bModifier = false;
293 m_bShift = false;
294 m_pISelEnd = nullptr;
295 m_nSelEndDu = 0;
296 m_pISelStart = nullptr;
297 m_nSelStartDu = 0;
298 m_bWillCommit = false;
299 m_pValue = nullptr;
300 m_bFieldFull = false;
301 m_pbRc = nullptr;
302 m_bRcDu = false;
303 m_bValid = true;
304}
305
306void CJS_EventContext::Destroy() {
307 m_bValid = false;
308}
309
310bool CJS_EventContext::IsUserGesture() const {
311 switch (m_eKind) {
315 return true;
316 default:
317 return false;
318 }
319}
320
321WideString& CJS_EventContext::Change() {
322 return m_pWideStrChange ? *m_pWideStrChange : m_WideStrChangeDu;
323}
324
325ByteStringView CJS_EventContext::Name() const {
326 switch (m_eKind) {
328 return "DidPrint";
330 return "DidSave";
331 case Kind::kDocOpen:
332 return "Open";
334 return "WillClose";
336 return "WillPrint";
338 return "WillSave";
340 return "Exec";
342 return "Focus";
343 case Kind::kFieldBlur:
344 return "Blur";
346 return "Mouse Down";
348 return "Mouse Up";
350 return "Mouse Enter";
352 return "Mouse Exit";
354 return "Calculate";
356 return "Format";
358 return "Keystroke";
360 return "Validate";
361 case Kind::kPageOpen:
362 return "Open";
363 case Kind::kPageClose:
364 return "Close";
366 return "InView";
368 return "OutView";
369 default:
370 return "";
371 }
372}
373
374ByteStringView CJS_EventContext::Type() const {
375 switch (m_eKind) {
378 case Kind::kDocOpen:
382 return "Doc";
384 return "External";
385 case Kind::kFieldBlur:
395 return "Field";
396 case Kind::kPageOpen:
397 case Kind::kPageClose:
400 return "Page";
401 default:
402 return "";
403 }
404}
405
406bool& CJS_EventContext::Rc() {
407 return m_pbRc ? *m_pbRc : m_bRcDu;
408}
409
410int CJS_EventContext::SelEnd() const {
411 return m_pISelEnd ? *m_pISelEnd : m_nSelEndDu;
412}
413
414int CJS_EventContext::SelStart() const {
415 return m_pISelStart ? *m_pISelStart : m_nSelStartDu;
416}
417
418void CJS_EventContext::SetSelEnd(int value) {
419 int& target = m_pISelEnd ? *m_pISelEnd : m_nSelEndDu;
420 target = value;
421}
422
423void CJS_EventContext::SetSelStart(int value) {
424 int& target = m_pISelStart ? *m_pISelStart : m_nSelStartDu;
425 target = value;
426}
CJS_Field * TargetField()
void OnPage_Close() override
void 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) override
void OnField_Blur(bool bModifier, bool bShift, CPDF_FormField *pTarget, WideString *Value) override
~CJS_EventContext() override
void OnPage_Open() override
void OnDoc_Open(const WideString &strTargetName) override
WideString SourceName() const
WideString TargetName() const
CJS_EventContext(CJS_Runtime *pRuntime)
void OnField_Focus(bool bModifier, bool bShift, CPDF_FormField *pTarget, WideString *Value) override
void OnField_MouseEnter(bool bModifier, bool bShift, CPDF_FormField *pTarget) override
void OnField_MouseDown(bool bModifier, bool bShift, CPDF_FormField *pTarget) override
ByteStringView Name() const
void OnExternal_Exec() override
void OnField_Validate(WideString *strChange, const WideString &strChangeEx, bool bKeyDown, bool bModifier, bool bShift, CPDF_FormField *pTarget, WideString *Value, bool *bRc) override
void OnDoc_DidSave() override
void OnField_Format(CPDF_FormField *pTarget, WideString *Value) override
void OnField_MouseExit(bool bModifier, bool bShift, CPDF_FormField *pTarget) override
void OnDoc_WillSave() override
ByteStringView Type() const
void OnDoc_WillClose() override
void SetSelEnd(int value)
void OnPage_InView() override
CJS_Field * SourceField()
void OnField_Calculate(CPDF_FormField *pSource, CPDF_FormField *pTarget, WideString *pValue, bool *pRc) override
absl::optional< IJS_Runtime::JS_Error > RunScript(const WideString &script) override
void OnDoc_WillPrint() override
void OnDoc_DidPrint() override
void OnField_MouseUp(bool bModifier, bool bShift, CPDF_FormField *pTarget) override
void OnPage_OutView() override
void SetSelStart(int value)
CPDFSDK_FormFillEnvironment * GetFormFillEnv() const
JSMessage
@ kDuplicateEventError
WideString JSGetStringFromID(JSMessage msg)
JS_Error(int line, int column, const WideString &exception)