blob: 72309dc4f60d3254caeedc5c2689b51168d1e64f [file] [log] [blame]
kumarashishg826308d2023-06-23 13:21:22 +00001// Copyright 2017 The PDFium Authors
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -07002// 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_eventpseudomodel.h"
8
Haibo Huang49cc9302020-04-27 16:14:24 -07009#include <algorithm>
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070010#include <vector>
11
kumarashishg826308d2023-06-23 13:21:22 +000012#include "fxjs/fxv8.h"
Haibo Huang49cc9302020-04-27 16:14:24 -070013#include "fxjs/xfa/cfxjse_engine.h"
kumarashishg826308d2023-06-23 13:21:22 +000014#include "third_party/base/notreached.h"
15#include "third_party/base/numerics/safe_conversions.h"
16#include "v8/include/v8-primitive.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070017#include "xfa/fxfa/cxfa_eventparam.h"
18#include "xfa/fxfa/cxfa_ffnotify.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070019#include "xfa/fxfa/parser/cscript_eventpseudomodel.h"
20
21namespace {
22
kumarashishg826308d2023-06-23 13:21:22 +000023void StringProperty(v8::Isolate* pIsolate,
24 v8::Local<v8::Value>* pReturn,
25 WideString* wsValue,
26 bool bSetting) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070027 if (bSetting) {
kumarashishg826308d2023-06-23 13:21:22 +000028 *wsValue = fxv8::ReentrantToWideStringHelper(pIsolate, *pReturn);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070029 return;
30 }
kumarashishg826308d2023-06-23 13:21:22 +000031 *pReturn = fxv8::NewStringHelper(pIsolate, wsValue->ToUTF8().AsStringView());
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070032}
33
kumarashishg826308d2023-06-23 13:21:22 +000034void IntegerProperty(v8::Isolate* pIsolate,
35 v8::Local<v8::Value>* pReturn,
36 int32_t* iValue,
37 bool bSetting) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070038 if (bSetting) {
kumarashishg826308d2023-06-23 13:21:22 +000039 *iValue = fxv8::ReentrantToInt32Helper(pIsolate, *pReturn);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070040 return;
41 }
kumarashishg826308d2023-06-23 13:21:22 +000042 *pReturn = fxv8::NewNumberHelper(pIsolate, *iValue);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070043}
44
kumarashishg826308d2023-06-23 13:21:22 +000045void BooleanProperty(v8::Isolate* pIsolate,
46 v8::Local<v8::Value>* pReturn,
47 bool* bValue,
48 bool bSetting) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070049 if (bSetting) {
kumarashishg826308d2023-06-23 13:21:22 +000050 *bValue = fxv8::ReentrantToBooleanHelper(pIsolate, *pReturn);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070051 return;
52 }
kumarashishg826308d2023-06-23 13:21:22 +000053 *pReturn = fxv8::NewBooleanHelper(pIsolate, *bValue);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070054}
55
56} // namespace
57
58const CJX_MethodSpec CJX_EventPseudoModel::MethodSpecs[] = {
59 {"emit", emit_static},
60 {"reset", reset_static}};
61
62CJX_EventPseudoModel::CJX_EventPseudoModel(CScript_EventPseudoModel* model)
63 : CJX_Object(model) {
Haibo Huang49cc9302020-04-27 16:14:24 -070064 DefineMethods(MethodSpecs);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070065}
66
kumarashishg826308d2023-06-23 13:21:22 +000067CJX_EventPseudoModel::~CJX_EventPseudoModel() = default;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070068
Haibo Huang49cc9302020-04-27 16:14:24 -070069bool CJX_EventPseudoModel::DynamicTypeIs(TypeTag eType) const {
70 return eType == static_type__ || ParentType__::DynamicTypeIs(eType);
71}
72
kumarashishg826308d2023-06-23 13:21:22 +000073void CJX_EventPseudoModel::cancelAction(v8::Isolate* pIsolate,
74 v8::Local<v8::Value>* pValue,
Haibo Huang49cc9302020-04-27 16:14:24 -070075 bool bSetting,
76 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +000077 Property(pIsolate, pValue, XFA_Event::CancelAction, bSetting);
Haibo Huang49cc9302020-04-27 16:14:24 -070078}
79
kumarashishg826308d2023-06-23 13:21:22 +000080void CJX_EventPseudoModel::change(v8::Isolate* pIsolate,
81 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070082 bool bSetting,
83 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +000084 Property(pIsolate, pValue, XFA_Event::Change, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070085}
86
kumarashishg826308d2023-06-23 13:21:22 +000087void CJX_EventPseudoModel::commitKey(v8::Isolate* pIsolate,
88 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070089 bool bSetting,
90 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +000091 Property(pIsolate, pValue, XFA_Event::CommitKey, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070092}
93
kumarashishg826308d2023-06-23 13:21:22 +000094void CJX_EventPseudoModel::fullText(v8::Isolate* pIsolate,
95 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070096 bool bSetting,
97 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +000098 Property(pIsolate, pValue, XFA_Event::FullText, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070099}
100
kumarashishg826308d2023-06-23 13:21:22 +0000101void CJX_EventPseudoModel::keyDown(v8::Isolate* pIsolate,
102 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700103 bool bSetting,
104 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000105 Property(pIsolate, pValue, XFA_Event::Keydown, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700106}
107
kumarashishg826308d2023-06-23 13:21:22 +0000108void CJX_EventPseudoModel::modifier(v8::Isolate* pIsolate,
109 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700110 bool bSetting,
111 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000112 Property(pIsolate, pValue, XFA_Event::Modifier, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700113}
114
kumarashishg826308d2023-06-23 13:21:22 +0000115void CJX_EventPseudoModel::newContentType(v8::Isolate* pIsolate,
116 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700117 bool bSetting,
118 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000119 Property(pIsolate, pValue, XFA_Event::NewContentType, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700120}
121
kumarashishg826308d2023-06-23 13:21:22 +0000122void CJX_EventPseudoModel::newText(v8::Isolate* pIsolate,
123 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700124 bool bSetting,
125 XFA_Attribute eAttribute) {
Haibo Huang49cc9302020-04-27 16:14:24 -0700126 if (bSetting)
127 return;
128
129 CFXJSE_Engine* pScriptContext = GetDocument()->GetScriptContext();
130 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();
131 if (!pEventParam)
132 return;
133
kumarashishg826308d2023-06-23 13:21:22 +0000134 *pValue = fxv8::NewStringHelper(
135 pIsolate, pEventParam->GetNewText().ToUTF8().AsStringView());
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700136}
137
kumarashishg826308d2023-06-23 13:21:22 +0000138void CJX_EventPseudoModel::prevContentType(v8::Isolate* pIsolate,
139 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700140 bool bSetting,
141 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000142 Property(pIsolate, pValue, XFA_Event::PreviousContentType, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700143}
144
kumarashishg826308d2023-06-23 13:21:22 +0000145void CJX_EventPseudoModel::prevText(v8::Isolate* pIsolate,
146 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700147 bool bSetting,
148 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000149 Property(pIsolate, pValue, XFA_Event::PreviousText, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700150}
151
kumarashishg826308d2023-06-23 13:21:22 +0000152void CJX_EventPseudoModel::reenter(v8::Isolate* pIsolate,
153 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700154 bool bSetting,
155 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000156 Property(pIsolate, pValue, XFA_Event::Reenter, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700157}
158
kumarashishg826308d2023-06-23 13:21:22 +0000159void CJX_EventPseudoModel::selEnd(v8::Isolate* pIsolate,
160 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700161 bool bSetting,
162 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000163 Property(pIsolate, pValue, XFA_Event::SelectionEnd, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700164}
165
kumarashishg826308d2023-06-23 13:21:22 +0000166void CJX_EventPseudoModel::selStart(v8::Isolate* pIsolate,
167 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700168 bool bSetting,
169 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000170 Property(pIsolate, pValue, XFA_Event::SelectionStart, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700171}
172
kumarashishg826308d2023-06-23 13:21:22 +0000173void CJX_EventPseudoModel::shift(v8::Isolate* pIsolate,
174 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700175 bool bSetting,
176 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000177 Property(pIsolate, pValue, XFA_Event::Shift, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700178}
179
kumarashishg826308d2023-06-23 13:21:22 +0000180void CJX_EventPseudoModel::soapFaultCode(v8::Isolate* pIsolate,
181 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700182 bool bSetting,
183 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000184 Property(pIsolate, pValue, XFA_Event::SoapFaultCode, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700185}
186
kumarashishg826308d2023-06-23 13:21:22 +0000187void CJX_EventPseudoModel::soapFaultString(v8::Isolate* pIsolate,
188 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700189 bool bSetting,
190 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000191 Property(pIsolate, pValue, XFA_Event::SoapFaultString, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700192}
193
kumarashishg826308d2023-06-23 13:21:22 +0000194void CJX_EventPseudoModel::target(v8::Isolate* pIsolate,
195 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700196 bool bSetting,
197 XFA_Attribute eAttribute) {
kumarashishg826308d2023-06-23 13:21:22 +0000198 Property(pIsolate, pValue, XFA_Event::Target, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700199}
200
Haibo Huang49cc9302020-04-27 16:14:24 -0700201CJS_Result CJX_EventPseudoModel::emit(
kumarashishg826308d2023-06-23 13:21:22 +0000202 CFXJSE_Engine* runtime,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700203 const std::vector<v8::Local<v8::Value>>& params) {
204 CFXJSE_Engine* pScriptContext = GetDocument()->GetScriptContext();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700205 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();
206 if (!pEventParam)
Haibo Huang49cc9302020-04-27 16:14:24 -0700207 return CJS_Result::Success();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700208
209 CXFA_FFNotify* pNotify = GetDocument()->GetNotify();
210 if (!pNotify)
Haibo Huang49cc9302020-04-27 16:14:24 -0700211 return CJS_Result::Success();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700212
kumarashishg826308d2023-06-23 13:21:22 +0000213 pNotify->HandleWidgetEvent(pScriptContext->GetEventTarget(), pEventParam);
Haibo Huang49cc9302020-04-27 16:14:24 -0700214 return CJS_Result::Success();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700215}
216
Haibo Huang49cc9302020-04-27 16:14:24 -0700217CJS_Result CJX_EventPseudoModel::reset(
kumarashishg826308d2023-06-23 13:21:22 +0000218 CFXJSE_Engine* runtime,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700219 const std::vector<v8::Local<v8::Value>>& params) {
220 CFXJSE_Engine* pScriptContext = GetDocument()->GetScriptContext();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700221 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();
Haibo Huang49cc9302020-04-27 16:14:24 -0700222 if (pEventParam)
223 *pEventParam = CXFA_EventParam();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700224
Haibo Huang49cc9302020-04-27 16:14:24 -0700225 return CJS_Result::Success();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700226}
227
kumarashishg826308d2023-06-23 13:21:22 +0000228void CJX_EventPseudoModel::Property(v8::Isolate* pIsolate,
229 v8::Local<v8::Value>* pValue,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700230 XFA_Event dwFlag,
231 bool bSetting) {
Haibo Huang49cc9302020-04-27 16:14:24 -0700232 // Only the cancelAction, selStart, selEnd and change properties are writable.
233 if (bSetting && dwFlag != XFA_Event::CancelAction &&
234 dwFlag != XFA_Event::SelectionStart &&
235 dwFlag != XFA_Event::SelectionEnd && dwFlag != XFA_Event::Change) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700236 return;
Haibo Huang49cc9302020-04-27 16:14:24 -0700237 }
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700238
Haibo Huang49cc9302020-04-27 16:14:24 -0700239 CFXJSE_Engine* pScriptContext = GetDocument()->GetScriptContext();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700240 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam();
241 if (!pEventParam)
242 return;
243
244 switch (dwFlag) {
245 case XFA_Event::CancelAction:
kumarashishg826308d2023-06-23 13:21:22 +0000246 BooleanProperty(pIsolate, pValue, &pEventParam->m_bCancelAction,
247 bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700248 break;
249 case XFA_Event::Change:
kumarashishg826308d2023-06-23 13:21:22 +0000250 StringProperty(pIsolate, pValue, &pEventParam->m_wsChange, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700251 break;
252 case XFA_Event::CommitKey:
kumarashishg826308d2023-06-23 13:21:22 +0000253 IntegerProperty(pIsolate, pValue, &pEventParam->m_iCommitKey, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700254 break;
255 case XFA_Event::FullText:
kumarashishg826308d2023-06-23 13:21:22 +0000256 StringProperty(pIsolate, pValue, &pEventParam->m_wsFullText, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700257 break;
258 case XFA_Event::Keydown:
kumarashishg826308d2023-06-23 13:21:22 +0000259 BooleanProperty(pIsolate, pValue, &pEventParam->m_bKeyDown, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700260 break;
261 case XFA_Event::Modifier:
kumarashishg826308d2023-06-23 13:21:22 +0000262 BooleanProperty(pIsolate, pValue, &pEventParam->m_bModifier, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700263 break;
264 case XFA_Event::NewContentType:
kumarashishg826308d2023-06-23 13:21:22 +0000265 StringProperty(pIsolate, pValue, &pEventParam->m_wsNewContentType,
266 bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700267 break;
268 case XFA_Event::NewText:
Haibo Huang49cc9302020-04-27 16:14:24 -0700269 NOTREACHED();
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700270 break;
271 case XFA_Event::PreviousContentType:
kumarashishg826308d2023-06-23 13:21:22 +0000272 StringProperty(pIsolate, pValue, &pEventParam->m_wsPrevContentType,
273 bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700274 break;
275 case XFA_Event::PreviousText:
kumarashishg826308d2023-06-23 13:21:22 +0000276 StringProperty(pIsolate, pValue, &pEventParam->m_wsPrevText, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700277 break;
278 case XFA_Event::Reenter:
kumarashishg826308d2023-06-23 13:21:22 +0000279 BooleanProperty(pIsolate, pValue, &pEventParam->m_bReenter, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700280 break;
281 case XFA_Event::SelectionEnd:
kumarashishg826308d2023-06-23 13:21:22 +0000282 IntegerProperty(pIsolate, pValue, &pEventParam->m_iSelEnd, bSetting);
Haibo Huang49cc9302020-04-27 16:14:24 -0700283
284 pEventParam->m_iSelEnd = std::max(0, pEventParam->m_iSelEnd);
kumarashishg826308d2023-06-23 13:21:22 +0000285 pEventParam->m_iSelEnd = std::min(
286 pEventParam->m_iSelEnd, pdfium::base::checked_cast<int32_t>(
287 pEventParam->m_wsPrevText.GetLength()));
Haibo Huang49cc9302020-04-27 16:14:24 -0700288 pEventParam->m_iSelStart =
289 std::min(pEventParam->m_iSelStart, pEventParam->m_iSelEnd);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700290 break;
291 case XFA_Event::SelectionStart:
kumarashishg826308d2023-06-23 13:21:22 +0000292 IntegerProperty(pIsolate, pValue, &pEventParam->m_iSelStart, bSetting);
Haibo Huang49cc9302020-04-27 16:14:24 -0700293 pEventParam->m_iSelStart = std::max(0, pEventParam->m_iSelStart);
kumarashishg826308d2023-06-23 13:21:22 +0000294 pEventParam->m_iSelStart = std::min(
295 pEventParam->m_iSelStart, pdfium::base::checked_cast<int32_t>(
296 pEventParam->m_wsPrevText.GetLength()));
Haibo Huang49cc9302020-04-27 16:14:24 -0700297 pEventParam->m_iSelEnd =
298 std::max(pEventParam->m_iSelStart, pEventParam->m_iSelEnd);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700299 break;
300 case XFA_Event::Shift:
kumarashishg826308d2023-06-23 13:21:22 +0000301 BooleanProperty(pIsolate, pValue, &pEventParam->m_bShift, bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700302 break;
303 case XFA_Event::SoapFaultCode:
kumarashishg826308d2023-06-23 13:21:22 +0000304 StringProperty(pIsolate, pValue, &pEventParam->m_wsSoapFaultCode,
305 bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700306 break;
307 case XFA_Event::SoapFaultString:
kumarashishg826308d2023-06-23 13:21:22 +0000308 StringProperty(pIsolate, pValue, &pEventParam->m_wsSoapFaultString,
309 bSetting);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700310 break;
311 case XFA_Event::Target:
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700312 default:
313 break;
314 }
315}