• 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 #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 =
86       GetDocument()->GetScriptContext()->GetEventParam();
87   if (pEventParam && (pEventParam->m_eType == XFA_EVENT_Calculate ||
88                       pEventParam->m_eType == XFA_EVENT_InitCalculate)) {
89     return CJS_Result::Success();
90   }
91   if (params.size() != 1)
92     return CJS_Result::Failure(JSMessage::kParamError);
93 
94   CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
95   if (!pNotify || runtime->ToInt32(params[0]) != 0)
96     return CJS_Result::Success();
97 
98   pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate, false, true);
99   pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Validate, false, true);
100   pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Ready, true, true);
101   return CJS_Result::Success();
102 }
103 
execCalculate(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)104 CJS_Result CJX_Form::execCalculate(
105     CFXJSE_Engine* runtime,
106     const std::vector<v8::Local<v8::Value>>& params) {
107   if (!params.empty())
108     return CJS_Result::Failure(JSMessage::kParamError);
109 
110   CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
111   if (pNotify)
112     pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate, false,
113                                   true);
114   return CJS_Result::Success();
115 }
116 
execValidate(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)117 CJS_Result CJX_Form::execValidate(
118     CFXJSE_Engine* runtime,
119     const std::vector<v8::Local<v8::Value>>& params) {
120   if (params.size() != 0)
121     return CJS_Result::Failure(JSMessage::kParamError);
122 
123   CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
124   if (!pNotify)
125     return CJS_Result::Success(runtime->NewBoolean(false));
126 
127   XFA_EventError iRet = pNotify->ExecEventByDeepFirst(
128       GetXFANode(), XFA_EVENT_Validate, false, true);
129   return CJS_Result::Success(
130       runtime->NewBoolean(iRet != XFA_EventError::kError));
131 }
132 
checksumS(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)133 void CJX_Form::checksumS(v8::Isolate* pIsolate,
134                          v8::Local<v8::Value>* pValue,
135                          bool bSetting,
136                          XFA_Attribute eAttribute) {
137   if (bSetting) {
138     SetAttributeByEnum(XFA_Attribute::Checksum,
139                        fxv8::ReentrantToWideStringHelper(pIsolate, *pValue),
140                        false);
141     return;
142   }
143 
144   absl::optional<WideString> checksum =
145       TryAttribute(XFA_Attribute::Checksum, false);
146   *pValue = fxv8::NewStringHelper(
147       pIsolate,
148       checksum.has_value() ? checksum.value().ToUTF8().AsStringView() : "");
149 }
150