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_form.h"
8
9 #include "core/fxcrt/span.h"
10 #include "fxjs/fxv8.h"
11 #include "fxjs/js_resources.h"
12 #include "fxjs/xfa/cfxjse_engine.h"
13 #include "v8/include/cppgc/allocation.h"
14 #include "v8/include/v8-object.h"
15 #include "v8/include/v8-primitive.h"
16 #include "xfa/fxfa/cxfa_eventparam.h"
17 #include "xfa/fxfa/cxfa_ffnotify.h"
18 #include "xfa/fxfa/parser/cxfa_arraynodelist.h"
19 #include "xfa/fxfa/parser/cxfa_document.h"
20 #include "xfa/fxfa/parser/cxfa_form.h"
21
22 const CJX_MethodSpec CJX_Form::MethodSpecs[] = {
23 {"execCalculate", execCalculate_static},
24 {"execInitialize", execInitialize_static},
25 {"execValidate", execValidate_static},
26 {"formNodes", formNodes_static},
27 {"recalculate", recalculate_static},
28 {"remerge", remerge_static}};
29
CJX_Form(CXFA_Form * form)30 CJX_Form::CJX_Form(CXFA_Form* form) : CJX_Model(form) {
31 DefineMethods(MethodSpecs);
32 }
33
34 CJX_Form::~CJX_Form() = default;
35
DynamicTypeIs(TypeTag eType) const36 bool CJX_Form::DynamicTypeIs(TypeTag eType) const {
37 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
38 }
39
formNodes(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)40 CJS_Result CJX_Form::formNodes(CFXJSE_Engine* runtime,
41 pdfium::span<v8::Local<v8::Value>> params) {
42 if (params.size() != 1)
43 return CJS_Result::Failure(JSMessage::kParamError);
44
45 CXFA_Node* pDataNode = ToNode(runtime->ToXFAObject(params[0]));
46 if (!pDataNode)
47 return CJS_Result::Failure(JSMessage::kValueError);
48
49 CXFA_Document* pDoc = GetDocument();
50 auto* pFormNodes = cppgc::MakeGarbageCollected<CXFA_ArrayNodeList>(
51 pDoc->GetHeap()->GetAllocationHandle(), pDoc);
52 pDoc->GetNodeOwner()->PersistList(pFormNodes);
53
54 v8::Local<v8::Value> value = runtime->GetOrCreateJSBindingFromMap(pFormNodes);
55 return CJS_Result::Success(value);
56 }
57
remerge(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)58 CJS_Result CJX_Form::remerge(CFXJSE_Engine* runtime,
59 pdfium::span<v8::Local<v8::Value>> params) {
60 if (!params.empty())
61 return CJS_Result::Failure(JSMessage::kParamError);
62
63 GetDocument()->DoDataRemerge();
64 return CJS_Result::Success();
65 }
66
execInitialize(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)67 CJS_Result CJX_Form::execInitialize(CFXJSE_Engine* runtime,
68 pdfium::span<v8::Local<v8::Value>> params) {
69 if (!params.empty())
70 return CJS_Result::Failure(JSMessage::kParamError);
71
72 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
73 if (pNotify)
74 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Initialize, false,
75 true);
76 return CJS_Result::Success();
77 }
78
recalculate(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)79 CJS_Result CJX_Form::recalculate(CFXJSE_Engine* runtime,
80 pdfium::span<v8::Local<v8::Value>> params) {
81 CXFA_EventParam* pEventParam = runtime->GetEventParam();
82 if (pEventParam && (pEventParam->m_eType == XFA_EVENT_Calculate ||
83 pEventParam->m_eType == XFA_EVENT_InitCalculate)) {
84 return CJS_Result::Success();
85 }
86 if (params.size() != 1)
87 return CJS_Result::Failure(JSMessage::kParamError);
88
89 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
90 if (!pNotify || runtime->ToInt32(params[0]) != 0)
91 return CJS_Result::Success();
92
93 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate, false, true);
94 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Validate, false, true);
95 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Ready, true, true);
96 return CJS_Result::Success();
97 }
98
execCalculate(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)99 CJS_Result CJX_Form::execCalculate(CFXJSE_Engine* runtime,
100 pdfium::span<v8::Local<v8::Value>> params) {
101 if (!params.empty())
102 return CJS_Result::Failure(JSMessage::kParamError);
103
104 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
105 if (pNotify)
106 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate, false,
107 true);
108 return CJS_Result::Success();
109 }
110
execValidate(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)111 CJS_Result CJX_Form::execValidate(CFXJSE_Engine* runtime,
112 pdfium::span<v8::Local<v8::Value>> params) {
113 if (params.size() != 0)
114 return CJS_Result::Failure(JSMessage::kParamError);
115
116 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
117 if (!pNotify)
118 return CJS_Result::Success(runtime->NewBoolean(false));
119
120 XFA_EventError iRet = pNotify->ExecEventByDeepFirst(
121 GetXFANode(), XFA_EVENT_Validate, false, true);
122 return CJS_Result::Success(
123 runtime->NewBoolean(iRet != XFA_EventError::kError));
124 }
125
checksumS(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)126 void CJX_Form::checksumS(v8::Isolate* pIsolate,
127 v8::Local<v8::Value>* pValue,
128 bool bSetting,
129 XFA_Attribute eAttribute) {
130 if (bSetting) {
131 SetAttributeByEnum(XFA_Attribute::Checksum,
132 fxv8::ReentrantToWideStringHelper(pIsolate, *pValue),
133 false);
134 return;
135 }
136
137 std::optional<WideString> checksum =
138 TryAttribute(XFA_Attribute::Checksum, false);
139 *pValue = fxv8::NewStringHelper(
140 pIsolate,
141 checksum.has_value() ? checksum.value().ToUTF8().AsStringView() : "");
142 }
143