Dan Sinclair | 93bb725 | 2017-12-07 22:33:43 +0000 | [diff] [blame] | 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_subform.h" |
| 8 | |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Lei Zhang | 7015634 | 2018-10-18 19:29:59 +0000 | [diff] [blame] | 11 | #include "fxjs/cfx_v8.h" |
Tom Sepez | aa44361 | 2021-01-21 19:58:48 +0000 | [diff] [blame] | 12 | #include "fxjs/fxv8.h" |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 13 | #include "fxjs/js_resources.h" |
Tom Sepez | 793691a | 2019-02-12 00:48:59 +0000 | [diff] [blame] | 14 | #include "fxjs/xfa/cfxjse_engine.h" |
Lei Zhang | 020fbf2 | 2021-09-21 21:58:22 +0000 | [diff] [blame] | 15 | #include "v8/include/v8-object.h" |
Dan Sinclair | 93bb725 | 2017-12-07 22:33:43 +0000 | [diff] [blame] | 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}, |
Dan Sinclair | 909fa2d | 2017-12-12 01:53:28 +0000 | [diff] [blame] | 26 | {"execValidate", execValidate_static}}; |
Dan Sinclair | 93bb725 | 2017-12-07 22:33:43 +0000 | [diff] [blame] | 27 | |
| 28 | CJX_Subform::CJX_Subform(CXFA_Node* node) : CJX_Container(node) { |
Tom Sepez | 88398a4 | 2018-06-11 17:08:07 +0000 | [diff] [blame] | 29 | DefineMethods(MethodSpecs); |
Dan Sinclair | 93bb725 | 2017-12-07 22:33:43 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Lei Zhang | 0e744a2 | 2020-06-02 00:44:28 +0000 | [diff] [blame] | 32 | CJX_Subform::~CJX_Subform() = default; |
Dan Sinclair | 93bb725 | 2017-12-07 22:33:43 +0000 | [diff] [blame] | 33 | |
Tom Sepez | 7e786cc | 2019-01-23 21:57:06 +0000 | [diff] [blame] | 34 | bool CJX_Subform::DynamicTypeIs(TypeTag eType) const { |
| 35 | return eType == static_type__ || ParentType__::DynamicTypeIs(eType); |
| 36 | } |
| 37 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 38 | CJS_Result CJX_Subform::execEvent( |
Tom Sepez | e0518bf | 2018-02-08 21:44:59 +0000 | [diff] [blame] | 39 | CFX_V8* runtime, |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 40 | const std::vector<v8::Local<v8::Value>>& params) { |
| 41 | if (params.size() != 1) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 42 | return CJS_Result::Failure(JSMessage::kParamError); |
Dan Sinclair | 93bb725 | 2017-12-07 22:33:43 +0000 | [diff] [blame] | 43 | |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 44 | execSingleEventByName(runtime->ToWideString(params[0]).AsStringView(), |
| 45 | XFA_Element::Subform); |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 46 | return CJS_Result::Success(); |
Dan Sinclair | 93bb725 | 2017-12-07 22:33:43 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 49 | CJS_Result CJX_Subform::execInitialize( |
Tom Sepez | e0518bf | 2018-02-08 21:44:59 +0000 | [diff] [blame] | 50 | CFX_V8* runtime, |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 51 | const std::vector<v8::Local<v8::Value>>& params) { |
| 52 | if (!params.empty()) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 53 | return CJS_Result::Failure(JSMessage::kParamError); |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 54 | |
| 55 | CXFA_FFNotify* pNotify = GetDocument()->GetNotify(); |
| 56 | if (pNotify) |
Dan Sinclair | bc948ce | 2018-01-22 21:28:58 +0000 | [diff] [blame] | 57 | pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Initialize, false, |
| 58 | true); |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 59 | return CJS_Result::Success(); |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 62 | CJS_Result CJX_Subform::execCalculate( |
Tom Sepez | e0518bf | 2018-02-08 21:44:59 +0000 | [diff] [blame] | 63 | CFX_V8* runtime, |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 64 | const std::vector<v8::Local<v8::Value>>& params) { |
| 65 | if (!params.empty()) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 66 | return CJS_Result::Failure(JSMessage::kParamError); |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 67 | |
| 68 | CXFA_FFNotify* pNotify = GetDocument()->GetNotify(); |
| 69 | if (pNotify) |
Dan Sinclair | bc948ce | 2018-01-22 21:28:58 +0000 | [diff] [blame] | 70 | pNotify->ExecEventByDeepFirst(GetXFANode(), XFA_EVENT_Calculate, false, |
| 71 | true); |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 72 | return CJS_Result::Success(); |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 75 | CJS_Result CJX_Subform::execValidate( |
Tom Sepez | e0518bf | 2018-02-08 21:44:59 +0000 | [diff] [blame] | 76 | CFX_V8* runtime, |
Dan Sinclair | cb22f9a | 2017-12-11 22:01:08 +0000 | [diff] [blame] | 77 | const std::vector<v8::Local<v8::Value>>& params) { |
| 78 | if (!params.empty()) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 79 | return CJS_Result::Failure(JSMessage::kParamError); |
Dan Sinclair | 93bb725 | 2017-12-07 22:33:43 +0000 | [diff] [blame] | 80 | |
| 81 | CXFA_FFNotify* pNotify = GetDocument()->GetNotify(); |
| 82 | if (!pNotify) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 83 | return CJS_Result::Success(runtime->NewBoolean(false)); |
Dan Sinclair | 93bb725 | 2017-12-07 22:33:43 +0000 | [diff] [blame] | 84 | |
Tom Sepez | 60e4bc2 | 2019-07-02 16:14:51 +0000 | [diff] [blame] | 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)); |
Dan Sinclair | 93bb725 | 2017-12-07 22:33:43 +0000 | [diff] [blame] | 89 | } |
Dan Sinclair | fe9d640 | 2017-12-14 19:45:53 +0000 | [diff] [blame] | 90 | |
Tom Sepez | 33001e2 | 2020-11-03 23:04:52 +0000 | [diff] [blame] | 91 | void CJX_Subform::locale(v8::Isolate* pIsolate, |
Tom Sepez | aa44361 | 2021-01-21 19:58:48 +0000 | [diff] [blame] | 92 | v8::Local<v8::Value>* pValue, |
Dan Sinclair | fe9d640 | 2017-12-14 19:45:53 +0000 | [diff] [blame] | 93 | bool bSetting, |
| 94 | XFA_Attribute eAttribute) { |
| 95 | if (bSetting) { |
Tom Sepez | aa44361 | 2021-01-21 19:58:48 +0000 | [diff] [blame] | 96 | SetCDataImpl(XFA_Attribute::Locale, |
| 97 | fxv8::ReentrantToWideStringHelper(pIsolate, *pValue), true, |
Tom Sepez | 33001e2 | 2020-11-03 23:04:52 +0000 | [diff] [blame] | 98 | true); |
Dan Sinclair | fe9d640 | 2017-12-14 19:45:53 +0000 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
Ryan Harrison | 5183e86 | 2018-01-05 15:46:40 -0500 | [diff] [blame] | 102 | WideString wsLocaleName = GetXFANode()->GetLocaleName().value_or(L""); |
Tom Sepez | aa44361 | 2021-01-21 19:58:48 +0000 | [diff] [blame] | 103 | *pValue = |
| 104 | fxv8::NewStringHelper(pIsolate, wsLocaleName.ToUTF8().AsStringView()); |
Dan Sinclair | fe9d640 | 2017-12-14 19:45:53 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Tom Sepez | 33001e2 | 2020-11-03 23:04:52 +0000 | [diff] [blame] | 107 | void CJX_Subform::instanceManager(v8::Isolate* pIsolate, |
Tom Sepez | aa44361 | 2021-01-21 19:58:48 +0000 | [diff] [blame] | 108 | v8::Local<v8::Value>* pValue, |
Tom Sepez | 793691a | 2019-02-12 00:48:59 +0000 | [diff] [blame] | 109 | bool bSetting, |
| 110 | XFA_Attribute eAttribute) { |
| 111 | if (bSetting) { |
Daniel Hosseinian | 2f6fbf6 | 2021-10-22 18:19:34 +0000 | [diff] [blame] | 112 | ThrowInvalidPropertyException(pIsolate); |
Tom Sepez | 793691a | 2019-02-12 00:48:59 +0000 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
| 116 | WideString wsName = GetCData(XFA_Attribute::Name); |
| 117 | CXFA_Node* pInstanceMgr = nullptr; |
Tom Sepez | 4429eb2 | 2020-07-10 20:09:20 +0000 | [diff] [blame] | 118 | for (CXFA_Node* pNode = GetXFANode()->GetPrevSibling(); pNode; |
Tom Sepez | 793691a | 2019-02-12 00:48:59 +0000 | [diff] [blame] | 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] == '_' && |
Daniel Hosseinian | a9a704e | 2020-01-28 19:52:32 +0000 | [diff] [blame] | 124 | wsInstMgrName.Last(wsInstMgrName.GetLength() - 1) == wsName) { |
Tom Sepez | 793691a | 2019-02-12 00:48:59 +0000 | [diff] [blame] | 125 | pInstanceMgr = pNode; |
| 126 | } |
| 127 | break; |
| 128 | } |
| 129 | } |
Tom Sepez | aa44361 | 2021-01-21 19:58:48 +0000 | [diff] [blame] | 130 | *pValue = pInstanceMgr ? GetDocument() |
| 131 | ->GetScriptContext() |
| 132 | ->GetOrCreateJSBindingFromMap(pInstanceMgr) |
| 133 | .As<v8::Value>() |
| 134 | : fxv8::NewNullHelper(pIsolate).As<v8::Value>(); |
Tom Sepez | 793691a | 2019-02-12 00:48:59 +0000 | [diff] [blame] | 135 | } |