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 void DeletePage(int page_index) override; 73 uint32_t GetUserPermissions() const override; 74 bool ContainsExtensionForm() const override; 75 bool ContainsExtensionFullForm() const override; 76 bool ContainsExtensionForegroundForm() const override; 77 78 // CXFA_FFApp::CallbackIface: 79 WideString GetLanguage() override; 80 WideString GetPlatform() override; 81 WideString GetAppName() override; 82 WideString GetAppTitle() const override; 83 void Beep(uint32_t dwType) override; 84 int32_t MsgBox(const WideString& wsMessage, 85 const WideString& wsTitle, 86 uint32_t dwIconType, 87 uint32_t dwButtonType) override; 88 WideString Response(const WideString& wsQuestion, 89 const WideString& wsTitle, 90 const WideString& wsDefaultAnswer, 91 bool bMark) override; 92 RetainPtr<IFX_SeekableReadStream> DownloadURL( 93 const WideString& wsURL) override; 94 bool PostRequestURL(const WideString& wsURL, 95 const WideString& wsData, 96 const WideString& wsContentType, 97 const WideString& wsEncode, 98 const WideString& wsHeader, 99 WideString& wsResponse) override; 100 bool PutRequestURL(const WideString& wsURL, 101 const WideString& wsData, 102 const WideString& wsEncode) override; 103 CFX_Timer::HandlerIface* GetTimerHandler() const override; 104 cppgc::Heap* GetGCHeap() const override; 105 106 bool SaveDatasetsPackage(const RetainPtr<IFX_SeekableStream>& pStream); 107 bool SaveFormPackage(const RetainPtr<IFX_SeekableStream>& pStream); 108 void SendPostSaveToXFADoc(); 109 void SendPreSaveToXFADoc( 110 std::vector<RetainPtr<IFX_SeekableStream>>* fileList); 111 112 private: 113 CJS_Runtime* GetCJSRuntime() const; 114 bool SavePackage(const RetainPtr<IFX_SeekableStream>& pStream, 115 XFA_HashCode code); 116 117 FormType m_FormType = FormType::kNone; 118 LoadStatus m_nLoadStatus = LoadStatus::kPreload; 119 int m_nPageCount = 0; 120 121 // The order in which the following members are destroyed is critical. 122 UnownedPtr<CPDF_Document> const m_pPDFDoc; 123 std::unique_ptr<CFX_XMLDocument> m_pXML; 124 ObservedPtr<CPDFSDK_FormFillEnvironment> m_pFormFillEnv; 125 std::vector<RetainPtr<CPDFXFA_Page>> m_XFAPageList; 126 127 // Can't outlive |m_pFormFillEnv|. 128 std::unique_ptr<CPDFXFA_DocEnvironment> m_pDocEnv; 129 130 FXGCScopedHeap m_pGCHeap; 131 cppgc::Persistent<CXFA_FFApp> m_pXFAApp; // can't outlive |m_pGCHeap| 132 cppgc::Persistent<CXFA_FFDoc> m_pXFADoc; // Can't outlive |m_pGCHeap| 133 cppgc::Persistent<CXFA_FFDocView> m_pXFADocView; // Can't outlive |m_pGCHeap| 134 }; 135 136 #endif // FPDFSDK_FPDFXFA_CPDFXFA_CONTEXT_H_ 137