Add helper methods for getting objects in CPDF_FormField. Consolidate calls to FPDF_GetFieldAttr() and make it more obvious to readers what objects are being retrieved. Change-Id: If348122bb23fa9e1e7857e30428cad929f74fbf7 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/59941 Reviewed-by: Henrique Nakashima <hnakashima@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp index 3de2bea..7bd99ea 100644 --- a/core/fpdfdoc/cpdf_formfield.cpp +++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -172,8 +172,7 @@ case kRichText: case kFile: default: { - const CPDF_Object* pDV = - FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kDV); + const CPDF_Object* pDV = GetDefaultValueObject(); WideString csDValue; if (pDV) csDValue = pDV->GetUnicodeText(); @@ -181,8 +180,7 @@ WideString csValue; { // Limit the scope of |pV| because it may get invalidated below. - const CPDF_Object* pV = - FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kV); + const CPDF_Object* pV = GetValueObject(); if (pV) csValue = pV->GetUnicodeText(); } @@ -294,11 +292,10 @@ return GetCheckValue(bDefault); const CPDF_Object* pValue = - FPDF_GetFieldAttr(m_pDict.Get(), bDefault ? pdfium::form_fields::kDV - : pdfium::form_fields::kV); + bDefault ? GetDefaultValueObject() : GetValueObject(); if (!pValue) { if (!bDefault && m_Type != kText) - pValue = FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kDV); + pValue = GetDefaultValueObject(); if (!pValue) return WideString(); } @@ -410,13 +407,9 @@ } int CPDF_FormField::CountSelectedItems() const { - const CPDF_Object* pValue = - FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kV); - if (!pValue) { - pValue = FPDF_GetFieldAttr(m_pDict.Get(), "I"); - if (!pValue) - return 0; - } + const CPDF_Object* pValue = GetValueOrSelectedIndicesObject(); + if (!pValue) + return 0; if (pValue->IsString() || pValue->IsNumber()) return pValue->GetString().IsEmpty() ? 0 : 1; @@ -425,13 +418,10 @@ } int CPDF_FormField::GetSelectedIndex(int index) const { - const CPDF_Object* pValue = - FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kV); - if (!pValue) { - pValue = FPDF_GetFieldAttr(m_pDict.Get(), "I"); - if (!pValue) - return -1; - } + const CPDF_Object* pValue = GetValueOrSelectedIndicesObject(); + if (!pValue) + return -1; + if (pValue->IsNumber()) return pValue->GetInteger(); @@ -485,13 +475,9 @@ return true; WideString opt_value = GetOptionValue(index); - const CPDF_Object* pValue = - FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kV); - if (!pValue) { - pValue = FPDF_GetFieldAttr(m_pDict.Get(), "I"); - if (!pValue) - return false; - } + const CPDF_Object* pValue = GetValueOrSelectedIndicesObject(); + if (!pValue) + return false; if (pValue->IsString()) return pValue->GetUnicodeText() == opt_value; @@ -562,8 +548,7 @@ void CPDF_FormField::SetItemSelectionUnselected(int index, const WideString& opt_value) { - const CPDF_Object* pValue = - FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kV); + const CPDF_Object* pValue = GetValueObject(); if (!pValue) return; @@ -604,8 +589,7 @@ int CPDF_FormField::GetDefaultSelectedItem() const { ASSERT(GetType() == kComboBox || GetType() == kListBox); - const CPDF_Object* pValue = - FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kDV); + const CPDF_Object* pValue = GetDefaultValueObject(); if (!pValue) return -1; WideString csDV = pValue->GetUnicodeText(); @@ -693,8 +677,7 @@ m_pDict->SetNewFor<CPDF_Name>(pdfium::form_fields::kV, csBExport); } else { ByteString csV; - const CPDF_Object* pV = - FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kV); + const CPDF_Object* pV = GetValueObject(); if (pV) csV = pV->GetString(); if (csV == csBExport) @@ -752,12 +735,12 @@ } int CPDF_FormField::CountSelectedOptions() const { - const CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "I")); + const CPDF_Array* pArray = ToArray(GetSelectedIndicesObject()); return pArray ? pArray->size() : 0; } int CPDF_FormField::GetSelectedOptionIndex(int index) const { - const CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "I")); + const CPDF_Array* pArray = ToArray(GetSelectedIndicesObject()); if (!pArray) return -1; @@ -768,7 +751,7 @@ } bool CPDF_FormField::IsOptionSelected(int iOptIndex) const { - const CPDF_Array* pArray = ToArray(FPDF_GetFieldAttr(m_pDict.Get(), "I")); + const CPDF_Array* pArray = ToArray(GetSelectedIndicesObject()); if (!pArray) return false; @@ -916,6 +899,25 @@ } } +const CPDF_Object* CPDF_FormField::GetDefaultValueObject() const { + return FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kDV); +} + +const CPDF_Object* CPDF_FormField::GetValueObject() const { + return FPDF_GetFieldAttr(m_pDict.Get(), pdfium::form_fields::kV); +} + +const CPDF_Object* CPDF_FormField::GetSelectedIndicesObject() const { + ASSERT(GetType() == kComboBox || GetType() == kListBox); + return FPDF_GetFieldAttr(m_pDict.Get(), "I"); +} + +const CPDF_Object* CPDF_FormField::GetValueOrSelectedIndicesObject() const { + ASSERT(GetType() == kComboBox || GetType() == kListBox); + const CPDF_Object* pValue = GetValueObject(); + return pValue ? pValue : GetSelectedIndicesObject(); +} + const std::vector<UnownedPtr<CPDF_FormControl>>& CPDF_FormField::GetControls() const { return m_pForm->GetControlsForField(this);
diff --git a/core/fpdfdoc/cpdf_formfield.h b/core/fpdfdoc/cpdf_formfield.h index 960fb0e..4c59591 100644 --- a/core/fpdfdoc/cpdf_formfield.h +++ b/core/fpdfdoc/cpdf_formfield.h
@@ -189,6 +189,16 @@ bool NotifyListOrComboBoxBeforeChange(const WideString& value); void NotifyListOrComboBoxAfterChange(); + const CPDF_Object* GetDefaultValueObject() const; + const CPDF_Object* GetValueObject() const; + + // For choice fields. + const CPDF_Object* GetSelectedIndicesObject() const; + + // For choice fields. + // Value object takes precedence over selected indices object. + const CPDF_Object* GetValueOrSelectedIndicesObject() const; + const std::vector<UnownedPtr<CPDF_FormControl>>& GetControls() const; CPDF_FormField::Type m_Type = kUnknown;