Make TranslateFWLMessage() own the message it is sending.

Limits the lifetime of the message so it won't outlive widgets
that may get destroyed when we process it. This is required for
a subsequent patch, and avoids one block where this was already
being worked around via nested scopes.

-- Rename the method to the more accurate SendMessageToFWLWidget().
-- Pass better arguments to message subclass constructors.

Change-Id: Ib99a06df854ab113c91b2a74a9bccefed0e27bee
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/59811
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fwl/cfwl_messagekey.cpp b/xfa/fwl/cfwl_messagekey.cpp
index 025175a..12464fa 100644
--- a/xfa/fwl/cfwl_messagekey.cpp
+++ b/xfa/fwl/cfwl_messagekey.cpp
@@ -10,9 +10,14 @@
 
 #include "third_party/base/ptr_util.h"
 
-CFWL_MessageKey::CFWL_MessageKey(CFWL_Widget* pSrcTarget,
-                                 CFWL_Widget* pDstTarget)
-    : CFWL_Message(CFWL_Message::Type::Key, pSrcTarget, pDstTarget) {}
+CFWL_MessageKey::CFWL_MessageKey(CFWL_Widget* pDstTarget,
+                                 FWL_KeyCommand cmd,
+                                 uint32_t flags,
+                                 uint32_t keycode)
+    : CFWL_Message(CFWL_Message::Type::Key, nullptr, pDstTarget),
+      m_dwCmd(cmd),
+      m_dwFlags(flags),
+      m_dwKeyCode(keycode) {}
 
 CFWL_MessageKey::CFWL_MessageKey(const CFWL_MessageKey& that) = default;
 
diff --git a/xfa/fwl/cfwl_messagekey.h b/xfa/fwl/cfwl_messagekey.h
index 5ecb161..4b54509 100644
--- a/xfa/fwl/cfwl_messagekey.h
+++ b/xfa/fwl/cfwl_messagekey.h
@@ -15,16 +15,19 @@
 
 class CFWL_MessageKey final : public CFWL_Message {
  public:
-  CFWL_MessageKey(CFWL_Widget* pSrcTarget, CFWL_Widget* pDstTarget);
+  CFWL_MessageKey(CFWL_Widget* pDstTarget,
+                  FWL_KeyCommand cmd,
+                  uint32_t flags,
+                  uint32_t keycode);
   CFWL_MessageKey(const CFWL_MessageKey& that);
   ~CFWL_MessageKey() override;
 
   // CFWL_Message
   std::unique_ptr<CFWL_Message> Clone() override;
 
-  uint32_t m_dwKeyCode;
-  uint32_t m_dwFlags;
   FWL_KeyCommand m_dwCmd;
+  uint32_t m_dwFlags;
+  uint32_t m_dwKeyCode;
 };
 
 #endif  // XFA_FWL_CFWL_MESSAGEKEY_H_
diff --git a/xfa/fwl/cfwl_messagemouse.cpp b/xfa/fwl/cfwl_messagemouse.cpp
index 1d56b0f..fdeee99 100644
--- a/xfa/fwl/cfwl_messagemouse.cpp
+++ b/xfa/fwl/cfwl_messagemouse.cpp
@@ -10,13 +10,23 @@
 
 #include "third_party/base/ptr_util.h"
 
-CFWL_MessageMouse::CFWL_MessageMouse(CFWL_Widget* pSrcTarget,
-                                     CFWL_Widget* pDstTarget)
-    : CFWL_Message(CFWL_Message::Type::Mouse, pSrcTarget, pDstTarget) {}
+CFWL_MessageMouse::CFWL_MessageMouse(CFWL_Widget* pDstTarget,
+                                     FWL_MouseCommand cmd)
+    : CFWL_Message(CFWL_Message::Type::Mouse, nullptr, pDstTarget),
+      m_dwCmd(cmd) {}
+
+CFWL_MessageMouse::CFWL_MessageMouse(CFWL_Widget* pDstTarget,
+                                     FWL_MouseCommand cmd,
+                                     uint32_t flags,
+                                     CFX_PointF pos)
+    : CFWL_Message(CFWL_Message::Type::Mouse, nullptr, pDstTarget),
+      m_dwCmd(cmd),
+      m_dwFlags(flags),
+      m_pos(pos) {}
 
 CFWL_MessageMouse::CFWL_MessageMouse(const CFWL_MessageMouse& other) = default;
 
