Make CPDF_FormField::GetFieldAttr() a non-static method

Most callers want to invoke this on the field itself, so we can avoid
the step of passing a dictionary back to the caller. Rename existing
method to GetFieldAttrForDict() to handle odd cases where there isn't
a form field to directly supply the dict.

Change-Id: Icfcde3cfc0c02a595ae9f9119ba24eb24dec6a71
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98790
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfdoc/cpdf_annotlist.cpp b/core/fpdfdoc/cpdf_annotlist.cpp
index ba48654..f6c8469 100644
--- a/core/fpdfdoc/cpdf_annotlist.cpp
+++ b/core/fpdfdoc/cpdf_annotlist.cpp
@@ -129,7 +129,7 @@
   }
 
   CPDF_Object* pFieldTypeObj =
-      CPDF_FormField::GetFieldAttr(pAnnotDict, pdfium::form_fields::kFT);
+      CPDF_FormField::GetFieldAttrForDict(pAnnotDict, pdfium::form_fields::kFT);
   if (!pFieldTypeObj)
     return;
 
@@ -141,7 +141,7 @@
   }
 
   CPDF_Object* pFieldFlagsObj =
-      CPDF_FormField::GetFieldAttr(pAnnotDict, pdfium::form_fields::kFf);
+      CPDF_FormField::GetFieldAttrForDict(pAnnotDict, pdfium::form_fields::kFf);
   uint32_t flags = pFieldFlagsObj ? pFieldFlagsObj->GetInteger() : 0;
   if (field_type == pdfium::form_fields::kCh) {
     auto type = (flags & pdfium::form_flags::kChoiceCombo)
diff --git a/core/fpdfdoc/cpdf_bafontmap.cpp b/core/fpdfdoc/cpdf_bafontmap.cpp
index 1ce8ea6..cc38d4f 100644
--- a/core/fpdfdoc/cpdf_bafontmap.cpp
+++ b/core/fpdfdoc/cpdf_bafontmap.cpp
@@ -238,13 +238,13 @@
 
   ByteString sDA;
   const CPDF_Object* pObj =
-      CPDF_FormField::GetFieldAttr(m_pAnnotDict.Get(), "DA");
+      CPDF_FormField::GetFieldAttrForDict(m_pAnnotDict.Get(), "DA");
   if (pObj)
     sDA = pObj->GetString();
 
   if (bWidget) {
     if (sDA.IsEmpty()) {
-      pObj = CPDF_FormField::GetFieldAttr(pAcroFormDict.Get(), "DA");
+      pObj = CPDF_FormField::GetFieldAttrForDict(pAcroFormDict.Get(), "DA");
       sDA = pObj ? pObj->GetString() : ByteString();
     }
   }
diff --git a/core/fpdfdoc/cpdf_formcontrol.cpp b/core/fpdfdoc/cpdf_formcontrol.cpp
index c5e1b11..6d85e33 100644
--- a/core/fpdfdoc/cpdf_formcontrol.cpp
+++ b/core/fpdfdoc/cpdf_formcontrol.cpp
@@ -76,7 +76,7 @@
   DCHECK(GetType() == CPDF_FormField::kCheckBox ||
          GetType() == CPDF_FormField::kRadioButton);
   ByteString csOn = GetOnStateName();
-  if (ToArray(CPDF_FormField::GetFieldAttr(m_pField->GetFieldDict(), "Opt")))
+  if (ToArray(m_pField->GetFieldAttr("Opt")))
     csOn = ByteString::FormatInteger(m_pField->GetControlIndex(this));
   if (csOn.IsEmpty())
     csOn = "Yes";
@@ -87,8 +87,7 @@
   DCHECK(GetType() == CPDF_FormField::kCheckBox ||
          GetType() == CPDF_FormField::kRadioButton);
   ByteString csOn = GetOnStateName();
-  const CPDF_Array* pArray =
-      ToArray(CPDF_FormField::GetFieldAttr(m_pField->GetFieldDict(), "Opt"));
+  const CPDF_Array* pArray = ToArray(m_pField->GetFieldAttr("Opt"));
   if (pArray)
     csOn = pArray->GetByteStringAt(m_pField->GetControlIndex(this));
   if (csOn.IsEmpty())
@@ -107,8 +106,7 @@
 bool CPDF_FormControl::IsDefaultChecked() const {
   DCHECK(GetType() == CPDF_FormField::kCheckBox ||
          GetType() == CPDF_FormField::kRadioButton);
-  const CPDF_Object* pDV =
-      CPDF_FormField::GetFieldAttr(m_pField->GetFieldDict(), "DV");
+  const CPDF_Object* pDV = m_pField->GetFieldAttr("DV");
   if (!pDV)
     return false;
 
@@ -187,8 +185,7 @@
     return CPDF_DefaultAppearance(
         m_pWidgetDict->GetByteStringFor(pdfium::form_fields::kDA));
   }
-  const CPDF_Object* pObj = CPDF_FormField::GetFieldAttr(
-      m_pField->GetFieldDict(), pdfium::form_fields::kDA);
+  const CPDF_Object* pObj = m_pField->GetFieldAttr(pdfium::form_fields::kDA);
   if (!pObj)
     return m_pForm->GetDefaultAppearance();
 
@@ -210,7 +207,8 @@
   if (!csFontNameTag.has_value() || csFontNameTag->IsEmpty())
     return nullptr;
 
-  CPDF_Object* pObj = CPDF_FormField::GetFieldAttr(m_pWidgetDict.Get(), "DR");
+  CPDF_Object* pObj =
+      CPDF_FormField::GetFieldAttrForDict(m_pWidgetDict.Get(), "DR");
   if (CPDF_Dictionary* pDict = ToDictionary(pObj)) {
     RetainPtr<CPDF_Dictionary> pFonts = pDict->GetMutableDictFor("Font");
     if (ValidateFontResourceDict(pFonts.Get())) {
@@ -229,8 +227,8 @@
     return pFormFont;
 
   RetainPtr<CPDF_Dictionary> pPageDict = m_pWidgetDict->GetMutableDictFor("P");
-  CPDF_Dictionary* pDict =
-      ToDictionary(CPDF_FormField::GetFieldAttr(pPageDict.Get(), "Resources"));
+  CPDF_Dictionary* pDict = ToDictionary(
+      CPDF_FormField::GetFieldAttrForDict(pPageDict.Get(), "Resources"));
   if (!pDict)
     return nullptr;
 
@@ -251,8 +249,7 @@
   if (m_pWidgetDict->KeyExist(pdfium::form_fields::kQ))
     return m_pWidgetDict->GetIntegerFor(pdfium::form_fields::kQ, 0);
 
-  const CPDF_Object* pObj = CPDF_FormField::GetFieldAttr(
-      m_pField->GetFieldDict(), pdfium::form_fields::kQ);
+  const CPDF_Object* pObj = m_pField->GetFieldAttr(pdfium::form_fields::kQ);
   if (pObj)
     return pObj->GetInteger();
 
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp
index 4e90b7e..3e83912 100644
--- a/core/fpdfdoc/cpdf_formfield.cpp
+++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -61,15 +61,15 @@
 }
 
 // static
-const CPDF_Object* CPDF_FormField::GetFieldAttr(
+const CPDF_Object* CPDF_FormField::GetFieldAttrForDict(
     const CPDF_Dictionary* pFieldDict,
     const ByteString& name) {
   return GetFieldAttrRecursive(pFieldDict, name, 0);
 }
 
 // static
-CPDF_Object* CPDF_FormField::GetFieldAttr(CPDF_Dictionary* pFieldDict,
-                                          const ByteString& name) {
+CPDF_Object* CPDF_FormField::GetFieldAttrForDict(CPDF_Dictionary* pFieldDict,
+                                                 const ByteString& name) {
   return const_cast<CPDF_Object*>(GetFieldAttrRecursive(
       static_cast<const CPDF_Dictionary*>(pFieldDict), name, 0));
 }
@@ -105,8 +105,7 @@
 CPDF_FormField::~CPDF_FormField() = default;
 
 void CPDF_FormField::InitFieldFlags() {
-  const CPDF_Object* ft_attr =
-      GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kFT);
+  const CPDF_Object* ft_attr = GetFieldAttr(pdfium::form_fields::kFT);
   ByteString type_name = ft_attr ? ft_attr->GetString() : ByteString();
   uint32_t flags = GetFieldFlags();
   m_bRequired = flags & pdfium::form_flags::kRequired;
@@ -193,7 +192,7 @@
           csValue = pV->GetUnicodeText();
       }
 
-      bool bHasRV = !!GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kRV);
+      bool bHasRV = !!GetFieldAttr(pdfium::form_fields::kRV);
       if (!bHasRV && (csDValue == csValue))
         return false;
 
@@ -269,25 +268,22 @@
 }
 
 CPDF_AAction CPDF_FormField::GetAdditionalAction() const {
-  CPDF_Object* pObj = GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kAA);
+  const CPDF_Object* pObj = GetFieldAttr(pdfium::form_fields::kAA);
   return CPDF_AAction(pObj ? pObj->GetDict() : nullptr);
 }
 
 WideString CPDF_FormField::GetAlternateName() const {
-  const CPDF_Object* pObj =
-      GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kTU);
+  const CPDF_Object* pObj = GetFieldAttr(pdfium::form_fields::kTU);
   return pObj ? pObj->GetUnicodeText() : WideString();
 }
 
 WideString CPDF_FormField::GetMappingName() const {
-  const CPDF_Object* pObj =
-      GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kTM);
+  const CPDF_Object* pObj = GetFieldAttr(pdfium::form_fields::kTM);
   return pObj ? pObj->GetUnicodeText() : WideString();
 }
 
 uint32_t CPDF_FormField::GetFieldFlags() const {
-  const CPDF_Object* pObj =
-      GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kFf);
+  const CPDF_Object* pObj = GetFieldAttr(pdfium::form_fields::kFf);
   return pObj ? pObj->GetInteger() : 0;
 }
 
