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 --- fpdfsdk/fpdfview.cpp | 50 ++++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) (limited to 'fpdfsdk/fpdfview.cpp') diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp index 29fe88d943..0c3a95d094 100644 --- a/fpdfsdk/fpdfview.cpp +++ b/fpdfsdk/fpdfview.cpp @@ -66,8 +66,8 @@ CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) { FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) { #ifdef PDF_ENABLE_XFA - return doc ? FPDFDocumentFromUnderlying( - new CPDFXFA_Document(doc, CPDFXFA_App::GetInstance())) + return doc ? FPDFDocumentFromUnderlying(new CPDFXFA_Document( + WrapUnique(doc), CPDFXFA_App::GetInstance())) : nullptr; #else // PDF_ENABLE_XFA return FPDFDocumentFromUnderlying(doc); @@ -365,22 +365,17 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, std::unique_ptr pParser(new CPDF_Parser); pParser->SetPassword(password); - std::unique_ptr pDocument(new CPDF_Document(pParser.get())); - CPDF_Parser::Error error = - pParser->StartParse(pFileAccess, std::move(pDocument)); + std::unique_ptr pDocument( + new CPDF_Document(std::move(pParser))); + CPDF_Parser::Error error = pParser->StartParse(pFileAccess, pDocument.get()); if (error != CPDF_Parser::SUCCESS) { ProcessParseError(error); return nullptr; } #ifdef PDF_ENABLE_XFA - CPDF_Document* pPDFDoc = pParser.release()->GetDocument(); - if (!pPDFDoc) - return nullptr; - - CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance(); - return new CPDFXFA_Document(pPDFDoc, pProvider); + return new CPDFXFA_Document(std::move(pDocument), CPDFXFA_App::GetInstance()); #else // PDF_ENABLE_XFA - return pParser.release()->GetDocument(); + return pDocument.release(); #endif // PDF_ENABLE_XFA } @@ -451,15 +446,16 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf, std::unique_ptr pParser(new CPDF_Parser); pParser->SetPassword(password); - std::unique_ptr pDocument(new CPDF_Document(pParser.get())); + std::unique_ptr pDocument( + new CPDF_Document(std::move(pParser))); CPDF_Parser::Error error = - pParser->StartParse(pMemFile, std::move(pDocument)); + pDocument->GetParser()->StartParse(pMemFile, pDocument.get()); if (error != CPDF_Parser::SUCCESS) { ProcessParseError(error); return nullptr; } - CheckUnSupportError(pParser->GetDocument(), error); - return FPDFDocumentFromCPDFDocument(pParser.release()->GetDocument()); + CheckUnSupportError(pDocument.get(), error); + return FPDFDocumentFromCPDFDocument(pDocument.release()); } DLLEXPORT FPDF_DOCUMENT STDCALL @@ -469,14 +465,16 @@ FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess, std::unique_ptr pParser(new CPDF_Parser); pParser->SetPassword(password); - std::unique_ptr pDocument(new CPDF_Document(pParser.get())); - CPDF_Parser::Error error = pParser->StartParse(pFile, std::move(pDocument)); + std::unique_ptr pDocument( + new CPDF_Document(std::move(pParser))); + CPDF_Parser::Error error = + pDocument->GetParser()->StartParse(pFile, pDocument.get()); if (error != CPDF_Parser::SUCCESS) { ProcessParseError(error); return nullptr; } - CheckUnSupportError(pParser->GetDocument(), error); - return FPDFDocumentFromCPDFDocument(pParser.release()->GetDocument()); + CheckUnSupportError(pDocument.get(), error); + return FPDFDocumentFromCPDFDocument(pDocument.release()); } DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc, @@ -683,19 +681,7 @@ DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) { } DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) { -#ifdef PDF_ENABLE_XFA delete UnderlyingFromFPDFDocument(document); -#else // PDF_ENABLE_XFA - CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); - if (!pDoc) - return; - CPDF_Parser* pParser = pDoc->GetParser(); - if (!pParser) { - delete pDoc; - return; - } - delete pParser; -#endif // PDF_ENABLE_XFA } DLLEXPORT unsigned long STDCALL FPDF_GetLastError() { -- cgit v1.2.3