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_exclgroup.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/fxfa.h"
17 #include "xfa/fxfa/parser/cxfa_document.h"
18 #include "xfa/fxfa/parser/cxfa_exclgroup.h"
19 
20 const CJX_MethodSpec CJX_ExclGroup::MethodSpecs[] = {
21     {"execCalculate", execCalculate_static},
22     {"execEvent", execEvent_static},
23     {"execInitialize", execInitialize_static},
24     {"execValidate", execValidate_static},
25     {"selectedMember", selectedMember_static}};
26 
CJX_ExclGroup(CXFA_ExclGroup * group)27 CJX_ExclGroup::CJX_ExclGroup(CXFA_ExclGroup* group) : CJX_Node(group) {
28   DefineMethods(MethodSpecs);
29 }
30 
~CJX_ExclGroup()31 CJX_ExclGroup::~CJX_ExclGroup() {}
32 
DynamicTypeIs(TypeTag eType) const33 bool CJX_ExclGroup::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_ExclGroup::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::ExclGroup);
45   return CJS_Result::Success();
46 }
47 
execInitialize(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)48 CJS_Result CJX_ExclGroup::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_ExclGroup::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_ExclGroup::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* notify = GetDocument()->GetNotify();
81   if (!notify)
82     return CJS_Result::Success(runtime->NewBoolean(false));
83 
84   XFA_EventError iRet = notify->ExecEventByDeepFirst(
85       GetXFANode(), XFA_EVENT_Validate, false, true);
86   return CJS_Result::Success(
87       runtime->NewBoolean(iRet != XFA_EventError::kError));
88 }
89 
selectedMember(CFX_V8 * runtime,const std::vector<v8::Local<v8::Value>> & params)90 CJS_Result CJX_ExclGroup::selectedMember(
91     CFX_V8* runtime,
92     const std::vector<v8::Local<v8::Value>>& params) {
93   if (!params.empty())
94     return CJS_Result::Failure(JSMessage::kParamError);
95 
96   CXFA_Node* node = GetXFANode();
97   if (!node->IsWidgetReady())
98     return CJS_Result::Success(runtime->NewNull());
99 
100   CXFA_Node* pReturnNode = nullptr;
101   if (params.empty()) {
102     pReturnNode = node->GetSelectedMember();
103   } else {
104     pReturnNode = node->SetSelectedMember(
105         runtime->ToWideString(params[0]).AsStringView(), true);
106   }
107   if (!pReturnNode)
108     return CJS_Result::Success(runtime->NewNull());
109 
110   CFXJSE_Value* value =
111       GetDocument()->GetScriptContext()->GetOrCreateJSBindingFromMap(
112           pReturnNode);
113 
114   return CJS_Result::Success(
115       value->DirectGetValue().Get(runtime->GetIsolate()));
116 }
117 
defaultValue(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)118 void CJX_ExclGroup::defaultValue(CFXJSE_Value* pValue,
119                                  bool bSetting,
120                                  XFA_Attribute eAttribute) {
121   CXFA_Node* node = GetXFANode();
122   if (!node->IsWidgetReady())
123     return;
124 
125   if (bSetting) {
126     node->SetSelectedMemberByValue(pValue->ToWideString().AsStringView(), true,
127                                    true, true);
128     return;
129   }
130 
131   WideString wsValue = GetContent(true);
132   XFA_VERSION curVersion = GetDocument()->GetCurVersionMode();
133   if (wsValue.IsEmpty() && curVersion >= XFA_VERSION_300) {
134     pValue->SetNull();
135     return;
136   }
137   pValue->SetString(wsValue.ToUTF8().AsStringView());
138 }
139 
rawValue(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)140 void CJX_ExclGroup::rawValue(CFXJSE_Value* pValue,
141                              bool bSetting,
142                              XFA_Attribute eAttribute) {
143   defaultValue(pValue, bSetting, eAttribute);
144 }
145 
transient(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)146 void CJX_ExclGroup::transient(CFXJSE_Value* pValue,
147                               bool bSetting,
148                               XFA_Attribute eAttribute) {}
149 
errorText(CFXJSE_Value * pValue,bool bSetting,XFA_Attribute eAttribute)150 void CJX_ExclGroup::errorText(CFXJSE_Value* pValue,
151                               bool bSetting,
152                               XFA_Attribute eAttribute) {
153   if (bSetting)
154     ThrowInvalidPropertyException();
155 }
156