From cedaa557316a3f5c436814e69d67f19795f471d7 Mon Sep 17 00:00:00 2001 From: dsinclair Date: Wed, 24 Aug 2016 11:12:19 -0700 Subject: Flip document and parser ownership This Cl switches the ownership between the parser and the document. Previously the parser owned the document and we'd jump through hoops during cleanup to delete the right object. This Cl flips the ownership so the document owns the parser and simplifies the cleanup logic where needed. BUG=pdfium:565 Review-Url: https://codereview.chromium.org/2275773003 --- core/fpdfapi/fpdf_parser/cpdf_document.cpp | 4 ++-- core/fpdfapi/fpdf_parser/cpdf_parser.cpp | 27 +++++++++++------------- core/fpdfapi/fpdf_parser/include/cpdf_document.h | 6 +++--- core/fpdfapi/fpdf_parser/include/cpdf_parser.h | 11 ++++------ 4 files changed, 21 insertions(+), 27 deletions(-) (limited to 'core/fpdfapi') diff --git a/core/fpdfapi/fpdf_parser/cpdf_document.cpp b/core/fpdfapi/fpdf_parser/cpdf_document.cpp index b11d0c1b79..89b41d5820 100644 --- a/core/fpdfapi/fpdf_parser/cpdf_document.cpp +++ b/core/fpdfapi/fpdf_parser/cpdf_document.cpp @@ -483,9 +483,9 @@ int CountPages(CPDF_Dictionary* pPages, } // namespace -CPDF_Document::CPDF_Document(CPDF_Parser* pParser) +CPDF_Document::CPDF_Document(std::unique_ptr pParser) : CPDF_IndirectObjectHolder(), - m_pParser(pParser), + m_pParser(std::move(pParser)), m_pRootDict(nullptr), m_pInfoDict(nullptr), m_bLinearized(false), diff --git a/core/fpdfapi/fpdf_parser/cpdf_parser.cpp b/core/fpdfapi/fpdf_parser/cpdf_parser.cpp index ffd3f79a62..e2bab450be 100644 --- a/core/fpdfapi/fpdf_parser/cpdf_parser.cpp +++ b/core/fpdfapi/fpdf_parser/cpdf_parser.cpp @@ -126,7 +126,7 @@ void CPDF_Parser::ShrinkObjectMap(uint32_t objnum) { void CPDF_Parser::CloseParser() { m_bVersionUpdated = false; - m_pDocument.reset(); + m_pDocument = nullptr; if (m_pTrailer) { m_pTrailer->Release(); @@ -158,9 +158,8 @@ void CPDF_Parser::CloseParser() { } } -CPDF_Parser::Error CPDF_Parser::StartParse( - IFX_FileRead* pFileAccess, - std::unique_ptr pDocument) { +CPDF_Parser::Error CPDF_Parser::StartParse(IFX_FileRead* pFileAccess, + CPDF_Document* pDocument) { CloseParser(); m_bXRefStream = FALSE; @@ -190,7 +189,7 @@ CPDF_Parser::Error CPDF_Parser::StartParse( return FORMAT_ERROR; m_pSyntax->RestorePos(m_pSyntax->m_FileLen - m_pSyntax->m_HeaderOffset - 9); - m_pDocument = std::move(pDocument); + m_pDocument = pDocument; FX_BOOL bXRefRebuilt = FALSE; if (m_pSyntax->SearchWord("startxref", TRUE, FALSE, 4096)) { @@ -765,7 +764,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { last_obj = start_pos; FX_FILESIZE obj_end = 0; CPDF_Object* pObject = ParseIndirectObjectAtByStrict( - m_pDocument.get(), obj_pos, objnum, &obj_end); + m_pDocument, obj_pos, objnum, &obj_end); if (CPDF_Stream* pStream = ToStream(pObject)) { if (CPDF_Dictionary* pDict = pStream->GetDict()) { if ((pDict->KeyExist("Type")) && @@ -828,8 +827,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { last_trailer = pos + i - 7; m_pSyntax->RestorePos(pos + i - m_pSyntax->m_HeaderOffset); - CPDF_Object* pObj = - m_pSyntax->GetObject(m_pDocument.get(), 0, 0, true); + CPDF_Object* pObj = m_pSyntax->GetObject(m_pDocument, 0, 0, true); if (pObj) { if (!pObj->IsDictionary() && !pObj->AsStream()) { pObj->Release(); @@ -851,7 +849,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { uint32_t dwObjNum = pElement ? pElement->GetObjNum() : 0; if (dwObjNum) { - m_pTrailer->SetAtReference(key, m_pDocument.get(), + m_pTrailer->SetAtReference(key, m_pDocument, dwObjNum); } else { m_pTrailer->SetAt(key, pElement->Clone()); @@ -975,7 +973,7 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { } FX_BOOL CPDF_Parser::LoadCrossRefV5(FX_FILESIZE* pos, FX_BOOL bMainXRef) { - CPDF_Object* pObject = ParseIndirectObjectAt(m_pDocument.get(), *pos, 0); + CPDF_Object* pObject = ParseIndirectObjectAt(m_pDocument, *pos, 0); if (!pObject) return FALSE; @@ -1478,7 +1476,7 @@ CPDF_Dictionary* CPDF_Parser::LoadTrailerV4() { return nullptr; std::unique_ptr> pObj( - m_pSyntax->GetObject(m_pDocument.get(), 0, 0, true)); + m_pSyntax->GetObject(m_pDocument, 0, 0, true)); if (!ToDictionary(pObj.get())) return nullptr; return pObj.release()->AsDictionary(); @@ -1550,9 +1548,8 @@ FX_BOOL CPDF_Parser::IsLinearizedFile(IFX_FileRead* pFileAccess, return FALSE; } -CPDF_Parser::Error CPDF_Parser::StartLinearizedParse( - IFX_FileRead* pFileAccess, - std::unique_ptr pDocument) { +CPDF_Parser::Error CPDF_Parser::StartLinearizedParse(IFX_FileRead* pFileAccess, + CPDF_Document* pDocument) { CloseParser(); m_bXRefStream = FALSE; m_LastXRefOffset = 0; @@ -1567,7 +1564,7 @@ CPDF_Parser::Error CPDF_Parser::StartLinearizedParse( return StartParse(pFileAccess, std::move(pDocument)); } - m_pDocument = std::move(pDocument); + m_pDocument = pDocument; FX_FILESIZE dwFirstXRefOffset = m_pSyntax->SavePos(); FX_BOOL bXRefRebuilt = FALSE; diff --git a/core/fpdfapi/fpdf_parser/include/cpdf_document.h b/core/fpdfapi/fpdf_parser/include/cpdf_document.h index 31988d8c9d..f3322425ed 100644 --- a/core/fpdfapi/fpdf_parser/include/cpdf_document.h +++ b/core/fpdfapi/fpdf_parser/include/cpdf_document.h @@ -40,10 +40,10 @@ class JBig2_DocumentContext; class CPDF_Document : public CPDF_IndirectObjectHolder { public: - explicit CPDF_Document(CPDF_Parser* pParser); + explicit CPDF_Document(std::unique_ptr pParser); ~CPDF_Document() override; - CPDF_Parser* GetParser() const { return m_pParser; } + CPDF_Parser* GetParser() const { return m_pParser.get(); } CPDF_Dictionary* GetRoot() const { return m_pRootDict; } CPDF_Dictionary* GetInfo() const { return m_pInfoDict; } @@ -128,7 +128,7 @@ class CPDF_Document : public CPDF_IndirectObjectHolder { FX_BOOL CheckOCGVisible(CPDF_Dictionary* pOCG, FX_BOOL bPrinting); CPDF_Object* ParseIndirectObject(uint32_t objnum) override; - CPDF_Parser* m_pParser; + std::unique_ptr m_pParser; CPDF_Dictionary* m_pRootDict; CPDF_Dictionary* m_pInfoDict; CFX_ByteString m_ID1; diff --git a/core/fpdfapi/fpdf_parser/include/cpdf_parser.h b/core/fpdfapi/fpdf_parser/include/cpdf_parser.h index a69f0fe38e..d6a5d5703b 100644 --- a/core/fpdfapi/fpdf_parser/include/cpdf_parser.h +++ b/core/fpdfapi/fpdf_parser/include/cpdf_parser.h @@ -37,17 +37,14 @@ class CPDF_Parser { CPDF_Parser(); ~CPDF_Parser(); - Error StartParse(IFX_FileRead* pFile, - std::unique_ptr pDocument); - - Error StartLinearizedParse(IFX_FileRead* pFile, - std::unique_ptr pDocument); + Error StartParse(IFX_FileRead* pFile, CPDF_Document* pDocument); + Error StartLinearizedParse(IFX_FileRead* pFile, CPDF_Document* pDocument); void SetPassword(const FX_CHAR* password) { m_Password = password; } CFX_ByteString GetPassword() { return m_Password; } CPDF_Dictionary* GetTrailer() const { return m_pTrailer; } FX_FILESIZE GetLastXRefOffset() const { return m_LastXRefOffset; } - CPDF_Document* GetDocument() const { return m_pDocument.get(); } + CPDF_Document* GetDocument() const { return m_pDocument; } uint32_t GetPermissions() const; uint32_t GetRootObjNum(); @@ -120,7 +117,7 @@ class CPDF_Parser { // the objects. bool VerifyCrossRefV4(); - std::unique_ptr m_pDocument; + CPDF_Document* m_pDocument; // not owned std::unique_ptr m_pSyntax; bool m_bOwnFileRead; int m_FileVersion; -- cgit v1.2.3