-CFWL_MessageMouse::~CFWL_MessageMouse() {}
+CFWL_MessageMouse::~CFWL_MessageMouse() = default;
 
 std::unique_ptr<CFWL_Message> CFWL_MessageMouse::Clone() {
   return pdfium::MakeUnique<CFWL_MessageMouse>(*this);
diff --git a/xfa/fwl/cfwl_messagemouse.h b/xfa/fwl/cfwl_messagemouse.h
index ae9711f..a38f60c 100644
--- a/xfa/fwl/cfwl_messagemouse.h
+++ b/xfa/fwl/cfwl_messagemouse.h
@@ -27,16 +27,20 @@
 
 class CFWL_MessageMouse final : public CFWL_Message {
  public:
-  CFWL_MessageMouse(CFWL_Widget* pSrcTarget, CFWL_Widget* pDstTarget);
+  CFWL_MessageMouse(CFWL_Widget* pDstTarget, FWL_MouseCommand cmd);
+  CFWL_MessageMouse(CFWL_Widget* pDstTarget,
+                    FWL_MouseCommand cmd,
+                    uint32_t flags,
+                    CFX_PointF pos);
   CFWL_MessageMouse(const CFWL_MessageMouse& other);
   ~CFWL_MessageMouse() override;
 
   // CFWL_Message
   std::unique_ptr<CFWL_Message> Clone() override;
 
-  CFX_PointF m_pos;
-  uint32_t m_dwFlags;
   FWL_MouseCommand m_dwCmd;
+  uint32_t m_dwFlags = 0;
+  CFX_PointF m_pos;
 };
 
 #endif  // XFA_FWL_CFWL_MESSAGEMOUSE_H_
diff --git a/xfa/fwl/cfwl_messagemousewheel.cpp b/xfa/fwl/cfwl_messagemousewheel.cpp
index 8996f65..fd6e69d 100644
--- a/xfa/fwl/cfwl_messagemousewheel.cpp
+++ b/xfa/fwl/cfwl_messagemousewheel.cpp
@@ -10,14 +10,19 @@
 
 #include "third_party/base/ptr_util.h"
 
-CFWL_MessageMouseWheel::CFWL_MessageMouseWheel(CFWL_Widget* pSrcTarget,
-                                               CFWL_Widget* pDstTarget)
-    : CFWL_Message(CFWL_Message::Type::MouseWheel, pSrcTarget, pDstTarget) {}
+CFWL_MessageMouseWheel::CFWL_MessageMouseWheel(CFWL_Widget* pDstTarget,
+                                               uint32_t flags,
+                                               CFX_PointF pos,
+                                               CFX_PointF delta)
+    : CFWL_Message(CFWL_Message::Type::MouseWheel, nullptr, pDstTarget),
+      m_dwFlags(flags),
+      m_pos(pos),
+      m_delta(delta) {}
 
 CFWL_MessageMouseWheel::CFWL_MessageMouseWheel(const CFWL_MessageMouseWheel&) =
     default;
 
-CFWL_MessageMouseWheel::~CFWL_MessageMouseWheel() {}
+CFWL_MessageMouseWheel::~CFWL_MessageMouseWheel() = default;
 
 std::unique_ptr<CFWL_Message> CFWL_MessageMouseWheel::Clone() {
   return pdfium::MakeUnique<CFWL_MessageMouseWheel>(*this);
diff --git a/xfa/fwl/cfwl_messagemousewheel.h b/xfa/fwl/cfwl_messagemousewheel.h
index 8fc3a63..5385458 100644
--- a/xfa/fwl/cfwl_messagemousewheel.h
+++ b/xfa/fwl/cfwl_messagemousewheel.h
@@ -14,16 +14,19 @@
 
 class CFWL_MessageMouseWheel final : public CFWL_Message {
  public:
-  CFWL_MessageMouseWheel(CFWL_Widget* pSrcTarget, CFWL_Widget* pDstTarget);
+  CFWL_MessageMouseWheel(CFWL_Widget* pDstTarget,
+                         uint32_t flags,
+                         CFX_PointF pos,
+                         CFX_PointF delta);
   CFWL_MessageMouseWheel(const CFWL_MessageMouseWheel&);
   ~CFWL_MessageMouseWheel() override;
 
   // CFWL_Message
   std::unique_ptr<CFWL_Message> Clone() override;
 
+  uint32_t m_dwFlags;
   CFX_PointF m_pos;
   CFX_PointF m_delta;
-  uint32_t m_dwFlags;
 };
 
 #endif  // XFA_FWL_CFWL_MESSAGEMOUSEWHEEL_H_
diff --git a/xfa/fwl/cfwl_notedriver.cpp b/xfa/fwl/cfwl_notedriver.cpp
index 614b71e..821dd5c 100644
--- a/xfa/fwl/cfwl_notedriver.cpp
+++ b/xfa/fwl/cfwl_notedriver.cpp
@@ -259,10 +259,9 @@
 
   CFWL_MessageMouse* pMsg = static_cast<CFWL_MessageMouse*>(pMessage);
   if (m_pHover) {
-    CFWL_MessageMouse msLeave(nullptr, m_pHover.Get());
-    msLeave.m_pos = pTarget->TransformTo(m_pHover.Get(), pMsg->m_pos);
-    msLeave.m_dwFlags = 0;
-    msLeave.m_dwCmd = FWL_MouseCommand::Leave;
+    CFWL_MessageMouse msLeave(
+        m_pHover.Get(), FWL_MouseCommand::Leave, 0,
+        pTarget->TransformTo(m_pHover.Get(), pMsg->m_pos));
     DispatchMessage(&msLeave, nullptr);
   }
   if (pTarget->GetClassID() == FWL_Type::Form) {
@@ -271,10 +270,7 @@
   }
   m_pHover = pTarget;
 
-  CFWL_MessageMouse msHover(nullptr, pTarget);
-  msHover.m_pos = pMsg->m_pos;
-  msHover.m_dwFlags = 0;
-  msHover.m_dwCmd = FWL_MouseCommand::Hover;
+  CFWL_MessageMouse msHover(pTarget, FWL_MouseCommand::Hover, 0, pMsg->m_pos);
   DispatchMessage(&msHover, nullptr);
 }
 
diff --git a/xfa/fxfa/cxfa_ffcheckbutton.cpp b/xfa/fxfa/cxfa_ffcheckbutton.cpp
index 7156ee6..d48cf33 100644
--- a/xfa/fxfa/cxfa_ffcheckbutton.cpp
+++ b/xfa/fxfa/cxfa_ffcheckbutton.cpp
@@ -250,11 +250,10 @@
     return false;
 
   SetButtonDown(false);
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::LeftButtonUp;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::LeftButtonUp, dwFlags,
+      FWLToClient(point)));
+
   return true;
 }
 
