Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt more smart pointers in RenderElement #19451

Merged

Conversation

cdumez
Copy link
Contributor

@cdumez cdumez commented Oct 23, 2023

ea4f8e5

Adopt more smart pointers in RenderElement
https://bugs.webkit.org/show_bug.cgi?id=263542

Reviewed by Ryosuke Niwa.

Adopt more smart pointers in RenderElement.

I also made some changes to EventHandler so that we can store it in
a CheckedPtr / CheckedRef. I made it forward its checked pointer count
to its associated frame since it is owned by the frame. As extra safety,
I made EventHandler::m_frame be a CheckedRef<Frame> which required me
to update all uses of m_frame in this file. That said, the changes in
EventHandler are NOT meant to be exhaustive.

* Source/WebCore/dom/Document.cpp:
(WebCore::Document::protectedView const):
* Source/WebCore/page/EventHandler.cpp:
(WebCore::EventHandler::incrementPtrCount const):
(WebCore::EventHandler::decrementPtrCount const):
* Source/WebCore/page/EventHandler.h:
* Source/WebCore/page/LocalFrame.cpp:
(WebCore::LocalFrame::checkedEventHandler):
(WebCore::LocalFrame::checkedEventHandler const):
(WebCore::Document::protectedView const): Deleted.
* Source/WebCore/page/LocalFrame.h:
* Source/WebCore/rendering/RenderElement.cpp:
(WebCore::RenderElement::createFor):
(WebCore::RenderElement::updateFillImages):
(WebCore::RenderElement::updateShapeImage):
(WebCore::RenderElement::repaintBeforeStyleChange):
(WebCore::RenderElement::initializeStyle):
(WebCore::RenderElement::setStyle):
(WebCore::RenderElement::didAttachChild):
(WebCore::RenderElement::attachRendererInternal):
(WebCore::RenderElement::detachRendererInternal):
(WebCore::findNextLayer):
(WebCore::layerNextSiblingRespectingTopLayer):
(WebCore::addLayers):
(WebCore::RenderElement::removeLayers):
(WebCore::RenderElement::moveLayers):
(WebCore::RenderElement::layerParent const):
(WebCore::RenderElement::propagateStyleToAnonymousChildren):
(WebCore::RenderElement::styleWillChange):
(WebCore::RenderElement::styleDidChange):
(WebCore::RenderElement::insertedIntoTree):
(WebCore::RenderElement::willBeRemovedFromTree):
(WebCore::RenderElement::clearSubtreeLayoutRootIfNeeded const):
(WebCore::RenderElement::willBeDestroyed):
* Source/WebCore/rendering/RenderElement.h:
* Source/WebCore/rendering/RenderObject.h:
(WebCore::RenderObject::protectedFrame const):
* Source/WebCore/rendering/RenderObjectInlines.h:
(WebCore::RenderObject::protectedDocument const):
* Source/WebCore/rendering/style/NinePieceImage.h:
(WebCore::NinePieceImage::protectedImage const):
* Source/WebCore/rendering/style/RenderStyle.h:
* Source/WebCore/rendering/style/RenderStyleInlines.h:
(WebCore::RenderStyle::protectedBackgroundLayers const):
(WebCore::RenderStyle::protectedMaskLayers const):
(WebCore::RenderStyle::protectedShapeOutside const):
* Source/WebCore/rendering/style/ShapeValue.h:
(WebCore::ShapeValue::protectedImage const):

Canonical link: https://commits.webkit.org/269712@main

73eae54

Misc iOS, tvOS & watchOS macOS Linux Windows
βœ… πŸ§ͺ style βœ… πŸ›  ios βœ… πŸ›  mac ⏳ πŸ›  wpe βœ… πŸ›  wincairo
βœ… πŸ§ͺ bindings βœ… πŸ›  ios-sim βœ… πŸ›  mac-AS-debug ⏳ πŸ§ͺ wpe-wk2
βœ… πŸ§ͺ webkitperl βœ… πŸ§ͺ ios-wk2 βœ… πŸ§ͺ api-mac βœ… πŸ›  gtk
βœ… πŸ§ͺ ios-wk2-wpt βœ… πŸ§ͺ mac-wk1 πŸ’₯ πŸ§ͺ gtk-wk2
βœ… πŸ§ͺ api-ios βœ… πŸ§ͺ mac-wk2 βœ… πŸ§ͺ api-gtk
βœ… πŸ›  tv βœ… πŸ§ͺ mac-AS-debug-wk2
βœ… πŸ›  tv-sim
βœ… πŸ›  watch
βœ… πŸ›  πŸ§ͺ unsafe-merge βœ… πŸ›  watch-sim

