Pass retained arguments to fpdfdoc/ class constructors.

These classes are goint to retain the argument for the lifetime of
instances of the class, so allow use of move semantics.

Affected classes are CPDF_AAction, CPDF_Annot, CPDF_FormField,
CPDF_Icon, and CPDF_Link.

Change-Id: I8fb8aaf358c12c78622852003898484db9192eb3
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98633
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfdoc/cpdf_aaction.cpp b/core/fpdfdoc/cpdf_aaction.cpp
index 3c9535a..13dd695 100644
--- a/core/fpdfdoc/cpdf_aaction.cpp
+++ b/core/fpdfdoc/cpdf_aaction.cpp
@@ -7,6 +7,7 @@
 #include "core/fpdfdoc/cpdf_aaction.h"
 
 #include <iterator>
+#include <utility>
 
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 
@@ -43,7 +44,8 @@
 
 }  // namespace
 
-CPDF_AAction::CPDF_AAction(const CPDF_Dictionary* pDict) : m_pDict(pDict) {}
+CPDF_AAction::CPDF_AAction(RetainPtr<const CPDF_Dictionary> pDict)
+    : m_pDict(std::move(pDict)) {}
 
 CPDF_AAction::CPDF_AAction(const CPDF_AAction& that) = default;
 
diff --git a/core/fpdfdoc/cpdf_aaction.h b/core/fpdfdoc/cpdf_aaction.h
index f3314e6..806ef2c 100644
--- a/core/fpdfdoc/cpdf_aaction.h
+++ b/core/fpdfdoc/cpdf_aaction.h
@@ -39,7 +39,7 @@
     kNumberOfActions  // Must be last.
   };
 
-  explicit CPDF_AAction(const CPDF_Dictionary* pDict);
+  explicit CPDF_AAction(RetainPtr<const CPDF_Dictionary> pDict);
   CPDF_AAction(const CPDF_AAction& that);
   ~CPDF_AAction();
 
diff --git a/core/fpdfdoc/cpdf_annot.cpp b/core/fpdfdoc/cpdf_annot.cpp
index 1049589..6da7eae 100644
--- a/core/fpdfdoc/cpdf_annot.cpp
+++ b/core/fpdfdoc/cpdf_annot.cpp
@@ -100,8 +100,9 @@
 
 }  // namespace
 
-CPDF_Annot::CPDF_Annot(CPDF_Dictionary* pDict, CPDF_Document* pDocument)
-    : m_pAnnotDict(pDict),
+CPDF_Annot::CPDF_Annot(RetainPtr<CPDF_Dictionary> pDict,
+                       CPDF_Document* pDocument)
+    : m_pAnnotDict(std::move(pDict)),
       m_pDocument(pDocument),
       m_nSubtype(StringToAnnotSubtype(
           m_pAnnotDict->GetByteStringFor(pdfium::annotation::kSubtype))),
diff --git a/core/fpdfdoc/cpdf_annot.h b/core/fpdfdoc/cpdf_annot.h
index f1d4b6f..cf4c7f6 100644
--- a/core/fpdfdoc/cpdf_annot.h
+++ b/core/fpdfdoc/cpdf_annot.h
@@ -73,7 +73,7 @@
                                           size_t nIndex);
   static size_t QuadPointCount(const CPDF_Array* pArray);
 
-  CPDF_Annot(CPDF_Dictionary* pDict, CPDF_Document* pDocument);
+  CPDF_Annot(RetainPtr<CPDF_Dictionary> pDict, CPDF_Document* pDocument);
   ~CPDF_Annot();
 
   Subtype GetSubtype() const;
diff --git a/core/fpdfdoc/cpdf_annotlist.cpp b/core/fpdfdoc/cpdf_annotlist.cpp
index 2cbbc91..ba48654 100644
--- a/core/fpdfdoc/cpdf_annotlist.cpp
+++ b/core/fpdfdoc/cpdf_annotlist.cpp
@@ -116,7 +116,8 @@
   pAnnotDict->SetRectFor(pdfium::annotation::kRect, popupRect);
   pAnnotDict->SetNewFor<CPDF_Number>(pdfium::annotation::kF, 0);
 
-  auto pPopupAnnot = std::make_unique<CPDF_Annot>(pAnnotDict.Get(), pDocument);
+  auto pPopupAnnot =
+      std::make_unique<CPDF_Annot>(std::move(pAnnotDict), pDocument);
   pAnnot->SetPopupAnnot(pPopupAnnot.get());
   return pPopupAnnot;
 }
