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 "core/dom/Document.h"
35 #include "core/dom/Fullscreen.h"
36 #include "core/frame/LocalFrame.h"
37 #include "core/frame/PageScaleConstraintsSet.h"
38 #include "core/html/HTMLMediaElement.h"
39 #include "core/html/HTMLVideoElement.h"
40 #include "platform/LayoutTestSupport.h"
41 #include "platform/RuntimeEnabledFeatures.h"
42 #include "public/platform/WebLayerTreeView.h"
43 #include "public/web/WebFrameClient.h"
44 #include "web/WebLocalFrameImpl.h"
45 #include "web/WebSettingsImpl.h"
46 #include "web/WebViewImpl.h"
50 PassOwnPtrWillBeRawPtr
<FullscreenController
> FullscreenController::create(WebViewImpl
* webViewImpl
)
52 return adoptPtrWillBeNoop(new FullscreenController(webViewImpl
));
55 FullscreenController::FullscreenController(WebViewImpl
* webViewImpl
)
56 : m_webViewImpl(webViewImpl
)
57 , m_exitFullscreenPageScaleFactor(0)
58 , m_isCancelingFullScreen(false)
62 void FullscreenController::didEnterFullScreen()
64 if (!m_provisionalFullScreenElement
)
67 RefPtrWillBeRawPtr
<Element
> element
= m_provisionalFullScreenElement
.release();
68 Document
& document
= element
->document();
69 m_fullScreenFrame
= document
.frame();
71 if (!m_fullScreenFrame
)
74 if (!m_exitFullscreenPageScaleFactor
) {
75 m_exitFullscreenPageScaleFactor
= m_webViewImpl
->pageScaleFactor();
76 m_exitFullscreenScrollOffset
= m_webViewImpl
->mainFrame()->scrollOffset();
77 m_exitFullscreenVisualViewportOffset
= m_webViewImpl
->visualViewportOffset();
79 updatePageScaleConstraints(false);
80 m_webViewImpl
->setPageScaleFactor(1.0f
);
81 m_webViewImpl
->mainFrame()->setScrollOffset(WebSize());
82 m_webViewImpl
->setVisualViewportOffset(FloatPoint());
85 Fullscreen::from(document
).didEnterFullScreenForElement(element
.get());
86 ASSERT(Fullscreen::currentFullScreenElementFrom(document
) == element
);
88 if (isHTMLVideoElement(element
)) {
89 HTMLVideoElement
* videoElement
= toHTMLVideoElement(element
);
90 if (videoElement
->usesOverlayFullscreenVideo()) {
91 if (videoElement
->webMediaPlayer()
92 // FIXME: There is no embedder-side handling in layout test mode.
93 && !LayoutTestSupport::isRunningLayoutTest()) {
94 videoElement
->webMediaPlayer()->enterFullscreen();
96 if (m_webViewImpl
->layerTreeView())
97 m_webViewImpl
->layerTreeView()->setHasTransparentBackground(true);
102 void FullscreenController::didExitFullScreen()
104 if (!m_fullScreenFrame
)
107 if (Document
* document
= m_fullScreenFrame
->document()) {
108 if (Fullscreen
* fullscreen
= Fullscreen::fromIfExists(*document
)) {
109 Element
* element
= fullscreen
->webkitCurrentFullScreenElement();
111 // When the client exits from full screen we have to call fullyExitFullscreen to notify
112 // the document. While doing that, suppress notifications back to the client.
113 m_isCancelingFullScreen
= true;
114 Fullscreen::fullyExitFullscreen(*document
);
115 m_isCancelingFullScreen
= false;
117 // If the video used overlay fullscreen mode, the background was made transparent. Restore the transparency.
118 if (isHTMLVideoElement(element
) && m_webViewImpl
->layerTreeView())
119 m_webViewImpl
->layerTreeView()->setHasTransparentBackground(m_webViewImpl
->isTransparent());
121 if (m_exitFullscreenPageScaleFactor
) {
122 updatePageScaleConstraints(true);
123 m_webViewImpl
->setPageScaleFactor(m_exitFullscreenPageScaleFactor
);
124 m_webViewImpl
->mainFrame()->setScrollOffset(WebSize(m_exitFullscreenScrollOffset
));
125 m_webViewImpl
->setVisualViewportOffset(m_exitFullscreenVisualViewportOffset
);
126 m_exitFullscreenPageScaleFactor
= 0;
127 m_exitFullscreenScrollOffset
= IntSize();
130 fullscreen
->didExitFullScreenForElement(0);
135 m_fullScreenFrame
.clear();
138 void FullscreenController::enterFullScreenForElement(Element
* element
)
140 // We are already transitioning to fullscreen for a different element.
141 if (m_provisionalFullScreenElement
) {
142 m_provisionalFullScreenElement
= element
;
146 // We are already in fullscreen mode.
147 if (m_fullScreenFrame
) {
148 m_provisionalFullScreenElement
= element
;
149 didEnterFullScreen();
153 // We need to transition to fullscreen mode.
154 WebLocalFrameImpl
* frame
= WebLocalFrameImpl::fromFrame(element
->document().frame());
155 if (frame
&& frame
->client()) {
156 frame
->client()->enterFullscreen();
157 m_provisionalFullScreenElement
= element
;
161 void FullscreenController::exitFullScreenForElement(Element
* element
)
165 // The client is exiting full screen, so don't send a notification.
166 if (m_isCancelingFullScreen
)
169 WebLocalFrameImpl
* frame
= WebLocalFrameImpl::fromFrame(element
->document().frame());
170 if (frame
&& frame
->client())
171 frame
->client()->exitFullscreen();
174 void FullscreenController::updateSize()
179 updatePageScaleConstraints(false);
181 LayoutFullScreen
* layoutObject
= Fullscreen::from(*m_fullScreenFrame
->document()).fullScreenLayoutObject();
183 layoutObject
->updateStyle();
186 void FullscreenController::updatePageScaleConstraints(bool removeConstraints
)
188 PageScaleConstraints fullscreenConstraints
;
189 if (!removeConstraints
) {
190 fullscreenConstraints
= PageScaleConstraints(1.0, 1.0, 1.0);
191 fullscreenConstraints
.layoutSize
= IntSize(m_webViewImpl
->size());
193 m_webViewImpl
->pageScaleConstraintsSet().setFullscreenConstraints(fullscreenConstraints
);
194 m_webViewImpl
->pageScaleConstraintsSet().computeFinalConstraints();
195 m_webViewImpl
->updateMainFrameLayoutSize();
198 DEFINE_TRACE(FullscreenController
)
200 visitor
->trace(m_provisionalFullScreenElement
);
201 visitor
->trace(m_fullScreenFrame
);