Title: [271000] trunk
Revision
271000
Author
[email protected]
Date
2020-12-18 20:30:57 -0800 (Fri, 18 Dec 2020)

Log Message

iOS wheel events don't work on many websites
https://bugs.webkit.org/show_bug.cgi?id=220034

Reviewed by Simon Fraser.

Source/WebKit:

* Shared/ios/WebIOSEventFactory.h:
* Shared/ios/WebIOSEventFactory.mm:
(WebIOSEventFactory::createWebWheelEvent):
It turns out that many sites look at the legacy mousewheel event properties,
so compute wheelTicks in the same way that we do on macOS.

* UIProcess/API/Cocoa/WKWebViewInternal.h:
* UIProcess/API/ios/WKWebViewIOS.mm:
(-[WKWebView _scrollView:asynchronouslyHandleScrollEvent:completion:]):
If the UI process sends a wheel event with delta=0,0 to the Web Content
process, it will not be sent to the page, and thus not handled.
If this is the first event, it will prevent future events from
being cancelable.

Instead, synthetically defer the "Begin" phase to the first event in a
given gesture that has non-zero deltas, and just short-circuit any
zero-delta events entirely.

This way, WebCore can continue with its existing only-the-first-event-is-cancelable
logic, and we are assured that the first event that WebCore sees is also
the first event that the page sees, and is a Begin-phase event.

* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::dispatchWheelEventWithoutScrolling):
wheelScrollGestureState isn't reset to nullopt until we send the
Begin-phase event to EventHandler, so we must make an exception for
the Begin-phase event, and explicitly make it cancelable. Otherwise,
the state of the previous gesture leaks into the new gesture. Existing
API tests caught this, I just apparently failed to run them after
adopting the shared wheelScrollGestureState().

Tools:

* TestWebKitAPI/Tests/ios/WKScrollViewTests.mm:
(TEST):
Add a test ensuring that if the UIScrollPhaseBegin scroll event
has zero delta, that we let a subsequent UIScrollPhaseChanged event
with non-zero delta cancel the gesture's default action.

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (270999 => 271000)


--- trunk/Source/WebKit/ChangeLog	2020-12-19 02:01:23 UTC (rev 270999)
+++ trunk/Source/WebKit/ChangeLog	2020-12-19 04:30:57 UTC (rev 271000)
@@ -1,3 +1,41 @@
+2020-12-18  Tim Horton  <[email protected]>
+
+        iOS wheel events don't work on many websites
+        https://bugs.webkit.org/show_bug.cgi?id=220034
+
+        Reviewed by Simon Fraser.
+
+        * Shared/ios/WebIOSEventFactory.h:
+        * Shared/ios/WebIOSEventFactory.mm:
+        (WebIOSEventFactory::createWebWheelEvent):
+        It turns out that many sites look at the legacy mousewheel event properties,
+        so compute wheelTicks in the same way that we do on macOS.
+
+        * UIProcess/API/Cocoa/WKWebViewInternal.h:
+        * UIProcess/API/ios/WKWebViewIOS.mm:
+        (-[WKWebView _scrollView:asynchronouslyHandleScrollEvent:completion:]):
+        If the UI process sends a wheel event with delta=0,0 to the Web Content
+        process, it will not be sent to the page, and thus not handled.
+        If this is the first event, it will prevent future events from
+        being cancelable.
+
+        Instead, synthetically defer the "Begin" phase to the first event in a
+        given gesture that has non-zero deltas, and just short-circuit any
+        zero-delta events entirely.
+
+        This way, WebCore can continue with its existing only-the-first-event-is-cancelable
+        logic, and we are assured that the first event that WebCore sees is also
+        the first event that the page sees, and is a Begin-phase event.
+
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::dispatchWheelEventWithoutScrolling):
+        wheelScrollGestureState isn't reset to nullopt until we send the
+        Begin-phase event to EventHandler, so we must make an exception for
+        the Begin-phase event, and explicitly make it cancelable. Otherwise,
+        the state of the previous gesture leaks into the new gesture. Existing
+        API tests caught this, I just apparently failed to run them after
+        adopting the shared wheelScrollGestureState().
+
 2020-12-18  Brady Eidson  <[email protected]>
 
         Fix some issues with PDFs as <object>.

Modified: trunk/Source/WebKit/Shared/ios/WebIOSEventFactory.h (270999 => 271000)


