Add CPDF_Page::Extension::GetDocExtension()

In turn, add CPDF_Document::Extension::GetPDFDoc() so that we can
use the abstract return type in more places.

Mark an internal-only cpdfxfa_context method as private while we're
at it.

Change-Id: I08e64f4b9438bf2f731c3a37cf2a41152bbbd8fa
Reviewed-on: https://pdfium-review.googlesource.com/31916
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_page.h b/core/fpdfapi/page/cpdf_page.h
index 47c5ed9..c26c42f 100644
--- a/core/fpdfapi/page/cpdf_page.h
+++ b/core/fpdfapi/page/cpdf_page.h
@@ -10,6 +10,7 @@
 #include <memory>
 
 #include "core/fpdfapi/page/cpdf_pageobjectholder.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"
@@ -25,7 +26,11 @@
 class CPDF_Page : public CPDF_PageObjectHolder {
  public:
   class View {};  // Caller implements as desired, empty here due to layering.
-  class Extension : public Retainable {};  // XFA page parent class, layering.
+
+  // XFA page parent class, layering.
+  class Extension : public Retainable {
+    virtual CPDF_Document::Extension* GetDocumentExtension() const = 0;
+  };
 
   CPDF_Page(CPDF_Document* pDocument,
             CPDF_Dictionary* pPageDict,
diff --git a/core/fpdfapi/parser/cpdf_document.h b/core/fpdfapi/parser/cpdf_document.h
index 759dd68..5ee3c77 100644
--- a/core/fpdfapi/parser/cpdf_document.h
+++ b/core/fpdfapi/parser/cpdf_document.h
@@ -45,6 +45,7 @@
   class Extension {
    public:
     virtual ~Extension() {}
+    virtual CPDF_Document* GetPDFDoc() const = 0;
     virtual int GetPageCount() const = 0;
     virtual void DeletePage(int page_index) = 0;
   };
diff --git a/fpdfsdk/cpdfsdk_pageview.cpp b/fpdfsdk/cpdfsdk_pageview.cpp
index e648e59..87856fb 100644
--- a/fpdfsdk/cpdfsdk_pageview.cpp
+++ b/fpdfsdk/cpdfsdk_pageview.cpp
@@ -89,7 +89,8 @@
   if (!pPage)
     return;
 
-  if (pPage->GetContext()->GetFormType() == FormType::kXFAFull) {
+  auto* pContext = static_cast<CPDFXFA_Context*>(pPage->GetDocumentExtension());
+  if (pContext->GetFormType() == FormType::kXFAFull) {
     CFX_RectF rectClip(
         static_cast<float>(pClip.left), static_cast<float>(pClip.top),
         static_cast<float>(pClip.Width()), static_cast<float>(pClip.Height()));
@@ -177,7 +178,11 @@
     return false;
 
   CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
-  if (!pPage || !pPage->GetContext()->ContainsXFAForm())
+  if (!pPage)
+    return false;
+
+  auto* pContext = static_cast<CPDFXFA_Context*>(pPage->GetDocumentExtension());
+  if (!pContext->ContainsXFAForm())
     return false;
 
   CPDFSDK_Annot::ObservedPtr pObserved(pAnnot);
@@ -204,7 +209,7 @@
 CPDF_Document* CPDFSDK_PageView::GetPDFDocument() {
   if (m_page) {
 #ifdef PDF_ENABLE_XFA
-    return m_page->GetContext()->GetPDFDoc();
+    return m_page->GetDocumentExtension()->GetPDFDoc();
 #else   // PDF_ENABLE_XFA
     return m_page->m_pDocument.Get();
 #endif  // PDF_ENABLE_XFA
@@ -530,7 +535,9 @@
     return -1;
 
 #ifdef PDF_ENABLE_XFA
-  switch (m_page->GetContext()->GetFormType()) {
+  auto* pContext =
+      static_cast<CPDFXFA_Context*>(m_page->GetDocumentExtension());
+  switch (pContext->GetFormType()) {
     case FormType::kXFAFull: {
       CXFA_FFPageView* pPageView = m_page->GetXFAPageView();
       return pPageView ? pPageView->GetPageIndex() : -1;
diff --git a/fpdfsdk/fpdf_formfill.cpp b/fpdfsdk/fpdf_formfill.cpp
index 202835c..b99bd5f 100644
--- a/fpdfsdk/fpdf_formfill.cpp
+++ b/fpdfsdk/fpdf_formfill.cpp
@@ -138,10 +138,10 @@
     return;
 
 #ifdef PDF_ENABLE_XFA
-  CPDFXFA_Context* pContext = pPage->GetContext();
-  if (!pContext)
+  CPDF_Document::Extension* pExtension = pPage->GetDocumentExtension();
+  if (!pExtension)
     return;
-  CPDF_Document* pPDFDoc = pContext->GetPDFDoc();
+  CPDF_Document* pPDFDoc = pExtension->GetPDFDoc();
   if (!pPDFDoc)
     return;
   CPDFSDK_FormFillEnvironment* pFormFillEnv =
diff --git a/fpdfsdk/fpdf_text.cpp b/fpdfsdk/fpdf_text.cpp
index dade84a..c06885a 100644
--- a/fpdfsdk/fpdf_text.cpp
+++ b/fpdfsdk/fpdf_text.cpp
@@ -52,7 +52,7 @@
 
 #ifdef PDF_ENABLE_XFA
   CPDFXFA_Page* pPage = UnderlyingFromFPDFPage(page);
-  CPDFXFA_Context* pContext = pPage->GetContext();
+  auto* pContext = static_cast<CPDFXFA_Context*>(pPage->GetDocumentExtension());
   CPDF_ViewerPreferences viewRef(pContext->GetPDFDoc());
 #else  // PDF_ENABLE_XFA
   CPDF_ViewerPreferences viewRef(pPDFPage->m_pDocument.Get());
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
index 97288c6..05bb56c 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
@@ -180,6 +180,10 @@
   return nullptr;
 }
 
+CPDF_Document* CPDFXFA_Context::GetPDFDoc() const {
+  return m_pPDFDoc.get();
+}
+
 void CPDFXFA_Context::DeletePage(int page_index) {
   // Delete from the document first because, if GetPage was never called for
   // this |page_index| then |m_XFAPageList| may have size < |page_index| even
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_context.h b/fpdfsdk/fpdfxfa/cpdfxfa_context.h
index 23f1ac2..c7fe02d 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_context.h
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_context.h
@@ -40,7 +40,6 @@
   ~CPDFXFA_Context() override;
 
   bool LoadXFADoc();
-  CPDF_Document* GetPDFDoc() { return m_pPDFDoc.get(); }
   CXFA_FFDoc* GetXFADoc() { return m_pXFADoc.get(); }
   CXFA_FFDocView* GetXFADocView() { return m_pXFADocView.Get(); }
   FormType GetFormType() const { return m_FormType; }
@@ -48,7 +47,6 @@
     return m_FormType == FormType::kXFAFull ||
            m_FormType == FormType::kXFAForeground;
   }
-  CJS_Runtime* GetCJSRuntime() const;
   CXFA_FFApp* GetXFAApp() { return m_pXFAApp.get(); }
 
   CPDFSDK_FormFillEnvironment* GetFormFillEnv() const {
@@ -61,6 +59,7 @@
   void ClearChangeMark();
 
   // CPDF_Document::Extension:
+  CPDF_Document* GetPDFDoc() const override;
   int GetPageCount() const override;
   void DeletePage(int page_index) override;
 
@@ -108,6 +107,7 @@
   }
 
  private:
+  CJS_Runtime* GetCJSRuntime() const;
   void CloseXFADoc();
 
   FormType m_FormType = FormType::kNone;
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_page.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_page.cpp
index 8268ed8..809214d 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_page.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_page.cpp
@@ -86,6 +86,10 @@
   return true;
 }
 
+CPDF_Document::Extension* CPDFXFA_Page::GetDocumentExtension() const {
+  return m_pContext.Get();
+}
+
 float CPDFXFA_Page::GetPageWidth() const {
   if (!m_pPDFPage && !m_pXFAPageView)
     return 0.0f;
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_page.h b/fpdfsdk/fpdfxfa/cpdfxfa_page.h
index 030b068..4f4d6b0 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_page.h
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_page.h
@@ -10,6 +10,7 @@
 #include <memory>
 
 #include "core/fpdfapi/page/cpdf_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"
@@ -28,7 +29,9 @@
   bool LoadPage();
   bool LoadPDFPage(CPDF_Dictionary* pageDict);
 
-  CPDFXFA_Context* GetContext() const { return m_pContext.Get(); }
+  // CPDF_Page::Extension:
+  CPDF_Document::Extension* GetDocumentExtension() const override;
+
   int GetPageIndex() const { return m_iPageIndex; }
   CPDF_Page* GetPDFPage() const { return m_pPDFPage.get(); }
   CXFA_FFPageView* GetXFAPageView() const { return m_pXFAPageView; }