blob: 46314cec88c3c525788b2744f65b5e065027667d [file] [log] [blame]
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +01001/*
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 "FullscreenController.h"
33
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010034#include "RuntimeEnabledFeatures.h"
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010035#include "WebFrame.h"
36#include "WebViewClient.h"
37#include "WebViewImpl.h"
38#include "core/dom/Document.h"
39#include "core/dom/FullscreenElementStack.h"
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000040#include "core/frame/LocalFrame.h"
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010041#include "core/html/HTMLMediaElement.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010042#include "platform/LayoutTestSupport.h"
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010043
44using namespace WebCore;
45
Torne (Richard Coles)51b29062013-11-28 11:56:03 +000046namespace blink {
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010047
48PassOwnPtr<FullscreenController> FullscreenController::create(WebViewImpl* webViewImpl)
49{
50 return adoptPtr(new FullscreenController(webViewImpl));
51}
52
53FullscreenController::FullscreenController(WebViewImpl* webViewImpl)
54 : m_webViewImpl(webViewImpl)
55 , m_exitFullscreenPageScaleFactor(0)
56 , m_isCancelingFullScreen(false)
57{
58}
59
60void FullscreenController::willEnterFullScreen()
61{
62 if (!m_provisionalFullScreenElement)
63 return;
64
65 // Ensure that this element's document is still attached.
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010066 Document& doc = m_provisionalFullScreenElement->document();
67 if (doc.frame()) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000068 FullscreenElementStack::from(doc).webkitWillEnterFullScreenForElement(m_provisionalFullScreenElement.get());
Torne (Richard Coles)8abfc582013-09-12 12:10:38 +010069 m_fullScreenFrame = doc.frame();
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010070 }
71 m_provisionalFullScreenElement.clear();
72}
73
74void FullscreenController::didEnterFullScreen()
75{
76 if (!m_fullScreenFrame)
77 return;
78
79 if (Document* doc = m_fullScreenFrame->document()) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000080 if (FullscreenElementStack::isFullScreen(*doc)) {
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010081 if (!m_exitFullscreenPageScaleFactor) {
82 m_exitFullscreenPageScaleFactor = m_webViewImpl->pageScaleFactor();
83 m_exitFullscreenScrollOffset = m_webViewImpl->mainFrame()->scrollOffset();
84 m_webViewImpl->setPageScaleFactorPreservingScrollOffset(1.0f);
85 }
86
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000087 FullscreenElementStack::from(*doc).webkitDidEnterFullScreenForElement(0);
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010088 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000089 Element* element = FullscreenElementStack::currentFullScreenElementFrom(*doc);
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010090 ASSERT(element);
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +000091 if (isHTMLMediaElement(*element) && m_webViewImpl->layerTreeView())
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +010092 m_webViewImpl->layerTreeView()->setHasTransparentBackground(true);
93 }
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +010094 }
95 }
96}
97
98void FullscreenController::willExitFullScreen()
99{
100 if (!m_fullScreenFrame)
101 return;
102
103 if (Document* doc = m_fullScreenFrame->document()) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000104 FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*doc);
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100105 if (!fullscreen)
106 return;
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000107 if (fullscreen->isFullScreen(*doc)) {
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100108 // When the client exits from full screen we have to call webkitCancelFullScreen to
109 // notify the document. While doing that, suppress notifications back to the client.
110 m_isCancelingFullScreen = true;
111 fullscreen->webkitCancelFullScreen();
112 m_isCancelingFullScreen = false;
113 fullscreen->webkitWillExitFullScreenForElement(0);
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100114 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled() && m_webViewImpl->layerTreeView())
115 m_webViewImpl->layerTreeView()->setHasTransparentBackground(m_webViewImpl->isTransparent());
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100116 }
117 }
118}
119
120void FullscreenController::didExitFullScreen()
121{
122 if (!m_fullScreenFrame)
123 return;
124
125 if (Document* doc = m_fullScreenFrame->document()) {
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000126 if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*doc)) {
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100127 if (fullscreen->webkitIsFullScreen()) {
128 if (m_exitFullscreenPageScaleFactor) {
129 m_webViewImpl->setPageScaleFactor(m_exitFullscreenPageScaleFactor,
130 WebPoint(m_exitFullscreenScrollOffset.width(), m_exitFullscreenScrollOffset.height()));
131 m_exitFullscreenPageScaleFactor = 0;
132 m_exitFullscreenScrollOffset = IntSize();
133 }
134
135 fullscreen->webkitDidExitFullScreenForElement(0);
136 }
137 }
138 }
139
140 m_fullScreenFrame.clear();
141}
142
143void FullscreenController::enterFullScreenForElement(WebCore::Element* element)
144{
145 // We are already transitioning to fullscreen for a different element.
146 if (m_provisionalFullScreenElement) {
147 m_provisionalFullScreenElement = element;
148 return;
149 }
150
151 // We are already in fullscreen mode.
152 if (m_fullScreenFrame) {
153 m_provisionalFullScreenElement = element;
154 willEnterFullScreen();
155 didEnterFullScreen();
156 return;
157 }
158
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100159 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000160 && isHTMLMediaElement(element)
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100161 // FIXME: There is no embedder-side handling in layout test mode.
162 && !isRunningLayoutTest()) {
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100163 HTMLMediaElement* mediaElement = toHTMLMediaElement(element);
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100164 if (mediaElement->player() && mediaElement->player()->canShowFullscreenOverlay()) {
165 mediaElement->player()->showFullscreenOverlay();
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100166 m_provisionalFullScreenElement = element;
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100167 return;
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100168 }
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100169 }
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100170
171 // We need to transition to fullscreen mode.
172 if (WebViewClient* client = m_webViewImpl->client()) {
173 if (client->enterFullScreen())
174 m_provisionalFullScreenElement = element;
175 }
176}
177
178void FullscreenController::exitFullScreenForElement(WebCore::Element* element)
179{
180 // The client is exiting full screen, so don't send a notification.
181 if (m_isCancelingFullScreen)
182 return;
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100183 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()
Torne (Richard Coles)d5428f32014-03-18 10:21:16 +0000184 && isHTMLMediaElement(element)
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100185 // FIXME: There is no embedder-side handling in layout test mode.
186 && !isRunningLayoutTest()) {
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +0100187 HTMLMediaElement* mediaElement = toHTMLMediaElement(element);
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100188 if (mediaElement->player())
Torne (Richard Coles)06f816c2013-09-26 13:25:12 +0100189 mediaElement->player()->hideFullscreenOverlay();
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100190 return;
191 }
Torne (Richard Coles)f5e4ad52013-08-05 13:57:57 +0100192 if (WebViewClient* client = m_webViewImpl->client())
193 client->exitFullScreen();
194}
195
196}
197