Encapsulate the CPDF_Page annots array.

The long-term goal is to prevent the fpdfsdk/fpdf_* layer from
specifying strings for dictionary keys to lower objects (these
should know and restrict what keys are valid themselves). This
is a small step in that direction.

Change-Id: I1e5d12869dc541a4fa505808641ad988cb73f8b6
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98954
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_page.cpp b/core/fpdfapi/page/cpdf_page.cpp
index 8be52b4..6edf44c 100644
--- a/core/fpdfapi/page/cpdf_page.cpp
+++ b/core/fpdfapi/page/cpdf_page.cpp
@@ -183,6 +183,18 @@
   return (rotate < 0) ? (rotate + 4) : rotate;
 }
 
+RetainPtr<CPDF_Array> CPDF_Page::GetOrCreateAnnotsArray() {
+  return GetMutableDict()->GetOrCreateArrayFor("Annots");
+}
+
+RetainPtr<CPDF_Array> CPDF_Page::GetMutableAnnotsArray() {
+  return GetMutableDict()->GetMutableArrayFor("Annots");
+}
+
+RetainPtr<const CPDF_Array> CPDF_Page::GetAnnotsArray() const {
+  return GetDict()->GetArrayFor("Annots");
+}
+
 void CPDF_Page::SetRenderContext(std::unique_ptr<RenderContextIface> pContext) {
   DCHECK(!m_pRenderContext);
   DCHECK(pContext);
diff --git a/core/fpdfapi/page/cpdf_page.h b/core/fpdfapi/page/cpdf_page.h
index 0274fe2..3fe4a80 100644
--- a/core/fpdfapi/page/cpdf_page.h
+++ b/core/fpdfapi/page/cpdf_page.h
@@ -19,6 +19,7 @@
 #include "core/fxcrt/unowned_ptr.h"
 #include "third_party/abseil-cpp/absl/types/optional.h"
 
+class CPDF_Array;
 class CPDF_Dictionary;
 class CPDF_Document;
 class CPDF_Image;
@@ -80,6 +81,11 @@
   const CFX_SizeF& GetPageSize() const { return m_PageSize; }
   const CFX_Matrix& GetPageMatrix() const { return m_PageMatrix; }
   int GetPageRotation() const;
+
+  RetainPtr<CPDF_Array> GetOrCreateAnnotsArray();
+  RetainPtr<CPDF_Array> GetMutableAnnotsArray();
+  RetainPtr<const CPDF_Array> GetAnnotsArray() const;
+
   RenderCacheIface* GetRenderCache() const { return m_pRenderCache.get(); }
   void SetRenderCache(std::unique_ptr<RenderCacheIface> pCache) {
     m_pRenderCache = std::move(pCache);
diff --git a/core/fpdfdoc/cpdf_annotlist.cpp b/core/fpdfdoc/cpdf_annotlist.cpp
index f6c8469..7bb8fe1 100644
--- a/core/fpdfdoc/cpdf_annotlist.cpp
+++ b/core/fpdfdoc/cpdf_annotlist.cpp
@@ -172,8 +172,7 @@
 
 CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage)
     : m_pDocument(pPage->GetDocument()) {
-  RetainPtr<CPDF_Array> pAnnots =
-      pPage->GetMutableDict()->GetMutableArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnots = pPage->GetMutableAnnotsArray();
   if (!pAnnots)
     return;
 
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index 7e6a344..5d99137 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -622,8 +622,7 @@
     const CPDF_Page* pPage,
     const CFX_PointF& point,
     int* z_order) const {
-  RetainPtr<const CPDF_Array> pAnnotList =
-      pPage->GetDict()->GetArrayFor("Annots");
+  RetainPtr<const CPDF_Array> pAnnotList = pPage->GetAnnotsArray();
   if (!pAnnotList)
     return nullptr;
 
@@ -785,8 +784,7 @@
 }
 
 void CPDF_InteractiveForm::FixPageFields(CPDF_Page* pPage) {
-  RetainPtr<CPDF_Array> pAnnots =
-      pPage->GetMutableDict()->GetMutableArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnots = pPage->GetMutableAnnotsArray();
   if (!pAnnots)
     return;
 
diff --git a/core/fpdfdoc/cpdf_linklist.cpp b/core/fpdfdoc/cpdf_linklist.cpp
index a3044ee..75b4f0c 100644
--- a/core/fpdfdoc/cpdf_linklist.cpp
+++ b/core/fpdfdoc/cpdf_linklist.cpp
@@ -54,8 +54,7 @@
 
   // std::map::operator[] forces the creation of a map entry.
   auto* page_link_list = &m_PageMap[objnum];
-  RetainPtr<CPDF_Array> pAnnotList =
-      pPage->GetMutableDict()->GetMutableArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnotList = pPage->GetMutableAnnotsArray();
   if (!pAnnotList)
     return page_link_list;
 
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index b9452b8..28597dd 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -371,8 +371,7 @@
   auto pNewAnnot =
       std::make_unique<CPDF_AnnotContext>(pDict, IPDFPageFromFPDFPage(page));
 
-  RetainPtr<CPDF_Array> pAnnotList =
-      pPage->GetMutableDict()->GetOrCreateArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnotList = pPage->GetOrCreateAnnotsArray();
   pAnnotList->Append(pDict);
 
   // Caller takes ownership.
@@ -384,7 +383,7 @@
   if (!pPage)
     return 0;
 
-  RetainPtr<const CPDF_Array> pAnnots = pPage->GetDict()->GetArrayFor("Annots");
+  RetainPtr<const CPDF_Array> pAnnots = pPage->GetAnnotsArray();
   return pAnnots ? fxcrt::CollectionSize<int>(*pAnnots) : 0;
 }
 
@@ -394,8 +393,7 @@
   if (!pPage || index < 0)
     return nullptr;
 
-  RetainPtr<CPDF_Array> pAnnots =
-      pPage->GetMutableDict()->GetMutableArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnots = pPage->GetMutableAnnotsArray();
   if (!pAnnots || static_cast<size_t>(index) >= pAnnots->size())
     return nullptr;
 
@@ -421,7 +419,7 @@
   if (!pAnnotDict)
     return -1;
 
-  RetainPtr<const CPDF_Array> pAnnots = pPage->GetDict()->GetArrayFor("Annots");
+  RetainPtr<const CPDF_Array> pAnnots = pPage->GetAnnotsArray();
   if (!pAnnots)
     return -1;
 
@@ -447,8 +445,7 @@
   if (!pPage || index < 0)
     return false;
 
-  RetainPtr<CPDF_Array> pAnnots =
-      pPage->GetMutableDict()->GetMutableArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnots = pPage->GetMutableAnnotsArray();
   if (!pAnnots || static_cast<size_t>(index) >= pAnnots->size())
     return false;
 
diff --git a/fpdfsdk/fpdf_doc.cpp b/fpdfsdk/fpdf_doc.cpp
index 1e84e7a..d5c4abf 100644
--- a/fpdfsdk/fpdf_doc.cpp
+++ b/fpdfsdk/fpdf_doc.cpp
@@ -372,8 +372,7 @@
   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return false;
-  RetainPtr<CPDF_Array> pAnnots =
-      pPage->GetMutableDict()->GetMutableArrayFor("Annots");
+  RetainPtr<CPDF_Array> pAnnots = pPage->GetMutableAnnotsArray();
   if (!pAnnots)
     return false;
   for (size_t i = *start_pos; i < pAnnots->size(); i++) {