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_subform.h"
8
9 #include <vector>
10
11 #include "fxjs/cfx_v8.h"
12 #include "fxjs/js_resources.h"
13 #include "fxjs/xfa/cfxjse_engine.h"
14 #include "fxjs/xfa/cfxjse_value.h"
15 #include "xfa/fxfa/cxfa_eventparam.h"
16 #include "xfa/fxfa/cxfa_ffnotify.h"
17 #include "xfa/fxfa/fxfa.h"
18 #include "xfa/fxfa/parser/cxfa_delta.h"
19 #include "xfa/fxfa/parser/cxfa_document.h"
20
21 const CJX_MethodSpec CJX_Subform::MethodSpecs[] = {
22 {"execCalculate", execCalculate_static},
23 {"execEvent", execEvent_static},
24 {"execInitialize", execInitialize_static},
25 {"execValidate", execValidate_static}};
26
CJX_Subform(CXFA_Node * node)27 CJX_Subform::CJX_Subform(CXFA_Node* node) : CJX_Container(node) {
28 DefineMethods(MethodSpecs);
29 }
30
31 CJX_Subform::~CJX_Subform() = default;
32
DynamicTypeIs(TypeTag eType) const33 bool CJX_Subform::DynamicTypeIs(TypeTag eType) const {
34 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
35 }
36
execEvent(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)37 CJS_Result CJX_Subform::execEvent(
38 CFX_V8* runtime,
39 const std::vector<v8::Local<v8::Value>>& params) {
40 if (params.size() != 1)
41 return CJS_Result::Failure(JSMessage::kParamError);
42
43 execSingleEventByName(runtime->ToWideString(params[0]).AsStringView(),
44 XFA_Element::Subform);
45 return CJS_Result::Success();
46 }
47
execInitialize(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)48 CJS_Result CJX_Subform::execInitialize(
49 CFX_V8* runtime,
50 const std::vector<v8::Local<v8::Value>>& params) {
51 if (!params.empty())
52 return CJS_Result::Failure(JSMessage::kParamError);
53
54 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
55 if (pNotify)
56 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Initialize, false,
57 true);
58 return CJS_Result::Success();
59 }
60
execCalculate(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)61 CJS_Result CJX_Subform::execCalculate(
62 CFX_V8* runtime,
63 const std::vector<v8::Local<v8::Value>>& params) {
64 if (!params.empty())
65 return CJS_Result::Failure(JSMessage::kParamError);
66
67 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
68 if (pNotify)
69 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate, false,
70 true);
71 return CJS_Result::Success();
72 }
73
execValidate(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)74 CJS_Result CJX_Subform::execValidate(
75 CFX_V8* runtime,
76 const std::vector<v8::Local<v8::Value>>& params) {
77 if (!params.empty())
78 return CJS_Result::Failure(JSMessage::kParamError);
79
80 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
81 if (!pNotify)
82 return CJS_Result::Success(runtime->NewBoolean(false));
83
84 XFA_EventError iRet = pNotify->ExecEventByDeepFirst(
85 GetXFANode(), XFA_EVENT_Validate, false, true);
86 return CJS_Result::Success(
87 runtime->NewBoolean(iRet != XFA_EventError::kError));
88 }
89
locale(v8::Isolate * pIsolate,CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)90 void CJX_Subform::locale(v8::Isolate* pIsolate,
91 CFXJSE_Value* pValue,
92 bool bSetting,
93 XFA_Attribute eAttribute) {
94 if (bSetting) {
95 SetCDataImpl(XFA_Attribute::Locale, pValue->ToWideString(pIsolate), true,
96 true);
97 return;
98 }
99
100 WideString wsLocaleName = GetXFANode()->GetLocaleName().value_or(L"");
101 pValue->SetString(pIsolate, wsLocaleName.ToUTF8().AsStringView());
102 }
103
instanceManager(v8::Isolate * pIsolate,CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)104 void CJX_Subform::instanceManager(v8::Isolate* pIsolate,
105 CFXJSE_Value* pValue,
106 bool bSetting,
107 XFA_Attribute eAttribute) {
108 if (bSetting) {
109 ThrowInvalidPropertyException();
110 return;
111 }
112
113 WideString wsName = GetCData(XFA_Attribute::Name);
114 CXFA_Node* pInstanceMgr = nullptr;
115 for (CXFA_Node* pNode = GetXFANode()->GetPrevSibling(); pNode;
116 pNode = pNode->GetPrevSibling()) {
117 if (pNode->GetElementType() == XFA_Element::InstanceManager) {
118 WideString wsInstMgrName =
119 pNode->JSObject()->GetCData(XFA_Attribute::Name);
120 if (wsInstMgrName.GetLength() >= 1 && wsInstMgrName[0] == '_' &&
121 wsInstMgrName.Last(wsInstMgrName.GetLength() - 1) == wsName) {
122 pInstanceMgr = pNode;
123 }
124 break;
125 }
126 }
127 if (!pInstanceMgr) {
128 pValue->SetNull(pIsolate);
129 return;
130 }
131
132 pValue->ForceSetValue(
133 pIsolate, GetDocument()->GetScriptContext()->GetOrCreateJSBindingFromMap(
134 pInstanceMgr));
135 }
136