057132ca8675ed3f966abb35f38956673e101021
[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             // FIXME: we should never have header and footer layers coming from the WebProcess.
112             if (scrollingStateNode->hasChangedProperty(ScrollingStateScrollingNode::HeaderLayer))
113                 scrollingStateNode->setHeaderLayer(layerTreeHost.getLayer(scrollingStateNode->headerLayer()));
114
115             if (scrollingStateNode->hasChangedProperty(ScrollingStateScrollingNode::FooterLayer))
116                 scrollingStateNode->setFooterLayer(layerTreeHost.getLayer(scrollingStateNode->footerLayer()));
117             break;
118         }
119         case FixedNode:
120             if (currNode->hasChangedProperty(ScrollingStateNode::ScrollLayer)) {
121                 currNode->setLayer(layerTreeHost.getLayer(currNode->layer()));
122                 fixedOrStickyLayerChanged = true;
123             }
124             break;
125         case StickyNode:
126             if (currNode->hasChangedProperty(ScrollingStateNode::ScrollLayer)) {
127                 currNode->setLayer(layerTreeHost.getLayer(currNode->layer()));
128                 fixedOrStickyLayerChanged = true;
129             }
130             break;
131         }
132     }
133 }
134 #endif
135
136 bool RemoteScrollingCoordinatorProxy::handleWheelEvent(const PlatformWheelEvent& event)
137 {
138     ScrollingTree::EventResult result = m_scrollingTree->tryToHandleWheelEvent(event);
139     return result == ScrollingTree::DidHandleEvent; // FIXME: handle other values.
140 }
141
142 bool RemoteScrollingCoordinatorProxy::isPointInNonFastScrollableRegion(const IntPoint& p) const
143 {
144     return m_scrollingTree->isPointInNonFastScrollableRegion(p);
145 }
146
147 void RemoteScrollingCoordinatorProxy::viewportChangedViaDelegatedScrolling(ScrollingNodeID nodeID, const FloatRect& viewportRect, double scale)
148 {
149     m_scrollingTree->viewportChangedViaDelegatedScrolling(nodeID, viewportRect, scale);
150 }
151
152 // This comes from the scrolling tree.
153 void RemoteScrollingCoordinatorProxy::scrollingTreeNodeDidScroll(ScrollingNodeID scrolledNodeID, const FloatPoint& newScrollPosition, SetOrSyncScrollingLayerPosition scrollingLayerPositionAction)
154 {
155     // Scroll updates for the main frame are sent via WebPageProxy::updateVisibleContentRects()
156     // so don't send them here.
157     if (!m_propagatesMainFrameScrolls && scrolledNodeID == rootScrollingNodeID())
158         return;
159
160     m_webPageProxy.send(Messages::RemoteScrollingCoordinator::ScrollPositionChangedForNode(scrolledNodeID, newScrollPosition, scrollingLayerPositionAction));
161 }
162
163 void RemoteScrollingCoordinatorProxy::scrollingTreeNodeRequestsScroll(ScrollingNodeID scrolledNodeID, const FloatPoint& scrollPosition, bool representsProgrammaticScroll)
164 {
165     if (scrolledNodeID == rootScrollingNodeID())
166         m_webPageProxy.requestScroll(scrollPosition, representsProgrammaticScroll);
167 }
168
169 } // namespace WebKit
170
171 #endif // ENABLE(ASYNC_SCROLLING)