Merge to XFA: Introduce CPDFPageFromFPFDPage()

Original Review URL: https://codereview.chromium.org/1406543004 .
(cherry picked from commit 943ea142b6a7e6edc91fe3c307013129e6eb0ef3)

R=thestig@chromium.org

Review URL: https://codereview.chromium.org/1411623002 .
diff --git a/fpdfsdk/src/fpdf_flatten.cpp b/fpdfsdk/src/fpdf_flatten.cpp
index 73410b6..9d5a0d4 100644
--- a/fpdfsdk/src/fpdf_flatten.cpp
+++ b/fpdfsdk/src/fpdf_flatten.cpp
@@ -312,14 +312,11 @@
 }
 
 DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!page) {
     return FLATTEN_FAIL;
   }
 
-  CPDF_Page* pPage = ((CPDFXFA_Page*)(page))->GetPDFPage();
-  if (!pPage)
-    return FLATTEN_FAIL;
-
   CPDF_Document* pDocument = pPage->m_pDocument;
   CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
 
diff --git a/fpdfsdk/src/fpdf_progressive.cpp b/fpdfsdk/src/fpdf_progressive.cpp
index f5e11e8..4412648 100644
--- a/fpdfsdk/src/fpdf_progressive.cpp
+++ b/fpdfsdk/src/fpdf_progressive.cpp
@@ -20,16 +20,10 @@
                                                   int rotate,
                                                   int flags,
                                                   IFSDK_PAUSE* pause) {
-  if (bitmap == NULL || page == NULL)
+  if (!bitmap || !pause || pause->version != 1)
     return FPDF_RENDER_FAILED;
 
-  if (!pause)
-    return FPDF_RENDER_FAILED;
-
-  if (pause->version != 1)
-    return FPDF_RENDER_FAILED;
-
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return FPDF_RENDER_FAILED;
 
@@ -66,16 +60,10 @@
 
 DLLEXPORT int STDCALL FPDF_RenderPage_Continue(FPDF_PAGE page,
                                                IFSDK_PAUSE* pause) {
-  if (page == NULL)
+  if (!pause || pause->version != 1)
     return FPDF_RENDER_FAILED;
 
-  if (!pause)
-    return FPDF_RENDER_FAILED;
-
-  if (pause->version != 1)
-    return FPDF_RENDER_FAILED;
-
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return FPDF_RENDER_FAILED;
 
@@ -93,9 +81,7 @@
 }
 
 DLLEXPORT void STDCALL FPDF_RenderPage_Close(FPDF_PAGE page) {
-  if (!page)
-    return;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
 
diff --git a/fpdfsdk/src/fpdf_transformpage.cpp b/fpdfsdk/src/fpdf_transformpage.cpp
index dbf97aa..132f668 100644
--- a/fpdfsdk/src/fpdf_transformpage.cpp
+++ b/fpdfsdk/src/fpdf_transformpage.cpp
@@ -11,10 +11,6 @@
 
 namespace {
 
-CPDF_Page* GetPageFromFPDFPage(FPDF_PAGE page) {
-  return page ? static_cast<CPDFXFA_Page*>(page)->GetPDFPage() : nullptr;
-}
-
 void SetBoundingBox(CPDF_Page* page,
                     const CFX_ByteStringC& key,
                     float left,
@@ -55,7 +51,7 @@
                                             float bottom,
                                             float right,
                                             float top) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
 
@@ -67,7 +63,7 @@
                                            float bottom,
                                            float right,
                                            float top) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
 
@@ -79,7 +75,7 @@
                                                  float* bottom,
                                                  float* right,
                                                  float* top) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   return pPage && GetBoundingBox(pPage, "MediaBox", left, bottom, right, top);
 }
 
@@ -88,14 +84,14 @@
                                                 float* bottom,
                                                 float* right,
                                                 float* top) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   return pPage && GetBoundingBox(pPage, "CropBox", left, bottom, right, top);
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
                                                        FS_MATRIX* matrix,
                                                        FS_RECTF* clipRect) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return FALSE;
 
