Deduplicate pointers to widgets in pwl.

See https://pdfium-review.googlesource.com/c/pdfium/+/58730 for
additional evidence this is correct. This CL introduces some double
observation that can be removed given better type consistency.

Change-Id: I5d856448bb367fa244f4769a05267aff91aa2cdc
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/58751
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/formfiller/cffl_formfiller.cpp b/fpdfsdk/formfiller/cffl_formfiller.cpp
index 1040172..e5a7d60 100644
--- a/fpdfsdk/formfiller/cffl_formfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_formfiller.cpp
@@ -378,10 +378,8 @@
       return nullptr;
 
     CPWL_Wnd::CreateParams cp = GetCreateParam();
-    cp.pAttachedWidget.Reset(m_pWidget.Get());
-
     auto pPrivateData = pdfium::MakeUnique<CFFL_PrivateData>();
-    pPrivateData->pWidget = m_pWidget.Get();
+    pPrivateData->pWidget.Reset(m_pWidget.Get());
     pPrivateData->pPageView = pPageView;
     pPrivateData->nWidgetAppearanceAge = m_pWidget->GetAppearanceAge();
     pPrivateData->nWidgetValueAge = 0;
@@ -580,7 +578,7 @@
 
 void CFFL_FormFiller::TimerProc() {}
 
-IPWL_SystemHandler* CFFL_FormFiller::GetSystemHandler() const {
+CFX_SystemHandler* CFFL_FormFiller::GetSystemHandler() const {
   return m_pFormFillEnv->GetSysHandler();
 }
 
diff --git a/fpdfsdk/formfiller/cffl_formfiller.h b/fpdfsdk/formfiller/cffl_formfiller.h
index 0397c1f..88c32d8 100644
--- a/fpdfsdk/formfiller/cffl_formfiller.h
+++ b/fpdfsdk/formfiller/cffl_formfiller.h
@@ -12,13 +12,14 @@
 
 #include "core/fpdfdoc/cba_fontmap.h"
 #include "core/fxcrt/unowned_ptr.h"
+#include "fpdfsdk/cfx_systemhandler.h"
 #include "fpdfsdk/cpdfsdk_fieldaction.h"
+#include "fpdfsdk/cpdfsdk_widget.h"
 #include "fpdfsdk/formfiller/cffl_interactiveformfiller.h"
 
 class CPDFSDK_Annot;
 class CPDFSDK_FormFillEnvironment;
 class CPDFSDK_PageView;
-class CPDFSDK_Widget;
 
 class CFFL_FormFiller : public CPWL_Wnd::ProviderIface,
                         public CPWL_TimerHandler {
@@ -85,7 +86,7 @@
 
   // CPWL_TimerHandler:
   void TimerProc() override;
-  IPWL_SystemHandler* GetSystemHandler() const override;
+  CFX_SystemHandler* GetSystemHandler() const override;  // Covariant return.
 
   // CPWL_Wnd::ProviderIface:
   CFX_Matrix GetWindowMatrix(const CPWL_Wnd::PrivateData* pAttached) override;
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
index b760365..c98afcc 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
@@ -589,7 +589,7 @@
     bool* bBottom,
     float* fPopupRet) {
   auto* pData = static_cast<const CFFL_PrivateData*>(pAttached);
-  CPDFSDK_Widget* pWidget = pData->pWidget;
+  CPDFSDK_Widget* pWidget = pData->GetWidget();
   CPDF_Page* pPage = pWidget->GetPDFPage();
 
   CFX_FloatRect rcPageView(0, pPage->GetPageHeight(), pPage->GetPageWidth(), 0);
@@ -813,7 +813,7 @@
   auto* pData = static_cast<const CFFL_PrivateData*>(pAttached);
   ASSERT(pData->pWidget);
 
-  ObservedPtr<CPDFSDK_Annot> pObserved(pData->pWidget);
+  ObservedPtr<CPDFSDK_Annot> pObserved(pData->GetWidget());
   return OnPreOpen(&pObserved, pData->pPageView, nFlag) || !pObserved;
 }
 
@@ -823,7 +823,7 @@
   auto* pData = static_cast<const CFFL_PrivateData*>(pAttached);
   ASSERT(pData->pWidget);
 
-  ObservedPtr<CPDFSDK_Annot> pObserved(pData->pWidget);
+  ObservedPtr<CPDFSDK_Annot> pObserved(pData->GetWidget());
   return OnPostOpen(&pObserved, pData->pPageView, nFlag) || !pObserved;
 }
 
@@ -908,11 +908,11 @@
       *static_cast<const CFFL_PrivateData*>(pAttached);
   ASSERT(privateData.pWidget);
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(privateData.pWidget);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(privateData.GetWidget());
 
 #ifdef PDF_ENABLE_XFA
   if (pFormFiller->IsFieldFull(privateData.pPageView)) {
-    ObservedPtr<CPDFSDK_Annot> pObserved(privateData.pWidget);
+    ObservedPtr<CPDFSDK_Annot> pObserved(privateData.GetWidget());
     if (OnFull(&pObserved, privateData.pPageView, nFlag) || !pObserved)
       return {true, true};
   }
