Introduce "underlying types" to abstract XFA differences.

See the comment in fsdk_define.h.  Also tidy up a bit,
and remove dead DeletePages().  This should help us
match master more closely (given corresponding changes
on the master side).

R=thestig@chromium.org

Review URL: https://codereview.chromium.org/1473503002 .
diff --git a/fpdfsdk/include/fsdk_baseannot.h b/fpdfsdk/include/fsdk_baseannot.h
index e9a3312..441036a 100644
--- a/fpdfsdk/include/fsdk_baseannot.h
+++ b/fpdfsdk/include/fsdk_baseannot.h
@@ -15,7 +15,8 @@
 
 #include "core/include/fpdfdoc/fpdf_doc.h"
 #include "core/include/fxcrt/fx_basic.h"
-#include "fx_systemhandler.h"
+#include "fpdfsdk/include/fsdk_define.h"
+#include "fpdfsdk/include/fx_systemhandler.h"
 
 class CPDFSDK_PageView;
 class CPDF_Annot;
@@ -91,6 +92,7 @@
                             CPDF_Matrix* pUser2Device,
                             CPDF_RenderOptions* pOptions) {}
 
+  UnderlyingPageType* GetUnderlyingPage();
   CPDF_Page* GetPDFPage();
   CPDFXFA_Page* GetPDFXFAPage();
 
diff --git a/fpdfsdk/include/fsdk_define.h b/fpdfsdk/include/fsdk_define.h
index 389e8e5..89c8f6c 100644
--- a/fpdfsdk/include/fsdk_define.h
+++ b/fpdfsdk/include/fsdk_define.h
@@ -22,6 +22,8 @@
 #include "core/include/fpdfdoc/fpdf_vt.h"
 #include "core/include/fxge/fx_ge.h"
 #include "core/include/fxge/fx_ge_win32.h"
+#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h"
+#include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h"
 #include "public/fpdfview.h"
 
 #ifdef _WIN32
@@ -87,6 +89,18 @@
   FX_FILESIZE m_nCurPos;
 };
 
+// Object types for public FPDF_ types; these correspond to next layer down
+// from fpdfsdk. For master, these are CPDF_ types, but for XFA, these are
+// CPDFXFA_ types.
+using UnderlyingDocumentType = CPDFXFA_Document;
+using UnderlyingPageType = CPDFXFA_Page;
+
+// Conversions to/from underlying types.
+UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc);
+FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc);
+
+UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page);
+
 // Conversions to/from FPDF_ types.
 CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc);
 FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc);