@@ -268,7 +264,7 @@
 
 DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page,
                                                FPDF_CLIPPATH clipPath) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
 
diff --git a/fpdfsdk/src/fpdfdoc.cpp b/fpdfsdk/src/fpdfdoc.cpp
index d4b6b08..d1b71ac 100644
--- a/fpdfsdk/src/fpdfdoc.cpp
+++ b/fpdfsdk/src/fpdfdoc.cpp
@@ -203,8 +203,7 @@
 
 DLLEXPORT FPDF_LINK STDCALL
 FPDFLink_GetLinkAtPoint(FPDF_PAGE page, double x, double y) {
-  CPDFXFA_Page* pXFAPage = (CPDFXFA_Page*)page;
-  CPDF_Page* pPage = pXFAPage->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return nullptr;
 
@@ -218,8 +217,7 @@
 
 DLLEXPORT int STDCALL
 FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page, double x, double y) {
-  CPDFXFA_Page* pXFAPage = (CPDFXFA_Page*)page;
-  CPDF_Page* pPage = pXFAPage->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return -1;
 
@@ -261,10 +259,10 @@
 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page,
                                                int* startPos,
                                                FPDF_LINK* linkAnnot) {
-  if (!page || !startPos || !linkAnnot)
+  if (!startPos || !linkAnnot)
     return FALSE;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
-  if (!pPage->m_pFormDict)
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage || !pPage->m_pFormDict)
     return FALSE;
   CPDF_Array* pAnnots = pPage->m_pFormDict->GetArray("Annots");
   if (!pAnnots)
diff --git a/fpdfsdk/src/fpdfeditimg.cpp b/fpdfsdk/src/fpdfeditimg.cpp
index 09295aa..cd4618e 100644
--- a/fpdfsdk/src/fpdfeditimg.cpp
+++ b/fpdfsdk/src/fpdfeditimg.cpp
@@ -32,7 +32,7 @@
   CPDF_ImageObject* pImgObj = (CPDF_ImageObject*)image_object;
   pImgObj->m_GeneralState.GetModify();
   for (int index = 0; index < nCount; index++) {
-    CPDF_Page* pPage = ((CPDFXFA_Page*)pages[index])->GetPDFPage();
+    CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
     if (!pPage)
       continue;
     pImgObj->m_pImage->ResetCache(pPage, NULL);
@@ -73,7 +73,7 @@
   CPDF_ImageObject* pImgObj = (CPDF_ImageObject*)image_object;
   pImgObj->m_GeneralState.GetModify();
   for (int index = 0; index < nCount; index++) {
-    CPDF_Page* pPage = ((CPDFXFA_Page*)pages[index])->GetPDFPage();
+    CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
     if (!pPage)
       continue;
     pImgObj->m_pImage->ResetCache(pPage, NULL);
diff --git a/fpdfsdk/src/fpdfeditpage.cpp b/fpdfsdk/src/fpdfeditpage.cpp
index e7bbf05..0cf7c88 100644
--- a/fpdfsdk/src/fpdfeditpage.cpp
+++ b/fpdfsdk/src/fpdfeditpage.cpp
@@ -89,7 +89,7 @@
 }
 
 DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
@@ -133,7 +133,7 @@
 
 DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
                                              FPDF_PAGEOBJECT page_obj) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
@@ -175,9 +175,6 @@
     default:
       break;
   }
-
-  //	pPage->ParseContent();
-  // pPage->GenerateContent();
 }
 
 DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
@@ -189,29 +186,22 @@
     return -1;
   }
   return pPage->CountObjects();
-  //	return 0;
 }
 
 DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
                                                      int index) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
           "Page")) {
     return NULL;
   }
   return pPage->GetObjectByIndex(index);
