Split CFFL_InteractiveFormFiller::GetFormFiller() in two.

Remove the bool parameter and make GetFormFiller() purely a getter. Move
the create functionality into GetOrCreateFormFiller().

Change-Id: I6b7b9da734ee68027284e42baf001924a520ded7
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/51872
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
index bee7bcd..170efcd 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
@@ -42,7 +42,7 @@
 
 FX_RECT CFFL_InteractiveFormFiller::GetViewBBox(CPDFSDK_PageView* pPageView,
                                                 CPDFSDK_Annot* pAnnot) {
-  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false))
+  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot))
     return pFormFiller->GetViewBBox(pPageView);
 
   ASSERT(pPageView);
@@ -65,7 +65,7 @@
   if (!IsVisible(pWidget))
     return;
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot);
   if (pFormFiller && pFormFiller->IsValid()) {
     pFormFiller->OnDraw(pPageView, pAnnot, pDevice, mtUser2Device);
     pAnnot->GetPDFPage();
@@ -135,14 +135,14 @@
         return;
 
       if (pWidget->IsAppModified()) {
-        if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, false)) {
+        if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget)) {
           pFormFiller->ResetPDFWindow(pPageView,
                                       pWidget->GetValueAge() == nValueAge);
         }
       }
     }
   }
-  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), true))
+  if (CFFL_FormFiller* pFormFiller = GetOrCreateFormFiller(pAnnot->Get()))
     pFormFiller->OnMouseEnter(pPageView);
 }
 
@@ -168,14 +168,14 @@
         return;
 
       if (pWidget->IsAppModified()) {
-        if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, false)) {
+        if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget)) {
           pFormFiller->ResetPDFWindow(pPageView,
                                       nValueAge == pWidget->GetValueAge());
         }
       }
     }
   }
-  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), false))
+  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get()))
     pFormFiller->OnMouseExit(pPageView);
 }
 
@@ -207,14 +207,14 @@
         return true;
 
       if (pWidget->IsAppModified()) {
-        if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, false)) {
+        if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget)) {
           pFormFiller->ResetPDFWindow(pPageView,
                                       nValueAge == pWidget->GetValueAge());
         }
       }
     }
   }
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get());
   return pFormFiller &&
          pFormFiller->OnLButtonDown(pPageView, pAnnot->Get(), nFlags, point);
 }
@@ -243,7 +243,7 @@
   if (bSetFocus)
     m_pFormFillEnv->SetFocusAnnot(pAnnot);
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get());
   bool bRet = pFormFiller &&
               pFormFiller->OnLButtonUp(pPageView, pAnnot->Get(), nFlags, point);
   if (m_pFormFillEnv->GetFocusAnnot() != pAnnot->Get())
@@ -283,7 +283,7 @@
   if (nAge == pWidget->GetAppearanceAge())
     return false;
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget);
   if (pFormFiller)
     pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
   return true;
@@ -295,7 +295,7 @@
     bool selected) {
   ASSERT((*pAnnot)->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get());
   return pFormFiller && pFormFiller->SetIndexSelected(index, selected);
 }
 
@@ -304,7 +304,7 @@
     int index) {
   ASSERT((*pAnnot)->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get());
   return pFormFiller && pFormFiller->IsIndexSelected(index);
 }
 
@@ -314,7 +314,7 @@
     uint32_t nFlags,
     const CFX_PointF& point) {
   ASSERT((*pAnnot)->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get());
   return pFormFiller && pFormFiller->OnLButtonDblClk(pPageView, nFlags, point);
 }
 
@@ -323,7 +323,7 @@
                                              uint32_t nFlags,
                                              const CFX_PointF& point) {
   ASSERT((*pAnnot)->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), true);
+  CFFL_FormFiller* pFormFiller = GetOrCreateFormFiller(pAnnot->Get());
   return pFormFiller && pFormFiller->OnMouseMove(pPageView, nFlags, point);
 }
 
@@ -334,7 +334,7 @@
     short zDelta,
     const CFX_PointF& point) {
   ASSERT((*pAnnot)->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get());
   return pFormFiller &&
          pFormFiller->OnMouseWheel(pPageView, nFlags, zDelta, point);
 }
@@ -345,7 +345,7 @@
     uint32_t nFlags,
     const CFX_PointF& point) {
   ASSERT((*pAnnot)->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get());
   return pFormFiller && pFormFiller->OnRButtonDown(pPageView, nFlags, point);
 }
 
