Change some CFFL classes to use early returns.

Also switch C-style casts to the appropriate C++ casts, and add helper
functions in some cases.

Change-Id: I73f1ab36c6c89ced9d2b7b98393805142661dcac
Reviewed-on: https://pdfium-review.googlesource.com/6650
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
diff --git a/fpdfsdk/formfiller/cffl_checkbox.cpp b/fpdfsdk/formfiller/cffl_checkbox.cpp
index a81458e..a89e76a 100644
--- a/fpdfsdk/formfiller/cffl_checkbox.cpp
+++ b/fpdfsdk/formfiller/cffl_checkbox.cpp
@@ -58,7 +58,8 @@
         return true;
 
       CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
-      if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, true))
+      CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true);
+      if (pWnd)
         pWnd->SetCheck(!pWnd->IsChecked());
 
       return CommitData(pPageView, nFlags);
@@ -74,40 +75,46 @@
                                 const CFX_PointF& point) {
   CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
 
-  if (IsValid()) {
-    if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, true)) {
-      CPDFSDK_Widget* pWidget = (CPDFSDK_Widget*)pAnnot;
-      pWnd->SetCheck(!pWidget->IsChecked());
-    }
+  if (!IsValid())
+    return true;
 
-    return CommitData(pPageView, nFlags);
+  CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true);
+  if (pWnd) {
+    CPDFSDK_Widget* pWidget = static_cast<CPDFSDK_Widget*>(pAnnot);
+    pWnd->SetCheck(!pWidget->IsChecked());
   }
 
-  return true;
+  return CommitData(pPageView, nFlags);
 }
 
 bool CFFL_CheckBox::IsDataChanged(CPDFSDK_PageView* pPageView) {
-  CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, false);
+  CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false);
   return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
 }
 
 void CFFL_CheckBox::SaveData(CPDFSDK_PageView* pPageView) {
-  if (CPWL_CheckBox* pWnd = (CPWL_CheckBox*)GetPDFWindow(pPageView, false)) {
-    bool bNewChecked = pWnd->IsChecked();
+  CPWL_CheckBox* pWnd = GetCheckBox(pPageView, false);
+  if (!pWnd)
+    return;
 
-    if (bNewChecked) {
-      CPDF_FormField* pField = m_pWidget->GetFormField();
-      for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
-        if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
-          if (pCtrl->IsChecked()) {
-            break;
-          }
+  bool bNewChecked = pWnd->IsChecked();
+  if (bNewChecked) {
+    CPDF_FormField* pField = m_pWidget->GetFormField();
+    for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
+      if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
+        if (pCtrl->IsChecked()) {
+          break;
         }
       }
     }
-
-    m_pWidget->SetCheck(bNewChecked, false);
-    m_pWidget->UpdateField();
-    SetChangeMark();
   }
+
+  m_pWidget->SetCheck(bNewChecked, false);
+  m_pWidget->UpdateField();
+  SetChangeMark();
+}
+
+CPWL_CheckBox* CFFL_CheckBox::GetCheckBox(CPDFSDK_PageView* pPageView,
+                                          bool bNew) {
+  return static_cast<CPWL_CheckBox*>(GetPDFWindow(pPageView, bNew));
 }
diff --git a/fpdfsdk/formfiller/cffl_checkbox.h b/fpdfsdk/formfiller/cffl_checkbox.h
index dd3184f..f6c7172 100644
--- a/fpdfsdk/formfiller/cffl_checkbox.h
+++ b/fpdfsdk/formfiller/cffl_checkbox.h
@@ -9,6 +9,8 @@
 
 #include "fpdfsdk/formfiller/cffl_formfiller.h"
 
+class CPWL_CheckBox;
+
 class CFFL_CheckBox : public CFFL_Button {
  public:
   CFFL_CheckBox(CPDFSDK_FormFillEnvironment* pApp, CPDFSDK_Widget* pWidget);
@@ -26,6 +28,9 @@
                    const CFX_PointF& point) override;
   bool IsDataChanged(CPDFSDK_PageView* pPageView) override;
   void SaveData(CPDFSDK_PageView* pPageView) override;
+
+ private:
+  CPWL_CheckBox* GetCheckBox(CPDFSDK_PageView* pPageView, bool bNew);
 };
 
 #endif  // FPDFSDK_FORMFILLER_CFFL_CHECKBOX_H_
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
index dca3194..7647fb1 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
@@ -917,67 +917,63 @@
   }
 #endif  // PDF_ENABLE_XFA
 
