afccd1689bd596e01087a1c7d66a91b538c90b95
[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/Color.h>
33 #include <WebCore/FloatRect.h>
34 #include <chrono>
35 #include <wtf/RetainPtr.h>
36 #include <wtf/RunLoop.h>
37
38 // FIXME: Move this file out of the mac/ subdirectory.
39
40 OBJC_CLASS CALayer;
41
42 #if PLATFORM(IOS)
43 OBJC_CLASS UIView;
44 OBJC_CLASS WKSwipeTransitionController;
45 OBJC_CLASS WKWebView;
46 OBJC_CLASS _UINavigationInteractiveTransitionBase;
47 OBJC_CLASS _UIViewControllerOneToOneTransitionContext;
48 OBJC_CLASS _UIViewControllerTransitionContext;
49 #else
50 OBJC_CLASS CAGradientLayer;
51 OBJC_CLASS NSEvent;
52 OBJC_CLASS NSView;
53 OBJC_CLASS WKSwipeCancellationTracker;
54 #endif
55
56 namespace WebCore {
57 class IOSurface;
58 }
59
60 namespace WebKit {
61
62 class ViewSnapshot;
63 class WebBackForwardListItem;
64 class WebPageProxy;
65
66 class ViewGestureController : private IPC::MessageReceiver {
67     WTF_MAKE_NONCOPYABLE(ViewGestureController);
68 public:
69     ViewGestureController(WebPageProxy&);
70     ~ViewGestureController();
71     void platformTeardown();
72     
73     enum class ViewGestureType {
74         None,
75 #if PLATFORM(MAC)
76         Magnification,
77         SmartMagnification,
78 #endif
79         Swipe
80     };
81     
82     enum class SwipeTransitionStyle {
83         Overlap,
84         Push
85     };
86     
87     enum class SwipeDirection {
88         Back,
89         Forward
90     };
91
92 #if PLATFORM(MAC)
93     double magnification() const;
94
95     void handleMagnificationGesture(double scale, WebCore::FloatPoint origin);
96     void endMagnificationGesture();
97
98     void handleSmartMagnificationGesture(WebCore::FloatPoint origin);
99
100     bool handleScrollWheelEvent(NSEvent *);
101     void wheelEventWasNotHandledByWebCore(NSEvent *event) { m_pendingSwipeTracker.eventWasNotHandledByWebCore(event); }
102
103     void setCustomSwipeViews(Vector<RetainPtr<NSView>> views) { m_customSwipeViews = WTF::move(views); }
104     void setCustomSwipeViewsTopContentInset(float topContentInset) { m_customSwipeViewsTopContentInset = topContentInset; }
105     WebCore::FloatRect windowRelativeBoundsForCustomSwipeViews() const;
106     void setDidMoveSwipeSnapshotCallback(void(^)(CGRect));
107
108     bool shouldIgnorePinnedState() { return m_pendingSwipeTracker.shouldIgnorePinnedState(); }
109     void setShouldIgnorePinnedState(bool ignore) { m_pendingSwipeTracker.setShouldIgnorePinnedState(ignore); }
110 #else
111     void installSwipeHandler(UIView *gestureRecognizerView, UIView *swipingView);
112     void setAlternateBackForwardListSourceView(WKWebView *);
113     bool canSwipeInDirection(SwipeDirection);
114     void beginSwipeGesture(_UINavigationInteractiveTransitionBase *, SwipeDirection);
115     void endSwipeGesture(WebBackForwardListItem* targetItem, _UIViewControllerTransitionContext *, bool cancelled);
116     void willCommitPostSwipeTransitionLayerTree(bool);
117     void setRenderTreeSize(uint64_t);
118 #endif
119
120     WebCore::Color backgroundColorForCurrentSnapshot() const { return m_backgroundColorForCurrentSnapshot; }
121
122     void didFinishLoadForMainFrame() { didReachMainFrameLoadTerminalState(); }
123     void didFailLoadForMainFrame() { didReachMainFrameLoadTerminalState(); }
124     void didFirstVisuallyNonEmptyLayoutForMainFrame();
125     void didRepaintAfterNavigation();
126     void didHitRenderTreeSizeThreshold();
127     void didRestoreScrollPosition();
128     void didReachMainFrameLoadTerminalState();
129     void didSameDocumentNavigationForMainFrame(SameDocumentNavigationType);
130
131     void checkForActiveLoads();
132
133     void removeSwipeSnapshot();
134
135 private:
136     // IPC::MessageReceiver.
137     virtual void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
138
139     static ViewGestureController* gestureControllerForPage(uint64_t);
140
141     class SnapshotRemovalTracker {
142     public:
143         enum Event : uint8_t {
144             VisuallyNonEmptyLayout = 1 << 0,
145             RenderTreeSizeThreshold = 1 << 1,
146             RepaintAfterNavigation = 1 << 2,
147             MainFrameLoad = 1 << 3,
148             SubresourceLoads = 1 << 4,
149             ScrollPositionRestoration = 1 << 5
150         };
151         typedef uint8_t Events;
152
153         SnapshotRemovalTracker();
154
155         void start(Events, std::function<void()>);
156         void reset();
157
158         bool eventOccurred(Events);
159         bool cancelOutstandingEvent(Events);
160
161         void startWatchdog(std::chrono::seconds);
162
163     private:
164         static String eventsDescription(Events);
165         void log(const String&) const;
166
167         void fireRemovalCallbackImmediately();
168         void fireRemovalCallbackIfPossible();
169         void watchdogTimerFired();
170
171         bool stopWaitingForEvent(Events, const String& logReason);
172
173         Events m_outstandingEvents { 0 };
174         std::function<void()> m_removalCallback;
175         std::chrono::steady_clock::time_point m_startTime;
176
177         RunLoop::Timer<SnapshotRemovalTracker> m_watchdogTimer;
178     };
179
180 #if PLATFORM(MAC)
181     // Message handlers.
182     void didCollectGeometryForMagnificationGesture(WebCore::FloatRect visibleContentBounds, bool frameHandlesMagnificationGesture);
183     void didCollectGeometryForSmartMagnificationGesture(WebCore::FloatPoint origin, WebCore::FloatRect renderRect, WebCore::FloatRect visibleContentBounds, bool isReplacedElement, double viewportMinimumScale, double viewportMaximumScale);
184
185     WebCore::FloatPoint scaledMagnificationOrigin(WebCore::FloatPoint origin, double scale);
186
187     void trackSwipeGesture(NSEvent *, SwipeDirection);
188     void beginSwipeGesture(WebBackForwardListItem* targetItem, SwipeDirection);
189     void handleSwipeGesture(WebBackForwardListItem* targetItem, double progress, SwipeDirection);
190     void willEndSwipeGesture(WebBackForwardListItem& targetItem, bool cancelled);
191     void endSwipeGesture(WebBackForwardListItem* targetItem, bool cancelled);
192     bool shouldUseSnapshotForSize(ViewSnapshot&, WebCore::FloatSize swipeLayerSize, float topContentInset);
193
194     CALayer *determineSnapshotLayerParent() const;
195     CALayer *determineLayerAdjacentToSnapshotForParent(SwipeDirection, CALayer *snapshotLayerParent) const;
196     void applyDebuggingPropertiesToSwipeViews();
197     void didMoveSwipeSnapshotLayer();
198
199     void forceRepaintIfNeeded();
200
201     class PendingSwipeTracker {
202     public:
203         PendingSwipeTracker(WebPageProxy&, std::function<void(NSEvent *, SwipeDirection)> trackSwipeCallback);
204         bool handleEvent(NSEvent *);
205         void eventWasNotHandledByWebCore(NSEvent *);
206
207         void reset(const char* resetReasonForLogging);
208
209         bool shouldIgnorePinnedState() { return m_shouldIgnorePinnedState; }
210         void setShouldIgnorePinnedState(bool ignore) { m_shouldIgnorePinnedState = ignore; }
211
212     private:
213         bool tryToStartSwipe(NSEvent *);
214         bool scrollEventCanBecomeSwipe(NSEvent *, SwipeDirection&);
215
216         enum class State {
217             None,
218             WaitingForWebCore,
219             InsufficientMagnitude
220         };
221
222         State m_state { State::None };
223         SwipeDirection m_direction;
224         WebCore::FloatSize m_cumulativeDelta;
225
226         bool m_shouldIgnorePinnedState { false };
227
228         std::function<void(NSEvent *, SwipeDirection)> m_trackSwipeCallback;
229         WebPageProxy& m_webPageProxy;
230     };
231 #endif
232
233     WebPageProxy& m_webPageProxy;
234     ViewGestureType m_activeGestureType { ViewGestureType::None };
235
236     RunLoop::Timer<ViewGestureController> m_swipeActiveLoadMonitoringTimer;
237
238     WebCore::Color m_backgroundColorForCurrentSnapshot;
239
240 #if PLATFORM(MAC)
241     RefPtr<ViewSnapshot> m_currentSwipeSnapshot;
242
243     double m_magnification;
244     WebCore::FloatPoint m_magnificationOrigin;
245
246     WebCore::FloatRect m_lastSmartMagnificationUnscaledTargetRect;
247     bool m_lastMagnificationGestureWasSmartMagnification { false };
248     WebCore::FloatPoint m_lastSmartMagnificationOrigin;
249
250     WebCore::FloatRect m_visibleContentRect;
251     bool m_visibleContentRectIsValid { false };
252     bool m_frameHandlesMagnificationGesture { false };
253
254     RetainPtr<WKSwipeCancellationTracker> m_swipeCancellationTracker;
255     RetainPtr<CALayer> m_swipeLayer;
256     RetainPtr<CALayer> m_swipeSnapshotLayer;
257     RetainPtr<CAGradientLayer> m_swipeShadowLayer;
258     RetainPtr<CALayer> m_swipeDimmingLayer;
259     Vector<RetainPtr<CALayer>> m_currentSwipeLiveLayers;
260
261     SwipeTransitionStyle m_swipeTransitionStyle { SwipeTransitionStyle::Overlap };
262     Vector<RetainPtr<NSView>> m_customSwipeViews;
263     float m_customSwipeViewsTopContentInset { 0 };
264     WebCore::FloatRect m_currentSwipeCustomViewBounds;
265
266     PendingSwipeTracker m_pendingSwipeTracker;
267
268     void (^m_didMoveSwipeSnapshotCallback)(CGRect) { nullptr };
269
270     bool m_hasOutstandingRepaintRequest { false };
271 #else    
272     UIView *m_liveSwipeView { nullptr };
273     RetainPtr<UIView> m_liveSwipeViewClippingView;
274     RetainPtr<UIView> m_snapshotView;
275     RetainPtr<UIView> m_transitionContainerView;
276     RetainPtr<WKSwipeTransitionController> m_swipeInteractiveTransitionDelegate;
277     RetainPtr<_UIViewControllerOneToOneTransitionContext> m_swipeTransitionContext;
278     uint64_t m_snapshotRemovalTargetRenderTreeSize { 0 };
279     WeakObjCPtr<WKWebView> m_alternateBackForwardListSourceView;
280     RefPtr<WebPageProxy> m_webPageProxyForBackForwardListForCurrentSwipe;
281     uint64_t m_gesturePendingSnapshotRemoval { 0 };
282 #endif
283
284     SnapshotRemovalTracker m_snapshotRemovalTracker;
285 };
286
287 } // namespace WebKit
288
289 #endif // ViewGestureController_h