• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(
113       GetDocument()->GetScriptContext()->GetOrCreateJSBindingFromMap(
114           pReturnNode));
115 }
116 
defaultValue(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)117 void CJX_ExclGroup::defaultValue(v8::Isolate* pIsolate,
118                                  v8::Local<v8::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(
127         fxv8::ReentrantToWideStringHelper(pIsolate, *pValue).AsStringView(),
128         true, true, true);
129     return;
130   }
131 
132   WideString wsValue = GetContent(true);
133   XFA_VERSION curVersion = GetDocument()->GetCurVersionMode();
134   if (wsValue.IsEmpty() && curVersion >= XFA_VERSION_300) {
135     *pValue = fxv8::NewNullHelper(pIsolate);
136     return;
137   }
138   *pValue = fxv8::NewStringHelper(pIsolate, wsValue.ToUTF8().AsStringView());
139 }
140 
rawValue(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)141 void CJX_ExclGroup::rawValue(v8::Isolate* pIsolate,
142                              v8::Local<v8::Value>* pValue,
143                              bool bSetting,
144                              XFA_Attribute eAttribute) {
145   defaultValue(pIsolate, pValue, bSetting, eAttribute);
146 }
147 
transient(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)148 void CJX_ExclGroup::transient(v8::Isolate* pIsolate,
149                               v8::Local<v8::Value>* pValue,
150                               bool bSetting,
151                               XFA_Attribute eAttribute) {}
152 
errorText(v8::Isolate * pIsolate,v8::Local<v8::Value> * pValue,bool bSetting,XFA_Attribute eAttribute)153 void CJX_ExclGroup::errorText(v8::Isolate* pIsolate,
154                               v8::Local<v8::Value>* pValue,
155                               bool bSetting,
156                               XFA_Attribute eAttribute) {
157   if (bSetting)
158     ThrowInvalidPropertyException(pIsolate);
159 }
160