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/widestring.h"
16 #include "fxjs/gc/heap.h"
17 #include "fxjs/xfa/fxjse.h"
18 #include "fxjs/xfa/jse_define.h"
19 #include "third_party/base/optional.h"
20 #include "third_party/base/span.h"
21 #include "v8/include/cppgc/garbage-collected.h"
22 #include "v8/include/cppgc/member.h"
23 #include "xfa/fxfa/fxfa_basic.h"
24 
25 class CFXJSE_MapModule;
26 class CFXJSE_Value;
27 class CFX_V8;
28 class CFX_XMLElement;
29 class CJX_Object;
30 class CXFA_Document;
31 class CXFA_LayoutItem;
32 class CXFA_Measurement;
33 class CXFA_Node;
34 class CXFA_Object;
35 
36 typedef CJS_Result (*CJX_MethodCall)(
37     CJX_Object* obj,
38     CFX_V8* runtime,
39     const std::vector<v8::Local<v8::Value>>& params);
40 
41 struct CJX_MethodSpec {
42   const char* pName;
43   CJX_MethodCall pMethodCall;
44 };
45 
46 enum XFA_SOM_MESSAGETYPE {
47   XFA_SOM_ValidationMessage,
48   XFA_SOM_FormatMessage,
49   XFA_SOM_MandatoryMessage
50 };
51 
52 class CJX_Object : public cppgc::GarbageCollected<CJX_Object>,
53                    public CFXJSE_HostObject {
54  public:
55   // Corresponds 1:1 with CJX_ subclasses.
56   enum class TypeTag {
57     Boolean,
58     Container,
59     DataWindow,
60     Delta,
61     Desc,
62     Draw,
63     Encrypt,
64     EventPseudoModel,
65     ExclGroup,
66     Extras,
67     Field,
68     Form,
69     Handler,
70     HostPseudoModel,
71     InstanceManager,
72     LayoutPseudoModel,
73     List,
74     LogPseudoModel,
75     Manifest,
76     Model,
77     Node,
78     Object,
79     Occur,
80     Packet,
81     Script,
82     SignaturePesudoModel,
83     Source,
84     Subform,
85     SubformSet,
86     Template,
87     TextNode,
88     Tree,
89     TreeList,
90     WsdlConnection,
91     Xfa,
92   };
93 
94   class CalcData : public cppgc::GarbageCollected<CalcData> {
95    public:
96     CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED;
97     ~CalcData();
98 
99     void Trace(cppgc::Visitor* visitor) const;
100 
101     std::vector<cppgc::Member<CXFA_Node>> m_Globals;
102 
103    private:
104     CalcData();
105   };
106 
107   CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED;
108   ~CJX_Object() override;
109 
110   // CFXJSE_HostObject:
111   CJX_Object* AsCJXObject() override;
112 
113   virtual void Trace(cppgc::Visitor* visitor) const;
114   virtual bool DynamicTypeIs(TypeTag eType) const;
115 
116   JSE_PROP(className);
117 
118   CXFA_Document* GetDocument() const;
119   CXFA_Node* GetXFANode() const;
GetXFAObject()120   CXFA_Object* GetXFAObject() const { return object_.Get(); }
121 
SetCalcRecursionCount(size_t count)122   void SetCalcRecursionCount(size_t count) { calc_recursion_count_ = count; }
GetCalcRecursionCount()123   size_t GetCalcRecursionCount() const { return calc_recursion_count_; }
124 
SetLayoutItem(CXFA_LayoutItem * item)125   void SetLayoutItem(CXFA_LayoutItem* item) { layout_item_ = item; }
GetLayoutItem()126   CXFA_LayoutItem* GetLayoutItem() const { return layout_item_; }
127 
128   bool HasMethod(const WideString& func) const;
129   CJS_Result RunMethod(const WideString& func,
130                        const std::vector<v8::Local<v8::Value>>& params);
131 
132   bool HasAttribute(XFA_Attribute eAttr);
133   void SetAttributeByEnum(XFA_Attribute eAttr,
134                           const WideString& wsValue,
135                           bool bNotify);
136   void SetAttributeByString(WideStringView wsAttr, const WideString& wsValue);
137   void RemoveAttribute(WideStringView wsAttr);
138   WideString GetAttributeByString(WideStringView attr);
139   WideString GetAttributeByEnum(XFA_Attribute attr);
140   Optional<WideString> TryAttribute(XFA_Attribute eAttr, bool bUseDefault);
141 
142   Optional<WideString> TryContent(bool bScriptModify, bool bProto);
143   void SetContent(const WideString& wsContent,
144                   const WideString& wsXMLValue,
145                   bool bNotify,
146                   bool bScriptModify,
147                   bool bSyncData);
148   WideString GetContent(bool bScriptModify);
149 
150   template <typename T>
GetProperty(int32_t index,XFA_Element eType)151   T* GetProperty(int32_t index, XFA_Element eType) const {
152     CXFA_Node* node;
153     int32_t count;
154     std::tie(node, count) = GetPropertyInternal(index, eType);
155     return static_cast<T*>(node);
156   }
157   template <typename T>
GetOrCreateProperty(int32_t index,XFA_Element eType)158   T* GetOrCreateProperty(int32_t index, XFA_Element eType) {
159     return static_cast<T*>(GetOrCreatePropertyInternal(index, eType));
160   }
161 
162   void SetAttributeValue(const WideString& wsValue,
163                          const WideString& wsXMLValue);
164 
165   // Not actual properties, but invoked as property handlers to cover
166   // a broad range of underlying properties.
167   JSE_PROP(ScriptAttributeString);
168   JSE_PROP(ScriptAttributeBool);
169   JSE_PROP(ScriptAttributeInteger);
170   JSE_PROP(ScriptSomFontColor);
171   JSE_PROP(ScriptSomFillColor);
172   JSE_PROP(ScriptSomBorderColor);
173   JSE_PROP(ScriptSomBorderWidth);
174   JSE_PROP(ScriptSomValidationMessage);
175   JSE_PROP(ScriptSomMandatoryMessage);
176   JSE_PROP(ScriptSomDefaultValue);
177   JSE_PROP(ScriptSomDefaultValue_Read);
178   JSE_PROP(ScriptSomDataNode);
179   JSE_PROP(ScriptSomMandatory);
180   JSE_PROP(ScriptSomInstanceIndex);
181   JSE_PROP(ScriptSubmitFormatMode);
182 
183   void ScriptSomMessage(v8::Isolate* pIsolate,
184                         CFXJSE_Value* pValue,
185                         bool bSetting,
186                         XFA_SOM_MESSAGETYPE iMessageType);
187 
188   Optional<WideString> TryNamespace();
189 
190   Optional<int32_t> TryInteger(XFA_Attribute eAttr, bool bUseDefault) const;
191   void SetInteger(XFA_Attribute eAttr, int32_t iValue, bool bNotify);
192   int32_t GetInteger(XFA_Attribute eAttr) const;
193 
194   Optional<WideString> TryCData(XFA_Attribute eAttr, bool bUseDefault) const;
195   void SetCData(XFA_Attribute eAttr, const WideString& wsValue);
196   WideString GetCData(XFA_Attribute eAttr) const;
197 
198   Optional<XFA_AttributeValue> TryEnum(XFA_Attribute eAttr,
199                                        bool bUseDefault) const;
200   void SetEnum(XFA_Attribute eAttr, XFA_AttributeValue eValue, bool bNotify);
201   XFA_AttributeValue GetEnum(XFA_Attribute eAttr) const;
202 
203   Optional<bool> TryBoolean(XFA_Attribute eAttr, bool bUseDefault);
204   void SetBoolean(XFA_Attribute eAttr, bool bValue, bool bNotify);
205   bool GetBoolean(XFA_Attribute eAttr);
206 
207   Optional<CXFA_Measurement> TryMeasure(XFA_Attribute eAttr,
208                                         bool bUseDefault) const;
209   Optional<float> TryMeasureAsFloat(XFA_Attribute attr) const;
210   void SetMeasure(XFA_Attribute eAttr,
211                   const CXFA_Measurement& mValue,
212                   bool bNotify);
213   CXFA_Measurement GetMeasure(XFA_Attribute eAttr) const;
214   float GetMeasureInUnit(XFA_Attribute eAttr, XFA_Unit unit) const;
215 
216   void MergeAllData(CXFA_Object* pDstObj);
217 
GetCalcData()218   CalcData* GetCalcData() const { return calc_data_; }
219   CalcData* GetOrCreateCalcData(cppgc::Heap* heap);
220   void TakeCalcDataFrom(CJX_Object* that);
221 
222   void ThrowInvalidPropertyException() const;
223   void ThrowArgumentMismatchException() const;
224   void ThrowIndexOutOfBoundsException() const;
225   void ThrowParamCountMismatchException(const WideString& method) const;
226   void ThrowTooManyOccurancesException(const WideString& obj) const;
227 
228  protected:
229   explicit CJX_Object(CXFA_Object* obj);
230 
231   void SetAttributeValueImpl(const WideString& wsValue,
232                              const WideString& wsXMLValue,
233                              bool bNotify,
234                              bool bScriptModify);
235   void SetCDataImpl(XFA_Attribute eAttr,
236                     const WideString& wsValue,
237                     bool bNotify,
238                     bool bScriptModify);
239   void DefineMethods(pdfium::span<const CJX_MethodSpec> methods);
240   void MoveBufferMapData(CXFA_Object* pSrcObj, CXFA_Object* pDstObj);
241   void ThrowException(const WideString& str) const;
242 
243  private:
244   using Type__ = CJX_Object;
245   static const TypeTag static_type__ = TypeTag::Object;
246 
247   std::pair<CXFA_Node*, int32_t> GetPropertyInternal(int32_t index,
248                                                      XFA_Element eType) const;
249   CXFA_Node* GetOrCreatePropertyInternal(int32_t index, XFA_Element eType);
250 
251   void OnChanged(XFA_Attribute eAttr, bool bNotify, bool bScriptModify);
252   void OnChanging(XFA_Attribute eAttr, bool bNotify);
253 
254   // Returns a pointer to the XML node that needs to be updated with the new
255   // attribute value. |nullptr| if no update is needed.
256   CFX_XMLElement* SetValue(XFA_Attribute eAttr, int32_t value, bool bNotify);
257   int32_t Subform_and_SubformSet_InstanceIndex();
258 
259   CFXJSE_MapModule* CreateMapModule();
260   CFXJSE_MapModule* GetMapModule() const;
261   void SetMapModuleValue(uint32_t key, int32_t value);
262   void SetMapModuleString(uint32_t key, const WideString& wsValue);
263   void SetMapModuleMeasurement(uint32_t key, const CXFA_Measurement& value);
264   Optional<int32_t> GetMapModuleValue(uint32_t key) const;
265   Optional<WideString> GetMapModuleString(uint32_t key) const;
266   Optional<CXFA_Measurement> GetMapModuleMeasurement(uint32_t key) const;
267   Optional<int32_t> GetMapModuleValueFollowingChain(uint32_t key) const;
268   Optional<WideString> GetMapModuleStringFollowingChain(uint32_t key) const;
269   Optional<CXFA_Measurement> GetMapModuleMeasurementFollowingChain(
270       uint32_t key) const;
271   bool HasMapModuleKey(uint32_t key);
272   void RemoveMapModuleKey(uint32_t key);
273   void MoveBufferMapData(CXFA_Object* pDstObj);
274 
275   cppgc::Member<CXFA_Object> object_;
276   cppgc::Member<CXFA_LayoutItem> layout_item_;
277   cppgc::Member<CalcData> calc_data_;
278   std::unique_ptr<CFXJSE_MapModule> map_module_;
279   std::map<ByteString, CJX_MethodCall> method_specs_;
280   size_t calc_recursion_count_ = 0;
281 };
282 
283 #endif  // FXJS_XFA_CJX_OBJECT_H_
284