diff --git a/fpdfsdk/include/fsdk_mgr.h b/fpdfsdk/include/fsdk_mgr.h
index 709a137..c35d2ff 100644
--- a/fpdfsdk/include/fsdk_mgr.h
+++ b/fpdfsdk/include/fsdk_mgr.h
@@ -35,7 +35,7 @@
 
 class CPDFDoc_Environment final {
  public:
-  CPDFDoc_Environment(CPDFXFA_Document* pDoc, FPDF_FORMFILLINFO* pFFinfo);
+  CPDFDoc_Environment(UnderlyingDocumentType* pDoc, FPDF_FORMFILLINFO* pFFinfo);
   ~CPDFDoc_Environment();
 
   void Release() {
@@ -449,7 +449,9 @@
   FX_BOOL IsJSInitiated() const { return m_pInfo && m_pInfo->m_pJsPlatform; }
   void SetSDKDocument(CPDFSDK_Document* pFXDoc) { m_pSDKDoc = pFXDoc; }
   CPDFSDK_Document* GetSDKDocument() const { return m_pSDKDoc; }
-  CPDFXFA_Document* GetPDFXFADocument() const { return m_pXFADoc; }
+  UnderlyingDocumentType* GetUnderlyingDocument() const {
+    return m_pUnderlyingDoc;
+  }
   CFX_ByteString GetAppName() const { return ""; }
   IFX_SystemHandler* GetSysHandler() const { return m_pSysHandler.get(); }
   FPDF_FORMFILLINFO* GetFormFillInfo() const { return m_pInfo; }
@@ -465,28 +467,32 @@
   nonstd::unique_ptr<IJS_Runtime> m_pJSRuntime;
   FPDF_FORMFILLINFO* const m_pInfo;
   CPDFSDK_Document* m_pSDKDoc;
-  CPDFXFA_Document* const m_pXFADoc;
+  UnderlyingDocumentType* const m_pUnderlyingDoc;
   nonstd::unique_ptr<CFFL_IFormFiller> m_pIFormFiller;
   nonstd::unique_ptr<IFX_SystemHandler> m_pSysHandler;
 };
 
 class CPDFSDK_Document {
  public:
-  CPDFSDK_Document(CPDFXFA_Document* pDoc, CPDFDoc_Environment* pEnv);
+  CPDFSDK_Document(UnderlyingDocumentType* pDoc, CPDFDoc_Environment* pEnv);
   ~CPDFSDK_Document();
 
   CPDFSDK_InterForm* GetInterForm();
-  CPDFXFA_Document* GetDocument() const { return m_pDoc; }
+
+  UnderlyingDocumentType* GetUnderlyingDocument() const {
+    return GetXFADocument();
+  }
   CPDF_Document* GetPDFDocument() const {
     return m_pDoc ? m_pDoc->GetPDFDoc() : nullptr;
   }
+  CPDFXFA_Document* GetXFADocument() const { return m_pDoc; }
 
   int GetPageViewCount() const { return m_pageMap.size(); }
-  CPDFSDK_PageView* GetPageView(CPDFXFA_Page* pPDFXFAPage,
+  CPDFSDK_PageView* GetPageView(UnderlyingPageType* pPDFXFAPage,
                                 FX_BOOL ReNew = TRUE);
   CPDFSDK_PageView* GetPageView(int nIndex);
   CPDFSDK_PageView* GetCurrentView();
-  void ReMovePageView(CPDFXFA_Page* pPDFPage);
+  void RemovePageView(UnderlyingPageType* pPDFPage);
   void UpdateAllViews(CPDFSDK_PageView* pSender, CPDFSDK_Annot* pAnnot);
 
   CPDFSDK_Annot* GetFocusAnnot();
@@ -501,7 +507,6 @@
   FX_BOOL InsertPages(int nInsertAt,
                       const CPDF_Document* pSrcDoc,
                       const CFX_WordArray& arrSrcPages);
-  FX_BOOL DeletePages(int nStart, int nCount);
   FX_BOOL ReplacePages(int nPage,
                        const CPDF_Document* pSrcDoc,
                        const CFX_WordArray& arrSrcPages);
@@ -522,7 +527,7 @@
 
  private:
   std::map<CPDFXFA_Page*, CPDFSDK_PageView*> m_pageMap;
-  CPDFXFA_Document* m_pDoc;
+  UnderlyingDocumentType* m_pDoc;
   nonstd::unique_ptr<CPDFSDK_InterForm> m_pInterForm;
   CPDFSDK_Annot* m_pFocusAnnot;
   CPDFDoc_Environment* m_pEnv;
diff --git a/fpdfsdk/src/fpdfformfill.cpp b/fpdfsdk/src/fpdfformfill.cpp
index 11290ee..a1e8072 100644
--- a/fpdfsdk/src/fpdfformfill.cpp
+++ b/fpdfsdk/src/fpdfformfill.cpp
@@ -611,8 +611,8 @@
   CPDFSDK_PageView* pPageView = pSDKDoc->GetPageView(pPage, FALSE);
   if (pPageView) {
     pPageView->SetValid(FALSE);
-    // ReMovePageView() takes care of the delete for us.
-    pSDKDoc->ReMovePageView(pPage);
+    // RemovePageView() takes care of the delete for us.
+    pSDKDoc->RemovePageView(pPage);
   }
 }
 
@@ -634,7 +634,7 @@
   if (!pSDKDoc)
     return;
 
-  CPDF_Document* pDoc = pSDKDoc->GetDocument()->GetPDFDoc();
+  CPDF_Document* pDoc = pSDKDoc->GetPDFDocument();
   CPDF_Dictionary* pDic = pDoc->GetRoot();
   if (!pDic)
     return;
diff --git a/fpdfsdk/src/fpdfview.cpp b/fpdfsdk/src/fpdfview.cpp
index 8b3dff2..7e56806 100644
--- a/fpdfsdk/src/fpdfview.cpp
+++ b/fpdfsdk/src/fpdfview.cpp
@@ -23,16 +23,30 @@
 #include "third_party/base/nonstd_unique_ptr.h"
 #include "third_party/base/numerics/safe_conversions_impl.h"
 
+UnderlyingDocumentType* UnderlyingFromFPDFDocument(FPDF_DOCUMENT doc) {
+  return static_cast<UnderlyingDocumentType*>(doc);
+}
+
+FPDF_DOCUMENT FPDFDocumentFromUnderlying(UnderlyingDocumentType* doc) {
+  return static_cast<FPDF_DOCUMENT>(doc);
+}
+
+UnderlyingPageType* UnderlyingFromFPDFPage(FPDF_PAGE page) {
+  return static_cast<UnderlyingPageType*>(page);
+}
+
 CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
-  return doc ? static_cast<CPDFXFA_Document*>(doc)->GetPDFDoc() : nullptr;
+  return doc ? UnderlyingFromFPDFDocument(doc)->GetPDFDoc() : nullptr;
 }
 
 FPDF_DOCUMENT FPDFDocumentFromCPDFDocument(CPDF_Document* doc) {
-  return doc ? new CPDFXFA_Document(doc, CPDFXFA_App::GetInstance()) : nullptr;
+  return doc ? FPDFDocumentFromUnderlying(
+                   new CPDFXFA_Document(doc, CPDFXFA_App::GetInstance()))
+             : nullptr;
 }
 
 CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
-  return page ? static_cast<CPDFXFA_Page*>(page)->GetPDFPage() : nullptr;
+  return page ? UnderlyingFromFPDFPage(page)->GetPDFPage() : nullptr;
 }
 
 CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
