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
cjx_hostpseudomodel.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/xfa/cjx_hostpseudomodel.h"
8
9#include "core/fxcrt/check.h"
10#include "core/fxcrt/span.h"
11#include "fxjs/fxv8.h"
12#include "fxjs/js_resources.h"
13#include "fxjs/xfa/cfxjse_engine.h"
14#include "v8/include/v8-object.h"
15#include "xfa/fxfa/cxfa_ffdoc.h"
16#include "xfa/fxfa/cxfa_ffnotify.h"
17#include "xfa/fxfa/parser/cscript_hostpseudomodel.h"
18#include "xfa/fxfa/parser/cxfa_node.h"
19
20namespace {
21
22size_t FilterName(WideStringView wsExpression,
23 size_t nStart,
24 WideString& wsFilter) {
25 const size_t nLength = wsExpression.GetLength();
26 if (nStart >= nLength)
27 return nLength;
28
29 size_t nCount = 0;
30 pdfium::span<const wchar_t> pSrc = wsExpression.span();
31 {
32 // Span's lifetime must end before ReleaseBuffer() below.
33 pdfium::span<wchar_t> pBuf = wsFilter.GetBuffer(nLength - nStart);
34 while (nStart < nLength) {
35 wchar_t wCur = pSrc[nStart++];
36 if (wCur == ',')
37 break;
38
39 pBuf[nCount++] = wCur;
40 }
41 }
42 wsFilter.ReleaseBuffer(nCount);
43 wsFilter.TrimWhitespace();
44 return nStart;
45}
46
47} // namespace
48
49const CJX_MethodSpec CJX_HostPseudoModel::MethodSpecs[] = {
50 {"beep", beep_static},
51 {"documentCountInBatch", documentCountInBatch_static},
52 {"documentInBatch", documentInBatch_static},
53 {"exportData", exportData_static},
54 {"getFocus", getFocus_static},
55 {"gotoURL", gotoURL_static},
56 {"importData", importData_static},
57 {"messageBox", messageBox_static},
58 {"openList", openList_static},
59 {"pageDown", pageDown_static},
60 {"pageUp", pageUp_static},
61 {"print", print_static},
62 {"resetData", resetData_static},
63 {"response", response_static},
64 {"setFocus", setFocus_static}};
65
66CJX_HostPseudoModel::CJX_HostPseudoModel(CScript_HostPseudoModel* model)
67 : CJX_Object(model) {
68 DefineMethods(MethodSpecs);
69}
70
71CJX_HostPseudoModel::~CJX_HostPseudoModel() = default;
72
73bool CJX_HostPseudoModel::DynamicTypeIs(TypeTag eType) const {
74 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
75}
76
77void CJX_HostPseudoModel::appType(v8::Isolate* pIsolate,
78 v8::Local<v8::Value>* pValue,
79 bool bSetting,
80 XFA_Attribute eAttribute) {
81 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
82 if (!pNotify)
83 return;
84
85 if (bSetting) {
87 return;
88 }
89 *pValue = fxv8::NewStringHelper(pIsolate, "Exchange");
90}
91
92void CJX_HostPseudoModel::calculationsEnabled(v8::Isolate* pIsolate,
93 v8::Local<v8::Value>* pValue,
94 bool bSetting,
95 XFA_Attribute eAttribute) {
96 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
97 if (!pNotify)
98 return;
99
100 CXFA_FFDoc* hDoc = pNotify->GetFFDoc();
101 if (bSetting) {
103 fxv8::ReentrantToBooleanHelper(pIsolate, *pValue));
104 return;
105 }
106 *pValue = fxv8::NewBooleanHelper(pIsolate, hDoc->IsCalculationsEnabled());
107}
108
109void CJX_HostPseudoModel::currentPage(v8::Isolate* pIsolate,
110 v8::Local<v8::Value>* pValue,
111 bool bSetting,
112 XFA_Attribute eAttribute) {
113 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
114 if (!pNotify)
115 return;
116
117 CXFA_FFDoc* hDoc = pNotify->GetFFDoc();
118 if (bSetting) {
119 hDoc->SetCurrentPage(fxv8::ReentrantToInt32Helper(pIsolate, *pValue));
120 return;
121 }
122 *pValue = fxv8::NewNumberHelper(pIsolate, hDoc->GetCurrentPage());
123}
124
125void CJX_HostPseudoModel::language(v8::Isolate* pIsolate,
126 v8::Local<v8::Value>* pValue,
127 bool bSetting,
128 XFA_Attribute eAttribute) {
129 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
130 if (!pNotify)
131 return;
132
133 if (bSetting) {
134 ThrowException(pIsolate,
135 WideString::FromASCII("Unable to set language value."));
136 return;
137 }
139 *pValue = fxv8::NewStringHelper(pIsolate, lang.AsStringView());
140}
141
142void CJX_HostPseudoModel::numPages(v8::Isolate* pIsolate,
143 v8::Local<v8::Value>* pValue,
144 bool bSetting,
145 XFA_Attribute eAttribute) {
146 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
147 if (!pNotify)
148 return;
149
150 CXFA_FFDoc* hDoc = pNotify->GetFFDoc();
151 if (bSetting) {
152 ThrowException(pIsolate,
153 WideString::FromASCII("Unable to set numPages value."));
154 return;
155 }
156 *pValue = fxv8::NewNumberHelper(pIsolate, hDoc->CountPages());
157}
158
159void CJX_HostPseudoModel::platform(v8::Isolate* pIsolate,
160 v8::Local<v8::Value>* pValue,
161 bool bSetting,
162 XFA_Attribute eAttribute) {
163 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
164 if (!pNotify)
165 return;
166
167 if (bSetting) {
168 ThrowException(pIsolate,
169 WideString::FromASCII("Unable to set platform value."));
170 return;
171 }
173 *pValue = fxv8::NewStringHelper(pIsolate, plat.AsStringView());
174}
175
176void CJX_HostPseudoModel::title(v8::Isolate* pIsolate,
177 v8::Local<v8::Value>* pValue,
178 bool bSetting,
179 XFA_Attribute eAttribute) {
180 if (!GetDocument()->GetScriptContext()->IsRunAtClient())
181 return;
182
183 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
184 if (!pNotify)
185 return;
186
187 CXFA_FFDoc* hDoc = pNotify->GetFFDoc();
188 if (bSetting) {
189 hDoc->SetTitle(fxv8::ReentrantToWideStringHelper(pIsolate, *pValue));
190 return;
191 }
192
193 ByteString bsTitle = hDoc->GetTitle().ToUTF8();
194 *pValue = fxv8::NewStringHelper(pIsolate, bsTitle.AsStringView());
195}
196
197void CJX_HostPseudoModel::validationsEnabled(v8::Isolate* pIsolate,
198 v8::Local<v8::Value>* pValue,
199 bool bSetting,
200 XFA_Attribute eAttribute) {
201 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
202 if (!pNotify)
203 return;
204
205 CXFA_FFDoc* hDoc = pNotify->GetFFDoc();
206 if (bSetting) {
208 fxv8::ReentrantToBooleanHelper(pIsolate, *pValue));
209 return;
210 }
211
212 *pValue = fxv8::NewBooleanHelper(pIsolate, hDoc->IsValidationsEnabled());
213}
214
215void CJX_HostPseudoModel::variation(v8::Isolate* pIsolate,
216 v8::Local<v8::Value>* pValue,
217 bool bSetting,
218 XFA_Attribute eAttribute) {
219 if (!GetDocument()->GetScriptContext()->IsRunAtClient())
220 return;
221
222 if (bSetting) {
223 ThrowException(pIsolate,
224 WideString::FromASCII("Unable to set variation value."));
225 return;
226 }
227 *pValue = fxv8::NewStringHelper(pIsolate, "Full");
228}
229
230void CJX_HostPseudoModel::version(v8::Isolate* pIsolate,
231 v8::Local<v8::Value>* pValue,
232 bool bSetting,
233 XFA_Attribute eAttribute) {
234 if (bSetting) {
235 ThrowException(pIsolate,
236 WideString::FromASCII("Unable to set version value."));
237 return;
238 }
239 *pValue = fxv8::NewStringHelper(pIsolate, "11");
240}
241
242void CJX_HostPseudoModel::name(v8::Isolate* pIsolate,
243 v8::Local<v8::Value>* pValue,
244 bool bSetting,
245 XFA_Attribute eAttribute) {
246 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
247 if (!pNotify)
248 return;
249
250 if (bSetting) {
252 return;
253 }
255 *pValue = fxv8::NewStringHelper(pIsolate, bsName.AsStringView());
256}
257
258CJS_Result CJX_HostPseudoModel::gotoURL(
259 CFXJSE_Engine* runtime,
260 pdfium::span<v8::Local<v8::Value>> params) {
261 if (!runtime->IsRunAtClient()) {
263 }
264
265 if (params.size() != 1)
267
268 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
269 if (!pNotify)
271
272 pNotify->GetFFDoc()->GotoURL(runtime->ToWideString(params[0]));
274}
275
276CJS_Result CJX_HostPseudoModel::openList(
277 CFXJSE_Engine* runtime,
278 pdfium::span<v8::Local<v8::Value>> params) {
279 if (!runtime->IsRunAtClient()) {
281 }
282
283 if (params.size() != 1)
285
286 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
287 if (!pNotify)
289
290 CXFA_Node* pNode = nullptr;
291 if (params[0]->IsObject()) {
292 pNode = ToNode(runtime->ToXFAObject(params[0]));
293 } else if (params[0]->IsString()) {
294 CXFA_Object* pObject = runtime->GetThisObject();
295 if (!pObject)
297
301 std::optional<CFXJSE_Engine::ResolveResult> maybeResult =
302 runtime->ResolveObjects(
303 pObject, runtime->ToWideString(params[0]).AsStringView(), kFlags);
304 if (!maybeResult.has_value() ||
305 !maybeResult.value().objects.front()->IsNode()) {
307 }
308 pNode = maybeResult.value().objects.front()->AsNode();
309 }
310 if (pNode)
311 pNotify->OpenDropDownList(pNode);
312
314}
315
316CJS_Result CJX_HostPseudoModel::response(
317 CFXJSE_Engine* runtime,
318 pdfium::span<v8::Local<v8::Value>> params) {
319 if (params.empty() || params.size() > 4)
321
322 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
323 if (!pNotify)
325
326 WideString question;
327 if (params.size() >= 1)
328 question = runtime->ToWideString(params[0]);
329
330 WideString title;
331 if (params.size() >= 2)
332 title = runtime->ToWideString(params[1]);
333
334 WideString defaultAnswer;
335 if (params.size() >= 3)
336 defaultAnswer = runtime->ToWideString(params[2]);
337
338 bool mark = false;
339 if (params.size() >= 4)
340 mark = runtime->ToInt32(params[3]) != 0;
341
342 WideString answer =
343 pNotify->GetAppProvider()->Response(question, title, defaultAnswer, mark);
345 runtime->NewString(answer.ToUTF8().AsStringView()));
346}
347
348CJS_Result CJX_HostPseudoModel::documentInBatch(
349 CFXJSE_Engine* runtime,
350 pdfium::span<v8::Local<v8::Value>> params) {
351 return CJS_Result::Success(runtime->NewNumber(0));
352}
353
354CJS_Result CJX_HostPseudoModel::resetData(
355 CFXJSE_Engine* runtime,
356 pdfium::span<v8::Local<v8::Value>> params) {
357 if (params.size() > 1)
359
360 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
361 if (!pNotify)
363
364 WideString expression;
365 if (params.size() >= 1)
366 expression = runtime->ToWideString(params[0]);
367
368 if (expression.IsEmpty()) {
369 pNotify->ResetData(nullptr);
371 }
372
373 WideString wsName;
374 CXFA_Node* pNode = nullptr;
375 size_t nStart = 0;
376 const size_t nExpLength = expression.GetLength();
377 while (nStart < nExpLength) {
378 nStart = FilterName(expression.AsStringView(), nStart, wsName);
379 CXFA_Object* pObject = runtime->GetThisObject();
380 if (!pObject)
382
386 std::optional<CFXJSE_Engine::ResolveResult> maybeResult =
387 runtime->ResolveObjects(pObject, wsName.AsStringView(), kFlags);
388 if (!maybeResult.has_value() ||
389 !maybeResult.value().objects.front()->IsNode())
390 continue;
391
392 pNode = maybeResult.value().objects.front()->AsNode();
393 pNotify->ResetData(pNode->IsWidgetReady() ? pNode : nullptr);
394 }
395 if (!pNode)
396 pNotify->ResetData(nullptr);
397
399}
400
401CJS_Result CJX_HostPseudoModel::beep(
402 CFXJSE_Engine* runtime,
403 pdfium::span<v8::Local<v8::Value>> params) {
404 if (!runtime->IsRunAtClient()) {
406 }
407
408 if (params.size() > 1)
410
411 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
412 if (!pNotify)
414
415 uint32_t dwType = 4;
416 if (params.size() >= 1)
417 dwType = runtime->ToInt32(params[0]);
418
419 pNotify->GetAppProvider()->Beep(dwType);
421}
422
423CJS_Result CJX_HostPseudoModel::setFocus(
424 CFXJSE_Engine* runtime,
425 pdfium::span<v8::Local<v8::Value>> params) {
426 if (!runtime->IsRunAtClient()) {
428 }
429
430 if (params.size() != 1)
432
433 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
434 if (!pNotify)
436
437 CXFA_Node* pNode = nullptr;
438 if (params.size() >= 1) {
439 if (params[0]->IsObject()) {
440 pNode = ToNode(runtime->ToXFAObject(params[0]));
441 } else if (params[0]->IsString()) {
442 CXFA_Object* pObject = runtime->GetThisObject();
443 if (!pObject)
445
449 std::optional<CFXJSE_Engine::ResolveResult> maybeResult =
450 runtime->ResolveObjects(
451 pObject, runtime->ToWideString(params[0]).AsStringView(), kFlags);
452 if (!maybeResult.has_value() ||
453 !maybeResult.value().objects.front()->IsNode()) {
455 }
456 pNode = maybeResult.value().objects.front()->AsNode();
457 }
458 }
459 pNotify->SetFocusWidgetNode(pNode);
461}
462
463CJS_Result CJX_HostPseudoModel::getFocus(
464 CFXJSE_Engine* runtime,
465 pdfium::span<v8::Local<v8::Value>> params) {
466 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
467 if (!pNotify)
469
470 CXFA_Node* pNode = pNotify->GetFocusWidgetNode();
471 if (!pNode)
473
474 return CJS_Result::Success(runtime->GetOrCreateJSBindingFromMap(pNode));
475}
476
477CJS_Result CJX_HostPseudoModel::messageBox(
478 CFXJSE_Engine* runtime,
479 pdfium::span<v8::Local<v8::Value>> params) {
480 if (!runtime->IsRunAtClient()) {
482 }
483
484 if (params.empty() || params.size() > 4)
486
487 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
488 if (!pNotify)
490
491 WideString message;
492 if (params.size() >= 1)
493 message = runtime->ToWideString(params[0]);
494
495 WideString title;
496 if (params.size() >= 2)
497 title = runtime->ToWideString(params[1]);
498
499 uint32_t messageType = static_cast<uint32_t>(AlertIcon::kDefault);
500 if (params.size() >= 3) {
501 messageType = runtime->ToInt32(params[2]);
502 if (messageType > static_cast<uint32_t>(AlertIcon::kStatus))
503 messageType = static_cast<uint32_t>(AlertIcon::kDefault);
504 }
505
506 uint32_t buttonType = static_cast<uint32_t>(AlertButton::kDefault);
507 if (params.size() >= 4) {
508 buttonType = runtime->ToInt32(params[3]);
509 if (buttonType > static_cast<uint32_t>(AlertButton::kYesNoCancel))
510 buttonType = static_cast<uint32_t>(AlertButton::kDefault);
511 }
512
513 int32_t iValue = pNotify->GetAppProvider()->MsgBox(message, title,
514 messageType, buttonType);
515 return CJS_Result::Success(runtime->NewNumber(iValue));
516}
517
518CJS_Result CJX_HostPseudoModel::documentCountInBatch(
519 CFXJSE_Engine* runtime,
520 pdfium::span<v8::Local<v8::Value>> params) {
521 return CJS_Result::Success(runtime->NewNumber(0));
522}
523
524CJS_Result CJX_HostPseudoModel::print(
525 CFXJSE_Engine* runtime,
526 pdfium::span<v8::Local<v8::Value>> params) {
527 if (!runtime->IsRunAtClient()) {
529 }
530
531 if (params.size() != 8)
533
534 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
535 if (!pNotify)
537
538 Mask<XFA_PrintOpt> dwOptions;
539 if (runtime->ToBoolean(params[0]))
540 dwOptions |= XFA_PrintOpt::kShowDialog;
541 if (runtime->ToBoolean(params[3]))
542 dwOptions |= XFA_PrintOpt::kCanCancel;
543 if (runtime->ToBoolean(params[4]))
544 dwOptions |= XFA_PrintOpt::kShrinkPage;
545 if (runtime->ToBoolean(params[5]))
546 dwOptions |= XFA_PrintOpt::kAsImage;
547 if (runtime->ToBoolean(params[6]))
548 dwOptions |= XFA_PrintOpt::kReverseOrder;
549 if (runtime->ToBoolean(params[7]))
550 dwOptions |= XFA_PrintOpt::kPrintAnnot;
551
552 int32_t nStartPage = runtime->ToInt32(params[1]);
553 int32_t nEndPage = runtime->ToInt32(params[2]);
554 pNotify->GetFFDoc()->Print(nStartPage, nEndPage, dwOptions);
556}
557
558CJS_Result CJX_HostPseudoModel::importData(
559 CFXJSE_Engine* runtime,
560 pdfium::span<v8::Local<v8::Value>> params) {
561 if (params.empty() || params.size() > 1)
563
565}
566
567CJS_Result CJX_HostPseudoModel::exportData(
568 CFXJSE_Engine* runtime,
569 pdfium::span<v8::Local<v8::Value>> params) {
570 if (params.empty() || params.size() > 2)
572
573 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
574 if (!pNotify)
576
577 WideString filePath;
578 if (params.size() >= 1)
579 filePath = runtime->ToWideString(params[0]);
580
581 bool XDP = true;
582 if (params.size() >= 2)
583 XDP = runtime->ToBoolean(params[1]);
584
585 pNotify->GetFFDoc()->ExportData(filePath, XDP);
587}
588
589CJS_Result CJX_HostPseudoModel::pageUp(
590 CFXJSE_Engine* runtime,
591 pdfium::span<v8::Local<v8::Value>> params) {
592 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
593 if (!pNotify)
595
596 CXFA_FFDoc* hDoc = pNotify->GetFFDoc();
597 int32_t nCurPage = hDoc->GetCurrentPage();
598 if (nCurPage <= 1)
600
601 hDoc->SetCurrentPage(nCurPage - 1);
603}
604
605CJS_Result CJX_HostPseudoModel::pageDown(
606 CFXJSE_Engine* runtime,
607 pdfium::span<v8::Local<v8::Value>> params) {
608 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
609 if (!pNotify)
611
612 CXFA_FFDoc* hDoc = pNotify->GetFFDoc();
613 int32_t nCurPage = hDoc->GetCurrentPage();
614 int32_t nPageCount = hDoc->CountPages();
615 if (!nPageCount || nCurPage == nPageCount)
617
618 int32_t nNewPage = 0;
619 if (nCurPage >= nPageCount)
620 nNewPage = nPageCount - 1;
621 else
622 nNewPage = nCurPage + 1;
623
624 hDoc->SetCurrentPage(nNewPage);
626}
fxcrt::ByteString ByteString
Definition bytestring.h:180
XFA_ResolveFlag
CXFA_Object * GetThisObject() const
friend class EventParamScope
static CJS_Result Success()
Definition cjs_result.h:28
static CJS_Result Failure(JSMessage id)
Definition cjs_result.h:35
~CJX_HostPseudoModel() override
bool DynamicTypeIs(TypeTag eType) const override
void ThrowInvalidPropertyException(v8::Isolate *pIsolate) const
CXFA_Document * GetDocument() const
virtual bool DynamicTypeIs(TypeTag eType) const
void ThrowException(v8::Isolate *pIsolate, const WideString &str) const
virtual WideString GetAppName()=0
virtual WideString GetPlatform()=0
virtual void Beep(uint32_t dwType)=0
virtual int32_t MsgBox(const WideString &wsMessage, const WideString &wsTitle, uint32_t dwIconType, uint32_t dwButtonType)=0
virtual WideString Response(const WideString &wsQuestion, const WideString &wsTitle, const WideString &wsDefaultAnswer, bool bMask)=0
virtual WideString GetLanguage()=0
void Print(int32_t nStartPage, int32_t nEndPage, Mask< XFA_PrintOpt > dwOptions)
void SetCurrentPage(int32_t iCurPage)
void SetCalculationsEnabled(bool bEnabled)
bool IsCalculationsEnabled() const
void SetValidationsEnabled(bool bEnabled)
WideString GetTitle() const
int32_t GetCurrentPage() const
bool IsValidationsEnabled() const
void ExportData(const WideString &wsFilePath, bool bXDP)
void SetTitle(const WideString &wsTitle)
int32_t CountPages() const
void ResetData(CXFA_Node *pNode)
void SetFocusWidgetNode(CXFA_Node *pNode)
CXFA_FFDoc * GetFFDoc() const
CXFA_FFApp::CallbackIface * GetAppProvider()
CXFA_Node * GetFocusWidgetNode()
void OpenDropDownList(CXFA_Node *pNode)
bool IsWidgetReady() const
Definition cxfa_node.h:328
ByteString ToUTF8() const
static WideString FromASCII(ByteStringView str)
AlertIcon
Definition fxfa.h:20
@ kStatus
Definition fxfa.h:25
@ kDefault
Definition fxfa.h:21
XFA_PrintOpt
Definition fxfa.h:45
@ kReverseOrder
Definition fxfa.h:50
@ kShrinkPage
Definition fxfa.h:48
@ kShowDialog
Definition fxfa.h:46
@ kCanCancel
Definition fxfa.h:47
@ kPrintAnnot
Definition fxfa.h:51
@ kAsImage
Definition fxfa.h:49
AlertButton
Definition fxfa.h:11
@ kDefault
Definition fxfa.h:12
@ kYesNoCancel
Definition fxfa.h:16
XFA_Attribute
Definition fxfa_basic.h:67
JSMessage
fxcrt::WideStringView WideStringView
fxcrt::WideString WideString
Definition widestring.h:207