Share more code between iOS and macOS ViewGestureController
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 8 Oct 2016 08:31:00 +0000 (08:31 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 8 Oct 2016 08:31:00 +0000 (08:31 +0000)
https://bugs.webkit.org/show_bug.cgi?id=163158

Reviewed by Simon Fraser.

Share canSwipeInDirection() and the (unused on Mac) alternate back-forward list mechanism.
Make ViewGestureController operate in terms of WebPageProxy, not WKWebView,
because it shouldn't know anything about WKWebView.
Refactor scrollEventCanBecomeSwipe a bit to be less repetitive.

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView setAllowsBackForwardNavigationGestures:]):
* UIProcess/Cocoa/ViewGestureController.cpp:
(WebKit::ViewGestureController::ViewGestureController):
(WebKit::ViewGestureController::setAlternateBackForwardListSourcePage):
(WebKit::ViewGestureController::canSwipeInDirection):
(WebKit::ViewGestureController::gestureControllerForPage): Deleted.
* UIProcess/Cocoa/ViewGestureController.h:
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::WebPageProxy):
* UIProcess/WebPageProxy.h:
(WebKit::WebPageProxy::createWeakPtr):
* UIProcess/ios/ViewGestureControllerIOS.mm:
(WebKit::ViewGestureController::beginSwipeGesture):
(WebKit::ViewGestureController::setAlternateBackForwardListSourceView): Deleted.
(WebKit::ViewGestureController::canSwipeInDirection): Deleted.
* UIProcess/mac/ViewGestureControllerMac.mm:
(WebKit::ViewGestureController::PendingSwipeTracker::PendingSwipeTracker):
(WebKit::ViewGestureController::PendingSwipeTracker::scrollEventCanBecomeSwipe):
(WebKit::ViewGestureController::PendingSwipeTracker::tryToStartSwipe):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@206950 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit2/UIProcess/Cocoa/ViewGestureController.cpp
Source/WebKit2/UIProcess/Cocoa/ViewGestureController.h
Source/WebKit2/UIProcess/WebPageProxy.cpp
Source/WebKit2/UIProcess/WebPageProxy.h
Source/WebKit2/UIProcess/ios/ViewGestureControllerIOS.mm
Source/WebKit2/UIProcess/mac/ViewGestureControllerMac.mm

index 9b0c31b..eafbae2 100644 (file)
@@ -1,3 +1,36 @@
+2016-10-08  Tim Horton  <timothy_horton@apple.com>
+
+        Share more code between iOS and macOS ViewGestureController
+        https://bugs.webkit.org/show_bug.cgi?id=163158
+
+        Reviewed by Simon Fraser.
+
+        Share canSwipeInDirection() and the (unused on Mac) alternate back-forward list mechanism.
+        Make ViewGestureController operate in terms of WebPageProxy, not WKWebView,
+        because it shouldn't know anything about WKWebView.
+        Refactor scrollEventCanBecomeSwipe a bit to be less repetitive.
+
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (-[WKWebView setAllowsBackForwardNavigationGestures:]):
+        * UIProcess/Cocoa/ViewGestureController.cpp:
+        (WebKit::ViewGestureController::ViewGestureController):
+        (WebKit::ViewGestureController::setAlternateBackForwardListSourcePage):
+        (WebKit::ViewGestureController::canSwipeInDirection):
+        (WebKit::ViewGestureController::gestureControllerForPage): Deleted.
+        * UIProcess/Cocoa/ViewGestureController.h:
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::WebPageProxy):
+        * UIProcess/WebPageProxy.h:
+        (WebKit::WebPageProxy::createWeakPtr):
+        * UIProcess/ios/ViewGestureControllerIOS.mm:
+        (WebKit::ViewGestureController::beginSwipeGesture):
+        (WebKit::ViewGestureController::setAlternateBackForwardListSourceView): Deleted.
+        (WebKit::ViewGestureController::canSwipeInDirection): Deleted.
+        * UIProcess/mac/ViewGestureControllerMac.mm:
+        (WebKit::ViewGestureController::PendingSwipeTracker::PendingSwipeTracker):
+        (WebKit::ViewGestureController::PendingSwipeTracker::scrollEventCanBecomeSwipe):
+        (WebKit::ViewGestureController::PendingSwipeTracker::tryToStartSwipe):
+
 2016-10-07  Anders Carlsson  <andersca@apple.com>
 
         Get rid of API::Session and WKSessionRef