@@ -416,15 +430,15 @@
 }
 
 DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
-  CPDFXFA_Document* pDoc = static_cast<CPDFXFA_Document*>(document);
+  UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
   return pDoc ? pDoc->GetPageCount() : 0;
 }
 
 DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
                                           int page_index) {
-  if (!document)
+  UnderlyingDocumentType* pDoc = UnderlyingFromFPDFDocument(document);
+  if (!pDoc)
     return nullptr;
-  CPDFXFA_Document* pDoc = static_cast<CPDFXFA_Document*>(document);
   if (page_index < 0 || page_index >= pDoc->GetPageCount())
     return nullptr;
 
@@ -432,12 +446,12 @@
 }
 
 DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
-  CPDFXFA_Page* pPage = static_cast<CPDFXFA_Page*>(page);
+  UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
   return pPage ? pPage->GetPageWidth() : 0.0;
 }
 
 DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
-  CPDFXFA_Page* pPage = static_cast<CPDFXFA_Page*>(page);
+  UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
   return pPage ? pPage->GetPageHeight() : 0.0;
 }
 
@@ -678,8 +692,7 @@
                                          double* page_y) {
   if (page == NULL || page_x == NULL || page_y == NULL)
     return;
-  CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
-
+  UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
   pPage->DeviceToPage(start_x, start_y, size_x, size_y, rotate, device_x,
                       device_y, page_x, page_y);
 }
@@ -696,7 +709,7 @@
                                          int* device_y) {
   if (!device_x || !device_y)
     return;
-  CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
+  UnderlyingPageType* pPage = UnderlyingFromFPDFPage(page);
   if (!pPage)
     return;
   pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
diff --git a/fpdfsdk/src/fsdk_actionhandler.cpp b/fpdfsdk/src/fsdk_actionhandler.cpp
index 451381b..0a8a066 100644
--- a/fpdfsdk/src/fsdk_actionhandler.cpp
+++ b/fpdfsdk/src/fsdk_actionhandler.cpp
@@ -434,7 +434,7 @@
                                           const CPDF_Action& action) {
   ASSERT(action);
 
-  CPDF_Document* pPDFDocument = pDocument->GetDocument()->GetPDFDoc();
+  CPDF_Document* pPDFDocument = pDocument->GetPDFDocument();
   ASSERT(pPDFDocument);
 
   CPDF_Dest MyDest = action.GetDest(pPDFDocument);
@@ -470,7 +470,7 @@
   CPDFDoc_Environment* pApp = pDocument->GetEnv();
   ASSERT(pApp != NULL);
 
-  CFX_ByteString sURI = action.GetURI(pDocument->GetDocument()->GetPDFDoc());
+  CFX_ByteString sURI = action.GetURI(pDocument->GetPDFDocument());
   pApp->FFI_DoURIAction(sURI.c_str());
 }
 
diff --git a/fpdfsdk/src/fsdk_annothandler.cpp b/fpdfsdk/src/fsdk_annothandler.cpp
index bf0993b..2965d32 100644
--- a/fpdfsdk/src/fsdk_annothandler.cpp
+++ b/fpdfsdk/src/fsdk_annothandler.cpp
@@ -646,42 +646,32 @@
 }
 
 void CPDFSDK_BFAnnotHandler::OnLoad(CPDFSDK_Annot* pAnnot) {
-  ASSERT(pAnnot != NULL);
+  if (pAnnot->GetSubType() == BFFT_SIGNATURE)
+    return;
+
+  CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
+  if (!pWidget->IsAppearanceValid())
+    pWidget->ResetAppearance(NULL, FALSE);
+
+  int nFieldType = pWidget->GetFieldType();
+  if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX) {
+    FX_BOOL bFormated = FALSE;
+    CFX_WideString sValue = pWidget->OnFormat(bFormated);
+    if (bFormated && nFieldType == FIELDTYPE_COMBOBOX) {
+      pWidget->ResetAppearance(sValue.c_str(), FALSE);
+    }
+  }
 
   CPDFSDK_PageView* pPageView = pAnnot->GetPageView();
