Fix XFA caret blinking only while mouse moves.

Currently rect invalidations in XFA are only sent to the embedder
only when RunInvalidate() is executed.
For things which redraw on a timer, such as the caret, there was
no user event to call RunInvalidate() so the page would not redraw.
This CL changes the XFA code to send the invalidations to the
embedder immediately and expects the embedder to combine the
invalidations to limit overdraw.

Bug: chromium:828561
Change-Id: I298052fd7d0c373b029eec191cc6c74c63978348
Reviewed-on: https://pdfium-review.googlesource.com/29670
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
diff --git a/xfa/fxfa/cxfa_ffcombobox.cpp b/xfa/fxfa/cxfa_ffcombobox.cpp
index f3d4d22..268d1da 100644
--- a/xfa/fxfa/cxfa_ffcombobox.cpp
+++ b/xfa/fxfa/cxfa_ffcombobox.cpp
@@ -266,14 +266,14 @@
 void CXFA_FFComboBox::SetItemState(int32_t nIndex, bool bSelected) {
   ToComboBox(m_pNormalWidget.get())->SetCurSel(bSelected ? nIndex : -1);
   m_pNormalWidget->Update();
-  AddInvalidateRect();
+  InvalidateRect();
 }
 
 void CXFA_FFComboBox::InsertItem(const WideStringView& wsLabel,
                                  int32_t nIndex) {
   ToComboBox(m_pNormalWidget.get())->AddString(wsLabel);
   m_pNormalWidget->Update();
-  AddInvalidateRect();
+  InvalidateRect();
 }
 
 void CXFA_FFComboBox::DeleteItem(int32_t nIndex) {
@@ -283,7 +283,7 @@
     ToComboBox(m_pNormalWidget.get())->RemoveAt(nIndex);
 
   m_pNormalWidget->Update();
-  AddInvalidateRect();
+  InvalidateRect();
 }
 
 void CXFA_FFComboBox::OnTextChanged(CFWL_Widget* pWidget,
diff --git a/xfa/fxfa/cxfa_ffdocview.cpp b/xfa/fxfa/cxfa_ffdocview.cpp
index 43879af..86a2a9f 100644
--- a/xfa/fxfa/cxfa_ffdocview.cpp
+++ b/xfa/fxfa/cxfa_ffdocview.cpp
@@ -185,7 +185,6 @@
 
   m_bLayoutEvent = false;
   m_CalculateNodes.clear();
-  RunInvalidate();
   UnlockUpdate();
 }
 
@@ -442,22 +441,9 @@
   m_pDoc->GetDocEnvironment()->PageViewEvent(pFFPageView, dwEvent);
 }
 
