Collapse some more xfa/non-xfa code in cpdfsdk annots.

cpdfsdk_annot.cpp:315 and cpdfsdk_annot.cpp:338 are nearly identical,
except that the #ifdef XFA code passed along the correct type from
the argument. Prefer this behaviour even when non-XFA.

Change-Id: Id4d5fbcc773ffd10746c4ddc77a571e818e5a957
Reviewed-on: https://pdfium-review.googlesource.com/35030
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_annot.cpp b/fpdfsdk/cpdfsdk_annot.cpp
index 6c17628..cfd87b3 100644
--- a/fpdfsdk/cpdfsdk_annot.cpp
+++ b/fpdfsdk/cpdfsdk_annot.cpp
@@ -75,10 +75,11 @@
 
 IPDF_Page* CPDFSDK_Annot::GetPage() {
 #ifdef PDF_ENABLE_XFA
-  return GetPDFXFAPage();
-#else   // PDF_ENABLE_XFA
-  return GetPDFPage();
+  CPDFXFA_Page* pXFAPage = GetPDFXFAPage();
+  if (pXFAPage)
+    return pXFAPage;
 #endif  // PDF_ENABLE_XFA
+  return GetPDFPage();
 }
 
 CPDF_Page* CPDFSDK_Annot::GetPDFPage() {
diff --git a/fpdfsdk/cpdfsdk_annot.h b/fpdfsdk/cpdfsdk_annot.h
index 2d97f08..459c6d0 100644
--- a/fpdfsdk/cpdfsdk_annot.h
+++ b/fpdfsdk/cpdfsdk_annot.h
@@ -45,7 +45,7 @@
   virtual CFX_FloatRect GetRect() const;
   virtual void SetRect(const CFX_FloatRect& rect);
 
-  IPDF_Page* GetPage();
+  IPDF_Page* GetPage();  // Returns XFA Page if possible, else PDF page.
   CPDF_Page* GetPDFPage();
 #ifdef PDF_ENABLE_XFA
   CPDFXFA_Page* GetPDFXFAPage();
diff --git a/fpdfsdk/cpdfsdk_annothandlermgr.cpp b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
index 117bf93..09d844f 100644
--- a/fpdfsdk/cpdfsdk_annothandlermgr.cpp
+++ b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
@@ -309,34 +309,29 @@
 #ifdef PDF_ENABLE_XFA
   CPDFSDK_PageView* pPageView = pSDKAnnot->GetPageView();
   CPDFXFA_Page* pPage = pPageView->GetPDFXFAPage();
-  if (!pPage)
-    return nullptr;
-  if (pPage->AsPDFPage()) {  // for pdf annots.
-    CPDFSDK_AnnotIterator ai(pSDKAnnot->GetPageView(),
-                             pSDKAnnot->GetAnnotSubtype());
-    CPDFSDK_Annot* pNext =
-        bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
-    return pNext;
-  }
-  // for xfa annots
-  std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
-      pPage->GetXFAPageView()->CreateWidgetIterator(
-          XFA_TRAVERSEWAY_Tranvalse, XFA_WidgetStatus_Visible |
-                                         XFA_WidgetStatus_Viewable |
-                                         XFA_WidgetStatus_Focused));
-  if (!pWidgetIterator)
-    return nullptr;
-  if (pWidgetIterator->GetCurrentWidget() != pSDKAnnot->GetXFAWidget())
-    pWidgetIterator->SetCurrentWidget(pSDKAnnot->GetXFAWidget());
-  CXFA_FFWidget* hNextFocus =
-      bNext ? pWidgetIterator->MoveToNext() : pWidgetIterator->MoveToPrevious();
-  if (!hNextFocus && pSDKAnnot)
-    hNextFocus = pWidgetIterator->MoveToFirst();
+  if (pPage && !pPage->AsPDFPage()) {
+    // For xfa annots in XFA pages not backed by PDF pages.
+    std::unique_ptr<IXFA_WidgetIterator> pWidgetIterator(
+        pPage->GetXFAPageView()->CreateWidgetIterator(
+            XFA_TRAVERSEWAY_Tranvalse, XFA_WidgetStatus_Visible |
+                                           XFA_WidgetStatus_Viewable |
+                                           XFA_WidgetStatus_Focused));
+    if (!pWidgetIterator)
+      return nullptr;
+    if (pWidgetIterator->GetCurrentWidget() != pSDKAnnot->GetXFAWidget())
+      pWidgetIterator->SetCurrentWidget(pSDKAnnot->GetXFAWidget());
+    CXFA_FFWidget* hNextFocus = bNext ? pWidgetIterator->MoveToNext()
+                                      : pWidgetIterator->MoveToPrevious();
+    if (!hNextFocus && pSDKAnnot)
+      hNextFocus = pWidgetIterator->MoveToFirst();
 
-  return pPageView->GetAnnotByXFAWidget(hNextFocus);
-#else   // PDF_ENABLE_XFA
-  CPDFSDK_AnnotIterator ai(pSDKAnnot->GetPageView(),
-                           CPDF_Annot::Subtype::WIDGET);
-  return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
+    return pPageView->GetAnnotByXFAWidget(hNextFocus);
+  }
 #endif  // PDF_ENABLE_XFA
+
+  // For PDF annots.
+  ASSERT(pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET);
+  CPDFSDK_AnnotIterator ai(pSDKAnnot->GetPageView(),
+                           pSDKAnnot->GetAnnotSubtype());
+  return bNext ? ai.GetNextAnnot(pSDKAnnot) : ai.GetPrevAnnot(pSDKAnnot);
 }