Make WebCore::IOSurface have single ownership instead of refcounting
[WebKit-https.git] / Source / WebKit2 / UIProcess / mac / ViewGestureController.h
1 /*
2  * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef ViewGestureController_h
27 #define ViewGestureController_h
28
29 #include "MessageReceiver.h"
30 #include "SameDocumentNavigationType.h"
31 #include "WeakObjCPtr.h"
32 #include <WebCore/FloatRect.h>
33 #include <wtf/RetainPtr.h>
34 #include <wtf/RunLoop.h>
35
36 OBJC_CLASS CALayer;
37
38 #if PLATFORM(IOS)
39 OBJC_CLASS UIView;
40 OBJC_CLASS WKSwipeTransitionController;
41 OBJC_CLASS WKWebView;
42 OBJC_CLASS _UINavigationInteractiveTransitionBase;
43 OBJC_CLASS _UIViewControllerOneToOneTransitionContext;
44 OBJC_CLASS _UIViewControllerTransitionContext;
45 #else
46 OBJC_CLASS NSEvent;
47 OBJC_CLASS NSView;
48 OBJC_CLASS WKSwipeCancellationTracker;
49 #endif
50
51 namespace WebCore {
52 class IOSurface;
53 }
54
55 namespace WebKit {
56
57 class ViewSnapshot;
58 class WebBackForwardListItem;
59 class WebPageProxy;
60
61 class ViewGestureController : private IPC::MessageReceiver {
62     WTF_MAKE_NONCOPYABLE(ViewGestureController);
63 public:
64     ViewGestureController(WebPageProxy&);
65     ~ViewGestureController();
66     
67     enum class ViewGestureType {
68         None,
69 #if PLATFORM(MAC)
70         Magnification,
71         SmartMagnification,
72 #endif
73         Swipe
74     };
75     
76     enum class SwipeTransitionStyle {
77         Overlap,
78         Push
79     };
80     
81     enum class SwipeDirection {
82         Left,
83         Right
84     };
85
86     enum class PendingSwipeReason {
87         None,
88         WebCoreMayScroll,
89         InsufficientMagnitude
90     };
91     
92 #if PLATFORM(MAC)
93     double magnification() const;
94
95     void handleMagnificationGesture(double scale, WebCore::FloatPoint origin);
96     void handleSmartMagnificationGesture(WebCore::FloatPoint origin);
97
98     bool handleScrollWheelEvent(NSEvent *);
99     void wheelEventWasNotHandledByWebCore(NSEvent *);
100
101     void setCustomSwipeViews(Vector<RetainPtr<NSView>> views) { m_customSwipeViews = WTF::move(views); }
102     void setCustomSwipeViewsTopContentInset(float topContentInset) { m_customSwipeViewsTopContentInset = topContentInset; }
103     WebCore::FloatRect windowRelativeBoundsForCustomSwipeViews() const;
104     void setDidMoveSwipeSnapshotCallback(void(^)(CGRect));
105
106     void endActiveGesture();
107
108     bool shouldIgnorePinnedState() { return m_shouldIgnorePinnedState; }
109     void setShouldIgnorePinnedState(bool ignore) { m_shouldIgnorePinnedState = ignore; }
110
111     void didFirstVisuallyNonEmptyLayoutForMainFrame();
112     void didFinishLoadForMainFrame();
113     void didSameDocumentNavigationForMainFrame(SameDocumentNavigationType);
114 #else
115     void installSwipeHandler(UIView *gestureRecognizerView, UIView *swipingView);
116     void setAlternateBackForwardListSourceView(WKWebView *);
117     bool canSwipeInDirection(SwipeDirection);
118     void beginSwipeGesture(_UINavigationInteractiveTransitionBase *, SwipeDirection);
119     void endSwipeGesture(WebBackForwardListItem* targetItem, _UIViewControllerTransitionContext *, bool cancelled);
120     void willCommitPostSwipeTransitionLayerTree(bool);
121     void setRenderTreeSize(uint64_t);
122 #endif
123
124     void removeSwipeSnapshot();
125
126 private:
127     // IPC::MessageReceiver.
128     virtual void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
129
130     void swipeSnapshotWatchdogTimerFired();
131
132 #if PLATFORM(MAC)
133     // Message handlers.
134     void didCollectGeometryForMagnificationGesture(WebCore::FloatRect visibleContentBounds, bool frameHandlesMagnificationGesture);
135     void didCollectGeometryForSmartMagnificationGesture(WebCore::FloatPoint origin, WebCore::FloatRect renderRect, WebCore::FloatRect visibleContentBounds, bool isReplacedElement, double viewportMinimumScale, double viewportMaximumScale);
136     void didHitRenderTreeSizeThreshold();
137     void removeSwipeSnapshotAfterRepaint();
138     void activeLoadMonitoringTimerFired();
139
140     void endMagnificationGesture();
141     WebCore::FloatPoint scaledMagnificationOrigin(WebCore::FloatPoint origin, double scale);
142
143     void trackSwipeGesture(NSEvent *, SwipeDirection);
144     void beginSwipeGesture(WebBackForwardListItem* targetItem, SwipeDirection);
145     void handleSwipeGesture(WebBackForwardListItem* targetItem, double progress, SwipeDirection);
146     void endSwipeGesture(WebBackForwardListItem* targetItem, bool cancelled);
147     bool deltaIsSufficientToBeginSwipe(NSEvent *);
148     bool scrollEventCanBecomeSwipe(NSEvent *, SwipeDirection&);
149     bool shouldUseSnapshotForSize(ViewSnapshot&, WebCore::FloatSize swipeLayerSize, float topContentInset);
150
151     CALayer *determineSnapshotLayerParent() const;
152     CALayer *determineLayerAdjacentToSnapshotForParent(SwipeDirection, CALayer *snapshotLayerParent) const;
153     void applyDebuggingPropertiesToSwipeViews();
154     void didMoveSwipeSnapshotLayer();
155 #endif
156
157     WebPageProxy& m_webPageProxy;
158     ViewGestureType m_activeGestureType;
159
160     RunLoop::Timer<ViewGestureController> m_swipeWatchdogTimer;
161
162 #if USE(IOSURFACE)
163     RefPtr<ViewSnapshot> m_currentSwipeSnapshot;
164 #endif
165
166 #if PLATFORM(MAC)
167     RunLoop::Timer<ViewGestureController> m_swipeWatchdogAfterFirstVisuallyNonEmptyLayoutTimer;
168     RunLoop::Timer<ViewGestureController> m_swipeActiveLoadMonitoringTimer;
169
170     double m_magnification;
171     WebCore::FloatPoint m_magnificationOrigin;
172
173     WebCore::FloatRect m_lastSmartMagnificationUnscaledTargetRect;
174     bool m_lastMagnificationGestureWasSmartMagnification;
175     WebCore::FloatPoint m_lastSmartMagnificationOrigin;
176
177     WebCore::FloatRect m_visibleContentRect;
178     bool m_visibleContentRectIsValid;
179     bool m_frameHandlesMagnificationGesture;
180
181     RetainPtr<WKSwipeCancellationTracker> m_swipeCancellationTracker;
182     RetainPtr<CALayer> m_swipeLayer;
183     RetainPtr<CALayer> m_swipeSnapshotLayer;
184     Vector<RetainPtr<CALayer>> m_currentSwipeLiveLayers;
185
186     SwipeTransitionStyle m_swipeTransitionStyle;
187     Vector<RetainPtr<NSView>> m_customSwipeViews;
188     float m_customSwipeViewsTopContentInset;
189     WebCore::FloatRect m_currentSwipeCustomViewBounds;
190
191     // If we need to wait for content to decide if it is going to consume
192     // the scroll event that would have started a swipe, we'll fill these in.
193     PendingSwipeReason m_pendingSwipeReason;
194     SwipeDirection m_pendingSwipeDirection;
195     WebCore::FloatSize m_cumulativeDeltaForPendingSwipe;
196
197     void (^m_didMoveSwipeSnapshotCallback)(CGRect);
198
199     bool m_shouldIgnorePinnedState;
200
201     bool m_swipeWaitingForVisuallyNonEmptyLayout;
202     bool m_swipeWaitingForRenderTreeSizeThreshold;
203     bool m_swipeWaitingForRepaint;
204     bool m_swipeInProgress;
205 #else    
206     UIView *m_liveSwipeView;
207     RetainPtr<UIView> m_liveSwipeViewClippingView;
208     RetainPtr<UIView> m_snapshotView;
209     RetainPtr<UIView> m_transitionContainerView;
210     RetainPtr<WKSwipeTransitionController> m_swipeInteractiveTransitionDelegate;
211     RetainPtr<_UIViewControllerOneToOneTransitionContext> m_swipeTransitionContext;
212     uint64_t m_snapshotRemovalTargetRenderTreeSize;
213     bool m_shouldRemoveSnapshotWhenTargetRenderTreeSizeHit;
214     WeakObjCPtr<WKWebView> m_alternateBackForwardListSourceView;
215     RefPtr<WebPageProxy> m_webPageProxyForBackForwardListForCurrentSwipe;
216     uint64_t m_gesturePendingSnapshotRemoval;
217 #endif
218 };
219
220 } // namespace WebKit
221
222 #endif // ViewGestureController_h