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 | |
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')
28 files changed, 274 insertions, 266 deletions
diff --git a/xfa/fde/xml/cfx_saxreader.cpp b/xfa/fde/xml/cfx_saxreader.cpp index b4d48d2937..458bed52d6 100644 --- a/xfa/fde/xml/cfx_saxreader.cpp +++ b/xfa/fde/xml/cfx_saxreader.cpp @@ -63,32 +63,34 @@ static const FX_SAXReader_LPFParse } // namespace CFX_SAXFile::CFX_SAXFile() - : m_pFile(nullptr), - m_dwStart(0), + : m_dwStart(0), m_dwEnd(0), m_dwCur(0), m_pBuf(nullptr), m_dwBufSize(0), m_dwBufIndex(0) {} -bool CFX_SAXFile::StartFile(IFX_SeekableReadStream* pFile, + +CFX_SAXFile::~CFX_SAXFile() {} + +bool CFX_SAXFile::StartFile(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile, uint32_t dwStart, uint32_t dwLen) { ASSERT(!m_pFile && pFile); uint32_t dwSize = pFile->GetSize(); - if (dwStart >= dwSize) { + if (dwStart >= dwSize) return false; - } - if (dwLen == static_cast<uint32_t>(-1) || dwStart + dwLen > dwSize) { + + if (dwLen == static_cast<uint32_t>(-1) || dwStart + dwLen > dwSize) dwLen = dwSize - dwStart; - } - if (dwLen == 0) { + + if (dwLen == 0) return false; - } + m_dwBufSize = std::min(dwLen, kSaxFileBufSize); m_pBuf = FX_Alloc(uint8_t, m_dwBufSize); - if (!pFile->ReadBlock(m_pBuf, dwStart, m_dwBufSize)) { + if (!pFile->ReadBlock(m_pBuf, dwStart, m_dwBufSize)) return false; - } + m_dwStart = dwStart; m_dwEnd = dwStart + dwLen; m_dwCur = dwStart; @@ -214,15 +216,16 @@ bool CFX_SAXReader::SkipSpace(uint8_t ch) { return (m_dwParseMode & CFX_SaxParseMode_NotSkipSpace) == 0 && ch < 0x21; } -int32_t CFX_SAXReader::StartParse(IFX_SeekableReadStream* pFile, - uint32_t dwStart, - uint32_t dwLen, - uint32_t dwParseMode) { +int32_t CFX_SAXReader::StartParse( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFile, + uint32_t dwStart, + uint32_t dwLen, + uint32_t dwParseMode) { m_iState = -1; Reset(); - if (!m_File.StartFile(pFile, dwStart, dwLen)) { + if (!m_File.StartFile(pFile, dwStart, dwLen)) return -1; - } + m_iState = 0; m_eMode = CFX_SaxMode::Text; m_ePrevMode = CFX_SaxMode::Text; diff --git a/xfa/fde/xml/cfx_saxreader.h b/xfa/fde/xml/cfx_saxreader.h index 667813cf76..8e06d9bb48 100644 --- a/xfa/fde/xml/cfx_saxreader.h +++ b/xfa/fde/xml/cfx_saxreader.h @@ -38,12 +38,15 @@ class CFX_SAXItem { class CFX_SAXFile { public: CFX_SAXFile(); - bool StartFile(IFX_SeekableReadStream* pFile, + ~CFX_SAXFile(); + + bool StartFile(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile, uint32_t dwStart, uint32_t dwLen); bool ReadNextBlock(); void Reset(); - IFX_SeekableReadStream* m_pFile; + + CFX_RetainPtr<IFX_SeekableReadStream> m_pFile; uint32_t m_dwStart; uint32_t m_dwEnd; uint32_t m_dwCur; @@ -72,7 +75,7 @@ class CFX_SAXReader { CFX_SAXReader(); ~CFX_SAXReader(); - int32_t StartParse(IFX_SeekableReadStream* pFile, + int32_t StartParse(const CFX_RetainPtr<IFX_SeekableReadStream>& pFile, uint32_t dwStart = 0, uint32_t dwLen = -1, uint32_t dwParseMode = 0); diff --git a/xfa/fgas/crt/fgas_stream.cpp b/xfa/fgas/crt/fgas_stream.cpp index 18d8e0bfe1..3ccb652141 100644 --- a/xfa/fgas/crt/fgas_stream.cpp +++ b/xfa/fgas/crt/fgas_stream.cpp @@ -14,6 +14,7 @@ #include <algorithm> #include <memory> +#include "third_party/base/ptr_util.h" #include "xfa/fgas/crt/fgas_codepage.h" namespace { @@ -100,7 +101,8 @@ class CFGAS_FileReadStreamImp : public IFGAS_StreamImp { CFGAS_FileReadStreamImp(); ~CFGAS_FileReadStreamImp() override {} - bool LoadFileRead(IFX_SeekableReadStream* pFileRead, uint32_t dwAccess); + bool LoadFileRead(const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, + uint32_t dwAccess); // IFGAS_StreamImp: int32_t GetLength() const override; @@ -119,7 +121,7 @@ class CFGAS_FileReadStreamImp : public IFGAS_StreamImp { bool SetLength(int32_t iLength) override { return false; } protected: - IFX_SeekableReadStream* m_pFileRead; + CFX_RetainPtr<IFX_SeekableReadStream> m_pFileRead; int32_t m_iPosition; int32_t m_iLength; }; @@ -129,10 +131,9 @@ class CFGAS_BufferReadStreamImp : public IFGAS_StreamImp { CFGAS_BufferReadStreamImp(); ~CFGAS_BufferReadStreamImp() override; - bool LoadBufferRead(IFX_BufferedReadStream* pBufferRead, + bool LoadBufferRead(const CFX_RetainPtr<IFX_BufferedReadStream>& pBufferRead, int32_t iFileSize, - uint32_t dwAccess, - bool bReleaseBufferRead); + uint32_t dwAccess); // IFGAS_StreamImp: int32_t GetLength() const override; @@ -151,8 +152,7 @@ class CFGAS_BufferReadStreamImp : public IFGAS_StreamImp { bool SetLength(int32_t iLength) override { return false; } private: - IFX_BufferedReadStream* m_pBufferRead; - bool m_bReleaseBufferRead; + CFX_RetainPtr<IFX_BufferedReadStream> m_pBufferRead; int32_t m_iPosition; int32_t m_iBufferSize; }; @@ -162,7 +162,8 @@ class CFGAS_FileWriteStreamImp : public IFGAS_StreamImp { CFGAS_FileWriteStreamImp(); ~CFGAS_FileWriteStreamImp() override {} - bool LoadFileWrite(IFX_SeekableWriteStream* pFileWrite, uint32_t dwAccess); + bool LoadFileWrite(const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, + uint32_t dwAccess); // IFGAS_StreamImp: int32_t GetLength() const override; @@ -179,7 +180,7 @@ class CFGAS_FileWriteStreamImp : public IFGAS_StreamImp { bool SetLength(int32_t iLength) override { return false; } protected: - IFX_SeekableWriteStream* m_pFileWrite; + CFX_RetainPtr<IFX_SeekableWriteStream> m_pFileWrite; int32_t m_iPosition; }; @@ -198,12 +199,13 @@ class CFGAS_Stream : public IFGAS_Stream { bool LoadFile(const FX_WCHAR* pszSrcFileName, uint32_t dwAccess); bool LoadBuffer(uint8_t* pData, int32_t iTotalSize, uint32_t dwAccess); - bool LoadFileRead(IFX_SeekableReadStream* pFileRead, uint32_t dwAccess); - bool LoadFileWrite(IFX_SeekableWriteStream* pFileWrite, uint32_t dwAccess); - bool LoadBufferRead(IFX_BufferedReadStream* pBufferRead, + bool LoadFileRead(const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, + uint32_t dwAccess); + bool LoadFileWrite(const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, + uint32_t dwAccess); + bool LoadBufferRead(const CFX_RetainPtr<IFX_BufferedReadStream>& pBufferRead, int32_t iFileSize, - uint32_t dwAccess, - bool bReleaseBufferRead); + uint32_t dwAccess); // IFGAS_Stream void Release() override; @@ -277,15 +279,17 @@ class CFGAS_TextStream : public IFGAS_Stream { class CFGAS_FileRead : public IFX_SeekableReadStream { public: - CFGAS_FileRead(IFGAS_Stream* pStream, bool bReleaseStream); + static CFX_RetainPtr<CFGAS_FileRead> Create(IFGAS_Stream* pStream, + bool bReleaseStream); ~CFGAS_FileRead() override; // IFX_SeekableReadStream - void Release() override; FX_FILESIZE GetSize() override; bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override; protected: + CFGAS_FileRead(IFGAS_Stream* pStream, bool bReleaseStream); + bool m_bReleaseStream; IFGAS_Stream* m_pStream; }; @@ -322,8 +326,9 @@ bool FileSetSize(FXSYS_FILE* file, int32_t size) { } // namespace // static -IFGAS_Stream* IFGAS_Stream::CreateStream(IFX_SeekableReadStream* pFileRead, - uint32_t dwAccess) { +IFGAS_Stream* IFGAS_Stream::CreateStream( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, + uint32_t dwAccess) { CFGAS_Stream* pSR = new CFGAS_Stream; if (!pSR->LoadFileRead(pFileRead, dwAccess)) { pSR->Release(); @@ -336,8 +341,9 @@ IFGAS_Stream* IFGAS_Stream::CreateStream(IFX_SeekableReadStream* pFileRead, } // static -IFGAS_Stream* IFGAS_Stream::CreateStream(IFX_SeekableWriteStream* pFileWrite, - uint32_t dwAccess) { +IFGAS_Stream* IFGAS_Stream::CreateStream( + const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, + uint32_t dwAccess) { CFGAS_Stream* pSR = new CFGAS_Stream; if (!pSR->LoadFileWrite(pFileWrite, dwAccess)) { pSR->Release(); @@ -534,18 +540,22 @@ bool CFGAS_FileStreamImp::SetLength(int32_t iLength) { m_iLength = FileLength(m_hFile); return bRet; } + CFGAS_FileReadStreamImp::CFGAS_FileReadStreamImp() : m_pFileRead(nullptr), m_iPosition(0), m_iLength(0) {} -bool CFGAS_FileReadStreamImp::LoadFileRead(IFX_SeekableReadStream* pFileRead, - uint32_t dwAccess) { + +bool CFGAS_FileReadStreamImp::LoadFileRead( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, + uint32_t dwAccess) { ASSERT(!m_pFileRead && pFileRead); - if (dwAccess & FX_STREAMACCESS_Write) { + if (dwAccess & FX_STREAMACCESS_Write) return false; - } + m_pFileRead = pFileRead; m_iLength = m_pFileRead->GetSize(); return true; } + int32_t CFGAS_FileReadStreamImp::GetLength() const { return m_iLength; } @@ -600,34 +610,28 @@ int32_t CFGAS_FileReadStreamImp::ReadString(FX_WCHAR* pStr, bEOS = (m_iPosition >= m_iLength) || pStr[i] == L'\0'; return i; } + CFGAS_BufferReadStreamImp::CFGAS_BufferReadStreamImp() - : m_pBufferRead(nullptr), - m_bReleaseBufferRead(false), - m_iPosition(0), - m_iBufferSize(0) {} -CFGAS_BufferReadStreamImp::~CFGAS_BufferReadStreamImp() { - if (m_bReleaseBufferRead && m_pBufferRead) { - m_pBufferRead->Release(); - } -} + : m_iPosition(0), m_iBufferSize(0) {} + +CFGAS_BufferReadStreamImp::~CFGAS_BufferReadStreamImp() {} + bool CFGAS_BufferReadStreamImp::LoadBufferRead( - IFX_BufferedReadStream* pBufferRead, + const CFX_RetainPtr<IFX_BufferedReadStream>& pBufferRead, int32_t iFileSize, - uint32_t dwAccess, - bool bReleaseBufferRead) { + uint32_t dwAccess) { ASSERT(!m_pBufferRead && pBufferRead); - if (dwAccess & FX_STREAMACCESS_Write) { + if (dwAccess & FX_STREAMACCESS_Write) return false; - } - m_bReleaseBufferRead = bReleaseBufferRead; + m_pBufferRead = pBufferRead; m_iBufferSize = iFileSize; - if (m_iBufferSize >= 0) { + if (m_iBufferSize >= 0) return true; - } - if (!m_pBufferRead->ReadNextBlock(true)) { + + if (!m_pBufferRead->ReadNextBlock(true)) return false; - } + m_iBufferSize = m_pBufferRead->GetBlockSize(); while (!m_pBufferRead->IsEOF()) { m_pBufferRead->ReadNextBlock(false); @@ -733,23 +737,25 @@ int32_t CFGAS_BufferReadStreamImp::ReadString(FX_WCHAR* pStr, } CFGAS_FileWriteStreamImp::CFGAS_FileWriteStreamImp() : m_pFileWrite(nullptr), m_iPosition(0) {} + bool CFGAS_FileWriteStreamImp::LoadFileWrite( - IFX_SeekableWriteStream* pFileWrite, + const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, uint32_t dwAccess) { ASSERT(!m_pFileWrite && pFileWrite); - if (dwAccess & FX_STREAMACCESS_Read) { + if (dwAccess & FX_STREAMACCESS_Read) return false; - } - if (dwAccess & FX_STREAMACCESS_Append) { + + if (dwAccess & FX_STREAMACCESS_Append) m_iPosition = pFileWrite->GetSize(); - } + m_pFileWrite = pFileWrite; return true; } + int32_t CFGAS_FileWriteStreamImp::GetLength() const { - if (!m_pFileWrite) { + if (!m_pFileWrite) return 0; - } + return (int32_t)m_pFileWrite->GetSize(); } int32_t CFGAS_FileWriteStreamImp::Seek(FX_STREAMSEEK eSeek, int32_t iOffset) { @@ -1159,8 +1165,9 @@ bool CFGAS_Stream::LoadFile(const FX_WCHAR* pszSrcFileName, uint32_t dwAccess) { return true; } -bool CFGAS_Stream::LoadFileRead(IFX_SeekableReadStream* pFileRead, - uint32_t dwAccess) { +bool CFGAS_Stream::LoadFileRead( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, + uint32_t dwAccess) { if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp) return false; @@ -1178,16 +1185,16 @@ bool CFGAS_Stream::LoadFileRead(IFX_SeekableReadStream* pFileRead, return true; } -bool CFGAS_Stream::LoadFileWrite(IFX_SeekableWriteStream* pFileWrite, - uint32_t dwAccess) { +bool CFGAS_Stream::LoadFileWrite( + const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, + uint32_t dwAccess) { if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp) return false; if (!pFileWrite) return false; - std::unique_ptr<CFGAS_FileWriteStreamImp> pImp( - new CFGAS_FileWriteStreamImp()); + auto pImp = pdfium::MakeUnique<CFGAS_FileWriteStreamImp>(); if (!pImp->LoadFileWrite(pFileWrite, dwAccess)) return false; @@ -1218,20 +1225,18 @@ bool CFGAS_Stream::LoadBuffer(uint8_t* pData, return true; } -bool CFGAS_Stream::LoadBufferRead(IFX_BufferedReadStream* pBufferRead, - int32_t iFileSize, - uint32_t dwAccess, - bool bReleaseBufferRead) { +bool CFGAS_Stream::LoadBufferRead( + const CFX_RetainPtr<IFX_BufferedReadStream>& pBufferRead, + int32_t iFileSize, + uint32_t dwAccess) { if (m_eStreamType != FX_SREAMTYPE_Unknown || m_pStreamImp) return false; if (!pBufferRead) return false; - std::unique_ptr<CFGAS_BufferReadStreamImp> pImp( - new CFGAS_BufferReadStreamImp); - if (!pImp->LoadBufferRead(pBufferRead, iFileSize, dwAccess, - bReleaseBufferRead)) + auto pImp = pdfium::MakeUnique<CFGAS_BufferReadStreamImp>(); + if (!pImp->LoadBufferRead(pBufferRead, iFileSize, dwAccess)) return false; m_pStreamImp = pImp.release(); @@ -1478,14 +1483,21 @@ IFGAS_Stream* CFGAS_Stream::CreateSharedStream(uint32_t dwAccess, return pShared; } -IFX_SeekableReadStream* IFGAS_Stream::MakeSeekableReadStream() { - return new CFGAS_FileRead(this, false); +CFX_RetainPtr<IFX_SeekableReadStream> IFGAS_Stream::MakeSeekableReadStream() { + return CFGAS_FileRead::Create(this, false); +} + +CFX_RetainPtr<CFGAS_FileRead> CFGAS_FileRead::Create(IFGAS_Stream* pStream, + bool bReleaseStream) { + return CFX_RetainPtr<CFGAS_FileRead>( + new CFGAS_FileRead(pStream, bReleaseStream)); } CFGAS_FileRead::CFGAS_FileRead(IFGAS_Stream* pStream, bool bReleaseStream) : m_bReleaseStream(bReleaseStream), m_pStream(pStream) { ASSERT(m_pStream); } + CFGAS_FileRead::~CFGAS_FileRead() { if (m_bReleaseStream) { m_pStream->Release(); @@ -1500,7 +1512,3 @@ bool CFGAS_FileRead::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) { int32_t iLen = m_pStream->ReadData((uint8_t*)buffer, (int32_t)size); return iLen == (int32_t)size; } - -void CFGAS_FileRead::Release() { - delete this; -} diff --git a/xfa/fgas/crt/fgas_stream.h b/xfa/fgas/crt/fgas_stream.h index bd065ff308..b6552d9825 100644 --- a/xfa/fgas/crt/fgas_stream.h +++ b/xfa/fgas/crt/fgas_stream.h @@ -7,6 +7,7 @@ #ifndef XFA_FGAS_CRT_FGAS_STREAM_H_ #define XFA_FGAS_CRT_FGAS_STREAM_H_ +#include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_stream.h" #include "core/fxcrt/fx_system.h" @@ -28,10 +29,12 @@ enum FX_STREAMSEEK { class IFGAS_Stream { public: - static IFGAS_Stream* CreateStream(IFX_SeekableReadStream* pFileRead, - uint32_t dwAccess); - static IFGAS_Stream* CreateStream(IFX_SeekableWriteStream* pFileWrite, - uint32_t dwAccess); + static IFGAS_Stream* CreateStream( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFileRead, + uint32_t dwAccess); + static IFGAS_Stream* CreateStream( + const CFX_RetainPtr<IFX_SeekableWriteStream>& pFileWrite, + uint32_t dwAccess); static IFGAS_Stream* CreateStream(uint8_t* pData, int32_t length, uint32_t dwAccess); @@ -63,7 +66,7 @@ class IFGAS_Stream { virtual uint16_t GetCodePage() const = 0; virtual uint16_t SetCodePage(uint16_t wCodePage) = 0; - IFX_SeekableReadStream* MakeSeekableReadStream(); + CFX_RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream(); }; diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp index f211e64bc3..060dd95b48 100644 --- a/xfa/fgas/font/cfgas_fontmgr.cpp +++ b/xfa/fgas/font/cfgas_fontmgr.cpp @@ -607,13 +607,6 @@ CFGAS_FontMgr::~CFGAS_FontMgr() { delete pFonts; } m_Hash2Fonts.RemoveAll(); - pos = m_IFXFont2FileRead.GetStartPosition(); - while (pos) { - CFGAS_GEFont* pFont; - IFX_SeekableReadStream* pFileRead; - m_IFXFont2FileRead.GetNextAssoc(pos, pFont, pFileRead); - pFileRead->Release(); - } } bool CFGAS_FontMgr::EnumFontsFromFontMapper() { @@ -628,7 +621,7 @@ bool CFGAS_FontMgr::EnumFontsFromFontMapper() { pSystemFontInfo->EnumFontList(pFontMapper); for (int32_t i = 0; i < pFontMapper->GetFaceSize(); ++i) { - IFX_SeekableReadStream* pFontStream = + CFX_RetainPtr<IFX_SeekableReadStream> pFontStream = CreateFontStream(pFontMapper, pSystemFontInfo, i); if (!pFontStream) continue; @@ -636,19 +629,15 @@ bool CFGAS_FontMgr::EnumFontsFromFontMapper() { CFX_WideString wsFaceName = CFX_WideString::FromLocal(pFontMapper->GetFaceName(i).c_str()); RegisterFaces(pFontStream, &wsFaceName); - pFontStream->Release(); } - if (m_InstalledFonts.GetSize() == 0) - return false; - - return true; + return m_InstalledFonts.GetSize() != 0; } bool CFGAS_FontMgr::EnumFontsFromFiles() { CFX_GEModule::Get()->GetFontMgr()->InitFTLibrary(); FX_POSITION pos = m_pFontSource->GetStartPosition(); IFX_FileAccess* pFontSource = nullptr; - IFX_SeekableReadStream* pFontStream = nullptr; + CFX_RetainPtr<IFX_SeekableReadStream> pFontStream; while (pos) { pFontSource = m_pFontSource->GetNext(pos); pFontStream = pFontSource->CreateFileStream(FX_FILEMODE_ReadOnly); @@ -657,12 +646,9 @@ bool CFGAS_FontMgr::EnumFontsFromFiles() { continue; } RegisterFaces(pFontStream, nullptr); - pFontStream->Release(); pFontSource->Release(); } - if (m_InstalledFonts.GetSize() == 0) - return false; - return true; + return m_InstalledFonts.GetSize() != 0; } bool CFGAS_FontMgr::EnumFonts() { @@ -765,7 +751,7 @@ CFGAS_GEFont* CFGAS_FontMgr::GetFontByUnicode(FX_WCHAR wUnicode, bool CFGAS_FontMgr::VerifyUnicode(CFX_FontDescriptor* pDesc, FX_WCHAR wcUnicode) { - IFX_SeekableReadStream* pFileRead = + CFX_RetainPtr<IFX_SeekableReadStream> pFileRead = CreateFontStream(pDesc->m_wsFaceName.UTF8Encode()); if (!pFileRead) return false; @@ -773,12 +759,12 @@ bool CFGAS_FontMgr::VerifyUnicode(CFX_FontDescriptor* pDesc, FXFT_Face pFace = LoadFace(pFileRead, pDesc->m_nFaceIndex); FT_Error retCharmap = FXFT_Select_Charmap(pFace, FXFT_ENCODING_UNICODE); FT_Error retIndex = FXFT_Get_Char_Index(pFace, wcUnicode); - pFileRead->Release(); if (!pFace) return false; if (FXFT_Get_Face_External_Stream(pFace)) FXFT_Clear_Face_External_Stream(pFace); + FXFT_Done_Face(pFace); return !retCharmap && retIndex; } @@ -811,24 +797,20 @@ CFGAS_GEFont* CFGAS_FontMgr::LoadFont(const CFX_WideString& wsFaceName, if (!pSystemFontInfo) return nullptr; - IFX_SeekableReadStream* pFontStream = + CFX_RetainPtr<IFX_SeekableReadStream> pFontStream = CreateFontStream(wsFaceName.UTF8Encode()); if (!pFontStream) return nullptr; - std::unique_ptr<CFX_Font> pInternalFont(new CFX_Font()); - if (!pInternalFont->LoadFile(pFontStream, iFaceIndex)) { - pFontStream->Release(); + auto pInternalFont = pdfium::MakeUnique<CFX_Font>(); + if (!pInternalFont->LoadFile(pFontStream, iFaceIndex)) return nullptr; - } CFGAS_GEFont* pFont = CFGAS_GEFont::LoadFont(std::move(pInternalFont), this); - if (!pFont) { - pFontStream->Release(); + if (!pFont) return nullptr; - } - m_IFXFont2FileRead.SetAt(pFont, pFontStream); + m_IFXFont2FileRead[pFont] = pFontStream; if (pFaceCount) *pFaceCount = pFont->GetDevFont()->GetFace()->num_faces; @@ -845,24 +827,26 @@ unsigned long _ftStreamRead(FXFT_Stream stream, return 0; IFX_SeekableReadStream* pFile = - (IFX_SeekableReadStream*)stream->descriptor.pointer; - int res = pFile->ReadBlock(buffer, offset, count); - if (res) - return count; - return 0; + static_cast<IFX_SeekableReadStream*>(stream->descriptor.pointer); + if (!pFile->ReadBlock(buffer, offset, count)) + return 0; + + return count; } void _ftStreamClose(FXFT_Stream stream) {} }; // extern "C" -FXFT_Face CFGAS_FontMgr::LoadFace(IFX_SeekableReadStream* pFontStream, - int32_t iFaceIndex) { +FXFT_Face CFGAS_FontMgr::LoadFace( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFontStream, + int32_t iFaceIndex) { if (!pFontStream) return nullptr; CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); pFontMgr->InitFTLibrary(); + FXFT_Library library = pFontMgr->GetFTLibrary(); if (!library) return nullptr; @@ -870,7 +854,7 @@ FXFT_Face CFGAS_FontMgr::LoadFace(IFX_SeekableReadStream* pFontStream, FXFT_Stream ftStream = FX_Alloc(FXFT_StreamRec, 1); FXSYS_memset(ftStream, 0, sizeof(FXFT_StreamRec)); ftStream->base = nullptr; - ftStream->descriptor.pointer = pFontStream; + ftStream->descriptor.pointer = static_cast<void*>(pFontStream.Get()); ftStream->pos = 0; ftStream->size = static_cast<unsigned long>(pFontStream->GetSize()); ftStream->read = _ftStreamRead; @@ -891,7 +875,7 @@ FXFT_Face CFGAS_FontMgr::LoadFace(IFX_SeekableReadStream* pFontStream, return pFace; } -IFX_SeekableReadStream* CFGAS_FontMgr::CreateFontStream( +CFX_RetainPtr<IFX_SeekableReadStream> CFGAS_FontMgr::CreateFontStream( CFX_FontMapper* pFontMapper, IFX_SystemFontInfo* pSystemFontInfo, uint32_t index) { @@ -912,11 +896,10 @@ IFX_SeekableReadStream* CFGAS_FontMgr::CreateFontStream( return IFX_MemoryStream::Create(pBuffer, dwFileSize, true); } -IFX_SeekableReadStream* CFGAS_FontMgr::CreateFontStream( +CFX_RetainPtr<IFX_SeekableReadStream> CFGAS_FontMgr::CreateFontStream( const CFX_ByteString& bsFaceName) { CFX_FontMgr* pFontMgr = CFX_GEModule::Get()->GetFontMgr(); CFX_FontMapper* pFontMapper = pFontMgr->GetBuiltinMapper(); - if (!pFontMapper) return nullptr; @@ -1039,11 +1022,8 @@ void CFGAS_FontMgr::RemoveFont(CFGAS_GEFont* pEFont) { if (!pEFont) return; - IFX_SeekableReadStream* pFileRead; - if (m_IFXFont2FileRead.Lookup(pEFont, pFileRead)) { - pFileRead->Release(); - m_IFXFont2FileRead.RemoveKey(pEFont); - } + m_IFXFont2FileRead.erase(pEFont); + FX_POSITION pos; pos = m_Hash2Fonts.GetStartPosition(); while (pos) { @@ -1096,8 +1076,9 @@ void CFGAS_FontMgr::RegisterFace(FXFT_Face pFace, m_InstalledFonts.Add(pFont.release()); } -void CFGAS_FontMgr::RegisterFaces(IFX_SeekableReadStream* pFontStream, - const CFX_WideString* pFaceName) { +void CFGAS_FontMgr::RegisterFaces( + const CFX_RetainPtr<IFX_SeekableReadStream>& pFontStream, + const CFX_WideString* pFaceName) { int32_t index = 0; int32_t num_faces = 0; do { diff --git a/xfa/fgas/font/cfgas_fontmgr.h b/xfa/fgas/font/cfgas_fontmgr.h index 7b68525188..28a8bb528a 100644 --- a/xfa/fgas/font/cfgas_fontmgr.h +++ b/xfa/fgas/font/cfgas_fontmgr.h @@ -7,6 +7,7 @@ #ifndef XFA_FGAS_FONT_CFGAS_FONTMGR_H_ #define XFA_FGAS_FONT_CFGAS_FONTMGR_H_ +#include <map> #include <memory> #include <vector> @@ -214,7 +215,7 @@ class CFGAS_FontMgr { bool EnumFontsFromFontMapper(); bool EnumFontsFromFiles(); void RegisterFace(FXFT_Face pFace, const CFX_WideString* pFaceName); - void RegisterFaces(IFX_SeekableReadStream* pFontStream, + void RegisterFaces(const CFX_RetainPtr<IFX_SeekableReadStream>& pFontStream, const CFX_WideString* pFaceName); void GetNames(const uint8_t* name_table, CFX_WideStringArray& Names); std::vector<uint16_t> GetCharsets(FXFT_Face pFace) const; @@ -236,16 +237,20 @@ class CFGAS_FontMgr { CFGAS_GEFont* LoadFont(const CFX_WideString& wsFaceName, int32_t iFaceIndex, int32_t* pFaceCount); - FXFT_Face LoadFace(IFX_SeekableReadStream* pFontStream, int32_t iFaceIndex); - IFX_SeekableReadStream* CreateFontStream(CFX_FontMapper* pFontMapper, - IFX_SystemFontInfo* pSystemFontInfo, - uint32_t index); - IFX_SeekableReadStream* CreateFontStream(const CFX_ByteString& bsFaceName); + FXFT_Face LoadFace(const CFX_RetainPtr<IFX_SeekableReadStream>& pFontStream, + int32_t iFaceIndex); + CFX_RetainPtr<IFX_SeekableReadStream> CreateFontStream( + CFX_FontMapper* pFontMapper, + IFX_SystemFontInfo* pSystemFontInfo, + uint32_t index); + CFX_RetainPtr<IFX_SeekableReadStream> CreateFontStream( + const CFX_ByteString& bsFaceName); CFX_FontDescriptors m_InstalledFonts; CFX_MapPtrTemplate<uint32_t, CFX_FontDescriptorInfos*> m_Hash2CandidateList; CFX_MapPtrTemplate<uint32_t, CFX_ArrayTemplate<CFGAS_GEFont*>*> m_Hash2Fonts; - CFX_MapPtrTemplate<CFGAS_GEFont*, IFX_SeekableReadStream*> m_IFXFont2FileRead; + std::map<CFGAS_GEFont*, CFX_RetainPtr<IFX_SeekableReadStream> > + m_IFXFont2FileRead; CFX_MapPtrTemplate<FX_WCHAR, CFGAS_GEFont*> m_FailedUnicodes2Nullptr; CFX_FontSourceEnum_File* const m_pFontSource; }; diff --git a/xfa/fgas/font/cfgas_gefont.cpp b/xfa/fgas/font/cfgas_gefont.cpp index 1d4624fb0d..3bca4dc09f 100644 --- a/xfa/fgas/font/cfgas_gefont.cpp +++ b/xfa/fgas/font/cfgas_gefont.cpp @@ -210,12 +210,13 @@ bool CFGAS_GEFont::LoadFontInternal(IFGAS_Stream* pFontStream, if (bSaveStream) m_pStream.reset(pFontStream); - m_pFileRead.reset(pFontStream->MakeSeekableReadStream()); + m_pFileRead = pFontStream->MakeSeekableReadStream(); m_pFont = new CFX_Font; - if (m_pFont->LoadFile(m_pFileRead.get())) - return InitFont(); - m_pFileRead.reset(); - return false; + if (!m_pFont->LoadFile(m_pFileRead)) { + m_pFileRead.Reset(); + return false; + } + return InitFont(); } #endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ diff --git a/xfa/fgas/font/cfgas_gefont.h b/xfa/fgas/font/cfgas_gefont.h index 2b4179e711..b4fcf25ae1 100644 --- a/xfa/fgas/font/cfgas_gefont.h +++ b/xfa/fgas/font/cfgas_gefont.h @@ -98,9 +98,7 @@ class CFGAS_GEFont { int32_t m_iRefCount; bool m_bExternalFont; std::unique_ptr<IFGAS_Stream, ReleaseDeleter<IFGAS_Stream>> m_pStream; - std::unique_ptr<IFX_SeekableReadStream, - ReleaseDeleter<IFX_SeekableReadStream>> - m_pFileRead; + CFX_RetainPtr<IFX_SeekableReadStream> m_pFileRead; std::unique_ptr<CFX_UnicodeEncoding> m_pFontEncoding; std::unique_ptr<CFX_DiscreteArrayTemplate<uint16_t>> m_pCharWidthMap; std::unique_ptr<CFX_MassArrayTemplate<CFX_Rect>> m_pRectArray; 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 = diff --git a/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp b/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp index 8bcdcdda31..01328f2374 100644 --- a/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp +++ b/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp @@ -4924,7 +4924,7 @@ void CXFA_FM2JSContext::Get(CFXJSE_Value* pThis, std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); CFX_ByteString urlString; ValueToUTF8String(argOne.get(), urlString); - IFX_SeekableReadStream* pFile = pAppProvider->DownloadURL( + CFX_RetainPtr<IFX_SeekableReadStream> pFile = pAppProvider->DownloadURL( CFX_WideString::FromUTF8(urlString.AsStringC())); if (!pFile) return; @@ -4933,7 +4933,6 @@ void CXFA_FM2JSContext::Get(CFXJSE_Value* pThis, std::unique_ptr<uint8_t> pData(FX_Alloc(uint8_t, size)); pFile->ReadBlock(pData.get(), size); args.GetReturnValue()->SetString(CFX_ByteStringC(pData.get(), size)); - pFile->Release(); } // static diff --git a/xfa/fxfa/fxfa.h b/xfa/fxfa/fxfa.h index d3ae838315..ec5282cf8a 100644 --- a/xfa/fxfa/fxfa.h +++ b/xfa/fxfa/fxfa.h @@ -9,6 +9,7 @@ #include <vector> +#include "core/fxcrt/cfx_retain_ptr.h" #include "xfa/fxfa/fxfa_basic.h" #include "xfa/fxfa/fxfa_widget.h" @@ -230,7 +231,8 @@ class IXFA_AppProvider { * @param[in] wsURL - http, ftp, such as * "http://www.w3.org/TR/REC-xml-names/". */ - virtual IFX_SeekableReadStream* DownloadURL(const CFX_WideString& wsURL) = 0; + virtual CFX_RetainPtr<IFX_SeekableReadStream> DownloadURL( + const CFX_WideString& wsURL) = 0; /** * POST data to the given url. @@ -320,7 +322,7 @@ class IXFA_DocEnvironment { virtual bool SetGlobalProperty(CXFA_FFDoc* hDoc, const CFX_ByteStringC& szPropName, CFXJSE_Value* pValue) = 0; - virtual IFX_SeekableReadStream* OpenLinkedFile( + virtual CFX_RetainPtr<IFX_SeekableReadStream> OpenLinkedFile( CXFA_FFDoc* hDoc, const CFX_WideString& wsLink) = 0; }; diff --git a/xfa/fxfa/parser/cxfa_dataexporter.cpp b/xfa/fxfa/parser/cxfa_dataexporter.cpp index f97e8a34ed..9760261baf 100644 --- a/xfa/fxfa/parser/cxfa_dataexporter.cpp +++ b/xfa/fxfa/parser/cxfa_dataexporter.cpp @@ -197,17 +197,20 @@ void RegenerateFormFile_Changed(CXFA_Node* pNode, if (!pRichTextXML) break; - IFX_MemoryStream* pMemStream = IFX_MemoryStream::Create(true); + CFX_RetainPtr<IFX_MemoryStream> pMemStream = + IFX_MemoryStream::Create(true); + + // Note: ambiguous without cast below. IFGAS_Stream* pTempStream = IFGAS_Stream::CreateStream( - (IFX_SeekableWriteStream*)pMemStream, FX_STREAMACCESS_Text | - FX_STREAMACCESS_Write | - FX_STREAMACCESS_Append); + CFX_RetainPtr<IFX_SeekableWriteStream>(pMemStream), + FX_STREAMACCESS_Text | FX_STREAMACCESS_Write | + FX_STREAMACCESS_Append); + pTempStream->SetCodePage(FX_CODEPAGE_UTF8); pRichTextXML->SaveXMLNode(pTempStream); wsChildren += CFX_WideString::FromUTF8( CFX_ByteStringC(pMemStream->GetBuffer(), pMemStream->GetSize())); pTempStream->Release(); - pMemStream->Release(); } else if (pRawValueNode->GetElementType() == XFA_Element::Sharpxml && wsContentType == FX_WSTRC(L"text/xml")) { CFX_WideString wsRawValue; @@ -444,18 +447,20 @@ CXFA_DataExporter::CXFA_DataExporter(CXFA_Document* pDocument) ASSERT(m_pDocument); } -bool CXFA_DataExporter::Export(IFX_SeekableWriteStream* pWrite) { +bool CXFA_DataExporter::Export( + const CFX_RetainPtr<IFX_SeekableWriteStream>& pWrite) { return Export(pWrite, m_pDocument->GetRoot(), 0, nullptr); } -bool CXFA_DataExporter::Export(IFX_SeekableWriteStream* pWrite, - CXFA_Node* pNode, - uint32_t dwFlag, - const FX_CHAR* pChecksum) { - if (!pWrite) { - ASSERT(false); +bool CXFA_DataExporter::Export( + const CFX_RetainPtr<IFX_SeekableWriteStream>& pWrite, + CXFA_Node* pNode, + uint32_t dwFlag, + const FX_CHAR* pChecksum) { + ASSERT(pWrite); + if (!pWrite) return false; - } + IFGAS_Stream* pStream = IFGAS_Stream::CreateStream( pWrite, FX_STREAMACCESS_Text | FX_STREAMACCESS_Write | FX_STREAMACCESS_Append); diff --git a/xfa/fxfa/parser/cxfa_dataexporter.h b/xfa/fxfa/parser/cxfa_dataexporter.h index 8496e0d9a6..f3c784a86a 100644 --- a/xfa/fxfa/parser/cxfa_dataexporter.h +++ b/xfa/fxfa/parser/cxfa_dataexporter.h @@ -7,6 +7,7 @@ #ifndef XFA_FXFA_PARSER_CXFA_DATAEXPORTER_H_ #define XFA_FXFA_PARSER_CXFA_DATAEXPORTER_H_ +#include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_string.h" class CXFA_Document; @@ -18,8 +19,8 @@ class CXFA_DataExporter { public: explicit CXFA_DataExporter(CXFA_Document* pDocument); - bool Export(IFX_SeekableWriteStream* pWrite); - bool Export(IFX_SeekableWriteStream* pWrite, + bool Export(const CFX_RetainPtr<IFX_SeekableWriteStream>& pWrite); + bool Export(const CFX_RetainPtr<IFX_SeekableWriteStream>& pWrite, CXFA_Node* pNode, uint32_t dwFlag, const FX_CHAR* pChecksum); diff --git a/xfa/fxfa/parser/cxfa_dataimporter.cpp b/xfa/fxfa/parser/cxfa_dataimporter.cpp index c0c798dfe6..0199028ecd 100644 --- a/xfa/fxfa/parser/cxfa_dataimporter.cpp +++ b/xfa/fxfa/parser/cxfa_dataimporter.cpp @@ -9,6 +9,7 @@ #include <memory> #include "core/fxcrt/fx_stream.h" +#include "third_party/base/ptr_util.h" #include "xfa/fde/xml/fde_xml_imp.h" #include "xfa/fxfa/fxfa.h" #include "xfa/fxfa/fxfa_basic.h" @@ -21,9 +22,10 @@ CXFA_DataImporter::CXFA_DataImporter(CXFA_Document* pDocument) ASSERT(m_pDocument); } -bool CXFA_DataImporter::ImportData(IFX_SeekableReadStream* pDataDocument) { - std::unique_ptr<CXFA_SimpleParser> pDataDocumentParser( - new CXFA_SimpleParser(m_pDocument, false)); +bool CXFA_DataImporter::ImportData( + const CFX_RetainPtr<IFX_SeekableReadStream>& pDataDocument) { + auto pDataDocumentParser = + pdfium::MakeUnique<CXFA_SimpleParser>(m_pDocument, false); if (pDataDocumentParser->StartParse(pDataDocument, XFA_XDPPACKET_Datasets) != XFA_PARSESTATUS_Ready) { return false; diff --git a/xfa/fxfa/parser/cxfa_dataimporter.h b/xfa/fxfa/parser/cxfa_dataimporter.h index 86e41a8130..bf04b05846 100644 --- a/xfa/fxfa/parser/cxfa_dataimporter.h +++ b/xfa/fxfa/parser/cxfa_dataimporter.h @@ -7,6 +7,7 @@ #ifndef XFA_FXFA_PARSER_CXFA_DATAIMPORTER_H_ #define XFA_FXFA_PARSER_CXFA_DATAIMPORTER_H_ +#include "core/fxcrt/cfx_retain_ptr.h" #include "core/fxcrt/fx_system.h" class CXFA_Document; @@ -16,7 +17,7 @@ class CXFA_DataImporter { public: explicit CXFA_DataImporter(CXFA_Document* pDocument); - bool ImportData(IFX_SeekableReadStream* pDataDocument); + bool ImportData(const CFX_RetainPtr<IFX_SeekableReadStream>& pDataDocument); protected: CXFA_Document* const m_pDocument; diff --git a/xfa/fxfa/parser/cxfa_document_parser.cpp b/xfa/fxfa/parser/cxfa_document_parser.cpp index fe3cb933aa..1bf327d667 100644 --- a/xfa/fxfa/parser/cxfa_document_parser.cpp +++ b/xfa/fxfa/parser/cxfa_document_parser.cpp @@ -15,8 +15,9 @@ CXFA_DocumentParser::CXFA_DocumentParser(CXFA_FFNotify* pNotify) CXFA_DocumentParser::~CXFA_DocumentParser() { } -int32_t CXFA_DocumentParser::StartParse(IFX_SeekableReadStream* pStream, - XFA_XDPPACKET ePacketID) { +int32_t CXFA_DocumentParser::StartParse( + const CFX_RetainPtr<IFX_SeekableReadStream>& pStream, + XFA_XDPPACKET ePacketID) { m_pDocument.reset(); m_nodeParser.CloseParser(); diff --git a/xfa/fxfa/parser/cxfa_document_parser.h b/xfa/fxfa/parser/cxfa_document_parser.h index 29aeca39bc..617bddf55b 100644 --- a/xfa/fxfa/parser/cxfa_document_parser.h +++ b/xfa/fxfa/parser/cxfa_document_parser.h @@ -23,7 +23,8 @@ class CXFA_DocumentParser { explicit CXFA_DocumentParser(CXFA_FFNotify* pNotify); ~CXFA_DocumentParser(); - int32_t StartParse(IFX_SeekableReadStream* pStream, XFA_XDPPACKET ePacketID); + int32_t StartParse(const CFX_RetainPtr<IFX_SeekableReadStream>& pStream, + XFA_XDPPACKET ePacketID); int32_t DoParse(IFX_Pause* pPause); CFDE_XMLDoc* GetXMLDoc() const; diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index 16062f7b6f..5dd6c52717 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -1441,13 +1441,16 @@ void CXFA_Node::Script_NodeClass_SaveXML(CFXJSE_Arguments* pArguments) { } XFA_DataExporter_DealWithDataGroupNode(this); } - std::unique_ptr<IFX_MemoryStream, ReleaseDeleter<IFX_MemoryStream>> - pMemoryStream(IFX_MemoryStream::Create(true)); + CFX_RetainPtr<IFX_MemoryStream> pMemoryStream = + IFX_MemoryStream::Create(true); + + // Note: ambiguious below without static_cast. std::unique_ptr<IFGAS_Stream, ReleaseDeleter<IFGAS_Stream>> pStream( IFGAS_Stream::CreateStream( - static_cast<IFX_SeekableWriteStream*>(pMemoryStream.get()), + CFX_RetainPtr<IFX_SeekableWriteStream>(pMemoryStream), FX_STREAMACCESS_Text | FX_STREAMACCESS_Write | FX_STREAMACCESS_Append)); + if (!pStream) { pArguments->GetReturnValue()->SetString(bsXMLHeader); return; diff --git a/xfa/fxfa/parser/cxfa_simple_parser.cpp b/xfa/fxfa/parser/cxfa_simple_parser.cpp index a9025fa42a..2329e86874 100644 --- a/xfa/fxfa/parser/cxfa_simple_parser.cpp +++ b/xfa/fxfa/parser/cxfa_simple_parser.cpp @@ -277,8 +277,9 @@ void CXFA_SimpleParser::SetFactory(CXFA_Document* pFactory) { m_pFactory = pFactory; } -int32_t CXFA_SimpleParser::StartParse(IFX_SeekableReadStream* pStream, - XFA_XDPPACKET ePacketID) { +int32_t CXFA_SimpleParser::StartParse( + const CFX_RetainPtr<IFX_SeekableReadStream>& pStream, + XFA_XDPPACKET ePacketID) { CloseParser(); m_pFileRead = pStream; m_pStream.reset(IFGAS_Stream::CreateStream( diff --git a/xfa/fxfa/parser/cxfa_simple_parser.h b/xfa/fxfa/parser/cxfa_simple_parser.h index fa9fdb3014..559df7124b 100644 --- a/xfa/fxfa/parser/cxfa_simple_parser.h +++ b/xfa/fxfa/parser/cxfa_simple_parser.h @@ -24,7 +24,8 @@ class CXFA_SimpleParser { CXFA_SimpleParser(CXFA_Document* pFactory, bool bDocumentParser); ~CXFA_SimpleParser(); - int32_t StartParse(IFX_SeekableReadStream* pStream, XFA_XDPPACKET ePacketID); + int32_t StartParse(const CFX_RetainPtr<IFX_SeekableReadStream>& pStream, + XFA_XDPPACKET ePacketID); int32_t DoParse(IFX_Pause* pPause); int32_t ParseXMLData(const CFX_WideString& wsXML, CFDE_XMLNode*& pXMLNode, @@ -78,7 +79,7 @@ class CXFA_SimpleParser { CXFA_XMLParser* m_pXMLParser; std::unique_ptr<CFDE_XMLDoc> m_pXMLDoc; std::unique_ptr<IFGAS_Stream, ReleaseDeleter<IFGAS_Stream>> m_pStream; - IFX_SeekableReadStream* m_pFileRead; + CFX_RetainPtr<IFX_SeekableReadStream> m_pFileRead; CXFA_Document* m_pFactory; CXFA_Node* m_pRootNode; XFA_XDPPACKET m_ePacketID; diff --git a/xfa/fxfa/xfa_checksum.h b/xfa/fxfa/xfa_checksum.h index aaa587fed0..32862c94c3 100644 --- a/xfa/fxfa/xfa_checksum.h +++ b/xfa/fxfa/xfa_checksum.h @@ -62,7 +62,7 @@ class CXFA_ChecksumContext { void StartChecksum(); void Update(const CFX_ByteStringC& bsText); - bool UpdateChecksum(IFX_SeekableReadStream* pSrcFile, + bool UpdateChecksum(const CFX_RetainPtr<IFX_SeekableReadStream>& pSrcFile, FX_FILESIZE offset = 0, size_t size = 0); void FinishChecksum(); diff --git a/xfa/fxfa/xfa_ffapp.h b/xfa/fxfa/xfa_ffapp.h index 95013ef82f..5883be2279 100644 --- a/xfa/fxfa/xfa_ffapp.h +++ b/xfa/fxfa/xfa_ffapp.h @@ -12,6 +12,7 @@ #include "core/fpdfapi/parser/cpdf_stream.h" #include "core/fpdfapi/parser/cpdf_stream_acc.h" +#include "core/fxcrt/cfx_retain_ptr.h" #include "xfa/fgas/font/cfgas_fontmgr.h" #include "xfa/fwl/core/cfwl_app.h" #include "xfa/fxfa/fxfa.h" @@ -26,7 +27,7 @@ class CFWL_WidgetMgrDelegate; // Layering prevents fxcrt from knowing about CPDF_Streams; this could go // in fpdfsdk, but it is XFA-Only. -IFX_SeekableReadStream* MakeSeekableReadStream( +CFX_RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream( const std::vector<CPDF_Stream*>& streams); class CXFA_FFApp { @@ -35,8 +36,7 @@ class CXFA_FFApp { ~CXFA_FFApp(); CXFA_FFDoc* CreateDoc(IXFA_DocEnvironment* pDocEnvironment, - IFX_SeekableReadStream* pStream, - bool bTakeOverFile); + const CFX_RetainPtr<IFX_SeekableReadStream>& pStream); CXFA_FFDoc* CreateDoc(IXFA_DocEnvironment* pDocEnvironment, CPDF_Document* pPDFDoc); void SetDefaultFontMgr(std::unique_ptr<CXFA_DefFontMgr> pFontMgr); diff --git a/xfa/fxfa/xfa_ffdoc.h b/xfa/fxfa/xfa_ffdoc.h index a31f6d7e50..44079704e7 100644 --- a/xfa/fxfa/xfa_ffdoc.h +++ b/xfa/fxfa/xfa_ffdoc.h @@ -40,7 +40,7 @@ class CXFA_FFDoc { CXFA_FFDocView* CreateDocView(uint32_t dwView = 0); - bool OpenDoc(IFX_SeekableReadStream* pStream, bool bTakeOverFile); + bool OpenDoc(const CFX_RetainPtr<IFX_SeekableReadStream>& pStream); bool OpenDoc(CPDF_Document* pPDFDoc); bool CloseDoc(); @@ -54,21 +54,21 @@ class CXFA_FFDoc { int32_t& iImageYDpi); bool SavePackage(XFA_HashCode code, - IFX_SeekableWriteStream* pFile, + const CFX_RetainPtr<IFX_SeekableWriteStream>& pFile, CXFA_ChecksumContext* pCSContext); - bool ImportData(IFX_SeekableReadStream* pStream, bool bXDP = true); + bool ImportData(const CFX_RetainPtr<IFX_SeekableReadStream>& pStream, + bool bXDP = true); protected: IXFA_DocEnvironment* const m_pDocEnvironment; std::unique_ptr<CXFA_DocumentParser> m_pDocumentParser; - IFX_SeekableReadStream* m_pStream; + CFX_RetainPtr<IFX_SeekableReadStream> m_pStream; CXFA_FFApp* m_pApp; std::unique_ptr<CXFA_FFNotify> m_pNotify; CPDF_Document* m_pPDFDoc; std::map<uint32_t, FX_IMAGEDIB_AND_DPI> m_HashToDibDpiMap; std::map<uint32_t, std::unique_ptr<CXFA_FFDocView>> m_TypeToDocViewMap; uint32_t m_dwDocType; - bool m_bOwnStream; }; #endif // XFA_FXFA_XFA_FFDOC_H_ diff --git a/xfa/fxfa/xfa_ffwidget.h b/xfa/fxfa/xfa_ffwidget.h index 9e02680a23..031c35ba2b 100644 --- a/xfa/fxfa/xfa_ffwidget.h +++ b/xfa/fxfa/xfa_ffwidget.h @@ -161,15 +161,19 @@ void XFA_DrawImage(CFX_Graphics* pGS, int32_t iImageYDpi, int32_t iHorzAlign = XFA_ATTRIBUTEENUM_Left, int32_t iVertAlign = XFA_ATTRIBUTEENUM_Top); + CFX_DIBitmap* XFA_LoadImageData(CXFA_FFDoc* pDoc, CXFA_Image* pImage, bool& bNameImage, int32_t& iImageXDpi, int32_t& iImageYDpi); -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); + FXCODEC_IMAGE_TYPE XFA_GetImageType(const CFX_WideString& wsType); FX_CHAR* XFA_Base64Encode(const uint8_t* buf, int32_t buf_len); void XFA_RectWidthoutMargin(CFX_RectF& rt, |