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
~CJX_Subform()31 CJX_Subform::~CJX_Subform() {}
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(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)90 void CJX_Subform::locale(CFXJSE_Value* pValue,
91 bool bSetting,
92 XFA_Attribute eAttribute) {
93 if (bSetting) {
94 SetCData(XFA_Attribute::Locale, pValue->ToWideString(), true, true);
95 return;
96 }
97
98 WideString wsLocaleName = GetXFANode()->GetLocaleName().value_or(L"");
99 pValue->SetString(wsLocaleName.ToUTF8().AsStringView());
100 }
101
instanceManager(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)102 void CJX_Subform::instanceManager(CFXJSE_Value* pValue,
103 bool bSetting,
104 XFA_Attribute eAttribute) {
105 if (bSetting) {
106 ThrowInvalidPropertyException();
107 return;
108 }
109
110 WideString wsName = GetCData(XFA_Attribute::Name);
111 CXFA_Node* pInstanceMgr = nullptr;
112 for (CXFA_Node* pNode = ToNode(GetXFAObject())->GetPrevSibling(); pNode;
113 pNode = pNode->GetPrevSibling()) {
114 if (pNode->GetElementType() == XFA_Element::InstanceManager) {
115 WideString wsInstMgrName =
116 pNode->JSObject()->GetCData(XFA_Attribute::Name);
117 if (wsInstMgrName.GetLength() >= 1 && wsInstMgrName[0] == '_' &&
118 wsInstMgrName.Last(wsInstMgrName.GetLength() - 1) == wsName) {
119 pInstanceMgr = pNode;
120 }
121 break;
122 }
123 }
124 if (!pInstanceMgr) {
125 pValue->SetNull();
126 return;
127 }
128
129 pValue->Assign(GetDocument()->GetScriptContext()->GetOrCreateJSBindingFromMap(
130 pInstanceMgr));
131 }
132