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_subform.h"
8
9 #include "core/fxcrt/span.h"
10 #include "fxjs/cfx_v8.h"
11 #include "fxjs/fxv8.h"
12 #include "fxjs/js_resources.h"
13 #include "fxjs/xfa/cfxjse_engine.h"
14 #include "v8/include/v8-object.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(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)37 CJS_Result CJX_Subform::execEvent(CFXJSE_Engine* runtime,
38 pdfium::span<v8::Local<v8::Value>> params) {
39 if (params.size() != 1)
40 return CJS_Result::Failure(JSMessage::kParamError);
41
42 execSingleEventByName(runtime->ToWideString(params[0]).AsStringView(),
43 XFA_Element::Subform);
44 return CJS_Result::Success();
45 }
46
execInitialize(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)47 CJS_Result CJX_Subform::execInitialize(
48 CFXJSE_Engine* runtime,
49 pdfium::span<v8::Local<v8::Value>> params) {
50 if (!params.empty())
51 return CJS_Result::Failure(JSMessage::kParamError);
52
53 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
54 if (pNotify)
55 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Initialize, false,
56 true);
57 return CJS_Result::Success();
58 }
59
execCalculate(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)60 CJS_Result CJX_Subform::execCalculate(
61 CFXJSE_Engine* runtime,
62 pdfium::span<v8::Local<v8::Value>> params) {
63 if (!params.empty())
64 return CJS_Result::Failure(JSMessage::kParamError);
65
66 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
67 if (pNotify)
68 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate, false,
69 true);
70 return CJS_Result::Success();
71 }
72
execValidate(CFXJSE_Engine * runtime,pdfium::span<v8::Local<v8::Value>> params)73 CJS_Result CJX_Subform::execValidate(
74 CFXJSE_Engine* runtime,
75 pdfium::span<v8::Local<v8::Value>> params) {
76 if (!params.empty())
77 return CJS_Result::Failure(JSMessage::kParamError);
78
79 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
80 if (!pNotify)
81 return CJS_Result::Success(runtime->NewBoolean(false));
82
83 XFA_EventError iRet = pNotify->ExecEventByDeepFirst(
84 GetXFANode(), XFA_EVENT_Validate, false, true);
85 return CJS_Result::Success(
86 runtime->NewBoolean(iRet != XFA_EventError::kError));
87 }
88
locale(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)89 void CJX_Subform::locale(v8::Isolate* pIsolate,
90 v8::Local<v8::Value>* pValue,
91 bool bSetting,
92 XFA_Attribute eAttribute) {
93 if (bSetting) {
94 SetCDataImpl(XFA_Attribute::Locale,
95 fxv8::ReentrantToWideStringHelper(pIsolate, *pValue), true,
96 true);
97 return;
98 }
99
100 WideString wsLocaleName = GetXFANode()->GetLocaleName().value_or(L"");
101 *pValue =
102 fxv8::NewStringHelper(pIsolate, wsLocaleName.ToUTF8().AsStringView());
103 }
104
instanceManager(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)105 void CJX_Subform::instanceManager(v8::Isolate* pIsolate,
106 v8::Local<v8::Value>* pValue,
107 bool bSetting,
108 XFA_Attribute eAttribute) {
109 if (bSetting) {
110 ThrowInvalidPropertyException(pIsolate);
111 return;
112 }
113
114 WideString wsName = GetCData(XFA_Attribute::Name);
115 CXFA_Node* pInstanceMgr = nullptr;
116 for (CXFA_Node* pNode = GetXFANode()->GetPrevSibling(); pNode;
117 pNode = pNode->GetPrevSibling()) {
118 if (pNode->GetElementType() == XFA_Element::InstanceManager) {
119 WideString wsInstMgrName =
120 pNode->JSObject()->GetCData(XFA_Attribute::Name);
121 if (wsInstMgrName.GetLength() >= 1 && wsInstMgrName[0] == '_' &&
122 wsInstMgrName.Last(wsInstMgrName.GetLength() - 1) == wsName) {
123 pInstanceMgr = pNode;
124 }
125 break;
126 }
127 }
128 *pValue = pInstanceMgr ? GetDocument()
129 ->GetScriptContext()
130 ->GetOrCreateJSBindingFromMap(pInstanceMgr)
131 .As<v8::Value>()
132 : fxv8::NewNullHelper(pIsolate).As<v8::Value>();
133 }
134