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