Title: [293094] trunk/Source/WebCore
Revision
293094
Author
[email protected]
Date
2022-04-20 08:04:25 -0700 (Wed, 20 Apr 2022)

Log Message

Some AutoscrollController cleanup
https://bugs.webkit.org/show_bug.cgi?id=239512

Reviewed by Alan Bujtas.

Have AutoscrollController store a WeakPtr to the render object. Address an apparent
null de-ref in AutoscrollController::stopAutoscrollTimer() where the Frame can null.

Refactor updateDragAndDrop() with a lambda so that all the code paths that exit early
clearly call stopAutoscrollTimer() which nulls out the renderer.

* page/AutoscrollController.cpp:
(WebCore::AutoscrollController::autoscrollRenderer const):
(WebCore::AutoscrollController::startAutoscrollForSelection):
(WebCore::AutoscrollController::stopAutoscrollTimer):
(WebCore::AutoscrollController::updateAutoscrollRenderer):
(WebCore::AutoscrollController::updateDragAndDrop):
(WebCore::AutoscrollController::startPanScrolling):
* page/AutoscrollController.h:
* page/EventHandler.cpp:
(WebCore::EventHandler::startPanScrolling):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (293093 => 293094)


--- trunk/Source/WebCore/ChangeLog	2022-04-20 13:51:24 UTC (rev 293093)
+++ trunk/Source/WebCore/ChangeLog	2022-04-20 15:04:25 UTC (rev 293094)
@@ -1,3 +1,27 @@
+2022-04-20  Simon Fraser  <[email protected]>
+
+        Some AutoscrollController cleanup
+        https://bugs.webkit.org/show_bug.cgi?id=239512
+
+        Reviewed by Alan Bujtas.
+
+        Have AutoscrollController store a WeakPtr to the render object. Address an apparent
+        null de-ref in AutoscrollController::stopAutoscrollTimer() where the Frame can null.
+        
+        Refactor updateDragAndDrop() with a lambda so that all the code paths that exit early
+        clearly call stopAutoscrollTimer() which nulls out the renderer.
+
+        * page/AutoscrollController.cpp:
+        (WebCore::AutoscrollController::autoscrollRenderer const):
+        (WebCore::AutoscrollController::startAutoscrollForSelection):
+        (WebCore::AutoscrollController::stopAutoscrollTimer):
+        (WebCore::AutoscrollController::updateAutoscrollRenderer):
+        (WebCore::AutoscrollController::updateDragAndDrop):
+        (WebCore::AutoscrollController::startPanScrolling):
+        * page/AutoscrollController.h:
+        * page/EventHandler.cpp:
+        (WebCore::EventHandler::startPanScrolling):
+
 2022-04-20  Yacine Bandou  <[email protected]>
 
         [GStreamer] REGRESSION(r285586): we never end the playback of some videos

Modified: trunk/Source/WebCore/page/AutoscrollController.cpp (293093 => 293094)


--- trunk/Source/WebCore/page/AutoscrollController.cpp	2022-04-20 13:51:24 UTC (rev 293093)
+++ trunk/Source/WebCore/page/AutoscrollController.cpp	2022-04-20 15:04:25 UTC (rev 293094)
@@ -56,14 +56,12 @@
 
 AutoscrollController::AutoscrollController()
     : m_autoscrollTimer(*this, &AutoscrollController::autoscrollTimerFired)
-    , m_autoscrollRenderer(nullptr)
-    , m_autoscrollType(NoAutoscroll)
 {
 }
 
 RenderBox* AutoscrollController::autoscrollRenderer() const
 {
-    return m_autoscrollRenderer;
+    return m_autoscrollRenderer.get();
 }
 
 bool AutoscrollController::autoscrollInProgress() const
@@ -76,17 +74,18 @@
     // We don't want to trigger the autoscroll or the panScroll if it's already active
     if (m_autoscrollTimer.isActive())
         return;
-    RenderBox* scrollable = RenderBox::findAutoscrollable(renderer);
+    auto* scrollable = RenderBox::findAutoscrollable(renderer);
     if (!scrollable)
         return;
     m_autoscrollType = AutoscrollForSelection;