@@ -354,7 +354,7 @@
                                              uint32_t nFlags,
                                              const CFX_PointF& point) {
   ASSERT((*pAnnot)->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get());
   return pFormFiller && pFormFiller->OnRButtonUp(pPageView, nFlags, point);
 }
 
@@ -363,7 +363,7 @@
                                            uint32_t nFlags) {
   ASSERT(pAnnot->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot);
   return pFormFiller && pFormFiller->OnKeyDown(nKeyCode, nFlags);
 }
 
@@ -374,7 +374,7 @@
   if (nChar == FWL_VKEY_Tab)
     return true;
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot);
   return pFormFiller && pFormFiller->OnChar(pAnnot, nChar, nFlags);
 }
 
@@ -392,7 +392,7 @@
       uint32_t nValueAge = pWidget->GetValueAge();
       pWidget->ClearAppModified();
 
-      CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, true);
+      CFFL_FormFiller* pFormFiller = GetOrCreateFormFiller(pWidget);
       if (!pFormFiller)
         return false;
 
@@ -409,7 +409,7 @@
         return false;
 
       if (pWidget->IsAppModified()) {
-        if (CFFL_FormFiller* pFiller = GetFormFiller(pWidget, false)) {
+        if (CFFL_FormFiller* pFiller = GetFormFiller(pWidget)) {
           pFiller->ResetPDFWindow(pPageView,
                                   nValueAge == pWidget->GetValueAge());
         }
@@ -417,7 +417,7 @@
     }
   }
 
-  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), true))
+  if (CFFL_FormFiller* pFormFiller = GetOrCreateFormFiller(pAnnot->Get()))
     pFormFiller->SetFocusForAnnot(pAnnot->Get(), nFlag);
 
   return true;
@@ -429,7 +429,7 @@
     return false;
 
   ASSERT((*pAnnot)->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get(), false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot->Get());
   if (!pFormFiller)
     return true;
 
@@ -480,19 +480,20 @@
 }
 
 CFFL_FormFiller* CFFL_InteractiveFormFiller::GetFormFiller(
-    CPDFSDK_Annot* pAnnot,
-    bool bRegister) {
+    CPDFSDK_Annot* pAnnot) {
   auto it = m_Map.find(pAnnot);
-  if (it != m_Map.end())
-    return it->second.get();
+  return it != m_Map.end() ? it->second.get() : nullptr;
+}
 
-  if (!bRegister)
-    return nullptr;
+CFFL_FormFiller* CFFL_InteractiveFormFiller::GetOrCreateFormFiller(
+    CPDFSDK_Annot* pAnnot) {
+  CFFL_FormFiller* result = GetFormFiller(pAnnot);
+  if (result)
+    return result;
 
   CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot);
-  FormFieldType fieldType = pWidget->GetFieldType();
   std::unique_ptr<CFFL_FormFiller> pFormFiller;
-  switch (fieldType) {
+  switch (pWidget->GetFieldType()) {
     case FormFieldType::kPushButton:
       pFormFiller =
           pdfium::MakeUnique<CFFL_PushButton>(m_pFormFillEnv.Get(), pWidget);
@@ -519,33 +520,30 @@
       break;
     case FormFieldType::kUnknown:
     default:
-      break;
+      return nullptr;
   }
 
-  if (!pFormFiller)
-    return nullptr;
-
-  CFFL_FormFiller* result = pFormFiller.get();
+  result = pFormFiller.get();
   m_Map[pAnnot] = std::move(pFormFiller);
   return result;
 }
 
 WideString CFFL_InteractiveFormFiller::GetText(CPDFSDK_Annot* pAnnot) {
   ASSERT(pAnnot->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot);
   return pFormFiller ? pFormFiller->GetText() : WideString();
 }
 
 WideString CFFL_InteractiveFormFiller::GetSelectedText(CPDFSDK_Annot* pAnnot) {
   ASSERT(pAnnot->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot);
   return pFormFiller ? pFormFiller->GetSelectedText() : WideString();
 }
 
 void CFFL_InteractiveFormFiller::ReplaceSelection(CPDFSDK_Annot* pAnnot,
                                                   const WideString& text) {
   ASSERT(pAnnot->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot);
   if (!pFormFiller)
     return;
 
@@ -554,25 +552,25 @@
 
 bool CFFL_InteractiveFormFiller::CanUndo(CPDFSDK_Annot* pAnnot) {
   ASSERT(pAnnot->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot);
   return pFormFiller && pFormFiller->CanUndo();
 }
 
 bool CFFL_InteractiveFormFiller::CanRedo(CPDFSDK_Annot* pAnnot) {
   ASSERT(pAnnot->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot);
   return pFormFiller && pFormFiller->CanRedo();
 }
 
 bool CFFL_InteractiveFormFiller::Undo(CPDFSDK_Annot* pAnnot) {
   ASSERT(pAnnot->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot);
   return pFormFiller && pFormFiller->Undo();
 }
 
 bool CFFL_InteractiveFormFiller::Redo(CPDFSDK_Annot* pAnnot) {
   ASSERT(pAnnot->GetPDFAnnot()->GetSubtype() == CPDF_Annot::Subtype::WIDGET);
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pAnnot);
   return pFormFiller && pFormFiller->Redo();
 }
 
