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