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