Still still more UnownedPtr<> conversions.

This uncovered a dangling reference in CPWL_EditCtrl, so add an
OnDestroy() method to "pre-finalize".

This also uncovered a stack-variable ordering issue in CFWL_ListBox,
so shuffle around and comment.

-- Move some now non-trivial destructors out of line.
-- Pack CFWL_ThemeText a little tighter.

Change-Id: I86b4d121c1851b3384db9ec091c51d6a6265c95d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/78893
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/pwl/cpwl_edit_ctrl.cpp b/fpdfsdk/pwl/cpwl_edit_ctrl.cpp
index 3e16b62..d353409 100644
--- a/fpdfsdk/pwl/cpwl_edit_ctrl.cpp
+++ b/fpdfsdk/pwl/cpwl_edit_ctrl.cpp
@@ -33,6 +33,10 @@
   m_pEdit->Initialize();
 }
 
+void CPWL_EditCtrl::OnDestroy() {
+  m_pEditCaret.Release();
+}
+
 bool CPWL_EditCtrl::IsWndHorV() const {
   CFX_Matrix mt = GetWindowMatrix();
   return mt.Transform(CFX_PointF(1, 1)).y == mt.Transform(CFX_PointF(0, 1)).y;
diff --git a/fpdfsdk/pwl/cpwl_edit_ctrl.h b/fpdfsdk/pwl/cpwl_edit_ctrl.h
index fc649f7..1db4728 100644
--- a/fpdfsdk/pwl/cpwl_edit_ctrl.h
+++ b/fpdfsdk/pwl/cpwl_edit_ctrl.h
@@ -42,6 +42,7 @@
 
   // CPWL_Wnd:
   void OnCreated() override;
+  void OnDestroy() override;
   bool OnKeyDown(uint16_t nChar, uint32_t nFlag) override;
   bool OnChar(uint16_t nChar, uint32_t nFlag) override;
   bool OnLButtonDown(uint32_t nFlag, const CFX_PointF& point) override;
@@ -81,7 +82,7 @@
   void SetEditCaret(bool bVisible);
 
   std::unique_ptr<CPWL_EditImpl> const m_pEdit;
-  CPWL_Caret* m_pEditCaret = nullptr;
+  UnownedPtr<CPWL_Caret> m_pEditCaret;
   bool m_bMouseDown = false;
 
  private:
diff --git a/xfa/fwl/BUILD.gn b/xfa/fwl/BUILD.gn
index ac39851..dead643 100644
--- a/xfa/fwl/BUILD.gn
+++ b/xfa/fwl/BUILD.gn
@@ -68,6 +68,7 @@
     "cfwl_themebackground.h",
     "cfwl_themepart.cpp",
     "cfwl_themepart.h",
+    "cfwl_themetext.cpp",
     "cfwl_themetext.h",
     "cfwl_widget.cpp",
     "cfwl_widget.h",
diff --git a/xfa/fwl/cfwl_listbox.cpp b/xfa/fwl/cfwl_listbox.cpp
index fdb3536..9f1c32a 100644
--- a/xfa/fwl/cfwl_listbox.cpp
+++ b/xfa/fwl/cfwl_listbox.cpp
@@ -395,6 +395,7 @@
     dwPartStates |= CFWL_PartState_Focused;
   }
 
+  CFX_RectF rtFocus(rtItem);  // Must outlive |bg_param|.
   CFWL_ThemeBackground bg_param;
   bg_param.m_pWidget = this;
   bg_param.m_iPart = CFWL_Part::ListItem;
@@ -403,7 +404,6 @@
   bg_param.m_matrix.Concat(*pMatrix);
   bg_param.m_PartRect = rtItem;
   bg_param.m_bMaximize = true;
