Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / FullscreenController.cpp
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 #include "config.h"
32 #include "web/FullscreenController.h"
33
34 #include "RuntimeEnabledFeatures.h"
35 #include "core/dom/Document.h"
36 #include "core/dom/FullscreenElementStack.h"
37 #include "core/frame/LocalFrame.h"
38 #include "core/html/HTMLMediaElement.h"
39 #include "platform/LayoutTestSupport.h"
40 #include "public/web/WebFrame.h"
41 #include "public/web/WebViewClient.h"
42 #include "web/WebViewImpl.h"
43
44 using namespace WebCore;
45
46 namespace blink {
47
48 PassOwnPtr<FullscreenController> FullscreenController::create(WebViewImpl* webViewImpl)
49 {
50     return adoptPtr(new FullscreenController(webViewImpl));
51 }
52
53 FullscreenController::FullscreenController(WebViewImpl* webViewImpl)
54     : m_webViewImpl(webViewImpl)
55     , m_exitFullscreenPageScaleFactor(0)
56     , m_isCancelingFullScreen(false)
57 {
58 }
59
60 void FullscreenController::willEnterFullScreen()
61 {
62     if (!m_provisionalFullScreenElement)
63         return;
64
65     // Ensure that this element's document is still attached.
66     Document& doc = m_provisionalFullScreenElement->document();
67     if (doc.frame()) {
68         FullscreenElementStack::from(doc).webkitWillEnterFullScreenForElement(m_provisionalFullScreenElement.get());
69         m_fullScreenFrame = doc.frame();
70     }
71     m_provisionalFullScreenElement.clear();
72 }
73
74 void FullscreenController::didEnterFullScreen()
75 {
76     if (!m_fullScreenFrame)
77         return;
78
79     if (Document* doc = m_fullScreenFrame->document()) {
80         if (FullscreenElementStack::isFullScreen(*doc)) {
81             if (!m_exitFullscreenPageScaleFactor) {
82                 m_exitFullscreenPageScaleFactor = m_webViewImpl->pageScaleFactor();
83                 m_exitFullscreenScrollOffset = m_webViewImpl->mainFrame()->scrollOffset();
84                 m_exitFullscreenPinchViewportOffset = m_webViewImpl->pinchViewportOffset();
85                 m_webViewImpl->setPageScaleFactor(1.0f);
86             }
87
88             FullscreenElementStack::from(*doc).webkitDidEnterFullScreenForElement(0);
89             if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) {
90                 Element* element = FullscreenElementStack::currentFullScreenElementFrom(*doc);
91                 ASSERT(element);
92                 if (isHTMLMediaElement(*element) && m_webViewImpl->layerTreeView())
93                     m_webViewImpl->layerTreeView()->setHasTransparentBackground(true);
94             }
95         }
96     }
97 }
98
99 void FullscreenController::willExitFullScreen()
100 {
101     if (!m_fullScreenFrame)
102         return;
103
104     if (Document* doc = m_fullScreenFrame->document()) {
105         FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*doc);
106         if (!fullscreen)
107             return;
108         if (fullscreen->isFullScreen(*doc)) {
109             // When the client exits from full screen we have to call webkitCancelFullScreen to
110             // notify the document. While doing that, suppress notifications back to the client.
111             m_isCancelingFullScreen = true;
112             fullscreen->webkitCancelFullScreen();
113             m_isCancelingFullScreen = false;
114             fullscreen->webkitWillExitFullScreenForElement(0);
115             if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled() && m_webViewImpl->layerTreeView())
116                 m_webViewImpl->layerTreeView()->setHasTransparentBackground(m_webViewImpl->isTransparent());
117         }
118     }
119 }
120
121 void FullscreenController::didExitFullScreen()
122 {
123     if (!m_fullScreenFrame)
124         return;
125
126     if (Document* doc = m_fullScreenFrame->document()) {
127         if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*doc)) {
128             if (fullscreen->webkitIsFullScreen()) {
129                 if (m_exitFullscreenPageScaleFactor) {
130                     m_webViewImpl->setPageScaleFactor(m_exitFullscreenPageScaleFactor);
131                     m_webViewImpl->setMainFrameScrollOffset(IntPoint(m_exitFullscreenScrollOffset));
132                     m_webViewImpl->setPinchViewportOffset(m_exitFullscreenPinchViewportOffset);
133                     m_exitFullscreenPageScaleFactor = 0;
134                     m_exitFullscreenScrollOffset = IntSize();
135                 }
136
137                 fullscreen->webkitDidExitFullScreenForElement(0);
138             }
139         }
140     }
141
142     m_fullScreenFrame.clear();
143 }
144
145 void FullscreenController::enterFullScreenForElement(WebCore::Element* element)
146 {
147     // We are already transitioning to fullscreen for a different element.
148     if (m_provisionalFullScreenElement) {
149         m_provisionalFullScreenElement = element;
150         return;
151     }
152
153     // We are already in fullscreen mode.
154     if (m_fullScreenFrame) {
155         m_provisionalFullScreenElement = element;
156         willEnterFullScreen();
157         didEnterFullScreen();
158         return;
159     }
160
161     if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()
162         && isHTMLMediaElement(element)
163         // FIXME: There is no embedder-side handling in layout test mode.
164         && !isRunningLayoutTest()) {
165         HTMLMediaElement* mediaElement = toHTMLMediaElement(element);
166         if (mediaElement->player() && mediaElement->player()->canShowFullscreenOverlay()) {
167             mediaElement->player()->showFullscreenOverlay();
168             m_provisionalFullScreenElement = element;
169             return;
170         }
171     }
172
173     // We need to transition to fullscreen mode.
174     if (WebViewClient* client = m_webViewImpl->client()) {
175         if (client->enterFullScreen())
176             m_provisionalFullScreenElement = element;
177     }
178 }
179
180 void FullscreenController::exitFullScreenForElement(WebCore::Element* element)
181 {
182     // The client is exiting full screen, so don't send a notification.
183     if (m_isCancelingFullScreen)
184         return;
185     if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()
186         && isHTMLMediaElement(element)
187         // FIXME: There is no embedder-side handling in layout test mode.
188         && !isRunningLayoutTest()) {
189         HTMLMediaElement* mediaElement = toHTMLMediaElement(element);
190         if (mediaElement->player())
191             mediaElement->player()->hideFullscreenOverlay();
192         return;
193     }
194     if (WebViewClient* client = m_webViewImpl->client())
195         client->exitFullScreen();
196 }
197
198 }
199