blob: 31ee3be337c96146f9bb9d7bcfa10e2f2e155b18 [file] [log] [blame]
Dan Sinclair93bb7252017-12-07 22:33:43 +00001// 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_subform.h"
8
Dan Sinclaircb22f9a2017-12-11 22:01:08 +00009#include <vector>
10
Lei Zhang70156342018-10-18 19:29:59 +000011#include "fxjs/cfx_v8.h"
Tom Sepezaa443612021-01-21 19:58:48 +000012#include "fxjs/fxv8.h"
Dan Sinclaircb22f9a2017-12-11 22:01:08 +000013#include "fxjs/js_resources.h"
Tom Sepez793691a2019-02-12 00:48:59 +000014#include "fxjs/xfa/cfxjse_engine.h"
Lei Zhang020fbf22021-09-21 21:58:22 +000015#include "v8/include/v8-object.h"
Dan Sinclair93bb7252017-12-07 22:33:43 +000016#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
22const CJX_MethodSpec CJX_Subform::MethodSpecs[] = {
23 {"execCalculate", execCalculate_static},
24 {"execEvent", execEvent_static},
25 {"execInitialize", execInitialize_static},
Dan Sinclair909fa2d2017-12-12 01:53:28 +000026 {"execValidate", execValidate_static}};
Dan Sinclair93bb7252017-12-07 22:33:43 +000027
28CJX_Subform::CJX_Subform(CXFA_Node* node) : CJX_Container(node) {
Tom Sepez88398a42018-06-11 17:08:07 +000029 DefineMethods(MethodSpecs);
Dan Sinclair93bb7252017-12-07 22:33:43 +000030}
31
Lei Zhang0e744a22020-06-02 00:44:28 +000032CJX_Subform::~CJX_Subform() = default;
Dan Sinclair93bb7252017-12-07 22:33:43 +000033
Tom Sepez7e786cc2019-01-23 21:57:06 +000034bool CJX_Subform::DynamicTypeIs(TypeTag eType) const {
35 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
36}
37
Tom Sepez3a6d0582018-08-17 19:28:52 +000038CJS_Result CJX_Subform::execEvent(
Tom Sepeze0518bf2018-02-08 21:44:59 +000039 CFX_V8* runtime,
Dan Sinclaircb22f9a2017-12-11 22:01:08 +000040 const std::vector<v8::Local<v8::Value>>& params) {
41 if (params.size() != 1)
Tom Sepez3a6d0582018-08-17 19:28:52 +000042 return CJS_Result::Failure(JSMessage::kParamError);
Dan Sinclair93bb7252017-12-07 22:33:43 +000043
Dan Sinclaircb22f9a2017-12-11 22:01:08 +000044 execSingleEventByName(runtime->ToWideString(params[0]).AsStringView(),
45 XFA_Element::Subform);
Tom Sepez3a6d0582018-08-17 19:28:52 +000046 return CJS_Result::Success();
Dan Sinclair93bb7252017-12-07 22:33:43 +000047}
48
Tom Sepez3a6d0582018-08-17 19:28:52 +000049CJS_Result CJX_Subform::execInitialize(
Tom Sepeze0518bf2018-02-08 21:44:59 +000050 CFX_V8* runtime,
Dan Sinclaircb22f9a2017-12-11 22:01:08 +000051 const std::vector<v8::Local<v8::Value>>& params) {
52 if (!params.empty())
Tom Sepez3a6d0582018-08-17 19:28:52 +000053 return CJS_Result::Failure(JSMessage::kParamError);
Dan Sinclaircb22f9a2017-12-11 22:01:08 +000054
55 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
56 if (pNotify)
Dan Sinclairbc948ce2018-01-22 21:28:58 +000057 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Initialize, false,
58 true);
Tom Sepez3a6d0582018-08-17 19:28:52 +000059 return CJS_Result::Success();
Dan Sinclaircb22f9a2017-12-11 22:01:08 +000060}
61
Tom Sepez3a6d0582018-08-17 19:28:52 +000062CJS_Result CJX_Subform::execCalculate(
Tom Sepeze0518bf2018-02-08 21:44:59 +000063 CFX_V8* runtime,
Dan Sinclaircb22f9a2017-12-11 22:01:08 +000064 const std::vector<v8::Local<v8::Value>>& params) {
65 if (!params.empty())
Tom Sepez3a6d0582018-08-17 19:28:52 +000066 return CJS_Result::Failure(JSMessage::kParamError);
Dan Sinclaircb22f9a2017-12-11 22:01:08 +000067
68 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
69 if (pNotify)
Dan Sinclairbc948ce2018-01-22 21:28:58 +000070 pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate, false,
71 true);
Tom Sepez3a6d0582018-08-17 19:28:52 +000072 return CJS_Result::Success();
Dan Sinclaircb22f9a2017-12-11 22:01:08 +000073}
74
Tom Sepez3a6d0582018-08-17 19:28:52 +000075CJS_Result CJX_Subform::execValidate(
Tom Sepeze0518bf2018-02-08 21:44:59 +000076 CFX_V8* runtime,
Dan Sinclaircb22f9a2017-12-11 22:01:08 +000077 const std::vector<v8::Local<v8::Value>>& params) {
78 if (!params.empty())
Tom Sepez3a6d0582018-08-17 19:28:52 +000079 return CJS_Result::Failure(JSMessage::kParamError);
Dan Sinclair93bb7252017-12-07 22:33:43 +000080
81 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
82 if (!pNotify)
Tom Sepez3a6d0582018-08-17 19:28:52 +000083 return CJS_Result::Success(runtime->NewBoolean(false));
Dan Sinclair93bb7252017-12-07 22:33:43 +000084
Tom Sepez60e4bc22019-07-02 16:14:51 +000085 XFA_EventError iRet = pNotify->ExecEventByDeepFirst(
86 GetXFANode(), XFA_EVENT_Validate, false, true);
87 return CJS_Result::Success(
88 runtime->NewBoolean(iRet != XFA_EventError::kError));
Dan Sinclair93bb7252017-12-07 22:33:43 +000089}
Dan Sinclairfe9d6402017-12-14 19:45:53 +000090
Tom Sepez33001e22020-11-03 23:04:52 +000091void CJX_Subform::locale(v8::Isolate* pIsolate,
Tom Sepezaa443612021-01-21 19:58:48 +000092 v8::Local<v8::Value>* pValue,
Dan Sinclairfe9d6402017-12-14 19:45:53 +000093 bool bSetting,
94 XFA_Attribute eAttribute) {
95 if (bSetting) {
Tom Sepezaa443612021-01-21 19:58:48 +000096 SetCDataImpl(XFA_Attribute::Locale,
97 fxv8::ReentrantToWideStringHelper(pIsolate, *pValue), true,
Tom Sepez33001e22020-11-03 23:04:52 +000098 true);
Dan Sinclairfe9d6402017-12-14 19:45:53 +000099 return;
100 }
101
Ryan Harrison5183e862018-01-05 15:46:40 -0500102 WideString wsLocaleName = GetXFANode()->GetLocaleName().value_or(L"");
Tom Sepezaa443612021-01-21 19:58:48 +0000103 *pValue =
104 fxv8::NewStringHelper(pIsolate, wsLocaleName.ToUTF8().AsStringView());
Dan Sinclairfe9d6402017-12-14 19:45:53 +0000105}
106
Tom Sepez33001e22020-11-03 23:04:52 +0000107void CJX_Subform::instanceManager(v8::Isolate* pIsolate,
Tom Sepezaa443612021-01-21 19:58:48 +0000108 v8::Local<v8::Value>* pValue,
Tom Sepez793691a2019-02-12 00:48:59 +0000109 bool bSetting,
110 XFA_Attribute eAttribute) {
111 if (bSetting) {
Daniel Hosseinian2f6fbf62021-10-22 18:19:34 +0000112 ThrowInvalidPropertyException(pIsolate);
Tom Sepez793691a2019-02-12 00:48:59 +0000113 return;
114 }
115
116 WideString wsName = GetCData(XFA_Attribute::Name);
117 CXFA_Node* pInstanceMgr = nullptr;
Tom Sepez4429eb22020-07-10 20:09:20 +0000118 for (CXFA_Node* pNode = GetXFANode()->GetPrevSibling(); pNode;
Tom Sepez793691a2019-02-12 00:48:59 +0000119 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] == '_' &&
Daniel Hosseiniana9a704e2020-01-28 19:52:32 +0000124 wsInstMgrName.Last(wsInstMgrName.GetLength() - 1) == wsName) {
Tom Sepez793691a2019-02-12 00:48:59 +0000125 pInstanceMgr = pNode;
126 }
127 break;
128 }
129 }
Tom Sepezaa443612021-01-21 19:58:48 +0000130 *pValue = pInstanceMgr ? GetDocument()
131 ->GetScriptContext()
132 ->GetOrCreateJSBindingFromMap(pInstanceMgr)
133 .As<v8::Value>()
134 : fxv8::NewNullHelper(pIsolate).As<v8::Value>();
Tom Sepez793691a2019-02-12 00:48:59 +0000135}