-
-void CXFA_FFDocView::AddInvalidateRect(CXFA_FFPageView* pPageView,
-                                       const CFX_RectF& rtInvalidate) {
-  if (m_mapPageInvalidate[pPageView]) {
-    m_mapPageInvalidate[pPageView]->Union(rtInvalidate);
-    return;
-  }
-
-  m_mapPageInvalidate[pPageView] = pdfium::MakeUnique<CFX_RectF>(rtInvalidate);
-}
-
-void CXFA_FFDocView::RunInvalidate() {
-  for (const auto& pair : m_mapPageInvalidate)
-    m_pDoc->GetDocEnvironment()->InvalidateRect(pair.first, *pair.second);
-
-  m_mapPageInvalidate.clear();
+void CXFA_FFDocView::InvalidateRect(CXFA_FFPageView* pPageView,
+                                    const CFX_RectF& rtInvalidate) {
+  m_pDoc->GetDocEnvironment()->InvalidateRect(pPageView, rtInvalidate);
 }
 
 bool CXFA_FFDocView::RunLayout() {
diff --git a/xfa/fxfa/cxfa_ffdocview.h b/xfa/fxfa/cxfa_ffdocview.h
index bccbe33..dbc7672 100644
--- a/xfa/fxfa/cxfa_ffdocview.h
+++ b/xfa/fxfa/cxfa_ffdocview.h
@@ -68,9 +68,8 @@
   void OnPageEvent(CXFA_ContainerLayoutItem* pSender, uint32_t dwEvent);
   void LockUpdate() { m_iLock++; }
   void UnlockUpdate() { m_iLock--; }
-  void AddInvalidateRect(CXFA_FFPageView* pPageView,
-                         const CFX_RectF& rtInvalidate);
-  void RunInvalidate();
+  void InvalidateRect(CXFA_FFPageView* pPageView,
+                      const CFX_RectF& rtInvalidate);
   void RunDocClose();
 
   void ProcessValueChanged(CXFA_Node* node);
@@ -120,7 +119,6 @@
   UnownedPtr<CXFA_Node> m_pFocusNode;
   UnownedPtr<CXFA_FFWidget> m_pFocusWidget;
   UnownedPtr<CXFA_FFWidget> m_pOldFocusWidget;
-  std::map<CXFA_FFPageView*, std::unique_ptr<CFX_RectF>> m_mapPageInvalidate;
   std::vector<CXFA_Node*> m_ValidateNodes;
   std::vector<CXFA_Node*> m_CalculateNodes;
   std::vector<CXFA_BindItems*> m_BindItems;
diff --git a/xfa/fxfa/cxfa_fffield.cpp b/xfa/fxfa/cxfa_fffield.cpp
index 39c117b..552a4e3 100644
--- a/xfa/fxfa/cxfa_fffield.cpp
+++ b/xfa/fxfa/cxfa_fffield.cpp
@@ -505,7 +505,7 @@
   CFWL_MessageSetFocus ms(nullptr, m_pNormalWidget.get());
   TranslateFWLMessage(&ms);
   m_dwStatus |= XFA_WidgetStatus_Focused;
-  AddInvalidateRect();
+  InvalidateRect();
   return true;
 }
 
@@ -516,7 +516,7 @@
   CFWL_MessageKillFocus ms(nullptr, m_pNormalWidget.get());
   TranslateFWLMessage(&ms);
   m_dwStatus &= ~XFA_WidgetStatus_Focused;
-  AddInvalidateRect();
+  InvalidateRect();
   CXFA_FFWidget::OnKillFocus(pNewWidget);
   return true;
 }
diff --git a/xfa/fxfa/cxfa_fflistbox.cpp b/xfa/fxfa/cxfa_fflistbox.cpp
index 92543e3..50f9d4a 100644
--- a/xfa/fxfa/cxfa_fflistbox.cpp
+++ b/xfa/fxfa/cxfa_fflistbox.cpp
@@ -167,14 +167,14 @@
   auto* pListBox = ToListBox(m_pNormalWidget.get());
   pListBox->SetSelItem(pListBox->GetSelItem(nIndex), bSelected);
   m_pNormalWidget->Update();
-  AddInvalidateRect();
+  InvalidateRect();
 }
 
 void CXFA_FFListBox::InsertItem(const WideStringView& wsLabel, int32_t nIndex) {
   WideString wsTemp(wsLabel);
   ToListBox(m_pNormalWidget.get())->AddString(wsTemp.AsStringView());
   m_pNormalWidget->Update();
-  AddInvalidateRect();
+  InvalidateRect();
 }
 
 void CXFA_FFListBox::DeleteItem(int32_t nIndex) {
@@ -185,7 +185,7 @@
     pListBox->DeleteString(pListBox->GetItem(nullptr, nIndex));
 
   pListBox->Update();
-  AddInvalidateRect();
+  InvalidateRect();
 }
 
 void CXFA_FFListBox::OnProcessMessage(CFWL_Message* pMessage) {
diff --git a/xfa/fxfa/cxfa_ffnotify.cpp b/xfa/fxfa/cxfa_ffnotify.cpp
index 5ba699b..5aca631 100644
--- a/xfa/fxfa/cxfa_ffnotify.cpp
+++ b/xfa/fxfa/cxfa_ffnotify.cpp
@@ -333,7 +333,7 @@
   CXFA_FFWidget* pWidget = m_pDoc->GetDocView()->GetWidgetForNode(pSender);
   for (; pWidget; pWidget = pSender->GetNextWidget(pWidget)) {
     if (pWidget->IsLoaded())
-      pWidget->AddInvalidateRect();
+      pWidget->InvalidateRect();
   }
 }
 
