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