Remove some more CPDFXFA_* type usage from fpdfsdk/cpdfsdk_*

Change-Id: I9ee4153db9521c3f3c1e18a5a9e3fc432745f7aa
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/59710
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_annot.cpp b/fpdfsdk/cpdfsdk_annot.cpp
index 95de938..32fdb6b 100644
--- a/fpdfsdk/cpdfsdk_annot.cpp
+++ b/fpdfsdk/cpdfsdk_annot.cpp
@@ -10,10 +10,6 @@
 
 #include "fpdfsdk/cpdfsdk_pageview.h"
 
-#ifdef PDF_ENABLE_XFA
-#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
-#endif  // PDF_ENABLE_XFA
-
 CPDFSDK_Annot::CPDFSDK_Annot(CPDFSDK_PageView* pPageView)
     : m_pPageView(pPageView) {}
 
@@ -24,7 +20,6 @@
 }
 
 #ifdef PDF_ENABLE_XFA
-
 bool CPDFSDK_Annot::IsXFAField() const {
   return false;
 }
@@ -32,13 +27,16 @@
 CXFA_FFWidget* CPDFSDK_Annot::GetXFAWidget() const {
   return nullptr;
 }
-
-CPDFXFA_Page* CPDFSDK_Annot::GetPDFXFAPage() {
-  return m_pPageView ? m_pPageView->GetPDFXFAPage() : nullptr;
-}
-
 #endif  // PDF_ENABLE_XFA
 
+IPDF_Page* CPDFSDK_Annot::GetXFAPage() {
+#ifdef PDF_ENABLE_XFA
+  if (m_pPageView)
+    return m_pPageView->GetXFAPage();
+#endif
+  return nullptr;
+}
+
 int CPDFSDK_Annot::GetLayoutOrder() const {
   return 5;
 }
@@ -63,7 +61,7 @@
 
 IPDF_Page* CPDFSDK_Annot::GetPage() {
 #ifdef PDF_ENABLE_XFA
-  CPDFXFA_Page* pXFAPage = GetPDFXFAPage();
+  IPDF_Page* pXFAPage = GetXFAPage();
   if (pXFAPage)
     return pXFAPage;
 #endif  // PDF_ENABLE_XFA
diff --git a/fpdfsdk/cpdfsdk_annot.h b/fpdfsdk/cpdfsdk_annot.h
index a01b414..536b6fe 100644
--- a/fpdfsdk/cpdfsdk_annot.h
+++ b/fpdfsdk/cpdfsdk_annot.h
@@ -19,7 +19,6 @@
 class CPDF_RenderOptions;
 class CPDFSDK_BAAnnot;
 class CPDFSDK_PageView;
-class CPDFXFA_Page;
 class IPDF_Page;
 
 #ifdef PDF_ENABLE_XFA
@@ -45,11 +44,10 @@
   virtual CFX_FloatRect GetRect() const;
   virtual void SetRect(const CFX_FloatRect& rect);
 
-  IPDF_Page* GetPage();  // Returns XFA Page if possible, else PDF page.
-  CPDF_Page* GetPDFPage();
-#ifdef PDF_ENABLE_XFA
-  CPDFXFA_Page* GetPDFXFAPage();
-#endif  // PDF_ENABLE_XFA
+  // Three cases: PDF page only, XFA page only, or XFA page backed by PDF page.
+  IPDF_Page* GetPage();     // Returns XFA Page if possible, else PDF page.
+  CPDF_Page* GetPDFPage();  // Returns PDF page or nullptr.
+  IPDF_Page* GetXFAPage();  // Returns XFA page or nullptr.
 
   CPDFSDK_PageView* GetPageView() const { return m_pPageView.Get(); }
 
diff --git a/fpdfsdk/cpdfsdk_annothandlermgr.cpp b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
index eed1df6..20744cc 100644
--- a/fpdfsdk/cpdfsdk_annothandlermgr.cpp
+++ b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
@@ -315,10 +315,10 @@
 CPDFSDK_Annot* CPDFSDK_AnnotHandlerMgr::GetNextAnnot(CPDFSDK_Annot* pSDKAnnot,
                                                      bool bNext) {
 #ifdef PDF_ENABLE_XFA
-  CPDFXFA_Page* pPage = pSDKAnnot->GetPageView()->GetPDFXFAPage();
+  IPDF_Page* pPage = pSDKAnnot->GetPageView()->GetXFAPage();
   if (pPage && !pPage->AsPDFPage()) {
     // For xfa annots in XFA pages not backed by PDF pages.
-    return pPage->GetNextXFAAnnot(pSDKAnnot, bNext);
+    return static_cast<CPDFXFA_Page*>(pPage)->GetNextXFAAnnot(pSDKAnnot, bNext);
   }
 #endif  // PDF_ENABLE_XFA
 
diff --git a/fpdfsdk/cpdfsdk_pageview.cpp b/fpdfsdk/cpdfsdk_pageview.cpp
index 907ff7f..e52a72b 100644
--- a/fpdfsdk/cpdfsdk_pageview.cpp
+++ b/fpdfsdk/cpdfsdk_pageview.cpp
@@ -68,13 +68,14 @@
   m_curMatrix = mtUser2Device;
 
 #ifdef PDF_ENABLE_XFA
-  CPDFXFA_Page* pPage = GetPDFXFAPage();
+  IPDF_Page* pPage = GetXFAPage();
   if (!pPage)
     return;
 
-  auto* pContext = pPage->GetDocumentExtension();
+  CPDF_Document::Extension* pContext = pPage->GetDocument()->GetExtension();
   if (pContext->ContainsExtensionFullForm()) {
-    pPage->DrawFocusAnnot(pDevice, GetFocusAnnot(), mtUser2Device, pClip);
+    static_cast<CPDFXFA_Page*>(pPage)->DrawFocusAnnot(pDevice, GetFocusAnnot(),
+                                                      mtUser2Device, pClip);
     return;
   }
 #endif  // PDF_ENABLE_XFA
@@ -141,11 +142,11 @@
   if (!pAnnot)
     return false;
 
-  CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
+  IPDF_Page* pPage = pAnnot->GetXFAPage();
   if (!pPage)
     return false;
 
-  CPDF_Document::Extension* pContext = pPage->GetDocumentExtension();
+  CPDF_Document::Extension* pContext = pPage->GetDocument()->GetExtension();
   if (!pContext->ContainsExtensionForm())
     return false;
 
@@ -197,6 +198,10 @@
   }
   return nullptr;
 }
