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