2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "web/FullscreenController.h"
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"
44 using namespace WebCore;
48 PassOwnPtr<FullscreenController> FullscreenController::create(WebViewImpl* webViewImpl)
50 return adoptPtr(new FullscreenController(webViewImpl));
53 FullscreenController::FullscreenController(WebViewImpl* webViewImpl)
54 : m_webViewImpl(webViewImpl)
55 , m_exitFullscreenPageScaleFactor(0)
56 , m_isCancelingFullScreen(false)
60 void FullscreenController::willEnterFullScreen()
62 if (!m_provisionalFullScreenElement)
65 // Ensure that this element's document is still attached.
66 Document& doc = m_provisionalFullScreenElement->document();
68 FullscreenElementStack::from(doc).webkitWillEnterFullScreenForElement(m_provisionalFullScreenElement.get());
69 m_fullScreenFrame = doc.frame();
71 m_provisionalFullScreenElement.clear();
74 void FullscreenController::didEnterFullScreen()
76 if (!m_fullScreenFrame)
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);
88 FullscreenElementStack::from(*doc).webkitDidEnterFullScreenForElement(0);
89 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) {
90 Element* element = FullscreenElementStack::currentFullScreenElementFrom(*doc);
92 if (isHTMLMediaElement(*element) && m_webViewImpl->layerTreeView())
93 m_webViewImpl->layerTreeView()->setHasTransparentBackground(true);
99 void FullscreenController::willExitFullScreen()
101 if (!m_fullScreenFrame)
104 if (Document* doc = m_fullScreenFrame->document()) {
105 FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*doc);
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());
121 void FullscreenController::didExitFullScreen()
123 if (!m_fullScreenFrame)
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();
137 fullscreen->webkitDidExitFullScreenForElement(0);
142 m_fullScreenFrame.clear();
145 void FullscreenController::enterFullScreenForElement(WebCore::Element* element)
147 // We are already transitioning to fullscreen for a different element.
148 if (m_provisionalFullScreenElement) {
149 m_provisionalFullScreenElement = element;
153 // We are already in fullscreen mode.
154 if (m_fullScreenFrame) {
155 m_provisionalFullScreenElement = element;
156 willEnterFullScreen();
157 didEnterFullScreen();
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;
173 // We need to transition to fullscreen mode.
174 if (WebViewClient* client = m_webViewImpl->client()) {
175 if (client->enterFullScreen())
176 m_provisionalFullScreenElement = element;
180 void FullscreenController::exitFullScreenForElement(WebCore::Element* element)
182 // The client is exiting full screen, so don't send a notification.
183 if (m_isCancelingFullScreen)
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();
194 if (WebViewClient* client = m_webViewImpl->client())
195 client->exitFullScreen();