@@ -194,7 +195,7 @@
     }
     pAnnots->ConvertToIndirectObjectAt(i, m_pDocument.Get());
     m_AnnotList.push_back(
-        std::make_unique<CPDF_Annot>(pDict.Get(), m_pDocument.Get()));
+        std::make_unique<CPDF_Annot>(pDict, m_pDocument.Get()));
     if (bRegenerateAP && subtype == "Widget" &&
         CPDF_InteractiveForm::IsUpdateAPEnabled() &&
         !pDict->GetDictFor(pdfium::annotation::kAP)) {
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp
index 2f9ec84..4e90b7e 100644
--- a/core/fpdfdoc/cpdf_formfield.cpp
+++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -97,8 +97,8 @@
 }
 
 CPDF_FormField::CPDF_FormField(CPDF_InteractiveForm* pForm,
-                               CPDF_Dictionary* pDict)
-    : m_pForm(pForm), m_pDict(pDict) {
+                               RetainPtr<CPDF_Dictionary> pDict)
+    : m_pForm(pForm), m_pDict(std::move(pDict)) {
   InitFieldFlags();
 }
 
@@ -270,7 +270,7 @@
 
 CPDF_AAction CPDF_FormField::GetAdditionalAction() const {
   CPDF_Object* pObj = GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kAA);
-  return CPDF_AAction(pObj ? pObj->GetDict().Get() : nullptr);
+  return CPDF_AAction(pObj ? pObj->GetDict() : nullptr);
 }
 
 WideString CPDF_FormField::GetAlternateName() const {
diff --git a/core/fpdfdoc/cpdf_formfield.h b/core/fpdfdoc/cpdf_formfield.h
index ee4495b..cafbad1 100644
--- a/core/fpdfdoc/cpdf_formfield.h
+++ b/core/fpdfdoc/cpdf_formfield.h
@@ -69,7 +69,7 @@
     kSign
   };
 
-  CPDF_FormField(CPDF_InteractiveForm* pForm, CPDF_Dictionary* pDict);
+  CPDF_FormField(CPDF_InteractiveForm* pForm, RetainPtr<CPDF_Dictionary> pDict);
   ~CPDF_FormField();
 
   static absl::optional<FormFieldType> IntToFormFieldType(int value);
diff --git a/core/fpdfdoc/cpdf_formfield_unittest.cpp b/core/fpdfdoc/cpdf_formfield_unittest.cpp
index ea51901..c3e83d2 100644
--- a/core/fpdfdoc/cpdf_formfield_unittest.cpp
+++ b/core/fpdfdoc/cpdf_formfield_unittest.cpp
@@ -54,7 +54,7 @@
 
   CPDF_TestDocument doc;
   CPDF_InteractiveForm form(&doc);
-  CPDF_FormField form_field(&form, form_dict.Get());
+  CPDF_FormField form_field(&form, std::move(form_dict));
   EXPECT_EQ(expected_use_indices, form_field.UseSelectedIndicesObject());
   for (int i = 0; i < form_field.CountOptions(); i++) {
     const bool expected_selected = pdfium::Contains(expected_indices, i);
diff --git a/core/fpdfdoc/cpdf_icon.cpp b/core/fpdfdoc/cpdf_icon.cpp
index 9417e8e..f2a9ac6 100644
--- a/core/fpdfdoc/cpdf_icon.cpp
+++ b/core/fpdfdoc/cpdf_icon.cpp
@@ -6,10 +6,13 @@
 
 #include "core/fpdfdoc/cpdf_icon.h"
 
+#include <utility>
+
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_stream.h"
 
-CPDF_Icon::CPDF_Icon(const CPDF_Stream* pStream) : m_pStream(pStream) {}
+CPDF_Icon::CPDF_Icon(RetainPtr<const CPDF_Stream> pStream)
+    : m_pStream(std::move(pStream)) {}
 
 CPDF_Icon::~CPDF_Icon() = default;
 
diff --git a/core/fpdfdoc/cpdf_icon.h b/core/fpdfdoc/cpdf_icon.h
index 62c86f7..cd6f898 100644
--- a/core/fpdfdoc/cpdf_icon.h
+++ b/core/fpdfdoc/cpdf_icon.h
@@ -15,7 +15,7 @@
 
 class CPDF_Icon final {
  public:
-  explicit CPDF_Icon(const CPDF_Stream* pStream);
+  explicit CPDF_Icon(RetainPtr<const CPDF_Stream> pStream);
   ~CPDF_Icon();
 
   CFX_SizeF GetImageSize() const;
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index b8dcba0..2babe7b 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -839,7 +839,7 @@
       }
     }
 
-    auto newField = std::make_unique<CPDF_FormField>(this, pParent.Get());
+    auto newField = std::make_unique<CPDF_FormField>(this, std::move(pParent));
     pField = newField.get();
     RetainPtr<const CPDF_Object> pTObj =
         pFieldDict->GetObjectFor(pdfium::form_fields::kT);