-    m_autoscrollRenderer = scrollable;
+    m_autoscrollRenderer = WeakPtr { *scrollable };
     startAutoscrollTimer();
 }
 
 void AutoscrollController::stopAutoscrollTimer(bool rendererIsBeingDestroyed)
 {
-    RenderBox* scrollable = m_autoscrollRenderer;
+    auto scrollable = m_autoscrollRenderer;
+
     m_autoscrollTimer.stop();
     m_autoscrollRenderer = nullptr;
 
@@ -93,9 +92,9 @@
     if (!scrollable)
         return;
 
-    Frame& frame = scrollable->frame();
-    if (autoscrollInProgress() && frame.eventHandler().mouseDownWasInSubframe()) {
-        if (auto subframe = frame.eventHandler().subframeForTargetNode(frame.eventHandler().mousePressNode()))
+    auto* frame = scrollable->document().frame();
+    if (autoscrollInProgress() && frame && frame->eventHandler().mouseDownWasInSubframe()) {
+        if (auto subframe = frame->eventHandler().subframeForTargetNode(frame->eventHandler().mousePressNode()))
             subframe->eventHandler().stopAutoscrollTimer(rendererIsBeingDestroyed);
         return;
     }
@@ -102,6 +101,7 @@
 
     if (!rendererIsBeingDestroyed)
         scrollable->stopAutoscroll();
+
 #if ENABLE(PAN_SCROLLING)
     if (panScrollInProgress()) {
         FrameView& frameView = scrollable->view().frameView();
@@ -114,8 +114,8 @@
 
 #if ENABLE(PAN_SCROLLING)
     // If we're not in the top frame we notify it that we are not doing a panScroll any more.
-    if (!frame.isMainFrame())
-        frame.mainFrame().eventHandler().didPanScrollStop();
+    if (frame && !frame->isMainFrame())
+        frame->mainFrame().eventHandler().didPanScrollStop();
 #endif
 }
 
@@ -124,44 +124,52 @@
     if (!m_autoscrollRenderer)
         return;
 
-    RenderObject* renderer = m_autoscrollRenderer;
+    RenderObject* renderer = m_autoscrollRenderer.get();
 
 #if ENABLE(PAN_SCROLLING)
     constexpr OptionSet<HitTestRequest::Type> hitType { HitTestRequest::Type::ReadOnly, HitTestRequest::Type::Active, HitTestRequest::Type::AllowChildFrameContent };
     HitTestResult hitTest = m_autoscrollRenderer->frame().eventHandler().hitTestResultAtPoint(m_panScrollStartPos, hitType);
 
-    if (Node* nodeAtPoint = hitTest.innerNode())
+    if (auto* nodeAtPoint = hitTest.innerNode())
         renderer = nodeAtPoint->renderer();
 #endif
 
     while (renderer && !(is<RenderBox>(*renderer) && downcast<RenderBox>(*renderer).canAutoscroll()))
         renderer = renderer->parent();
-    m_autoscrollRenderer = dynamicDowncast<RenderBox>(renderer);
+
+    if (!is<RenderBox>(renderer)) {
+        m_autoscrollRenderer = nullptr;
+        return;
+    }
+
+    m_autoscrollRenderer = WeakPtr { downcast<RenderBox>(*renderer) };
 }
 
 void AutoscrollController::updateDragAndDrop(Node* dropTargetNode, const IntPoint& eventPosition, WallTime eventTime)
 {
-    if (!dropTargetNode) {
-        stopAutoscrollTimer();
-        return;
-    }
+    IntSize offset;
+    auto findDragAndDropScroller = [&]() -> RenderBox* {
+        if (!dropTargetNode)
+            return nullptr;
 
-    RenderBox* scrollable = RenderBox::findAutoscrollable(dropTargetNode->renderer());
-    if (!scrollable) {
-        stopAutoscrollTimer();
-        return;
-    }
+        auto* scrollable = RenderBox::findAutoscrollable(dropTargetNode->renderer());
+        if (!scrollable)
+            return nullptr;
 
-    Frame& frame = scrollable->frame();
+        auto& frame = scrollable->frame();
+        auto* page = frame.page();
+        if (!page || !page->settings().autoscrollForDragAndDropEnabled())
+            return nullptr;
 
-    Page* page = frame.page();
-    if (!page || !page->settings().autoscrollForDragAndDropEnabled()) {
-        stopAutoscrollTimer();
-        return;
-    }
+        offset = scrollable->calculateAutoscrollDirection(eventPosition);
+        if (offset.isZero())
+            return nullptr;
 
-    IntSize offset = scrollable->calculateAutoscrollDirection(eventPosition);
-    if (offset.isZero()) {
+        return scrollable;
+    };
+    
+    RenderBox* scrollable = findDragAndDropScroller();
+    if (!scrollable) {
         stopAutoscrollTimer();
         return;
     }
@@ -170,12 +178,12 @@
 
     if (m_autoscrollType == NoAutoscroll) {
         m_autoscrollType = AutoscrollForDragAndDrop;
-        m_autoscrollRenderer = scrollable;
+        m_autoscrollRenderer = WeakPtr { *scrollable };
         m_dragAndDropAutoscrollStartTime = eventTime;
         startAutoscrollTimer();
     } else if (m_autoscrollRenderer != scrollable) {
         m_dragAndDropAutoscrollStartTime = eventTime;
-        m_autoscrollRenderer = scrollable;
+        m_autoscrollRenderer = WeakPtr { *scrollable };
     }
 }
 
@@ -210,7 +218,7 @@
     return m_autoscrollType == AutoscrollForPan || m_autoscrollType == AutoscrollForPanCanStop;
 }
 
-void AutoscrollController::startPanScrolling(RenderBox* scrollable, const IntPoint& lastKnownMousePosition)
+void AutoscrollController::startPanScrolling(RenderBox& scrollable, const IntPoint& lastKnownMousePosition)
 {
     // We don't want to trigger the autoscroll or the panScroll if it's already active
     if (m_autoscrollTimer.isActive())
@@ -217,12 +225,13 @@
         return;
 
     m_autoscrollType = AutoscrollForPan;
-    m_autoscrollRenderer = scrollable;
+    m_autoscrollRenderer = WeakPtr { scrollable };
     m_panScrollStartPos = lastKnownMousePosition;
 
-    if (FrameView* view = scrollable->frame().view())
+    if (auto* view = scrollable.frame().view())
         view->addPanScrollIcon(lastKnownMousePosition);
-    scrollable->frame().eventHandler().didPanScrollStart();
+
+    scrollable.frame().eventHandler().didPanScrollStart();
     startAutoscrollTimer();
 }
 #else

Modified: trunk/Source/WebCore/page/AutoscrollController.h (293093 => 293094)


--- trunk/Source/WebCore/page/AutoscrollController.h	2022-04-20 13:51:24 UTC (rev 293093)
+++ trunk/Source/WebCore/page/AutoscrollController.h	2022-04-20 15:04:25 UTC (rev 293094)
@@ -66,7 +66,7 @@
     void didPanScrollStop();
     void handleMouseReleaseEvent(const PlatformMouseEvent&);
     void setPanScrollInProgress(bool);
-    void startPanScrolling(RenderBox*, const IntPoint&);
+    void startPanScrolling(RenderBox&, const IntPoint&);
 #endif
 
 private:
@@ -77,8 +77,8 @@
 #endif
 
     Timer m_autoscrollTimer;
-    RenderBox* m_autoscrollRenderer;
-    AutoscrollType m_autoscrollType;
+    WeakPtr<RenderBox> m_autoscrollRenderer;
+    AutoscrollType m_autoscrollType { NoAutoscroll };
     IntPoint m_dragAndDropAutoscrollReferencePosition;
     WallTime m_dragAndDropAutoscrollStartTime;
 #if ENABLE(PAN_SCROLLING)

Modified: trunk/Source/WebCore/page/EventHandler.cpp (293093 => 293094)


--- trunk/Source/WebCore/page/EventHandler.cpp	2022-04-20 13:51:24 UTC (rev 293093)
+++ trunk/Source/WebCore/page/EventHandler.cpp	2022-04-20 15:04:25 UTC (rev 293094)
@@ -1127,12 +1127,10 @@
 
 void EventHandler::startPanScrolling(RenderElement& renderer)
 {
-#if !PLATFORM(IOS_FAMILY)
     if (!is<RenderBox>(renderer))
         return;
-    m_autoscrollController->startPanScrolling(&downcast<RenderBox>(renderer), lastKnownMousePosition());
+    m_autoscrollController->startPanScrolling(downcast<RenderBox>(renderer), lastKnownMousePosition());
     invalidateClick();
-#endif
 }
 
 #endif // ENABLE(PAN_SCROLLING)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to