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 #ifndef FXJS_XFA_CJX_OBJECT_H_ 8 #define FXJS_XFA_CJX_OBJECT_H_ 9 10 #include <map> 11 #include <memory> 12 #include <utility> 13 #include <vector> 14 15 #include "core/fxcrt/unowned_ptr.h" 16 #include "core/fxcrt/widestring.h" 17 #include "core/fxcrt/xml/cfx_xmlelement.h" 18 #include "fxjs/CJX_Define.h" 19 #include "third_party/base/optional.h" 20 #include "xfa/fxfa/fxfa_basic.h" 21 22 class CFXJSE_Value; 23 class CJS_V8; 24 class CXFA_CalcData; 25 class CXFA_Document; 26 class CXFA_LayoutItem; 27 class CXFA_Node; 28 class CXFA_Object; 29 struct XFA_MAPMODULEDATA; 30 31 typedef CJS_Return (*CJX_MethodCall)( 32 CJX_Object* obj, 33 CJS_V8* runtime, 34 const std::vector<v8::Local<v8::Value>>& params); 35 struct CJX_MethodSpec { 36 const char* pName; 37 CJX_MethodCall pMethodCall; 38 }; 39 40 typedef void (*PD_CALLBACK_FREEDATA)(void* pData); 41 typedef void (*PD_CALLBACK_DUPLICATEDATA)(void*& pData); 42 43 struct XFA_MAPDATABLOCKCALLBACKINFO { 44 PD_CALLBACK_FREEDATA pFree; 45 PD_CALLBACK_DUPLICATEDATA pCopy; 46 }; 47 48 enum XFA_SOM_MESSAGETYPE { 49 XFA_SOM_ValidationMessage, 50 XFA_SOM_FormatMessage, 51 XFA_SOM_MandatoryMessage 52 }; 53 54 class CJX_Object { 55 public: 56 explicit CJX_Object(CXFA_Object* obj); 57 virtual ~CJX_Object(); 58 59 JS_PROP(className); 60 GetXFAObject()61 CXFA_Object* GetXFAObject() { return object_.Get(); } GetXFAObject()62 const CXFA_Object* GetXFAObject() const { return object_.Get(); } 63 64 CXFA_Document* GetDocument() const; 65 SetCalcRecursionCount(size_t count)66 void SetCalcRecursionCount(size_t count) { calc_recursion_count_ = count; } GetCalcRecursionCount()67 size_t GetCalcRecursionCount() const { return calc_recursion_count_; } 68 SetLayoutItem(CXFA_LayoutItem * item)69 void SetLayoutItem(CXFA_LayoutItem* item) { layout_item_ = item; } GetLayoutItem()70 CXFA_LayoutItem* GetLayoutItem() const { return layout_item_.Get(); } 71 72 bool HasMethod(const WideString& func) const; 73 CJS_Return RunMethod(const WideString& func, 74 const std::vector<v8::Local<v8::Value>>& params); 75 76 bool HasAttribute(XFA_Attribute eAttr); 77 bool SetAttribute(XFA_Attribute eAttr, 78 const WideStringView& wsValue, 79 bool bNotify); 80 bool SetAttribute(const WideStringView& wsAttr, 81 const WideStringView& wsValue, 82 bool bNotify); 83 void RemoveAttribute(const WideStringView& wsAttr); 84 WideString GetAttribute(const WideStringView& attr); 85 WideString GetAttribute(XFA_Attribute attr); 86 Optional<WideString> TryAttribute(const WideStringView& wsAttr, 87 bool bUseDefault); 88 Optional<WideString> TryAttribute(XFA_Attribute eAttr, bool bUseDefault); 89 90 Optional<WideString> TryContent(bool bScriptModify, bool bProto); 91 bool SetContent(const WideString& wsContent, 92 const WideString& wsXMLValue, 93 bool bNotify, 94 bool bScriptModify, 95 bool bSyncData); 96 WideString GetContent(bool bScriptModify); 97 98 template <typename T> GetProperty(int32_t index,XFA_Element eType)99 T* GetProperty(int32_t index, XFA_Element eType) const { 100 CXFA_Node* node; 101 int32_t count; 102 std::tie(node, count) = GetPropertyInternal(index, eType); 103 return static_cast<T*>(node); 104 } 105 template <typename T> GetOrCreateProperty(int32_t index,XFA_Element eType)106 T* GetOrCreateProperty(int32_t index, XFA_Element eType) { 107 return static_cast<T*>(GetOrCreatePropertyInternal(index, eType)); 108 } 109 110 void SetAttributeValue(const WideString& wsValue, 111 const WideString& wsXMLValue, 112 bool bNotify, 113 bool bScriptModify); 114 115 void Script_Attribute_String(CFXJSE_Value* pValue, 116 bool bSetting, 117 XFA_Attribute eAttribute); 118 void Script_Attribute_BOOL(CFXJSE_Value* pValue, 119 bool bSetting, 120 XFA_Attribute eAttribute); 121 void Script_Attribute_Integer(CFXJSE_Value* pValue, 122 bool bSetting, 123 XFA_Attribute eAttribute); 124 125 void Script_Som_FontColor(CFXJSE_Value* pValue, 126 bool bSetting, 127 XFA_Attribute eAttribute); 128 void Script_Som_FillColor(CFXJSE_Value* pValue, 129 bool bSetting, 130 XFA_Attribute eAttribute); 131 void Script_Som_BorderColor(CFXJSE_Value* pValue, 132 bool bSetting, 133 XFA_Attribute eAttribute); 134 void Script_Som_BorderWidth(CFXJSE_Value* pValue, 135 bool bSetting, 136 XFA_Attribute eAttribute); 137 void Script_Som_ValidationMessage(CFXJSE_Value* pValue, 138 bool bSetting, 139 XFA_Attribute eAttribute); 140 void Script_Som_MandatoryMessage(CFXJSE_Value* pValue, 141 bool bSetting, 142 XFA_Attribute eAttribute); 143 void Script_Field_Length(CFXJSE_Value* pValue, 144 bool bSetting, 145 XFA_Attribute eAttribute); 146 void Script_Som_DefaultValue(CFXJSE_Value* pValue, 147 bool bSetting, 148 XFA_Attribute eAttribute); 149 void Script_Som_DefaultValue_Read(CFXJSE_Value* pValue, 150 bool bSetting, 151 XFA_Attribute eAttribute); 152 void Script_Som_DataNode(CFXJSE_Value* pValue, 153 bool bSetting, 154 XFA_Attribute eAttribute); 155 void Script_Som_Mandatory(CFXJSE_Value* pValue, 156 bool bSetting, 157 XFA_Attribute eAttribute); 158 void Script_Som_InstanceIndex(CFXJSE_Value* pValue, 159 bool bSetting, 160 XFA_Attribute eAttribute); 161 void Script_Som_Message(CFXJSE_Value* pValue, 162 bool bSetting, 163 XFA_SOM_MESSAGETYPE iMessageType); 164 void Script_Subform_InstanceManager(CFXJSE_Value* pValue, 165 bool bSetting, 166 XFA_AttributeEnum eAttribute); 167 void Script_SubmitFormat_Mode(CFXJSE_Value* pValue, 168 bool bSetting, 169 XFA_Attribute eAttribute); 170 void Script_Form_Checksum(CFXJSE_Value* pValue, 171 bool bSetting, 172 XFA_Attribute eAttribute); 173 void Script_ExclGroup_ErrorText(CFXJSE_Value* pValue, 174 bool bSetting, 175 XFA_Attribute eAttribute); 176 177 Optional<WideString> TryNamespace(); 178 179 Optional<int32_t> TryInteger(XFA_Attribute eAttr, bool bUseDefault); 180 bool SetInteger(XFA_Attribute eAttr, int32_t iValue, bool bNotify); 181 int32_t GetInteger(XFA_Attribute eAttr); 182 183 Optional<WideString> TryCData(XFA_Attribute eAttr, bool bUseDefault); 184 bool SetCData(XFA_Attribute eAttr, 185 const WideString& wsValue, 186 bool bNotify, 187 bool bScriptModify); 188 WideString GetCData(XFA_Attribute eAttr); 189 190 Optional<XFA_AttributeEnum> TryEnum(XFA_Attribute eAttr, bool bUseDefault); 191 bool SetEnum(XFA_Attribute eAttr, XFA_AttributeEnum eValue, bool bNotify); 192 XFA_AttributeEnum GetEnum(XFA_Attribute eAttr); 193 194 Optional<bool> TryBoolean(XFA_Attribute eAttr, bool bUseDefault); 195 bool SetBoolean(XFA_Attribute eAttr, bool bValue, bool bNotify); 196 bool GetBoolean(XFA_Attribute eAttr); 197 198 Optional<CXFA_Measurement> TryMeasure(XFA_Attribute eAttr, 199 bool bUseDefault) const; 200 Optional<float> TryMeasureAsFloat(XFA_Attribute attr) const; 201 bool SetMeasure(XFA_Attribute eAttr, CXFA_Measurement mValue, bool bNotify); 202 CXFA_Measurement GetMeasure(XFA_Attribute eAttr) const; 203 204 void MergeAllData(CXFA_Object* pDstModule); 205 206 void SetCalcData(std::unique_ptr<CXFA_CalcData> data); GetCalcData()207 CXFA_CalcData* GetCalcData() const { return calc_data_.get(); } 208 std::unique_ptr<CXFA_CalcData> ReleaseCalcData(); 209 210 int32_t InstanceManager_SetInstances(int32_t iDesired); 211 int32_t InstanceManager_MoveInstance(int32_t iTo, int32_t iFrom); 212 213 void ThrowInvalidPropertyException() const; 214 void ThrowArgumentMismatchException() const; 215 void ThrowIndexOutOfBoundsException() const; 216 void ThrowParamCountMismatchException(const WideString& method) const; 217 void ThrowTooManyOccurancesException(const WideString& obj) const; 218 219 protected: 220 void DefineMethods(const CJX_MethodSpec method_specs[], size_t count); 221 222 void MoveBufferMapData(CXFA_Object* pSrcModule, CXFA_Object* pDstModule); 223 void SetMapModuleString(void* pKey, const WideStringView& wsValue); 224 void ThrowException(const wchar_t* str, ...) const; 225 226 private: 227 void Script_Boolean_DefaultValue(CFXJSE_Value* pValue, 228 bool bSetting, 229 XFA_Attribute eAttribute); 230 void Script_Draw_DefaultValue(CFXJSE_Value* pValue, 231 bool bSetting, 232 XFA_Attribute eAttribute); 233 void Script_Field_DefaultValue(CFXJSE_Value* pValue, 234 bool bSetting, 235 XFA_Attribute eAttribute); 236 237 std::pair<CXFA_Node*, int32_t> GetPropertyInternal(int32_t index, 238 XFA_Element eType) const; 239 CXFA_Node* GetOrCreatePropertyInternal(int32_t index, XFA_Element eType); 240 241 void OnChanged(XFA_Attribute eAttr, bool bNotify, bool bScriptModify); 242 void OnChanging(XFA_Attribute eAttr, bool bNotify); 243 bool SetUserData(void* pKey, 244 void* pData, 245 XFA_MAPDATABLOCKCALLBACKINFO* pCallbackInfo); 246 247 // Returns a pointer to the XML node that needs to be updated with the new 248 // attribute value. |nullptr| if no update is needed. 249 CFX_XMLElement* SetValue(XFA_Attribute eAttr, 250 XFA_AttributeType eType, 251 void* pValue, 252 bool bNotify); 253 int32_t Subform_and_SubformSet_InstanceIndex(); 254 255 XFA_MAPMODULEDATA* CreateMapModuleData(); 256 XFA_MAPMODULEDATA* GetMapModuleData() const; 257 void SetMapModuleValue(void* pKey, void* pValue); 258 bool GetMapModuleValue(void* pKey, void*& pValue); 259 bool GetMapModuleString(void* pKey, WideStringView& wsValue); 260 void SetMapModuleBuffer(void* pKey, 261 void* pValue, 262 int32_t iBytes, 263 XFA_MAPDATABLOCKCALLBACKINFO* pCallbackInfo); 264 bool GetMapModuleBuffer(void* pKey, 265 void*& pValue, 266 int32_t& iBytes, 267 bool bProtoAlso) const; 268 bool HasMapModuleKey(void* pKey); 269 void ClearMapModuleBuffer(); 270 void RemoveMapModuleKey(void* pKey); 271 void MoveBufferMapData(CXFA_Object* pDstModule); 272 273 UnownedPtr<CXFA_Object> object_; 274 UnownedPtr<CXFA_LayoutItem> layout_item_; 275 std::unique_ptr<XFA_MAPMODULEDATA> map_module_data_; 276 std::unique_ptr<CXFA_CalcData> calc_data_; 277 std::map<ByteString, CJX_MethodCall> method_specs_; 278 size_t calc_recursion_count_ = 0; 279 }; 280 281 #endif // FXJS_XFA_CJX_OBJECT_H_ 282