diff --git a/core/fpdfdoc/cpdf_link.cpp b/core/fpdfdoc/cpdf_link.cpp
index 24d1a5d..57ed88e 100644
--- a/core/fpdfdoc/cpdf_link.cpp
+++ b/core/fpdfdoc/cpdf_link.cpp
@@ -6,11 +6,14 @@
 
 #include "core/fpdfdoc/cpdf_link.h"
 
+#include <utility>
+
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 
 CPDF_Link::CPDF_Link() = default;
 
-CPDF_Link::CPDF_Link(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
+CPDF_Link::CPDF_Link(RetainPtr<CPDF_Dictionary> pDict)
+    : m_pDict(std::move(pDict)) {}
 
 CPDF_Link::CPDF_Link(const CPDF_Link& that) = default;
 
diff --git a/core/fpdfdoc/cpdf_link.h b/core/fpdfdoc/cpdf_link.h
index 43e8542..2b2faec 100644
--- a/core/fpdfdoc/cpdf_link.h
+++ b/core/fpdfdoc/cpdf_link.h
@@ -16,7 +16,7 @@
 class CPDF_Link {
  public:
   CPDF_Link();
-  explicit CPDF_Link(CPDF_Dictionary* pDict);
+  explicit CPDF_Link(RetainPtr<CPDF_Dictionary> pDict);
   CPDF_Link(const CPDF_Link& that);
   ~CPDF_Link();
 
diff --git a/core/fpdfdoc/cpdf_linklist.cpp b/core/fpdfdoc/cpdf_linklist.cpp
index cb2d20c..a3044ee 100644
--- a/core/fpdfdoc/cpdf_linklist.cpp
+++ b/core/fpdfdoc/cpdf_linklist.cpp
@@ -6,6 +6,8 @@
 
 #include "core/fpdfdoc/cpdf_linklist.h"
 
+#include <utility>
+
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
@@ -25,11 +27,11 @@
 
   for (size_t i = pPageLinkList->size(); i > 0; --i) {
     size_t annot_index = i - 1;
-    CPDF_Dictionary* pAnnot = (*pPageLinkList)[annot_index].Get();
+    RetainPtr<CPDF_Dictionary> pAnnot = (*pPageLinkList)[annot_index];
     if (!pAnnot)
       continue;
 
-    CPDF_Link link(pAnnot);
+    CPDF_Link link(std::move(pAnnot));
     if (!link.GetRect().Contains(point))
       continue;
 
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index 5b5550d..c000588 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -703,7 +703,7 @@
 }
 
 ByteString GenerateIconAppStream(CPDF_IconFit& fit,
-                                 CPDF_Stream* pIconStream,
+                                 RetainPtr<CPDF_Stream> pIconStream,
                                  const CFX_FloatRect& rcIcon) {
   if (rcIcon.IsEmpty() || !pIconStream)
     return ByteString();
@@ -715,7 +715,7 @@
   if (!pWnd->Move(rcIcon, false, false))
     return ByteString();
 
-  auto pPDFIcon = std::make_unique<CPDF_Icon>(pIconStream);
+  auto pPDFIcon = std::make_unique<CPDF_Icon>(std::move(pIconStream));
   ByteString sAlias = pPDFIcon->GetImageAlias();
   if (sAlias.GetLength() <= 0)
     return ByteString();
@@ -748,7 +748,7 @@
 
 ByteString GetPushButtonAppStream(const CFX_FloatRect& rcBBox,
                                   IPVT_FontMap* pFontMap,
-                                  CPDF_Stream* pIconStream,
+                                  RetainPtr<CPDF_Stream> pIconStream,
                                   CPDF_IconFit& IconFit,
                                   const WideString& sLabel,
                                   const CFX_Color& crText,
@@ -910,7 +910,7 @@
   }
 
   fxcrt::ostringstream sTemp;
-  sTemp << GenerateIconAppStream(IconFit, pIconStream, rcIcon);
+  sTemp << GenerateIconAppStream(IconFit, std::move(pIconStream), rcIcon);
 
   if (!rcLabel.IsEmpty()) {
     pEdit->SetPlateRect(rcLabel);
@@ -1242,8 +1242,8 @@
         GetBorderAppStreamInternal(rcWindow, fBorderWidth, crBorder, crLeftTop,
                                    crRightBottom, nBorderStyle, dsBorder) +
         GetPushButtonAppStream(iconFit.GetFittingBounds() ? rcWindow : rcClient,
-                               &font_map, pNormalIcon.Get(), iconFit,
-                               csNormalCaption, crText, fFontSize, nLayout);
+                               &font_map, pNormalIcon, iconFit, csNormalCaption,
+                               crText, fFontSize, nLayout);
 
     Write("N", csAP, ByteString());
     if (pNormalIcon)
@@ -1269,7 +1269,7 @@
         GetBorderAppStreamInternal(rcWindow, fBorderWidth, crBorder, crLeftTop,
                                    crRightBottom, nBorderStyle, dsBorder) +
         GetPushButtonAppStream(iconFit.GetFittingBounds() ? rcWindow : rcClient,
-                               &font_map, pRolloverIcon.Get(), iconFit,
+                               &font_map, pRolloverIcon, iconFit,
                                csRolloverCaption, crText, fFontSize, nLayout);
 
     Write("R", csAP, ByteString());
@@ -1305,8 +1305,8 @@
         GetBorderAppStreamInternal(rcWindow, fBorderWidth, crBorder, crLeftTop,
                                    crRightBottom, nBorderStyle, dsBorder) +
         GetPushButtonAppStream(iconFit.GetFittingBounds() ? rcWindow : rcClient,
-                               &font_map, pDownIcon.Get(), iconFit,
-                               csDownCaption, crText, fFontSize, nLayout);
+                               &font_map, pDownIcon, iconFit, csDownCaption,
+                               crText, fFontSize, nLayout);
 
     Write("D", csAP, ByteString());
     if (pDownIcon)
diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp
index a72ec83..47743c9 100644
--- a/fpdfsdk/cpdfsdk_baannot.cpp
+++ b/fpdfsdk/cpdfsdk_baannot.cpp
@@ -211,8 +211,7 @@
 }
 
 CPDF_AAction CPDFSDK_BAAnnot::GetAAction() const {
-  return CPDF_AAction(
-      GetAnnotDict()->GetDictFor(pdfium::form_fields::kAA).Get());
+  return CPDF_AAction(GetAnnotDict()->GetDictFor(pdfium::form_fields::kAA));
 }
 
 CPDF_Action CPDFSDK_BAAnnot::GetAAction(CPDF_AAction::AActionType eAAT) {
diff --git a/fpdfsdk/fpdf_doc.cpp b/fpdfsdk/fpdf_doc.cpp
index b53fa97..fcc38ba 100644
--- a/fpdfsdk/fpdf_doc.cpp
+++ b/fpdfsdk/fpdf_doc.cpp
@@ -345,7 +345,7 @@
   CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
   if (!pDoc)
     return nullptr;
-  CPDF_Link cLink(CPDFDictionaryFromFPDFLink(link));
+  CPDF_Link cLink(pdfium::WrapRetain(CPDFDictionaryFromFPDFLink(link)));
   FPDF_DEST dest = FPDFDestFromCPDFArray(cLink.GetDest(pDoc).GetArray());
   if (dest)
     return dest;
@@ -360,7 +360,7 @@
   if (!link)
     return nullptr;
 
-  CPDF_Link cLink(CPDFDictionaryFromFPDFLink(link));
+  CPDF_Link cLink(pdfium::WrapRetain(CPDFDictionaryFromFPDFLink(link)));
   return FPDFActionFromCPDFDictionary(cLink.GetAction().GetDict());
 }
 
@@ -447,7 +447,7 @@
     return nullptr;
 
   const CPDF_Dictionary* page_dict = pdf_page->GetDict();
-  CPDF_AAction aa(page_dict->GetDictFor(pdfium::form_fields::kAA).Get());
+  CPDF_AAction aa(page_dict->GetDictFor(pdfium::form_fields::kAA));
 
   CPDF_AAction::AActionType type;
   if (aa_type == FPDFPAGE_AACTION_OPEN)
diff --git a/fpdfsdk/fpdf_formfill.cpp b/fpdfsdk/fpdf_formfill.cpp
index 04e500c..2694f75 100644
--- a/fpdfsdk/fpdf_formfill.cpp
+++ b/fpdfsdk/fpdf_formfill.cpp
@@ -778,7 +778,7 @@
   if (!pDict)
     return;
 
-  CPDF_AAction aa(pDict->GetDictFor(pdfium::form_fields::kAA).Get());
+  CPDF_AAction aa(pDict->GetDictFor(pdfium::form_fields::kAA));
   auto type = static_cast<CPDF_AAction::AActionType>(aaType);
   if (aa.ActionExist(type))
     pFormFillEnv->DoActionDocument(aa.GetAction(type), type);
@@ -801,7 +801,7 @@
     return;
 
   const CPDF_Dictionary* pPageDict = pPDFPage->GetDict();
-  CPDF_AAction aa(pPageDict->GetDictFor(pdfium::form_fields::kAA).Get());
+  CPDF_AAction aa(pPageDict->GetDictFor(pdfium::form_fields::kAA));
   CPDF_AAction::AActionType type = aaType == FPDFPAGE_AACTION_OPEN
                                        ? CPDF_AAction::kOpenPage
                                        : CPDF_AAction::kClosePage;