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_eventpseudomodel.h" 8 9 #include <vector> 10 11 #include "fxjs/cfxjse_engine.h" 12 #include "fxjs/cfxjse_value.h" 13 #include "xfa/fxfa/cxfa_eventparam.h" 14 #include "xfa/fxfa/cxfa_ffnotify.h" 15 #include "xfa/fxfa/cxfa_ffwidgethandler.h" 16 #include "xfa/fxfa/parser/cscript_eventpseudomodel.h" 17 18 namespace { 19 20 void StringProperty(CFXJSE_Value* pValue, WideString& wsValue, bool bSetting) { 21 if (bSetting) { 22 wsValue = pValue->ToWideString(); 23 return; 24 } 25 pValue->SetString(wsValue.UTF8Encode().AsStringView()); 26 } 27 28 void InterProperty(CFXJSE_Value* pValue, int32_t& iValue, bool bSetting) { 29 if (bSetting) { 30 iValue = pValue->ToInteger(); 31 return; 32 } 33 pValue->SetInteger(iValue); 34 } 35 36 void BooleanProperty(CFXJSE_Value* pValue, bool& bValue, bool bSetting) { 37 if (bSetting) { 38 bValue = pValue->ToBoolean(); 39 return; 40 } 41 pValue->SetBoolean(bValue); 42 } 43 44 } // namespace 45 46 const CJX_MethodSpec CJX_EventPseudoModel::MethodSpecs[] = { 47 {"emit", emit_static}, 48 {"reset", reset_static}}; 49 50 CJX_EventPseudoModel::CJX_EventPseudoModel(CScript_EventPseudoModel* model) 51 : CJX_Object(model) { 52 DefineMethods(MethodSpecs, FX_ArraySize(MethodSpecs)); 53 } 54 55 CJX_EventPseudoModel::~CJX_EventPseudoModel() {} 56 57 void CJX_EventPseudoModel::change(CFXJSE_Value* pValue, 58 bool bSetting, 59 XFA_Attribute eAttribute) { 60 Property(pValue, XFA_Event::Change, bSetting); 61 } 62 63 void CJX_EventPseudoModel::commitKey(CFXJSE_Value* pValue, 64 bool bSetting, 65 XFA_Attribute eAttribute) { 66 Property(pValue, XFA_Event::CommitKey, bSetting); 67 } 68 69 void CJX_EventPseudoModel::fullText(CFXJSE_Value* pValue, 70 bool bSetting, 71 XFA_Attribute eAttribute) { 72 Property(pValue, XFA_Event::FullText, bSetting); 73 } 74 75 void CJX_EventPseudoModel::keyDown(CFXJSE_Value* pValue, 76 bool bSetting, 77 XFA_Attribute eAttribute) { 78 Property(pValue, XFA_Event::Keydown, bSetting); 79 } 80 81 void CJX_EventPseudoModel::modifier(CFXJSE_Value* pValue, 82 bool bSetting, 83 XFA_Attribute eAttribute) { 84 Property(pValue, XFA_Event::Modifier, bSetting); 85 } 86 87 void CJX_EventPseudoModel::newContentType(CFXJSE_Value* pValue, 88 bool bSetting, 89 XFA_Attribute eAttribute) { 90 Property(pValue, XFA_Event::NewContentType, bSetting); 91 } 92 93 void CJX_EventPseudoModel::newText(CFXJSE_Value* pValue, 94 bool bSetting, 95 XFA_Attribute eAttribute) { 96 Property(pValue, XFA_Event::NewText, bSetting); 97 } 98 99 void CJX_EventPseudoModel::prevContentType(CFXJSE_Value* pValue, 100 bool bSetting, 101 XFA_Attribute eAttribute) { 102 Property(pValue, XFA_Event::PreviousContentType, bSetting); 103 } 104 105 void CJX_EventPseudoModel::prevText(CFXJSE_Value* pValue, 106 bool bSetting, 107 XFA_Attribute eAttribute) { 108 Property(pValue, XFA_Event::PreviousText, bSetting); 109 } 110 111 void CJX_EventPseudoModel::reenter(CFXJSE_Value* pValue, 112 bool bSetting, 113 XFA_Attribute eAttribute) { 114 Property(pValue, XFA_Event::Reenter, bSetting); 115 } 116 117 void CJX_EventPseudoModel::selEnd(CFXJSE_Value* pValue, 118 bool bSetting, 119 XFA_Attribute eAttribute) { 120 Property(pValue, XFA_Event::SelectionEnd, bSetting); 121 } 122 123 void CJX_EventPseudoModel::selStart(CFXJSE_Value* pValue, 124 bool bSetting, 125 XFA_Attribute eAttribute) { 126 Property(pValue, XFA_Event::SelectionStart, bSetting); 127 } 128 129 void CJX_EventPseudoModel::shift(CFXJSE_Value* pValue, 130 bool bSetting, 131 XFA_Attribute eAttribute) { 132 Property(pValue, XFA_Event::Shift, bSetting); 133 } 134 135 void CJX_EventPseudoModel::soapFaultCode(CFXJSE_Value* pValue, 136 bool bSetting, 137 XFA_Attribute eAttribute) { 138 Property(pValue, XFA_Event::SoapFaultCode, bSetting); 139 } 140 141 void CJX_EventPseudoModel::soapFaultString(CFXJSE_Value* pValue, 142 bool bSetting, 143 XFA_Attribute eAttribute) { 144 Property(pValue, XFA_Event::SoapFaultString, bSetting); 145 } 146 147 void CJX_EventPseudoModel::target(CFXJSE_Value* pValue, 148 bool bSetting, 149 XFA_Attribute eAttribute) { 150 Property(pValue, XFA_Event::Target, bSetting); 151 } 152 153 CJS_Return CJX_EventPseudoModel::emit( 154 CJS_V8* runtime, 155 const std::vector<v8::Local<v8::Value>>& params) { 156 CFXJSE_Engine* pScriptContext = GetDocument()->GetScriptContext(); 157 if (!pScriptContext) 158 return CJS_Return(true); 159 160 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam(); 161 if (!pEventParam) 162 return CJS_Return(true); 163 164 CXFA_FFNotify* pNotify = GetDocument()->GetNotify(); 165 if (!pNotify) 166 return CJS_Return(true); 167 168 CXFA_FFWidgetHandler* pWidgetHandler = pNotify->GetWidgetHandler(); 169 if (!pWidgetHandler) 170 return CJS_Return(true); 171 172 pWidgetHandler->ProcessEvent(pEventParam->m_pTarget, pEventParam); 173 return CJS_Return(true); 174 } 175 176 CJS_Return CJX_EventPseudoModel::reset( 177 CJS_V8* runtime, 178 const std::vector<v8::Local<v8::Value>>& params) { 179 CFXJSE_Engine* pScriptContext = GetDocument()->GetScriptContext(); 180 if (!pScriptContext) 181 return CJS_Return(true); 182 183 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam(); 184 if (!pEventParam) 185 return CJS_Return(true); 186 187 pEventParam->Reset(); 188 return CJS_Return(true); 189 } 190 191 void CJX_EventPseudoModel::Property(CFXJSE_Value* pValue, 192 XFA_Event dwFlag, 193 bool bSetting) { 194 CFXJSE_Engine* pScriptContext = GetDocument()->GetScriptContext(); 195 if (!pScriptContext) 196 return; 197 198 CXFA_EventParam* pEventParam = pScriptContext->GetEventParam(); 199 if (!pEventParam) 200 return; 201 202 switch (dwFlag) { 203 case XFA_Event::CancelAction: 204 BooleanProperty(pValue, pEventParam->m_bCancelAction, bSetting); 205 break; 206 case XFA_Event::Change: 207 StringProperty(pValue, pEventParam->m_wsChange, bSetting); 208 break; 209 case XFA_Event::CommitKey: 210 InterProperty(pValue, pEventParam->m_iCommitKey, bSetting); 211 break; 212 case XFA_Event::FullText: 213 StringProperty(pValue, pEventParam->m_wsFullText, bSetting); 214 break; 215 case XFA_Event::Keydown: 216 BooleanProperty(pValue, pEventParam->m_bKeyDown, bSetting); 217 break; 218 case XFA_Event::Modifier: 219 BooleanProperty(pValue, pEventParam->m_bModifier, bSetting); 220 break; 221 case XFA_Event::NewContentType: 222 StringProperty(pValue, pEventParam->m_wsNewContentType, bSetting); 223 break; 224 case XFA_Event::NewText: 225 StringProperty(pValue, pEventParam->m_wsNewText, bSetting); 226 break; 227 case XFA_Event::PreviousContentType: 228 StringProperty(pValue, pEventParam->m_wsPrevContentType, bSetting); 229 break; 230 case XFA_Event::PreviousText: 231 StringProperty(pValue, pEventParam->m_wsPrevText, bSetting); 232 break; 233 case XFA_Event::Reenter: 234 BooleanProperty(pValue, pEventParam->m_bReenter, bSetting); 235 break; 236 case XFA_Event::SelectionEnd: 237 InterProperty(pValue, pEventParam->m_iSelEnd, bSetting); 238 break; 239 case XFA_Event::SelectionStart: 240 InterProperty(pValue, pEventParam->m_iSelStart, bSetting); 241 break; 242 case XFA_Event::Shift: 243 BooleanProperty(pValue, pEventParam->m_bShift, bSetting); 244 break; 245 case XFA_Event::SoapFaultCode: 246 StringProperty(pValue, pEventParam->m_wsSoapFaultCode, bSetting); 247 break; 248 case XFA_Event::SoapFaultString: 249 StringProperty(pValue, pEventParam->m_wsSoapFaultString, bSetting); 250 break; 251 case XFA_Event::Target: 252 break; 253 default: 254 break; 255 } 256 } 257