Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / PageOverlay.cpp
1 /*
2  * Copyright (C) 2011 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  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20  * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "web/PageOverlay.h"
31
32 #include "core/frame/Settings.h"
33 #include "core/page/Page.h"
34 #include "platform/graphics/GraphicsLayer.h"
35 #include "platform/graphics/GraphicsLayerClient.h"
36 #include "public/platform/WebLayer.h"
37 #include "public/web/WebPageOverlay.h"
38 #include "public/web/WebViewClient.h"
39 #include "web/WebViewImpl.h"
40
41 using namespace WebCore;
42
43 namespace blink {
44
45 namespace {
46
47 WebCanvas* ToWebCanvas(GraphicsContext* gc)
48 {
49     return gc->canvas();
50 }
51
52 } // namespace
53
54 PassOwnPtr<PageOverlay> PageOverlay::create(WebViewImpl* viewImpl, WebPageOverlay* overlay)
55 {
56     return adoptPtr(new PageOverlay(viewImpl, overlay));
57 }
58
59 PageOverlay::PageOverlay(WebViewImpl* viewImpl, WebPageOverlay* overlay)
60     : m_viewImpl(viewImpl)
61     , m_overlay(overlay)
62     , m_zOrder(0)
63 {
64 }
65
66 class OverlayGraphicsLayerClientImpl : public WebCore::GraphicsLayerClient {
67 public:
68     static PassOwnPtr<OverlayGraphicsLayerClientImpl> create(WebPageOverlay* overlay)
69     {
70         return adoptPtr(new OverlayGraphicsLayerClientImpl(overlay));
71     }
72
73     virtual ~OverlayGraphicsLayerClientImpl() { }
74
75     virtual void notifyAnimationStarted(const GraphicsLayer*, double monotonicTime) OVERRIDE { }
76
77     virtual void paintContents(const GraphicsLayer*, GraphicsContext& gc, GraphicsLayerPaintingPhase, const IntRect& inClip)
78     {
79         if (gc.paintingDisabled())
80             return;
81         gc.save();
82         m_overlay->paintPageOverlay(ToWebCanvas(&gc));
83         gc.restore();
84     }
85
86     virtual String debugName(const GraphicsLayer* graphicsLayer) OVERRIDE
87     {
88         return String("WebViewImpl Page Overlay Content Layer");
89     }
90
91 private:
92     explicit OverlayGraphicsLayerClientImpl(WebPageOverlay* overlay)
93         : m_overlay(overlay)
94     {
95     }
96
97     WebPageOverlay* m_overlay;
98 };
99
100 void PageOverlay::clear()
101 {
102     invalidateWebFrame();
103
104     if (m_layer) {
105         m_layer->removeFromParent();
106         if (WebCore::Page* page = m_viewImpl->page())
107             page->inspectorController().didRemovePageOverlay(m_layer.get());
108         m_layer = nullptr;
109         m_layerClient = nullptr;
110     }
111 }
112
113 void PageOverlay::update()
114 {
115     invalidateWebFrame();
116
117     if (!m_layer) {
118         m_layerClient = OverlayGraphicsLayerClientImpl::create(m_overlay);
119         m_layer = GraphicsLayer::create(m_viewImpl->graphicsLayerFactory(), m_layerClient.get());
120         m_layer->setDrawsContent(true);
121
122         if (WebCore::Page* page = m_viewImpl->page())
123             page->inspectorController().willAddPageOverlay(m_layer.get());
124
125         // Compositor hit-testing does not know how to deal with layers that may be
126         // transparent to events (see http://crbug.com/269598). So require
127         // scrolling and touches on this layer to go to the main thread.
128         WebLayer* platformLayer = m_layer->platformLayer();
129         platformLayer->setShouldScrollOnMainThread(true);
130         WebVector<WebRect> webRects(static_cast<size_t>(1));
131         webRects[0] = WebRect(0, 0, INT_MAX, INT_MAX);
132         platformLayer->setTouchEventHandlerRegion(webRects);
133     }
134
135     FloatSize size(m_viewImpl->size());
136     if (size != m_layer->size()) {
137         // Triggers re-adding to root layer to ensure that we are on top of
138         // scrollbars.
139         m_layer->removeFromParent();
140         m_layer->setSize(size);
141     }
142
143     m_viewImpl->setOverlayLayer(m_layer.get());
144     m_layer->setNeedsDisplay();
145 }
146
147 void PageOverlay::paintWebFrame(GraphicsContext& gc)
148 {
149     if (!m_viewImpl->isAcceleratedCompositingActive() && !gc.paintingDisabled()) {
150         gc.save();
151         m_overlay->paintPageOverlay(ToWebCanvas(&gc));
152         gc.restore();
153     }
154 }
155
156 void PageOverlay::invalidateWebFrame()
157 {
158     // WebPageOverlay does the actual painting of the overlay.
159     // Here we just make sure to invalidate.
160     if (!m_viewImpl->isAcceleratedCompositingActive()) {
161         // FIXME: able to invalidate a smaller rect.
162         // FIXME: Is it important to just invalidate a smaller rect given that
163         // this is not on a critical codepath? In order to do so, we'd
164         // have to take scrolling into account.
165         const WebSize& size = m_viewImpl->size();
166         WebRect damagedRect(0, 0, size.width, size.height);
167         if (m_viewImpl->client())
168             m_viewImpl->client()->didInvalidateRect(damagedRect);
169     }
170 }
171
172 } // namespace blink