--- trunk/Source/WebKit/Shared/ios/WebIOSEventFactory.h	2020-12-19 02:01:23 UTC (rev 270999)
+++ trunk/Source/WebKit/Shared/ios/WebIOSEventFactory.h	2020-12-19 04:30:57 UTC (rev 271000)
@@ -32,6 +32,7 @@
 #import "WebWheelEvent.h"
 #import <UIKit/UIKit.h>
 #import <WebCore/WebEvent.h>
+#import <wtf/Optional.h>
 
 OBJC_CLASS UIScrollEvent;
 
@@ -41,7 +42,7 @@
     static WebKit::WebMouseEvent createWebMouseEvent(::WebEvent *);
 
 #if HAVE(UISCROLLVIEW_ASYNCHRONOUS_SCROLL_EVENT_HANDLING)
-    static WebKit::WebWheelEvent createWebWheelEvent(UIScrollEvent *, UIView *contentView);
+    static WebKit::WebWheelEvent createWebWheelEvent(UIScrollEvent *, UIView *contentView, Optional<WebKit::WebWheelEvent::Phase> overridePhase = WTF::nullopt);
 #endif
 
     static UIKeyModifierFlags toUIKeyModifierFlags(OptionSet<WebKit::WebEvent::Modifier>);

Modified: trunk/Source/WebKit/Shared/ios/WebIOSEventFactory.mm (270999 => 271000)


--- trunk/Source/WebKit/Shared/ios/WebIOSEventFactory.mm	2020-12-19 02:01:23 UTC (rev 270999)
+++ trunk/Source/WebKit/Shared/ios/WebIOSEventFactory.mm	2020-12-19 04:30:57 UTC (rev 271000)
@@ -31,6 +31,7 @@
 #import "UIKitSPI.h"
 #import <WebCore/KeyEventCodesIOS.h>
 #import <WebCore/PlatformEventFactoryIOS.h>
+#import <WebCore/Scrollbar.h>
 
 UIKeyModifierFlags WebIOSEventFactory::toUIKeyModifierFlags(OptionSet<WebKit::WebEvent::Modifier> modifiers)
 {
@@ -153,11 +154,13 @@
     }
 }
 
-WebKit::WebWheelEvent WebIOSEventFactory::createWebWheelEvent(UIScrollEvent *event, UIView *contentView)
+WebKit::WebWheelEvent WebIOSEventFactory::createWebWheelEvent(UIScrollEvent *event, UIView *contentView, Optional<WebKit::WebWheelEvent::Phase> overridePhase)
 {
     WebCore::IntPoint scrollLocation = WebCore::roundedIntPoint([event locationInView:contentView]);
     CGVector deltaVector = [event _adjustedAcceleratedDeltaInView:contentView];
     WebCore::FloatSize delta(deltaVector.dx, deltaVector.dy);
+    WebCore::FloatSize wheelTicks = delta;
+    wheelTicks.scale(1. / static_cast<float>(WebCore::Scrollbar::pixelsPerLineStep()));
 
     return {
         WebKit::WebEvent::Wheel,
@@ -164,10 +167,10 @@
         scrollLocation,
         scrollLocation,
         delta,
-        { } /* wheelTicks */,
+        wheelTicks,
         WebKit::WebWheelEvent::Granularity::ScrollByPixelWheelEvent,
         false,
-        toWebPhase(event.phase),
+        overridePhase.valueOr(toWebPhase(event.phase)),
         WebKit::WebWheelEvent::PhaseNone,
         true,
         1,

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewInternal.h (270999 => 271000)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewInternal.h	2020-12-19 02:01:23 UTC (rev 270999)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebViewInternal.h	2020-12-19 04:30:57 UTC (rev 271000)
@@ -234,6 +234,7 @@
     BOOL _visibleContentRectUpdateScheduledFromScrollViewInStableState;
 
     Optional<WebCore::WheelScrollGestureState> _currentScrollGestureState;
+    uint64_t _wheelEventCountInCurrentScrollGesture;
 
     _WKDragInteractionPolicy _dragInteractionPolicy;
 

Modified: trunk/Source/WebKit/UIProcess/API/ios/WKWebViewIOS.mm (270999 => 271000)


--- trunk/Source/WebKit/UIProcess/API/ios/WKWebViewIOS.mm	2020-12-19 02:01:23 UTC (rev 270999)
+++ trunk/Source/WebKit/UIProcess/API/ios/WKWebViewIOS.mm	2020-12-19 04:30:57 UTC (rev 271000)
@@ -1607,6 +1607,11 @@
         return;
     }
 
+    if (scrollEvent.phase == UIScrollPhaseBegan) {
+        _currentScrollGestureState = WTF::nullopt;
+        _wheelEventCountInCurrentScrollGesture = 0;
+    }
+
     WebCore::IntPoint scrollLocation = WebCore::roundedIntPoint([scrollEvent locationInView:_contentView.get()]);
     auto eventListeners = WebKit::eventListenerTypesAtPoint(_contentView.get(), scrollLocation);
     bool hasWheelHandlers = eventListeners.contains(WebCore::EventListenerRegionType::Wheel);
@@ -1615,15 +1620,29 @@
         return;
     }
 
