From 774c90d5ec92d2c58d821c445230d8cb71928dcb Mon Sep 17 00:00:00 2001 From: Artem Strygin Date: Thu, 28 Jun 2018 00:03:03 +0000 Subject: Use UnownedPtr for document within CPDF_XXXAvail. Change-Id: I9ded1664564c330132f43047293e18696d77fc7d Reviewed-on: https://pdfium-review.googlesource.com/36310 Commit-Queue: Art Snake Reviewed-by: Tom Sepez --- core/fpdfapi/parser/cpdf_data_avail.cpp | 38 ++++++++++++++++++++++++++------- core/fpdfapi/parser/cpdf_data_avail.h | 4 +++- core/fpdfapi/parser/cpdf_object_avail.h | 3 +-- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/core/fpdfapi/parser/cpdf_data_avail.cpp b/core/fpdfapi/parser/cpdf_data_avail.cpp index 95db3d81c0..89b576bdea 100644 --- a/core/fpdfapi/parser/cpdf_data_avail.cpp +++ b/core/fpdfapi/parser/cpdf_data_avail.cpp @@ -70,6 +70,17 @@ class HintsScope { UnownedPtr validator_; }; +class CPDF_DocumentForAvail : public CPDF_Document { + public: + explicit CPDF_DocumentForAvail(CPDF_DataAvail* avail) : m_pAvail(avail) { + DCHECK(avail); + } + ~CPDF_DocumentForAvail() override { m_pAvail->BeforeDocumentDestroyed(); } + + private: + UnownedPtr m_pAvail; +}; + } // namespace CPDF_DataAvail::FileAvail::~FileAvail() {} @@ -89,6 +100,14 @@ CPDF_DataAvail::~CPDF_DataAvail() { m_pHintTables.reset(); } +void CPDF_DataAvail::BeforeDocumentDestroyed() { + m_pDocument = nullptr; + m_pFormAvail.reset(); + m_PagesArray.clear(); + m_PagesObjAvail.clear(); + m_PagesResourcesAvail.clear(); +} + CPDF_DataAvail::DocAvailStatus CPDF_DataAvail::IsDocAvail( DownloadHints* pHints) { if (!m_dwFileLen) @@ -816,7 +835,7 @@ CPDF_DataAvail::DocAvailStatus CPDF_DataAvail::IsPageAvail( auto page_num_obj = std::make_pair( dwPage, pdfium::MakeUnique( - GetValidator().Get(), m_pDocument, pPageDict)); + GetValidator().Get(), m_pDocument.Get(), pPageDict)); CPDF_PageObjectAvail* page_obj_avail = m_PagesObjAvail.insert(std::move(page_num_obj)).first->second.get(); @@ -868,7 +887,7 @@ CPDF_DataAvail::DocAvailStatus CPDF_DataAvail::IsPageAvail( { auto page_num_obj = std::make_pair( dwPage, pdfium::MakeUnique( - GetValidator().Get(), m_pDocument, pPageDict)); + GetValidator().Get(), m_pDocument.Get(), pPageDict)); CPDF_PageObjectAvail* page_obj_avail = m_PagesObjAvail.insert(std::move(page_num_obj)).first->second.get(); const DocAvailStatus status = page_obj_avail->CheckAvail(); @@ -900,8 +919,9 @@ CPDF_DataAvail::DocAvailStatus CPDF_DataAvail::CheckResources( CPDF_PageObjectAvail* resource_avail = m_PagesResourcesAvail .insert(std::make_pair( - resources, pdfium::MakeUnique( - GetValidator().Get(), m_pDocument, resources))) + resources, + pdfium::MakeUnique( + GetValidator().Get(), m_pDocument.Get(), resources))) .first->second.get(); return resource_avail->CheckAvail(); } @@ -944,7 +964,8 @@ CPDF_Dictionary* CPDF_DataAvail::GetPageDictionary(int index) const { // Page object already can be parsed in document. if (!m_pDocument->GetIndirectObject(dwObjNum)) { m_pDocument->ReplaceIndirectObjectIfHigherGeneration( - dwObjNum, ParseIndirectObjectAt(szPageStartPos, dwObjNum, m_pDocument)); + dwObjNum, + ParseIndirectObjectAt(szPageStartPos, dwObjNum, m_pDocument.Get())); } if (!ValidatePage(index)) return nullptr; @@ -979,7 +1000,7 @@ CPDF_DataAvail::DocFormStatus CPDF_DataAvail::CheckAcroForm() { return FormNotExist; m_pFormAvail = pdfium::MakeUnique( - GetValidator().Get(), m_pDocument, pAcroForm); + GetValidator().Get(), m_pDocument.Get(), pAcroForm); } switch (m_pFormAvail->CheckAvail()) { case DocAvailStatus::DataError: @@ -999,7 +1020,8 @@ bool CPDF_DataAvail::ValidatePage(uint32_t dwPage) const { auto* pPageDict = m_pDocument->GetPageDictionary(safePage.ValueOrDie()); if (!pPageDict) return false; - CPDF_PageObjectAvail obj_avail(GetValidator().Get(), m_pDocument, pPageDict); + CPDF_PageObjectAvail obj_avail(GetValidator().Get(), m_pDocument.Get(), + pPageDict); return obj_avail.CheckAvail() == DocAvailStatus::DataAvailable; } @@ -1009,7 +1031,7 @@ CPDF_DataAvail::ParseDocument(const char* password) { // We already returned parsed document. return std::make_pair(CPDF_Parser::HANDLER_ERROR, nullptr); } - auto document = pdfium::MakeUnique(); + auto document = pdfium::MakeUnique(this); CPDF_ReadValidator::Session read_session(GetValidator().Get()); CPDF_Parser::Error error = diff --git a/core/fpdfapi/parser/cpdf_data_avail.h b/core/fpdfapi/parser/cpdf_data_avail.h index b6396bf82a..3c45116088 100644 --- a/core/fpdfapi/parser/cpdf_data_avail.h +++ b/core/fpdfapi/parser/cpdf_data_avail.h @@ -97,6 +97,8 @@ class CPDF_DataAvail final { bool bSupportHintTable); ~CPDF_DataAvail(); + void BeforeDocumentDestroyed(); + DocAvailStatus IsDocAvail(DownloadHints* pHints); DocAvailStatus IsPageAvail(uint32_t dwPage, DownloadHints* pHints); DocFormStatus IsFormAvail(DownloadHints* pHints); @@ -172,7 +174,7 @@ class CPDF_DataAvail final { std::unique_ptr m_pCrossRefAvail; PDF_DATAAVAIL_STATUS m_docStatus = PDF_DATAAVAIL_HEADER; const FX_FILESIZE m_dwFileLen; - CPDF_Document* m_pDocument = nullptr; + UnownedPtr m_pDocument; std::vector m_PageObjList; uint32_t m_PagesObjNum = 0; bool m_bLinearedDataOK = false; diff --git a/core/fpdfapi/parser/cpdf_object_avail.h b/core/fpdfapi/parser/cpdf_object_avail.h index f593ea2159..040cec7252 100644 --- a/core/fpdfapi/parser/cpdf_object_avail.h +++ b/core/fpdfapi/parser/cpdf_object_avail.h @@ -43,8 +43,7 @@ class CPDF_ObjectAvail { bool HasObjectParsed(uint32_t obj_num) const; UnownedPtr validator_; - // TODO(art-snake): Make it UnownedPtr<, after fix document owning issue. - CPDF_IndirectObjectHolder* holder_; + UnownedPtr holder_; MaybeOwned root_; std::set parsed_objnums_; std::stack non_parsed_objects_; -- cgit v1.2.3