index 7df0abc..265b297 100644 (file)
@@ -2198,7 +2198,7 @@ static bool scrollViewCanScroll(UIScrollView *scrollView)
         if (!_gestureController) {
             _gestureController = std::make_unique<WebKit::ViewGestureController>(*_page);
             _gestureController->installSwipeHandler(self, [self scrollView]);
-            _gestureController->setAlternateBackForwardListSourceView([_configuration _alternateWebViewForNavigationGestures]);
+            _gestureController->setAlternateBackForwardListSourcePage([_configuration _alternateWebViewForNavigationGestures]->_page.get());
         }
     } else
         _gestureController = nullptr;
index 856cca7..49f18e3 100644 (file)
@@ -29,6 +29,7 @@
 #import "Logging.h"
 #import "RemoteLayerTreeDrawingAreaProxy.h"
 #import "ViewGestureControllerMessages.h"
+#import "WebBackForwardList.h"
 #import "WebPageProxy.h"
 #import "WebProcessProxy.h"
 #import <wtf/MathExtras.h>
@@ -59,7 +60,7 @@ ViewGestureController::ViewGestureController(WebPageProxy& webPageProxy)
     : m_webPageProxy(webPageProxy)
     , m_swipeActiveLoadMonitoringTimer(RunLoop::main(), this, &ViewGestureController::checkForActiveLoads)
 #if PLATFORM(MAC)
