Use retained references in fpdfsdk/fpdf_*.cpp files

Change-Id: Ie9b0e6b14d3e6f4ffe8c8f585042a48aba9ac032
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98614
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index 38b7cf8..8ccd845 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -179,9 +179,9 @@
 }
 
 // Loads the charcode to unicode mapping into a stream
-// TODO(tsepez): return retained result.
-CPDF_Stream* LoadUnicode(CPDF_Document* pDoc,
-                         const std::multimap<uint32_t, uint32_t>& to_unicode) {
+RetainPtr<CPDF_Stream> LoadUnicode(
+    CPDF_Document* pDoc,
+    const std::multimap<uint32_t, uint32_t>& to_unicode) {
   // A map charcode->unicode
   std::map<uint32_t, uint32_t> char_to_uni;
   // A map <char_start, char_end> to vector v of unicode characters of size (end
@@ -286,7 +286,7 @@
   // TODO(npm): Encrypt / Compress?
   auto stream = pDoc->NewIndirect<CPDF_Stream>();
   stream->SetDataFromStringstream(&buffer);
-  return stream.Get();
+  return stream;
 }
 
 RetainPtr<CPDF_Font> LoadSimpleFont(CPDF_Document* pDoc,
@@ -444,7 +444,7 @@
   auto pDescendant = pFontDict->SetNewFor<CPDF_Array>("DescendantFonts");
   pDescendant->AppendNew<CPDF_Reference>(pDoc, pCIDFont->GetObjNum());
 
-  CPDF_Stream* toUnicodeStream = LoadUnicode(pDoc, to_unicode);
+  RetainPtr<CPDF_Stream> toUnicodeStream = LoadUnicode(pDoc, to_unicode);
   pFontDict->SetNewFor<CPDF_Reference>("ToUnicode", pDoc,
                                        toUnicodeStream->GetObjNum());
   return CPDF_DocPageData::FromDocument(pDoc)->GetFont(pFontDict);
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index aa99541..025545b 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -180,13 +180,13 @@
   return "q 1 0 0 1 0 0 cm /" + key + " Do Q";
 }
 
-// TODO(tsepez): return retained reference.
-CPDF_Object* NewIndirectContentsStream(CPDF_Document* pDocument,
-                                       const ByteString& contents) {
+RetainPtr<CPDF_Reference> NewIndirectContentsStreamReference(
+    CPDF_Document* pDocument,
+    const ByteString& contents) {
   auto pNewContents = pDocument->NewIndirect<CPDF_Stream>(
       nullptr, 0, pDocument->New<CPDF_Dictionary>());
   pNewContents->SetData(contents.raw_span());
-  return pNewContents.Get();
+  return pNewContents->MakeReference(pDocument);
 }
 
 void SetPageContents(const ByteString& key,
@@ -198,10 +198,9 @@
       pPage->GetMutableStreamFor(pdfium::page_object::kContents);
   if (!pContentsStream && !pContentsArray) {
     if (!key.IsEmpty()) {
-      pPage->SetFor(
-          pdfium::page_object::kContents,
-          NewIndirectContentsStream(pDocument, GenerateFlattenedContent(key))
-              ->MakeReference(pDocument));
+      pPage->SetFor(pdfium::page_object::kContents,
+                    NewIndirectContentsStreamReference(
+                        pDocument, GenerateFlattenedContent(key)));
     }
     return;
   }
@@ -209,9 +208,8 @@
   pPage->ConvertToIndirectObjectFor(pdfium::page_object::kContents, pDocument);
   if (pContentsArray) {
     pContentsArray->InsertAt(
-        0, NewIndirectContentsStream(pDocument, "q")->MakeReference(pDocument));
-    pContentsArray->Append(
-        NewIndirectContentsStream(pDocument, "Q")->MakeReference(pDocument));
+        0, NewIndirectContentsStreamReference(pDocument, "q"));
+    pContentsArray->Append(NewIndirectContentsStreamReference(pDocument, "Q"));
   } else {
     ByteString sStream = "q\n";
     {
@@ -228,9 +226,8 @@
                                      pContentsArray->GetObjNum());
   }
   if (!key.IsEmpty()) {
-    pContentsArray->Append(
-        NewIndirectContentsStream(pDocument, GenerateFlattenedContent(key))
-            ->MakeReference(pDocument));
+    pContentsArray->Append(NewIndirectContentsStreamReference(
+        pDocument, GenerateFlattenedContent(key)));
   }
 }
 
diff --git a/fpdfsdk/fpdf_ppo.cpp b/fpdfsdk/fpdf_ppo.cpp
index 4cb2e59..670bc3a 100644
--- a/fpdfsdk/fpdf_ppo.cpp
+++ b/fpdfsdk/fpdf_ppo.cpp
@@ -149,7 +149,7 @@
   return CalculatePageEdit(iSubX, iSubY, pagesize);
 }
 