+
+IPDF_Page* CPDFSDK_PageView::GetXFAPage() {
+  return ToXFAPage(m_page);
+}
 #endif  // PDF_ENABLE_XFA
 
 WideString CPDFSDK_PageView::GetFocusedFormText() {
@@ -529,7 +534,7 @@
 
 int CPDFSDK_PageView::GetPageIndex() const {
 #ifdef PDF_ENABLE_XFA
-  auto* pContext = m_page->AsXFAPage()->GetDocumentExtension();
+  CPDF_Document::Extension* pContext = m_page->GetDocument()->GetExtension();
   if (pContext->ContainsExtensionFullForm()) {
     CXFA_FFPageView* pPageView = m_page->AsXFAPage()->GetXFAPageView();
     return pPageView ? pPageView->GetLayoutItem()->GetPageIndex() : -1;
diff --git a/fpdfsdk/cpdfsdk_pageview.h b/fpdfsdk/cpdfsdk_pageview.h
index 3b04dfb..1c47503 100644
--- a/fpdfsdk/cpdfsdk_pageview.h
+++ b/fpdfsdk/cpdfsdk_pageview.h
@@ -45,7 +45,7 @@
   bool DeleteAnnot(CPDFSDK_Annot* pAnnot);
   CPDFSDK_Annot* AddAnnot(CXFA_FFWidget* pPDFAnnot);
   CPDFSDK_Annot* GetAnnotByXFAWidget(CXFA_FFWidget* hWidget);
-  CPDFXFA_Page* GetPDFXFAPage() { return ToXFAPage(m_page); }
+  IPDF_Page* GetXFAPage();
 #endif  // PDF_ENABLE_XFA
 
   CPDF_Page* GetPDFPage() const;
diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp
index 007feff..802cd90 100644
--- a/fpdfsdk/fpdf_editpage.cpp
+++ b/fpdfsdk/fpdf_editpage.cpp
@@ -221,9 +221,8 @@
   pPageDict->SetNewFor<CPDF_Dictionary>(pdfium::page_object::kResources);
 
 #ifdef PDF_ENABLE_XFA
-  auto* pContext = static_cast<CPDFXFA_Context*>(pDoc->GetExtension());
-  if (pContext) {
-    auto pXFAPage = pdfium::MakeRetain<CPDFXFA_Page>(pContext, page_index);
+  if (pDoc->GetExtension()) {
+    auto pXFAPage = pdfium::MakeRetain<CPDFXFA_Page>(pDoc, page_index);
     pXFAPage->LoadPDFPageFromDict(pPageDict);
     return FPDFPageFromIPDFPage(pXFAPage.Leak());  // Caller takes ownership.
   }
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
index f91c400..7d75948 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
@@ -157,7 +157,7 @@
     m_XFAPageList.resize(m_nPageCount);
   }
 
-  auto pPage = pdfium::MakeRetain<CPDFXFA_Page>(this, page_index);
+  auto pPage = pdfium::MakeRetain<CPDFXFA_Page>(GetPDFDoc(), page_index);
   if (!pPage->LoadPage())
     return nullptr;
 
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_page.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_page.cpp
index 39874eb..3f26e66 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_page.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_page.cpp
@@ -21,9 +21,9 @@
 #include "xfa/fxfa/cxfa_rendercontext.h"
 #include "xfa/fxgraphics/cxfa_graphics.h"
 
-CPDFXFA_Page::CPDFXFA_Page(CPDFXFA_Context* pContext, int page_index)
-    : m_pContext(pContext), m_iPageIndex(page_index) {
-  ASSERT(m_pContext);
+CPDFXFA_Page::CPDFXFA_Page(CPDF_Document* pDocument, int page_index)
+    : m_pDocument(pDocument), m_iPageIndex(page_index) {
+  ASSERT(m_pDocument->GetExtension());
   ASSERT(m_iPageIndex >= 0);
 }
 
@@ -38,7 +38,7 @@
 }
 
 CPDF_Document* CPDFXFA_Page::GetDocument() const {
-  return m_pContext->GetPDFDoc();
+  return m_pDocument.Get();
 }
 
 bool CPDFXFA_Page::LoadPDFPage() {
@@ -54,12 +54,14 @@
 }
 
 CXFA_FFPageView* CPDFXFA_Page::GetXFAPageView() const {
-  CXFA_FFDocView* pXFADocView = m_pContext->GetXFADocView();
+  auto* pContext = static_cast<CPDFXFA_Context*>(m_pDocument->GetExtension());
+  CXFA_FFDocView* pXFADocView = pContext->GetXFADocView();
   return pXFADocView ? pXFADocView->GetPageView(m_iPageIndex) : nullptr;
 }
 
 bool CPDFXFA_Page::LoadPage() {
-  switch (m_pContext->GetFormType()) {
+  auto* pContext = static_cast<CPDFXFA_Context*>(m_pDocument->GetExtension());
+  switch (pContext->GetFormType()) {
     case FormType::kNone:
     case FormType::kAcroForm:
     case FormType::kXFAForeground:
@@ -79,16 +81,13 @@
   m_pPDFPage->ParseContent();
 }
 
-CPDF_Document::Extension* CPDFXFA_Page::GetDocumentExtension() const {
-  return m_pContext.Get();
-}
-
 float CPDFXFA_Page::GetPageWidth() const {
   CXFA_FFPageView* pPageView = GetXFAPageView();
   if (!m_pPDFPage && !pPageView)
     return 0.0f;
 
-  switch (m_pContext->GetFormType()) {
+  auto* pContext = static_cast<CPDFXFA_Context*>(m_pDocument->GetExtension());
+  switch (pContext->GetFormType()) {
     case FormType::kNone:
     case FormType::kAcroForm:
     case FormType::kXFAForeground:
@@ -109,7 +108,8 @@
   if (!m_pPDFPage && !pPageView)
     return 0.0f;
 
-  switch (m_pContext->GetFormType()) {
+  auto* pContext = static_cast<CPDFXFA_Context*>(m_pDocument->GetExtension());
+  switch (pContext->GetFormType()) {
     case FormType::kNone:
     case FormType::kAcroForm:
     case FormType::kXFAForeground:
@@ -156,7 +156,8 @@
   if (!m_pPDFPage && !pPageView)
     return CFX_Matrix();
 
-  switch (m_pContext->GetFormType()) {
+  auto* pContext = static_cast<CPDFXFA_Context*>(m_pDocument->GetExtension());
+  switch (pContext->GetFormType()) {
     case FormType::kNone:
     case FormType::kAcroForm:
     case FormType::kXFAForeground:
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_page.h b/fpdfsdk/fpdfxfa/cpdfxfa_page.h
index 5bc5f61..73d5421 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_page.h
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_page.h
@@ -9,7 +9,6 @@
 
 #include "core/fpdfapi/page/cpdf_page.h"
 #include "core/fpdfapi/page/ipdf_page.h"
-#include "core/fpdfapi/parser/cpdf_document.h"
 #include "core/fxcrt/fx_coordinates.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxcrt/retain_ptr.h"
@@ -18,8 +17,8 @@
 
 class CFX_RenderDevice;
 class CPDF_Dictionary;
+class CPDF_Document;
 class CPDFSDK_Annot;
-class CPDFXFA_Context;
 class CXFA_FFPageView;
 
 class CPDFXFA_Page final : public IPDF_Page {
@@ -45,7 +44,6 @@
 
   bool LoadPage();
   void LoadPDFPageFromDict(CPDF_Dictionary* pPageDict);
-  CPDF_Document::Extension* GetDocumentExtension() const;
   int GetPageIndex() const { return m_iPageIndex; }
   void SetXFAPageViewIndex(int index) { m_iPageIndex = index; }
   CXFA_FFPageView* GetXFAPageView() const;
@@ -58,13 +56,13 @@
 
  private:
   // Refcounted class.
-  CPDFXFA_Page(CPDFXFA_Context* pContext, int page_index);
+  CPDFXFA_Page(CPDF_Document* pDocument, int page_index);
   ~CPDFXFA_Page() override;
 
   bool LoadPDFPage();
 
-  RetainPtr<CPDF_Page> m_pPDFPage;
-  UnownedPtr<CPDFXFA_Context> const m_pContext;
+  RetainPtr<CPDF_Page> m_pPDFPage;  // Backing page, if any.
+  UnownedPtr<CPDF_Document> const m_pDocument;
   int m_iPageIndex;
 };