Introduce CXFA_FFDocView::UpdateScope RAII helper class

Change-Id: I82c4aa5f32971d62926f8353e3680eccf98c0dc8
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/84253
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fxfa/cxfa_ffdocview.cpp b/xfa/fxfa/cxfa_ffdocview.cpp
index 65e5157..8be2f1e 100644
--- a/xfa/fxfa/cxfa_ffdocview.cpp
+++ b/xfa/fxfa/cxfa_ffdocview.cpp
@@ -60,6 +60,16 @@
     XFA_AttributeValue::Unknown,
 };
 
+CXFA_FFDocView::UpdateScope::UpdateScope(CXFA_FFDocView* pDocView)
+    : m_pDocView(pDocView) {
+  m_pDocView->LockUpdate();
+}
+
+CXFA_FFDocView::UpdateScope::~UpdateScope() {
+  m_pDocView->UnlockUpdate();
+  m_pDocView->UpdateDocView();
+}
+
 CXFA_FFDocView::CXFA_FFDocView(CXFA_FFDoc* pDoc) : m_pDoc(pDoc) {}
 
 CXFA_FFDocView::~CXFA_FFDocView() = default;
diff --git a/xfa/fxfa/cxfa_ffdocview.h b/xfa/fxfa/cxfa_ffdocview.h
index 25f032f..e23fe33 100644
--- a/xfa/fxfa/cxfa_ffdocview.h
+++ b/xfa/fxfa/cxfa_ffdocview.h
@@ -32,6 +32,15 @@
  public:
   enum class LayoutStatus : uint8_t { kNone, kStart, kDoing, kEnd };
 
+  class UpdateScope {
+   public:
+    explicit UpdateScope(CXFA_FFDocView* pDocView);
+    ~UpdateScope();
+
+   private:
+    UnownedPtr<CXFA_FFDocView> const m_pDocView;
+  };
+
   CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED;
   ~CXFA_FFDocView();
 
diff --git a/xfa/fxfa/cxfa_ffnotify.cpp b/xfa/fxfa/cxfa_ffnotify.cpp
index 4e833bd..1997f46 100644
--- a/xfa/fxfa/cxfa_ffnotify.cpp
+++ b/xfa/fxfa/cxfa_ffnotify.cpp
@@ -281,11 +281,8 @@
   if (!pComboBox)
     return;
 
-  CXFA_FFDocView* pDocView = m_pDoc->GetDocView();
-  pDocView->LockUpdate();
+  CXFA_FFDocView::UpdateScope scope(m_pDoc->GetDocView());
   pComboBox->OpenDropDownList();
-  pDocView->UnlockUpdate();
-  pDocView->UpdateDocView();
 }
 
 void CXFA_FFNotify::ResetData(CXFA_Node* pNode) {
diff --git a/xfa/fxfa/cxfa_ffwidgethandler.cpp b/xfa/fxfa/cxfa_ffwidgethandler.cpp
index 991325b..c413187 100644
--- a/xfa/fxfa/cxfa_ffwidgethandler.cpp
+++ b/xfa/fxfa/cxfa_ffwidgethandler.cpp
@@ -30,49 +30,37 @@
 }
 
 bool CXFA_FFWidgetHandler::OnMouseEnter(CXFA_FFWidget* hWidget) {
-  m_pDocView->LockUpdate();
-  bool bRet = hWidget->OnMouseEnter();
-  m_pDocView->UnlockUpdate();
-  m_pDocView->UpdateDocView();
-  return bRet;
+  CXFA_FFDocView::UpdateScope scope(m_pDocView);
+  return hWidget->OnMouseEnter();
 }
 
 bool CXFA_FFWidgetHandler::OnMouseExit(CXFA_FFWidget* hWidget) {
-  m_pDocView->LockUpdate();
-  bool bRet = hWidget->OnMouseExit();
-  m_pDocView->UnlockUpdate();
-  m_pDocView->UpdateDocView();
-  return bRet;
+  CXFA_FFDocView::UpdateScope scope(m_pDocView);
+  return hWidget->OnMouseExit();
 }
 
 bool CXFA_FFWidgetHandler::OnLButtonDown(CXFA_FFWidget* hWidget,
                                          Mask<XFA_FWL_KeyFlag> dwFlags,
                                          const CFX_PointF& point) {
-  m_pDocView->LockUpdate();
-  bool bRet = hWidget->AcceptsFocusOnButtonDown(
-      dwFlags, hWidget->Rotate2Normal(point),
-      CFWL_MessageMouse::MouseCommand::kLeftButtonDown);
-  if (bRet) {
-    // May re-enter JS.
-    if (m_pDocView->SetFocus(hWidget))
-      m_pDocView->GetDoc()->SetFocusWidget(hWidget);
-
-    bRet = hWidget->OnLButtonDown(dwFlags, hWidget->Rotate2Normal(point));
+  CXFA_FFDocView::UpdateScope scope(m_pDocView);
+  if (!hWidget->AcceptsFocusOnButtonDown(
+          dwFlags, hWidget->Rotate2Normal(point),
+          CFWL_MessageMouse::MouseCommand::kLeftButtonDown)) {
+    return false;
   }
-  m_pDocView->UnlockUpdate();
-  m_pDocView->UpdateDocView();
-  return bRet;
+  // May re-enter JS.
+  if (m_pDocView->SetFocus(hWidget))
+    m_pDocView->GetDoc()->SetFocusWidget(hWidget);
+
+  return hWidget->OnLButtonDown(dwFlags, hWidget->Rotate2Normal(point));
 }
 
 bool CXFA_FFWidgetHandler::OnLButtonUp(CXFA_FFWidget* hWidget,
                                        Mask<XFA_FWL_KeyFlag> dwFlags,
                                        const CFX_PointF& point) {
-  m_pDocView->LockUpdate();
+  CXFA_FFDocView::UpdateScope scope(m_pDocView);
   m_pDocView->SetLayoutEvent();
-  bool bRet = hWidget->OnLButtonUp(dwFlags, hWidget->Rotate2Normal(point));
-  m_pDocView->UnlockUpdate();
-  m_pDocView->UpdateDocView();
-  return bRet;
+  return hWidget->OnLButtonUp(dwFlags, hWidget->Rotate2Normal(point));
 }
 
 bool CXFA_FFWidgetHandler::OnLButtonDblClk(CXFA_FFWidget* hWidget,