@@ -397,7 +397,7 @@
     if (bUpdateProperty)
       pWidget->UpdateWidgetProperty();
     pWidget->PerformLayout();
-    pWidget->AddInvalidateRect();
+    pWidget->InvalidateRect();
   }
 }
 
@@ -461,7 +461,7 @@
   } else {
     pWidget->LoadWidget();
   }
-  pWidget->AddInvalidateRect();
+  pWidget->InvalidateRect();
 }
 
 void CXFA_FFNotify::OnLayoutItemRemoving(CXFA_LayoutProcessor* pLayout,
@@ -476,5 +476,5 @@
 
   pDocView->DeleteLayoutItem(pWidget);
   m_pDoc->GetDocEnvironment()->WidgetPreRemove(pWidget);
-  pWidget->AddInvalidateRect();
+  pWidget->InvalidateRect();
 }
diff --git a/xfa/fxfa/cxfa_fftextedit.cpp b/xfa/fxfa/cxfa_fftextedit.cpp
index d91e7f7..51ee147 100644
--- a/xfa/fxfa/cxfa_fftextedit.cpp
+++ b/xfa/fxfa/cxfa_fftextedit.cpp
@@ -114,7 +114,7 @@
   if (!IsFocused()) {
     m_dwStatus |= XFA_WidgetStatus_Focused;
     UpdateFWLData();
-    AddInvalidateRect();
+    InvalidateRect();
   }
 
   SetButtonDown(true);
@@ -134,7 +134,7 @@
   if (!IsFocused()) {
     m_dwStatus |= XFA_WidgetStatus_Focused;
     UpdateFWLData();
-    AddInvalidateRect();
+    InvalidateRect();
   }
 
   SetButtonDown(true);
@@ -159,7 +159,7 @@
   if (!IsFocused()) {
     m_dwStatus |= XFA_WidgetStatus_Focused;
     UpdateFWLData();
-    AddInvalidateRect();
+    InvalidateRect();
   }
   CXFA_FFWidget::OnSetFocus(pOldWidget);
   CFWL_MessageSetFocus ms(nullptr, m_pNormalWidget.get());
@@ -175,7 +175,7 @@
   SetEditScrollOffset();
   ProcessCommittedData();
   UpdateFWLData();
-  AddInvalidateRect();
+  InvalidateRect();
   CXFA_FFWidget::OnKillFocus(pNewWidget);
 
   m_dwStatus &= ~XFA_WidgetStatus_TextEditValueChanged;
diff --git a/xfa/fxfa/cxfa_ffwidget.cpp b/xfa/fxfa/cxfa_ffwidget.cpp
index c3e0d4d..f16e1ea 100644
--- a/xfa/fxfa/cxfa_ffwidget.cpp
+++ b/xfa/fxfa/cxfa_ffwidget.cpp
@@ -346,10 +346,10 @@
     box->Draw(pGS, rtBorder, matrix, forceRound);
 }
 
-void CXFA_FFWidget::AddInvalidateRect() {
+void CXFA_FFWidget::InvalidateRect() {
   CFX_RectF rtWidget = GetBBox(XFA_WidgetStatus_Focused);
   rtWidget.Inflate(2, 2);
-  m_pDocView->AddInvalidateRect(m_pPageView, rtWidget);
+  m_pDocView->InvalidateRect(m_pPageView, rtWidget);
 }
 
 bool CXFA_FFWidget::OnMouseEnter() {
diff --git a/xfa/fxfa/cxfa_ffwidget.h b/xfa/fxfa/cxfa_ffwidget.h
index 24f236e..2b72aa5 100644
--- a/xfa/fxfa/cxfa_ffwidget.h
+++ b/xfa/fxfa/cxfa_ffwidget.h
@@ -157,7 +157,7 @@
   CXFA_FFDoc* GetDoc();
   CXFA_FFApp* GetApp();
   IXFA_AppProvider* GetAppProvider();
-  void AddInvalidateRect();
+  void InvalidateRect();
   bool IsFocused() const { return !!(m_dwStatus & XFA_WidgetStatus_Focused); }
   CFX_PointF Rotate2Normal(const CFX_PointF& point);
   CFX_Matrix GetRotateMatrix();
diff --git a/xfa/fxfa/cxfa_ffwidgethandler.cpp b/xfa/fxfa/cxfa_ffwidgethandler.cpp
index 87d7a14..566923e 100644
--- a/xfa/fxfa/cxfa_ffwidgethandler.cpp
+++ b/xfa/fxfa/cxfa_ffwidgethandler.cpp
@@ -72,7 +72,6 @@
                                            uint32_t dwFlags,
                                            const CFX_PointF& point) {
   bool bRet = hWidget->OnLButtonDblClk(dwFlags, hWidget->Rotate2Normal(point));
-  m_pDocView->RunInvalidate();
   return bRet;
 }
 
@@ -80,7 +79,6 @@
                                        uint32_t dwFlags,
                                        const CFX_PointF& point) {
   bool bRet = hWidget->OnMouseMove(dwFlags, hWidget->Rotate2Normal(point));
-  m_pDocView->RunInvalidate();
   return bRet;
 }
 