-  if (!m_bNotifying) {
-    if (pData->pWidget->GetAAction(CPDF_AAction::KeyStroke).GetDict()) {
-      m_bNotifying = true;
-      int nAge = pData->pWidget->GetAppearanceAge();
-      int nValueAge = pData->pWidget->GetValueAge();
+  if (m_bNotifying)
+    return;
 
-      CPDFSDK_FormFillEnvironment* pFormFillEnv =
-          pData->pPageView->GetFormFillEnv();
+  if (!pData->pWidget->GetAAction(CPDF_AAction::KeyStroke).GetDict())
+    return;
 
-      PDFSDK_FieldAction fa;
-      fa.bModifier = m_pFormFillEnv->IsCTRLKeyDown(nFlag);
-      fa.bShift = m_pFormFillEnv->IsSHIFTKeyDown(nFlag);
-      fa.sChange = strChange;
-      fa.sChangeEx = strChangeEx;
-      fa.bKeyDown = bKeyDown;
-      fa.bWillCommit = false;
-      fa.bRC = true;
-      fa.nSelStart = nSelStart;
-      fa.nSelEnd = nSelEnd;
+  CFX_AutoRestorer<bool> restorer(&m_bNotifying);
+  m_bNotifying = true;
+  int nAge = pData->pWidget->GetAppearanceAge();
+  int nValueAge = pData->pWidget->GetValueAge();
 
-      pFormFiller->GetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa);
-      pFormFiller->SaveState(pData->pPageView);
+  CPDFSDK_FormFillEnvironment* pFormFillEnv =
+      pData->pPageView->GetFormFillEnv();
 
-      CPDFSDK_Annot::ObservedPtr pObserved(pData->pWidget);
-      if (pData->pWidget->OnAAction(CPDF_AAction::KeyStroke, fa,
-                                    pData->pPageView)) {
-        if (!pObserved || !IsValidAnnot(pData->pPageView, pData->pWidget)) {
-          bExit = true;
-          m_bNotifying = false;
-          return;
-        }
+  PDFSDK_FieldAction fa;
+  fa.bModifier = m_pFormFillEnv->IsCTRLKeyDown(nFlag);
+  fa.bShift = m_pFormFillEnv->IsSHIFTKeyDown(nFlag);
+  fa.sChange = strChange;
+  fa.sChangeEx = strChangeEx;
+  fa.bKeyDown = bKeyDown;
+  fa.bWillCommit = false;
+  fa.bRC = true;
+  fa.nSelStart = nSelStart;
+  fa.nSelEnd = nSelEnd;
 
-        if (nAge != pData->pWidget->GetAppearanceAge()) {
-          CPWL_Wnd* pWnd = pFormFiller->ResetPDFWindow(
-              pData->pPageView, nValueAge == pData->pWidget->GetValueAge());
-          pData = reinterpret_cast<CFFL_PrivateData*>(pWnd->GetAttachedData());
-          bExit = true;
-        }
+  pFormFiller->GetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa);
+  pFormFiller->SaveState(pData->pPageView);
 
-        if (fa.bRC) {
-          pFormFiller->SetActionData(pData->pPageView, CPDF_AAction::KeyStroke,
-                                     fa);
-          bRC = false;
-        } else {
-          pFormFiller->RestoreState(pData->pPageView);
-          bRC = false;
-        }
-
-        if (pFormFillEnv->GetFocusAnnot() != pData->pWidget) {
-          pFormFiller->CommitData(pData->pPageView, nFlag);
-          bExit = true;
-        }
-      } else {
-        if (!IsValidAnnot(pData->pPageView, pData->pWidget)) {
-          bExit = true;
-          m_bNotifying = false;
-          return;
-        }
-      }
-
-      m_bNotifying = false;
-    }
+  CPDFSDK_Annot::ObservedPtr pObserved(pData->pWidget);
+  if (!pData->pWidget->OnAAction(CPDF_AAction::KeyStroke, fa,
+                                 pData->pPageView)) {
+    if (!IsValidAnnot(pData->pPageView, pData->pWidget))
+      bExit = true;
+    return;
   }
+
+  if (!pObserved || !IsValidAnnot(pData->pPageView, pData->pWidget)) {
+    bExit = true;
+    return;
+  }
+
+  if (nAge != pData->pWidget->GetAppearanceAge()) {
+    CPWL_Wnd* pWnd = pFormFiller->ResetPDFWindow(
+        pData->pPageView, nValueAge == pData->pWidget->GetValueAge());
+    pData = reinterpret_cast<CFFL_PrivateData*>(pWnd->GetAttachedData());
+    bExit = true;
+  }
+
+  if (fa.bRC)
+    pFormFiller->SetActionData(pData->pPageView, CPDF_AAction::KeyStroke, fa);
+  else
+    pFormFiller->RestoreState(pData->pPageView);
+  bRC = false;
+
+  if (pFormFillEnv->GetFocusAnnot() == pData->pWidget)
+    return;
+
+  pFormFiller->CommitData(pData->pPageView, nFlag);
+  bExit = true;
 }
