From 4ef943b992782d930a7550b98e3d0389886a0ba3 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 26 Jul 2018 19:06:06 +0000 Subject: Be more careful casting to widget subclasses. Part of the effort to reduce unchecked static casts. Change-Id: I1bff1c53aa7c5804660de4b65cf01523d70fcbb7 Reviewed-on: https://pdfium-review.googlesource.com/38896 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang --- fpdfsdk/cpdfsdk_formfillenvironment.cpp | 2 +- fpdfsdk/cpdfsdk_interform.cpp | 11 +++------ fpdfsdk/cpdfsdk_widgethandler.cpp | 5 ++--- fpdfsdk/cpdfsdk_xfawidget.h | 5 +++++ fpdfsdk/cpdfsdk_xfawidgethandler.cpp | 2 +- fpdfsdk/formfiller/cffl_button.cpp | 2 +- fpdfsdk/formfiller/cffl_checkbox.cpp | 4 ++-- fpdfsdk/formfiller/cffl_formfiller.cpp | 2 +- fpdfsdk/formfiller/cffl_interactiveformfiller.cpp | 3 +-- fpdfsdk/pwl/cpwl_wnd.cpp | 27 +++++++++++------------ fxjs/cjs_document.cpp | 2 +- fxjs/cjs_field.cpp | 18 +++++++-------- 12 files changed, 39 insertions(+), 44 deletions(-) diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp index 649d3bbd2e..da342865cb 100644 --- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp +++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp @@ -699,7 +699,7 @@ bool CPDFSDK_FormFillEnvironment::KillFocusAnnot(uint32_t nFlag) { } if (pFocusAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::WIDGET) { - CPDFSDK_Widget* pWidget = static_cast(pFocusAnnot.Get()); + CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pFocusAnnot.Get()); FormFieldType fieldType = pWidget->GetFieldType(); if (fieldType == FormFieldType::kTextField || fieldType == FormFieldType::kComboBox) { diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp index 9d646b6574..3cfc84dfd9 100644 --- a/fpdfsdk/cpdfsdk_interform.cpp +++ b/fpdfsdk/cpdfsdk_interform.cpp @@ -111,10 +111,8 @@ CPDFSDK_Widget* CPDFSDK_InterForm::GetSibling(CPDFSDK_Widget* pWidget, auto pIterator = pdfium::MakeUnique( pWidget->GetPageView(), CPDF_Annot::Subtype::WIDGET); - if (bNext) - return static_cast(pIterator->GetNextAnnot(pWidget)); - - return static_cast(pIterator->GetPrevAnnot(pWidget)); + return ToCPDFSDKWidget(bNext ? pIterator->GetNextAnnot(pWidget) + : pIterator->GetPrevAnnot(pWidget)); } CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const { @@ -144,10 +142,7 @@ CPDFSDK_Widget* CPDFSDK_InterForm::GetWidget(CPDF_FormControl* pControl) const { pPage = m_pFormFillEnv->GetPageView(nPageIndex); } - if (!pPage) - return nullptr; - - return static_cast(pPage->GetAnnotByDict(pControlDict)); + return pPage ? ToCPDFSDKWidget(pPage->GetAnnotByDict(pControlDict)) : nullptr; } void CPDFSDK_InterForm::GetWidgets( diff --git a/fpdfsdk/cpdfsdk_widgethandler.cpp b/fpdfsdk/cpdfsdk_widgethandler.cpp index 12ae17372a..22f5d3933a 100644 --- a/fpdfsdk/cpdfsdk_widgethandler.cpp +++ b/fpdfsdk/cpdfsdk_widgethandler.cpp @@ -77,11 +77,10 @@ CPDFSDK_Annot* CPDFSDK_WidgetHandler::NewAnnot(CXFA_FFWidget* hWidget, void CPDFSDK_WidgetHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot) { ASSERT(pAnnot); - if (m_pFormFiller) m_pFormFiller->OnDelete(pAnnot); - std::unique_ptr pWidget(static_cast(pAnnot)); + std::unique_ptr pWidget(ToCPDFSDKWidget(pAnnot)); CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm(); CPDF_FormControl* pControl = pWidget->GetFormControl(); pInterForm->RemoveMap(pControl); @@ -222,7 +221,7 @@ void CPDFSDK_WidgetHandler::OnLoad(CPDFSDK_Annot* pAnnot) { if (pAnnot->IsSignatureWidget()) return; - CPDFSDK_Widget* pWidget = static_cast(pAnnot); + CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot); if (!pWidget->IsAppearanceValid()) pWidget->ResetAppearance(nullptr, false); diff --git a/fpdfsdk/cpdfsdk_xfawidget.h b/fpdfsdk/cpdfsdk_xfawidget.h index d2635eb4e1..5db9f8d066 100644 --- a/fpdfsdk/cpdfsdk_xfawidget.h +++ b/fpdfsdk/cpdfsdk_xfawidget.h @@ -36,4 +36,9 @@ class CPDFSDK_XFAWidget : public CPDFSDK_Annot { UnownedPtr m_hXFAWidget; }; +inline CPDFSDK_XFAWidget* ToXFAWidget(CPDFSDK_Annot* pAnnot) { + return pAnnot && pAnnot->GetAnnotSubtype() == CPDF_Annot::Subtype::XFAWIDGET + ? static_cast(pAnnot) : nullptr; +} + #endif // FPDFSDK_CPDFSDK_XFAWIDGET_H_ diff --git a/fpdfsdk/cpdfsdk_xfawidgethandler.cpp b/fpdfsdk/cpdfsdk_xfawidgethandler.cpp index 1a8ec69ba2..a395eef038 100644 --- a/fpdfsdk/cpdfsdk_xfawidgethandler.cpp +++ b/fpdfsdk/cpdfsdk_xfawidgethandler.cpp @@ -70,7 +70,7 @@ void CPDFSDK_XFAWidgetHandler::OnDraw(CPDFSDK_PageView* pPageView, void CPDFSDK_XFAWidgetHandler::OnLoad(CPDFSDK_Annot* pAnnot) {} void CPDFSDK_XFAWidgetHandler::ReleaseAnnot(CPDFSDK_Annot* pAnnot) { - CPDFSDK_XFAWidget* pWidget = static_cast(pAnnot); + CPDFSDK_XFAWidget* pWidget = ToXFAWidget(pAnnot); CPDFSDK_InterForm* pInterForm = pWidget->GetInterForm(); pInterForm->RemoveXFAMap(pWidget->GetXFAWidget()); diff --git a/fpdfsdk/formfiller/cffl_button.cpp b/fpdfsdk/formfiller/cffl_button.cpp index dab0045373..ea2315d5a6 100644 --- a/fpdfsdk/formfiller/cffl_button.cpp +++ b/fpdfsdk/formfiller/cffl_button.cpp @@ -64,7 +64,7 @@ void CFFL_Button::OnDraw(CPDFSDK_PageView* pPageView, CFX_RenderDevice* pDevice, const CFX_Matrix& mtUser2Device) { ASSERT(pPageView); - CPDFSDK_Widget* pWidget = static_cast(pAnnot); + CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot); CPDF_FormControl* pCtrl = pWidget->GetFormControl(); if (pCtrl->GetHighlightingMode() != CPDF_FormControl::Push) { pWidget->DrawAppearance(pDevice, mtUser2Device, CPDF_Annot::Normal, diff --git a/fpdfsdk/formfiller/cffl_checkbox.cpp b/fpdfsdk/formfiller/cffl_checkbox.cpp index e9c72efd39..57119c5c31 100644 --- a/fpdfsdk/formfiller/cffl_checkbox.cpp +++ b/fpdfsdk/formfiller/cffl_checkbox.cpp @@ -61,7 +61,7 @@ bool CFFL_CheckBox::OnChar(CPDFSDK_Annot* pAnnot, CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true); if (pWnd) { - CPDFSDK_Widget* pWidget = static_cast(pAnnot); + CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot); pWnd->SetCheck(!pWidget->IsChecked()); } @@ -83,7 +83,7 @@ bool CFFL_CheckBox::OnLButtonUp(CPDFSDK_PageView* pPageView, CPWL_CheckBox* pWnd = GetCheckBox(pPageView, true); if (pWnd) { - CPDFSDK_Widget* pWidget = static_cast(pAnnot); + CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot); pWnd->SetCheck(!pWidget->IsChecked()); } diff --git a/fpdfsdk/formfiller/cffl_formfiller.cpp b/fpdfsdk/formfiller/cffl_formfiller.cpp index 94592e719f..85f53469e3 100644 --- a/fpdfsdk/formfiller/cffl_formfiller.cpp +++ b/fpdfsdk/formfiller/cffl_formfiller.cpp @@ -291,7 +291,7 @@ bool CFFL_FormFiller::Redo(CPDFSDK_Annot* pAnnot) { } void CFFL_FormFiller::SetFocusForAnnot(CPDFSDK_Annot* pAnnot, uint32_t nFlag) { - auto* pWidget = static_cast(pAnnot); + CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot); IPDF_Page* pPage = pWidget->GetPage(); CPDFSDK_PageView* pPageView = m_pFormFillEnv->GetPageView(pPage, true); if (CPWL_Wnd* pWnd = GetPDFWindow(pPageView, true)) diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp index d73aaf93b3..c1cc355156 100644 --- a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp +++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp @@ -58,7 +58,7 @@ void CFFL_InteractiveFormFiller::OnDraw(CPDFSDK_PageView* pPageView, CFX_RenderDevice* pDevice, CFX_Matrix* pUser2Device) { ASSERT(pPageView); - CPDFSDK_Widget* pWidget = static_cast(pAnnot); + CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot); if (!IsVisible(pWidget)) return; @@ -66,7 +66,6 @@ void CFFL_InteractiveFormFiller::OnDraw(CPDFSDK_PageView* pPageView, if (pFormFiller && pFormFiller->IsValid()) { pFormFiller->OnDraw(pPageView, pAnnot, pDevice, *pUser2Device); pAnnot->GetPDFPage(); - if (m_pFormFillEnv->GetFocusAnnot() != pAnnot) return; diff --git a/fpdfsdk/pwl/cpwl_wnd.cpp b/fpdfsdk/pwl/cpwl_wnd.cpp index 8782e85a14..a561b2de60 100644 --- a/fpdfsdk/pwl/cpwl_wnd.cpp +++ b/fpdfsdk/pwl/cpwl_wnd.cpp @@ -277,33 +277,32 @@ void CPWL_Wnd::DrawChildAppearance(CFX_RenderDevice* pDevice, } bool CPWL_Wnd::InvalidateRect(CFX_FloatRect* pRect) { - ObservedPtr thisObserved(this); - if (!IsValid()) + if (!IsValid()) return true; + ObservedPtr thisObserved(this); CFX_FloatRect rcRefresh = pRect ? *pRect : GetWindowRect(); - if (!HasFlag(PWS_NOREFRESHCLIP)) { CFX_FloatRect rcClip = GetClipRect(); - if (!rcClip.IsEmpty()) { + if (!rcClip.IsEmpty()) rcRefresh.Intersect(rcClip); - } } CFX_FloatRect rcWin = PWLtoWnd(rcRefresh); rcWin.Inflate(1, 1); rcWin.Normalize(); - if (CFX_SystemHandler* pSH = GetSystemHandler()) { - if (CPDFSDK_Widget* widget = static_cast( - m_CreationParams.pAttachedWidget.Get())) { - pSH->InvalidateRect(widget, rcWin); - if (!thisObserved) - return false; - } - } + CFX_SystemHandler* pSH = GetSystemHandler(); + if (!pSH) + return true; - return true; + CPDFSDK_Widget* widget = + ToCPDFSDKWidget(m_CreationParams.pAttachedWidget.Get()); + if (!widget) + return true; + + pSH->InvalidateRect(widget, rcWin); + return !!thisObserved; } #define PWL_IMPLEMENT_KEY_METHOD(key_method_name) \ diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp index 51e189ec88..4ac2f15abd 100644 --- a/fxjs/cjs_document.cpp +++ b/fxjs/cjs_document.cpp @@ -417,7 +417,7 @@ CJS_Return CJS_Document::removeField( return CJS_Return(); for (const auto& pAnnot : widgets) { - CPDFSDK_Widget* pWidget = static_cast(pAnnot.Get()); + CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pAnnot.Get()); if (!pWidget) continue; diff --git a/fxjs/cjs_field.cpp b/fxjs/cjs_field.cpp index 6513d4e486..19e6712f2a 100644 --- a/fxjs/cjs_field.cpp +++ b/fxjs/cjs_field.cpp @@ -54,20 +54,18 @@ void UpdateFormField(CPDFSDK_FormFillEnvironment* pFormFillEnv, for (auto& pObserved : widgets) { if (pObserved) { bool bFormatted = false; - WideString sValue = static_cast(pObserved.Get()) - ->OnFormat(bFormatted); + WideString sValue = + ToCPDFSDKWidget(pObserved.Get())->OnFormat(bFormatted); if (pObserved) { // Not redundant, may be clobbered by OnFormat. - static_cast(pObserved.Get()) - ->ResetAppearance(bFormatted ? &sValue : nullptr, false); + ToCPDFSDKWidget(pObserved.Get())->ResetAppearance( + bFormatted ? &sValue : nullptr, false); } } } } else { for (auto& pObserved : widgets) { - if (pObserved) { - static_cast(pObserved.Get()) - ->ResetAppearance(nullptr, false); - } + if (pObserved) + ToCPDFSDKWidget(pObserved.Get())->ResetAppearance(nullptr, false); } } } @@ -84,7 +82,7 @@ void UpdateFormField(CPDFSDK_FormFillEnvironment* pFormFillEnv, // |GetFormFillEnv| out of the loop. for (auto& pObserved : widgets) { if (pObserved) { - CPDFSDK_Widget* pWidget = static_cast(pObserved.Get()); + CPDFSDK_Widget* pWidget = ToCPDFSDKWidget(pObserved.Get()); pWidget->GetInterForm()->GetFormFillEnv()->UpdateAllViews(nullptr, pWidget); } @@ -1540,7 +1538,7 @@ CJS_Return CJS_Field::get_page(CJS_Runtime* pRuntime) { if (!pObserved) return CJS_Return(JSMessage::kBadObjectError); - auto* pWidget = static_cast(pObserved.Get()); + auto* pWidget = ToCPDFSDKWidget(pObserved.Get()); CPDFSDK_PageView* pPageView = pWidget->GetPageView(); if (!pPageView) return CJS_Return(JSMessage::kBadObjectError); -- cgit v1.2.3