diff --git a/xfa/fxfa/cxfa_fffield.cpp b/xfa/fxfa/cxfa_fffield.cpp
index 7a6f845..10f146e 100644
--- a/xfa/fxfa/cxfa_fffield.cpp
+++ b/xfa/fxfa/cxfa_fffield.cpp
@@ -7,8 +7,10 @@
 #include "xfa/fxfa/cxfa_fffield.h"
 
 #include <algorithm>
+#include <utility>
 
 #include "core/fxge/render_defines.h"
+#include "third_party/base/ptr_util.h"
 #include "xfa/fwl/cfwl_edit.h"
 #include "xfa/fwl/cfwl_eventmouse.h"
 #include "xfa/fwl/cfwl_messagekey.h"
@@ -365,9 +367,8 @@
   if (!m_pNormalWidget)
     return false;
 
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::Enter;
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::Enter));
   return true;
 }
 
@@ -375,9 +376,8 @@
   if (!m_pNormalWidget)
     return false;
 
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::Leave;
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::Leave));
   return true;
 }
 
@@ -401,11 +401,9 @@
 
 void CXFA_FFField::OnLButtonDown(uint32_t dwFlags, const CFX_PointF& point) {
   SetButtonDown(true);
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::LeftButtonDown;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::LeftButtonDown, dwFlags,
+      FWLToClient(point)));
 }
 
 bool CXFA_FFField::OnLButtonUp(uint32_t dwFlags, const CFX_PointF& point) {
@@ -415,11 +413,9 @@
     return false;
 
   SetButtonDown(false);
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::LeftButtonUp;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::LeftButtonUp, dwFlags,
+      FWLToClient(point)));
   return true;
 }
 
