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