Add CPDF_IndirectObjectHolder::New<T>()
Similar to CPDF_IndirectObjectHolder::NewIndirect<T>(), but returns
an owned object that is capable of interning strings with the indirect
object holder's string pool.
Split off from a forthcoming CL. Eventually, we'd like objects created
in this manner to automatically orphan some objects back to the
indirect object holder under a few cases, and this greatly simplifies
the ability to create the proper objects.
Change-Id: I2b06b98372fa5a510fd424a07b5cb074f8035381
Reviewed-on: https://pdfium-review.googlesource.com/c/45410
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp
index 298911e..899beff 100644
--- a/core/fpdfapi/font/cpdf_font.cpp
+++ b/core/fpdfapi/font/cpdf_font.cpp
@@ -293,7 +293,7 @@
if (pFont)
return pFont;
- CPDF_Dictionary* pDict = new CPDF_Dictionary(pDoc->GetByteStringPool());
+ CPDF_Dictionary* pDict = pDoc->New<CPDF_Dictionary>().release();
pDict->SetNewFor<CPDF_Name>("Type", "Font");
pDict->SetNewFor<CPDF_Name>("Subtype", "Type1");
pDict->SetNewFor<CPDF_Name>("BaseFont", fontname);
diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp
index ec4f414..accc002 100644
--- a/core/fpdfapi/page/cpdf_image.cpp
+++ b/core/fpdfapi/page/cpdf_image.cpp
@@ -99,8 +99,7 @@
if (!IsValidJpegComponent(num_comps) || !IsValidJpegBitsPerComponent(bits))
return nullptr;
- auto pDict =
- pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
+ auto pDict = m_pDocument->New<CPDF_Dictionary>();
pDict->SetNewFor<CPDF_Name>("Type", "XObject");
pDict->SetNewFor<CPDF_Name>("Subtype", "Image");
pDict->SetNewFor<CPDF_Number>("Width", width);
@@ -179,8 +178,7 @@
if (BitmapWidth < 1 || BitmapHeight < 1)
return;
- auto pDict =
- pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
+ auto pDict = m_pDocument->New<CPDF_Dictionary>();
pDict->SetNewFor<CPDF_Name>("Type", "XObject");
pDict->SetNewFor<CPDF_Name>("Subtype", "Image");
pDict->SetNewFor<CPDF_Number>("Width", BitmapWidth);
@@ -249,8 +247,7 @@
ptr[2] = (uint8_t)argb;
ptr += 3;
}
- auto pNewDict =
- pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
+ auto pNewDict = m_pDocument->New<CPDF_Dictionary>();
CPDF_Stream* pCTS = m_pDocument->NewIndirect<CPDF_Stream>(
std::move(pColorTable), iPalette * 3, std::move(pNewDict));
pCS->Add(pCTS->MakeReference(m_pDocument.Get()));
@@ -276,8 +273,7 @@
int32_t maskHeight = pMaskBitmap->GetHeight();
std::unique_ptr<uint8_t, FxFreeDeleter> mask_buf;
int32_t mask_size = 0;
- auto pMaskDict =
- pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
+ auto pMaskDict = m_pDocument->New<CPDF_Dictionary>();
pMaskDict->SetNewFor<CPDF_Name>("Type", "XObject");
pMaskDict->SetNewFor<CPDF_Name>("Subtype", "Image");
pMaskDict->SetNewFor<CPDF_Number>("Width", maskWidth);
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
index df9fa3f..2799684 100644
--- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
@@ -371,8 +371,7 @@
}
if (param.m_Type == ContentParam::NAME) {
param.m_Type = ContentParam::OBJECT;
- param.m_pObject = pdfium::MakeUnique<CPDF_Name>(
- m_pDocument->GetByteStringPool(), param.m_Name);
+ param.m_pObject = m_pDocument->New<CPDF_Name>(param.m_Name);
return param.m_pObject.get();
}
if (param.m_Type == ContentParam::OBJECT)
@@ -615,8 +614,7 @@
void CPDF_StreamContentParser::Handle_BeginImage() {
FX_FILESIZE savePos = m_pSyntax->GetPos();
- auto pDict =
- pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
+ auto pDict = m_pDocument->New<CPDF_Dictionary>();
while (1) {
CPDF_StreamParser::SyntaxType type = m_pSyntax->ParseNextElement();
if (type == CPDF_StreamParser::Keyword) {
diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp
index c2ad8b7..880aea2 100644
--- a/core/fpdfapi/parser/cpdf_document.cpp
+++ b/core/fpdfapi/parser/cpdf_document.cpp
@@ -168,8 +168,7 @@
int descend,
std::unique_ptr<CPDF_Array> bbox,
int32_t stemV) {
- auto pFontDesc =
- pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool());
+ auto pFontDesc = pDoc->New<CPDF_Dictionary>();
pFontDesc->SetNewFor<CPDF_Name>("Type", "FontDescriptor");
pFontDesc->SetNewFor<CPDF_Name>("FontName", basefont);
pFontDesc->SetNewFor<CPDF_Number>("Flags", flags);
diff --git a/core/fpdfapi/parser/cpdf_indirect_object_holder.h b/core/fpdfapi/parser/cpdf_indirect_object_holder.h
index 1d95db1..e812044 100644
--- a/core/fpdfapi/parser/cpdf_indirect_object_holder.h
+++ b/core/fpdfapi/parser/cpdf_indirect_object_holder.h
@@ -47,6 +47,15 @@
pdfium::MakeUnique<T>(m_pByteStringPool, std::forward<Args>(args)...)));
}
+ // Creates and adds a new object not owned by the indirect object holder,
+ // but which can intern strings from it.
+ template <typename T, typename... Args>
+ typename std::enable_if<CanInternStrings<T>::value, std::unique_ptr<T>>::type
+ New(Args&&... args) {
+ return pdfium::MakeUnique<T>(m_pByteStringPool,
+ std::forward<Args>(args)...);
+ }
+
// Takes ownership of |pObj|, returns unowned pointer to it.
CPDF_Object* AddIndirectObject(std::unique_ptr<CPDF_Object> pObj);
diff --git a/core/fpdfdoc/cpdf_annotlist.cpp b/core/fpdfdoc/cpdf_annotlist.cpp
index 61ba6e9..3c0ec30 100644
--- a/core/fpdfdoc/cpdf_annotlist.cpp
+++ b/core/fpdfdoc/cpdf_annotlist.cpp
@@ -81,8 +81,7 @@
if (sContents.IsEmpty())
return nullptr;
- auto pAnnotDict =
- pdfium::MakeUnique<CPDF_Dictionary>(pDocument->GetByteStringPool());
+ auto pAnnotDict = pDocument->New<CPDF_Dictionary>();
pAnnotDict->SetNewFor<CPDF_Name>("Type", "Annot");
pAnnotDict->SetNewFor<CPDF_Name>("Subtype", "Popup");
pAnnotDict->SetNewFor<CPDF_String>("T", pParentDict->GetStringFor("T"),
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index 382d3dd..1c0a8e9 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -1025,8 +1025,7 @@
pMainDict->SetNewFor<CPDF_String>("UF", PDF_EncodeText(wsFilePath),
false);
} else {
- auto pNewDict =
- pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool());
+ auto pNewDict = pDoc->New<CPDF_Dictionary>();
pNewDict->SetNewFor<CPDF_Name>("Type", "Filespec");
CPDF_FileSpec filespec(pNewDict.get());
filespec.SetFileName(pdf_path);
@@ -1054,8 +1053,7 @@
}
WideString fullname = FPDF_GetFullName(pField->GetFieldDict());
- auto pFieldDict =
- pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool());
+ auto pFieldDict = pDoc->New<CPDF_Dictionary>();
pFieldDict->SetNewFor<CPDF_String>("T", fullname);
if (pField->GetType() == CPDF_FormField::kCheckBox ||
pField->GetType() == CPDF_FormField::kRadioButton) {
diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp
index 734173f..2dc491b 100644
--- a/core/fpdfdoc/cpvt_generateap.cpp
+++ b/core/fpdfdoc/cpvt_generateap.cpp
@@ -402,8 +402,7 @@
pFontDict->SetNewFor<CPDF_Name>("BaseFont", CFX_Font::kDefaultAnsiFontName);
pFontDict->SetNewFor<CPDF_Name>("Encoding", "WinAnsiEncoding");
- auto pResourceFontDict =
- pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool());
+ auto pResourceFontDict = pDoc->New<CPDF_Dictionary>();
pResourceFontDict->SetFor(sFontDictName, pFontDict->MakeReference(pDoc));
return pResourceFontDict;
}
@@ -489,8 +488,7 @@
CPDF_Document* pDoc,
std::unique_ptr<CPDF_Dictionary> pExtGStateDict,
std::unique_ptr<CPDF_Dictionary> pResourceFontDict) {
- auto pResourceDict =
- pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool());
+ auto pResourceDict = pDoc->New<CPDF_Dictionary>();
if (pExtGStateDict)
pResourceDict->SetFor("ExtGState", std::move(pExtGStateDict));
if (pResourceFontDict)
diff --git a/fpdfsdk/formfiller/cba_fontmap.cpp b/fpdfsdk/formfiller/cba_fontmap.cpp
index 24d1ef9..38948ce 100644
--- a/fpdfsdk/formfiller/cba_fontmap.cpp
+++ b/fpdfsdk/formfiller/cba_fontmap.cpp
@@ -168,8 +168,7 @@
CPDF_Dictionary* pStreamDict = pStream->GetDict();
if (!pStreamDict) {
- auto pOwnedDict =
- pdfium::MakeUnique<CPDF_Dictionary>(m_pDocument->GetByteStringPool());
+ auto pOwnedDict = m_pDocument->New<CPDF_Dictionary>();
pStreamDict = pOwnedDict.get();
pStream->InitStream({}, std::move(pOwnedDict));
}
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 95730e5..5a96818 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -225,8 +225,7 @@
if (!pPage || !FPDFAnnot_IsSupportedSubtype(subtype))
return nullptr;
- auto pDict = pdfium::MakeUnique<CPDF_Dictionary>(
- pPage->GetDocument()->GetByteStringPool());
+ auto pDict = pPage->GetDocument()->New<CPDF_Dictionary>();
pDict->SetNewFor<CPDF_Name>("Type", "Annot");
pDict->SetNewFor<CPDF_Name>("Subtype",
CPDF_Annot::AnnotSubtypeToString(
diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp
index 47f4caa..0f4c02e 100644
--- a/fpdfsdk/fpdf_editpage.cpp
+++ b/fpdfsdk/fpdf_editpage.cpp
@@ -117,8 +117,7 @@
// If the Params dict does not exist, create a new one.
if (!pParams) {
- auto new_dict =
- pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool());
+ auto new_dict = pDoc->New<CPDF_Dictionary>();
pParams = new_dict.get();
pMarkItem->SetDirectDict(std::move(new_dict));
}
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index 58074bd..5ac0be8 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -174,8 +174,7 @@
CPDF_Object* NewIndirectContentsStream(CPDF_Document* pDocument,
const ByteString& contents) {
CPDF_Stream* pNewContents = pDocument->NewIndirect<CPDF_Stream>(
- nullptr, 0,
- pdfium::MakeUnique<CPDF_Dictionary>(pDocument->GetByteStringPool()));
+ nullptr, 0, pDocument->New<CPDF_Dictionary>());
pNewContents->SetData(contents.AsRawSpan());
return pNewContents;
}
@@ -292,8 +291,7 @@
}
CPDF_Stream* pNewXObject = pDocument->NewIndirect<CPDF_Stream>(
- nullptr, 0,
- pdfium::MakeUnique<CPDF_Dictionary>(pDocument->GetByteStringPool()));
+ nullptr, 0, pDocument->New<CPDF_Dictionary>());
CPDF_Dictionary* pPageXObject = pRes->GetDictFor("XObject");
if (!pPageXObject)
diff --git a/fpdfsdk/fpdf_ppo.cpp b/fpdfsdk/fpdf_ppo.cpp
index e313f4a..5f1de23 100644
--- a/fpdfsdk/fpdf_ppo.cpp
+++ b/fpdfsdk/fpdf_ppo.cpp
@@ -685,8 +685,7 @@
const CPDF_Object* pSrcContentObj = GetPageOrganizerPageContent(pSrcPageDict);
CPDF_Stream* pNewXObject = dest()->NewIndirect<CPDF_Stream>(
- nullptr, 0,
- pdfium::MakeUnique<CPDF_Dictionary>(dest()->GetByteStringPool()));
+ nullptr, 0, dest()->New<CPDF_Dictionary>());
CPDF_Dictionary* pNewXObjectDict = pNewXObject->GetDict();
const ByteString bsResourceString = "Resources";
if (!CopyInheritable(pNewXObjectDict, pSrcPageDict, bsResourceString)) {
@@ -747,7 +746,7 @@
for (auto& it : xObjNameNumberMap)
pPageXObject->SetNewFor<CPDF_Reference>(it.first, dest(), it.second);
- auto pDict = pdfium::MakeUnique<CPDF_Dictionary>(dest()->GetByteStringPool());
+ auto pDict = dest()->New<CPDF_Dictionary>();
CPDF_Stream* pStream =
dest()->NewIndirect<CPDF_Stream>(nullptr, 0, std::move(pDict));
pStream->SetData(bsContent.AsRawSpan());
diff --git a/fpdfsdk/fpdf_save.cpp b/fpdfsdk/fpdf_save.cpp
index 237c262..4c72d1c 100644
--- a/fpdfsdk/fpdf_save.cpp
+++ b/fpdfsdk/fpdf_save.cpp
@@ -129,8 +129,7 @@
ToNode(ffdoc->GetXFADoc()->GetXFAObject(XFA_HASHCODE_Datasets)),
pDsfileWrite) &&
pDsfileWrite->GetSize() > 0) {
- auto pDataDict = pdfium::MakeUnique<CPDF_Dictionary>(
- pPDFDocument->GetByteStringPool());
+ auto pDataDict = pPDFDocument->New<CPDF_Dictionary>();
if (iDataSetsIndex != -1) {
if (pDataSetsStream) {
pDataSetsStream->InitStreamFromFile(pDsfileWrite,
@@ -156,8 +155,7 @@
ToNode(ffdoc->GetXFADoc()->GetXFAObject(XFA_HASHCODE_Form)),
pfileWrite) &&
pfileWrite->GetSize() > 0) {
- auto pDataDict = pdfium::MakeUnique<CPDF_Dictionary>(
- pPDFDocument->GetByteStringPool());
+ auto pDataDict = pPDFDocument->New<CPDF_Dictionary>();
if (iFormIndex != -1) {
if (pFormStream)
pFormStream->InitStreamFromFile(pfileWrite, std::move(pDataDict));
diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp
index debf38b..40ef160 100644
--- a/fpdfsdk/fpdf_transformpage.cpp
+++ b/fpdfsdk/fpdf_transformpage.cpp
@@ -191,14 +191,12 @@
if (!pDoc)
return false;
- CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>(
- nullptr, 0,
- pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()));
+ CPDF_Stream* pStream =
+ pDoc->NewIndirect<CPDF_Stream>(nullptr, 0, pDoc->New<CPDF_Dictionary>());
pStream->SetDataFromStringstream(&textBuf);
- CPDF_Stream* pEndStream = pDoc->NewIndirect<CPDF_Stream>(
- nullptr, 0,
- pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()));
+ CPDF_Stream* pEndStream =
+ pDoc->NewIndirect<CPDF_Stream>(nullptr, 0, pDoc->New<CPDF_Dictionary>());
pEndStream->SetData(ByteStringView(" Q").span());
if (CPDF_Array* pContentArray = ToArray(pContentObj)) {
@@ -353,9 +351,8 @@
if (!pDoc)
return;
- CPDF_Stream* pStream = pDoc->NewIndirect<CPDF_Stream>(
- nullptr, 0,
- pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool()));
+ CPDF_Stream* pStream =
+ pDoc->NewIndirect<CPDF_Stream>(nullptr, 0, pDoc->New<CPDF_Dictionary>());
pStream->SetDataFromStringstream(&strClip);
if (CPDF_Array* pArray = ToArray(pContentObj)) {
diff --git a/fpdfsdk/pwl/cpwl_appstream.cpp b/fpdfsdk/pwl/cpwl_appstream.cpp
index 93bd3e7..a81a4eb 100644
--- a/fpdfsdk/pwl/cpwl_appstream.cpp
+++ b/fpdfsdk/pwl/cpwl_appstream.cpp
@@ -1933,8 +1933,8 @@
CPDF_Dictionary* pStreamDict = pStream->GetDict();
if (!pStreamDict) {
- auto pNewDict = pdfium::MakeUnique<CPDF_Dictionary>(
- widget_->GetPDFAnnot()->GetDocument()->GetByteStringPool());
+ auto pNewDict =
+ widget_->GetPDFAnnot()->GetDocument()->New<CPDF_Dictionary>();
pStreamDict = pNewDict.get();
pStreamDict->SetNewFor<CPDF_Name>("Type", "XObject");
pStreamDict->SetNewFor<CPDF_Name>("Subtype", "Form");