From fa89a203151c3ea524fc4dfd7167e2a3baf332fa Mon Sep 17 00:00:00 2001 From: tsepez Date: Fri, 2 Dec 2016 09:48:30 -0800 Subject: Make concrete stream classes private to .cpp, part 4. This one is more complicated because we were using stack-based instances of what should have been a Release()-only type. Review-Url: https://codereview.chromium.org/2549573003 --- fpdfsdk/cpdfsdk_formfillenvironment.cpp | 2 +- fpdfsdk/fpdfview.cpp | 109 ++++++++++++++++++----------- fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp | 53 +++++++------- fpdfsdk/fsdk_define.h | 25 +------ 4 files changed, 100 insertions(+), 89 deletions(-) diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp index 8ab3b46a91..ee7758c281 100644 --- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp +++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp @@ -473,7 +473,7 @@ IFX_SeekableReadStream* CPDFSDK_FormFillEnvironment::DownloadFromURL( FPDF_LPFILEHANDLER fileHandler = m_pInfo->FFI_DownloadFromURL(m_pInfo, wsURL); - return new CFPDF_FileStream(fileHandler); + return MakeSeekableStream(fileHandler); } CFX_WideString CPDFSDK_FormFillEnvironment::PostRequestURL( diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp index 449433ed90..f0c269a312 100644 --- a/fpdfsdk/fpdfview.cpp +++ b/fpdfsdk/fpdfview.cpp @@ -158,51 +158,30 @@ bool CPDF_CustomAccess::ReadBlock(void* buffer, reinterpret_cast(buffer), size); } -} // namespace - -UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) { - return static_cast(doc); -} - -FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) { - return static_cast(doc); -} - -UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) { - return static_cast(page); -} - -CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) { #ifdef PDF_ENABLE_XFA - return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr; -#else // PDF_ENABLE_XFA - return UnderlyingFromFPDFDocument(doc); -#endif // PDF_ENABLE_XFA -} +class CFPDF_FileStream : public IFX_SeekableStream { + public: + explicit CFPDF_FileStream(FPDF_FILEHANDLER* pFS); + ~CFPDF_FileStream() override {} -FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) { -#ifdef PDF_ENABLE_XFA - return doc ? FPDFDocumentFromUnderlying( - new CPDFXFA_Context(pdfium::WrapUnique(doc))) - : nullptr; -#else // PDF_ENABLE_XFA - return FPDFDocumentFromUnderlying(doc); -#endif // PDF_ENABLE_XFA -} + // IFX_SeekableStream: + IFX_SeekableStream* Retain() override; + void Release() override; + FX_FILESIZE GetSize() override; + bool IsEOF() override; + FX_FILESIZE GetPosition() override; + bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; + size_t ReadBlock(void* buffer, size_t size) override; + bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; + bool Flush() override; -CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) { -#ifdef PDF_ENABLE_XFA - return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr; -#else // PDF_ENABLE_XFA - return UnderlyingFromFPDFPage(page); -#endif // PDF_ENABLE_XFA -} + void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; } -CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) { - return static_cast(bitmap); -} + protected: + FPDF_FILEHANDLER* m_pFS; + FX_FILESIZE m_nCurPos; +}; -#ifdef PDF_ENABLE_XFA CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) { m_pFS = pFS; m_nCurPos = 0; @@ -287,10 +266,60 @@ bool CFPDF_FileStream::Flush() { } #endif // PDF_ENABLE_XFA +} // namespace + +UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) { + return static_cast(doc); +} + +FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) { + return static_cast(doc); +} + +UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) { + return static_cast(page); +} + +CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) { +#ifdef PDF_ENABLE_XFA + return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr; +#else // PDF_ENABLE_XFA + return UnderlyingFromFPDFDocument(doc); +#endif // PDF_ENABLE_XFA +} + +FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) { +#ifdef PDF_ENABLE_XFA + return doc ? FPDFDocumentFromUnderlying( + new CPDFXFA_Context(pdfium::WrapUnique(doc))) + : nullptr; +#else // PDF_ENABLE_XFA + return FPDFDocumentFromUnderlying(doc); +#endif // PDF_ENABLE_XFA +} + +CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) { +#ifdef PDF_ENABLE_XFA + return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr; +#else // PDF_ENABLE_XFA + return UnderlyingFromFPDFPage(page); +#endif // PDF_ENABLE_XFA +} + +CFX_DIBitmap* CFXBitmapFromFPDFBitmap(FPDF_BITMAP bitmap) { + return static_cast(bitmap); +} + IFX_SeekableReadStream* MakeSeekableReadStream(FPDF_FILEACCESS* pFileAccess) { return new CPDF_CustomAccess(pFileAccess); } +#ifdef PDF_ENABLE_XFA +IFX_SeekableStream* MakeSeekableStream(FPDF_FILEHANDLER* pFilehandler) { + return new CFPDF_FileStream(pFilehandler); +} +#endif // PDF_ENABLE_XFA + // 0 bit: FPDF_POLICY_MACHINETIME_ACCESS static uint32_t foxit_sandbox_policy = 0xFFFFFFFF; diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp index 9bb1418379..6e199e2ff1 100644 --- a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp +++ b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp @@ -437,14 +437,15 @@ void CPDFXFA_DocEnvironment::ExportData(CXFA_FFDoc* hDoc, if (!pFileHandler) return; - CFPDF_FileStream fileWrite(pFileHandler); + std::unique_ptr> + fileWrite(MakeSeekableStream(pFileHandler)); CFX_ByteString content; if (fileType == FXFA_SAVEAS_XML) { content = "\r\n"; - fileWrite.WriteBlock(content.c_str(), fileWrite.GetSize(), - content.GetLength()); - m_pContext->GetXFADocView()->GetDoc()->SavePackage(XFA_HASHCODE_Data, - &fileWrite, nullptr); + fileWrite->WriteBlock(content.c_str(), fileWrite->GetSize(), + content.GetLength()); + m_pContext->GetXFADocView()->GetDoc()->SavePackage( + XFA_HASHCODE_Data, fileWrite.get(), nullptr); } else if (fileType == FXFA_SAVEAS_XDP) { if (!m_pContext->GetPDFDoc()) return; @@ -474,13 +475,13 @@ void CPDFXFA_DocEnvironment::ExportData(CXFA_FFDoc* hDoc, if (!pStream) continue; if (pPrePDFObj->GetString() == "form") { - m_pContext->GetXFADocView()->GetDoc()->SavePackage(XFA_HASHCODE_Form, - &fileWrite, nullptr); + m_pContext->GetXFADocView()->GetDoc()->SavePackage( + XFA_HASHCODE_Form, fileWrite.get(), nullptr); continue; } if (pPrePDFObj->GetString() == "datasets") { m_pContext->GetXFADocView()->GetDoc()->SavePackage( - XFA_HASHCODE_Datasets, &fileWrite, nullptr); + XFA_HASHCODE_Datasets, fileWrite.get(), nullptr); continue; } if (i == size - 1) { @@ -491,18 +492,16 @@ void CPDFXFA_DocEnvironment::ExportData(CXFA_FFDoc* hDoc, const char* szFormat = "\n"; content.Format(szFormat, bPath.c_str()); - fileWrite.WriteBlock(content.c_str(), fileWrite.GetSize(), - content.GetLength()); + fileWrite->WriteBlock(content.c_str(), fileWrite->GetSize(), + content.GetLength()); } std::unique_ptr pAcc(new CPDF_StreamAcc); pAcc->LoadAllData(pStream); - fileWrite.WriteBlock(pAcc->GetData(), fileWrite.GetSize(), - pAcc->GetSize()); + fileWrite->WriteBlock(pAcc->GetData(), fileWrite->GetSize(), + pAcc->GetSize()); } } - if (!fileWrite.Flush()) { - // Ignoring flush error. - } + fileWrite->Flush(); } void CPDFXFA_DocEnvironment::GotoURL(CXFA_FFDoc* hDoc, @@ -715,7 +714,7 @@ IFX_SeekableReadStream* CPDFXFA_DocEnvironment::OpenLinkedFile( if (!pFileHandler) return nullptr; - return new CFPDF_FileStream(pFileHandler); + return MakeSeekableStream(pFileHandler); } bool CPDFXFA_DocEnvironment::ExportSubmitFile(FPDF_FILEHANDLER* pFileHandler, @@ -730,11 +729,13 @@ bool CPDFXFA_DocEnvironment::ExportSubmitFile(FPDF_FILEHANDLER* pFileHandler, if (!pFormFillEnv) return false; - CFPDF_FileStream fileStream(pFileHandler); + std::unique_ptr> + fileStream(MakeSeekableStream(pFileHandler)); + if (fileType == FXFA_SAVEAS_XML) { const char kContent[] = "\r\n"; - fileStream.WriteBlock(kContent, 0, strlen(kContent)); - m_pContext->GetXFADoc()->SavePackage(XFA_HASHCODE_Data, &fileStream, + fileStream->WriteBlock(kContent, 0, strlen(kContent)); + m_pContext->GetXFADoc()->SavePackage(XFA_HASHCODE_Data, fileStream.get(), nullptr); return true; } @@ -747,25 +748,25 @@ bool CPDFXFA_DocEnvironment::ExportSubmitFile(FPDF_FILEHANDLER* pFileHandler, FXFA_XMPMETA | FXFA_XFDF | FXFA_FORM; } if (!m_pContext->GetPDFDoc()) { - fileStream.Flush(); + fileStream->Flush(); return false; } CPDF_Dictionary* pRoot = m_pContext->GetPDFDoc()->GetRoot(); if (!pRoot) { - fileStream.Flush(); + fileStream->Flush(); return false; } CPDF_Dictionary* pAcroForm = pRoot->GetDictFor("AcroForm"); if (!pAcroForm) { - fileStream.Flush(); + fileStream->Flush(); return false; } CPDF_Array* pArray = ToArray(pAcroForm->GetObjectFor("XFA")); if (!pArray) { - fileStream.Flush(); + fileStream->Flush(); return false; } @@ -796,11 +797,11 @@ bool CPDFXFA_DocEnvironment::ExportSubmitFile(FPDF_FILEHANDLER* pFileHandler, if (pPrePDFObj->GetString() == "form" && !(flag & FXFA_FORM)) continue; if (pPrePDFObj->GetString() == "form") { - m_pContext->GetXFADoc()->SavePackage(XFA_HASHCODE_Form, &fileStream, + m_pContext->GetXFADoc()->SavePackage(XFA_HASHCODE_Form, fileStream.get(), nullptr); } else if (pPrePDFObj->GetString() == "datasets") { - m_pContext->GetXFADoc()->SavePackage(XFA_HASHCODE_Datasets, &fileStream, - nullptr); + m_pContext->GetXFADoc()->SavePackage(XFA_HASHCODE_Datasets, + fileStream.get(), nullptr); } else { // PDF,creator. } diff --git a/fpdfsdk/fsdk_define.h b/fpdfsdk/fsdk_define.h index d878e54963..a3f5a30836 100644 --- a/fpdfsdk/fsdk_define.h +++ b/fpdfsdk/fsdk_define.h @@ -30,28 +30,9 @@ class IFSDK_PAUSE_Adapter; IFX_SeekableReadStream* MakeSeekableReadStream(FPDF_FILEACCESS* pFileAccess); #ifdef PDF_ENABLE_XFA -class CFPDF_FileStream : public IFX_SeekableStream { - public: - explicit CFPDF_FileStream(FPDF_FILEHANDLER* pFS); - ~CFPDF_FileStream() override {} - - // IFX_SeekableStream: - IFX_SeekableStream* Retain() override; - void Release() override; - FX_FILESIZE GetSize() override; - bool IsEOF() override; - FX_FILESIZE GetPosition() override; - bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; - size_t ReadBlock(void* buffer, size_t size) override; - bool WriteBlock(const void* buffer, FX_FILESIZE offset, size_t size) override; - bool Flush() override; - - void SetPosition(FX_FILESIZE pos) { m_nCurPos = pos; } - - protected: - FPDF_FILEHANDLER* m_pFS; - FX_FILESIZE m_nCurPos; -}; +// Layering prevents fxcrt from knowing about FPDF_FILEHANDLER, so this can't +// be a static method of IFX_SeekableStream. +IFX_SeekableStream* MakeSeekableStream(FPDF_FILEHANDLER* pFileHandler); #endif // PDF_ENABLE_XFA // Object types for public FPDF_ types; these correspond to next layer down -- cgit v1.2.3