Remove IPDFSDK_AnnotHandler::CanAnswer() and HitTest().

Remove IPDFSDK_AnnotHandler and CPDFSDK_AnnotHandlerMgr pieces, and move
the IPDFSDK_AnnotHandler implementations into CPDFSDK_Annot.

Change-Id: Ibac1958b4f10fe3566598613a0637f5b7d060093
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/92231
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_annot.h b/fpdfsdk/cpdfsdk_annot.h
index d96876c..12aa975 100644
--- a/fpdfsdk/cpdfsdk_annot.h
+++ b/fpdfsdk/cpdfsdk_annot.h
@@ -30,6 +30,7 @@
   virtual CPDF_Annot* GetPDFAnnot() const;
   virtual CPDF_Annot::Subtype GetAnnotSubtype() const = 0;
   virtual CFX_FloatRect GetRect() const = 0;
+  virtual bool DoHitTest(const CFX_PointF& point) = 0;
 
   // Three cases: PDF page only, XFA page only, or XFA page backed by PDF page.
   IPDF_Page* GetPage();     // Returns XFA Page if possible, else PDF page.
diff --git a/fpdfsdk/cpdfsdk_annothandlermgr.cpp b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
index 7036a48..716315c 100644
--- a/fpdfsdk/cpdfsdk_annothandlermgr.cpp
+++ b/fpdfsdk/cpdfsdk_annothandlermgr.cpp
@@ -225,13 +225,3 @@
   DCHECK(pAnnot);
   return GetAnnotHandler(pAnnot)->GetViewBBox(pAnnot);
 }
-
-bool CPDFSDK_AnnotHandlerMgr::Annot_OnHitTest(CPDFSDK_Annot* pAnnot,
-                                              const CFX_PointF& point) {
-  DCHECK(pAnnot);
-  IPDFSDK_AnnotHandler* pAnnotHandler = GetAnnotHandler(pAnnot);
-  if (pAnnotHandler->CanAnswer(pAnnot))
-    return pAnnotHandler->HitTest(pAnnot, point);
-
-  return false;
-}
diff --git a/fpdfsdk/cpdfsdk_annothandlermgr.h b/fpdfsdk/cpdfsdk_annothandlermgr.h
index b6625f1..7fdd7b8 100644
--- a/fpdfsdk/cpdfsdk_annothandlermgr.h
+++ b/fpdfsdk/cpdfsdk_annothandlermgr.h
@@ -93,7 +93,6 @@
   bool Annot_IsIndexSelected(ObservedPtr<CPDFSDK_Annot>& pAnnot, int index);
 
   CFX_FloatRect Annot_OnGetViewBBox(CPDFSDK_Annot* pAnnot);
-  bool Annot_OnHitTest(CPDFSDK_Annot* pAnnot, const CFX_PointF& point);
 
  private:
   friend class CPDFSDK_BAAnnotHandlerTest;
diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp
index cb00e69..fc1b3d8 100644
--- a/fpdfsdk/cpdfsdk_baannot.cpp
+++ b/fpdfsdk/cpdfsdk_baannot.cpp
@@ -212,6 +212,10 @@
   return CPDFSDK_Annot::GetLayoutOrder();
 }
 
