Skip to content

Commit

Permalink
Add more smart pointers to RenderSearchField and SearchPopup
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=271504
rdar://125268281

Reviewed by Ryosuke Niwa and Chris Dumez.

Add more smart pointers to RenderSearchField and modify supporting files and functions as needed

* Source/WebCore/loader/EmptyClients.cpp:
* Source/WebCore/platform/SearchPopupMenu.h:
* Source/WebCore/rendering/RenderSearchField.cpp:
(WebCore::RenderSearchField::willBeDestroyed):
(WebCore::RenderSearchField::resultsButtonElement const):
(WebCore::RenderSearchField::cancelButtonElement const):
(WebCore::RenderSearchField::addSearchResult):
(WebCore::RenderSearchField::showPopup):
(WebCore::RenderSearchField::hidePopup):
(WebCore::RenderSearchField::computeControlLogicalHeight const):
(WebCore::RenderSearchField::updateFromElement):
(WebCore::RenderSearchField::autosaveName const):
(WebCore::RenderSearchField::valueChanged):
(WebCore::RenderSearchField::clientPaddingLeft const):
(WebCore::RenderSearchField::clientPaddingRight const):
(WebCore::RenderSearchField::setTextFromItem):
(WebCore::RenderSearchField::createScrollbar):
* Source/WebKit/WebProcess/WebCoreSupport/WebSearchPopupMenu.cpp:
(WebKit::WebSearchPopupMenu::protectedPopupMenu):
* Source/WebKit/WebProcess/WebCoreSupport/WebSearchPopupMenu.h:
* Source/WebKitLegacy/ios/WebCoreSupport/SearchPopupMenuIOS.cpp:
(SearchPopupMenuIOS::protectedPopupMenu):
* Source/WebKitLegacy/ios/WebCoreSupport/SearchPopupMenuIOS.h:
* Source/WebKitLegacy/mac/WebCoreSupport/SearchPopupMenuMac.h:
* Source/WebKitLegacy/mac/WebCoreSupport/SearchPopupMenuMac.mm:
(SearchPopupMenuMac::protectedPopupMenu):

Canonical link: https://commits.webkit.org/276849@main
  • Loading branch information
abigailfox committed Mar 30, 2024
1 parent 55febbb commit 2fa0921
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
2 changes: 2 additions & 0 deletions Source/WebCore/platform/SearchPopupMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#pragma once