-    bool isFirstEventInGesture = scrollEvent.phase == UIScrollPhaseBegan;
-    if (isFirstEventInGesture)
-        _currentScrollGestureState = WTF::nullopt;
+    // Scroll events with zero delta are not dispatched to the page, so cannot be
+    // cancelled, so we can short-circuit them here.
+    // We make an exception for end-phase events, similar to the logic in
+    // EventHandler::handleWheelEventInAppropriateEnclosingBox.
+    CGVector deltaVector = [scrollEvent _adjustedAcceleratedDeltaInView:_contentView.get()];
+    if (!deltaVector.dx && !deltaVector.dy && scrollEvent.phase != UIScrollPhaseEnded)  {
+        completion(NO);
+        return;
+    }
 
     bool hasActiveWheelHandlers = eventListeners.contains(WebCore::EventListenerRegionType::NonPassiveWheel);
     bool isCancelable = hasActiveWheelHandlers && (!_currentScrollGestureState || _currentScrollGestureState == WebCore::WheelScrollGestureState::Blocking);
-    auto event = WebIOSEventFactory::createWebWheelEvent(scrollEvent, _contentView.get());
 
-    _page->dispatchWheelEventWithoutScrolling(event, [weakSelf = WeakObjCPtr<WKWebView>(self), strongCompletion = makeBlockPtr(completion), isCancelable, isFirstEventInGesture](bool handled) {
+    Optional<WebKit::WebWheelEvent::Phase> overridePhase;
+    // The first event with non-zero delta in a given gesture should be considered the
+    // "Began" event in the WebCore sense (e.g. for deciding cancelability). Note that
+    // this may not be a UIScrollPhaseBegin event, nor even necessarily the first UIScrollPhaseChanged event.
+    if (!_wheelEventCountInCurrentScrollGesture)
+        overridePhase = WebKit::WebWheelEvent::PhaseBegan;
+    auto event = WebIOSEventFactory::createWebWheelEvent(scrollEvent, _contentView.get(), overridePhase);
+
+    _wheelEventCountInCurrentScrollGesture++;
+    _page->dispatchWheelEventWithoutScrolling(event, [weakSelf = WeakObjCPtr<WKWebView>(self), strongCompletion = makeBlockPtr(completion), isCancelable](bool handled) {
         auto strongSelf = weakSelf.get();
         if (!strongSelf) {
             strongCompletion(NO);
@@ -1631,7 +1650,7 @@
         }
 
         if (isCancelable) {
-            if (isFirstEventInGesture)
+            if (!strongSelf->_currentScrollGestureState)
                 strongSelf->_currentScrollGestureState = handled ? WebCore::WheelScrollGestureState::Blocking : WebCore::WheelScrollGestureState::NonBlocking;
             strongCompletion(handled);
         }

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp (270999 => 271000)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2020-12-19 02:01:23 UTC (rev 270999)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2020-12-19 04:30:57 UTC (rev 271000)
@@ -2898,7 +2898,7 @@
 {
 #if ENABLE(KINETIC_SCROLLING)
     auto gestureState = m_page->mainFrame().eventHandler().wheelScrollGestureState();
-    bool isCancelable = !gestureState || gestureState == WheelScrollGestureState::Blocking;
+    bool isCancelable = !gestureState || gestureState == WheelScrollGestureState::Blocking || wheelEvent.phase() == WebWheelEvent::PhaseBegan;
 #else
     bool isCancelable = true;
 #endif

Modified: trunk/Tools/ChangeLog (270999 => 271000)


--- trunk/Tools/ChangeLog	2020-12-19 02:01:23 UTC (rev 270999)
+++ trunk/Tools/ChangeLog	2020-12-19 04:30:57 UTC (rev 271000)
@@ -1,3 +1,16 @@
+2020-12-18  Tim Horton  <[email protected]>
+
+        iOS wheel events don't work on many websites
+        https://bugs.webkit.org/show_bug.cgi?id=220034
+
+        Reviewed by Simon Fraser.
+
+        * TestWebKitAPI/Tests/ios/WKScrollViewTests.mm:
+        (TEST):
+        Add a test ensuring that if the UIScrollPhaseBegin scroll event
+        has zero delta, that we let a subsequent UIScrollPhaseChanged event
+        with non-zero delta cancel the gesture's default action.
+
 2020-12-18  Said Abou-Hallawa  <[email protected]>
 
         Upgrade motionmark1.1.plan to r270959

Modified: trunk/Tools/TestWebKitAPI/Tests/ios/WKScrollViewTests.mm (270999 => 271000)


--- trunk/Tools/TestWebKitAPI/Tests/ios/WKScrollViewTests.mm	2020-12-19 02:01:23 UTC (rev 270999)
+++ trunk/Tools/TestWebKitAPI/Tests/ios/WKScrollViewTests.mm	2020-12-19 04:30:57 UTC (rev 271000)
@@ -152,6 +152,7 @@
     EXPECT_FALSE(wasHandled);
     EXPECT_TRUE([[webView objectByEvaluatingJavaScript:@"window.lastWheelEvent.cancelable"] intValue]);
     EXPECT_EQ(-10, [[webView objectByEvaluatingJavaScript:@"window.lastWheelEvent.deltaY"] intValue]);
+    EXPECT_EQ(30, [[webView objectByEvaluatingJavaScript:@"window.lastWheelEvent.wheelDeltaY"] intValue]);
     synchronouslyHandleScrollEvent(UIScrollPhaseChanged, CGPointMake(100, 100), CGVectorMake(0, 10));
     EXPECT_FALSE(wasHandled);
     synchronouslyHandleScrollEvent(UIScrollPhaseEnded, CGPointMake(100, 100), CGVectorMake(0, 0));
@@ -195,6 +196,24 @@
     EXPECT_FALSE(wasHandled);
     synchronouslyHandleScrollEvent(UIScrollPhaseEnded, CGPointMake(100, 100), CGVectorMake(0, 0));
     EXPECT_FALSE(wasHandled);
+
+    // preventDefault() on the first event with non-zero deltas, and some subsequent events.
+    // In this case, the begin event has zero delta, and is not dispatched to the page, so the
+    // first non-zero scroll event is actually the first preventable one.
+    [webView stringByEvaluatingJavaScript:@"window.preventDefaultOnScrollEvents = true;"];
+    synchronouslyHandleScrollEvent(UIScrollPhaseMayBegin, CGPointMake(100, 100), CGVectorMake(0, 0));
+    EXPECT_FALSE(wasHandled);
+    synchronouslyHandleScrollEvent(UIScrollPhaseBegan, CGPointMake(100, 100), CGVectorMake(0, 0));
+    EXPECT_FALSE(wasHandled);
+    synchronouslyHandleScrollEvent(UIScrollPhaseChanged, CGPointMake(100, 100), CGVectorMake(0, 10));
+    EXPECT_TRUE(wasHandled);
+    synchronouslyHandleScrollEvent(UIScrollPhaseChanged, CGPointMake(100, 100), CGVectorMake(0, 10));
+    EXPECT_TRUE(wasHandled);
+    [webView stringByEvaluatingJavaScript:@"window.preventDefaultOnScrollEvents = false;"];
+    synchronouslyHandleScrollEvent(UIScrollPhaseChanged, CGPointMake(100, 100), CGVectorMake(0, 10));
+    EXPECT_FALSE(wasHandled);
+    synchronouslyHandleScrollEvent(UIScrollPhaseEnded, CGPointMake(100, 100), CGVectorMake(0, 0));
+    EXPECT_FALSE(wasHandled);
 }
 #endif // HAVE(UISCROLLVIEW_ASYNCHRONOUS_SCROLL_EVENT_HANDLING)
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to