summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortsepez <tsepez@chromium.org>2016-08-16 11:12:21 -0700
committerCommit bot <commit-bot@chromium.org>2016-08-16 11:12:21 -0700
commit4540fbaa718b0b06e076321e0e1a61e80a161499 (patch)
tree0264321d30996058a3b120cff7707e93562bb425
parent3421d2bd1f12dc51916df1869ec8beab8711faec (diff)
downloadpdfium-4540fbaa718b0b06e076321e0e1a61e80a161499.tar.xz
Get CPDF_Parser out of the document creation business.
Precursor to someday using possibly subclassed documents. Review-Url: https://codereview.chromium.org/2248123002
-rw-r--r--core/fpdfapi/fpdf_parser/cpdf_parser.cpp14
-rw-r--r--core/fpdfapi/fpdf_parser/include/cpdf_parser.h10
-rw-r--r--fpdfsdk/fpdf_dataavail.cpp11
-rw-r--r--fpdfsdk/fpdfview.cpp40
4 files changed, 42 insertions, 33 deletions
diff --git a/core/fpdfapi/fpdf_parser/cpdf_parser.cpp b/core/fpdfapi/fpdf_parser/cpdf_parser.cpp
index e2c88d7e71..7bcbf392d6 100644
--- a/core/fpdfapi/fpdf_parser/cpdf_parser.cpp
+++ b/core/fpdfapi/fpdf_parser/cpdf_parser.cpp
@@ -158,7 +158,9 @@ void CPDF_Parser::CloseParser() {
}
}
-CPDF_Parser::Error CPDF_Parser::StartParse(IFX_FileRead* pFileAccess) {
+CPDF_Parser::Error CPDF_Parser::StartParse(
+ IFX_FileRead* pFileAccess,
+ std::unique_ptr<CPDF_Document> pDocument) {
CloseParser();
m_bXRefStream = FALSE;
@@ -188,7 +190,7 @@ CPDF_Parser::Error CPDF_Parser::StartParse(IFX_FileRead* pFileAccess) {
return FORMAT_ERROR;
m_pSyntax->RestorePos(m_pSyntax->m_FileLen - m_pSyntax->m_HeaderOffset - 9);
- m_pDocument.reset(new CPDF_Document(this));
+ m_pDocument = std::move(pDocument);
FX_BOOL bXRefRebuilt = FALSE;
if (m_pSyntax->SearchWord("startxref", TRUE, FALSE, 4096)) {
@@ -1543,7 +1545,9 @@ FX_BOOL CPDF_Parser::IsLinearizedFile(IFX_FileRead* pFileAccess,
return FALSE;
}
-CPDF_Parser::Error CPDF_Parser::StartAsyncParse(IFX_FileRead* pFileAccess) {
+CPDF_Parser::Error CPDF_Parser::StartAsyncParse(
+ IFX_FileRead* pFileAccess,
+ std::unique_ptr<CPDF_Document> pDocument) {
CloseParser();
m_bXRefStream = FALSE;
m_LastXRefOffset = 0;
@@ -1555,10 +1559,10 @@ CPDF_Parser::Error CPDF_Parser::StartAsyncParse(IFX_FileRead* pFileAccess) {
if (!IsLinearizedFile(pFileAccess, offset)) {
m_pSyntax->m_pFileAccess = nullptr;
- return StartParse(pFileAccess);
+ return StartParse(pFileAccess, std::move(pDocument));
}
- m_pDocument.reset(new CPDF_Document(this));
+ m_pDocument = std::move(pDocument);
FX_FILESIZE dwFirstXRefOffset = m_pSyntax->SavePos();
FX_BOOL bXRefRebuilt = FALSE;
diff --git a/core/fpdfapi/fpdf_parser/include/cpdf_parser.h b/core/fpdfapi/fpdf_parser/include/cpdf_parser.h
index 1291180197..3ad3b867df 100644
--- a/core/fpdfapi/fpdf_parser/include/cpdf_parser.h
+++ b/core/fpdfapi/fpdf_parser/include/cpdf_parser.h
@@ -37,8 +37,11 @@ class CPDF_Parser {
CPDF_Parser();
~CPDF_Parser();
- Error StartParse(IFX_FileRead* pFile);
- uint32_t GetPermissions() const;
+ Error StartParse(IFX_FileRead* pFile,
+ std::unique_ptr<CPDF_Document> pDocument);
+
+ Error StartAsyncParse(IFX_FileRead* pFile,
+ std::unique_ptr<CPDF_Document> pDocument);
void SetPassword(const FX_CHAR* password) { m_Password = password; }
CFX_ByteString GetPassword() { return m_Password; }
@@ -46,6 +49,7 @@ class CPDF_Parser {
FX_FILESIZE GetLastXRefOffset() const { return m_LastXRefOffset; }
CPDF_Document* GetDocument() const { return m_pDocument.get(); }
+ uint32_t GetPermissions() const;
uint32_t GetRootObjNum();
uint32_t GetInfoObjNum();
CPDF_Array* GetIDArray();
@@ -83,8 +87,6 @@ class CPDF_Parser {
uint32_t objnum,
FX_FILESIZE* pResultPos);
- Error StartAsyncParse(IFX_FileRead* pFile);
-
uint32_t GetFirstPageNo() const { return m_dwFirstPageNo; }
protected:
diff --git a/fpdfsdk/fpdf_dataavail.cpp b/fpdfsdk/fpdf_dataavail.cpp
index b7b47f86a1..3cc7fc0347 100644
--- a/fpdfsdk/fpdf_dataavail.cpp
+++ b/fpdfsdk/fpdf_dataavail.cpp
@@ -136,18 +136,19 @@ FPDFAvail_GetDocument(FPDF_AVAIL avail, FPDF_BYTESTRING password) {
if (!pDataAvail)
return nullptr;
- CPDF_Parser* pParser = new CPDF_Parser;
+ std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
pParser->SetPassword(password);
- CPDF_Parser::Error error =
- pParser->StartAsyncParse(pDataAvail->m_pDataAvail->GetFileRead());
+
+ std::unique_ptr<CPDF_Document> pDocument(new CPDF_Document(pParser.get()));
+ CPDF_Parser::Error error = pParser->StartAsyncParse(
+ pDataAvail->m_pDataAvail->GetFileRead(), std::move(pDocument));
if (error != CPDF_Parser::SUCCESS) {
- delete pParser;
ProcessParseError(error);
return nullptr;
}
pDataAvail->m_pDataAvail->SetDocument(pParser->GetDocument());
CheckUnSupportError(pParser->GetDocument(), FPDF_ERR_SUCCESS);
- return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
+ return FPDFDocumentFromCPDFDocument(pParser.release()->GetDocument());
}
DLLEXPORT int STDCALL FPDFAvail_GetFirstPageNum(FPDF_DOCUMENT doc) {
diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp
index 8aebb550ff..29fe88d943 100644
--- a/fpdfsdk/fpdfview.cpp
+++ b/fpdfsdk/fpdfview.cpp
@@ -362,24 +362,25 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
return nullptr;
}
- CPDF_Parser* pParser = new CPDF_Parser;
+ std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
pParser->SetPassword(password);
- CPDF_Parser::Error error = pParser->StartParse(pFileAccess);
+ std::unique_ptr<CPDF_Document> pDocument(new CPDF_Document(pParser.get()));
+ CPDF_Parser::Error error =
+ pParser->StartParse(pFileAccess, std::move(pDocument));
if (error != CPDF_Parser::SUCCESS) {
- delete pParser;
ProcessParseError(error);
return nullptr;
}
#ifdef PDF_ENABLE_XFA
- CPDF_Document* pPDFDoc = pParser->GetDocument();
+ CPDF_Document* pPDFDoc = pParser.release()->GetDocument();
if (!pPDFDoc)
return nullptr;
CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
return new CPDFXFA_Document(pPDFDoc, pProvider);
#else // PDF_ENABLE_XFA
- return pParser->GetDocument();
+ return pParser.release()->GetDocument();
#endif // PDF_ENABLE_XFA
}
@@ -446,35 +447,36 @@ class CMemFile final : public IFX_FileRead {
DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
int size,
FPDF_BYTESTRING password) {
- CPDF_Parser* pParser = new CPDF_Parser;
- pParser->SetPassword(password);
CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
- CPDF_Parser::Error error = pParser->StartParse(pMemFile);
+ std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
+ pParser->SetPassword(password);
+
+ std::unique_ptr<CPDF_Document> pDocument(new CPDF_Document(pParser.get()));
+ CPDF_Parser::Error error =
+ pParser->StartParse(pMemFile, std::move(pDocument));
if (error != CPDF_Parser::SUCCESS) {
- delete pParser;
ProcessParseError(error);
return nullptr;
}
- CPDF_Document* pDoc = pParser ? pParser->GetDocument() : nullptr;
- CheckUnSupportError(pDoc, error);
- return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
+ CheckUnSupportError(pParser->GetDocument(), error);
+ return FPDFDocumentFromCPDFDocument(pParser.release()->GetDocument());
}
DLLEXPORT FPDF_DOCUMENT STDCALL
FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
FPDF_BYTESTRING password) {
- CPDF_Parser* pParser = new CPDF_Parser;
- pParser->SetPassword(password);
CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
- CPDF_Parser::Error error = pParser->StartParse(pFile);
+ std::unique_ptr<CPDF_Parser> pParser(new CPDF_Parser);
+ pParser->SetPassword(password);
+
+ std::unique_ptr<CPDF_Document> pDocument(new CPDF_Document(pParser.get()));
+ CPDF_Parser::Error error = pParser->StartParse(pFile, std::move(pDocument));
if (error != CPDF_Parser::SUCCESS) {
- delete pParser;
ProcessParseError(error);
return nullptr;
}
- CPDF_Document* pDoc = pParser ? pParser->GetDocument() : nullptr;
- CheckUnSupportError(pDoc, error);
- return FPDFDocumentFromCPDFDocument(pParser->GetDocument());
+ CheckUnSupportError(pParser->GetDocument(), error);
+ return FPDFDocumentFromCPDFDocument(pParser.release()->GetDocument());
}
DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,