Remove page parameter from FORM_SetFocusedAnnot().

It should not be necessary, as the annot parameter, which is backed by a
CPDF_AnnotContext, already has its own page member. To make it work,
change the CPDF_AnnotContext's page member from a CPDF_Page to an
IPDF_Page.

Change-Id: I8eb92c14e691f8f97b8a26f46861b2d1ff45de94
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/68632
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_annotcontext.cpp b/core/fpdfapi/page/cpdf_annotcontext.cpp
index 1078e46..ec3a7ff 100644
--- a/core/fpdfapi/page/cpdf_annotcontext.cpp
+++ b/core/fpdfapi/page/cpdf_annotcontext.cpp
@@ -13,10 +13,11 @@
 #include "third_party/base/ptr_util.h"
 
 CPDF_AnnotContext::CPDF_AnnotContext(CPDF_Dictionary* pAnnotDict,
-                                     CPDF_Page* pPage)
+                                     IPDF_Page* pPage)
     : m_pAnnotDict(pAnnotDict), m_pPage(pPage) {
   ASSERT(m_pAnnotDict);
   ASSERT(m_pPage);
+  ASSERT(m_pPage->AsPDFPage());
 }
 
 CPDF_AnnotContext::~CPDF_AnnotContext() = default;
@@ -30,6 +31,7 @@
   pStream->GetDict()->SetMatrixFor("Matrix", CFX_Matrix());
 
   m_pAnnotForm = pdfium::MakeUnique<CPDF_Form>(
-      m_pPage->GetDocument(), m_pPage->m_pResources.Get(), pStream);
+      m_pPage->GetDocument(), m_pPage->AsPDFPage()->m_pResources.Get(),
+      pStream);
   m_pAnnotForm->ParseContent();
 }
diff --git a/core/fpdfapi/page/cpdf_annotcontext.h b/core/fpdfapi/page/cpdf_annotcontext.h
index ee9f3fc..c3c23c4 100644
--- a/core/fpdfapi/page/cpdf_annotcontext.h
+++ b/core/fpdfapi/page/cpdf_annotcontext.h
@@ -14,12 +14,12 @@
 
 class CPDF_Dictionary;
 class CPDF_Form;
-class CPDF_Page;
 class CPDF_Stream;
+class IPDF_Page;
 
 class CPDF_AnnotContext {
  public:
-  CPDF_AnnotContext(CPDF_Dictionary* pAnnotDict, CPDF_Page* pPage);
+  CPDF_AnnotContext(CPDF_Dictionary* pAnnotDict, IPDF_Page* pPage);
   ~CPDF_AnnotContext();
 
   void SetForm(CPDF_Stream* pStream);
@@ -30,12 +30,12 @@
   CPDF_Dictionary* GetAnnotDict() const { return m_pAnnotDict.Get(); }
 
   // Never nullptr.
-  CPDF_Page* GetPage() const { return m_pPage.Get(); }
+  IPDF_Page* GetPage() const { return m_pPage.Get(); }
 
  private:
   std::unique_ptr<CPDF_Form> m_pAnnotForm;
   RetainPtr<CPDF_Dictionary> const m_pAnnotDict;
-  UnownedPtr<CPDF_Page> const m_pPage;
+  UnownedPtr<IPDF_Page> const m_pPage;
 };
 
 #endif  // CORE_FPDFAPI_PAGE_CPDF_ANNOTCONTEXT_H_
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
index 0f7f48c..fc479e6 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
@@ -759,23 +759,21 @@
   if (!m_pInfo || m_pInfo->version < 2 || !m_pInfo->FFI_OnFocusChange)
     return;
 
-  if ((*pAnnot)->AsXFAWidget()) {
-    // TODO(crbug.com/pdfium/1482): Handle XFA case.
+  // TODO(crbug.com/pdfium/1482): Handle XFA case.
+  if ((*pAnnot)->AsXFAWidget())
     return;
-  }
 
   CPDFSDK_PageView* pPageView = (*pAnnot)->GetPageView();
   if (!pPageView->IsValid())
     return;
 
-  CPDF_Page* cpdf_page = (*pAnnot)->GetPDFPage();
-  if (!cpdf_page)
+  IPDF_Page* page = (*pAnnot)->GetPage();
+  if (!page)
     return;
 
   CPDF_Dictionary* annot_dict = (*pAnnot)->GetPDFAnnot()->GetAnnotDict();
 
-  auto focused_annot =
-      pdfium::MakeUnique<CPDF_AnnotContext>(annot_dict, cpdf_page);
+  auto focused_annot = pdfium::MakeUnique<CPDF_AnnotContext>(annot_dict, page);
   FPDF_ANNOTATION fpdf_annot =
       FPDFAnnotationFromCPDFAnnotContext(focused_annot.get());
 
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 44b2b0f..61956a6 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -6,6 +6,7 @@
 
 #include <memory>
 #include <utility>
+#include <vector>
 
 #include "constants/annotation_common.h"
 #include "core/fpdfapi/edit/cpdf_pagecontentgenerator.h"
@@ -295,7 +296,8 @@
   pDict->SetNewFor<CPDF_Name>(pdfium::annotation::kSubtype,
                               CPDF_Annot::AnnotSubtypeToString(
                                   static_cast<CPDF_Annot::Subtype>(subtype)));
