iOS: Crash in InteractiveUpdateHandler set by ViewGestureController::beginSwipeGesture
[WebKit-https.git] / Source / WebKit / UIProcess / Cocoa / 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 <WebCore/Color.h>
32 #include <WebCore/FloatRect.h>
33 #include <wtf/BlockPtr.h>
34 #include <wtf/MonotonicTime.h>
35 #include <wtf/RetainPtr.h>
36 #include <wtf/RunLoop.h>
37 #include <wtf/WeakPtr.h>
38
39 OBJC_CLASS CALayer;
40
41 #if PLATFORM(IOS_FAMILY)
42 OBJC_CLASS UIGestureRecognizer;
43 OBJC_CLASS UIView;
44 OBJC_CLASS WKSwipeTransitionController;
45 OBJC_CLASS _UINavigationInteractiveTransitionBase;
46 OBJC_CLASS _UIViewControllerOneToOneTransitionContext;
47 OBJC_CLASS _UIViewControllerTransitionContext;
48 #else
49 OBJC_CLASS CAGradientLayer;
50 OBJC_CLASS NSEvent;
51 OBJC_CLASS NSView;
52 OBJC_CLASS WKSwipeCancellationTracker;
53 #endif
54
55 namespace WebCore {
56 class IOSurface;
57 }
58
59 namespace WebKit {
60
61 class ViewSnapshot;
62 class WebBackForwardListItem;
63 class WebPageProxy;
64 class WebProcessProxy;
65
66 class ViewGestureController : private IPC::MessageReceiver {
67     WTF_MAKE_NONCOPYABLE(ViewGestureController);
68 public:
69     ViewGestureController(WebPageProxy&);
70     ~ViewGestureController();
71     void platformTeardown();
72
73     void disconnectFromProcess();
74     void connectToProcess();
75     
76     enum class ViewGestureType {
77         None,
78 #if PLATFORM(MAC)
79         Magnification,
80         SmartMagnification,
81 #endif
82         Swipe
83     };
84
85     enum class SwipeDirection {
86         Back,
87         Forward
88     };
89
90     typedef uint64_t GestureID;
91
92 #if PLATFORM(MAC)
93     double magnification() const;
94
95     void handleMagnificationGestureEvent(NSEvent *, WebCore::FloatPoint origin);
96
97     bool hasActiveMagnificationGesture() const { return m_activeGestureType == ViewGestureType::Magnification; }
98
99     void handleSmartMagnificationGesture(WebCore::FloatPoint origin);
100
101     bool handleScrollWheelEvent(NSEvent *);
102     void wheelEventWasNotHandledByWebCore(NSEvent *event) { m_pendingSwipeTracker.eventWasNotHandledByWebCore(event); }
103     void gestureEventWasNotHandledByWebCore(NSEvent *, WebCore::FloatPoint origin);
104
105     void setCustomSwipeViews(Vector<RetainPtr<NSView>> views) { m_customSwipeViews = WTFMove(views); }
106     void setCustomSwipeViewsTopContentInset(float topContentInset) { m_customSwipeViewsTopContentInset = topContentInset; }
107     WebCore::FloatRect windowRelativeBoundsForCustomSwipeViews() const;
108     void setDidMoveSwipeSnapshotCallback(BlockPtr<void (CGRect)>&& callback) { m_didMoveSwipeSnapshotCallback = WTFMove(callback); }
109
110     bool shouldIgnorePinnedState() { return m_pendingSwipeTracker.shouldIgnorePinnedState(); }
111     void setShouldIgnorePinnedState(bool ignore) { m_pendingSwipeTracker.setShouldIgnorePinnedState(ignore); }
112
113     bool isPhysicallySwipingLeft(SwipeDirection) const;
114 #else
115     bool isNavigationSwipeGestureRecognizer(UIGestureRecognizer *) const;
116     void installSwipeHandler(UIView *gestureRecognizerView, UIView *swipingView);
117     void beginSwipeGesture(_UINavigationInteractiveTransitionBase *, SwipeDirection);
118     void endSwipeGesture(WebBackForwardListItem* targetItem, _UIViewControllerTransitionContext *, bool cancelled);
119     void willCommitPostSwipeTransitionLayerTree(bool);
120     void setRenderTreeSize(uint64_t);
121 #endif
122     
123     void setAlternateBackForwardListSourcePage(WebPageProxy*);
124     
125     bool canSwipeInDirection(SwipeDirection) const;
126
127     WebCore::Color backgroundColorForCurrentSnapshot() const { return m_backgroundColorForCurrentSnapshot; }
128
129     void didStartProvisionalLoadForMainFrame();
130     void didFinishLoadForMainFrame() { didReachMainFrameLoadTerminalState(); }
131     void didFailLoadForMainFrame() { didReachMainFrameLoadTerminalState(); }
132     void didFirstVisuallyNonEmptyLayoutForMainFrame();
133     void didRepaintAfterNavigation();
134     void didHitRenderTreeSizeThreshold();
135     void didRestoreScrollPosition();
136     void didReachMainFrameLoadTerminalState();
137     void didSameDocumentNavigationForMainFrame(SameDocumentNavigationType);
138
139     void checkForActiveLoads();
140
141     void removeSwipeSnapshot();
142
143     void setSwipeGestureEnabled(bool enabled) { m_swipeGestureEnabled = enabled; }
144     bool isSwipeGestureEnabled() { return m_swipeGestureEnabled; }
145
146     // Testing
147     bool beginSimulatedSwipeInDirectionForTesting(SwipeDirection);
148     bool completeSimulatedSwipeInDirectionForTesting(SwipeDirection);
149
150 private:
151     // IPC::MessageReceiver.
152     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
153
154     static ViewGestureController* controllerForGesture(uint64_t pageID, GestureID);
155
156     static GestureID takeNextGestureID();
157     void willBeginGesture(ViewGestureType);
158     void didEndGesture();
159
160     void didStartProvisionalOrSameDocumentLoadForMainFrame();
161
162     class SnapshotRemovalTracker {
163     public:
164         enum Event : uint8_t {
165             VisuallyNonEmptyLayout = 1 << 0,
166             RenderTreeSizeThreshold = 1 << 1,
167             RepaintAfterNavigation = 1 << 2,
168             MainFrameLoad = 1 << 3,
169             SubresourceLoads = 1 << 4,
170             ScrollPositionRestoration = 1 << 5
171         };
172         typedef uint8_t Events;
173
174         SnapshotRemovalTracker();
175
176         void start(Events, WTF::Function<void()>&&);
177         void reset();
178         
179         void pause() { m_paused = true; }
180         void resume();
181         bool isPaused() const { return m_paused; }
182         bool hasRemovalCallback() const { return !!m_removalCallback; }
183
184         bool eventOccurred(Events);
185         bool cancelOutstandingEvent(Events);
186         bool hasOutstandingEvent(Event);
187
188         void startWatchdog(Seconds);
189
190         uint64_t renderTreeSizeThreshold() const { return m_renderTreeSizeThreshold; }
191         void setRenderTreeSizeThreshold(uint64_t threshold) { m_renderTreeSizeThreshold = threshold; }
192
193     private:
194         static String eventsDescription(Events);
195         void log(const String&) const;
196
197         void fireRemovalCallbackImmediately();
198         void fireRemovalCallbackIfPossible();
199         void watchdogTimerFired();
200
201         bool stopWaitingForEvent(Events, const String& logReason);
202
203         Events m_outstandingEvents { 0 };
204         WTF::Function<void()> m_removalCallback;
205         MonotonicTime m_startTime;
206
207         uint64_t m_renderTreeSizeThreshold { 0 };
208
209         RunLoop::Timer<SnapshotRemovalTracker> m_watchdogTimer;
210         
211         bool m_paused { true };
212     };
213
214 #if PLATFORM(MAC)
215     // Message handlers.
216     void didCollectGeometryForMagnificationGesture(WebCore::FloatRect visibleContentBounds, bool frameHandlesMagnificationGesture);
217     void didCollectGeometryForSmartMagnificationGesture(WebCore::FloatPoint origin, WebCore::FloatRect renderRect, WebCore::FloatRect visibleContentBounds, bool fitEntireRect, double viewportMinimumScale, double viewportMaximumScale);
218
219     void endMagnificationGesture();
220
221     WebCore::FloatPoint scaledMagnificationOrigin(WebCore::FloatPoint origin, double scale);
222
223     void trackSwipeGesture(NSEvent *, SwipeDirection);
224     void beginSwipeGesture(WebBackForwardListItem* targetItem, SwipeDirection);
225     void handleSwipeGesture(WebBackForwardListItem* targetItem, double progress, SwipeDirection);
226     void willEndSwipeGesture(WebBackForwardListItem& targetItem, bool cancelled);
227     void endSwipeGesture(WebBackForwardListItem* targetItem, bool cancelled);
228     bool shouldUseSnapshotForSize(ViewSnapshot&, WebCore::FloatSize swipeLayerSize, float topContentInset);
229
230     CALayer *determineSnapshotLayerParent() const;
231     CALayer *determineLayerAdjacentToSnapshotForParent(SwipeDirection, CALayer *snapshotLayerParent) const;
232     void applyDebuggingPropertiesToSwipeViews();
233     void didMoveSwipeSnapshotLayer();
234
235     void forceRepaintIfNeeded();
236
237     void requestRenderTreeSizeNotificationIfNeeded();
238
239     class PendingSwipeTracker {
240     public:
241         PendingSwipeTracker(WebPageProxy&, ViewGestureController&);
242         bool handleEvent(NSEvent *);
243         void eventWasNotHandledByWebCore(NSEvent *);
244
245         void reset(const char* resetReasonForLogging);
246
247         bool shouldIgnorePinnedState() { return m_shouldIgnorePinnedState; }
248         void setShouldIgnorePinnedState(bool ignore) { m_shouldIgnorePinnedState = ignore; }
249
250     private:
251         bool tryToStartSwipe(NSEvent *);
252         bool scrollEventCanBecomeSwipe(NSEvent *, SwipeDirection&);
253
254         enum class State {
255             None,
256             WaitingForWebCore,
257             InsufficientMagnitude
258         };
259
260         State m_state { State::None };
261         SwipeDirection m_direction;
262         WebCore::FloatSize m_cumulativeDelta;
263
264         bool m_shouldIgnorePinnedState { false };
265
266         ViewGestureController& m_viewGestureController;
267         WebPageProxy& m_webPageProxy;
268     };
269 #endif
270
271     WebPageProxy& m_webPageProxy;
272     ViewGestureType m_activeGestureType { ViewGestureType::None };
273
274     bool m_swipeGestureEnabled { true };
275
276     RunLoop::Timer<ViewGestureController> m_swipeActiveLoadMonitoringTimer;
277
278     WebCore::Color m_backgroundColorForCurrentSnapshot;
279     
280     WeakPtr<WebPageProxy> m_alternateBackForwardListSourcePage;
281     RefPtr<WebPageProxy> m_webPageProxyForBackForwardListForCurrentSwipe;
282
283     GestureID m_currentGestureID;
284
285 #if PLATFORM(MAC)
286     RefPtr<ViewSnapshot> m_currentSwipeSnapshot;
287
288     double m_magnification;
289     WebCore::FloatPoint m_magnificationOrigin;
290
291     WebCore::FloatRect m_lastSmartMagnificationUnscaledTargetRect;
292     bool m_lastMagnificationGestureWasSmartMagnification { false };
293     WebCore::FloatPoint m_lastSmartMagnificationOrigin;
294
295     WebCore::FloatRect m_visibleContentRect;
296     bool m_visibleContentRectIsValid { false };
297     bool m_frameHandlesMagnificationGesture { false };
298
299     RetainPtr<WKSwipeCancellationTracker> m_swipeCancellationTracker;
300     RetainPtr<CALayer> m_swipeLayer;
301     RetainPtr<CALayer> m_swipeSnapshotLayer;
302     RetainPtr<CAGradientLayer> m_swipeShadowLayer;
303     RetainPtr<CALayer> m_swipeDimmingLayer;
304     Vector<RetainPtr<CALayer>> m_currentSwipeLiveLayers;
305
306     Vector<RetainPtr<NSView>> m_customSwipeViews;
307     float m_customSwipeViewsTopContentInset { 0 };
308     WebCore::FloatRect m_currentSwipeCustomViewBounds;
309
310     PendingSwipeTracker m_pendingSwipeTracker;
311
312     BlockPtr<void (CGRect)> m_didMoveSwipeSnapshotCallback;
313
314     bool m_hasOutstandingRepaintRequest { false };
315 #else    
316     UIView *m_liveSwipeView { nullptr };
317     RetainPtr<UIView> m_liveSwipeViewClippingView;
318     RetainPtr<UIView> m_snapshotView;
319     RetainPtr<UIView> m_transitionContainerView;
320     RetainPtr<WKSwipeTransitionController> m_swipeInteractiveTransitionDelegate;
321     RetainPtr<_UIViewControllerOneToOneTransitionContext> m_swipeTransitionContext;
322     uint64_t m_snapshotRemovalTargetRenderTreeSize { 0 };
323 #endif
324     bool m_isConnectedToProcess { false };
325
326     SnapshotRemovalTracker m_snapshotRemovalTracker;
327     WTF::Function<void()> m_loadCallback;
328 };
329
330 } // namespace WebKit
331
332 #endif // ViewGestureController_h