-  ASSERT(pPageView != NULL);
-
   CPDFSDK_Document* pSDKDoc = pPageView->GetSDKDocument();
-  ASSERT(pSDKDoc != NULL);
-
-  CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
-  ASSERT(pDoc != NULL);
-
-  CFX_ByteString sSubType = pAnnot->GetSubType();
-
-  if (sSubType == BFFT_SIGNATURE) {
-  } else {
-    CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
-    if (!pWidget->IsAppearanceValid())
-      pWidget->ResetAppearance(NULL, FALSE);
-
-    int nFieldType = pWidget->GetFieldType();
-    if (nFieldType == FIELDTYPE_TEXTFIELD || nFieldType == FIELDTYPE_COMBOBOX) {
-      FX_BOOL bFormated = FALSE;
-      CFX_WideString sValue = pWidget->OnFormat(bFormated);
-      if (bFormated && nFieldType == FIELDTYPE_COMBOBOX) {
-        pWidget->ResetAppearance(sValue.c_str(), FALSE);
-      }
-    }
-
-    if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
-      if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
-        pWidget->ResetAppearance(FALSE);
-    }
-
-    if (m_pFormFiller)
-      m_pFormFiller->OnLoad(pAnnot);
+  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
+    if (!pWidget->IsAppearanceValid() && !pWidget->GetValue().IsEmpty())
+      pWidget->ResetAppearance(FALSE);
   }
+
+  if (m_pFormFiller)
+    m_pFormFiller->OnLoad(pAnnot);
 }
 
 FX_BOOL CPDFSDK_BFAnnotHandler::OnSetFocus(CPDFSDK_Annot* pAnnot,
@@ -842,7 +832,7 @@
   if (!pSDKDoc)
     return FALSE;
 
-  CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
+  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
   if (!pDoc)
     return FALSE;
 
@@ -1116,7 +1106,7 @@
   if (!pSDKDoc)
     return NULL;
 
-  CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
+  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
   if (!pDoc)
     return NULL;
 
diff --git a/fpdfsdk/src/fsdk_baseannot.cpp b/fpdfsdk/src/fsdk_baseannot.cpp
index ccb20ba..cecf5f7 100644
--- a/fpdfsdk/src/fsdk_baseannot.cpp
+++ b/fpdfsdk/src/fsdk_baseannot.cpp
@@ -981,6 +981,10 @@
   return;
 }
 