diff --git a/fpdfsdk/formfiller/cffl_radiobutton.cpp b/fpdfsdk/formfiller/cffl_radiobutton.cpp
index c6ce432..9ffdf49 100644
--- a/fpdfsdk/formfiller/cffl_radiobutton.cpp
+++ b/fpdfsdk/formfiller/cffl_radiobutton.cpp
@@ -57,8 +57,8 @@
         return true;
 
       CFFL_FormFiller::OnChar(pAnnot, nChar, nFlags);
-      if (CPWL_RadioButton* pWnd =
-              (CPWL_RadioButton*)GetPDFWindow(pPageView, true))
+      CPWL_RadioButton* pWnd = GetRadioButton(pPageView, true);
+      if (pWnd)
         pWnd->SetCheck(true);
       return CommitData(pPageView, nFlags);
     }
@@ -73,44 +73,45 @@
                                    const CFX_PointF& point) {
   CFFL_Button::OnLButtonUp(pPageView, pAnnot, nFlags, point);
 
-  if (IsValid()) {
-    if (CPWL_RadioButton* pWnd =
-            (CPWL_RadioButton*)GetPDFWindow(pPageView, true))
-      pWnd->SetCheck(true);
+  if (!IsValid())
+    return true;
 
-    return CommitData(pPageView, nFlags);
-  }
+  CPWL_RadioButton* pWnd = GetRadioButton(pPageView, true);
+  if (pWnd)
+    pWnd->SetCheck(true);
 
-  return true;
+  return CommitData(pPageView, nFlags);
 }
 
 bool CFFL_RadioButton::IsDataChanged(CPDFSDK_PageView* pPageView) {
-  if (CPWL_RadioButton* pWnd =
-          (CPWL_RadioButton*)GetPDFWindow(pPageView, false)) {
-    return pWnd->IsChecked() != m_pWidget->IsChecked();
-  }
-
-  return false;
+  CPWL_RadioButton* pWnd = GetRadioButton(pPageView, false);
+  return pWnd && pWnd->IsChecked() != m_pWidget->IsChecked();
 }
 
 void CFFL_RadioButton::SaveData(CPDFSDK_PageView* pPageView) {
-  if (CPWL_RadioButton* pWnd =
-          (CPWL_RadioButton*)GetPDFWindow(pPageView, false)) {
-    bool bNewChecked = pWnd->IsChecked();
+  CPWL_RadioButton* pWnd = GetRadioButton(pPageView, false);
+  if (!pWnd)
+    return;
 
-    if (bNewChecked) {
-      CPDF_FormField* pField = m_pWidget->GetFormField();
-      for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
-        if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
-          if (pCtrl->IsChecked()) {
-            break;
-          }
+  bool bNewChecked = pWnd->IsChecked();
+
+  if (bNewChecked) {
+    CPDF_FormField* pField = m_pWidget->GetFormField();
+    for (int32_t i = 0, sz = pField->CountControls(); i < sz; i++) {
+      if (CPDF_FormControl* pCtrl = pField->GetControl(i)) {
+        if (pCtrl->IsChecked()) {
+          break;
         }
       }
     }
-
-    m_pWidget->SetCheck(bNewChecked, false);
-    m_pWidget->UpdateField();
-    SetChangeMark();
   }
+
+  m_pWidget->SetCheck(bNewChecked, false);
+  m_pWidget->UpdateField();
+  SetChangeMark();
+}
+
+CPWL_RadioButton* CFFL_RadioButton::GetRadioButton(CPDFSDK_PageView* pPageView,
+                                                   bool bNew) {
+  return static_cast<CPWL_RadioButton*>(GetPDFWindow(pPageView, bNew));
 }
diff --git a/fpdfsdk/formfiller/cffl_radiobutton.h b/fpdfsdk/formfiller/cffl_radiobutton.h
index 905fc1b..29b7e38 100644
--- a/fpdfsdk/formfiller/cffl_radiobutton.h
+++ b/fpdfsdk/formfiller/cffl_radiobutton.h
@@ -9,6 +9,8 @@
 
 #include "fpdfsdk/formfiller/cffl_formfiller.h"
 
+class CPWL_RadioButton;
+
 class CFFL_RadioButton : public CFFL_Button {
  public:
   CFFL_RadioButton(CPDFSDK_FormFillEnvironment* pApp, CPDFSDK_Widget* pWidget);
@@ -26,6 +28,9 @@
                    const CFX_PointF& point) override;
   bool IsDataChanged(CPDFSDK_PageView* pPageView) override;
   void SaveData(CPDFSDK_PageView* pPageView) override;
+
+ private:
+  CPWL_RadioButton* GetRadioButton(CPDFSDK_PageView* pPageView, bool bNew);
 };
 
 #endif  // FPDFSDK_FORMFILLER_CFFL_RADIOBUTTON_H_
diff --git a/fpdfsdk/formfiller/cffl_textfield.cpp b/fpdfsdk/formfiller/cffl_textfield.cpp
index c34bf3e..da8c14d 100644
--- a/fpdfsdk/formfiller/cffl_textfield.cpp
+++ b/fpdfsdk/formfiller/cffl_textfield.cpp
@@ -97,26 +97,28 @@
                             uint32_t nChar,
                             uint32_t nFlags) {
   switch (nChar) {
-    case FWL_VKEY_Return:
-      if (!(m_pWidget->GetFieldFlags() & FIELDFLAG_MULTILINE)) {
-        CPDFSDK_PageView* pPageView = GetCurPageView(true);
-        ASSERT(pPageView);
-        m_bValid = !m_bValid;
-        m_pFormFillEnv->Invalidate(pAnnot->GetUnderlyingPage(),
-                                   pAnnot->GetRect().ToFxRect());
+    case FWL_VKEY_Return: {
+      if (m_pWidget->GetFieldFlags() & FIELDFLAG_MULTILINE)
+        break;
 
-        if (m_bValid) {
-          if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true))
-            pWnd->SetFocus();
-        } else {
-          if (CommitData(pPageView, nFlags)) {
-            DestroyPDFWindow(pPageView);
-            return true;
-          }
-          return false;
-        }
+      CPDFSDK_PageView* pPageView = GetCurPageView(true);
+      ASSERT(pPageView);
+      m_bValid = !m_bValid;
+      m_pFormFillEnv->Invalidate(pAnnot->GetUnderlyingPage(),
+                                 pAnnot->GetRect().ToFxRect());
+
+      if (m_bValid) {
+        if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true))
+          pWnd->SetFocus();
+        break;
       }