@@ -945,13 +945,14 @@
                              fa);
   pFormFiller->SaveState(privateData.pPageView);
 
-  ObservedPtr<CPDFSDK_Annot> pObserved(privateData.pWidget);
+  ObservedPtr<CPDFSDK_Annot> pObserved(privateData.GetWidget());
   bool action_status = privateData.pWidget->OnAAction(
       CPDF_AAction::kKeyStroke, &fa, privateData.pPageView);
 
-  if (!pObserved || !IsValidAnnot(privateData.pPageView, privateData.pWidget))
+  if (!pObserved ||
+      !IsValidAnnot(privateData.pPageView, privateData.GetWidget())) {
     return {true, true};
-
+  }
   if (!action_status)
     return {true, false};
 
@@ -971,7 +972,7 @@
   } else {
     pFormFiller->RestoreState(privateData.pPageView);
   }
-  if (pFormFillEnv->GetFocusAnnot() == privateData.pWidget)
+  if (pFormFillEnv->GetFocusAnnot() == privateData.GetWidget())
     return {false, bExit};
 
   pFormFiller->CommitData(privateData.pPageView, nFlag);
@@ -987,3 +988,7 @@
 std::unique_ptr<CPWL_Wnd::PrivateData> CFFL_PrivateData::Clone() const {
   return pdfium::MakeUnique<CFFL_PrivateData>(*this);
 }
+
+CPDFSDK_Widget* CFFL_PrivateData::GetWidget() const {
+  return pWidget.Get();
+}
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.h b/fpdfsdk/formfiller/cffl_interactiveformfiller.h
index 6541374..fb546ab 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.h
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.h
@@ -11,6 +11,7 @@
 #include <memory>
 #include <utility>
 
+#include "core/fxcrt/observed_ptr.h"
 #include "core/fxcrt/unowned_ptr.h"
 #include "fpdfsdk/cpdfsdk_annot.h"
 #include "fpdfsdk/pwl/cpwl_edit.h"
@@ -178,8 +179,9 @@
 
   // CPWL_Wnd::PrivateData:
   std::unique_ptr<CPWL_Wnd::PrivateData> Clone() const override;
+  CPDFSDK_Widget* GetWidget() const override;
 
-  CPDFSDK_Widget* pWidget = nullptr;
+  ObservedPtr<CPDFSDK_Widget> pWidget;
   CPDFSDK_PageView* pPageView = nullptr;
   uint32_t nWidgetAppearanceAge = 0;
   uint32_t nWidgetValueAge = 0;
diff --git a/fpdfsdk/pwl/cpwl_wnd.cpp b/fpdfsdk/pwl/cpwl_wnd.cpp
index 6316ced..d2daa9c 100644
--- a/fpdfsdk/pwl/cpwl_wnd.cpp
+++ b/fpdfsdk/pwl/cpwl_wnd.cpp
@@ -12,6 +12,7 @@
 #include <vector>
 
 #include "core/fxge/cfx_renderdevice.h"
+#include "fpdfsdk/cpdfsdk_widget.h"
 #include "fpdfsdk/pwl/cpwl_scroll_bar.h"
 #include "public/fpdf_fwlevent.h"
 #include "third_party/base/ptr_util.h"
@@ -272,8 +273,7 @@
   if (!pSH)
     return true;
 
-  CPDFSDK_Widget* widget =
-      ToCPDFSDKWidget(m_CreationParams.pAttachedWidget.Get());
+  CPDFSDK_Widget* widget = m_pAttachedData->GetWidget();
   if (!widget)
     return true;
 
diff --git a/fpdfsdk/pwl/cpwl_wnd.h b/fpdfsdk/pwl/cpwl_wnd.h
index 2349783..d0fe7b7 100644
--- a/fpdfsdk/pwl/cpwl_wnd.h
+++ b/fpdfsdk/pwl/cpwl_wnd.h
@@ -15,10 +15,10 @@
 #include "core/fxcrt/unowned_ptr.h"
 #include "core/fxge/cfx_color.h"
 #include "core/fxge/cfx_renderdevice.h"
-#include "fpdfsdk/cpdfsdk_widget.h"
 #include "fpdfsdk/pwl/cpwl_timer.h"
 #include "fpdfsdk/pwl/cpwl_timer_handler.h"
 
+class CPDFSDK_Widget;
 class CPWL_Edit;
 class CPWL_MsgControl;
 class CPWL_ScrollBar;
@@ -98,6 +98,7 @@
    public:
     virtual ~PrivateData() = default;
     virtual std::unique_ptr<PrivateData> Clone() const = 0;
+    virtual CPDFSDK_Widget* GetWidget() const = 0;
   };
 
   class ProviderIface : public Observable {
@@ -127,7 +128,6 @@
     UnownedPtr<FocusHandlerIface> pFocusHandler;      // optional
     uint32_t dwFlags = 0;                             // optional
     CFX_Color sBackgroundColor;                       // optional
-    ObservedPtr<CPDFSDK_Widget> pAttachedWidget;      // required
     BorderStyle nBorderStyle = BorderStyle::SOLID;    // optional
     int32_t dwBorderWidth = 1;                        // optional
     CFX_Color sBorderColor;                           // optional