+UnderlyingPageType* CPDFSDK_Annot::GetUnderlyingPage() {
+  return GetPDFXFAPage();
+}
+
 CPDF_Page* CPDFSDK_Annot::GetPDFPage() {
   if (m_pPageView)
     return m_pPageView->GetPDFPage();
diff --git a/fpdfsdk/src/fsdk_baseform.cpp b/fpdfsdk/src/fsdk_baseform.cpp
index 5a94649..f6edc3b 100644
--- a/fpdfsdk/src/fsdk_baseform.cpp
+++ b/fpdfsdk/src/fsdk_baseform.cpp
@@ -36,7 +36,7 @@
 
 IXFA_Widget* CPDFSDK_Widget::GetMixXFAWidget() const {
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
+  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (!m_hMixXFAWidget) {
       if (IXFA_DocView* pDocView = pDoc->GetXFADocView()) {
@@ -59,29 +59,22 @@
 }
 
 IXFA_Widget* CPDFSDK_Widget::GetGroupMixXFAWidget() {
-  ASSERT(m_pPageView != NULL);
-
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  ASSERT(pSDKDoc != NULL);
-
-  CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
-  ASSERT(pDoc != NULL);
-
+  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (IXFA_DocView* pDocView = pDoc->GetXFADocView()) {
       CFX_WideString sName = GetName();
-
       if (!sName.IsEmpty())
         return pDocView->GetWidgetByName(sName);
     }
   }
 
-  return NULL;
+  return nullptr;
 }
 
 IXFA_WidgetHandler* CPDFSDK_Widget::GetXFAWidgetHandler() const {
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
+  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
   if (pDoc->GetDocType() == DOCTYPE_STATIC_XFA) {
     if (!m_pWidgetHandler) {
       if (IXFA_DocView* pDocView = pDoc->GetXFADocView()) {
@@ -196,15 +189,9 @@
 FX_BOOL CPDFSDK_Widget::OnXFAAAction(PDFSDK_XFAAActionType eXFAAAT,
                                      PDFSDK_FieldAction& data,
                                      CPDFSDK_PageView* pPageView) {
-  ASSERT(m_pPageView != NULL);
-
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  ASSERT(pSDKDoc != NULL);
-
-  CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
-  ASSERT(pDoc != NULL);
-
-  if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
+  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
+  if (IXFA_Widget* hWidget = GetMixXFAWidget()) {
     XFA_EVENTTYPE eEventType = GetXFAEventType(eXFAAAT);
 
     if (eEventType != XFA_EVENT_Unknown) {
@@ -314,14 +301,8 @@
 
 void CPDFSDK_Widget::SynchronizeXFAValue() {
 #ifdef PDF_ENABLE_XFA
-  ASSERT(m_pPageView != NULL);
-
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  ASSERT(pSDKDoc != NULL);
-
-  CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
-  ASSERT(pDoc != NULL);
-
+  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
   IXFA_DocView* pXFADocView = pDoc->GetXFADocView();
   if (!pXFADocView)
     return;
@@ -337,14 +318,8 @@
 
 void CPDFSDK_Widget::SynchronizeXFAItems() {
 #ifdef PDF_ENABLE_XFA
-  ASSERT(m_pPageView != NULL);
-
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  ASSERT(pSDKDoc != NULL);
-
-  CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
-  ASSERT(pDoc != NULL);
-
+  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
   IXFA_DocView* pXFADocView = pDoc->GetXFADocView();
   if (!pXFADocView)
     return;
@@ -527,14 +502,8 @@
 }
 
 FX_BOOL CPDFSDK_Widget::IsAppearanceValid() {
-  ASSERT(m_pPageView != NULL);
-
   CPDFSDK_Document* pSDKDoc = m_pPageView->GetSDKDocument();
-  ASSERT(pSDKDoc != NULL);
-
-  CPDFXFA_Document* pDoc = pSDKDoc->GetDocument();
-  ASSERT(pDoc != NULL);
-
+  CPDFXFA_Document* pDoc = pSDKDoc->GetXFADocument();
   int nDocType = pDoc->GetDocType();
   if (nDocType == DOCTYPE_PDF || nDocType == DOCTYPE_STATIC_XFA)
     return CPDFSDK_BAAnnot::IsAppearanceValid();
@@ -1986,13 +1955,8 @@
                                   PDFSDK_FieldAction& data,
                                   CPDFSDK_PageView* pPageView) {
   CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
-  ASSERT(pDocument != NULL);
-
-  CPDFXFA_Document* pDoc = pDocument->GetDocument();
-  ASSERT(pDoc != NULL);
-
+  CPDFXFA_Document* pDoc = pDocument->GetXFADocument();
   CPDFDoc_Environment* pEnv = pDocument->GetEnv();
-  ASSERT(pEnv != NULL);
 
   if (IXFA_Widget* hWidget = this->GetMixXFAWidget()) {
     XFA_EVENTTYPE eEventType = GetXFAEventType(type, data.bWillCommit);
@@ -2121,7 +2085,7 @@
 CFX_FloatRect CPDFSDK_XFAWidget::GetRect() const {
   CPDFSDK_PageView* pPageView = GetPageView();
   CPDFSDK_Document* pDocument = pPageView->GetSDKDocument();
-  CPDFXFA_Document* pDoc = pDocument->GetDocument();
+  CPDFXFA_Document* pDoc = pDocument->GetXFADocument();
   IXFA_DocView* pDocView = pDoc->GetXFADocView();
   IXFA_WidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
 
@@ -2141,8 +2105,7 @@
       m_bXfaValidationsEnabled(TRUE),
       m_bBusy(FALSE) {
   ASSERT(m_pDocument != NULL);
-  m_pInterForm =
-      new CPDF_InterForm(m_pDocument->GetDocument()->GetPDFDoc(), FALSE);
+  m_pInterForm = new CPDF_InterForm(m_pDocument->GetPDFDocument(), FALSE);
   m_pInterForm->SetFormNotify(this);
 
   for (int i = 0; i < kNumFieldTypes; ++i)
@@ -2185,7 +2148,7 @@
     return pWidget;
 
   CPDF_Dictionary* pControlDict = pControl->GetWidget();
-  CPDF_Document* pDocument = m_pDocument->GetDocument()->GetPDFDoc();
+  CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
   CPDFSDK_PageView* pPage = nullptr;
 
   if (CPDF_Dictionary* pPageDict = pControlDict->GetDict("P")) {
diff --git a/fpdfsdk/src/fsdk_mgr.cpp b/fpdfsdk/src/fsdk_mgr.cpp
index aac1673..69432a4 100644
--- a/fpdfsdk/src/fsdk_mgr.cpp
+++ b/fpdfsdk/src/fsdk_mgr.cpp
@@ -95,12 +95,9 @@
 }
 
 void CFX_SystemHandler::InvalidateRect(FX_HWND hWnd, FX_RECT rect) {
-  // g_pFormFillApp->FFI_Invalidate();
   CPDFSDK_Annot* pSDKAnnot = (CPDFSDK_Annot*)hWnd;
-  CPDFXFA_Page* pPage = NULL;
-  CPDFSDK_PageView* pPageView = NULL;
-  pPageView = pSDKAnnot->GetPageView();
-  pPage = pSDKAnnot->GetPDFXFAPage();
+  CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
+  UnderlyingPageType* pPage = pSDKAnnot->GetUnderlyingPage();
   if (!pPage || !pPageView)
     return;
   CPDF_Matrix page2device;
@@ -124,10 +121,8 @@
     CPDF_Point righttop = CPDF_Point(rect.right, rect.top);
     CPDF_Point ptA = pFFL->PWLtoFFL(leftbottom);
     CPDF_Point ptB = pFFL->PWLtoFFL(righttop);
-
     CPDFSDK_Annot* pAnnot = pFFL->GetSDKAnnot();
-    ASSERT(pAnnot);
-    CPDFXFA_Page* pPage = pAnnot->GetPDFXFAPage();
+    UnderlyingPageType* pPage = pAnnot->GetUnderlyingPage();
     ASSERT(pPage);
     m_pEnv->FFI_OutputSelectedRect(pPage, ptA.x, ptB.y, ptB.x, ptA.y);
   }
@@ -210,9 +205,9 @@
   return m_pEnv->FFI_GetLocalTime();
 }
 
-CPDFDoc_Environment::CPDFDoc_Environment(CPDFXFA_Document* pDoc,
+CPDFDoc_Environment::CPDFDoc_Environment(UnderlyingDocumentType* pDoc,
                                          FPDF_FORMFILLINFO* pFFinfo)
-    : m_pInfo(pFFinfo), m_pSDKDoc(NULL), m_pXFADoc(pDoc) {
+    : m_pInfo(pFFinfo), m_pSDKDoc(NULL), m_pUnderlyingDoc(pDoc) {
   m_pSysHandler.reset(new CFX_SystemHandler(this));
 }
 
@@ -390,14 +385,13 @@
   return m_pIFormFiller.get();
 }
 
-CPDFSDK_Document::CPDFSDK_Document(CPDFXFA_Document* pDoc,
+CPDFSDK_Document::CPDFSDK_Document(UnderlyingDocumentType* pDoc,
                                    CPDFDoc_Environment* pEnv)
     : m_pDoc(pDoc),
       m_pFocusAnnot(nullptr),
       m_pEnv(pEnv),
       m_bChangeMask(FALSE),
-      m_bBeingDestroyed(FALSE) {
-}
+      m_bBeingDestroyed(FALSE) {}
 
 CPDFSDK_Document::~CPDFSDK_Document() {
   m_bBeingDestroyed = TRUE;
@@ -410,29 +404,32 @@
   m_pageMap.clear();
 }
 
-CPDFSDK_PageView* CPDFSDK_Document::GetPageView(CPDFXFA_Page* pPDFXFAPage,
-                                                FX_BOOL ReNew) {
-  auto it = m_pageMap.find(pPDFXFAPage);
+CPDFSDK_PageView* CPDFSDK_Document::GetPageView(
+    UnderlyingPageType* pUnderlyingPage,
+    FX_BOOL ReNew) {
+  auto it = m_pageMap.find(pUnderlyingPage);
   if (it != m_pageMap.end())
     return it->second;
 
   if (!ReNew)
     return nullptr;
 
-  CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pPDFXFAPage);
-  m_pageMap[pPDFXFAPage] = pPageView;
+  CPDFSDK_PageView* pPageView = new CPDFSDK_PageView(this, pUnderlyingPage);
+  m_pageMap[pUnderlyingPage] = pPageView;
   // Delay to load all the annotations, to avoid endless loop.
   pPageView->LoadFXAnnots();
   return pPageView;
 }
 
 CPDFSDK_PageView* CPDFSDK_Document::GetCurrentView() {
-  CPDFXFA_Page* pPage = (CPDFXFA_Page*)m_pEnv->FFI_GetCurrentPage(m_pDoc);
+  UnderlyingPageType* pPage =
+      UnderlyingFromFPDFPage(m_pEnv->FFI_GetCurrentPage(m_pDoc));
   return pPage ? GetPageView(pPage, TRUE) : nullptr;
 }
 
 CPDFSDK_PageView* CPDFSDK_Document::GetPageView(int nIndex) {
-  CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
+  UnderlyingPageType* pTempPage =
+      UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex));
   if (!pTempPage)
     return nullptr;
 
@@ -441,8 +438,8 @@
 }
 
 void CPDFSDK_Document::ProcJavascriptFun() {
-  CPDFXFA_Document* pPDFDoc = GetDocument();
-  CPDF_DocJSActions docJS(pPDFDoc->GetPDFDoc());
+  CPDF_Document* pPDFDoc = GetPDFDocument();
+  CPDF_DocJSActions docJS(pPDFDoc);
   int iCount = docJS.CountJSActions();
   if (iCount < 1)
     return;
@@ -459,7 +456,7 @@
   if (!m_pDoc)
     return FALSE;
 
-  CPDF_Dictionary* pRoot = m_pDoc->GetPDFDoc()->GetRoot();
+  CPDF_Dictionary* pRoot = GetPDFDocument()->GetRoot();
   if (!pRoot)
     return FALSE;
 
@@ -484,12 +481,12 @@
 
 CPDF_OCContext* CPDFSDK_Document::GetOCContext() {
   if (!m_pOccontent)
-    m_pOccontent.reset(new CPDF_OCContext(m_pDoc->GetPDFDoc()));
+    m_pOccontent.reset(new CPDF_OCContext(GetPDFDocument()));
   return m_pOccontent.get();
 }
 
-void CPDFSDK_Document::ReMovePageView(CPDFXFA_Page* pPDFXFAPage) {
-  auto it = m_pageMap.find(pPDFXFAPage);
+void CPDFSDK_Document::RemovePageView(UnderlyingPageType* pUnderlyingPage) {
+  auto it = m_pageMap.find(pUnderlyingPage);
   if (it == m_pageMap.end())
     return;
 
@@ -502,11 +499,8 @@
   m_pageMap.erase(it);
 }
 
-CPDFXFA_Page* CPDFSDK_Document::GetPage(int nIndex) {
-  CPDFXFA_Page* pTempPage = (CPDFXFA_Page*)m_pEnv->FFI_GetPage(m_pDoc, nIndex);
-  if (!pTempPage)
-    return NULL;
-  return pTempPage;
+UnderlyingPageType* CPDFSDK_Document::GetPage(int nIndex) {
+  return UnderlyingFromFPDFPage(m_pEnv->FFI_GetPage(m_pDoc, nIndex));
 }
 
 CPDFSDK_InterForm* CPDFSDK_Document::GetInterForm() {
@@ -590,21 +584,6 @@
   return FALSE;
 }
 
-FX_BOOL CPDFSDK_Document::DeletePages(int nStart, int nCount) {
-  if (nStart < 0 || nStart >= GetPageCount() || nCount <= 0) {
-    return FALSE;
-  }
-
-  CPDFXFA_Page* pTempPage = NULL;
-  for (int i = nCount - 1; i >= 0; i--) {
-    pTempPage = GetPage(nStart + i);
-    if (pTempPage != NULL) {
-      ReMovePageView(pTempPage);
-    }
-  }
-  return TRUE;
-}
-
 void CPDFSDK_Document::OnCloseDocument() {
   KillFocusAnnot();
 }
@@ -1011,7 +990,7 @@
 
   SetLock(TRUE);
   m_page->AddRef();
-  if (m_pSDKDoc->GetDocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
+  if (m_pSDKDoc->GetXFADocument()->GetDocType() == DOCTYPE_DYNIMIC_XFA) {
     IXFA_PageView* pageView = NULL;
     pageView = m_page->GetXFAPageView();
     ASSERT(pageView != NULL);
@@ -1081,9 +1060,9 @@
 int CPDFSDK_PageView::GetPageIndex() {
   if (m_page) {
     CPDF_Dictionary* pDic = m_page->GetPDFPage()->m_pFormDict;
-    CPDFXFA_Document* pDoc = m_pSDKDoc->GetDocument();
+    CPDF_Document* pDoc = m_pSDKDoc->GetPDFDocument();
     if (pDoc && pDic) {
-      return pDoc->GetPDFDoc()->GetPageIndex(pDic->GetObjNum());
+      return pDoc->GetPageIndex(pDic->GetObjNum());
     }
   }
   return -1;
diff --git a/fpdfsdk/src/javascript/Document.cpp b/fpdfsdk/src/javascript/Document.cpp
index 003820a..1fa88f9 100644
--- a/fpdfsdk/src/javascript/Document.cpp
+++ b/fpdfsdk/src/javascript/Document.cpp
@@ -536,7 +536,7 @@
     CFX_RectArray aRefresh;
     aRefresh.Add(rcAnnot);
 
-    CPDFXFA_Page* pPage = pWidget->GetPDFXFAPage();
+    UnderlyingPageType* pPage = pWidget->GetUnderlyingPage();
     ASSERT(pPage);
 
     CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage);
@@ -779,8 +779,7 @@
 FX_BOOL Document::author(IJS_Context* cc,
                          CJS_PropValue& vp,
                          CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary =
-      m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -802,8 +801,7 @@
 FX_BOOL Document::info(IJS_Context* cc,
                        CJS_PropValue& vp,
                        CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary =
-      m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -860,8 +858,7 @@
 FX_BOOL Document::creationDate(IJS_Context* cc,
                                CJS_PropValue& vp,
                                CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary =
-      m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -882,8 +879,7 @@
 FX_BOOL Document::creator(IJS_Context* cc,
                           CJS_PropValue& vp,
                           CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary =
-      m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -939,8 +935,7 @@
 FX_BOOL Document::keywords(IJS_Context* cc,
                            CJS_PropValue& vp,
                            CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary =
-      m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -961,8 +956,7 @@
 FX_BOOL Document::modDate(IJS_Context* cc,
                           CJS_PropValue& vp,
                           CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary =
-      m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -983,8 +977,7 @@
 FX_BOOL Document::producer(IJS_Context* cc,
                            CJS_PropValue& vp,
                            CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary =
-      m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -1005,8 +998,7 @@
 FX_BOOL Document::subject(IJS_Context* cc,
                           CJS_PropValue& vp,
                           CFX_WideString& sError) {
-  CPDF_Dictionary* pDictionary =
-      m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -1027,11 +1019,10 @@
 FX_BOOL Document::title(IJS_Context* cc,
                         CJS_PropValue& vp,
                         CFX_WideString& sError) {
-  if (m_pDocument == NULL || m_pDocument->GetDocument() == NULL)
+  if (!m_pDocument || !m_pDocument->GetUnderlyingDocument())
     return FALSE;
 
-  CPDF_Dictionary* pDictionary =
-      m_pDocument->GetDocument()->GetPDFDoc()->GetInfo();
+  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -1521,7 +1512,7 @@
   int nWordNo = params.size() > 1 ? params[1].ToInt() : 0;
   bool bStrip = params.size() > 2 ? params[2].ToBool() : true;
 
-  CPDF_Document* pDocument = m_pDocument->GetDocument()->GetPDFDoc();
+  CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
   if (!pDocument)
     return FALSE;
 
@@ -1593,7 +1584,7 @@
 
   int nPageNo = params.size() > 0 ? params[0].ToInt() : 0;
 
-  CPDF_Document* pDocument = m_pDocument->GetDocument()->GetPDFDoc();
+  CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
   ASSERT(pDocument != NULL);
 
   CJS_Context* pContext = static_cast<CJS_Context*>(cc);
diff --git a/fpdfsdk/src/javascript/Field.cpp b/fpdfsdk/src/javascript/Field.cpp
index 4119b8c..39e15e9 100644
--- a/fpdfsdk/src/javascript/Field.cpp
+++ b/fpdfsdk/src/javascript/Field.cpp
@@ -3397,9 +3397,8 @@
     pWidget = pInterForm->GetWidget(pFormField->GetControl(0));
   } else {
     CPDFDoc_Environment* pEnv = m_pDocument->GetEnv();
-    ASSERT(pEnv);
-    CPDFXFA_Page* pPage =
-        (CPDFXFA_Page*)pEnv->FFI_GetCurrentPage(m_pDocument->GetDocument());
+    UnderlyingPageType* pPage = UnderlyingFromFPDFPage(
+        pEnv->FFI_GetCurrentPage(m_pDocument->GetUnderlyingDocument()));
     if (!pPage)
       return FALSE;
     if (CPDFSDK_PageView* pCurPageView = m_pDocument->GetPageView(pPage)) {
diff --git a/fpdfsdk/src/javascript/JS_Runtime.cpp b/fpdfsdk/src/javascript/JS_Runtime.cpp
index ac6e99d..b195b42 100644
--- a/fpdfsdk/src/javascript/JS_Runtime.cpp
+++ b/fpdfsdk/src/javascript/JS_Runtime.cpp
@@ -93,7 +93,7 @@
   for (auto* obs : m_observers)
     obs->OnDestroyed();
 
-  for (int i = 0; i < m_ContextArray.GetSize(); i++)
+  for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++)
     delete m_ContextArray.GetAt(i);
 
   m_ContextArray.RemoveAll();
diff --git a/fpdfsdk/src/javascript/app.cpp b/fpdfsdk/src/javascript/app.cpp
index f8b2e35..09b1444 100644
--- a/fpdfsdk/src/javascript/app.cpp
+++ b/fpdfsdk/src/javascript/app.cpp
@@ -200,7 +200,7 @@
 
   CJS_Context* pContext = (CJS_Context*)cc;
   CPDFSDK_Document* pCurDoc = pContext->GetReaderDocument();
-  CPDFXFA_Document* pDoc = pCurDoc->GetDocument();
+  CPDFXFA_Document* pDoc = pCurDoc->GetXFADocument();
   if (pDoc->GetDocType() == 1 || pDoc->GetDocType() == 2) {
     vp << JS_NUM_VIEWERVERSION_XFA;
     return TRUE;