diff options
author | tsepez <tsepez@chromium.org> | 2016-12-07 09:21:17 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-12-07 09:21:18 -0800 |
commit | 833619b4441915c7c55085d44b3221eaef0d9800 (patch) | |
tree | 68bf76e83078223ba03f490c2c13f484e40154d4 /xfa/fxfa/app | |
parent | 8f875507a986d10335e40a5f7c1679aff9770d0a (diff) | |
download | pdfium-833619b4441915c7c55085d44b3221eaef0d9800.tar.xz |
Refcount all the IFX_ stream classes all the time.
We can remove a lot of "bOwnsStream" logic in the process.
Always pass these by const reference, in case the called method
wants to hang on to the stream (one exception is where we stick
a raw pointer into a void* slot in a context from another layer).
Review-Url: https://codereview.chromium.org/2451493002
Diffstat (limited to 'xfa/fxfa/app')
-rw-r--r-- | xfa/fxfa/app/xfa_checksum.cpp | 8 | ||||
-rw-r--r-- | xfa/fxfa/app/xfa_ffapp.cpp | 20 | ||||
-rw-r--r-- | xfa/fxfa/app/xfa_ffapp_unittest.cpp | 20 | ||||
-rw-r--r-- | xfa/fxfa/app/xfa_ffdoc.cpp | 42 | ||||
-rw-r--r-- | xfa/fxfa/app/xfa_ffwidget.cpp | 21 |
5 files changed, 48 insertions, 63 deletions
diff --git a/xfa/fxfa/app/xfa_checksum.cpp b/xfa/fxfa/app/xfa_checksum.cpp index 01035f8ea4..61cbe97fb3 100644 --- a/xfa/fxfa/app/xfa_checksum.cpp +++ b/xfa/fxfa/app/xfa_checksum.cpp @@ -225,11 +225,13 @@ void CXFA_ChecksumContext::StartChecksum() { m_pSAXReader = new CFX_SAXReader; } -bool CXFA_ChecksumContext::UpdateChecksum(IFX_SeekableReadStream* pSrcFile, - FX_FILESIZE offset, - size_t size) { +bool CXFA_ChecksumContext::UpdateChecksum( + const CFX_RetainPtr<IFX_SeekableReadStream>& pSrcFile, + FX_FILESIZE offset, + size_t size) { if (!m_pSAXReader || !pSrcFile) return false; + if (size < 1) size = pSrcFile->GetSize(); diff --git a/xfa/fxfa/app/xfa_ffapp.cpp b/xfa/fxfa/app/xfa_ffapp.cpp index 9c0411b731..a0674b6522 100644 --- a/xfa/fxfa/app/xfa_ffapp.cpp +++ b/xfa/fxfa/app/xfa_ffapp.cpp @@ -31,7 +31,6 @@ class CXFA_FileRead : public IFX_SeekableReadStream { // IFX_SeekableReadStream FX_FILESIZE GetSize() override; bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; - void Release() override; private: CFX_ObjectArray<CPDF_StreamAcc> m_Data; @@ -84,15 +83,11 @@ bool CXFA_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) { return false; } -void CXFA_FileRead::Release() { - delete this; -} - } // namespace -IFX_SeekableReadStream* MakeSeekableReadStream( +CFX_RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream( const std::vector<CPDF_Stream*>& streams) { - return new CXFA_FileRead(streams); + return CFX_RetainPtr<IFX_SeekableReadStream>(new CXFA_FileRead(streams)); } CXFA_FFApp::CXFA_FFApp(IXFA_AppProvider* pProvider) @@ -108,12 +103,11 @@ CXFA_FFDocHandler* CXFA_FFApp::GetDocHandler() { return m_pDocHandler.get(); } -CXFA_FFDoc* CXFA_FFApp::CreateDoc(IXFA_DocEnvironment* pDocEnvironment, - IFX_SeekableReadStream* pStream, - bool bTakeOverFile) { - std::unique_ptr<CXFA_FFDoc> pDoc(new CXFA_FFDoc(this, pDocEnvironment)); - bool bSuccess = pDoc->OpenDoc(pStream, bTakeOverFile); - return bSuccess ? pDoc.release() : nullptr; +CXFA_FFDoc* CXFA_FFApp::CreateDoc( + IXFA_DocEnvironment* pDocEnvironment, + const CFX_RetainPtr<IFX_SeekableReadStream>& pStream) { + auto pDoc = pdfium::MakeUnique<CXFA_FFDoc>(this, pDocEnvironment); + return pDoc->OpenDoc(pStream) ? pDoc.release() : nullptr; } CXFA_FFDoc* CXFA_FFApp::CreateDoc(IXFA_DocEnvironment* pDocEnvironment, diff --git a/xfa/fxfa/app/xfa_ffapp_unittest.cpp b/xfa/fxfa/app/xfa_ffapp_unittest.cpp index 7a65dcc45b..b0879012b4 100644 --- a/xfa/fxfa/app/xfa_ffapp_unittest.cpp +++ b/xfa/fxfa/app/xfa_ffapp_unittest.cpp @@ -13,12 +13,10 @@ #include "testing/gtest/include/gtest/gtest.h" #include "third_party/base/ptr_util.h" -using UniqueFileRead = std::unique_ptr<IFX_SeekableReadStream, - ReleaseDeleter<IFX_SeekableReadStream>>; - TEST(CXFAFileRead, NoStreams) { std::vector<CPDF_Stream*> streams; - UniqueFileRead fileread(MakeSeekableReadStream(streams)); + CFX_RetainPtr<IFX_SeekableReadStream> fileread = + MakeSeekableReadStream(streams); uint8_t output_buffer[16]; memset(output_buffer, 0xbd, sizeof(output_buffer)); @@ -28,9 +26,10 @@ TEST(CXFAFileRead, NoStreams) { TEST(CXFAFileRead, EmptyStreams) { std::vector<CPDF_Stream*> streams; - std::unique_ptr<CPDF_Stream> stream1 = pdfium::MakeUnique<CPDF_Stream>(); + auto stream1 = pdfium::MakeUnique<CPDF_Stream>(); streams.push_back(stream1.get()); - UniqueFileRead fileread(MakeSeekableReadStream(streams)); + CFX_RetainPtr<IFX_SeekableReadStream> fileread = + MakeSeekableReadStream(streams); uint8_t output_buffer[16]; memset(output_buffer, 0xbd, sizeof(output_buffer)); @@ -40,9 +39,9 @@ TEST(CXFAFileRead, EmptyStreams) { TEST(CXFAFileRead, NormalStreams) { std::vector<CPDF_Stream*> streams; - std::unique_ptr<CPDF_Stream> stream1 = pdfium::MakeUnique<CPDF_Stream>(); - std::unique_ptr<CPDF_Stream> stream2 = pdfium::MakeUnique<CPDF_Stream>(); - std::unique_ptr<CPDF_Stream> stream3 = pdfium::MakeUnique<CPDF_Stream>(); + auto stream1 = pdfium::MakeUnique<CPDF_Stream>(); + auto stream2 = pdfium::MakeUnique<CPDF_Stream>(); + auto stream3 = pdfium::MakeUnique<CPDF_Stream>(); // 16 chars total. stream1->InitStream(reinterpret_cast<const uint8_t*>("one t"), 5, @@ -55,7 +54,8 @@ TEST(CXFAFileRead, NormalStreams) { streams.push_back(stream1.get()); streams.push_back(stream2.get()); streams.push_back(stream3.get()); - UniqueFileRead fileread(MakeSeekableReadStream(streams)); + CFX_RetainPtr<IFX_SeekableReadStream> fileread = + MakeSeekableReadStream(streams); uint8_t output_buffer[16]; memset(output_buffer, 0xbd, sizeof(output_buffer)); diff --git a/xfa/fxfa/app/xfa_ffdoc.cpp b/xfa/fxfa/app/xfa_ffdoc.cpp index e719a41c63..917c853991 100644 --- a/xfa/fxfa/app/xfa_ffdoc.cpp +++ b/xfa/fxfa/app/xfa_ffdoc.cpp @@ -152,12 +152,10 @@ int32_t Base64DecodeW(const FX_WCHAR* pSrc, int32_t iSrcLen, uint8_t* pDst) { CXFA_FFDoc::CXFA_FFDoc(CXFA_FFApp* pApp, IXFA_DocEnvironment* pDocEnvironment) : m_pDocEnvironment(pDocEnvironment), m_pDocumentParser(nullptr), - m_pStream(nullptr), m_pApp(pApp), m_pNotify(nullptr), m_pPDFDoc(nullptr), - m_dwDocType(XFA_DOCTYPE_Static), - m_bOwnStream(true) {} + m_dwDocType(XFA_DOCTYPE_Static) {} CXFA_FFDoc::~CXFA_FFDoc() { CloseDoc(); @@ -292,8 +290,7 @@ CXFA_FFDocView* CXFA_FFDoc::GetDocView() { return it != m_TypeToDocViewMap.end() ? it->second.get() : nullptr; } -bool CXFA_FFDoc::OpenDoc(IFX_SeekableReadStream* pStream, bool bTakeOverFile) { - m_bOwnStream = bTakeOverFile; +bool CXFA_FFDoc::OpenDoc(const CFX_RetainPtr<IFX_SeekableReadStream>& pStream) { m_pStream = pStream; return true; } @@ -326,14 +323,8 @@ bool CXFA_FFDoc::OpenDoc(CPDF_Document* pPDFDoc) { if (xfaStreams.empty()) return false; - IFX_SeekableReadStream* pFileRead = MakeSeekableReadStream(xfaStreams); m_pPDFDoc = pPDFDoc; - if (m_pStream) { - m_pStream->Release(); - m_pStream = nullptr; - } - m_pStream = pFileRead; - m_bOwnStream = true; + m_pStream = MakeSeekableReadStream(xfaStreams); return true; } @@ -351,11 +342,6 @@ bool CXFA_FFDoc::CloseDoc() { m_pNotify.reset(nullptr); m_pApp->GetXFAFontMgr()->ReleaseDocFonts(this); - if (m_dwDocType != XFA_DOCTYPE_XDP && m_pStream && m_bOwnStream) { - m_pStream->Release(); - m_pStream = nullptr; - } - for (const auto& pair : m_HashToDibDpiMap) delete pair.second.pDibSource; @@ -417,21 +403,21 @@ CFX_DIBitmap* CXFA_FFDoc::GetPDFNamedImage(const CFX_WideStringC& wsName, CPDF_StreamAcc streamAcc; streamAcc.LoadAllData(pStream); - IFX_SeekableReadStream* pImageFileRead = IFX_MemoryStream::Create( - (uint8_t*)streamAcc.GetData(), streamAcc.GetSize()); + CFX_RetainPtr<IFX_SeekableReadStream> pImageFileRead = + IFX_MemoryStream::Create((uint8_t*)streamAcc.GetData(), + streamAcc.GetSize()); CFX_DIBitmap* pDibSource = XFA_LoadImageFromBuffer( pImageFileRead, FXCODEC_IMAGE_UNKNOWN, iImageXDpi, iImageYDpi); m_HashToDibDpiMap[dwHash] = {pDibSource, iImageXDpi, iImageYDpi}; - pImageFileRead->Release(); return pDibSource; } -bool CXFA_FFDoc::SavePackage(XFA_HashCode code, - IFX_SeekableWriteStream* pFile, - CXFA_ChecksumContext* pCSContext) { +bool CXFA_FFDoc::SavePackage( + XFA_HashCode code, + const CFX_RetainPtr<IFX_SeekableWriteStream>& pFile, + CXFA_ChecksumContext* pCSContext) { CXFA_Document* doc = m_pDocumentParser->GetDocument(); - std::unique_ptr<CXFA_DataExporter> pExport(new CXFA_DataExporter(doc)); CXFA_Node* pNode = code == XFA_HASHCODE_Xfa ? doc->GetRoot() : ToNode(doc->GetXFAObject(code)); @@ -446,8 +432,10 @@ bool CXFA_FFDoc::SavePackage(XFA_HashCode code, pFile, pNode, 0, bsChecksum.GetLength() ? bsChecksum.c_str() : nullptr); } -bool CXFA_FFDoc::ImportData(IFX_SeekableReadStream* pStream, bool bXDP) { - std::unique_ptr<CXFA_DataImporter> importer( - new CXFA_DataImporter(m_pDocumentParser->GetDocument())); +bool CXFA_FFDoc::ImportData( + const CFX_RetainPtr<IFX_SeekableReadStream>& pStream, + bool bXDP) { + auto importer = + pdfium::MakeUnique<CXFA_DataImporter>(m_pDocumentParser->GetDocument()); return importer->ImportData(pStream); } diff --git a/xfa/fxfa/app/xfa_ffwidget.cpp b/xfa/fxfa/app/xfa_ffwidget.cpp index dfe418b76b..99333ba1dd 100644 --- a/xfa/fxfa/app/xfa_ffwidget.cpp +++ b/xfa/fxfa/app/xfa_ffwidget.cpp @@ -1054,7 +1054,7 @@ CFX_DIBitmap* XFA_LoadImageData(CXFA_FFDoc* pDoc, FXCODEC_IMAGE_TYPE type = XFA_GetImageType(wsContentType); CFX_ByteString bsContent; uint8_t* pImageBuffer = nullptr; - IFX_SeekableReadStream* pImageFileRead = nullptr; + CFX_RetainPtr<IFX_SeekableReadStream> pImageFileRead; if (wsImage.GetLength() > 0) { XFA_ATTRIBUTEENUM iEncoding = (XFA_ATTRIBUTEENUM)pImage->GetTransferEncoding(); @@ -1092,7 +1092,6 @@ CFX_DIBitmap* XFA_LoadImageData(CXFA_FFDoc* pDoc, CFX_DIBitmap* pBitmap = XFA_LoadImageFromBuffer(pImageFileRead, type, iImageXDpi, iImageYDpi); FX_Free(pImageBuffer); - pImageFileRead->Release(); return pBitmap; } static FXDIB_Format XFA_GetDIBFormat(FXCODEC_IMAGE_TYPE type, @@ -1115,18 +1114,20 @@ static FXDIB_Format XFA_GetDIBFormat(FXCODEC_IMAGE_TYPE type, } return dibFormat; } -CFX_DIBitmap* XFA_LoadImageFromBuffer(IFX_SeekableReadStream* pImageFileRead, - FXCODEC_IMAGE_TYPE type, - int32_t& iImageXDpi, - int32_t& iImageYDpi) { + +CFX_DIBitmap* XFA_LoadImageFromBuffer( + const CFX_RetainPtr<IFX_SeekableReadStream>& pImageFileRead, + FXCODEC_IMAGE_TYPE type, + int32_t& iImageXDpi, + int32_t& iImageYDpi) { CFX_GEModule* pGeModule = CFX_GEModule::Get(); - if (!pGeModule) { + if (!pGeModule) return nullptr; - } + CCodec_ModuleMgr* pCodecMgr = pGeModule->GetCodecModule(); - if (!pCodecMgr) { + if (!pCodecMgr) return nullptr; - } + CFX_DIBAttribute dibAttr; CFX_DIBitmap* pBitmap = nullptr; CCodec_ProgressiveDecoder* pProgressiveDecoder = |