Convert CPDFSDK_PageView to have a CPDFSDK_FormFillEnvironment

This CL changes CPDFSDK_PageView to have a CPDFSDK_FormFillEnvironment instead
of a CPDFSDK_Document.

Review-Url: https://codereview.chromium.org/2400673002
diff --git a/fpdfsdk/cpdfsdk_annothandlermgr.cpp b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
index 574efc1..077c7fa 100644
--- a/fpdfsdk/cpdfsdk_annothandlermgr.cpp
+++ b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
@@ -208,7 +208,7 @@
     CPDFSDK_Annot::ObservedPtr pNext(
         GetNextAnnot(pFocusAnnot, !m_pEnv->IsSHIFTKeyDown(nFlag)));
     if (pNext && pNext.Get() != pFocusAnnot) {
-      pPage->GetSDKDocument()->SetFocusAnnot(&pNext);
+      pPage->GetFormFillEnv()->GetSDKDocument()->SetFocusAnnot(&pNext);
       return TRUE;
     }
   }
@@ -226,11 +226,7 @@
     CPDFSDK_Annot::ObservedPtr* pAnnot,
     uint32_t nFlag) {
   ASSERT(*pAnnot);
-  if (!GetAnnotHandler(pAnnot->Get())->OnSetFocus(pAnnot, nFlag))
-    return FALSE;
-
-  (*pAnnot)->GetPageView()->GetSDKDocument();
-  return TRUE;
+  return GetAnnotHandler(pAnnot->Get())->OnSetFocus(pAnnot, nFlag);
 }
 
 FX_BOOL CPDFSDK_AnnotHandlerMgr::Annot_OnKillFocus(
diff --git a/fpdfsdk/cpdfsdk_document.cpp b/fpdfsdk/cpdfsdk_document.cpp
index 78bcf49..a3d2680 100644
--- a/fpdfsdk/cpdfsdk_document.cpp
+++ b/fpdfsdk/cpdfsdk_document.cpp
@@ -63,7 +63,7 @@
   if (!ReNew)
     return nullptr;
 
-  CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
+  CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(GetEnv(), pUnderlyingPage);
   m_pageMap[pUnderlyingPage] = pPageView;
   // Delay to load all the annotations, to avoid endless loop.
   pPageView->LoadFXAnnots();
diff --git a/fpdfsdk/cpdfsdk_pageview.cpp b/fpdfsdk/cpdfsdk_pageview.cpp
index c1424ec..a5c60d6 100644
--- a/fpdfsdk/cpdfsdk_pageview.cpp
+++ b/fpdfsdk/cpdfsdk_pageview.cpp
@@ -29,10 +29,10 @@
 #include "xfa/fxgraphics/cfx_graphics.h"
 #endif  // PDF_ENABLE_XFA
 
-CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc,
+CPDFSDK_PageView::CPDFSDK_PageView(CPDFSDK_FormFillEnvironment* pFormFillEnv,
                                    UnderlyingPageType* page)
     : m_page(page),
-      m_pSDKDoc(pSDKDoc),
+      m_pFormFillEnv(pFormFillEnv),
 #ifndef PDF_ENABLE_XFA
       m_bOwnsPage(false),
 #endif  // PDF_ENABLE_XFA
@@ -42,7 +42,8 @@
       m_bValid(FALSE),
       m_bLocked(FALSE),
       m_bBeingDestroyed(false) {
-  CPDFSDK_InterForm* pInterForm = pSDKDoc->GetInterForm();
+  CPDFSDK_InterForm* pInterForm =
+      pFormFillEnv->GetSDKDocument()->GetInterForm();
   if (pInterForm) {
     CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
 #ifdef PDF_ENABLE_XFA
@@ -65,8 +66,8 @@
   m_page->SetView(nullptr);
 #endif  // PDF_ENABLE_XFA
 
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+      m_pFormFillEnv->GetAnnotHandlerMgr();
   for (CPDFSDK_Annot* pAnnot : m_SDKAnnotArray)
     pAnnotHandlerMgr->ReleaseAnnot(pAnnot);
 
@@ -88,7 +89,6 @@
                                        CPDF_RenderOptions* pOptions) {
 #endif  // PDF_ENABLE_XFA
   m_curMatrix = *pUser2Device;
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
 
 #ifdef PDF_ENABLE_XFA
   CPDFXFA_Page* pPage = GetPDFXFAPage();
@@ -127,7 +127,8 @@
   // for pdf/static xfa.
   CPDFSDK_AnnotIterator annotIterator(this, true);
   while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
-    CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+    CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+        m_pFormFillEnv->GetAnnotHandlerMgr();
     pAnnotHandlerMgr->Annot_OnDraw(this, pSDKAnnot, pDevice, pUser2Device,
                                    pOptions->m_bDrawAnnots);
   }
@@ -135,8 +136,7 @@
 
 CPDFSDK_Annot* CPDFSDK_PageView::GetFXAnnotAtPoint(FX_FLOAT pageX,
                                                    FX_FLOAT pageY) {
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
   CPDFSDK_AnnotIterator annotIterator(this, false);
   while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
     CFX_FloatRect rc = pAnnotMgr->Annot_OnGetViewBBox(this, pSDKAnnot);
@@ -151,8 +151,7 @@
 
 CPDFSDK_Annot* CPDFSDK_PageView::GetFXWidgetAtPoint(FX_FLOAT pageX,
                                                     FX_FLOAT pageY) {
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotMgr = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
   CPDFSDK_AnnotIterator annotIterator(this, false);
   while (CPDFSDK_Annot* pSDKAnnot = annotIterator.Next()) {
     bool bHitTest = pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET;
@@ -180,8 +179,7 @@
   if (pSDKAnnot)
     return pSDKAnnot;
 
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
   pSDKAnnot = pAnnotHandler->NewAnnot(pPDFAnnot, this);
   if (!pSDKAnnot)
     return nullptr;
@@ -199,9 +197,8 @@
     return FALSE;
 
   if (GetFocusAnnot() == pAnnot)
-    m_pSDKDoc->KillFocusAnnot(0);
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotHandler = pEnv->GetAnnotHandlerMgr();
+    m_pFormFillEnv->GetSDKDocument()->KillFocusAnnot(0);
+  CPDFSDK_AnnotHandlerMgr* pAnnotHandler = m_pFormFillEnv->GetAnnotHandlerMgr();
   if (pAnnotHandler)
     pAnnotHandler->ReleaseAnnot(pAnnot);
 
@@ -259,19 +256,19 @@
                                         uint32_t nFlag) {
   CPDFSDK_Annot::ObservedPtr pAnnot(GetFXWidgetAtPoint(point.x, point.y));
   if (!pAnnot) {
-    m_pSDKDoc->KillFocusAnnot(nFlag);
+    m_pFormFillEnv->GetSDKDocument()->KillFocusAnnot(nFlag);
     return FALSE;
   }
 
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+      m_pFormFillEnv->GetAnnotHandlerMgr();
   if (!pAnnotHandlerMgr->Annot_OnLButtonDown(this, &pAnnot, nFlag, point))
     return FALSE;
 
   if (!pAnnot)
     return FALSE;
 
-  m_pSDKDoc->SetFocusAnnot(&pAnnot);
+  m_pFormFillEnv->GetSDKDocument()->SetFocusAnnot(&pAnnot);
   return TRUE;
 }
 
@@ -282,29 +279,29 @@
   if (!pAnnot)
     return FALSE;
 
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+      m_pFormFillEnv->GetAnnotHandlerMgr();
   FX_BOOL ok =
       pAnnotHandlerMgr->Annot_OnRButtonDown(this, &pAnnot, nFlag, point);
   if (!pAnnot)
     return FALSE;
 
   if (ok)
-    m_pSDKDoc->SetFocusAnnot(&pAnnot);
+    m_pFormFillEnv->GetSDKDocument()->SetFocusAnnot(&pAnnot);
 
   return TRUE;
 }
 
 FX_BOOL CPDFSDK_PageView::OnRButtonUp(const CFX_FloatPoint& point,
                                       uint32_t nFlag) {
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+      m_pFormFillEnv->GetAnnotHandlerMgr();
   CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point.x, point.y));
   if (!pFXAnnot)
     return FALSE;
 
   if (pAnnotHandlerMgr->Annot_OnRButtonUp(this, &pFXAnnot, nFlag, point))
-    m_pSDKDoc->SetFocusAnnot(&pFXAnnot);
+    m_pFormFillEnv->GetSDKDocument()->SetFocusAnnot(&pFXAnnot);
 
   return TRUE;
 }
@@ -312,8 +309,8 @@
 
 FX_BOOL CPDFSDK_PageView::OnLButtonUp(const CFX_FloatPoint& point,
                                       uint32_t nFlag) {
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+      m_pFormFillEnv->GetAnnotHandlerMgr();
   CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXWidgetAtPoint(point.x, point.y));
   CPDFSDK_Annot::ObservedPtr pFocusAnnot(GetFocusAnnot());
   if (pFocusAnnot && pFocusAnnot != pFXAnnot) {
@@ -326,8 +323,8 @@
 }
 
 FX_BOOL CPDFSDK_PageView::OnMouseMove(const CFX_FloatPoint& point, int nFlag) {
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+      m_pFormFillEnv->GetAnnotHandlerMgr();
   CPDFSDK_Annot::ObservedPtr pFXAnnot(GetFXAnnotAtPoint(point.x, point.y));
   if (pFXAnnot) {
     if (m_pCaptureWidget && m_pCaptureWidget != pFXAnnot) {
@@ -365,16 +362,16 @@
   if (!pAnnot)
     return FALSE;
 
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+      m_pFormFillEnv->GetAnnotHandlerMgr();
   return pAnnotHandlerMgr->Annot_OnMouseWheel(this, &pAnnot, nFlag, (int)deltaY,
                                               point);
 }
 
 FX_BOOL CPDFSDK_PageView::OnChar(int nChar, uint32_t nFlag) {
   if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
-    CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-    CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+    CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+        m_pFormFillEnv->GetAnnotHandlerMgr();
     return pAnnotHandlerMgr->Annot_OnChar(pAnnot, nChar, nFlag);
   }
 
@@ -383,8 +380,8 @@
 
 FX_BOOL CPDFSDK_PageView::OnKeyDown(int nKeyCode, int nFlag) {
   if (CPDFSDK_Annot* pAnnot = GetFocusAnnot()) {
-    CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-    CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+    CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+        m_pFormFillEnv->GetAnnotHandlerMgr();
     return pAnnotHandlerMgr->Annot_OnKeyDown(pAnnot, nKeyCode, nFlag);
   }
   return FALSE;
@@ -395,14 +392,15 @@
 }
 
 void CPDFSDK_PageView::LoadFXAnnots() {
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr = pEnv->GetAnnotHandlerMgr();
+  CPDFSDK_AnnotHandlerMgr* pAnnotHandlerMgr =
+      m_pFormFillEnv->GetAnnotHandlerMgr();
 
   SetLock(TRUE);
 
 #ifdef PDF_ENABLE_XFA
   CFX_RetainPtr<CPDFXFA_Page> protector(m_page);
-  if (m_pSDKDoc->GetXFADocument()->GetDocType() == DOCTYPE_DYNAMIC_XFA) {
+  if (m_pFormFillEnv->GetSDKDocument()->GetXFADocument()->GetDocType() ==
+      DOCTYPE_DYNAMIC_XFA) {
     CXFA_FFPageView* pageView = m_page->GetXFAPageView();
     std::unique_ptr<IXFA_WidgetIterator> pWidgetHander(
         pageView->CreateWidgetIterator(
@@ -449,16 +447,14 @@
 }
 
 void CPDFSDK_PageView::UpdateRects(const std::vector<CFX_FloatRect>& rects) {
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
   for (const auto& rc : rects)
-    pEnv->Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
+    m_pFormFillEnv->Invalidate(m_page, rc.left, rc.top, rc.right, rc.bottom);
 }
 
 void CPDFSDK_PageView::UpdateView(CPDFSDK_Annot* pAnnot) {
   CFX_FloatRect rcWindow = pAnnot->GetRect();
-  CPDFSDK_FormFillEnvironment* pEnv = m_pSDKDoc->GetEnv();
-  pEnv->Invalidate(m_page, rcWindow.left, rcWindow.top, rcWindow.right,
-                   rcWindow.bottom);
+  m_pFormFillEnv->Invalidate(m_page, rcWindow.left, rcWindow.top,
+                             rcWindow.right, rcWindow.bottom);
 }
 
 int CPDFSDK_PageView::GetPageIndex() const {
@@ -502,12 +498,13 @@
 }
 
 CPDFSDK_Annot* CPDFSDK_PageView::GetFocusAnnot() {
-  CPDFSDK_Annot* pFocusAnnot = m_pSDKDoc->GetFocusAnnot();
+  CPDFSDK_Annot* pFocusAnnot =
+      m_pFormFillEnv->GetSDKDocument()->GetFocusAnnot();
   return IsValidSDKAnnot(pFocusAnnot) ? pFocusAnnot : nullptr;
 }
 
 int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
   CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict;
-  CPDF_Document* pDoc = m_pSDKDoc->GetPDFDocument();
+  CPDF_Document* pDoc = m_pFormFillEnv->GetSDKDocument()->GetPDFDocument();
   return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
 }
diff --git a/fpdfsdk/cpdfsdk_pageview.h b/fpdfsdk/cpdfsdk_pageview.h
index cd9ddec..a265436 100644
--- a/fpdfsdk/cpdfsdk_pageview.h
+++ b/fpdfsdk/cpdfsdk_pageview.h
@@ -21,7 +21,8 @@
 
 class CPDFSDK_PageView final : public CPDF_Page::View {
  public:
-  CPDFSDK_PageView(CPDFSDK_Document* pSDKDoc, UnderlyingPageType* page);
+  CPDFSDK_PageView(CPDFSDK_FormFillEnvironment* pFormFillEnv,
+                   UnderlyingPageType* page);
   ~CPDFSDK_PageView();
 
 #ifdef PDF_ENABLE_XFA
@@ -58,7 +59,7 @@
 
   CPDF_Page* GetPDFPage() const;
   CPDF_Document* GetPDFDocument();
-  CPDFSDK_Document* GetSDKDocument() { return m_pSDKDoc; }
+  CPDFSDK_FormFillEnvironment* GetFormFillEnv() const { return m_pFormFillEnv; }
   FX_BOOL OnLButtonDown(const CFX_FloatPoint& point, uint32_t nFlag);
   FX_BOOL OnLButtonUp(const CFX_FloatPoint& point, uint32_t nFlag);
 #ifdef PDF_ENABLE_XFA
@@ -102,7 +103,7 @@
   UnderlyingPageType* const m_page;
   std::unique_ptr<CPDF_AnnotList> m_pAnnotList;
   std::vector<CPDFSDK_Annot*> m_SDKAnnotArray;
-  CPDFSDK_Document* const m_pSDKDoc;
+  CPDFSDK_FormFillEnvironment* const m_pFormFillEnv;  // Not owned.
   CPDFSDK_Annot::ObservedPtr m_pCaptureWidget;
 #ifndef PDF_ENABLE_XFA
   bool m_bOwnsPage;
diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp
index 0624b07..12f9158 100644
--- a/fpdfsdk/cpdfsdk_widget.cpp
+++ b/fpdfsdk/cpdfsdk_widget.cpp
@@ -69,8 +69,8 @@
 
 #ifdef PDF_ENABLE_XFA
 CXFA_FFWidget* CPDFSDK_Widget::GetMixXFAWidget() const {
-  CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  CPDFXFA_Document* pDoc =
+      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (!m_hMixXFAWidget) {
       if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
@@ -94,8 +94,8 @@
 }
 
 CXFA_FFWidget* CPDFSDK_Widget::GetGroupMixXFAWidget() {
-  CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  CPDFXFA_Document* pDoc =
+      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView()) {
       CFX_WideString sName = GetName();
@@ -108,8 +108,8 @@
 }
 
 CXFA_FFWidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() const {
-  CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  CPDFXFA_Document* pDoc =
+      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (!m_pWidgetHandler) {
       if (CXFA_FFDocView* pDocView = pDoc->GetXFADocView())
@@ -223,8 +223,8 @@
 FX_BOOL CPDFSDK_Widget::OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
                                      PDFSDK_FieldAction& data,
                                      CPDFSDK_PageView* pPageView) {
-  CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  CPDFXFA_Document* pDoc =
+      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
 
   CXFA_FFWidget* hWidget = GetMixXFAWidget();
   if (!hWidget)
@@ -327,8 +327,8 @@
 }
 
 void CPDFSDK_Widget::SynchronizeXFAValue() {
-  CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  CPDFXFA_Document* pDoc =
+      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
   CXFA_FFDocView* pXFADocView = pDoc->GetXFADocView();
   if (!pXFADocView)
     return;
@@ -342,8 +342,8 @@
 }
 
 void CPDFSDK_Widget::SynchronizeXFAItems() {
-  CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  CPDFXFA_Document* pDoc =
+      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
   CXFA_FFDocView* pXFADocView = pDoc->GetXFADocView();
   if (!pXFADocView)
     return;
@@ -509,8 +509,8 @@
 
 FX_BOOL CPDFSDK_Widget::IsAppearanceValid() {
 #ifdef PDF_ENABLE_XFA
-  CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  CPDFXFA_Document* pDoc =
+      m_pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
   int nDocType = pDoc->GetDocType();
   if (nDocType != DOCTYPE_PDF && nDocType != DOCTYPE_STATIC_XFA)
     return TRUE;
@@ -852,10 +852,6 @@
   uint8_t alpha = m_pInterForm->GetHighlightAlpha();
 
   CFX_FloatRect rcDevice;
-  CPDFSDK_FormFillEnvironment* pEnv = m_pInterForm->GetFormFillEnv();
-  if (!pEnv)
-    return;
-
   CFX_Matrix page2device;
   pPageView->GetCurrentMatrix(page2device);
   page2device.Transform(((FX_FLOAT)rc.left), ((FX_FLOAT)rc.bottom),
@@ -1835,11 +1831,10 @@
 FX_BOOL CPDFSDK_Widget::OnAAction(CPDF_AAction::AActionType type,
                                   PDFSDK_FieldAction& data,
                                   CPDFSDK_PageView* pPageView) {
-  CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
-  CPDFSDK_FormFillEnvironment* pEnv = pDocument->GetEnv();
+  CPDFSDK_FormFillEnvironment* pFormFillEnv = pPageView->GetFormFillEnv();
 
 #ifdef PDF_ENABLE_XFA
-  CPDFXFA_Document* pDoc = pDocument->GetXFADocument();
+  CPDFXFA_Document* pDoc = pFormFillEnv->GetSDKDocument()->GetXFADocument();
   if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
     XFA_EVENTTYPE eEventType = GetXFAEventType(type, data.bWillCommit);
 
@@ -1879,9 +1874,9 @@
 
   CPDF_Action action = GetAAction(type);
   if (action.GetDict() && action.GetType() != CPDF_Action::Unknown) {
-    CPDFSDK_ActionHandler* pActionHandler = pEnv->GetActionHander();
-    return pActionHandler->DoAction_Field(action, type, pDocument,
-                                          GetFormField(), data);
+    CPDFSDK_ActionHandler* pActionHandler = pFormFillEnv->GetActionHander();
+    return pActionHandler->DoAction_Field(
+        action, type, pFormFillEnv->GetSDKDocument(), GetFormField(), data);
   }
   return FALSE;
 }
diff --git a/fpdfsdk/cpdfsdk_widgethandler.cpp b/fpdfsdk/cpdfsdk_widgethandler.cpp
index 8bae149..1621181 100644
--- a/fpdfsdk/cpdfsdk_widgethandler.cpp
+++ b/fpdfsdk/cpdfsdk_widgethandler.cpp
@@ -251,8 +251,8 @@
 
 #ifdef PDF_ENABLE_XFA
   CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
-  CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  CPDFXFA_Document* pDoc =
+      pPageView->GetFormFillEnv()->GetSDKDocument()->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
       pWidget->ResetAppearance(FALSE);
diff --git a/fpdfsdk/cpdfsdk_xfawidgethandler.cpp b/fpdfsdk/cpdfsdk_xfawidgethandler.cpp
index 17d80cc..b936bc4 100644
--- a/fpdfsdk/cpdfsdk_xfawidgethandler.cpp
+++ b/fpdfsdk/cpdfsdk_xfawidgethandler.cpp
@@ -54,9 +54,6 @@
   ASSERT(pPageView);
   ASSERT(pAnnot);
 
-  CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
-  CXFA_FFWidgetHandler* pWidgetHandler = GetXFAWidgetHandler(pAnnot);
-
   CFX_Graphics gs;
   gs.Create(pDevice);
 
@@ -64,10 +61,11 @@
   mt = *pUser2Device;
 
   FX_BOOL bIsHighlight = FALSE;
-  if (pSDKDoc->GetFocusAnnot() != pAnnot)
+  if (pPageView->GetFormFillEnv()->GetSDKDocument()->GetFocusAnnot() != pAnnot)
     bIsHighlight = TRUE;
 
-  pWidgetHandler->RenderWidget(pAnnot->GetXFAWidget(), &gs, &mt, bIsHighlight);
+  GetXFAWidgetHandler(pAnnot)->RenderWidget(pAnnot->GetXFAWidget(), &gs, &mt,
+                                            bIsHighlight);
 
   // to do highlight and shadow
 }
@@ -117,11 +115,11 @@
   if (!pPageView || !pAnnot)
     return FALSE;
 
-  CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
-  if (!pSDKDoc)
+  CPDFSDK_FormFillEnvironment* pFormFillEnv = pPageView->GetFormFillEnv();
+  if (!pFormFillEnv->GetSDKDocument())
     return FALSE;
 
-  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  CPDFXFA_Document* pDoc = pFormFillEnv->GetSDKDocument()->GetXFADocument();
   if (!pDoc)
     return FALSE;
 
@@ -346,11 +344,11 @@
   if (!pPageView)
     return nullptr;
 
-  CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
-  if (!pSDKDoc)
+  CPDFSDK_FormFillEnvironment* pFormFillEnv = pPageView->GetFormFillEnv();
+  if (!pFormFillEnv->GetSDKDocument())
     return nullptr;
 
-  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  CPDFXFA_Document* pDoc = pFormFillEnv->GetSDKDocument()->GetXFADocument();
   if (!pDoc)
     return nullptr;
 
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
index 12c55d3..c62de9b 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
@@ -691,8 +691,8 @@
                                              uint32_t nFlag) {
   if (!m_bNotifying) {
     ASSERT(pWidget);
-    CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
-    CPDFSDK_InterForm* pInterForm = pDocument->GetInterForm();
+    CPDFSDK_InterForm* pInterForm =
+        pPageView->GetFormFillEnv()->GetSDKDocument()->GetInterForm();
     pInterForm->OnCalculate(pWidget->GetFormField());
     m_bNotifying = FALSE;
   }
@@ -704,8 +704,8 @@
                                           uint32_t nFlag) {
   if (!m_bNotifying) {
     ASSERT(pWidget);
-    CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
-    CPDFSDK_InterForm* pInterForm = pDocument->GetInterForm();
+    CPDFSDK_InterForm* pInterForm =
+        pPageView->GetFormFillEnv()->GetSDKDocument()->GetInterForm();
 
     FX_BOOL bFormatted = FALSE;
     CFX_WideString sValue =
@@ -932,7 +932,8 @@
       int nAge = pData->pWidget->GetAppearanceAge();
       int nValueAge = pData->pWidget->GetValueAge();
 
-      CPDFSDK_Document* pDocument = pData->pPageView->GetSDKDocument();
+      CPDFSDK_FormFillEnvironment* pFormFillEnv =
+          pData->pPageView->GetFormFillEnv();
 
       PDFSDK_FieldAction fa;
       fa.bModifier = m_pEnv->IsCTRLKeyDown(nFlag);
@@ -973,7 +974,7 @@
           bRC = FALSE;
         }
 
-        if (pDocument->GetFocusAnnot() != pData->pWidget) {
+        if (pFormFillEnv->GetSDKDocument()->GetFocusAnnot() != pData->pWidget) {
           pFormFiller->CommitData(pData->pPageView, nFlag);
           bExit = TRUE;
         }
diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp
index 001ebb0..41fdc64 100644
--- a/fpdfsdk/fpdfview.cpp
+++ b/fpdfsdk/fpdfview.cpp
@@ -26,6 +26,7 @@
 #include "core/fxcrt/fx_safe_types.h"
 #include "core/fxge/cfx_fxgedevice.h"
 #include "core/fxge/cfx_gemodule.h"
+#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
 #include "fpdfsdk/fsdk_define.h"
 #include "fpdfsdk/fsdk_pauseadapter.h"
@@ -684,7 +685,7 @@
     // This will delete the |pPageView| object. We must cleanup the PageView
     // first because it will attempt to reset the View on the |pPage| during
     // destruction.
-    pPageView->GetSDKDocument()->RemovePageView(pPage);
+    pPageView->GetFormFillEnv()->GetSDKDocument()->RemovePageView(pPage);
     // If the page was owned then the pageview will have deleted the page.
     if (owned)
       return;