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 "FullscreenController.h"
33
34 #include "RuntimeEnabledFeatures.h"
35 #include "WebFrame.h"
36 #include "WebViewClient.h"
37 #include "WebViewImpl.h"
38 #include "core/dom/Document.h"
39 #include "core/dom/FullscreenElementStack.h"
40 #include "core/html/HTMLMediaElement.h"
41 #include "core/frame/Frame.h"
42 #include "platform/LayoutTestSupport.h"
43
44 using namespace WebCore;
45
46 namespace blink {
47
create(WebViewImpl * webViewImpl)48 PassOwnPtr<FullscreenController> FullscreenController::create(WebViewImpl* webViewImpl)
49 {
50 return adoptPtr(new FullscreenController(webViewImpl));
51 }
52
FullscreenController(WebViewImpl * webViewImpl)53 FullscreenController::FullscreenController(WebViewImpl* webViewImpl)
54 : m_webViewImpl(webViewImpl)
55 , m_exitFullscreenPageScaleFactor(0)
56 , m_isCancelingFullScreen(false)
57 {
58 }
59
willEnterFullScreen()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
didEnterFullScreen()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_webViewImpl->setPageScaleFactorPreservingScrollOffset(1.0f);
85 }
86
87 FullscreenElementStack::from(doc)->webkitDidEnterFullScreenForElement(0);
88 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) {
89 Element* element = FullscreenElementStack::currentFullScreenElementFrom(doc);
90 ASSERT(element);
91 if (element->isMediaElement() && m_webViewImpl->layerTreeView())
92 m_webViewImpl->layerTreeView()->setHasTransparentBackground(true);
93 }
94 }
95 }
96 }
97
willExitFullScreen()98 void FullscreenController::willExitFullScreen()
99 {
100 if (!m_fullScreenFrame)
101 return;
102
103 if (Document* doc = m_fullScreenFrame->document()) {
104 FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(doc);
105 if (!fullscreen)
106 return;
107 if (fullscreen->isFullScreen(doc)) {
108 // 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);
114 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled() && m_webViewImpl->layerTreeView())
115 m_webViewImpl->layerTreeView()->setHasTransparentBackground(m_webViewImpl->isTransparent());
116 }
117 }
118 }
119
didExitFullScreen()120 void FullscreenController::didExitFullScreen()
121 {
122 if (!m_fullScreenFrame)
123 return;
124
125 if (Document* doc = m_fullScreenFrame->document()) {
126 if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(doc)) {
127 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
enterFullScreenForElement(WebCore::Element * element)143 void 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
159 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()
160 && element && element->isMediaElement()
161 // FIXME: There is no embedder-side handling in layout test mode.
162 && !isRunningLayoutTest()) {
163 HTMLMediaElement* mediaElement = toHTMLMediaElement(element);
164 if (mediaElement->player() && mediaElement->player()->canShowFullscreenOverlay()) {
165 mediaElement->player()->showFullscreenOverlay();
166 m_provisionalFullScreenElement = element;
167 return;
168 }
169 }
170
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
exitFullScreenForElement(WebCore::Element * element)178 void 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;
183 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()
184 && element && element->isMediaElement()
185 // FIXME: There is no embedder-side handling in layout test mode.
186 && !isRunningLayoutTest()) {
187 HTMLMediaElement* mediaElement = toHTMLMediaElement(element);
188 if (mediaElement->player())
189 mediaElement->player()->hideFullscreenOverlay();
190 return;
191 }
192 if (WebViewClient* client = m_webViewImpl->client())
193 client->exitFullScreen();
194 }
195
196 }
197
198