@@ -90,7 +88,6 @@
                                         const CFX_PointF& point) {
   bool bRet =
       hWidget->OnMouseWheel(dwFlags, zDelta, hWidget->Rotate2Normal(point));
-  m_pDocView->RunInvalidate();
   return bRet;
 }
 
@@ -102,7 +99,6 @@
     m_pDocView->GetDoc()->GetDocEnvironment()->SetFocusWidget(
         m_pDocView->GetDoc(), hWidget);
   }
-  m_pDocView->RunInvalidate();
   return bRet;
 }
 
@@ -110,7 +106,6 @@
                                        uint32_t dwFlags,
                                        const CFX_PointF& point) {
   bool bRet = hWidget->OnRButtonUp(dwFlags, hWidget->Rotate2Normal(point));
-  m_pDocView->RunInvalidate();
   return bRet;
 }
 
@@ -118,7 +113,6 @@
                                            uint32_t dwFlags,
                                            const CFX_PointF& point) {
   bool bRet = hWidget->OnRButtonDblClk(dwFlags, hWidget->Rotate2Normal(point));
-  m_pDocView->RunInvalidate();
   return bRet;
 }
 
@@ -126,7 +120,6 @@
                                      uint32_t dwKeyCode,
                                      uint32_t dwFlags) {
   bool bRet = hWidget->OnKeyDown(dwKeyCode, dwFlags);
-  m_pDocView->RunInvalidate();
   m_pDocView->UpdateDocView();
   return bRet;
 }
@@ -135,7 +128,6 @@
                                    uint32_t dwKeyCode,
                                    uint32_t dwFlags) {
   bool bRet = hWidget->OnKeyUp(dwKeyCode, dwFlags);
-  m_pDocView->RunInvalidate();
   return bRet;
 }
 
@@ -143,7 +135,6 @@
                                   uint32_t dwChar,
                                   uint32_t dwFlags) {
   bool bRet = hWidget->OnChar(dwChar, dwFlags);
-  m_pDocView->RunInvalidate();
   return bRet;
 }
 
diff --git a/xfa/fxfa/cxfa_fwladapterwidgetmgr.cpp b/xfa/fxfa/cxfa_fwladapterwidgetmgr.cpp
index 6fdd553..befa830 100644
--- a/xfa/fxfa/cxfa_fwladapterwidgetmgr.cpp
+++ b/xfa/fxfa/cxfa_fwladapterwidgetmgr.cpp
@@ -21,7 +21,7 @@
   if (!pFFWidget)
     return;
 
-  pFFWidget->AddInvalidateRect();
+  pFFWidget->InvalidateRect();
 }
 
 bool CXFA_FWLAdapterWidgetMgr::GetPopupPos(CFWL_Widget* pWidget,
diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp
index 505de1c..b2535ae 100644
--- a/xfa/fxfa/parser/cxfa_node.cpp
+++ b/xfa/fxfa/parser/cxfa_node.cpp
@@ -2680,7 +2680,7 @@
       continue;
     }
     pWidget->UpdateFWLData();
-    pWidget->AddInvalidateRect();
+    pWidget->InvalidateRect();
   }
 }