xref: /aosp_15_r20/external/pdfium/fxjs/xfa/cjx_exclgroup.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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_exclgroup.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/v8-object.h"
15 #include "v8/include/v8-primitive.h"
16 #include "xfa/fxfa/cxfa_eventparam.h"
17 #include "xfa/fxfa/cxfa_ffnotify.h"
18 #include "xfa/fxfa/fxfa.h"
19 #include "xfa/fxfa/parser/cxfa_document.h"
20 #include "xfa/fxfa/parser/cxfa_exclgroup.h"
21 
22 const CJX_MethodSpec CJX_ExclGroup::MethodSpecs[] = {
23     {"execCalculate", execCalculate_static},
24     {"execEvent", execEvent_static},
25     {"execInitialize", execInitialize_static},
26     {"execValidate", execValidate_static},
27     {"selectedMember", selectedMember_static}};
28 
CJX_ExclGroup(CXFA_ExclGroup * group)29 CJX_ExclGroup::CJX_ExclGroup(CXFA_ExclGroup* group) : CJX_Node(group) {
30   DefineMethods(MethodSpecs);
31 }
32 
33 CJX_ExclGroup::~CJX_ExclGroup() = default;
34 
DynamicTypeIs(TypeTag eType) const35 bool CJX_ExclGroup::DynamicTypeIs(TypeTag eType) const {
36   return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
37 }
38 
execEvent(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)39 CJS_Result CJX_ExclGroup::execEvent(
40     CFXJSE_Engine* runtime,
41     const std::vector<v8::Local<v8::Value>>& params) {
42   if (params.size() != 1)
43     return CJS_Result::Failure(JSMessage::kParamError);
44 
45   execSingleEventByName(runtime->ToWideString(params[0]).AsStringView(),
46                         XFA_Element::ExclGroup);
47   return CJS_Result::Success();
48 }
49 
execInitialize(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)50 CJS_Result CJX_ExclGroup::execInitialize(
51     CFXJSE_Engine* runtime,
52     const std::vector<v8::Local<v8::Value>>& params) {
53   if (!params.empty())
54     return CJS_Result::Failure(JSMessage::kParamError);
55 
56   CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
57   if (pNotify)
58     pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Initialize, false,
59                                   true);
60   return CJS_Result::Success();
61 }
62 
execCalculate(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)63 CJS_Result CJX_ExclGroup::execCalculate(
64     CFXJSE_Engine* runtime,
65     const std::vector<v8::Local<v8::Value>>& params) {
66   if (!params.empty())
67     return CJS_Result::Failure(JSMessage::kParamError);
68 
69   CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
70   if (pNotify)
71     pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate, false,
72                                   true);
73   return CJS_Result::Success();
74 }
75 
execValidate(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)76 CJS_Result CJX_ExclGroup::execValidate(
77     CFXJSE_Engine* runtime,
78     const std::vector<v8::Local<v8::Value>>& params) {
79   if (!params.empty())
80     return CJS_Result::Failure(JSMessage::kParamError);
81 
82   CXFA_FFNotify* notify = GetDocument()->GetNotify();
83   if (!notify)
84     return CJS_Result::Success(runtime->NewBoolean(false));
85 
86   XFA_EventError iRet = notify->ExecEventByDeepFirst(
87       GetXFANode(), XFA_EVENT_Validate, false, true);
88   return CJS_Result::Success(
89       runtime->NewBoolean(iRet != XFA_EventError::kError));
90 }
91 
selectedMember(CFXJSE_Engine * runtime,const std::vector<v8::Local<v8::Value>> & params)92 CJS_Result CJX_ExclGroup::selectedMember(
93     CFXJSE_Engine* runtime,
94     const std::vector<v8::Local<v8::Value>>& params) {
95   if (!params.empty())
96     return CJS_Result::Failure(JSMessage::kParamError);
97 
98   CXFA_Node* node = GetXFANode();
99   if (!node->IsWidgetReady())
100     return CJS_Result::Success(runtime->NewNull());
101 
102   CXFA_Node* pReturnNode = nullptr;
103   if (params.empty()) {
104     pReturnNode = node->GetSelectedMember();
105   } else {
106     pReturnNode = node->SetSelectedMember(
107         runtime->ToWideString(params[0]).AsStringView());
108   }
109   if (!pReturnNode)
110     return CJS_Result::Success(runtime->NewNull());
111 
112   return CJS_Result::Success(runtime->GetOrCreateJSBindingFromMap(pReturnNode));
113 }
114 
defaultValue(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)115 void CJX_ExclGroup::defaultValue(v8::Isolate* pIsolate,
116                                  v8::Local<v8::Value>* pValue,
117                                  bool bSetting,
118                                  XFA_Attribute eAttribute) {
119   CXFA_Node* node = GetXFANode();
120   if (!node->IsWidgetReady())
121     return;
122 
123   if (bSetting) {
124     node->SetSelectedMemberByValue(
125         fxv8::ReentrantToWideStringHelper(pIsolate, *pValue).AsStringView(),
126         true, true, true);
127     return;
128   }
129 
130   WideString wsValue = GetContent(true);
131   XFA_VERSION curVersion = GetDocument()->GetCurVersionMode();
132   if (wsValue.IsEmpty() && curVersion >= XFA_VERSION_300) {
133     *pValue = fxv8::NewNullHelper(pIsolate);
134     return;
135   }
136   *pValue = fxv8::NewStringHelper(pIsolate, wsValue.ToUTF8().AsStringView());
137 }
138 
rawValue(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)139 void CJX_ExclGroup::rawValue(v8::Isolate* pIsolate,
140                              v8::Local<v8::Value>* pValue,
141                              bool bSetting,
142                              XFA_Attribute eAttribute) {
143   defaultValue(pIsolate, pValue, bSetting, eAttribute);
144 }
145 
transient(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)146 void CJX_ExclGroup::transient(v8::Isolate* pIsolate,
147                               v8::Local<v8::Value>* pValue,
148                               bool bSetting,
149                               XFA_Attribute eAttribute) {}
150 
errorText(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)151 void CJX_ExclGroup::errorText(v8::Isolate* pIsolate,
152                               v8::Local<v8::Value>* pValue,
153                               bool bSetting,
154                               XFA_Attribute eAttribute) {
155   if (bSetting)
156     ThrowInvalidPropertyException(pIsolate);
157 }
158