#include "PopupMenu.h"
#include <wtf/Forward.h>
#include <wtf/Vector.h>
#include <wtf/WallTime.h>
Expand All @@ -40,6 +41,7 @@ class SearchPopupMenu : public RefCounted<SearchPopupMenu> {
public:
virtual ~SearchPopupMenu() = default;
virtual PopupMenu* popupMenu() = 0;
RefPtr<PopupMenu> protectedPopupMenu() { return popupMenu(); }
virtual void saveRecentSearches(const AtomString& name, const Vector<RecentSearch>&) = 0;
virtual void loadRecentSearches(const AtomString& name, Vector<RecentSearch>&) = 0;
virtual bool enabled() = 0;
Expand Down
59 changes: 31 additions & 28 deletions Source/WebCore/rendering/RenderSearchField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,29 @@ RenderSearchField::~RenderSearchField()

void RenderSearchField::willBeDestroyed()
{
if (m_searchPopup) {
m_searchPopup->popupMenu()->disconnectClient();
m_searchPopup = nullptr;
}
if (RefPtr searchPopup = std::exchange(m_searchPopup, nullptr))
searchPopup->popupMenu()->disconnectClient();

RenderTextControlSingleLine::willBeDestroyed();
}

inline HTMLElement* RenderSearchField::resultsButtonElement() const
{
return inputElement().resultsButtonElement();
return protectedInputElement()->resultsButtonElement();
}

inline HTMLElement* RenderSearchField::cancelButtonElement() const
{
return inputElement().cancelButtonElement();
return protectedInputElement()->cancelButtonElement();
}

void RenderSearchField::addSearchResult()
{
if (inputElement().maxResults() <= 0)
Ref inputElement = this->inputElement();
if (inputElement->maxResults() <= 0)
return;

String value = inputElement().value();
String value = inputElement->value();
if (value.isEmpty())
return;

Expand All @@ -108,10 +107,10 @@ void RenderSearchField::addSearchResult()

RecentSearch recentSearch = { value, WallTime::now() };
m_recentSearches.insert(0, recentSearch);
while (static_cast<int>(m_recentSearches.size()) > inputElement().maxResults())
while (static_cast<int>(m_recentSearches.size()) > inputElement->maxResults())
m_recentSearches.removeLast();

const AtomString& name = autosaveName();
AtomString name = autosaveName();
if (!m_searchPopup)
m_searchPopup = page().chrome().createSearchPopupMenu(*this);

Expand Down Expand Up @@ -146,25 +145,25 @@ void RenderSearchField::showPopup()
FloatPoint absTopLeft = localToAbsolute(FloatPoint(), UseTransforms);
IntRect absBounds = absoluteBoundingBoxRectIgnoringTransforms();
absBounds.setLocation(roundedIntPoint(absTopLeft));
m_searchPopup->popupMenu()->show(absBounds, &view().frameView(), -1);
protectedSearchPopup()->protectedPopupMenu()->show(absBounds, &view().frameView(), -1);
}

void RenderSearchField::hidePopup()
{
if (m_searchPopup)
m_searchPopup->popupMenu()->hide();
if (RefPtr searchPopup = m_searchPopup)
searchPopup->protectedPopupMenu()->hide();
}

LayoutUnit RenderSearchField::computeControlLogicalHeight(LayoutUnit lineHeight, LayoutUnit nonContentHeight) const
{
HTMLElement* resultsButton = resultsButtonElement();
if (RenderBox* resultsRenderer = resultsButton ? resultsButton->renderBox() : 0) {
RefPtr resultsButton = resultsButtonElement();
if (auto* resultsRenderer = resultsButton ? resultsButton->renderBox() : nullptr) {
resultsRenderer->updateLogicalHeight();
nonContentHeight = std::max(nonContentHeight, resultsRenderer->borderAndPaddingLogicalHeight() + resultsRenderer->marginLogicalHeight());
lineHeight = std::max(lineHeight, resultsRenderer->logicalHeight());
}
HTMLElement* cancelButton = cancelButtonElement();
if (RenderBox* cancelRenderer = cancelButton ? cancelButton->renderBox() : 0) {
RefPtr cancelButton = cancelButtonElement();
if (auto* cancelRenderer = cancelButton ? cancelButton->renderBox() : nullptr) {
cancelRenderer->updateLogicalHeight();
nonContentHeight = std::max(nonContentHeight, cancelRenderer->borderAndPaddingLogicalHeight() + cancelRenderer->marginLogicalHeight());
lineHeight = std::max(lineHeight, cancelRenderer->logicalHeight());
Expand Down Expand Up @@ -192,7 +191,7 @@ void RenderSearchField::updateFromElement()
updateCancelButtonVisibility();

if (m_searchPopupIsVisible)
m_searchPopup->popupMenu()->updateFromElement();
protectedSearchPopup()->protectedPopupMenu()->updateFromElement();
}

void RenderSearchField::updateCancelButtonVisibility() const
Expand All @@ -218,7 +217,7 @@ Visibility RenderSearchField::visibilityForCancelButton() const

const AtomString& RenderSearchField::autosaveName() const
{
return inputElement().attributeWithoutSynchronization(nameAttr);
return protectedInputElement()->attributeWithoutSynchronization(nameAttr);
}

// PopupMenuClient methods
Expand All @@ -236,11 +235,12 @@ void RenderSearchField::valueChanged(unsigned listIndex, bool fireEvents)
}
}
} else {
inputElement().setValue(itemText(listIndex));
if (inputElement().document().settings().searchInputIncrementalAttributeAndSearchEventEnabled()
Ref input = inputElement();
input->setValue(itemText(listIndex));
if (input->document().settings().searchInputIncrementalAttributeAndSearchEventEnabled()
&& fireEvents)
inputElement().onSearch();
inputElement().select();
input->onSearch();
input->select();
}
}

Expand Down Expand Up @@ -310,16 +310,19 @@ int RenderSearchField::clientInsetRight() const
LayoutUnit RenderSearchField::clientPaddingLeft() const
{
LayoutUnit padding = paddingLeft();
if (RenderBox* box = innerBlockElement() ? innerBlockElement()->renderBox() : 0)
RefPtr innerBlock = innerBlockElement();
if (auto* box = innerBlock ? innerBlock->renderBox() : nullptr)
padding += box->x();
return padding;
}

LayoutUnit RenderSearchField::clientPaddingRight() const
{
LayoutUnit padding = paddingRight();
if (RenderBox* containerBox = containerElement() ? containerElement()->renderBox() : 0) {
if (RenderBox* innerBlockBox = innerBlockElement() ? innerBlockElement()->renderBox() : 0)
RefPtr container = containerElement();
if (auto* containerBox = container ? container->renderBox() : nullptr) {
RefPtr innerBlock = innerBlockElement();
if (auto* innerBlockBox = innerBlock ? innerBlock->renderBox() : nullptr)
padding += containerBox->width() - (innerBlockBox->x() + innerBlockBox->width());
}
return padding;
Expand Down Expand Up @@ -362,7 +365,7 @@ bool RenderSearchField::itemIsSelected(unsigned) const

void RenderSearchField::setTextFromItem(unsigned listIndex)
{
inputElement().setValue(itemText(listIndex));
protectedInputElement()->setValue(itemText(listIndex));
}

FontSelector* RenderSearchField::fontSelector() const
Expand All @@ -379,7 +382,7 @@ Ref<Scrollbar> RenderSearchField::createScrollbar(ScrollableArea& scrollableArea
{
bool usesLegacyScrollbarStyle = style().usesLegacyScrollbarStyle();
if (usesLegacyScrollbarStyle)
return RenderScrollbar::createCustomScrollbar(scrollableArea, orientation, &inputElement());
return RenderScrollbar::createCustomScrollbar(scrollableArea, orientation, protectedInputElement().ptr());
return Scrollbar::createNativeScrollbar(scrollableArea, orientation, widthStyle);
}

Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/rendering/RenderSearchField.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class RenderSearchField final : public RenderTextControlSingleLine, private Popu
FontSelector* fontSelector() const override;
HostWindow* hostWindow() const override;
Ref<Scrollbar> createScrollbar(ScrollableArea&, ScrollbarOrientation, ScrollbarWidth) override;
RefPtr<SearchPopupMenu> protectedSearchPopup() const { return m_searchPopup; };

HTMLElement* resultsButtonElement() const;
HTMLElement* cancelButtonElement() const;
Expand Down
5 changes: 5 additions & 0 deletions Source/WebCore/rendering/RenderTextControlSingleLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,11 @@ HTMLInputElement& RenderTextControlSingleLine::inputElement() const
return downcast<HTMLInputElement>(RenderTextControl::textFormControlElement());
}

Ref<HTMLInputElement> RenderTextControlSingleLine::protectedInputElement() const
{
return downcast<HTMLInputElement>(RenderTextControl::textFormControlElement());
}

RenderTextControlInnerBlock::RenderTextControlInnerBlock(Element& element, RenderStyle&& style)
: RenderBlockFlow(Type::TextControlInnerBlock, element, WTFMove(style))
{
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/rendering/RenderTextControlSingleLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class RenderTextControlSingleLine : public RenderTextControl {
HTMLElement* containerElement() const;
HTMLElement* innerBlockElement() const;
HTMLInputElement& inputElement() const;
Ref<HTMLInputElement> protectedInputElement() const;

private:
void textFormControlElement() const = delete;
Expand Down

0 comments on commit 2fa0921

Please sign in to comment.