-    , m_pendingSwipeTracker(webPageProxy, std::bind(&ViewGestureController::trackSwipeGesture, this, std::placeholders::_1, std::placeholders::_2))
+    , m_pendingSwipeTracker(webPageProxy, *this)
 #endif
 {
     m_webPageProxy.process().addMessageReceiver(Messages::ViewGestureController::messageReceiverName(), m_webPageProxy.pageID(), *this);
@@ -83,6 +84,24 @@ ViewGestureController* ViewGestureController::gestureControllerForPage(uint64_t
         return nullptr;
     return gestureControllerIter->value;
 }
+    
+void ViewGestureController::setAlternateBackForwardListSourcePage(WebPageProxy* page)
+{
+    if (page)
+        m_alternateBackForwardListSourcePage = page->createWeakPtr();
+    else
+        m_alternateBackForwardListSourcePage.clear();
+}
+    
+bool ViewGestureController::canSwipeInDirection(SwipeDirection direction) const
+{
+    RefPtr<WebPageProxy> alternateBackForwardListSourcePage = m_alternateBackForwardListSourcePage.get();
+    auto& backForwardList = alternateBackForwardListSourcePage ? alternateBackForwardListSourcePage->backForwardList() : m_webPageProxy.backForwardList();
+    if (direction == SwipeDirection::Back)
+        return !!backForwardList.backItem();
+    return !!backForwardList.forwardItem();
+}
+
 
 void ViewGestureController::didFirstVisuallyNonEmptyLayoutForMainFrame()
 {
index 1657b1e..758d735 100644 (file)
 
 #include "MessageReceiver.h"
 #include "SameDocumentNavigationType.h"
-#include "WeakObjCPtr.h"
 #include <WebCore/Color.h>
 #include <WebCore/FloatRect.h>
 #include <chrono>
 #include <wtf/BlockPtr.h>
 #include <wtf/RetainPtr.h>
 #include <wtf/RunLoop.h>
-
-// FIXME: Move this file out of the mac/ subdirectory.
+#include <wtf/WeakPtr.h>
 
 OBJC_CLASS CALayer;
 
 #if PLATFORM(IOS)
 OBJC_CLASS UIView;
 OBJC_CLASS WKSwipeTransitionController;
-OBJC_CLASS WKWebView;
 OBJC_CLASS _UINavigationInteractiveTransitionBase;
 OBJC_CLASS _UIViewControllerOneToOneTransitionContext;
 OBJC_CLASS _UIViewControllerTransitionContext;
@@ -109,13 +106,15 @@ public:
     bool isPhysicallySwipingLeft(SwipeDirection) const;
 #else
     void installSwipeHandler(UIView *gestureRecognizerView, UIView *swipingView);
-    void setAlternateBackForwardListSourceView(WKWebView *);
-    bool canSwipeInDirection(SwipeDirection);
     void beginSwipeGesture(_UINavigationInteractiveTransitionBase *, SwipeDirection);
     void endSwipeGesture(WebBackForwardListItem* targetItem, _UIViewControllerTransitionContext *, bool cancelled);
     void willCommitPostSwipeTransitionLayerTree(bool);
     void setRenderTreeSize(uint64_t);
 #endif
+    
+    void setAlternateBackForwardListSourcePage(WebPageProxy*);
+    
+    bool canSwipeInDirection(SwipeDirection) const;
 
     WebCore::Color backgroundColorForCurrentSnapshot() const { return m_backgroundColorForCurrentSnapshot; }
 
@@ -202,7 +201,7 @@ private:
 
     class PendingSwipeTracker {
     public:
-        PendingSwipeTracker(WebPageProxy&, std::function<void(NSEvent *, SwipeDirection)> trackSwipeCallback);
+        PendingSwipeTracker(WebPageProxy&, ViewGestureController&);
         bool handleEvent(NSEvent *);
         void eventWasNotHandledByWebCore(NSEvent *);
 
@@ -227,7 +226,7 @@ private:
 
         bool m_shouldIgnorePinnedState { false };
 
-        std::function<void(NSEvent *, SwipeDirection)> m_trackSwipeCallback;
+        ViewGestureController& m_viewGestureController;
         WebPageProxy& m_webPageProxy;
     };
 #endif
@@ -238,6 +237,9 @@ private:
     RunLoop::Timer<ViewGestureController> m_swipeActiveLoadMonitoringTimer;
 
     WebCore::Color m_backgroundColorForCurrentSnapshot;
+    
+    WeakPtr<WebPageProxy> m_alternateBackForwardListSourcePage;
+    RefPtr<WebPageProxy> m_webPageProxyForBackForwardListForCurrentSwipe;
 
 #if PLATFORM(MAC)
     RefPtr<ViewSnapshot> m_currentSwipeSnapshot;
@@ -277,8 +279,6 @@ private:
     RetainPtr<WKSwipeTransitionController> m_swipeInteractiveTransitionDelegate;
     RetainPtr<_UIViewControllerOneToOneTransitionContext> m_swipeTransitionContext;
     uint64_t m_snapshotRemovalTargetRenderTreeSize { 0 };
-    WeakObjCPtr<WKWebView> m_alternateBackForwardListSourceView;
-    RefPtr<WebPageProxy> m_webPageProxyForBackForwardListForCurrentSwipe;
     uint64_t m_gesturePendingSnapshotRemoval { 0 };
 #endif
 
index 2799315..764c6d3 100644 (file)
@@ -450,6 +450,7 @@ WebPageProxy::WebPageProxy(PageClient& pageClient, WebProcessProxy& process, uin
     , m_configurationPreferenceValues(m_configuration->preferenceValues())
     , m_potentiallyChangedViewStateFlags(ViewState::NoFlags)
     , m_viewStateChangeWantsSynchronousReply(false)
+    , m_weakPtrFactory(this)
 {
     m_webProcessLifetimeTracker.addObserver(m_visitedLinkStore);
     m_webProcessLifetimeTracker.addObserver(m_websiteDataStore);
index 3d5adee..5203ed8 100644 (file)
@@ -1129,6 +1129,8 @@ public:
 #if ENABLE(GAMEPAD)
     void gamepadActivity(const Vector<GamepadData>&);
 #endif
+        
+    WeakPtr<WebPageProxy> createWeakPtr() const { return m_weakPtrFactory.createWeakPtr(); }
 
 private:
     WebPageProxy(PageClient&, WebProcessProxy&, uint64_t pageID, Ref<API::PageConfiguration>&&);
@@ -1889,6 +1891,8 @@ private:
 #if ENABLE(DOWNLOAD_ATTRIBUTE)
     bool m_syncNavigationActionHasDownloadAttribute { false };
 #endif
+        
+    WeakPtrFactory<WebPageProxy> m_weakPtrFactory;
 };
 
 } // namespace WebKit
index 3d8f736..81cfd36 100644 (file)
@@ -34,6 +34,7 @@
 #import "ViewSnapshotStore.h"
 #import "WKBackForwardListItemInternal.h"
 #import "WKWebViewInternal.h"
+#import "WeakObjCPtr.h"
 #import "WebBackForwardList.h"
 #import "WebPageGroup.h"
 #import "WebPageMessages.h"
@@ -140,11 +141,6 @@ void ViewGestureController::platformTeardown()
     [m_swipeInteractiveTransitionDelegate invalidate];
 }
 
-void ViewGestureController::setAlternateBackForwardListSourceView(WKWebView *view)
-{
-    m_alternateBackForwardListSourceView = view;
-}
-
 void ViewGestureController::installSwipeHandler(UIView *gestureRecognizerView, UIView *swipingView)
 {
     ASSERT(!m_swipeInteractiveTransitionDelegate);
@@ -159,7 +155,9 @@ void ViewGestureController::beginSwipeGesture(_UINavigationInteractiveTransition
 
     m_webPageProxy.recordAutomaticNavigationSnapshot();
 
-    m_webPageProxyForBackForwardListForCurrentSwipe = m_alternateBackForwardListSourceView.get() ? m_alternateBackForwardListSourceView.get()->_page : &m_webPageProxy;
+    RefPtr<WebPageProxy> alternateBackForwardListSourcePage = m_alternateBackForwardListSourcePage.get();
+    m_webPageProxyForBackForwardListForCurrentSwipe = alternateBackForwardListSourcePage ? alternateBackForwardListSourcePage.get() : &m_webPageProxy;
+
     m_webPageProxyForBackForwardListForCurrentSwipe->navigationGestureDidBegin();
     if (&m_webPageProxy != m_webPageProxyForBackForwardListForCurrentSwipe)
         m_webPageProxy.navigationGestureDidBegin();
@@ -239,14 +237,6 @@ void ViewGestureController::beginSwipeGesture(_UINavigationInteractiveTransition
     m_activeGestureType = ViewGestureType::Swipe;
 }
 
-bool ViewGestureController::canSwipeInDirection(SwipeDirection direction)
-{
-    auto& backForwardList = m_alternateBackForwardListSourceView.get() ? m_alternateBackForwardListSourceView.get()->_page->backForwardList() : m_webPageProxy.backForwardList();
-    if (direction == SwipeDirection::Back)
-        return !!backForwardList.backItem();
-    return !!backForwardList.forwardItem();
-}
-
 void ViewGestureController::endSwipeGesture(WebBackForwardListItem* targetItem, _UIViewControllerTransitionContext *context, bool cancelled)
 {
     [context _setTransitionIsInFlight:NO];
index c40fbb7..864e2da 100644 (file)
@@ -272,8 +272,8 @@ static bool deltaShouldCancelSwipe(float x, float y)
     return std::abs(y) >= std::abs(x) * minimumScrollEventRatioForSwipe;
 }
 
-ViewGestureController::PendingSwipeTracker::PendingSwipeTracker(WebPageProxy& webPageProxy, std::function<void(NSEvent *, SwipeDirection)> trackSwipeCallback)
-    : m_trackSwipeCallback(WTFMove(trackSwipeCallback))
+ViewGestureController::PendingSwipeTracker::PendingSwipeTracker(WebPageProxy& webPageProxy, ViewGestureController& viewGestureController)
+    : m_viewGestureController(viewGestureController)
     , m_webPageProxy(webPageProxy)
 {
 }
@@ -292,21 +292,16 @@ bool ViewGestureController::PendingSwipeTracker::scrollEventCanBecomeSwipe(NSEve
     bool isPinnedToLeft = m_shouldIgnorePinnedState || m_webPageProxy.isPinnedToLeftSide();
     bool isPinnedToRight = m_shouldIgnorePinnedState || m_webPageProxy.isPinnedToRightSide();
 
-    bool willSwipeBack = false;
-    bool willSwipeForward = false;
-    if (m_webPageProxy.userInterfaceLayoutDirection() == WebCore::UserInterfaceLayoutDirection::LTR) {
-        willSwipeBack = event.scrollingDeltaX > 0 && isPinnedToLeft && m_webPageProxy.backForwardList().backItem();
-        willSwipeForward = event.scrollingDeltaX < 0 && isPinnedToRight && m_webPageProxy.backForwardList().forwardItem();
-    } else {
-        willSwipeBack = event.scrollingDeltaX < 0 && isPinnedToRight && m_webPageProxy.backForwardList().backItem();
-        willSwipeForward = event.scrollingDeltaX > 0 && isPinnedToLeft && m_webPageProxy.backForwardList().forwardItem();
-    }
-    if (!willSwipeBack && !willSwipeForward)
+    bool tryingToSwipeBack = event.scrollingDeltaX > 0 && isPinnedToLeft;
+    bool tryingToSwipeForward = event.scrollingDeltaX < 0 && isPinnedToRight;
+    if (m_webPageProxy.userInterfaceLayoutDirection() != WebCore::UserInterfaceLayoutDirection::LTR)
+        std::swap(tryingToSwipeBack, tryingToSwipeForward);
+    
+    if (!tryingToSwipeBack && !tryingToSwipeForward)
         return false;
 
-    potentialSwipeDirection = willSwipeBack ? ViewGestureController::SwipeDirection::Back : ViewGestureController::SwipeDirection::Forward;
-
-    return true;
+    potentialSwipeDirection = tryingToSwipeBack ? SwipeDirection::Back : SwipeDirection::Forward;
+    return m_viewGestureController.canSwipeInDirection(potentialSwipeDirection);
 }
 
 bool ViewGestureController::handleScrollWheelEvent(NSEvent *event)
@@ -372,7 +367,7 @@ bool ViewGestureController::PendingSwipeTracker::tryToStartSwipe(NSEvent *event)
     }
 
     if (std::abs(m_cumulativeDelta.width()) >= minimumHorizontalSwipeDistance)
-        m_trackSwipeCallback(event, m_direction);
+        m_viewGestureController.trackSwipeGesture(event, m_direction);
     else
         m_state = State::InsufficientMagnitude;