Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / frame / PinchViewport.h
1 /*
2  * Copyright (C) 2013 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef PinchViewport_h
32 #define PinchViewport_h
33
34 #include "platform/geometry/FloatPoint.h"
35 #include "platform/geometry/FloatRect.h"
36 #include "platform/geometry/IntSize.h"
37 #include "platform/graphics/GraphicsLayerClient.h"
38 #include "platform/scroll/ScrollableArea.h"
39 #include "public/platform/WebScrollbar.h"
40 #include "public/platform/WebSize.h"
41 #include "wtf/OwnPtr.h"
42 #include "wtf/PassOwnPtr.h"
43
44 namespace blink {
45 class WebLayerTreeView;
46 class WebScrollbarLayer;
47 }
48
49 namespace blink {
50
51 class FrameHost;
52 class GraphicsContext;
53 class GraphicsLayer;
54 class GraphicsLayerFactory;
55 class IntRect;
56 class IntSize;
57 class LocalFrame;
58
59 // Represents the pinch-to-zoom viewport the user is currently seeing the page through. This
60 // class corresponds to the InnerViewport on the compositor. It is a ScrollableArea; it's
61 // offset is set through the GraphicsLayer <-> CC sync mechanisms. Its contents is the page's
62 // main FrameView, which corresponds to the outer viewport. The inner viewport is always contained
63 // in the outer viewport and can pan within it.
64 class PinchViewport final : public NoBaseWillBeGarbageCollectedFinalized<PinchViewport>, public GraphicsLayerClient, public ScrollableArea {
65 public:
66     static PassOwnPtrWillBeRawPtr<PinchViewport> create(FrameHost& host)
67     {
68         return adoptPtrWillBeNoop(new PinchViewport(host));
69     }
70     virtual ~PinchViewport();
71     virtual void trace(Visitor*);
72
73     void attachToLayerTree(GraphicsLayer*, GraphicsLayerFactory*);
74     GraphicsLayer* rootGraphicsLayer()
75     {
76         return m_rootTransformLayer.get();
77     }
78     GraphicsLayer* containerLayer()
79     {
80         return m_innerViewportContainerLayer.get();
81     }
82     GraphicsLayer* scrollLayer()
83     {
84         return m_innerViewportScrollLayer.get();
85     }
86
87     // Sets the location of the inner viewport relative to the outer viewport. The
88     // coordinates are in partial CSS pixels.
89     void setLocation(const FloatPoint&);
90     void move(const FloatPoint&);
91     FloatPoint location() const { return m_offset; }
92
93     // Sets the size of the inner viewport when unscaled in CSS pixels.
94     void setSize(const IntSize&);
95     IntSize size() const { return m_size; }
96
97     // Resets the viewport to initial state.
98     void reset();
99
100     // Let the viewport know that the main frame changed size (either through screen
101     // rotation on Android or window resize elsewhere).
102     void mainFrameDidChangeSize();
103
104     // Sets scale and location in one operation, preventing intermediate clamping.
105     void setScaleAndLocation(float scale, const FloatPoint& location);
106     void setScale(float);
107     float scale() const { return m_scale; }
108
109     void registerLayersWithTreeView(blink::WebLayerTreeView*) const;
110     void clearLayersForTreeView(blink::WebLayerTreeView*) const;
111
112     // The portion of the unzoomed frame visible in the inner "pinch" viewport,
113     // in partial CSS pixels. Relative to the main frame.
114     FloatRect visibleRect() const;
115
116     // The viewport rect relative to the document origin, in partial CSS pixels.
117     FloatRect visibleRectInDocument() const;
118
119     // Scroll the main frame and pinch viewport so that the given rect in the
120     // top-level document is centered in the viewport. This method will avoid
121     // scrolling the pinch viewport unless necessary.
122     void scrollIntoView(const LayoutRect&);
123
124     // Clamp the given point, in document coordinates, to the maximum/minimum
125     // scroll extents of the viewport within the document.
126     IntPoint clampDocumentOffsetAtScale(const IntPoint& offset, float scale);
127
128     // FIXME: This is kind of a hack. Ideally, we would just resize the
129     // viewports to account for top controls. However, FrameView includes much
130     // more than just scrolling so we can't simply resize it without incurring
131     // all sorts of side-effects. Until we can seperate out the scrollability
132     // aspect from FrameView, we use this method to let PinchViewport make the
133     // necessary adjustments so that we don't incorrectly clamp scroll offsets
134     // coming from the compositor. crbug.com/422328
135     void setTopControlsAdjustment(float);
136
137     // Adjust the viewport's offset so that it remains bounded by the outer
138     // viepwort.
139     void clampToBoundaries();
140 private:
141     explicit PinchViewport(FrameHost&);
142
143     // ScrollableArea implementation
144     virtual bool isActive() const override { return false; }
145     virtual int scrollSize(ScrollbarOrientation) const override;
146     virtual bool isScrollCornerVisible() const override { return false; }
147     virtual IntRect scrollCornerRect() const override { return IntRect(); }
148     virtual IntPoint scrollPosition() const override { return flooredIntPoint(m_offset); }
149     virtual IntPoint minimumScrollPosition() const override;
150     virtual IntPoint maximumScrollPosition() const override;
151     virtual int visibleHeight() const override { return visibleRect().height(); };
152     virtual int visibleWidth() const override { return visibleRect().width(); };
153     virtual IntSize contentsSize() const override;
154     virtual bool scrollbarsCanBeActive() const override { return false; }
155     virtual IntRect scrollableAreaBoundingBox() const override;
156     virtual bool userInputScrollable(ScrollbarOrientation) const override { return true; }
157     virtual bool shouldPlaceVerticalScrollbarOnLeft() const override { return false; }
158     virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) override;
159     virtual void invalidateScrollCornerRect(const IntRect&) override { }
160     virtual void setScrollOffset(const IntPoint&) override;
161     virtual GraphicsLayer* layerForContainer() const override;
162     virtual GraphicsLayer* layerForScrolling() const override;
163     virtual GraphicsLayer* layerForHorizontalScrollbar() const override;
164     virtual GraphicsLayer* layerForVerticalScrollbar() const override;
165
166     // GraphicsLayerClient implementation.
167     virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& inClip) override;
168     virtual String debugName(const GraphicsLayer*) override;
169
170     void setupScrollbar(blink::WebScrollbar::Orientation);
171     FloatPoint clampOffsetToBoundaries(const FloatPoint&);
172
173     LocalFrame* mainFrame() const;
174
175     FrameHost& frameHost() const
176     {
177         ASSERT(m_frameHost);
178         return *m_frameHost;
179     }
180
181     RawPtrWillBeMember<FrameHost> m_frameHost;
182     OwnPtr<GraphicsLayer> m_rootTransformLayer;
183     OwnPtr<GraphicsLayer> m_innerViewportContainerLayer;
184     OwnPtr<GraphicsLayer> m_pageScaleLayer;
185     OwnPtr<GraphicsLayer> m_innerViewportScrollLayer;
186     OwnPtr<GraphicsLayer> m_overlayScrollbarHorizontal;
187     OwnPtr<GraphicsLayer> m_overlayScrollbarVertical;
188     OwnPtr<blink::WebScrollbarLayer> m_webOverlayScrollbarHorizontal;
189     OwnPtr<blink::WebScrollbarLayer> m_webOverlayScrollbarVertical;
190
191     // Offset of the pinch viewport from the main frame's origin, in CSS pixels.
192     FloatPoint m_offset;
193     float m_scale;
194     IntSize m_size;
195     float m_topControlsAdjustment;
196 };
197
198 } // namespace blink
199
200 #endif // PinchViewport_h