@@ -667,7 +665,7 @@
   fa.bKeyDown = true;
   fa.bRC = true;
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget);
   pFormFiller->GetActionData(pPageView, CPDF_AAction::kKeyStroke, fa);
   pFormFiller->SaveState(pPageView);
   pWidget->OnAAction(CPDF_AAction::kKeyStroke, &fa, pPageView);
@@ -698,7 +696,7 @@
   fa.bKeyDown = true;
   fa.bRC = true;
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget);
   pFormFiller->GetActionData(pPageView, CPDF_AAction::kValidate, fa);
   pFormFiller->SaveState(pPageView);
   pWidget->OnAAction(CPDF_AAction::kValidate, &fa, pPageView);
@@ -773,7 +771,7 @@
   if (nAge == pWidget->GetAppearanceAge())
     return false;
 
-  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, false))
+  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget))
     pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
   return false;
 }
@@ -803,7 +801,7 @@
   if (nAge == pWidget->GetAppearanceAge())
     return false;
 
-  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, false))
+  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget))
     pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
 
   return true;
@@ -854,7 +852,7 @@
   if (nAge == pWidget->GetAppearanceAge())
     return false;
 
-  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, false))
+  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget))
     pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
 
   return true;
@@ -885,7 +883,7 @@
   if (nAge == pWidget->GetAppearanceAge())
     return false;
 
-  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget, false))
+  if (CFFL_FormFiller* pFormFiller = GetFormFiller(pWidget))
     pFormFiller->ResetPDFWindow(pPageView, nValueAge == pWidget->GetValueAge());
 
   return true;
@@ -910,7 +908,7 @@
       *static_cast<const CFFL_PrivateData*>(pAttached);
   ASSERT(privateData.pWidget);
 
-  CFFL_FormFiller* pFormFiller = GetFormFiller(privateData.pWidget, false);
+  CFFL_FormFiller* pFormFiller = GetFormFiller(privateData.pWidget);
 
 #ifdef PDF_ENABLE_XFA
   if (pFormFiller->IsFieldFull(privateData.pPageView)) {
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.h b/fpdfsdk/formfiller/cffl_interactiveformfiller.h
index 2a01418..11704be 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.h
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.h
@@ -81,7 +81,7 @@
   bool OnKillFocus(CPDFSDK_Annot::ObservedPtr* pAnnot, uint32_t nFlag);
 
   CFFL_FormFiller* GetFormFillerForTesting(CPDFSDK_Annot* pAnnot) {
-    return GetFormFiller(pAnnot, false);
+    return GetFormFiller(pAnnot);
   }
 
   WideString GetText(CPDFSDK_Annot* pAnnot);
@@ -161,7 +161,8 @@
   void SetFocusAnnotTab(CPDFSDK_Annot* pWidget, bool bSameField, bool bNext);
 #endif  // PDF_ENABLE_XFA
 
-  CFFL_FormFiller* GetFormFiller(CPDFSDK_Annot* pAnnot, bool bRegister);
+  CFFL_FormFiller* GetFormFiller(CPDFSDK_Annot* pAnnot);
+  CFFL_FormFiller* GetOrCreateFormFiller(CPDFSDK_Annot* pAnnot);
   void UnRegisterFormFiller(CPDFSDK_Annot* pAnnot);
 
   UnownedPtr<CPDFSDK_FormFillEnvironment> const m_pFormFillEnv;