Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 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 | |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 34 | #include "RuntimeEnabledFeatures.h" |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 35 | #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) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 40 | #include "core/frame/LocalFrame.h" |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 41 | #include "core/html/HTMLMediaElement.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 42 | #include "platform/LayoutTestSupport.h" |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 43 | |
| 44 | using namespace WebCore; |
| 45 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 46 | namespace blink { |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 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. |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 66 | Document& doc = m_provisionalFullScreenElement->document(); |
| 67 | if (doc.frame()) { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 68 | FullscreenElementStack::from(doc).webkitWillEnterFullScreenForElement(m_provisionalFullScreenElement.get()); |
Torne (Richard Coles) | 8abfc58 | 2013-09-12 12:10:38 +0100 | [diff] [blame] | 69 | m_fullScreenFrame = doc.frame(); |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 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()) { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 80 | if (FullscreenElementStack::isFullScreen(*doc)) { |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 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 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 87 | FullscreenElementStack::from(*doc).webkitDidEnterFullScreenForElement(0); |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 88 | if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 89 | Element* element = FullscreenElementStack::currentFullScreenElementFrom(*doc); |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 90 | ASSERT(element); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 91 | if (isHTMLMediaElement(*element) && m_webViewImpl->layerTreeView()) |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 92 | m_webViewImpl->layerTreeView()->setHasTransparentBackground(true); |
| 93 | } |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void FullscreenController::willExitFullScreen() |
| 99 | { |
| 100 | if (!m_fullScreenFrame) |
| 101 | return; |
| 102 | |
| 103 | if (Document* doc = m_fullScreenFrame->document()) { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 104 | FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*doc); |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 105 | if (!fullscreen) |
| 106 | return; |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 107 | if (fullscreen->isFullScreen(*doc)) { |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 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); |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 114 | if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled() && m_webViewImpl->layerTreeView()) |
| 115 | m_webViewImpl->layerTreeView()->setHasTransparentBackground(m_webViewImpl->isTransparent()); |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void FullscreenController::didExitFullScreen() |
| 121 | { |
| 122 | if (!m_fullScreenFrame) |
| 123 | return; |
| 124 | |
| 125 | if (Document* doc = m_fullScreenFrame->document()) { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 126 | if (FullscreenElementStack* fullscreen = FullscreenElementStack::fromIfExists(*doc)) { |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 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 | |
| 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 | |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 159 | if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled() |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 160 | && isHTMLMediaElement(element) |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 161 | // FIXME: There is no embedder-side handling in layout test mode. |
| 162 | && !isRunningLayoutTest()) { |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 163 | HTMLMediaElement* mediaElement = toHTMLMediaElement(element); |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 164 | if (mediaElement->player() && mediaElement->player()->canShowFullscreenOverlay()) { |
| 165 | mediaElement->player()->showFullscreenOverlay(); |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 166 | m_provisionalFullScreenElement = element; |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 167 | return; |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 168 | } |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 169 | } |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 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 | |
| 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; |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 183 | if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled() |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 184 | && isHTMLMediaElement(element) |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 185 | // FIXME: There is no embedder-side handling in layout test mode. |
| 186 | && !isRunningLayoutTest()) { |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 187 | HTMLMediaElement* mediaElement = toHTMLMediaElement(element); |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 188 | if (mediaElement->player()) |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 189 | mediaElement->player()->hideFullscreenOverlay(); |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 190 | return; |
| 191 | } |
Torne (Richard Coles) | f5e4ad5 | 2013-08-05 13:57:57 +0100 | [diff] [blame] | 192 | if (WebViewClient* client = m_webViewImpl->client()) |
| 193 | client->exitFullScreen(); |
| 194 | } |
| 195 | |
| 196 | } |
| 197 | |