@@ -427,11 +423,9 @@
   if (!m_pNormalWidget)
     return false;
 
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::LeftButtonDblClk;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::LeftButtonDblClk, dwFlags,
+      FWLToClient(point)));
   return true;
 }
 
@@ -439,11 +433,9 @@
   if (!m_pNormalWidget)
     return false;
 
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::Move;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::Move, dwFlags,
+      FWLToClient(point)));
   return true;
 }
 
@@ -453,22 +445,17 @@
   if (!m_pNormalWidget)
     return false;
 
-  CFWL_MessageMouseWheel ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  ms.m_delta = CFX_PointF(zDelta, 0);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouseWheel>(
+      m_pNormalWidget.get(), dwFlags, FWLToClient(point),
+      CFX_PointF(zDelta, 0)));
   return true;
 }
 
 void CXFA_FFField::OnRButtonDown(uint32_t dwFlags, const CFX_PointF& point) {
   SetButtonDown(true);
-
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::RightButtonDown;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::RightButtonDown, dwFlags,
+      FWLToClient(point)));
 }
 
 bool CXFA_FFField::OnRButtonUp(uint32_t dwFlags, const CFX_PointF& point) {
@@ -478,11 +465,9 @@
     return false;
 
   SetButtonDown(false);
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::RightButtonUp;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::RightButtonUp, dwFlags,
+      FWLToClient(point)));
   return true;
 }
 
@@ -490,11 +475,9 @@
   if (!m_pNormalWidget)
     return false;
 
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::RightButtonDblClk;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::RightButtonDblClk, dwFlags,
+      FWLToClient(point)));
   return true;
 }
 
@@ -505,8 +488,8 @@
   if (!m_pNormalWidget)
     return false;
 
-  CFWL_MessageSetFocus ms(nullptr, m_pNormalWidget.get());
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(
+      pdfium::MakeUnique<CFWL_MessageSetFocus>(nullptr, m_pNormalWidget.get()));
   GetLayoutItem()->SetStatusBits(XFA_WidgetStatus_Focused);
   InvalidateRect();
   return true;
@@ -514,8 +497,8 @@
 
 bool CXFA_FFField::OnKillFocus(CXFA_FFWidget* pNewWidget) {
   if (m_pNormalWidget) {
-    CFWL_MessageKillFocus ms(nullptr, m_pNormalWidget.get());
-    TranslateFWLMessage(&ms);
+    SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageKillFocus>(
+        nullptr, m_pNormalWidget.get()));
     GetLayoutItem()->ClearStatusBits(XFA_WidgetStatus_Focused);
     InvalidateRect();
   }
@@ -526,11 +509,8 @@
   if (!m_pNormalWidget || !GetDoc()->GetXFADoc()->IsInteractive())
     return false;
 
-  CFWL_MessageKey ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_KeyCommand::KeyDown;
-  ms.m_dwFlags = dwFlags;
-  ms.m_dwKeyCode = dwKeyCode;
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageKey>(
+      m_pNormalWidget.get(), FWL_KeyCommand::KeyDown, dwFlags, dwKeyCode));
   return true;
 }
 
@@ -538,11 +518,8 @@
   if (!m_pNormalWidget || !GetDoc()->GetXFADoc()->IsInteractive())
     return false;
 
-  CFWL_MessageKey ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_KeyCommand::KeyUp;
-  ms.m_dwFlags = dwFlags;
-  ms.m_dwKeyCode = dwKeyCode;
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageKey>(
+      m_pNormalWidget.get(), FWL_KeyCommand::KeyUp, dwFlags, dwKeyCode));
   return true;
 }
 
@@ -556,11 +533,8 @@
   if (!m_pNode->IsOpenAccess())
     return false;
 
-  CFWL_MessageKey ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_KeyCommand::Char;
-  ms.m_dwFlags = dwFlags;
-  ms.m_dwKeyCode = dwChar;
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageKey>(
+      m_pNormalWidget.get(), FWL_KeyCommand::Char, dwFlags, dwChar));
   return true;
 }
 
@@ -722,8 +696,9 @@
   return false;
 }
 