-const CPDF_Object* PageDictGetInheritableTag(
+RetainPtr<const CPDF_Object> PageDictGetInheritableTag(
     RetainPtr<const CPDF_Dictionary> pDict,
     const ByteString& bsSrcTag) {
   if (!pDict || bsSrcTag.IsEmpty())
@@ -169,13 +169,12 @@
   if (!pp)
     return nullptr;
 
-  // TODO(tsepez): return retained objects throughout.
   if (pDict->KeyExist(bsSrcTag))
-    return pDict->GetObjectFor(bsSrcTag).Get();
+    return pDict->GetObjectFor(bsSrcTag);
 
   while (pp) {
     if (pp->KeyExist(bsSrcTag))
-      return pp->GetObjectFor(bsSrcTag).Get();
+      return pp->GetObjectFor(bsSrcTag);
     if (!pp->KeyExist(pdfium::page_object::kParent))
       break;
     pp = ToDictionary(
@@ -190,7 +189,7 @@
   if (pDestPageDict->KeyExist(key))
     return true;
 
-  const CPDF_Object* pInheritable =
+  RetainPtr<const CPDF_Object> pInheritable =
       PageDictGetInheritableTag(std::move(pSrcPageDict), key);
   if (!pInheritable)
     return false;
@@ -422,7 +421,7 @@
                          pdfium::page_object::kMediaBox)) {
       // Search for "CropBox" in the source page dictionary.
       // If it does not exist, use the default letter size.
-      const CPDF_Object* pInheritable = PageDictGetInheritableTag(
+      RetainPtr<const CPDF_Object> pInheritable = PageDictGetInheritableTag(
           pSrcPageDict, pdfium::page_object::kCropBox);
       if (pInheritable) {
         pDestPageDict->SetFor(pdfium::page_object::kMediaBox,
@@ -496,8 +495,8 @@
 
   // Creates an XObject from |pSrcPage|. Updates mapping as needed.
   // Returns the name of the newly created XObject.
-  ByteString MakeXObjectFromPage(const RetainPtr<CPDF_Page>& pSrcPage);
-  CPDF_Stream* MakeXObjectFromPageRaw(const RetainPtr<CPDF_Page>& pSrcPage);
+  ByteString MakeXObjectFromPage(RetainPtr<CPDF_Page> pSrcPage);
+  RetainPtr<CPDF_Stream> MakeXObjectFromPageRaw(RetainPtr<CPDF_Page> pSrcPage);
 
   // Adds |bsContent| as the Contents key in |pDestPageDict|.
   // Adds the objects in |m_XObjectNameToNumberMap| to the XObject dictionary in
@@ -600,9 +599,8 @@
   return ByteString(contentStream);
 }
 
-// TODO(tsepez): return retained object.
-CPDF_Stream* CPDF_NPageToOneExporter::MakeXObjectFromPageRaw(
-    const RetainPtr<CPDF_Page>& pSrcPage) {
+RetainPtr<CPDF_Stream> CPDF_NPageToOneExporter::MakeXObjectFromPageRaw(
+    RetainPtr<CPDF_Page> pSrcPage) {
   // TODO(tsepez): return retained object from CPDF_Page::GetDict()'
   RetainPtr<const CPDF_Dictionary> pSrcPageDict(pSrcPage->GetDict());
   RetainPtr<const CPDF_Object> pSrcContentObj =
@@ -645,12 +643,12 @@
     }
     pNewXObject->SetDataAndRemoveFilter(bsSrcContentStream.raw_span());
   }
-  return pNewXObject.Get();
+  return pNewXObject;
 }
 
 ByteString CPDF_NPageToOneExporter::MakeXObjectFromPage(
-    const RetainPtr<CPDF_Page>& pSrcPage) {
-  CPDF_Stream* pNewXObject = MakeXObjectFromPageRaw(pSrcPage);
+    RetainPtr<CPDF_Page> pSrcPage) {
+  RetainPtr<CPDF_Stream> pNewXObject = MakeXObjectFromPageRaw(pSrcPage);
 
   // TODO(xlou): A better name schema to avoid possible object name collision.
   ByteString bsXObjectName = ByteString::Format("X%d", ++m_nObjectNumber);
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index 6c19b01..a727a7b 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -136,22 +136,21 @@
 #endif
 }
 
-const CPDF_Object* GetXFAEntryFromDocument(const CPDF_Document* doc) {
+RetainPtr<const CPDF_Object> GetXFAEntryFromDocument(const CPDF_Document* doc) {
   const CPDF_Dictionary* root = doc->GetRoot();
   if (!root)
     return nullptr;
 
-  // TODO(tsepez): return retained objects.
   RetainPtr<const CPDF_Dictionary> acro_form = root->GetDictFor("AcroForm");
-  return acro_form ? acro_form->GetObjectFor("XFA").Get() : nullptr;
+  return acro_form ? acro_form->GetObjectFor("XFA") : nullptr;
 }
 
 struct XFAPacket {
   ByteString name;
-  const CPDF_Stream* data;
+  RetainPtr<const CPDF_Stream> data;
 };
 
-std::vector<XFAPacket> GetXFAPackets(const CPDF_Object* xfa_object) {
+std::vector<XFAPacket> GetXFAPackets(RetainPtr<const CPDF_Object> xfa_object) {
   std::vector<XFAPacket> packets;
 
   if (!xfa_object)
@@ -159,8 +158,7 @@
 
   RetainPtr<const CPDF_Stream> xfa_stream = ToStream(xfa_object->GetDirect());
   if (xfa_stream) {
-    // TODO(tsepez): push retained objects.
-    packets.push_back({"", xfa_stream.Get()});
+    packets.push_back({"", std::move(xfa_stream)});
     return packets;
   }
 
@@ -181,8 +179,7 @@
     if (!data)
       continue;
 
-    // TODO(tsepez): push retained objects.
-    packets.push_back({name->GetString(), data.Get()});
+    packets.push_back({name->GetString(), std::move(data)});
   }
   return packets;
 }