• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 XFA_FXFA_PARSER_CXFA_DOCUMENT_H_
8 #define XFA_FXFA_PARSER_CXFA_DOCUMENT_H_
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include <map>
14 #include <memory>
15 #include <vector>
16 
17 #include "core/fxcrt/unowned_ptr.h"
18 #include "core/fxcrt/widestring.h"
19 #include "fxjs/gc/heap.h"
20 #include "third_party/abseil-cpp/absl/types/optional.h"
21 #include "third_party/base/span.h"
22 #include "v8/include/cppgc/garbage-collected.h"
23 #include "v8/include/cppgc/member.h"
24 #include "v8/include/cppgc/persistent.h"
25 #include "v8/include/cppgc/visitor.h"
26 #include "xfa/fxfa/fxfa.h"
27 #include "xfa/fxfa/fxfa_basic.h"
28 #include "xfa/fxfa/parser/cxfa_localemgr.h"
29 #include "xfa/fxfa/parser/cxfa_nodeowner.h"
30 
31 class CFXJSE_Engine;
32 class CJS_Runtime;
33 class CScript_DataWindow;
34 class CScript_EventPseudoModel;
35 class CScript_HostPseudoModel;
36 class CScript_LayoutPseudoModel;
37 class CScript_LogPseudoModel;
38 class CScript_SignaturePseudoModel;
39 class CXFA_FFNotify;
40 class CXFA_Node;
41 class CXFA_Object;
42 
43 namespace cppgc {
44 class Heap;
45 }  // namespace cppgc
46 
47 enum XFA_VERSION {
48   XFA_VERSION_UNKNOWN = 0,
49   XFA_VERSION_200 = 200,
50   XFA_VERSION_202 = 202,
51   XFA_VERSION_204 = 204,
52   XFA_VERSION_205 = 205,
53   XFA_VERSION_206 = 206,
54   XFA_VERSION_207 = 207,
55   XFA_VERSION_208 = 208,
56   XFA_VERSION_300 = 300,
57   XFA_VERSION_301 = 301,
58   XFA_VERSION_303 = 303,
59   XFA_VERSION_306 = 306,
60   XFA_VERSION_DEFAULT = XFA_VERSION_303,
61   XFA_VERSION_MIN = 200,
62   XFA_VERSION_MAX = 400,
63 };
64 
65 class CXFA_Document final : public cppgc::GarbageCollected<CXFA_Document> {
66  public:
67   class LayoutProcessorIface
68       : public cppgc::GarbageCollected<LayoutProcessorIface> {
69    public:
70     LayoutProcessorIface();
71     virtual ~LayoutProcessorIface();
72 
73     virtual void Trace(cppgc::Visitor* visitor) const;
74     virtual void SetForceRelayout() = 0;
75     virtual void SetHasChangedContainer() = 0;
76 
SetDocument(CXFA_Document * pDocument)77     void SetDocument(CXFA_Document* pDocument) { m_pDocument = pDocument; }
GetDocument()78     CXFA_Document* GetDocument() const { return m_pDocument; }
79 
80    private:
81     cppgc::Member<CXFA_Document> m_pDocument;
82   };
83 
84   CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED;
85   ~CXFA_Document();
86 
87   void Trace(cppgc::Visitor* visitor) const;
88 
HasScriptContext()89   bool HasScriptContext() const { return !!m_pScriptContext; }
90   CFXJSE_Engine* InitScriptContext(CJS_Runtime* fxjs_runtime);
91 
92   // Only safe to call in situations where the context is known to exist,
93   // and always returns non-NULL in those situations. In other words, we have
94   // to call InitScriptContext() first to avoid a situation where the context
95   // won't have an isolate set into it.
96   CFXJSE_Engine* GetScriptContext() const;
97 
GetNotify()98   CXFA_FFNotify* GetNotify() const { return notify_; }
GetNodeOwner()99   CXFA_NodeOwner* GetNodeOwner() { return node_owner_; }
100   cppgc::Heap* GetHeap() const;
101   CXFA_LocaleMgr* GetLocaleMgr();
102   CXFA_Object* GetXFAObject(XFA_HashCode wsNodeNameHash);
103   CXFA_Node* GetNodeByID(CXFA_Node* pRoot, WideStringView wsID) const;
104   CXFA_Node* GetNotBindNode(
105       pdfium::span<cppgc::Member<CXFA_Object>> arrayNodes) const;
106 
GetLayoutProcessor()107   LayoutProcessorIface* GetLayoutProcessor() const {
108     return m_pLayoutProcessor;
109   }
GetRoot()110   CXFA_Node* GetRoot() const { return m_pRootNode; }
SetRoot(CXFA_Node * pNewRoot)111   void SetRoot(CXFA_Node* pNewRoot) { m_pRootNode = pNewRoot; }
112 
is_strict_scoping()113   bool is_strict_scoping() const { return m_bStrictScoping; }
set_is_strict_scoping()114   void set_is_strict_scoping() { m_bStrictScoping = true; }
115 
is_scripting()116   bool is_scripting() const { return m_bScripting; }
set_is_scripting()117   void set_is_scripting() { m_bScripting = true; }
118 
119   bool IsInteractive();
GetCurVersionMode()120   XFA_VERSION GetCurVersionMode() const { return m_eCurVersionMode; }
121   XFA_VERSION RecognizeXFAVersionNumber(const WideString& wsTemplateNS);
122   FormType GetFormType() const;
123 
124   CXFA_Node* CreateNode(XFA_PacketType packet, XFA_Element eElement);
125 
126   void DoProtoMerge();
127   void DoDataMerge();
128   void DoDataRemerge();
129   CXFA_Node* DataMerge_CopyContainer(CXFA_Node* pTemplateNode,
130                                      CXFA_Node* pFormNode,
131                                      CXFA_Node* pDataScope,
132                                      bool bOneInstance,
133                                      bool bDataMerge,
134                                      bool bUpLevel);
135   void DataMerge_UpdateBindingRelations(CXFA_Node* pFormUpdateRoot);
136 
137   void ClearLayoutData();
138 
139   CXFA_Node* GetGlobalBinding(uint32_t dwNameHash);
140   void RegisterGlobalBinding(uint32_t dwNameHash, CXFA_Node* pDataNode);
141 
142   size_t GetPendingNodesCount() const;
143   CXFA_Node* GetPendingNodeAtIndex(size_t index) const;
144   void AppendPendingNode(CXFA_Node* node);
145   void ClearPendingNodes();
146   void SetPendingNodesUnusedAndUnbound();
147 
148  private:
149   friend class CXFA_DocumentTest_ParseXFAVersion_Test;
150   friend class CXFA_DocumentTest_ParseUseHref_Test;
151   friend class CXFA_DocumentTest_ParseUse_Test;
152 
153   static XFA_VERSION ParseXFAVersion(const WideString& wsTemplateNS);
154   static void ParseUseHref(const WideString& wsUseVal,
155                            WideStringView& wsURI,
156                            WideStringView& wsID,
157                            WideStringView& wsSOM);
158   static void ParseUse(const WideString& wsUseVal,
159                        WideStringView& wsID,
160                        WideStringView& wsSOM);
161 
162   CXFA_Document(CXFA_FFNotify* notify,
163                 cppgc::Heap* heap,
164                 LayoutProcessorIface* pLayout);
165 
166   UnownedPtr<cppgc::Heap> heap_;
167   cppgc::Member<CXFA_FFNotify> const notify_;
168   cppgc::Member<CXFA_NodeOwner> const node_owner_;
169   cppgc::Member<CXFA_Node> m_pRootNode;
170   std::unique_ptr<CFXJSE_Engine> m_pScriptContext;
171   cppgc::Member<LayoutProcessorIface> m_pLayoutProcessor;
172   cppgc::Member<CXFA_LocaleMgr> m_pLocaleMgr;
173   cppgc::Member<CScript_DataWindow> m_pScriptDataWindow;
174   cppgc::Member<CScript_EventPseudoModel> m_pScriptEvent;
175   cppgc::Member<CScript_HostPseudoModel> m_pScriptHost;
176   cppgc::Member<CScript_LogPseudoModel> m_pScriptLog;
177   cppgc::Member<CScript_LayoutPseudoModel> m_pScriptLayout;
178   cppgc::Member<CScript_SignaturePseudoModel> m_pScriptSignature;
179   std::map<uint32_t, cppgc::Member<CXFA_Node>> m_rgGlobalBinding;
180   std::vector<cppgc::Member<CXFA_Node>> m_pPendingPageSet;
181   XFA_VERSION m_eCurVersionMode = XFA_VERSION_DEFAULT;
182   absl::optional<bool> m_Interactive;
183   bool m_bStrictScoping = false;
184   bool m_bScripting = false;
185 };
186 
187 #endif  // XFA_FXFA_PARSER_CXFA_DOCUMENT_H_
188