-      break;
+
+      if (!CommitData(pPageView, nFlags))
+        return false;
+
+      DestroyPDFWindow(pPageView);
+      return true;
+    }
     case FWL_VKEY_Escape: {
       CPDFSDK_PageView* pPageView = GetCurPageView(true);
       ASSERT(pPageView);
@@ -129,22 +131,22 @@
 }
 
 bool CFFL_TextField::IsDataChanged(CPDFSDK_PageView* pPageView) {
-  if (CPWL_Edit* pEdit = (CPWL_Edit*)GetPDFWindow(pPageView, false))
-    return pEdit->GetText() != m_pWidget->GetValue();
-
-  return false;
+  CPWL_Edit* pEdit = GetEdit(pPageView, false);
+  return pEdit && pEdit->GetText() != m_pWidget->GetValue();
 }
 
 void CFFL_TextField::SaveData(CPDFSDK_PageView* pPageView) {
-  if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
-    CFX_WideString sOldValue = m_pWidget->GetValue();
-    CFX_WideString sNewValue = pWnd->GetText();
+  CPWL_Edit* pWnd = GetEdit(pPageView, false);
+  if (!pWnd)
+    return;
 
-    m_pWidget->SetValue(sNewValue, false);
-    m_pWidget->ResetFieldAppearance(true);
-    m_pWidget->UpdateField();
-    SetChangeMark();
-  }
+  CFX_WideString sOldValue = m_pWidget->GetValue();
+  CFX_WideString sNewValue = pWnd->GetText();
+
+  m_pWidget->SetValue(sNewValue, false);
+  m_pWidget->ResetFieldAppearance(true);
+  m_pWidget->UpdateField();
+  SetChangeMark();
 }
 
 void CFFL_TextField::GetActionData(CPDFSDK_PageView* pPageView,
@@ -152,7 +154,7 @@
                                    PDFSDK_FieldAction& fa) {
   switch (type) {
     case CPDF_AAction::KeyStroke:
-      if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
+      if (CPWL_Edit* pWnd = GetEdit(pPageView, false)) {
         fa.bFieldFull = pWnd->IsTextFull();
 
         fa.sValue = pWnd->GetText();
@@ -164,7 +166,7 @@
       }
       break;
     case CPDF_AAction::Validate:
-      if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
+      if (CPWL_Edit* pWnd = GetEdit(pPageView, false)) {
         fa.sValue = pWnd->GetText();
       }
       break;
@@ -182,7 +184,7 @@
                                    const PDFSDK_FieldAction& fa) {
   switch (type) {
     case CPDF_AAction::KeyStroke:
-      if (CPWL_Edit* pEdit = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
+      if (CPWL_Edit* pEdit = GetEdit(pPageView, false)) {
         pEdit->SetFocus();
         pEdit->SetSel(fa.nSelStart, fa.nSelEnd);
         pEdit->ReplaceSel(fa.sChange);
@@ -211,19 +213,23 @@
 void CFFL_TextField::SaveState(CPDFSDK_PageView* pPageView) {
   ASSERT(pPageView);
 
-  if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
-    pWnd->GetSel(m_State.nStart, m_State.nEnd);
-    m_State.sValue = pWnd->GetText();
-  }
+  CPWL_Edit* pWnd = GetEdit(pPageView, false);
+  if (!pWnd)
+    return;
+
+  pWnd->GetSel(m_State.nStart, m_State.nEnd);
+  m_State.sValue = pWnd->GetText();
 }
 
 void CFFL_TextField::RestoreState(CPDFSDK_PageView* pPageView) {
   ASSERT(pPageView);
 
-  if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, true)) {
-    pWnd->SetText(m_State.sValue);
-    pWnd->SetSel(m_State.nStart, m_State.nEnd);
-  }
+  CPWL_Edit* pWnd = GetEdit(pPageView, true);
+  if (!pWnd)
+    return;
+
+  pWnd->SetText(m_State.sValue);
+  pWnd->SetSel(m_State.nStart, m_State.nEnd);
 }
 
 CPWL_Wnd* CFFL_TextField::ResetPDFWindow(CPDFSDK_PageView* pPageView,
@@ -235,25 +241,17 @@
 
   CPWL_Wnd* pRet = nullptr;
 
-  if (bRestoreValue) {
+  if (bRestoreValue)
     RestoreState(pPageView);
-    pRet = GetPDFWindow(pPageView, false);
-  } else {
-    pRet = GetPDFWindow(pPageView, true);
-  }
-
+  pRet = GetPDFWindow(pPageView, !bRestoreValue);
   m_pWidget->UpdateField();
-
   return pRet;
 }
 
 #ifdef PDF_ENABLE_XFA
 bool CFFL_TextField::IsFieldFull(CPDFSDK_PageView* pPageView) {
-  if (CPWL_Edit* pWnd = (CPWL_Edit*)GetPDFWindow(pPageView, false)) {
-    return pWnd->IsTextFull();
-  }
-
-  return false;
+  CPWL_Edit* pWnd = GetEdit(pPageView, false);
+  return pWnd && pWnd->IsTextFull();
 }
 #endif  // PDF_ENABLE_XFA
 
@@ -271,3 +269,7 @@
   auto* pBuffer = reinterpret_cast<const unsigned short*>(bsUTFText.c_str());
   m_pFormFillEnv->OnSetFieldInputFocus(pBuffer, nCharacters, true);
 }
+
+CPWL_Edit* CFFL_TextField::GetEdit(CPDFSDK_PageView* pPageView, bool bNew) {
+  return static_cast<CPWL_Edit*>(GetPDFWindow(pPageView, bNew));
+}
diff --git a/fpdfsdk/formfiller/cffl_textfield.h b/fpdfsdk/formfiller/cffl_textfield.h
index cde477a..621c118 100644
--- a/fpdfsdk/formfiller/cffl_textfield.h
+++ b/fpdfsdk/formfiller/cffl_textfield.h
@@ -16,6 +16,7 @@
 #define BF_ALIGN_RIGHT 2
 
 class CBA_FontMap;
+class CPWL_Edit;
 
 struct FFL_TextFieldState {
   FFL_TextFieldState() : nStart(0), nEnd(0) {}
@@ -59,6 +60,8 @@
 #endif  // PDF_ENABLE_XFA
 
  private:
+  CPWL_Edit* GetEdit(CPDFSDK_PageView* pPageView, bool bNew);
+
   std::unique_ptr<CBA_FontMap> m_pFontMap;
   FFL_TextFieldState m_State;
 };