@cdumez cdumez self-assigned this Oct 23, 2023
@cdumez cdumez added the Layout and Rendering For bugs with layout and rendering of Web pages. label Oct 23, 2023
@@ -1706,7 +1700,7 @@ std::optional<Cursor> EventHandler::selectCursor(const HitTestResult& result, bo
#if ENABLE(CURSOR_VISIBILITY)
void EventHandler::startAutoHideCursorTimer()
{
Page* page = m_frame.page();
Page* page = m_frame->page();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use checkedPage here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to EventHandler are not meant to be exhaustive. This patch's focus is RenderElement. I just made some changes to EventHandler store it in a CheckedPtr/CheckedRef and changing m_frame ended up making the change large.

Looking at this particular function though, you can see the page is only used to query a setting. I figure this was safe so I didn't opportunistically use CheckedPtr for the page here.

@@ -1728,11 +1722,11 @@ void EventHandler::cancelAutoHideCursorTimer()

void EventHandler::autoHideCursorTimerFired()
{
auto* view = m_frame.view();
auto* view = m_frame->view();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use RefPtr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only called view->isActive() so it seemed excessive?

MonotonicTime savedLastHandledUserGestureTimestamp;
bool savedUserDidInteractWithPage = topDocument ? topDocument->userDidInteractWithPage() : false;

if (m_frame.document())
savedLastHandledUserGestureTimestamp = m_frame.document()->lastHandledUserGestureTimestamp();
if (auto* document = frame->document())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use RefPtr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only call a trivial getter on the document so this seemed excessive.

Also note that there are still plenty of potentially unsafe pointer usage in this file. The focus on this patch is RenderElement, not EventHandler.

@@ -4308,10 +4302,10 @@ bool EventHandler::handleDrag(const MouseEventWithHitTestResults& event, CheckDr
// FIXME: Consider doing this earlier in this function as the earliest point we're sure it would be safe to drop an old drag.
invalidateDataTransfer();

if (!m_frame.document())
if (!frame->document())
Copy link
Member

@rniwa rniwa Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can do:

RefPtr document = frame->document();
if (document)
    return false;

dragState().dataTransfer = DataTransfer::createForDrag(*document);

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Oct 23, 2023
@@ -136,6 +137,10 @@ class EventHandler {
explicit EventHandler(LocalFrame&);
~EventHandler();

// For CheckedPtr / CheckedRef use.
void incrementPtrCount() const;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just make EventHandler inherit from CanMakeCheckedPtr unless this is absolutely necessary for correctness, in which case the reason should be stated in the commit message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this goes against @darinadler 's recommendation on my earlier PR?

The EventHandler is completely owned by the Frame via a UniqueRef so we don't need to increase the size of EventHandler unnecessarily. I also made EventHandler::m_frame be a CheckedRef so that the lifetime safety is bi-directional.

I don't mind either way. I merely went this way to be consistent with my earlier PR where I think Darin suggested this (the review comments were not 100% clear to me so I may have misunderstood as well).

@@ -734,7 +734,7 @@ static bool frameHasPlatformWidget(const LocalFrame& frame)
PlatformMouseEvent EventHandler::currentPlatformMouseEvent() const
{
NSView *windowView = nil;
if (Page* page = m_frame.page())
if (Page* page = m_frame->page())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use CheckedPtr?

@@ -338,7 +338,7 @@ static bool findViewInSubviews(NSView *superview, NSView *target)
if (!mouseDownView) {
return nil;
}
auto* topFrameView = m_frame.view();
auto* topFrameView = m_frame->view();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use RefPtr?

@@ -518,7 +518,7 @@ static void selfRetainingNSScrollViewScrollWheel(NSScrollView *self, SEL selecto

void EventHandler::mouseDown(NSEvent *event, NSEvent *correspondingPressureEvent)
{
auto* v = m_frame.view();
auto* v = m_frame->view();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use RefPtr?

@@ -535,7 +535,7 @@ static void selfRetainingNSScrollViewScrollWheel(NSScrollView *self, SEL selecto

void EventHandler::mouseDragged(NSEvent *event, NSEvent *correspondingPressureEvent)
{
auto* v = m_frame.view();
auto* v = m_frame->view();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use RefPtr?

@@ -549,7 +549,7 @@ static void selfRetainingNSScrollViewScrollWheel(NSScrollView *self, SEL selecto

void EventHandler::mouseUp(NSEvent *event, NSEvent *correspondingPressureEvent)
{
auto* v = m_frame.view();
auto* v = m_frame->view();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use RefPtr?

@@ -586,7 +586,7 @@ static void selfRetainingNSScrollViewScrollWheel(NSScrollView *self, SEL selecto
*/
void EventHandler::sendFakeEventsAfterWidgetTracking(NSEvent *initiatingEvent)
{
auto* view = m_frame.view();
auto* view = m_frame->view();
if (!view)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use RefPtr?

if (!page)
return;

auto* view = m_frame.view();
auto* view = m_frame->view();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use RefPtr?

@cdumez cdumez removed the merging-blocked Applied to prevent a change from being merged label Oct 23, 2023
@cdumez cdumez force-pushed the 263542_RenderElement_smart_pointer branch from 065db4c to cbc9a86 Compare October 23, 2023 22:06
@cdumez cdumez force-pushed the 263542_RenderElement_smart_pointer branch from cbc9a86 to 80e0df0 Compare October 24, 2023 02:26
@cdumez cdumez marked this pull request as ready for review October 24, 2023 02:27
@cdumez cdumez force-pushed the 263542_RenderElement_smart_pointer branch from 80e0df0 to 73eae54 Compare October 24, 2023 05:18
@cdumez cdumez added the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Oct 24, 2023
https://bugs.webkit.org/show_bug.cgi?id=263542

Reviewed by Ryosuke Niwa.

Adopt more smart pointers in RenderElement.

I also made some changes to EventHandler so that we can store it in
a CheckedPtr / CheckedRef. I made it forward its checked pointer count
to its associated frame since it is owned by the frame. As extra safety,
I made EventHandler::m_frame be a CheckedRef<Frame> which required me
to update all uses of m_frame in this file. That said, the changes in
EventHandler are NOT meant to be exhaustive.

* Source/WebCore/dom/Document.cpp:
(WebCore::Document::protectedView const):
* Source/WebCore/page/EventHandler.cpp:
(WebCore::EventHandler::incrementPtrCount const):
(WebCore::EventHandler::decrementPtrCount const):
* Source/WebCore/page/EventHandler.h:
* Source/WebCore/page/LocalFrame.cpp:
(WebCore::LocalFrame::checkedEventHandler):
(WebCore::LocalFrame::checkedEventHandler const):
(WebCore::Document::protectedView const): Deleted.
* Source/WebCore/page/LocalFrame.h:
* Source/WebCore/rendering/RenderElement.cpp:
(WebCore::RenderElement::createFor):
(WebCore::RenderElement::updateFillImages):
(WebCore::RenderElement::updateShapeImage):
(WebCore::RenderElement::repaintBeforeStyleChange):
(WebCore::RenderElement::initializeStyle):
(WebCore::RenderElement::setStyle):
(WebCore::RenderElement::didAttachChild):
(WebCore::RenderElement::attachRendererInternal):
(WebCore::RenderElement::detachRendererInternal):
(WebCore::findNextLayer):
(WebCore::layerNextSiblingRespectingTopLayer):
(WebCore::addLayers):
(WebCore::RenderElement::removeLayers):
(WebCore::RenderElement::moveLayers):
(WebCore::RenderElement::layerParent const):
(WebCore::RenderElement::propagateStyleToAnonymousChildren):
(WebCore::RenderElement::styleWillChange):
(WebCore::RenderElement::styleDidChange):
(WebCore::RenderElement::insertedIntoTree):
(WebCore::RenderElement::willBeRemovedFromTree):
(WebCore::RenderElement::clearSubtreeLayoutRootIfNeeded const):
(WebCore::RenderElement::willBeDestroyed):
* Source/WebCore/rendering/RenderElement.h:
* Source/WebCore/rendering/RenderObject.h:
(WebCore::RenderObject::protectedFrame const):
* Source/WebCore/rendering/RenderObjectInlines.h:
(WebCore::RenderObject::protectedDocument const):
* Source/WebCore/rendering/style/NinePieceImage.h:
(WebCore::NinePieceImage::protectedImage const):
* Source/WebCore/rendering/style/RenderStyle.h:
* Source/WebCore/rendering/style/RenderStyleInlines.h:
(WebCore::RenderStyle::protectedBackgroundLayers const):
(WebCore::RenderStyle::protectedMaskLayers const):
(WebCore::RenderStyle::protectedShapeOutside const):
* Source/WebCore/rendering/style/ShapeValue.h:
(WebCore::ShapeValue::protectedImage const):

Canonical link: https://commits.webkit.org/269712@main
@webkit-commit-queue
Copy link
Collaborator

Committed 269712@main (ea4f8e5): https://commits.webkit.org/269712@main

Reviewed commits have been landed. Closing PR #19451 and removing active labels.

@webkit-commit-queue webkit-commit-queue merged commit ea4f8e5 into WebKit:main Oct 24, 2023
@webkit-commit-queue webkit-commit-queue removed the unsafe-merge-queue Applied to send a pull request to merge-queue, but skip building and testing label Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Layout and Rendering For bugs with layout and rendering of Web pages.
Projects
None yet
5 participants