-  auto pNewAnnot = pdfium::MakeUnique<CPDF_AnnotContext>(pDict.Get(), pPage);
+  auto pNewAnnot = pdfium::MakeUnique<CPDF_AnnotContext>(
+      pDict.Get(), IPDFPageFromFPDFPage(page));
 
   CPDF_Array* pAnnotList = pPage->GetDict()->GetArrayFor("Annots");
   if (!pAnnotList)
@@ -329,7 +331,8 @@
   if (!pDict)
     return nullptr;
 
-  auto pNewAnnot = pdfium::MakeUnique<CPDF_AnnotContext>(pDict, pPage);
+  auto pNewAnnot =
+      pdfium::MakeUnique<CPDF_AnnotContext>(pDict, IPDFPageFromFPDFPage(page));
 
   // Caller takes ownership.
   return FPDFAnnotationFromCPDFAnnotContext(pNewAnnot.release());
diff --git a/fpdfsdk/fpdf_formfill.cpp b/fpdfsdk/fpdf_formfill.cpp
index 3ddcd2a..96e4c20 100644
--- a/fpdfsdk/fpdf_formfill.cpp
+++ b/fpdfsdk/fpdf_formfill.cpp
@@ -594,11 +594,15 @@
   if (!cpdfsdk_annot)
     return true;
 
+  // TODO(crbug.com/pdfium/1482): Handle XFA case.
+  if (cpdfsdk_annot->AsXFAWidget())
+    return true;
+
   CPDFSDK_PageView* page_view = cpdfsdk_annot->GetPageView();
   if (!page_view->IsValid())
     return true;
 
-  CPDF_Page* page = cpdfsdk_annot->GetPDFPage();
+  IPDF_Page* page = cpdfsdk_annot->GetPage();
   if (!page)
     return true;
 
@@ -612,9 +616,7 @@
 }
 
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
-FORM_SetFocusedAnnot(FPDF_FORMHANDLE handle,
-                     FPDF_PAGE page,
-                     FPDF_ANNOTATION annot) {
+FORM_SetFocusedAnnot(FPDF_FORMHANDLE handle, FPDF_ANNOTATION annot) {
   CPDFSDK_FormFillEnvironment* form_fill_env =
       CPDFSDKFormFillEnvironmentFromFPDFFormHandle(handle);
   if (!form_fill_env)
@@ -624,11 +626,8 @@
   if (!annot_context)
     return false;
 
-  IPDF_Page* pdf_page = IPDFPageFromFPDFPage(page);
-  if (!pdf_page)
-    return false;
-
-  CPDFSDK_PageView* page_view = form_fill_env->GetPageView(pdf_page, true);
+  CPDFSDK_PageView* page_view =
+      form_fill_env->GetPageView(annot_context->GetPage(), true);
   if (!page_view->IsValid())
     return false;
 
diff --git a/fpdfsdk/fpdf_formfill_embeddertest.cpp b/fpdfsdk/fpdf_formfill_embeddertest.cpp
index c4e6b48..d37a95e 100644
--- a/fpdfsdk/fpdf_formfill_embeddertest.cpp
+++ b/fpdfsdk/fpdf_formfill_embeddertest.cpp
@@ -701,9 +701,8 @@
   EXPECT_EQ(-1, page_index);
 
   // Validate that nullptr values are handled properly.
-  EXPECT_FALSE(FORM_SetFocusedAnnot(nullptr, pages[0], annot));
-  EXPECT_FALSE(FORM_SetFocusedAnnot(form_handle(), nullptr, annot));
-  EXPECT_FALSE(FORM_SetFocusedAnnot(form_handle(), pages[0], nullptr));
+  EXPECT_FALSE(FORM_SetFocusedAnnot(nullptr, annot));
+  EXPECT_FALSE(FORM_SetFocusedAnnot(form_handle(), nullptr));
 
   constexpr int kExpectedAnnotIndex = 2;
 
@@ -713,8 +712,7 @@
         FPDFPage_GetAnnot(pages[i], kExpectedAnnotIndex));
     ASSERT_TRUE(focused_annot);
 
-    ASSERT_TRUE(
-        FORM_SetFocusedAnnot(form_handle(), pages[i], focused_annot.get()));
+    ASSERT_TRUE(FORM_SetFocusedAnnot(form_handle(), focused_annot.get()));
 
     ASSERT_TRUE(FORM_GetFocusedAnnot(form_handle(), &page_index, &annot));
     EXPECT_EQ(kExpectedAnnotIndex, FPDFPage_GetAnnotIndex(pages[i], annot));
diff --git a/public/fpdf_formfill.h b/public/fpdf_formfill.h
index 9b6c4d4..84537e4 100644
--- a/public/fpdf_formfill.h
+++ b/public/fpdf_formfill.h
@@ -1606,7 +1606,6 @@
  * Parameters:
  *       handle      -   Handle to the form fill module, as returned by
  *                       FPDFDOC_InitFormFillEnvironment().
- *       page        -   Handle to a page.
  *       annot       -   Handle to an annotation.
  * Return Value:
  *       True indicates success; otherwise false.
@@ -1615,9 +1614,7 @@
  *       instead.
  */
 FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
-FORM_SetFocusedAnnot(FPDF_FORMHANDLE handle,
-                     FPDF_PAGE page,
-                     FPDF_ANNOTATION annot);
+FORM_SetFocusedAnnot(FPDF_FORMHANDLE handle, FPDF_ANNOTATION annot);
 
 // Form Field Types
 // The names of the defines are stable, but the specific values associated with