@@ -402,7 +398,8 @@
 }
 
 int CPDF_FormField::GetMaxLen() const {
-  if (const CPDF_Object* pObj = GetFieldAttr(m_pDict.Get(), "MaxLen"))
+  const CPDF_Object* pObj = GetFieldAttr("MaxLen");
+  if (pObj)
     return pObj->GetInteger();
 
   for (auto& pControl : GetControls()) {
@@ -551,12 +548,12 @@
 }
 
 int CPDF_FormField::CountOptions() const {
-  const CPDF_Array* pArray = ToArray(GetFieldAttr(m_pDict.Get(), "Opt"));
+  const CPDF_Array* pArray = ToArray(GetFieldAttr("Opt"));
   return pArray ? fxcrt::CollectionSize<int>(*pArray) : 0;
 }
 
 WideString CPDF_FormField::GetOptionText(int index, int sub_index) const {
-  const CPDF_Array* pArray = ToArray(GetFieldAttr(m_pDict.Get(), "Opt"));
+  const CPDF_Array* pArray = ToArray(GetFieldAttr("Opt"));
   if (!pArray)
     return WideString();
 
@@ -623,7 +620,7 @@
     }
   }
 
-  const CPDF_Object* pOpt = GetFieldAttr(m_pDict.Get(), "Opt");
+  const CPDF_Object* pOpt = GetFieldAttr("Opt");
   if (!ToArray(pOpt)) {
     ByteString csBExport = PDF_EncodeText(csWExport.AsStringView());
     if (bChecked) {
@@ -683,7 +680,7 @@
 }
 
 int CPDF_FormField::GetTopVisibleIndex() const {
-  const CPDF_Object* pObj = GetFieldAttr(m_pDict.Get(), "TI");
+  const CPDF_Object* pObj = GetFieldAttr("TI");
   return pObj ? pObj->GetInteger() : 0;
 }
 
@@ -880,16 +877,16 @@
 }
 
 const CPDF_Object* CPDF_FormField::GetDefaultValueObject() const {
-  return GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kDV);
+  return GetFieldAttr(pdfium::form_fields::kDV);
 }
 
 const CPDF_Object* CPDF_FormField::GetValueObject() const {
-  return GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kV);
+  return GetFieldAttr(pdfium::form_fields::kV);
 }
 
 const CPDF_Object* CPDF_FormField::GetSelectedIndicesObject() const {
   DCHECK(GetType() == kComboBox || GetType() == kListBox);
-  return GetFieldAttr(m_pDict.Get(), "I");
+  return GetFieldAttr("I");
 }
 
 const CPDF_Object* CPDF_FormField::GetValueOrSelectedIndicesObject() const {
diff --git a/core/fpdfdoc/cpdf_formfield.h b/core/fpdfdoc/cpdf_formfield.h
index b49c0a3..db8952b 100644
--- a/core/fpdfdoc/cpdf_formfield.h
+++ b/core/fpdfdoc/cpdf_formfield.h
@@ -73,17 +73,20 @@
   ~CPDF_FormField();
 
   static absl::optional<FormFieldType> IntToFormFieldType(int value);
-
-  static const CPDF_Object* GetFieldAttr(const CPDF_Dictionary* pFieldDict,
-                                         const ByteString& name);
-  static CPDF_Object* GetFieldAttr(CPDF_Dictionary* pFieldDict,
-                                   const ByteString& name);
-
   static WideString GetFullNameForDict(const CPDF_Dictionary* pFieldDict);
+  static const CPDF_Object* GetFieldAttrForDict(
+      const CPDF_Dictionary* pFieldDict,
+      const ByteString& name);
+  static CPDF_Object* GetFieldAttrForDict(CPDF_Dictionary* pFieldDict,
+                                          const ByteString& name);
 
   WideString GetFullName() const;
   Type GetType() const { return m_Type; }
 
+  const CPDF_Object* GetFieldAttr(const ByteString& name) const {
+    return GetFieldAttrForDict(m_pDict.Get(), name);
+  }
+
   const CPDF_Dictionary* GetFieldDict() const { return m_pDict.Get(); }
   bool ResetField();
 
diff --git a/core/fpdfdoc/cpdf_generateap.cpp b/core/fpdfdoc/cpdf_generateap.cpp
index e77bf32..50addf6 100644
--- a/core/fpdfdoc/cpdf_generateap.cpp
+++ b/core/fpdfdoc/cpdf_generateap.cpp
@@ -916,7 +916,8 @@
     return;
 
   ByteString DA;
-  if (CPDF_Object* pDAObj = CPDF_FormField::GetFieldAttr(pAnnotDict, "DA"))
+  CPDF_Object* pDAObj = CPDF_FormField::GetFieldAttrForDict(pAnnotDict, "DA");
+  if (pDAObj)
     DA = pDAObj->GetString();
   if (DA.IsEmpty())
     DA = pFormDict->GetByteStringFor("DA");
@@ -1094,16 +1095,17 @@
 
   switch (type) {
     case CPDF_GenerateAP::kTextField: {
-      const CPDF_Object* pV =
-          CPDF_FormField::GetFieldAttr(pAnnotDict, pdfium::form_fields::kV);
+      const CPDF_Object* pV = CPDF_FormField::GetFieldAttrForDict(
+          pAnnotDict, pdfium::form_fields::kV);
       WideString swValue = pV ? pV->GetUnicodeText() : WideString();
-      const CPDF_Object* pQ = CPDF_FormField::GetFieldAttr(pAnnotDict, "Q");
+      const CPDF_Object* pQ =
+          CPDF_FormField::GetFieldAttrForDict(pAnnotDict, "Q");
       int32_t nAlign = pQ ? pQ->GetInteger() : 0;
-      const CPDF_Object* pFf =
-          CPDF_FormField::GetFieldAttr(pAnnotDict, pdfium::form_fields::kFf);
+      const CPDF_Object* pFf = CPDF_FormField::GetFieldAttrForDict(
+          pAnnotDict, pdfium::form_fields::kFf);
       uint32_t dwFlags = pFf ? pFf->GetInteger() : 0;
       const CPDF_Object* pMaxLen =
-          CPDF_FormField::GetFieldAttr(pAnnotDict, "MaxLen");
+          CPDF_FormField::GetFieldAttrForDict(pAnnotDict, "MaxLen");
       uint32_t dwMaxLen = pMaxLen ? pMaxLen->GetInteger() : 0;
       CPVT_VariableText vt(&prd);
       vt.SetPlateRect(rcBody);
@@ -1157,8 +1159,8 @@
       break;
     }
     case CPDF_GenerateAP::kComboBox: {
-      const CPDF_Object* pV =
-          CPDF_FormField::GetFieldAttr(pAnnotDict, pdfium::form_fields::kV);
+      const CPDF_Object* pV = CPDF_FormField::GetFieldAttrForDict(
+          pAnnotDict, pdfium::form_fields::kV);
       WideString swValue = pV ? pV->GetUnicodeText() : WideString();
       CPVT_VariableText vt(&prd);
       CFX_FloatRect rcButton = rcBody;
@@ -1225,10 +1227,10 @@
     }
     case CPDF_GenerateAP::kListBox: {
       CPDF_Array* pOpts =
-          ToArray(CPDF_FormField::GetFieldAttr(pAnnotDict, "Opt"));
+          ToArray(CPDF_FormField::GetFieldAttrForDict(pAnnotDict, "Opt"));
       CPDF_Array* pSels =
-          ToArray(CPDF_FormField::GetFieldAttr(pAnnotDict, "I"));
-      CPDF_Object* pTi = CPDF_FormField::GetFieldAttr(pAnnotDict, "TI");
+          ToArray(CPDF_FormField::GetFieldAttrForDict(pAnnotDict, "I"));
+      CPDF_Object* pTi = CPDF_FormField::GetFieldAttrForDict(pAnnotDict, "TI");
       int32_t nTop = pTi ? pTi->GetInteger() : 0;
       fxcrt::ostringstream sBody;
       if (pOpts) {
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index 1e99f86..02c06f4 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -977,8 +977,7 @@
         pField->GetType() == CPDF_FormField::kRadioButton) {
       WideString csExport = pField->GetCheckValue(false);
       ByteString csBExport = PDF_EncodeText(csExport.AsStringView());
-      const CPDF_Object* pOpt =
-          CPDF_FormField::GetFieldAttr(pField->GetFieldDict(), "Opt");
+      const CPDF_Object* pOpt = pField->GetFieldAttr("Opt");
       if (pOpt) {
         pFieldDict->SetNewFor<CPDF_String>(pdfium::form_fields::kV, csBExport,
                                            false);
@@ -986,8 +985,7 @@
         pFieldDict->SetNewFor<CPDF_Name>(pdfium::form_fields::kV, csBExport);
       }
     } else {
-      const CPDF_Object* pV = CPDF_FormField::GetFieldAttr(
-          pField->GetFieldDict(), pdfium::form_fields::kV);
+      const CPDF_Object* pV = pField->GetFieldAttr(pdfium::form_fields::kV);
       if (pV)
         pFieldDict->SetFor(pdfium::form_fields::kV, pV->CloneDirectObject());
     }