-  //	return NULL;
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
-  if (!page)
-    return FALSE;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
-  if (!pPage)
-    return FALSE;
-
-  return pPage->BackgroundAlphaNeeded();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  return pPage && pPage->BackgroundAlphaNeeded();
 }
 
 DLLEXPORT FPDF_BOOL STDCALL
@@ -253,7 +243,7 @@
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
@@ -288,9 +278,7 @@
                                                 double d,
                                                 double e,
                                                 double f) {
-  if (page == NULL)
-    return;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
   CPDF_AnnotList AnnotList(pPage);
@@ -318,12 +306,7 @@
 }
 
 DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) {
-  if (page == NULL)
-    return;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
-  if (!pPage)
-    return;
-
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
diff --git a/fpdfsdk/src/fpdfformfill.cpp b/fpdfsdk/src/fpdfformfill.cpp
index e86a47b..9fe024c 100644
--- a/fpdfsdk/src/fpdfformfill.cpp
+++ b/fpdfsdk/src/fpdfformfill.cpp
@@ -42,10 +42,9 @@
                                                    FPDF_PAGE page,
                                                    double page_x,
                                                    double page_y) {
-  if (!page || !hHandle)
+  if (!hHandle)
     return -1;
-
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (pPage) {
     CPDF_InterForm interform(pPage->m_pDocument, FALSE);
     CPDF_FormControl* pFormCtrl = interform.GetControlAtPoint(
@@ -115,10 +114,11 @@
                                                       FPDF_PAGE page,
                                                       double page_x,
                                                       double page_y) {
-  if (!page || !hHandle)
+  if (!hHandle)
     return -1;
-
-  CPDF_Page* pPage = (CPDF_Page*)page;
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage)
+    return -1;
   CPDF_InterForm interform(pPage->m_pDocument, FALSE);
   int z_order = -1;
   (void)interform.GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y,
diff --git a/fpdfsdk/src/fpdfview.cpp b/fpdfsdk/src/fpdfview.cpp
index 5d9bf4b..6254eb2 100644
--- a/fpdfsdk/src/fpdfview.cpp
+++ b/fpdfsdk/src/fpdfview.cpp
@@ -26,6 +26,10 @@
   return doc ? static_cast<CPDFXFA_Document*>(doc)->GetPDFDoc() : nullptr;
 }
 
+CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
+  return page ? static_cast<CPDFXFA_Page*>(page)->GetPDFPage() : nullptr;
+}
+
 CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
   m_pFS = pFS;
   m_nCurPos = 0;
@@ -436,13 +440,11 @@
   if (!page)
     return 0.0;
   return ((CPDFXFA_Page*)page)->GetPageWidth();
-  //	return ((CPDF_Page*)page)->GetPageWidth();
 }
 
 DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
   if (!page)
     return 0.0;
-  //	return ((CPDF_Page*)page)->GetPageHeight();
   return ((CPDFXFA_Page*)page)->GetPageHeight();
 }
 
@@ -463,9 +465,7 @@
                                        int size_y,
                                        int rotate,
                                        int flags) {
-  if (page == NULL)
-    return;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
 
@@ -625,12 +625,11 @@
                                              int size_y,
                                              int rotate,
                                              int flags) {
-  if (bitmap == NULL || page == NULL)
+  if (!bitmap)
     return;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
-
   CRenderContext* pContext = new CRenderContext;
   pPage->SetPrivateData((void*)1, pContext, DropContext);
 #ifdef _SKIA_SUPPORT_
@@ -702,9 +701,11 @@
                                          double page_y,
                                          int* device_x,
                                          int* device_y) {
-  if (page == NULL || device_x == NULL || device_y == NULL)
+  if (!device_x || !device_y)
     return;
   CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
+  if (!pPage)
+    return;
   pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
                       device_x, device_y);
 }
@@ -804,8 +805,8 @@
                             int flags,
                             FX_BOOL bNeedToRestore,
                             IFSDK_PAUSE_Adapter* pause) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
-  if (pPage == NULL)
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage)
     return;
 
   if (!pContext->m_pOptions)