[iOS WK2] Header bar on nytimes articles lands in the wrong place after rubberbanding
[WebKit-https.git] / Source / WebKit2 / UIProcess / Scrolling / RemoteScrollingCoordinatorProxy.cpp
1 /*
2  * Copyright (C) 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 #include "config.h"
27 #include "RemoteScrollingCoordinatorProxy.h"
28
29 #if ENABLE(ASYNC_SCROLLING)
30
31 #include "ArgumentCoders.h"
32 #include "MessageDecoder.h"
33 #include "MessageEncoder.h"
34 #include "RemoteLayerTreeDrawingAreaProxy.h"
35 #include "RemoteScrollingCoordinator.h"
36 #include "RemoteScrollingCoordinatorMessages.h"
37 #include "RemoteScrollingCoordinatorTransaction.h"
38 #include "WebPageProxy.h"
39 #include "WebProcessProxy.h"
40 #include <WebCore/ScrollingStateFrameScrollingNode.h>
41 #include <WebCore/ScrollingStateOverflowScrollingNode.h>
42 #include <WebCore/ScrollingStateTree.h>
43 #include <WebCore/ScrollingTreeScrollingNode.h>
44
45 using namespace WebCore;
46
47 namespace WebKit {
48
49 RemoteScrollingCoordinatorProxy::RemoteScrollingCoordinatorProxy(WebPageProxy& webPageProxy)
50     : m_webPageProxy(webPageProxy)
51     , m_scrollingTree(RemoteScrollingTree::create(*this))
52     , m_requestedScrollInfo(nullptr)
53     , m_propagatesMainFrameScrolls(true)
54 {
55 }
56
57 RemoteScrollingCoordinatorProxy::~RemoteScrollingCoordinatorProxy()
58 {
59 }
60
61 ScrollingNodeID RemoteScrollingCoordinatorProxy::rootScrollingNodeID() const
62 {
63     if (!m_scrollingTree->rootNode())
64         return 0;
65
66     return m_scrollingTree->rootNode()->scrollingNodeID();
67 }
68
69 const RemoteLayerTreeHost* RemoteScrollingCoordinatorProxy::layerTreeHost() const
70 {
71     DrawingAreaProxy* drawingArea = m_webPageProxy.drawingArea();
72     if (!drawingArea || drawingArea->type() != DrawingAreaTypeRemoteLayerTree) {
73         ASSERT_NOT_REACHED();
74         return nullptr;
75     }
76
77     RemoteLayerTreeDrawingAreaProxy* remoteDrawingArea = toRemoteLayerTreeDrawingAreaProxy(drawingArea);
78     return &remoteDrawingArea->remoteLayerTreeHost();
79 }
80
81 void RemoteScrollingCoordinatorProxy::updateScrollingTree(const RemoteScrollingCoordinatorTransaction& transaction, RequestedScrollInfo& requestedScrollInfo)
82 {
83     m_requestedScrollInfo = &requestedScrollInfo;
84
85     OwnPtr<ScrollingStateTree> stateTree = const_cast<RemoteScrollingCoordinatorTransaction&>(transaction).scrollingStateTree().release();
86     
87     const RemoteLayerTreeHost* layerTreeHost = this->layerTreeHost();
88     if (!layerTreeHost) {
89         ASSERT_NOT_REACHED();
90         return;
91     }
92
93     connectStateNodeLayers(*stateTree, *layerTreeHost);
94     m_scrollingTree->commitNewTreeState(stateTree.release());
95
96     m_requestedScrollInfo = nullptr;
97 }
98
99 #if !PLATFORM(IOS)
100 void RemoteScrollingCoordinatorProxy::connectStateNodeLayers(ScrollingStateTree& stateTree, const RemoteLayerTreeHost& layerTreeHost)
101 {
102     for (auto& currNode : stateTree.nodeMap().values()) {
103         if (currNode->hasChangedProperty(ScrollingStateNode::ScrollLayer))
104             currNode->setLayer(layerTreeHost.getLayer(currNode->layer()));
105
106         switch (currNode->nodeType()) {
107         case FrameScrollingNode: {
108             ScrollingStateFrameScrollingNode* scrollingStateNode = toScrollingStateFrameScrollingNode(currNode);
109             
110             if (scrollingStateNode->hasChangedProperty(ScrollingStateFrameScrollingNode::ScrolledContentsLayer))
111                 scrollingStateNode->setScrolledContentsLayer(layerTreeHost.getLayer(scrollingStateNode->scrolledContentsLayer()));
112
113             if (scrollingStateNode->hasChangedProperty(ScrollingStateFrameScrollingNode::CounterScrollingLayer))
114                 scrollingStateNode->setCounterScrollingLayer(layerTreeHost.getLayer(scrollingStateNode->counterScrollingLayer()));
115
116             if (scrollingStateNode->hasChangedProperty(ScrollingStateFrameScrollingNode::InsetClipLayer))
117                 scrollingStateNode->setInsetClipLayer(layerTreeHost.getLayer(scrollingStateNode->insetClipLayer()));
118
119             if (scrollingStateNode->hasChangedProperty(ScrollingStateFrameScrollingNode::ContentShadowLayer))
120                 scrollingStateNode->setContentShadowLayer(layerTreeHost.getLayer(scrollingStateNode->contentShadowLayer()));
121
122             // FIXME: we should never have header and footer layers coming from the WebProcess.
123             if (scrollingStateNode->hasChangedProperty(ScrollingStateFrameScrollingNode::HeaderLayer))
124                 scrollingStateNode->setHeaderLayer(layerTreeHost.getLayer(scrollingStateNode->headerLayer()));
125
126             if (scrollingStateNode->hasChangedProperty(ScrollingStateFrameScrollingNode::FooterLayer))
127                 scrollingStateNode->setFooterLayer(layerTreeHost.getLayer(scrollingStateNode->footerLayer()));
128             break;
129         }
130         case OverflowScrollingNode: {
131             ScrollingStateOverflowScrollingNode* scrollingStateNode = toScrollingStateOverflowScrollingNode(currNode);
132
133             if (scrollingStateNode->hasChangedProperty(ScrollingStateOverflowScrollingNode::ScrolledContentsLayer))
134                 scrollingStateNode->setScrolledContentsLayer(layerTreeHost.getLayer(scrollingStateNode->scrolledContentsLayer()));
135             break;
136         }
137         case FixedNode:
138         case StickyNode:
139             break;
140         }
141     }
142 }
143 #endif
144
145 bool RemoteScrollingCoordinatorProxy::handleWheelEvent(const PlatformWheelEvent& event)
146 {
147     ScrollingTree::EventResult result = m_scrollingTree->tryToHandleWheelEvent(event);
148     return result == ScrollingTree::DidHandleEvent; // FIXME: handle other values.
149 }
150
151 bool RemoteScrollingCoordinatorProxy::isPointInNonFastScrollableRegion(const IntPoint& p) const
152 {
153     return m_scrollingTree->isPointInNonFastScrollableRegion(p);
154 }
155
156 void RemoteScrollingCoordinatorProxy::viewportChangedViaDelegatedScrolling(ScrollingNodeID nodeID, const FloatRect& fixedPositionRect, double scale)
157 {
158     m_scrollingTree->viewportChangedViaDelegatedScrolling(nodeID, fixedPositionRect, scale);
159 }
160
161 // This comes from the scrolling tree.
162 void RemoteScrollingCoordinatorProxy::scrollingTreeNodeDidScroll(ScrollingNodeID scrolledNodeID, const FloatPoint& newScrollPosition, SetOrSyncScrollingLayerPosition scrollingLayerPositionAction)
163 {
164     // Scroll updates for the main frame are sent via WebPageProxy::updateVisibleContentRects()
165     // so don't send them here.
166     if (!m_propagatesMainFrameScrolls && scrolledNodeID == rootScrollingNodeID())
167         return;
168
169 #if PLATFORM(IOS)
170     m_webPageProxy.overflowScrollViewDidScroll();
171 #endif
172     m_webPageProxy.send(Messages::RemoteScrollingCoordinator::ScrollPositionChangedForNode(scrolledNodeID, newScrollPosition, scrollingLayerPositionAction));
173 }
174
175 void RemoteScrollingCoordinatorProxy::scrollingTreeNodeRequestsScroll(ScrollingNodeID scrolledNodeID, const FloatPoint& scrollPosition, bool representsProgrammaticScroll)
176 {
177     if (scrolledNodeID == rootScrollingNodeID() && m_requestedScrollInfo) {
178         m_requestedScrollInfo->requestsScrollPositionUpdate = true;
179         m_requestedScrollInfo->requestIsProgrammaticScroll = representsProgrammaticScroll;
180         m_requestedScrollInfo->requestedScrollPosition = scrollPosition;
181     }
182 }
183
184 } // namespace WebKit
185
186 #endif // ENABLE(ASYNC_SCROLLING)