Occasional crashes (null deref) under ViewGestureController::endMagnificationGesture
[WebKit-https.git] / Source / WebKit2 / UIProcess / mac / ViewGestureControllerMac.mm
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 #import "config.h"
27 #import "ViewGestureController.h"
28
29 #if PLATFORM(MAC)
30
31 #import "NativeWebWheelEvent.h"
32 #import "WebPageGroup.h"
33 #import "ViewGestureControllerMessages.h"
34 #import "ViewGestureGeometryCollectorMessages.h"
35 #import "ViewSnapshotStore.h"
36 #import "WebBackForwardList.h"
37 #import "WebPageMessages.h"
38 #import "WebPageProxy.h"
39 #import "WebPreferences.h"
40 #import "WebProcessProxy.h"
41 #import <Cocoa/Cocoa.h>
42 #import <QuartzCore/QuartzCore.h>
43 #import <WebCore/IOSurface.h>
44 #import <WebCore/WebActionDisablingCALayerDelegate.h>
45
46 #if defined(__has_include) && __has_include(<QuartzCore/QuartzCorePrivate.h>)
47 #import <QuartzCore/QuartzCorePrivate.h>
48 #else
49 @interface CAFilter : NSObject <NSCopying, NSMutableCopying, NSCoding>
50 @end
51 #endif
52
53 @interface CAFilter (Details)
54 + (CAFilter *)filterWithType:(NSString *)type;
55 @end
56
57 extern NSString * const kCAFilterColorInvert;
58
59 using namespace WebCore;
60
61 static const double minMagnification = 1;
62 static const double maxMagnification = 3;
63
64 static const double minElasticMagnification = 0.75;
65 static const double maxElasticMagnification = 4;
66
67 static const double zoomOutBoost = 1.6;
68 static const double zoomOutResistance = 0.10;
69
70 static const float smartMagnificationElementPadding = 0.05;
71 static const float smartMagnificationPanScrollThreshold = 100;
72
73 static const double swipeOverlayShadowOpacity = 0.66;
74 static const double swipeOverlayShadowRadius = 3;
75
76 static const CGFloat minimumHorizontalSwipeDistance = 15;
77 static const float minimumScrollEventRatioForSwipe = 0.5;
78
79 static const float swipeSnapshotRemovalRenderTreeSizeTargetFraction = 0.5;
80 static const std::chrono::seconds swipeSnapshotRemovalWatchdogDuration = 5_s;
81 static const std::chrono::seconds swipeSnapshotRemovalWatchdogAfterFirstVisuallyNonEmptyLayoutDuration = 3_s;
82
83 @interface WKSwipeCancellationTracker : NSObject {
84 @private
85     BOOL _isCancelled;
86 }
87
88 @property (nonatomic) BOOL isCancelled;
89
90 @end
91
92 @implementation WKSwipeCancellationTracker
93 @synthesize isCancelled=_isCancelled;
94 @end
95
96 namespace WebKit {
97
98 ViewGestureController::ViewGestureController(WebPageProxy& webPageProxy)
99     : m_webPageProxy(webPageProxy)
100     , m_activeGestureType(ViewGestureType::None)
101     , m_swipeWatchdogTimer(RunLoop::main(), this, &ViewGestureController::swipeSnapshotWatchdogTimerFired)
102     , m_swipeWatchdogAfterFirstVisuallyNonEmptyLayoutTimer(RunLoop::main(), this, &ViewGestureController::swipeSnapshotWatchdogTimerFired)
103     , m_lastMagnificationGestureWasSmartMagnification(false)
104     , m_visibleContentRectIsValid(false)
105     , m_frameHandlesMagnificationGesture(false)
106     , m_swipeTransitionStyle(SwipeTransitionStyle::Overlap)
107     , m_customSwipeViewsTopContentInset(0)
108     , m_pendingSwipeReason(PendingSwipeReason::None)
109     , m_didMoveSwipeSnapshotCallback(nullptr)
110     , m_shouldIgnorePinnedState(false)
111     , m_swipeWaitingForVisuallyNonEmptyLayout(false)
112     , m_swipeWaitingForRenderTreeSizeThreshold(false)
113     , m_swipeWaitingForRepaint(false)
114     , m_swipeInProgress(false)
115 {
116     m_webPageProxy.process().addMessageReceiver(Messages::ViewGestureController::messageReceiverName(), m_webPageProxy.pageID(), *this);
117 }
118
119 ViewGestureController::~ViewGestureController()
120 {
121     if (m_swipeCancellationTracker)
122         [m_swipeCancellationTracker setIsCancelled:YES];
123
124     if (m_activeGestureType == ViewGestureType::Swipe)
125         removeSwipeSnapshot();
126
127     if (m_didMoveSwipeSnapshotCallback) {
128         Block_release(m_didMoveSwipeSnapshotCallback);
129         m_didMoveSwipeSnapshotCallback = nullptr;
130     }
131
132     m_webPageProxy.process().removeMessageReceiver(Messages::ViewGestureController::messageReceiverName(), m_webPageProxy.pageID());
133 }
134
135 static double resistanceForDelta(double deltaScale, double currentScale)
136 {
137     // Zoom out with slight acceleration, until we reach minimum scale.
138     if (deltaScale < 0 && currentScale > minMagnification)
139         return zoomOutBoost;
140
141     // Zoom in with no acceleration, until we reach maximum scale.
142     if (deltaScale > 0 && currentScale < maxMagnification)
143         return 1;
144
145     // Outside of the extremes, resist further scaling.
146     double limit = currentScale < minMagnification ? minMagnification : maxMagnification;
147     double scaleDistance = fabs(limit - currentScale);
148     double scalePercent = std::min(std::max(scaleDistance / limit, 0.), 1.);
149     double resistance = zoomOutResistance + scalePercent * (0.01 - zoomOutResistance);
150
151     return resistance;
152 }
153
154 FloatPoint ViewGestureController::scaledMagnificationOrigin(FloatPoint origin, double scale)
155 {
156     FloatPoint scaledMagnificationOrigin(origin);
157     scaledMagnificationOrigin.moveBy(m_visibleContentRect.location());
158     float magnificationOriginScale = 1 - (scale / m_webPageProxy.pageScaleFactor());
159     scaledMagnificationOrigin.scale(magnificationOriginScale, magnificationOriginScale);
160     return scaledMagnificationOrigin;
161 }
162
163 void ViewGestureController::didCollectGeometryForMagnificationGesture(FloatRect visibleContentRect, bool frameHandlesMagnificationGesture)
164 {
165     m_activeGestureType = ViewGestureType::Magnification;
166     m_visibleContentRect = visibleContentRect;
167     m_visibleContentRectIsValid = true;
168     m_frameHandlesMagnificationGesture = frameHandlesMagnificationGesture;
169 }
170
171 void ViewGestureController::handleMagnificationGesture(double scale, FloatPoint origin)
172 {
173     ASSERT(m_activeGestureType == ViewGestureType::None || m_activeGestureType == ViewGestureType::Magnification);
174
175     if (m_activeGestureType == ViewGestureType::None) {
176         // FIXME: We drop the first frame of the gesture on the floor, because we don't have the visible content bounds yet.
177         m_magnification = m_webPageProxy.pageScaleFactor();
178         m_webPageProxy.process().send(Messages::ViewGestureGeometryCollector::CollectGeometryForMagnificationGesture(), m_webPageProxy.pageID());
179         m_lastMagnificationGestureWasSmartMagnification = false;
180
181         return;
182     }
183
184     // We're still waiting for the DidCollectGeometry callback.
185     if (!m_visibleContentRectIsValid)
186         return;
187
188     m_activeGestureType = ViewGestureType::Magnification;
189
190     double scaleWithResistance = resistanceForDelta(scale, m_magnification) * scale;
191
192     m_magnification += m_magnification * scaleWithResistance;
193     m_magnification = std::min(std::max(m_magnification, minElasticMagnification), maxElasticMagnification);
194
195     m_magnificationOrigin = origin;
196
197     if (m_frameHandlesMagnificationGesture)
198         m_webPageProxy.scalePage(m_magnification, roundedIntPoint(origin));
199     else
200         m_webPageProxy.drawingArea()->adjustTransientZoom(m_magnification, scaledMagnificationOrigin(origin, m_magnification));
201 }
202
203 void ViewGestureController::endMagnificationGesture()
204 {
205     ASSERT(m_activeGestureType == ViewGestureType::Magnification);
206
207     double newMagnification = std::min(std::max(m_magnification, minMagnification), maxMagnification);
208
209     if (m_frameHandlesMagnificationGesture)
210         m_webPageProxy.scalePage(newMagnification, roundedIntPoint(m_magnificationOrigin));
211     else {
212         if (auto drawingArea = m_webPageProxy.drawingArea())
213             drawingArea->commitTransientZoom(newMagnification, scaledMagnificationOrigin(m_magnificationOrigin, newMagnification));
214     }
215
216     m_activeGestureType = ViewGestureType::None;
217 }
218
219 void ViewGestureController::handleSmartMagnificationGesture(FloatPoint origin)
220 {
221     if (m_activeGestureType != ViewGestureType::None)
222         return;
223
224     m_webPageProxy.process().send(Messages::ViewGestureGeometryCollector::CollectGeometryForSmartMagnificationGesture(origin), m_webPageProxy.pageID());
225 }
226
227 static float maximumRectangleComponentDelta(FloatRect a, FloatRect b)
228 {
229     return std::max(fabs(a.x() - b.x()), std::max(fabs(a.y() - b.y()), std::max(fabs(a.width() - b.width()), fabs(a.height() - b.height()))));
230 }
231
232 void ViewGestureController::didCollectGeometryForSmartMagnificationGesture(FloatPoint origin, FloatRect renderRect, FloatRect visibleContentRect, bool isReplacedElement, double viewportMinimumScale, double viewportMaximumScale)
233 {
234     double currentScaleFactor = m_webPageProxy.pageScaleFactor();
235
236     FloatRect unscaledTargetRect = renderRect;
237
238     // If there was no usable element under the cursor, we'll scale towards the cursor instead.
239     if (unscaledTargetRect.isEmpty())
240         unscaledTargetRect.setLocation(origin);
241
242     unscaledTargetRect.scale(1 / currentScaleFactor);
243     unscaledTargetRect.inflateX(unscaledTargetRect.width() * smartMagnificationElementPadding);
244     unscaledTargetRect.inflateY(unscaledTargetRect.height() * smartMagnificationElementPadding);
245
246     double targetMagnification = visibleContentRect.width() / unscaledTargetRect.width();
247
248     // For replaced elements like images, we want to fit the whole element
249     // in the view, so scale it down enough to make both dimensions fit if possible.
250     if (isReplacedElement)
251         targetMagnification = std::min(targetMagnification, static_cast<double>(visibleContentRect.height() / unscaledTargetRect.height()));
252
253     targetMagnification = std::min(std::max(targetMagnification, minMagnification), maxMagnification);
254
255     // Allow panning between elements via double-tap while magnified, unless the target rect is
256     // similar to the last one or the mouse has not moved, in which case we'll zoom all the way out.
257     if (currentScaleFactor > 1 && m_lastMagnificationGestureWasSmartMagnification) {
258         if (maximumRectangleComponentDelta(m_lastSmartMagnificationUnscaledTargetRect, unscaledTargetRect) < smartMagnificationPanScrollThreshold)
259             targetMagnification = 1;
260
261         if (m_lastSmartMagnificationOrigin == origin)
262             targetMagnification = 1;
263     }
264
265     FloatRect targetRect(unscaledTargetRect);
266     targetRect.scale(targetMagnification);
267     FloatPoint targetOrigin(visibleContentRect.center());
268     targetOrigin.moveBy(-targetRect.center());
269
270     m_webPageProxy.drawingArea()->adjustTransientZoom(m_webPageProxy.pageScaleFactor(), scaledMagnificationOrigin(FloatPoint(), m_webPageProxy.pageScaleFactor()));
271     m_webPageProxy.drawingArea()->commitTransientZoom(targetMagnification, targetOrigin);
272
273     m_lastSmartMagnificationUnscaledTargetRect = unscaledTargetRect;
274     m_lastSmartMagnificationOrigin = origin;
275
276     m_lastMagnificationGestureWasSmartMagnification = true;
277 }
278
279 bool ViewGestureController::scrollEventCanBecomeSwipe(NSEvent *event, ViewGestureController::SwipeDirection& potentialSwipeDirection)
280 {
281     if (event.phase != NSEventPhaseBegan)
282         return false;
283
284     if (!event.hasPreciseScrollingDeltas)
285         return false;
286
287     if (![NSEvent isSwipeTrackingFromScrollEventsEnabled])
288         return false;
289
290     if (fabs(event.scrollingDeltaX) <= fabs(event.scrollingDeltaY))
291         return false;
292
293     bool isPinnedToLeft = m_shouldIgnorePinnedState || m_webPageProxy.isPinnedToLeftSide();
294     bool isPinnedToRight = m_shouldIgnorePinnedState || m_webPageProxy.isPinnedToRightSide();
295
296     bool willSwipeLeft = event.scrollingDeltaX > 0 && isPinnedToLeft && m_webPageProxy.backForwardList().backItem();
297     bool willSwipeRight = event.scrollingDeltaX < 0 && isPinnedToRight && m_webPageProxy.backForwardList().forwardItem();
298     if (!willSwipeLeft && !willSwipeRight)
299         return false;
300
301     potentialSwipeDirection = willSwipeLeft ? ViewGestureController::SwipeDirection::Left : ViewGestureController::SwipeDirection::Right;
302
303     return true;
304 }
305
306 bool ViewGestureController::deltaIsSufficientToBeginSwipe(NSEvent *event)
307 {
308     if (m_pendingSwipeReason != PendingSwipeReason::InsufficientMagnitude)
309         return false;
310
311     m_cumulativeDeltaForPendingSwipe += FloatSize(event.scrollingDeltaX, event.scrollingDeltaY);
312
313     // If the cumulative delta is ever "too vertical", we will stop tracking this
314     // as a potential swipe until we get another "begin" event.
315     if (fabs(m_cumulativeDeltaForPendingSwipe.height()) >= fabs(m_cumulativeDeltaForPendingSwipe.width()) * minimumScrollEventRatioForSwipe) {
316         m_pendingSwipeReason = PendingSwipeReason::None;
317         return false;
318     }
319
320     if (fabs(m_cumulativeDeltaForPendingSwipe.width()) < minimumHorizontalSwipeDistance)
321         return false;
322
323     return true;
324 }
325
326 void ViewGestureController::setDidMoveSwipeSnapshotCallback(void(^callback)(CGRect))
327 {
328     if (m_didMoveSwipeSnapshotCallback)
329         Block_release(m_didMoveSwipeSnapshotCallback);
330     m_didMoveSwipeSnapshotCallback = Block_copy(callback);
331 }
332
333 bool ViewGestureController::handleScrollWheelEvent(NSEvent *event)
334 {
335     if (event.phase == NSEventPhaseEnded) {
336         m_cumulativeDeltaForPendingSwipe = FloatSize();
337         m_pendingSwipeReason = PendingSwipeReason::None;
338     }
339
340     if (m_pendingSwipeReason == PendingSwipeReason::InsufficientMagnitude) {
341         if (deltaIsSufficientToBeginSwipe(event)) {
342             trackSwipeGesture(event, m_pendingSwipeDirection);
343             return true;
344         }
345     }
346
347     if (m_activeGestureType != ViewGestureType::None)
348         return false;
349
350     SwipeDirection direction;
351     if (!scrollEventCanBecomeSwipe(event, direction))
352         return false;
353
354     if (!m_shouldIgnorePinnedState && m_webPageProxy.willHandleHorizontalScrollEvents()) {
355         m_pendingSwipeReason = PendingSwipeReason::WebCoreMayScroll;
356         m_pendingSwipeDirection = direction;
357         return false;
358     }
359
360     if (!deltaIsSufficientToBeginSwipe(event)) {
361         m_pendingSwipeReason = PendingSwipeReason::InsufficientMagnitude;
362         m_pendingSwipeDirection = direction;
363         return true;
364     }
365
366     trackSwipeGesture(event, direction);
367
368     return true;
369 }
370
371 void ViewGestureController::wheelEventWasNotHandledByWebCore(NSEvent *event)
372 {
373     if (m_pendingSwipeReason != PendingSwipeReason::WebCoreMayScroll)
374         return;
375
376     m_pendingSwipeReason = PendingSwipeReason::None;
377
378     SwipeDirection direction;
379     if (!scrollEventCanBecomeSwipe(event, direction))
380         return;
381
382     if (!deltaIsSufficientToBeginSwipe(event)) {
383         m_pendingSwipeReason = PendingSwipeReason::InsufficientMagnitude;
384         return;
385     }
386
387     trackSwipeGesture(event, m_pendingSwipeDirection);
388 }
389
390 void ViewGestureController::trackSwipeGesture(NSEvent *event, SwipeDirection direction)
391 {
392     ASSERT(m_activeGestureType == ViewGestureType::None);
393     m_pendingSwipeReason = PendingSwipeReason::None;
394
395     m_webPageProxy.recordNavigationSnapshot();
396
397     CGFloat maxProgress = (direction == SwipeDirection::Left) ? 1 : 0;
398     CGFloat minProgress = (direction == SwipeDirection::Right) ? -1 : 0;
399     RefPtr<WebBackForwardListItem> targetItem = (direction == SwipeDirection::Left) ? m_webPageProxy.backForwardList().backItem() : m_webPageProxy.backForwardList().forwardItem();
400     __block bool swipeCancelled = false;
401
402     ASSERT(!m_swipeCancellationTracker);
403     RetainPtr<WKSwipeCancellationTracker> swipeCancellationTracker = adoptNS([[WKSwipeCancellationTracker alloc] init]);
404     m_swipeCancellationTracker = swipeCancellationTracker;
405
406     [event trackSwipeEventWithOptions:0 dampenAmountThresholdMin:minProgress max:maxProgress usingHandler:^(CGFloat progress, NSEventPhase phase, BOOL isComplete, BOOL *stop) {
407         if ([swipeCancellationTracker isCancelled]) {
408             *stop = YES;
409             return;
410         }
411         if (phase == NSEventPhaseBegan)
412             this->beginSwipeGesture(targetItem.get(), direction);
413         CGFloat clampedProgress = std::min(std::max(progress, minProgress), maxProgress);
414         this->handleSwipeGesture(targetItem.get(), clampedProgress, direction);
415         if (phase == NSEventPhaseCancelled)
416             swipeCancelled = true;
417         if (isComplete)
418             this->endSwipeGesture(targetItem.get(), swipeCancelled);
419     }];
420 }
421
422 FloatRect ViewGestureController::windowRelativeBoundsForCustomSwipeViews() const
423 {
424     FloatRect swipeArea;
425     for (const auto& view : m_customSwipeViews)
426         swipeArea.unite([view convertRect:[view bounds] toView:nil]);
427     swipeArea.setHeight(swipeArea.height() - m_customSwipeViewsTopContentInset);
428     return swipeArea;
429 }
430
431 static CALayer *leastCommonAncestorLayer(const Vector<RetainPtr<CALayer>>& layers)
432 {
433     Vector<Vector<CALayer *>> liveLayerPathsFromRoot(layers.size());
434
435     size_t shortestPathLength = std::numeric_limits<size_t>::max();
436
437     for (size_t layerIndex = 0; layerIndex < layers.size(); layerIndex++) {
438         CALayer *parent = [layers[layerIndex] superlayer];
439         while (parent) {
440             liveLayerPathsFromRoot[layerIndex].insert(0, parent);
441             shortestPathLength = std::min(shortestPathLength, liveLayerPathsFromRoot[layerIndex].size());
442             parent = parent.superlayer;
443         }
444     }
445
446     for (size_t pathIndex = 0; pathIndex < shortestPathLength; pathIndex++) {
447         CALayer *firstPathLayer = liveLayerPathsFromRoot[0][pathIndex];
448         for (size_t layerIndex = 1; layerIndex < layers.size(); layerIndex++) {
449             if (liveLayerPathsFromRoot[layerIndex][pathIndex] != firstPathLayer)
450                 return firstPathLayer;
451         }
452     }
453
454     return liveLayerPathsFromRoot[0][shortestPathLength];
455 }
456
457 CALayer *ViewGestureController::determineSnapshotLayerParent() const
458 {
459     if (m_currentSwipeLiveLayers.size() == 1)
460         return [m_currentSwipeLiveLayers[0] superlayer];
461
462     // We insert our snapshot into the first shared superlayer of the custom views' layer, above the frontmost or below the bottommost layer.
463     return leastCommonAncestorLayer(m_currentSwipeLiveLayers);
464 }
465
466 CALayer *ViewGestureController::determineLayerAdjacentToSnapshotForParent(SwipeDirection direction, CALayer *snapshotLayerParent) const
467 {
468     // If we have custom swiping views, we assume that the views were passed to us in back-to-front z-order.
469     CALayer *layerAdjacentToSnapshot = direction == SwipeDirection::Left ? m_currentSwipeLiveLayers.first().get() : m_currentSwipeLiveLayers.last().get();
470
471     if (m_currentSwipeLiveLayers.size() == 1)
472         return layerAdjacentToSnapshot;
473
474     // If the layers are not all siblings, find the child of the layer we're going to insert the snapshot into which has the frontmost/bottommost layer as a child.
475     while (snapshotLayerParent != layerAdjacentToSnapshot.superlayer)
476         layerAdjacentToSnapshot = layerAdjacentToSnapshot.superlayer;
477     return layerAdjacentToSnapshot;
478 }
479
480 bool ViewGestureController::shouldUseSnapshotForSize(ViewSnapshot& snapshot, FloatSize swipeLayerSize, float topContentInset)
481 {
482     float deviceScaleFactor = m_webPageProxy.deviceScaleFactor();
483     if (snapshot.deviceScaleFactor() != deviceScaleFactor)
484         return false;
485
486     FloatSize unobscuredSwipeLayerSizeInDeviceCoordinates = swipeLayerSize - FloatSize(0, topContentInset);
487     unobscuredSwipeLayerSizeInDeviceCoordinates.scale(deviceScaleFactor);
488     if (snapshot.size() != unobscuredSwipeLayerSizeInDeviceCoordinates)
489         return false;
490
491     return true;
492 }
493
494 static bool layerGeometryFlippedToRoot(CALayer *layer)
495 {
496     bool flipped = false;
497     CALayer *parent = layer;
498     while (parent) {
499         if (parent.isGeometryFlipped)
500             flipped = !flipped;
501         parent = parent.superlayer;
502     }
503     return flipped;
504 }
505
506 void ViewGestureController::applyDebuggingPropertiesToSwipeViews()
507 {
508 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
509     CAFilter* filter = [CAFilter filterWithType:kCAFilterColorInvert];
510     [m_swipeLayer setFilters:@[ filter ]];
511 #endif
512     [m_swipeLayer setBackgroundColor:[NSColor blueColor].CGColor];
513     [m_swipeLayer setBorderColor:[NSColor yellowColor].CGColor];
514     [m_swipeLayer setBorderWidth:4];
515
516     [m_swipeSnapshotLayer setBackgroundColor:[NSColor greenColor].CGColor];
517     [m_swipeSnapshotLayer setBorderColor:[NSColor redColor].CGColor];
518     [m_swipeSnapshotLayer setBorderWidth:2];
519 }
520
521 void ViewGestureController::beginSwipeGesture(WebBackForwardListItem* targetItem, SwipeDirection direction)
522 {
523     ASSERT(m_currentSwipeLiveLayers.isEmpty());
524
525     m_webPageProxy.navigationGestureDidBegin();
526
527     m_activeGestureType = ViewGestureType::Swipe;
528     m_swipeInProgress = true;
529
530     CALayer *rootContentLayer = m_webPageProxy.acceleratedCompositingRootLayer();
531
532     m_swipeLayer = adoptNS([[CALayer alloc] init]);
533     m_swipeSnapshotLayer = adoptNS([[CALayer alloc] init]);
534     m_currentSwipeCustomViewBounds = windowRelativeBoundsForCustomSwipeViews();
535
536     FloatRect swipeArea;
537     float topContentInset = 0;
538     if (!m_customSwipeViews.isEmpty()) {
539         topContentInset = m_customSwipeViewsTopContentInset;
540         swipeArea = m_currentSwipeCustomViewBounds;
541         swipeArea.expand(0, topContentInset);
542
543         for (const auto& view : m_customSwipeViews) {
544             CALayer *layer = [view layer];
545             ASSERT(layer);
546             m_currentSwipeLiveLayers.append(layer);
547         }
548     } else {
549         swipeArea = FloatRect(FloatPoint(), m_webPageProxy.viewSize());
550         topContentInset = m_webPageProxy.topContentInset();
551         m_currentSwipeLiveLayers.append(rootContentLayer);
552     }
553
554     CALayer *snapshotLayerParent = determineSnapshotLayerParent();
555     bool geometryIsFlippedToRoot = layerGeometryFlippedToRoot(snapshotLayerParent);
556
557     RetainPtr<CGColorRef> backgroundColor = CGColorGetConstantColor(kCGColorWhite);
558     if (ViewSnapshot* snapshot = targetItem->snapshot()) {
559         if (shouldUseSnapshotForSize(*snapshot, swipeArea.size(), topContentInset))
560             [m_swipeSnapshotLayer setContents:snapshot->asLayerContents()];
561
562         Color coreColor = snapshot->backgroundColor();
563         if (coreColor.isValid())
564             backgroundColor = cachedCGColor(coreColor, ColorSpaceDeviceRGB);
565 #if USE_IOSURFACE_VIEW_SNAPSHOTS
566         m_currentSwipeSnapshotSurface = snapshot->surface();
567 #endif
568     }
569
570     [m_swipeLayer setBackgroundColor:backgroundColor.get()];
571     [m_swipeLayer setAnchorPoint:CGPointZero];
572     [m_swipeLayer setFrame:swipeArea];
573     [m_swipeLayer setName:@"Gesture Swipe Root Layer"];
574     [m_swipeLayer setGeometryFlipped:geometryIsFlippedToRoot];
575     [m_swipeLayer setDelegate:[WebActionDisablingCALayerDelegate shared]];
576
577     [m_swipeSnapshotLayer setContentsGravity:kCAGravityTopLeft];
578     [m_swipeSnapshotLayer setContentsScale:m_webPageProxy.deviceScaleFactor()];
579     [m_swipeSnapshotLayer setAnchorPoint:CGPointZero];
580     [m_swipeSnapshotLayer setFrame:CGRectMake(0, 0, swipeArea.width(), swipeArea.height() - topContentInset)];
581     [m_swipeSnapshotLayer setName:@"Gesture Swipe Snapshot Layer"];
582     [m_swipeSnapshotLayer setDelegate:[WebActionDisablingCALayerDelegate shared]];
583
584     [m_swipeLayer addSublayer:m_swipeSnapshotLayer.get()];
585
586     if (m_webPageProxy.preferences().viewGestureDebuggingEnabled())
587         applyDebuggingPropertiesToSwipeViews();
588
589     // We don't know enough about the custom views' hierarchy to apply a shadow.
590     if (m_swipeTransitionStyle == SwipeTransitionStyle::Overlap && m_customSwipeViews.isEmpty()) {
591         if (direction == SwipeDirection::Left) {
592             float topContentInset = m_webPageProxy.topContentInset();
593             FloatRect shadowRect(FloatPoint(0, topContentInset), m_webPageProxy.viewSize() - FloatSize(0, topContentInset));
594             RetainPtr<CGPathRef> shadowPath = adoptCF(CGPathCreateWithRect(shadowRect, 0));
595             [rootContentLayer setShadowColor:CGColorGetConstantColor(kCGColorBlack)];
596             [rootContentLayer setShadowOpacity:swipeOverlayShadowOpacity];
597             [rootContentLayer setShadowRadius:swipeOverlayShadowRadius];
598             [rootContentLayer setShadowPath:shadowPath.get()];
599         } else {
600             RetainPtr<CGPathRef> shadowPath = adoptCF(CGPathCreateWithRect([m_swipeLayer bounds], 0));
601             [m_swipeLayer setShadowColor:CGColorGetConstantColor(kCGColorBlack)];
602             [m_swipeLayer setShadowOpacity:swipeOverlayShadowOpacity];
603             [m_swipeLayer setShadowRadius:swipeOverlayShadowRadius];
604             [m_swipeLayer setShadowPath:shadowPath.get()];
605         }
606     }
607
608     CALayer *layerAdjacentToSnapshot = determineLayerAdjacentToSnapshotForParent(direction, snapshotLayerParent);
609     if (direction == SwipeDirection::Left)
610         [snapshotLayerParent insertSublayer:m_swipeLayer.get() below:layerAdjacentToSnapshot];
611     else
612         [snapshotLayerParent insertSublayer:m_swipeLayer.get() above:layerAdjacentToSnapshot];
613 }
614
615 void ViewGestureController::handleSwipeGesture(WebBackForwardListItem* targetItem, double progress, SwipeDirection direction)
616 {
617     ASSERT(m_activeGestureType == ViewGestureType::Swipe);
618
619     if (!m_webPageProxy.drawingArea())
620         return;
621
622     double width;
623     if (!m_customSwipeViews.isEmpty())
624         width = m_currentSwipeCustomViewBounds.width();
625     else
626         width = m_webPageProxy.drawingArea()->size().width();
627
628     double swipingLayerOffset = floor(width * progress);
629
630     if (m_swipeTransitionStyle == SwipeTransitionStyle::Overlap) {
631         if (direction == SwipeDirection::Right) {
632             [m_swipeLayer setTransform:CATransform3DMakeTranslation(width + swipingLayerOffset, 0, 0)];
633             didMoveSwipeSnapshotLayer();
634         }
635     } else if (m_swipeTransitionStyle == SwipeTransitionStyle::Push)
636         [m_swipeLayer setTransform:CATransform3DMakeTranslation((direction == SwipeDirection::Left ? -width : width) + swipingLayerOffset, 0, 0)];
637
638     for (const auto& layer : m_currentSwipeLiveLayers) {
639         if (m_swipeTransitionStyle == SwipeTransitionStyle::Overlap) {
640             if (direction == SwipeDirection::Left)
641                 [layer setTransform:CATransform3DMakeTranslation(swipingLayerOffset, 0, 0)];
642         } else if (m_swipeTransitionStyle == SwipeTransitionStyle::Push)
643             [layer setTransform:CATransform3DMakeTranslation(swipingLayerOffset, 0, 0)];
644     }
645 }
646
647 void ViewGestureController::didMoveSwipeSnapshotLayer()
648 {
649     if (!m_didMoveSwipeSnapshotCallback)
650         return;
651
652     m_didMoveSwipeSnapshotCallback(m_webPageProxy.boundsOfLayerInLayerBackedWindowCoordinates(m_swipeLayer.get()));
653 }
654
655 void ViewGestureController::endSwipeGesture(WebBackForwardListItem* targetItem, bool cancelled)
656 {
657     ASSERT(m_activeGestureType == ViewGestureType::Swipe);
658
659     m_swipeCancellationTracker = nullptr;
660
661     m_swipeInProgress = false;
662
663     CALayer *rootLayer = m_webPageProxy.acceleratedCompositingRootLayer();
664
665     [rootLayer setShadowOpacity:0];
666     [rootLayer setShadowRadius:0];
667
668     if (cancelled) {
669         removeSwipeSnapshot();
670         m_webPageProxy.navigationGestureDidEnd(false, *targetItem);
671         return;
672     }
673
674     uint64_t renderTreeSize = 0;
675     if (ViewSnapshot* snapshot = targetItem->snapshot())
676         renderTreeSize = snapshot->renderTreeSize();
677
678     m_webPageProxy.process().send(Messages::ViewGestureGeometryCollector::SetRenderTreeSizeNotificationThreshold(renderTreeSize * swipeSnapshotRemovalRenderTreeSizeTargetFraction), m_webPageProxy.pageID());
679
680     m_swipeWaitingForVisuallyNonEmptyLayout = true;
681     m_swipeWaitingForRenderTreeSizeThreshold = true;
682
683     m_webPageProxy.navigationGestureDidEnd(true, *targetItem);
684     m_webPageProxy.goToBackForwardItem(targetItem);
685
686     m_swipeWatchdogTimer.startOneShot(swipeSnapshotRemovalWatchdogDuration.count());
687 }
688
689 void ViewGestureController::didHitRenderTreeSizeThreshold()
690 {
691     if (m_activeGestureType != ViewGestureType::Swipe || m_swipeInProgress)
692         return;
693
694     m_swipeWaitingForRenderTreeSizeThreshold = false;
695
696     if (!m_swipeWaitingForVisuallyNonEmptyLayout)
697         removeSwipeSnapshotAfterRepaint();
698 }
699
700 void ViewGestureController::didFirstVisuallyNonEmptyLayoutForMainFrame()
701 {
702     if (m_activeGestureType != ViewGestureType::Swipe || m_swipeInProgress)
703         return;
704
705     m_swipeWaitingForVisuallyNonEmptyLayout = false;
706
707     if (!m_swipeWaitingForRenderTreeSizeThreshold)
708         removeSwipeSnapshotAfterRepaint();
709     else {
710         m_swipeWatchdogAfterFirstVisuallyNonEmptyLayoutTimer.startOneShot(swipeSnapshotRemovalWatchdogAfterFirstVisuallyNonEmptyLayoutDuration.count());
711         m_swipeWatchdogTimer.stop();
712     }
713 }
714
715 void ViewGestureController::didFinishLoadForMainFrame()
716 {
717     removeSwipeSnapshotAfterRepaint();
718 }
719
720 void ViewGestureController::didSameDocumentNavigationForMainFrame(SameDocumentNavigationType type)
721 {
722     if (type != SameDocumentNavigationSessionStateReplace && type != SameDocumentNavigationSessionStatePop)
723         return;
724
725     removeSwipeSnapshotAfterRepaint();
726 }
727
728 void ViewGestureController::swipeSnapshotWatchdogTimerFired()
729 {
730     removeSwipeSnapshotAfterRepaint();
731 }
732
733 void ViewGestureController::removeSwipeSnapshotAfterRepaint()
734 {
735     if (m_activeGestureType != ViewGestureType::Swipe || m_swipeInProgress)
736         return;
737
738     if (m_swipeWaitingForRepaint)
739         return;
740
741     m_swipeWaitingForRepaint = true;
742
743     WebPageProxy* webPageProxy = &m_webPageProxy;
744     m_webPageProxy.forceRepaint(VoidCallback::create([webPageProxy] (CallbackBase::Error error) {
745         webPageProxy->removeNavigationGestureSnapshot();
746     }));
747 }
748
749 void ViewGestureController::removeSwipeSnapshot()
750 {
751     m_swipeWaitingForRepaint = false;
752
753     m_swipeWatchdogTimer.stop();
754     m_swipeWatchdogAfterFirstVisuallyNonEmptyLayoutTimer.stop();
755
756     if (m_activeGestureType != ViewGestureType::Swipe)
757         return;
758
759 #if USE_IOSURFACE_VIEW_SNAPSHOTS
760     if (m_currentSwipeSnapshotSurface)
761         m_currentSwipeSnapshotSurface->setIsVolatile(true);
762     m_currentSwipeSnapshotSurface = nullptr;
763 #endif
764
765     for (const auto& layer : m_currentSwipeLiveLayers)
766         [layer setTransform:CATransform3DIdentity];
767
768     [m_swipeSnapshotLayer removeFromSuperlayer];
769     m_swipeSnapshotLayer = nullptr;
770
771     [m_swipeLayer removeFromSuperlayer];
772     m_swipeLayer = nullptr;
773
774     m_currentSwipeLiveLayers.clear();
775
776     m_activeGestureType = ViewGestureType::None;
777
778     m_webPageProxy.navigationGestureSnapshotWasRemoved();
779 }
780
781 void ViewGestureController::endActiveGesture()
782 {
783     if (m_activeGestureType == ViewGestureType::Magnification) {
784         endMagnificationGesture();
785         m_visibleContentRectIsValid = false;
786     }
787 }
788
789 double ViewGestureController::magnification() const
790 {
791     if (m_activeGestureType == ViewGestureType::Magnification)
792         return m_magnification;
793
794     return m_webPageProxy.pageScaleFactor();
795 }
796
797 } // namespace WebKit
798
799 #endif // !PLATFORM(IOS)