+bool CPDFSDK_BAAnnot::DoHitTest(const CFX_PointF& point) {
+  return false;
+}
+
 CPDF_Dest CPDFSDK_BAAnnot::GetDestination() const {
   if (m_pAnnot->GetSubtype() != CPDF_Annot::Subtype::LINK)
     return CPDF_Dest(nullptr);
diff --git a/fpdfsdk/cpdfsdk_baannot.h b/fpdfsdk/cpdfsdk_baannot.h
index a5f0284..c3c61c5 100644
--- a/fpdfsdk/cpdfsdk_baannot.h
+++ b/fpdfsdk/cpdfsdk_baannot.h
@@ -31,6 +31,7 @@
   CFX_FloatRect GetRect() const override;
   CPDF_Annot* GetPDFAnnot() const override;
   int GetLayoutOrder() const override;
+  bool DoHitTest(const CFX_PointF& point) override;
 
   virtual CPDF_Action GetAAction(CPDF_AAction::AActionType eAAT);
   virtual bool IsAppearanceValid();
diff --git a/fpdfsdk/cpdfsdk_baannothandler.cpp b/fpdfsdk/cpdfsdk_baannothandler.cpp
index 8290da4..249eef9 100644
--- a/fpdfsdk/cpdfsdk_baannothandler.cpp
+++ b/fpdfsdk/cpdfsdk_baannothandler.cpp
@@ -44,10 +44,6 @@
 
 CPDFSDK_BAAnnotHandler::~CPDFSDK_BAAnnotHandler() = default;
 
-bool CPDFSDK_BAAnnotHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
-  return false;
-}
-
 std::unique_ptr<CPDFSDK_Annot> CPDFSDK_BAAnnotHandler::NewAnnot(
     CPDF_Annot* pAnnot,
     CPDFSDK_PageView* pPageView) {
@@ -269,9 +265,3 @@
 bool CPDFSDK_BAAnnotHandler::Redo(CPDFSDK_Annot* pAnnot) {
   return false;
 }
-
-bool CPDFSDK_BAAnnotHandler::HitTest(CPDFSDK_Annot* pAnnot,
-                                     const CFX_PointF& point) {
-  DCHECK(pAnnot);
-  return GetViewBBox(pAnnot).Contains(point);
-}
diff --git a/fpdfsdk/cpdfsdk_baannothandler.h b/fpdfsdk/cpdfsdk_baannothandler.h
index 4afce70..5d0c90e 100644
--- a/fpdfsdk/cpdfsdk_baannothandler.h
+++ b/fpdfsdk/cpdfsdk_baannothandler.h
@@ -24,7 +24,6 @@
   ~CPDFSDK_BAAnnotHandler() override;
 
   // IPDFSDK_AnnotHandler:
-  bool CanAnswer(CPDFSDK_Annot* pAnnot) override;
   std::unique_ptr<CPDFSDK_Annot> NewAnnot(CPDF_Annot* pAnnot,
                                           CPDFSDK_PageView* pPageView) override;
 
@@ -37,7 +36,6 @@
   bool CanRedo(CPDFSDK_Annot* pAnnot) override;
   bool Undo(CPDFSDK_Annot* pAnnot) override;
   bool Redo(CPDFSDK_Annot* pAnnot) override;
-  bool HitTest(CPDFSDK_Annot* pAnnot, const CFX_PointF& point) override;
   void OnDraw(CPDFSDK_Annot* pAnnot,
               CFX_RenderDevice* pDevice,
               const CFX_Matrix& mtUser2Device,
diff --git a/fpdfsdk/cpdfsdk_pageview.cpp b/fpdfsdk/cpdfsdk_pageview.cpp
index 23c7de3..c9880a7 100644
--- a/fpdfsdk/cpdfsdk_pageview.cpp
+++ b/fpdfsdk/cpdfsdk_pageview.cpp
@@ -105,14 +105,14 @@
   CPDFSDK_AnnotHandlerMgr* pAnnotMgr = m_pFormFillEnv->GetAnnotHandlerMgr();
   CPDFSDK_AnnotForwardIteration annot_iteration(this);
   for (const auto& pSDKAnnot : annot_iteration) {
-    bool bHitTest = pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET;
+    const CPDF_Annot::Subtype sub_type = pSDKAnnot->GetAnnotSubtype();
+    bool do_hit_test = sub_type == CPDF_Annot::Subtype::WIDGET;
 #ifdef PDF_ENABLE_XFA
-    bHitTest = bHitTest ||
-               pSDKAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::XFAWIDGET;
+    do_hit_test = do_hit_test || sub_type == CPDF_Annot::Subtype::XFAWIDGET;
 #endif  // PDF_ENABLE_XFA
-    if (bHitTest) {
+    if (do_hit_test) {
       pAnnotMgr->Annot_OnGetViewBBox(pSDKAnnot.Get());
-      if (pAnnotMgr->Annot_OnHitTest(pSDKAnnot.Get(), point))
+      if (pSDKAnnot->DoHitTest(point))
         return pSDKAnnot.Get();
     }
   }
diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp
index 299af05..8cabedf 100644
--- a/fpdfsdk/cpdfsdk_widget.cpp
+++ b/fpdfsdk/cpdfsdk_widget.cpp
@@ -6,8 +6,10 @@
 
 #include "fpdfsdk/cpdfsdk_widget.h"
 
+#include "constants/access_permissions.h"
 #include "constants/annotation_common.h"
 #include "constants/appearance.h"
+#include "constants/form_flags.h"
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
@@ -672,6 +674,27 @@
   m_pInteractiveForm->ResetFieldAppearance(pFormField, absl::nullopt);
 }
 
+bool CPDFSDK_Widget::DoHitTest(const CFX_PointF& point) {
+  if (IsSignatureWidget() || !IsVisible())
+    return false;
+
+  if (GetFieldFlags() & pdfium::form_flags::kReadOnly)
+    return false;
+
+  bool do_hit_test = GetFieldType() == FormFieldType::kPushButton;
+  if (!do_hit_test) {
+    uint32_t perms = GetPDFPage()->GetDocument()->GetUserPermissions();
+    do_hit_test = (perms & pdfium::access_permissions::kFillForm) ||
+                  (perms & pdfium::access_permissions::kModifyAnnotation);
+  }
+  if (!do_hit_test)
+    return false;
+
+  auto* form_filler = m_pPageView->GetFormFillEnv()->GetInteractiveFormFiller();
+  CFX_FloatRect bbox(form_filler->GetViewBBox(GetPageView(), this));
+  return bbox.Contains(point);
+}
+
 void CPDFSDK_Widget::DrawAppearance(CFX_RenderDevice* pDevice,
                                     const CFX_Matrix& mtUser2Device,
                                     CPDF_Annot::AppearanceMode mode) {
diff --git a/fpdfsdk/cpdfsdk_widget.h b/fpdfsdk/cpdfsdk_widget.h
index 17ddc52..eeca473 100644
--- a/fpdfsdk/cpdfsdk_widget.h
+++ b/fpdfsdk/cpdfsdk_widget.h
@@ -53,6 +53,7 @@
   CPDF_Action GetAAction(CPDF_AAction::AActionType eAAT) override;
   bool IsAppearanceValid() override;
   int GetLayoutOrder() const override;
+  bool DoHitTest(const CFX_PointF& point) override;
   void DrawAppearance(CFX_RenderDevice* pDevice,
                       const CFX_Matrix& mtUser2Device,
                       CPDF_Annot::AppearanceMode mode) override;
diff --git a/fpdfsdk/cpdfsdk_widgethandler.cpp b/fpdfsdk/cpdfsdk_widgethandler.cpp
index 711f2a8..bc0d6e6 100644
--- a/fpdfsdk/cpdfsdk_widgethandler.cpp
+++ b/fpdfsdk/cpdfsdk_widgethandler.cpp
@@ -28,27 +28,6 @@
 
 CPDFSDK_WidgetHandler::~CPDFSDK_WidgetHandler() = default;
 
-bool CPDFSDK_WidgetHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
-  CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
-  if (pWidget->IsSignatureWidget())
-    return false;
-
-  if (!pWidget->IsVisible())
-    return false;
-
-  int nFieldFlags = pWidget->GetFieldFlags();
-  if (nFieldFlags & pdfium::form_flags::kReadOnly)
-    return false;
-
-  if (pWidget->GetFieldType() == FormFieldType::kPushButton)
-    return true;
-
-  CPDF_Page* pPage = pWidget->GetPDFPage();
-  uint32_t dwPermissions = pPage->GetDocument()->GetUserPermissions();
-  return (dwPermissions & pdfium::access_permissions::kFillForm) ||
-         (dwPermissions & pdfium::access_permissions::kModifyAnnotation);
-}
-
 std::unique_ptr<CPDFSDK_Annot> CPDFSDK_WidgetHandler::NewAnnot(
     CPDF_Annot* pAnnot,
     CPDFSDK_PageView* pPageView) {
@@ -300,12 +279,6 @@
          GetFormFillEnvironment()->GetInteractiveFormFiller()->Redo(pWidget);
 }
 
-bool CPDFSDK_WidgetHandler::HitTest(CPDFSDK_Annot* pAnnot,
-                                    const CFX_PointF& point) {
-  DCHECK(pAnnot);
-  return GetViewBBox(pAnnot).Contains(point);
-}
-
 bool CPDFSDK_WidgetHandler::IsFocusableAnnot(
     const CPDF_Annot::Subtype& annot_type) const {
   DCHECK_EQ(annot_type, CPDF_Annot::Subtype::WIDGET);
diff --git a/fpdfsdk/cpdfsdk_widgethandler.h b/fpdfsdk/cpdfsdk_widgethandler.h
index 47fdc21..28a187f 100644
--- a/fpdfsdk/cpdfsdk_widgethandler.h
+++ b/fpdfsdk/cpdfsdk_widgethandler.h
@@ -24,7 +24,6 @@
   ~CPDFSDK_WidgetHandler() override;
 
   // IPDFSDK_AnnotHandler:
-  bool CanAnswer(CPDFSDK_Annot* pAnnot) override;
   std::unique_ptr<CPDFSDK_Annot> NewAnnot(CPDF_Annot* pAnnot,
                                           CPDFSDK_PageView* pPageView) override;
 
@@ -37,7 +36,6 @@
   bool CanRedo(CPDFSDK_Annot* pAnnot) override;
   bool Undo(CPDFSDK_Annot* pAnnot) override;
   bool Redo(CPDFSDK_Annot* pAnnot) override;
-  bool HitTest(CPDFSDK_Annot* pAnnot, const CFX_PointF& point) override;
   void OnDraw(CPDFSDK_Annot* pAnnot,
               CFX_RenderDevice* pDevice,
               const CFX_Matrix& mtUser2Device,
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_widget.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_widget.cpp
index 2c9c665..fadec8a 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_widget.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_widget.cpp
@@ -10,6 +10,7 @@
 #include "xfa/fxfa/cxfa_ffdocview.h"
 #include "xfa/fxfa/cxfa_ffpageview.h"
 #include "xfa/fxfa/cxfa_ffwidget.h"
+#include "xfa/fxfa/cxfa_ffwidgethandler.h"
 
 CPDFXFA_Widget::CPDFXFA_Widget(CXFA_FFWidget* pXFAFFWidget,
                                CPDFSDK_PageView* pPageView)
@@ -29,6 +30,23 @@
   return GetXFAFFWidget()->GetLayoutItem()->GetAbsoluteRect().ToFloatRect();
 }
 
+bool CPDFXFA_Widget::DoHitTest(const CFX_PointF& point) {
+  CXFA_FFWidget* widget = GetXFAFFWidget();
+  CXFA_FFPageView* page_view = widget->GetPageView();
+  if (!page_view)
+    return false;
+
+  CXFA_FFDocView* doc_view = page_view->GetDocView();
+  if (!doc_view)
+    return false;
+
+  CXFA_FFWidgetHandler* widget_handler = doc_view->GetWidgetHandler();
+  if (!widget_handler)
+    return false;
+
+  return widget_handler->HitTest(widget, point) != FWL_WidgetHit::Unknown;
+}
+
 bool CPDFXFA_Widget::OnChangedFocus() {
   CXFA_FFWidget* widget = GetXFAFFWidget();
   CXFA_FFPageView* page_view = widget->GetPageView();
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_widget.h b/fpdfsdk/fpdfxfa/cpdfxfa_widget.h
index d20ef24..4e24a14 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_widget.h
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_widget.h
@@ -23,6 +23,7 @@
   CPDFXFA_Widget* AsXFAWidget() override;
   CPDF_Annot::Subtype GetAnnotSubtype() const override;
   CFX_FloatRect GetRect() const override;
+  bool DoHitTest(const CFX_PointF& point) override;
 
   CXFA_FFWidget* GetXFAFFWidget() const { return m_pXFAFFWidget.Get(); }
 
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_widgethandler.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_widgethandler.cpp
index 83854d0..80b88b0 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_widgethandler.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_widgethandler.cpp
@@ -204,11 +204,6 @@
 
 CPDFXFA_WidgetHandler::~CPDFXFA_WidgetHandler() = default;
 
-bool CPDFXFA_WidgetHandler::CanAnswer(CPDFSDK_Annot* pAnnot) {
-  CPDFXFA_Widget* pWidget = ToXFAWidget(pAnnot);
-  return pWidget && pWidget->GetXFAFFWidget();
-}
-
 std::unique_ptr<CPDFSDK_Annot> CPDFXFA_WidgetHandler::NewAnnot(
     CPDF_Annot* pAnnot,
     CPDFSDK_PageView* pPageView) {
@@ -295,24 +290,6 @@
   return pWidgetHandler->Redo(pXFAWidget->GetXFAFFWidget());
 }
 
-bool CPDFXFA_WidgetHandler::HitTest(CPDFSDK_Annot* pAnnot,
-                                    const CFX_PointF& point) {
-  CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot);
-  auto* pContext = static_cast<CPDFXFA_Context*>(
-      GetFormFillEnvironment()->GetDocExtension());
-  if (!pContext)
-    return false;
-
-  CXFA_FFDocView* pDocView = pContext->GetXFADocView();
-  if (!pDocView)
-    return false;
-
-  CXFA_FFWidgetHandler* pWidgetHandler = pDocView->GetWidgetHandler();
-  return pWidgetHandler &&
-         pWidgetHandler->HitTest(pXFAWidget->GetXFAFFWidget(), point) !=
-             FWL_WidgetHit::Unknown;
-}
-
 void CPDFXFA_WidgetHandler::OnMouseEnter(ObservedPtr<CPDFSDK_Annot>& pAnnot,
                                          Mask<FWL_EVENTFLAG> nFlag) {
   CPDFXFA_Widget* pXFAWidget = ToXFAWidget(pAnnot.Get());
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_widgethandler.h b/fpdfsdk/fpdfxfa/cpdfxfa_widgethandler.h
index c8920b5..436163a 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_widgethandler.h
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_widgethandler.h
@@ -27,7 +27,6 @@
   ~CPDFXFA_WidgetHandler() override;
 
   // IPDFSDK_AnnotHandler:
-  bool CanAnswer(CPDFSDK_Annot* pAnnot) override;
   std::unique_ptr<CPDFSDK_Annot> NewAnnot(CPDF_Annot* pAnnot,
                                           CPDFSDK_PageView* pPageView) override;
 
@@ -40,7 +39,6 @@
   bool CanRedo(CPDFSDK_Annot* pAnnot) override;
   bool Undo(CPDFSDK_Annot* pAnnot) override;
   bool Redo(CPDFSDK_Annot* pAnnot) override;
-  bool HitTest(CPDFSDK_Annot* pAnnot, const CFX_PointF& point) override;
   void OnDraw(CPDFSDK_Annot* pAnnot,
               CFX_RenderDevice* pDevice,
               const CFX_Matrix& mtUser2Device,
diff --git a/fpdfsdk/ipdfsdk_annothandler.h b/fpdfsdk/ipdfsdk_annothandler.h
index 12e6c69..2233ba5 100644
--- a/fpdfsdk/ipdfsdk_annothandler.h
+++ b/fpdfsdk/ipdfsdk_annothandler.h
@@ -35,7 +35,6 @@
     return m_pFormFillEnv.Get();
   }
 
-  virtual bool CanAnswer(CPDFSDK_Annot* pAnnot) = 0;
   virtual std::unique_ptr<CPDFSDK_Annot> NewAnnot(
       CPDF_Annot* pAnnot,
       CPDFSDK_PageView* pPageView) = 0;
@@ -50,7 +49,6 @@
   virtual bool CanRedo(CPDFSDK_Annot* pAnnot) = 0;
   virtual bool Undo(CPDFSDK_Annot* pAnnot) = 0;
   virtual bool Redo(CPDFSDK_Annot* pAnnot) = 0;
-  virtual bool HitTest(CPDFSDK_Annot* pAnnot, const CFX_PointF& point) = 0;
   virtual void OnDraw(CPDFSDK_Annot* pAnnot,
                       CFX_RenderDevice* pDevice,
                       const CFX_Matrix& mtUser2Device,