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 FPDFSDK_FPDFXFA_CPDFXFA_CONTEXT_H_ 8 #define FPDFSDK_FPDFXFA_CPDFXFA_CONTEXT_H_ 9 10 #include <stdint.h> 11 12 #include <memory> 13 #include <vector> 14 15 #include "core/fpdfapi/parser/cpdf_document.h" 16 #include "core/fxcrt/cfx_timer.h" 17 #include "core/fxcrt/observed_ptr.h" 18 #include "core/fxcrt/retain_ptr.h" 19 #include "core/fxcrt/unowned_ptr.h" 20 #include "fpdfsdk/cpdfsdk_formfillenvironment.h" 21 #include "fpdfsdk/fpdfxfa/cpdfxfa_page.h" 22 #include "fxjs/gc/heap.h" 23 #include "v8/include/cppgc/persistent.h" 24 #include "xfa/fxfa/cxfa_ffapp.h" 25 #include "xfa/fxfa/cxfa_ffdoc.h" 26 27 class CFX_XMLDocument; 28 class CJS_Runtime; 29 class CPDFXFA_DocEnvironment; 30 31 // Per-process initializations. 32 void CPDFXFA_ModuleInit(); 33 void CPDFXFA_ModuleDestroy(); 34 35 class CPDFXFA_Context final : public CPDF_Document::Extension, 36 public CXFA_FFApp::CallbackIface { 37 public: 38 enum class LoadStatus : uint8_t { 39 kPreload = 0, 40 kLoading, 41 kLoaded, 42 kClosing, 43 }; 44 45 explicit CPDFXFA_Context(CPDF_Document* pPDFDoc); 46 ~CPDFXFA_Context() override; 47 48 bool LoadXFADoc(); GetLoadStatus()49 LoadStatus GetLoadStatus() const { return m_nLoadStatus; } GetFormType()50 FormType GetFormType() const { return m_FormType; } GetOriginalPageCount()51 int GetOriginalPageCount() const { return m_nPageCount; } SetOriginalPageCount(int count)52 void SetOriginalPageCount(int count) { 53 m_nPageCount = count; 54 m_XFAPageList.resize(count); 55 } 56 GetPDFDoc()57 CPDF_Document* GetPDFDoc() const { return m_pPDFDoc; } GetXMLDoc()58 CFX_XMLDocument* GetXMLDoc() { return m_pXML.get(); } GetXFADoc()59 CXFA_FFDoc* GetXFADoc() { return m_pXFADoc; } GetXFADocView()60 CXFA_FFDocView* GetXFADocView() const { return m_pXFADocView.Get(); } GetFormFillEnv()61 CPDFSDK_FormFillEnvironment* GetFormFillEnv() const { 62 return m_pFormFillEnv.Get(); 63 } 64 void SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv); 65 RetainPtr<CPDFXFA_Page> GetOrCreateXFAPage(int page_index); 66 RetainPtr<CPDFXFA_Page> GetXFAPage(int page_index); 67 RetainPtr<CPDFXFA_Page> GetXFAPage(CXFA_FFPageView* pPage) const; 68 void ClearChangeMark(); 69 70 // CPDF_Document::Extension: 71 int GetPageCount() const override; 72 uint32_t DeletePage(int page_index) override; 73 bool ContainsExtensionForm() const override; 74 bool ContainsExtensionFullForm() const override; 75 bool ContainsExtensionForegroundForm() const override; 76 77 // CXFA_FFApp::CallbackIface: 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 = LoadStatus::kPreload; 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