-void CXFA_FFField::TranslateFWLMessage(CFWL_Message* pMessage) {
-  GetApp()->GetFWLWidgetMgr()->OnProcessMessageToForm(pMessage);
+void CXFA_FFField::SendMessageToFWLWidget(
+    std::unique_ptr<CFWL_Message> pMessage) {
+  GetApp()->GetFWLWidgetMgr()->OnProcessMessageToForm(pMessage.get());
 }
 
 void CXFA_FFField::OnProcessMessage(CFWL_Message* pMessage) {}
diff --git a/xfa/fxfa/cxfa_fffield.h b/xfa/fxfa/cxfa_fffield.h
index 1e2b9db..d3ae1fb 100644
--- a/xfa/fxfa/cxfa_fffield.h
+++ b/xfa/fxfa/cxfa_fffield.h
@@ -86,7 +86,7 @@
                      HighlightOption highlight,
                      ShapeOption shape);
   void DrawFocus(CXFA_Graphics* pGS, CFX_Matrix* pMatrix);
-  void TranslateFWLMessage(CFWL_Message* pMessage);
+  void SendMessageToFWLWidget(std::unique_ptr<CFWL_Message> pMessage);
   void CapPlacement();
   void CapTopBottomPlacement(const CXFA_Margin* margin,
                              const CFX_RectF& rtWidget,
diff --git a/xfa/fxfa/cxfa_ffimageedit.cpp b/xfa/fxfa/cxfa_ffimageedit.cpp
index ae683db..0f50c6a 100644
--- a/xfa/fxfa/cxfa_ffimageedit.cpp
+++ b/xfa/fxfa/cxfa_ffimageedit.cpp
@@ -105,12 +105,9 @@
 void CXFA_FFImageEdit::OnLButtonDown(uint32_t dwFlags,
                                      const CFX_PointF& point) {
   SetButtonDown(true);
-
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::LeftButtonDown;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::LeftButtonDown, dwFlags,
+      FWLToClient(point)));
 }
 
 void CXFA_FFImageEdit::SetFWLRect() {
diff --git a/xfa/fxfa/cxfa_fftextedit.cpp b/xfa/fxfa/cxfa_fftextedit.cpp
index da52cf5..7916432 100644
--- a/xfa/fxfa/cxfa_fftextedit.cpp
+++ b/xfa/fxfa/cxfa_fftextedit.cpp
@@ -125,13 +125,10 @@
     UpdateFWLData();
     InvalidateRect();
   }
-
   SetButtonDown(true);
-  CFWL_MessageMouse ms(nullptr, m_pNormalWidget.get());
-  ms.m_dwCmd = FWL_MouseCommand::LeftButtonDown;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      m_pNormalWidget.get(), FWL_MouseCommand::LeftButtonDown, dwFlags,
+      FWLToClient(point)));
 }
 
 void CXFA_FFTextEdit::OnRButtonDown(uint32_t dwFlags, const CFX_PointF& point) {
@@ -140,13 +137,9 @@
     UpdateFWLData();
     InvalidateRect();
   }
-
   SetButtonDown(true);
-  CFWL_MessageMouse ms(nullptr, nullptr);
-  ms.m_dwCmd = FWL_MouseCommand::RightButtonDown;
-  ms.m_dwFlags = dwFlags;
-  ms.m_pos = FWLToClient(point);
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageMouse>(
+      nullptr, FWL_MouseCommand::RightButtonDown, dwFlags, FWLToClient(point)));
 }
 
 bool CXFA_FFTextEdit::OnRButtonUp(uint32_t dwFlags, const CFX_PointF& point) {
@@ -167,17 +160,16 @@
   if (!CXFA_FFWidget::OnSetFocus(pOldWidget))
     return false;
 
-  CFWL_MessageSetFocus ms(nullptr, m_pNormalWidget.get());
-  TranslateFWLMessage(&ms);
+  SendMessageToFWLWidget(
+      pdfium::MakeUnique<CFWL_MessageSetFocus>(nullptr, m_pNormalWidget.get()));
+
   return true;
 }
 
 bool CXFA_FFTextEdit::OnKillFocus(CXFA_FFWidget* pNewWidget) {
-  {
-    // Message can't outlive the OnKillFocus call.
-    CFWL_MessageKillFocus ms(nullptr, m_pNormalWidget.get());
-    TranslateFWLMessage(&ms);
-  }
+  SendMessageToFWLWidget(pdfium::MakeUnique<CFWL_MessageKillFocus>(
+      nullptr, m_pNormalWidget.get()));
+
   GetLayoutItem()->ClearStatusBits(XFA_WidgetStatus_Focused);
   SetEditScrollOffset();
   ProcessCommittedData();