1 // Copyright 2014 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 FPDFSDK_FPDFXFA_CPDFXFA_CONTEXT_H_
8 #define FPDFSDK_FPDFXFA_CPDFXFA_CONTEXT_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fpdfapi/parser/cpdf_document.h"
14 #include "core/fxcrt/cfx_timer.h"
15 #include "core/fxcrt/fx_system.h"
16 #include "core/fxcrt/observed_ptr.h"
17 #include "core/fxcrt/unowned_ptr.h"
18 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
19 #include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
20 #include "fxjs/gc/heap.h"
21 #include "v8/include/cppgc/persistent.h"
22 #include "xfa/fxfa/cxfa_ffdoc.h"
23 
24 class CFX_XMLDocument;
25 class CJS_Runtime;
26 class CPDFXFA_DocEnvironment;
27 
28 enum LoadStatus {
29   FXFA_LOADSTATUS_PRELOAD = 0,
30   FXFA_LOADSTATUS_LOADING,
31   FXFA_LOADSTATUS_LOADED,
32   FXFA_LOADSTATUS_CLOSING,
33 };
34 
35 // Per-process initializations.
36 void CPDFXFA_ModuleInit();
37 void CPDFXFA_ModuleDestroy();
38 
39 class CPDFXFA_Context final : public CPDF_Document::Extension,
40                               public IXFA_AppProvider {
41  public:
42   explicit CPDFXFA_Context(CPDF_Document* pPDFDoc);
43   ~CPDFXFA_Context() override;
44 
45   bool LoadXFADoc();
GetLoadStatus()46   LoadStatus GetLoadStatus() const { return m_nLoadStatus; }
GetFormType()47   FormType GetFormType() const { return m_FormType; }
GetOriginalPageCount()48   int GetOriginalPageCount() const { return m_nPageCount; }
SetOriginalPageCount(int count)49   void SetOriginalPageCount(int count) {
50     m_nPageCount = count;
51     m_XFAPageList.resize(count);
52   }
53 
GetXMLDoc()54   CFX_XMLDocument* GetXMLDoc() { return m_pXML.get(); }
GetXFADoc()55   CXFA_FFDoc* GetXFADoc() { return m_pXFADoc; }
GetXFADocView()56   CXFA_FFDocView* GetXFADocView() const { return m_pXFADocView.Get(); }
GetFormFillEnv()57   CPDFSDK_FormFillEnvironment* GetFormFillEnv() const {
58     return m_pFormFillEnv.Get();
59   }
60   void SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv);
61   RetainPtr<CPDFXFA_Page> GetXFAPage(int page_index);
62   RetainPtr<CPDFXFA_Page> GetXFAPage(CXFA_FFPageView* pPage) const;
GetXFAPageList()63   std::vector<RetainPtr<CPDFXFA_Page>>* GetXFAPageList() {
64     return &m_XFAPageList;
65   }
66   void ClearChangeMark();
67 
68   // CPDF_Document::Extension:
69   CPDF_Document* GetPDFDoc() const override;
70   int GetPageCount() const override;
71   void DeletePage(int page_index) override;
72   uint32_t GetUserPermissions() const override;
73   bool ContainsExtensionForm() const override;
74   bool ContainsExtensionFullForm() const override;
75   bool ContainsExtensionForegroundForm() const override;
76 
77   // IFXA_AppProvider:
78   WideString GetLanguage() override;
79   WideString GetPlatform() override;
80   WideString GetAppName() override;
81   WideString GetAppTitle() const override;
82   void Beep(uint32_t dwType) override;
83   int32_t MsgBox(const WideString& wsMessage,
84                  const WideString& wsTitle,
85                  uint32_t dwIconType,
86                  uint32_t dwButtonType) override;
87   WideString Response(const WideString& wsQuestion,
88                       const WideString& wsTitle,
89                       const WideString& wsDefaultAnswer,
90                       bool bMark) override;
91   RetainPtr<IFX_SeekableReadStream> DownloadURL(
92       const WideString& wsURL) override;
93   bool PostRequestURL(const WideString& wsURL,
94                       const WideString& wsData,
95                       const WideString& wsContentType,
96                       const WideString& wsEncode,
97                       const WideString& wsHeader,
98                       WideString& wsResponse) override;
99   bool PutRequestURL(const WideString& wsURL,
100                      const WideString& wsData,
101                      const WideString& wsEncode) override;
102   CFX_Timer::HandlerIface* GetTimerHandler() const override;
103   cppgc::Heap* GetGCHeap() const override;
104 
105   bool SaveDatasetsPackage(const RetainPtr<IFX_SeekableStream>& pStream);
106   bool SaveFormPackage(const RetainPtr<IFX_SeekableStream>& pStream);
107   void SendPostSaveToXFADoc();
108   void SendPreSaveToXFADoc(
109       std::vector<RetainPtr<IFX_SeekableStream>>* fileList);
110 
111  private:
112   CJS_Runtime* GetCJSRuntime() const;
113   bool SavePackage(const RetainPtr<IFX_SeekableStream>& pStream,
114                    XFA_HashCode code);
115 
116   FormType m_FormType = FormType::kNone;
117   LoadStatus m_nLoadStatus = FXFA_LOADSTATUS_PRELOAD;
118   int m_nPageCount = 0;
119 
120   // The order in which the following members are destroyed is critical.
121   UnownedPtr<CPDF_Document> const m_pPDFDoc;
122   std::unique_ptr<CFX_XMLDocument> m_pXML;
123   ObservedPtr<CPDFSDK_FormFillEnvironment> m_pFormFillEnv;
124   std::vector<RetainPtr<CPDFXFA_Page>> m_XFAPageList;
125 
126   // Can't outlive |m_pFormFillEnv|.
127   std::unique_ptr<CPDFXFA_DocEnvironment> m_pDocEnv;
128 
129   FXGCScopedHeap m_pGCHeap;
130   cppgc::Persistent<CXFA_FFApp> m_pXFAApp;          // can't outlive |m_pGCHeap|
131   cppgc::Persistent<CXFA_FFDoc> m_pXFADoc;          // Can't outlive |m_pGCHeap|
132   cppgc::Persistent<CXFA_FFDocView> m_pXFADocView;  // Can't outlive |m_pGCHeap|
133 };
134 
135 #endif  // FPDFSDK_FPDFXFA_CPDFXFA_CONTEXT_H_
136