-  CFX_RectF rtFocus(rtItem);
   bg_param.m_pRtData = &rtFocus;
   if (m_pVertScrollBar && !m_pHorzScrollBar &&
       (dwPartStates & CFWL_PartState_Focused)) {
diff --git a/xfa/fwl/cfwl_message.h b/xfa/fwl/cfwl_message.h
index 35e4155..c77cc59 100644
--- a/xfa/fwl/cfwl_message.h
+++ b/xfa/fwl/cfwl_message.h
@@ -8,6 +8,7 @@
 #define XFA_FWL_CFWL_MESSAGE_H_
 
 #include "core/fxcrt/fx_system.h"
+#include "core/fxcrt/unowned_ptr.h"
 #include "v8/include/cppgc/macros.h"
 
 class CFWL_Widget;
@@ -33,8 +34,8 @@
 
  private:
   const Type m_type;
-  CFWL_Widget* m_pSrcTarget = nullptr;
-  CFWL_Widget* m_pDstTarget = nullptr;
+  UnownedPtr<CFWL_Widget> m_pSrcTarget;
+  UnownedPtr<CFWL_Widget> m_pDstTarget;
 };
 
 #endif  // XFA_FWL_CFWL_MESSAGE_H_
diff --git a/xfa/fwl/cfwl_themepart.cpp b/xfa/fwl/cfwl_themepart.cpp
index 0a98a4b..f488f2f 100644
--- a/xfa/fwl/cfwl_themepart.cpp
+++ b/xfa/fwl/cfwl_themepart.cpp
@@ -7,3 +7,5 @@
 #include "xfa/fwl/cfwl_themepart.h"
 
 CFWL_ThemePart::CFWL_ThemePart() = default;
+
+CFWL_ThemePart::~CFWL_ThemePart() = default;
diff --git a/xfa/fwl/cfwl_themepart.h b/xfa/fwl/cfwl_themepart.h
index af85e37..56ad8c8 100644
--- a/xfa/fwl/cfwl_themepart.h
+++ b/xfa/fwl/cfwl_themepart.h
@@ -9,6 +9,7 @@
 
 #include "core/fxcrt/fx_coordinates.h"
 #include "core/fxcrt/fx_system.h"
+#include "core/fxcrt/unowned_ptr.h"
 
 enum class CFWL_Part : uint8_t {
   None = 0,
@@ -79,11 +80,12 @@
 class CFWL_ThemePart {
  public:
   CFWL_ThemePart();
+  ~CFWL_ThemePart();
 
   CFX_Matrix m_matrix;
   CFX_RectF m_PartRect;
-  CFWL_Widget* m_pWidget = nullptr;
-  CFX_RectF* m_pRtData = nullptr;
+  UnownedPtr<CFWL_Widget> m_pWidget;
+  UnownedPtr<CFX_RectF> m_pRtData;
   uint32_t m_dwStates = CFWL_PartState_Normal;
   CFWL_Part m_iPart = CFWL_Part::None;
   bool m_bMaximize = false;
diff --git a/xfa/fwl/cfwl_themetext.cpp b/xfa/fwl/cfwl_themetext.cpp
new file mode 100644
index 0000000..2cd07d0
--- /dev/null
+++ b/xfa/fwl/cfwl_themetext.cpp
@@ -0,0 +1,11 @@
+// Copyright 2021 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "xfa/fwl/cfwl_themetext.h"
+
+CFWL_ThemeText::CFWL_ThemeText() = default;
+
+CFWL_ThemeText::~CFWL_ThemeText() = default;
diff --git a/xfa/fwl/cfwl_themetext.h b/xfa/fwl/cfwl_themetext.h
index cb1b770..ae924ed 100644
--- a/xfa/fwl/cfwl_themetext.h
+++ b/xfa/fwl/cfwl_themetext.h
@@ -8,17 +8,21 @@
 #define XFA_FWL_CFWL_THEMETEXT_H_
 
 #include "core/fxcrt/fx_string.h"
-#include "core/fxcrt/fx_system.h"
+#include "core/fxcrt/unowned_ptr.h"
+#include "xfa/fde/cfde_data.h"
 #include "xfa/fwl/cfwl_themepart.h"
 
+class CFGAS_GEGraphics;
+
 class CFWL_ThemeText final : public CFWL_ThemePart {
  public:
-  CFWL_ThemeText() = default;
+  CFWL_ThemeText();
+  ~CFWL_ThemeText();
 
   FDE_TextAlignment m_iTTOAlign = FDE_TextAlignment::kTopLeft;
-  CFGAS_GEGraphics* m_pGraphics = nullptr;
-  WideString m_wsText;
   FDE_TextStyle m_dwTTOStyles;
+  UnownedPtr<CFGAS_GEGraphics> m_pGraphics;
+  WideString m_wsText;
 };
 
 #endif  // XFA_FWL_CFWL_THEMETEXT_H_