<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[193340] trunk/Source</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/193340">193340</a></dd>
<dt>Author</dt> <dd>jer.noble@apple.com</dd>
<dt>Date</dt> <dd>2015-12-03 11:12:26 -0800 (Thu, 03 Dec 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>[iOS] Fullscreen -&gt; PiP should resume to Fullscreen, not inline
https://bugs.webkit.org/show_bug.cgi?id=150906

Reviewed by Simon Fraser.

Source/WebCore:

When restoring the user interface when exiting PiP, we should return to Fullscreen if that
is where fullscreen was initiated from. Additionally, when we &quot;auto-PiP&quot; by backgrounding the
app in Fullscreen mode, we should &quot;auto-un-PiP&quot; when restoring the application to foreground.

Rather than have a separate method to request exiting fullscreen, entering standard fullscreen,
and switching to &amp; from PiP, add a new method setFullscreenMode() which does all three.

* html/HTMLVideoElement.cpp:
(WebCore::presentationModeToFullscreenMode):
(WebCore::HTMLVideoElement::webkitSetPresentationMode):
(WebCore::HTMLVideoElement::setFullscreenMode):
* html/HTMLVideoElement.h:

Add methods to for clients to request a specific fullscreen mode, and to query whether
the page is currently visible.

* platform/ios/WebVideoFullscreenControllerAVKit.mm:
(WebVideoFullscreenControllerContext::requestFullscreenMode):
(WebVideoFullscreenControllerContext::isVisible):
(WebVideoFullscreenControllerContext::requestExitFullscreen): Deleted.
* platform/ios/WebVideoFullscreenModel.h:
* platform/ios/WebVideoFullscreenModelVideoElement.h:
* platform/ios/WebVideoFullscreenModelVideoElement.mm:
(WebVideoFullscreenModelVideoElement::requestFullscreenMode):
(WebVideoFullscreenModelVideoElement::isVisible):
(WebVideoFullscreenModelVideoElement::requestExitFullscreen): Deleted.

Track whether returning from PiP should enter fullscreen, or whether foregrounding the app
should cause PiP to return to fullscreen.

* platform/ios/WebVideoFullscreenInterfaceAVKit.h:
* platform/ios/WebVideoFullscreenInterfaceAVKit.mm:
(-[WebAVPlayerLayer layoutSublayers]): Renamed from -setBounds:. -layoutSublayers is called
    more consistently than -setBounds:, including when a sublayer is added to the layer.
(WebAVPlayerLayerView_stopRoutingVideoToPictureInPicturePlayerLayerView): The PiP WebAVPlayerLayer's
    modelVideoLayerFrame property may have been modified, so pass it back up to the fullscreen
    layer when exiting PiP.
(WebVideoFullscreenInterfaceAVKit::applicationDidBecomeActive): Request fullscreen if we auto-PiPed.
(WebVideoFullscreenInterfaceAVKit::setupFullscreen): Only recreate owned objects if they did not already exist.
(WebVideoFullscreenInterfaceAVKit::enterFullscreenStandard): If we are in PiP mode, enter fullscreen by stopping PiP.
(WebVideoFullscreenInterfaceAVKit::requestHideAndExitFullscreen): requestExitFullscreen() -&gt; requestFullscreenMode().
(WebVideoFullscreenInterfaceAVKit::willStartPictureInPicture): Animate out the fullscreen window, if exiting Fullscreen -&gt; PiP.
(WebVideoFullscreenInterfaceAVKit::didStartPictureInPicture): Set m_shouldReturnToFullscreenAfterEnteringForeground if we auto-PiPed.
(WebVideoFullscreenInterfaceAVKit::failedToStartPictureInPicture): requestExitFullscreen() -&gt; requestFullscreenMode().
(WebVideoFullscreenInterfaceAVKit::willStopPictureInPicture): If we are returning from PiP -&gt; Fullscreen, do not hide the fullscreen window.
(WebVideoFullscreenInterfaceAVKit::didStopPictureInPicture): If we are returning from PiP -&gt; Fullscreen, show the controls and notify clients.
(WebVideoFullscreenInterfaceAVKit::prepareForPictureInPictureStopWithCompletionHandler): Animate in the fullscreen window, if returning from PiP -&gt; Fullscreen.
(WebVideoFullscreenInterfaceAVKit::shouldExitFullscreenWithReason): Set m_shouldReturnToFullscreenWhenStoppingPiP if we are entering PiP from Fullscreen.
* platform/spi/cocoa/AVKitSPI.h:

Source/WebKit2:

Add methods to pass isVisible(), applicationDidBecomeActive(), and requestFullscreenMode()
to and from models and interfaces.

* UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::applicationDidBecomeActive): Notify the fullscreen manager, if preset.

* UIProcess/ios/WebVideoFullscreenManagerProxy.mm:
(WebKit::WebVideoFullscreenModelContext::requestFullscreenMode): Renamed from requestExitFullscreen(); takes a mode enum.
(WebKit::WebVideoFullscreenModelContext::isVisible): Added.
(WebKit::WebVideoFullscreenManagerProxy::applicationDidBecomeActive): Pass to all existing interfaces.
(WebKit::WebVideoFullscreenManagerProxy::setupFullscreenWithID): Do not re-create the view if it already exists.
(WebKit::WebVideoFullscreenManagerProxy::requestFullscreenMode): Renamed from requestExitFullscreen(); takes a mode enum.
(WebKit::WebVideoFullscreenManagerProxy::isVisible): Added. Query Page::isVisible() and Page::isInWindow(), the latter because
        PiP will cause Page::isVisible() to be TRUE even when the containing tab is backgrounded.
(WebKit::WebVideoFullscreenModelContext::requestExitFullscreen): Deleted.
(WebKit::WebVideoFullscreenManagerProxy::requestExitFullscreen): Deleted.
* WebProcess/ios/WebVideoFullscreenManager.mm:
(WebKit::WebVideoFullscreenManager::enterVideoFullscreenForVideoElement): Only resize the view if we are not already in one of
        the fullscreen modes.
(WebKit::WebVideoFullscreenManager::requestFullscreenMode): Renamed from requestExitFullscreen().
(WebKit::WebVideoFullscreenManager::requestExitFullscreen): Deleted.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorehtmlHTMLVideoElementcpp">trunk/Source/WebCore/html/HTMLVideoElement.cpp</a></li>
<li><a href="#trunkSourceWebCorehtmlHTMLVideoElementh">trunk/Source/WebCore/html/HTMLVideoElement.h</a></li>
<li><a href="#trunkSourceWebCoreplatformiosWebVideoFullscreenControllerAVKitmm">trunk/Source/WebCore/platform/ios/WebVideoFullscreenControllerAVKit.mm</a></li>
<li><a href="#trunkSourceWebCoreplatformiosWebVideoFullscreenInterfaceAVKith">trunk/Source/WebCore/platform/ios/WebVideoFullscreenInterfaceAVKit.h</a></li>
<li><a href="#trunkSourceWebCoreplatformiosWebVideoFullscreenInterfaceAVKitmm">trunk/Source/WebCore/platform/ios/WebVideoFullscreenInterfaceAVKit.mm</a></li>
<li><a href="#trunkSourceWebCoreplatformiosWebVideoFullscreenModelh">trunk/Source/WebCore/platform/ios/WebVideoFullscreenModel.h</a></li>
<li><a href="#trunkSourceWebCoreplatformiosWebVideoFullscreenModelVideoElementh">trunk/Source/WebCore/platform/ios/WebVideoFullscreenModelVideoElement.h</a></li>
<li><a href="#trunkSourceWebCoreplatformiosWebVideoFullscreenModelVideoElementmm">trunk/Source/WebCore/platform/ios/WebVideoFullscreenModelVideoElement.mm</a></li>
<li><a href="#trunkSourceWebCoreplatformspicocoaAVKitSPIh">trunk/Source/WebCore/platform/spi/cocoa/AVKitSPI.h</a></li>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2UIProcessiosWebPageProxyIOSmm">trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm</a></li>
<li><a href="#trunkSourceWebKit2UIProcessiosWebVideoFullscreenManagerProxyh">trunk/Source/WebKit2/UIProcess/ios/WebVideoFullscreenManagerProxy.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessiosWebVideoFullscreenManagerProxymm">trunk/Source/WebKit2/UIProcess/ios/WebVideoFullscreenManagerProxy.mm</a></li>
<li><a href="#trunkSourceWebKit2WebProcessiosWebVideoFullscreenManagerh">trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.h</a></li>
<li><a href="#trunkSourceWebKit2WebProcessiosWebVideoFullscreenManagermessagesin">trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.messages.in</a></li>
<li><a href="#trunkSourceWebKit2WebProcessiosWebVideoFullscreenManagermm">trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebCore/ChangeLog        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -1,3 +1,60 @@
</span><ins>+2015-12-03  Jer Noble  &lt;jer.noble@apple.com&gt;
+
+        [iOS] Fullscreen -&gt; PiP should resume to Fullscreen, not inline
+        https://bugs.webkit.org/show_bug.cgi?id=150906
+
+        Reviewed by Simon Fraser.
+
+        When restoring the user interface when exiting PiP, we should return to Fullscreen if that
+        is where fullscreen was initiated from. Additionally, when we &quot;auto-PiP&quot; by backgrounding the
+        app in Fullscreen mode, we should &quot;auto-un-PiP&quot; when restoring the application to foreground.
+
+        Rather than have a separate method to request exiting fullscreen, entering standard fullscreen,
+        and switching to &amp; from PiP, add a new method setFullscreenMode() which does all three.
+
+        * html/HTMLVideoElement.cpp:
+        (WebCore::presentationModeToFullscreenMode):
+        (WebCore::HTMLVideoElement::webkitSetPresentationMode):
+        (WebCore::HTMLVideoElement::setFullscreenMode):
+        * html/HTMLVideoElement.h:
+
+        Add methods to for clients to request a specific fullscreen mode, and to query whether
+        the page is currently visible.
+
+        * platform/ios/WebVideoFullscreenControllerAVKit.mm:
+        (WebVideoFullscreenControllerContext::requestFullscreenMode):
+        (WebVideoFullscreenControllerContext::isVisible):
+        (WebVideoFullscreenControllerContext::requestExitFullscreen): Deleted.
+        * platform/ios/WebVideoFullscreenModel.h:
+        * platform/ios/WebVideoFullscreenModelVideoElement.h:
+        * platform/ios/WebVideoFullscreenModelVideoElement.mm:
+        (WebVideoFullscreenModelVideoElement::requestFullscreenMode):
+        (WebVideoFullscreenModelVideoElement::isVisible):
+        (WebVideoFullscreenModelVideoElement::requestExitFullscreen): Deleted.
+
+        Track whether returning from PiP should enter fullscreen, or whether foregrounding the app
+        should cause PiP to return to fullscreen.
+
+        * platform/ios/WebVideoFullscreenInterfaceAVKit.h:
+        * platform/ios/WebVideoFullscreenInterfaceAVKit.mm:
+        (-[WebAVPlayerLayer layoutSublayers]): Renamed from -setBounds:. -layoutSublayers is called
+            more consistently than -setBounds:, including when a sublayer is added to the layer.
+        (WebAVPlayerLayerView_stopRoutingVideoToPictureInPicturePlayerLayerView): The PiP WebAVPlayerLayer's
+            modelVideoLayerFrame property may have been modified, so pass it back up to the fullscreen
+            layer when exiting PiP.
+        (WebVideoFullscreenInterfaceAVKit::applicationDidBecomeActive): Request fullscreen if we auto-PiPed.
+        (WebVideoFullscreenInterfaceAVKit::setupFullscreen): Only recreate owned objects if they did not already exist.
+        (WebVideoFullscreenInterfaceAVKit::enterFullscreenStandard): If we are in PiP mode, enter fullscreen by stopping PiP.
+        (WebVideoFullscreenInterfaceAVKit::requestHideAndExitFullscreen): requestExitFullscreen() -&gt; requestFullscreenMode().
+        (WebVideoFullscreenInterfaceAVKit::willStartPictureInPicture): Animate out the fullscreen window, if exiting Fullscreen -&gt; PiP.
+        (WebVideoFullscreenInterfaceAVKit::didStartPictureInPicture): Set m_shouldReturnToFullscreenAfterEnteringForeground if we auto-PiPed.
+        (WebVideoFullscreenInterfaceAVKit::failedToStartPictureInPicture): requestExitFullscreen() -&gt; requestFullscreenMode().
+        (WebVideoFullscreenInterfaceAVKit::willStopPictureInPicture): If we are returning from PiP -&gt; Fullscreen, do not hide the fullscreen window.
+        (WebVideoFullscreenInterfaceAVKit::didStopPictureInPicture): If we are returning from PiP -&gt; Fullscreen, show the controls and notify clients.
+        (WebVideoFullscreenInterfaceAVKit::prepareForPictureInPictureStopWithCompletionHandler): Animate in the fullscreen window, if returning from PiP -&gt; Fullscreen.
+        (WebVideoFullscreenInterfaceAVKit::shouldExitFullscreenWithReason): Set m_shouldReturnToFullscreenWhenStoppingPiP if we are entering PiP from Fullscreen.
+        * platform/spi/cocoa/AVKitSPI.h:
+
</ins><span class="cx"> 2015-12-02  Sam Weinig  &lt;sam@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Promise callbacks should be called at microtask checkpoints
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlHTMLVideoElementcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/HTMLVideoElement.cpp (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/HTMLVideoElement.cpp        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebCore/html/HTMLVideoElement.cpp        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -381,9 +381,38 @@
</span><span class="cx">     return false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static bool presentationModeToFullscreenMode(const String&amp; presentationMode, HTMLMediaElementEnums::VideoFullscreenMode&amp; fullscreenMode)
+{
+    if (presentationMode == presentationModeFullscreen()) {
+        fullscreenMode = HTMLMediaElementEnums::VideoFullscreenModeStandard;
+        return true;
+    }
+
+    if (presentationMode == presentationModePictureInPicture()) {
+        fullscreenMode = HTMLMediaElementEnums::VideoFullscreenModePictureInPicture;
+        return true;
+    }
+
+    if (presentationMode == presentationModeInline()) {
+        fullscreenMode = HTMLMediaElementEnums::VideoFullscreenModeNone;
+        return true;
+    }
+    return false;
+}
+
</ins><span class="cx"> void HTMLVideoElement::webkitSetPresentationMode(const String&amp; mode)
</span><span class="cx"> {
</span><del>-    if (mode == presentationModeInline() &amp;&amp; isFullscreen()) {
</del><ins>+    VideoFullscreenMode fullscreenMode = VideoFullscreenModeNone;
+    if (!presentationModeToFullscreenMode(mode, fullscreenMode))
+        return;
+
+    LOG_WITH_STREAM(Media, stream &lt;&lt; &quot;HTMLVideoElement::webkitSetPresentationMode(&quot; &lt;&lt; this &lt;&lt; &quot;) - setting to \&quot;&quot; &lt;&lt; mode &lt;&lt; &quot;\&quot;&quot;);
+    setFullscreenMode(fullscreenMode);
+}
+
+void HTMLVideoElement::setFullscreenMode(HTMLMediaElementEnums::VideoFullscreenMode mode)
+{
+    if (mode == VideoFullscreenModeNone &amp;&amp; isFullscreen()) {
</ins><span class="cx">         exitFullscreen();
</span><span class="cx">         return;
</span><span class="cx">     }
</span><span class="lines">@@ -391,12 +420,7 @@
</span><span class="cx">     if (!mediaSession().fullscreenPermitted(*this) || !supportsFullscreen())
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    LOG(Media, &quot;HTMLVideoElement::webkitSetPresentationMode(%p) - setting to \&quot;%s\&quot;&quot;, this, mode.utf8().data());
-
-    if (mode == presentationModeFullscreen())
-        enterFullscreen(VideoFullscreenModeStandard);
-    else if (mode == presentationModePictureInPicture())
-        enterFullscreen(VideoFullscreenModePictureInPicture);
</del><ins>+    enterFullscreen(mode);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> String HTMLVideoElement::webkitPresentationMode() const
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlHTMLVideoElementh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/HTMLVideoElement.h (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/HTMLVideoElement.h        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebCore/html/HTMLVideoElement.h        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -82,6 +82,7 @@
</span><span class="cx">     bool webkitSupportsPresentationMode(const String&amp;) const;
</span><span class="cx">     void webkitSetPresentationMode(const String&amp;);
</span><span class="cx">     String webkitPresentationMode() const;
</span><ins>+    void setFullscreenMode(VideoFullscreenMode);
</ins><span class="cx">     virtual void fullscreenModeChanged(VideoFullscreenMode) override;
</span><span class="cx"> #endif
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformiosWebVideoFullscreenControllerAVKitmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/ios/WebVideoFullscreenControllerAVKit.mm (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/ios/WebVideoFullscreenControllerAVKit.mm        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebCore/platform/ios/WebVideoFullscreenControllerAVKit.mm        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -143,13 +143,14 @@
</span><span class="cx">     virtual void beginScanningForward() override;
</span><span class="cx">     virtual void beginScanningBackward() override;
</span><span class="cx">     virtual void endScanning() override;
</span><del>-    virtual void requestExitFullscreen() override;
</del><ins>+    virtual void requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenMode) override;
</ins><span class="cx">     virtual void setVideoLayerFrame(FloatRect) override;
</span><span class="cx">     virtual void setVideoLayerGravity(WebVideoFullscreenModel::VideoGravity) override;
</span><span class="cx">     virtual void selectAudioMediaOption(uint64_t index) override;
</span><span class="cx">     virtual void selectLegibleMediaOption(uint64_t index) override;
</span><span class="cx">     virtual void fullscreenModeChanged(HTMLMediaElementEnums::VideoFullscreenMode) override;
</span><del>-    
</del><ins>+    virtual bool isVisible() const override;
+
</ins><span class="cx">     RefPtr&lt;WebVideoFullscreenInterfaceAVKit&gt; m_interface;
</span><span class="cx">     RefPtr&lt;WebVideoFullscreenModelVideoElement&gt; m_model;
</span><span class="cx">     RefPtr&lt;HTMLVideoElement&gt; m_videoElement;
</span><span class="lines">@@ -461,13 +462,13 @@
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebVideoFullscreenControllerContext::requestExitFullscreen()
</del><ins>+void WebVideoFullscreenControllerContext::requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenMode mode)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(isUIThread());
</span><span class="cx">     RefPtr&lt;WebVideoFullscreenControllerContext&gt; strongThis(this);
</span><del>-    WebThreadRun([strongThis, this] {
</del><ins>+    WebThreadRun([strongThis, this, mode] {
</ins><span class="cx">         if (m_model)
</span><del>-            m_model-&gt;requestExitFullscreen();
</del><ins>+            m_model-&gt;requestFullscreenMode(mode);
</ins><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -534,6 +535,12 @@
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool WebVideoFullscreenControllerContext::isVisible() const
+{
+    ASSERT(isUIThread());
+    return m_model ? m_model-&gt;isVisible() : false;
+}
+
</ins><span class="cx"> #pragma mark Other
</span><span class="cx"> 
</span><span class="cx"> void WebVideoFullscreenControllerContext::setUpFullscreen(HTMLVideoElement&amp; videoElement, UIView *view, HTMLMediaElementEnums::VideoFullscreenMode mode)
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformiosWebVideoFullscreenInterfaceAVKith"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/ios/WebVideoFullscreenInterfaceAVKit.h (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/ios/WebVideoFullscreenInterfaceAVKit.h        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebCore/platform/ios/WebVideoFullscreenInterfaceAVKit.h        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -115,6 +115,7 @@
</span><span class="cx">     WEBCORE_EXPORT bool mayAutomaticallyShowVideoPictureInPicture() const;
</span><span class="cx">     void fullscreenMayReturnToInline(std::function&lt;void(bool)&gt; callback);
</span><span class="cx">     bool wirelessVideoPlaybackDisabled() const;
</span><ins>+    void applicationDidBecomeActive();
</ins><span class="cx"> 
</span><span class="cx">     void willStartPictureInPicture();
</span><span class="cx">     void didStartPictureInPicture();
</span><span class="lines">@@ -154,6 +155,8 @@
</span><span class="cx">     bool m_exitCompleted { false };
</span><span class="cx">     bool m_enterRequested { false };
</span><span class="cx">     bool m_wirelessVideoPlaybackDisabled { true };
</span><ins>+    bool m_shouldReturnToFullscreenWhenStoppingPiP { false };
+    bool m_shouldReturnToFullscreenAfterEnteringForeground { false };
</ins><span class="cx"> 
</span><span class="cx">     void doEnterFullscreen();
</span><span class="cx"> };
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformiosWebVideoFullscreenInterfaceAVKitmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/ios/WebVideoFullscreenInterfaceAVKit.mm (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/ios/WebVideoFullscreenInterfaceAVKit.mm        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebCore/platform/ios/WebVideoFullscreenInterfaceAVKit.mm        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -650,21 +650,16 @@
</span><span class="cx">     return _videoSublayer.get();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-- (void)setBounds:(CGRect)bounds
</del><ins>+- (void)layoutSublayers
</ins><span class="cx"> {
</span><del>-    if (CGRectEqualToRect(bounds, self.bounds))
-        return;
-    
-    [super setBounds:bounds];
-    
</del><span class="cx">     if ([_videoSublayer superlayer] != self)
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    [_videoSublayer setPosition:CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds))];
-    
</del><span class="cx">     if (![_avPlayerController delegate])
</span><span class="cx">         return;
</span><span class="cx"> 
</span><ins>+    [_videoSublayer setPosition:CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds))];
+
</ins><span class="cx">     FloatRect sourceVideoFrame;
</span><span class="cx">     FloatRect targetVideoFrame;
</span><span class="cx">     float videoAspectRatio = self.videoDimensions.width / self.videoDimensions.height;
</span><span class="lines">@@ -840,6 +835,10 @@
</span><span class="cx"> {
</span><span class="cx">     WebAVPlayerLayerView *playerLayerView = aSelf;
</span><span class="cx">     [playerLayerView addSubview:playerLayerView.videoView];
</span><ins>+    WebAVPictureInPicturePlayerLayerView *pipView = (WebAVPictureInPicturePlayerLayerView *)[playerLayerView pictureInPicturePlayerLayerView];
+    WebAVPlayerLayer *playerLayer = (WebAVPlayerLayer *)[playerLayerView playerLayer];
+    WebAVPlayerLayer *pipPlayerLayer = (WebAVPlayerLayer *)[pipView layer];
+    [playerLayer setModelVideoLayerFrame:pipPlayerLayer.modelVideoLayerFrame];
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static WebAVPictureInPicturePlayerLayerView *WebAVPlayerLayerView_pictureInPicturePlayerLayerView(id aSelf, SEL)
</span><span class="lines">@@ -1051,6 +1050,13 @@
</span><span class="cx">     return [m_playerController allowsExternalPlayback];
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebVideoFullscreenInterfaceAVKit::applicationDidBecomeActive()
+{
+    LOG(Fullscreen, &quot;WebVideoFullscreenInterfaceAVKit::applicationDidBecomeActive(%p)&quot;, this);
+    if (m_shouldReturnToFullscreenAfterEnteringForeground &amp;&amp; m_videoFullscreenModel &amp;&amp; m_videoFullscreenModel-&gt;isVisible())
+        [m_playerViewController stopPictureInPicture];
+}
+
</ins><span class="cx"> @interface UIWindow ()
</span><span class="cx"> - (BOOL)_isHostedInAnotherProcess;
</span><span class="cx"> @end
</span><span class="lines">@@ -1068,33 +1074,40 @@
</span><span class="cx"> 
</span><span class="cx">     [CATransaction begin];
</span><span class="cx">     [CATransaction setDisableActions:YES];
</span><ins>+    bool isInPictureInPictureMode = hasMode(HTMLMediaElementEnums::VideoFullscreenModePictureInPicture);
</ins><span class="cx">     m_mode = mode;
</span><span class="cx">     m_parentView = parentView;
</span><span class="cx">     m_parentWindow = parentView.window;
</span><span class="cx"> 
</span><span class="cx">     if (![[parentView window] _isHostedInAnotherProcess]) {
</span><del>-        m_window = adoptNS([allocUIWindowInstance() initWithFrame:[[getUIScreenClass() mainScreen] bounds]]);
</del><ins>+        if (!m_window)
+            m_window = adoptNS([allocUIWindowInstance() initWithFrame:[[getUIScreenClass() mainScreen] bounds]]);
</ins><span class="cx">         [m_window setBackgroundColor:[getUIColorClass() clearColor]];
</span><del>-        m_viewController = adoptNS([allocUIViewControllerInstance() init]);
</del><ins>+        if (!m_viewController)
+            m_viewController = adoptNS([allocUIViewControllerInstance() init]);
</ins><span class="cx">         [[m_viewController view] setFrame:[m_window bounds]];
</span><span class="cx">         [m_viewController _setIgnoreAppSupportedOrientations:YES];
</span><span class="cx">         [m_window setRootViewController:m_viewController.get()];
</span><span class="cx">         [m_window makeKeyAndVisible];
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    m_playerLayerView = adoptNS([[getWebAVPlayerLayerViewClass() alloc] init]);
</del><ins>+    if (!m_playerLayerView)
+        m_playerLayerView = adoptNS([[getWebAVPlayerLayerViewClass() alloc] init]);
</ins><span class="cx">     [m_playerLayerView setHidden:[m_playerController isExternalPlaybackActive]];
</span><span class="cx">     [m_playerLayerView setBackgroundColor:[getUIColorClass() clearColor]];
</span><del>-    
-    [m_playerLayerView setVideoView:&amp;videoView];
-    [m_playerLayerView addSubview:&amp;videoView];
</del><span class="cx"> 
</span><ins>+    if (!isInPictureInPictureMode) {
+        [m_playerLayerView setVideoView:&amp;videoView];
+        [m_playerLayerView addSubview:&amp;videoView];
+    }
+
</ins><span class="cx">     WebAVPlayerLayer *playerLayer = (WebAVPlayerLayer *)[m_playerLayerView playerLayer];
</span><span class="cx"> 
</span><span class="cx">     [playerLayer setModelVideoLayerFrame:CGRectMake(0, 0, initialRect.width(), initialRect.height())];
</span><span class="cx">     [playerLayer setVideoDimensions:[m_playerController contentDimensions]];
</span><span class="cx"> 
</span><del>-    m_playerViewController = adoptNS([allocAVPlayerViewControllerInstance() initWithPlayerLayerView:m_playerLayerView.get()]);
</del><ins>+    if (!m_playerViewController)
+        m_playerViewController = adoptNS([allocAVPlayerViewControllerInstance() initWithPlayerLayerView:m_playerLayerView.get()]);
</ins><span class="cx"> 
</span><span class="cx">     [m_playerViewController setShowsPlaybackControls:NO];
</span><span class="cx">     [m_playerViewController setPlayerController:(AVPlayerController *)m_playerController.get()];
</span><span class="lines">@@ -1155,6 +1168,15 @@
</span><span class="cx"> {
</span><span class="cx">     LOG(Fullscreen, &quot;WebVideoFullscreenInterfaceAVKit::enterFullscreenStandard(%p)&quot;, this);
</span><span class="cx">     RefPtr&lt;WebVideoFullscreenInterfaceAVKit&gt; strongThis(this);
</span><ins>+
+    if ([m_playerViewController isPictureInPictureActive]) {
+        // NOTE: The fullscreen mode will be restored in prepareForPictureInPictureStopWithCompletionHandler().
+        m_shouldReturnToFullscreenWhenStoppingPiP = true;
+        [m_playerViewController stopPictureInPicture];
+        return;
+    }
+
+    [m_playerLayerView setBackgroundColor:[getUIColorClass() blackColor]];
</ins><span class="cx">     [m_playerViewController enterFullScreenAnimated:YES completionHandler:[this, strongThis] (BOOL succeeded, NSError*) {
</span><span class="cx">         UNUSED_PARAM(succeeded);
</span><span class="cx">         LOG(Fullscreen, &quot;WebVideoFullscreenInterfaceAVKit::enterFullscreenStandard - lambda(%p) - succeeded(%s)&quot;, this, boolString(succeeded));
</span><span class="lines">@@ -1287,7 +1309,7 @@
</span><span class="cx"> 
</span><span class="cx">     if (m_videoFullscreenModel &amp;&amp; !m_exitRequested) {
</span><span class="cx">         m_videoFullscreenModel-&gt;pause();
</span><del>-        m_videoFullscreenModel-&gt;requestExitFullscreen();
</del><ins>+        m_videoFullscreenModel-&gt;requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenModeNone);
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -1324,11 +1346,10 @@
</span><span class="cx"> void WebVideoFullscreenInterfaceAVKit::didStartPictureInPicture()
</span><span class="cx"> {
</span><span class="cx">     LOG(Fullscreen, &quot;WebVideoFullscreenInterfaceAVKit::didStartPictureInPicture(%p)&quot;, this);
</span><ins>+    m_shouldReturnToFullscreenAfterEnteringForeground = [m_playerViewController pictureInPictureWasStartedWhenEnteringBackground];
</ins><span class="cx">     [m_playerViewController setShowsPlaybackControls:YES];
</span><span class="cx"> 
</span><span class="cx">     if (m_mode &amp; HTMLMediaElementEnums::VideoFullscreenModeStandard) {
</span><del>-        clearMode(HTMLMediaElementEnums::VideoFullscreenModeStandard);
-
</del><span class="cx">         RefPtr&lt;WebVideoFullscreenInterfaceAVKit&gt; strongThis(this);
</span><span class="cx">         [m_playerViewController exitFullScreenAnimated:YES completionHandler:[strongThis, this] (BOOL, NSError*) {
</span><span class="cx">             [m_window setHidden:YES];
</span><span class="lines">@@ -1357,24 +1378,34 @@
</span><span class="cx">         m_fullscreenChangeObserver-&gt;didEnterFullscreen();
</span><span class="cx"> 
</span><span class="cx">     if (m_videoFullscreenModel)
</span><del>-        m_videoFullscreenModel-&gt;requestExitFullscreen();
</del><ins>+        m_videoFullscreenModel-&gt;requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenModeNone);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebVideoFullscreenInterfaceAVKit::willStopPictureInPicture()
</span><span class="cx"> {
</span><span class="cx">     LOG(Fullscreen, &quot;WebVideoFullscreenInterfaceAVKit::willStopPictureInPicture(%p)&quot;, this);
</span><ins>+    if (hasMode(HTMLMediaElementEnums::VideoFullscreenModeStandard))
+        return;
+
</ins><span class="cx">     [m_window setHidden:NO];
</span><span class="cx">     [[m_playerViewController view] setHidden:NO];
</span><span class="cx"> 
</span><span class="cx">     if (m_videoFullscreenModel)
</span><del>-        m_videoFullscreenModel-&gt;requestExitFullscreen();
</del><ins>+        m_videoFullscreenModel-&gt;requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenModeNone);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebVideoFullscreenInterfaceAVKit::didStopPictureInPicture()
</span><span class="cx"> {
</span><span class="cx">     LOG(Fullscreen, &quot;WebVideoFullscreenInterfaceAVKit::didStopPictureInPicture(%p)&quot;, this);
</span><del>-    if (hasMode(HTMLMediaElementEnums::VideoFullscreenModeStandard))
</del><ins>+    if (hasMode(HTMLMediaElementEnums::VideoFullscreenModeStandard)) {
+        // ASSUMPTION: we are exiting pip because we are entering fullscreen
+        clearMode(HTMLMediaElementEnums::VideoFullscreenModePictureInPicture);
+        [m_playerViewController setShowsPlaybackControls:YES];
+
+        if (m_fullscreenChangeObserver)
+            m_fullscreenChangeObserver-&gt;didEnterFullscreen();
</ins><span class="cx">         return;
</span><ins>+    }
</ins><span class="cx"> 
</span><span class="cx">     m_exitCompleted = true;
</span><span class="cx"> 
</span><span class="lines">@@ -1392,6 +1423,21 @@
</span><span class="cx"> void WebVideoFullscreenInterfaceAVKit::prepareForPictureInPictureStopWithCompletionHandler(void (^completionHandler)(BOOL restored))
</span><span class="cx"> {
</span><span class="cx">     LOG(Fullscreen, &quot;WebVideoFullscreenInterfaceAVKit::prepareForPictureInPictureStopWithCompletionHandler(%p)&quot;, this);
</span><ins>+    if (m_shouldReturnToFullscreenWhenStoppingPiP || m_shouldReturnToFullscreenAfterEnteringForeground) {
+        m_shouldReturnToFullscreenWhenStoppingPiP = false;
+        m_shouldReturnToFullscreenAfterEnteringForeground = false;
+
+        // ASSUMPTION: we are exiting pip because we are entering fullscreen
+        [m_window setHidden:NO];
+        [[m_playerViewController view] setHidden:NO];
+
+        [m_playerViewController enterFullScreenAnimated:YES completionHandler:^(BOOL success, NSError*) {
+            setMode(HTMLMediaElementEnums::VideoFullscreenModeStandard);
+            completionHandler(success);
+        }];
+        return;
+    }
+
</ins><span class="cx">     RefPtr&lt;WebVideoFullscreenInterfaceAVKit&gt; strongThis(this);
</span><span class="cx">     RetainPtr&lt;id&gt; strongCompletionHandler = adoptNS([completionHandler copy]);
</span><span class="cx">     fullscreenMayReturnToInline([strongThis, strongCompletionHandler](bool restored)  {
</span><span class="lines">@@ -1407,6 +1453,7 @@
</span><span class="cx">         return true;
</span><span class="cx"> 
</span><span class="cx">     if (reason == ExitFullScreenReason::PictureInPictureStarted) {
</span><ins>+        m_shouldReturnToFullscreenWhenStoppingPiP = hasMode(HTMLMediaElementEnums::VideoFullscreenModeStandard);
</ins><span class="cx">         clearMode(HTMLMediaElementEnums::VideoFullscreenModeStandard);
</span><span class="cx">         return true;
</span><span class="cx">     }
</span><span class="lines">@@ -1415,7 +1462,7 @@
</span><span class="cx">         m_videoFullscreenModel-&gt;pause();
</span><span class="cx">     
</span><span class="cx"> 
</span><del>-    m_videoFullscreenModel-&gt;requestExitFullscreen();
</del><ins>+    m_videoFullscreenModel-&gt;requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenModeNone);
</ins><span class="cx"> 
</span><span class="cx">     if (!m_watchdogTimer.isActive())
</span><span class="cx">         m_watchdogTimer.startOneShot(DefaultWatchdogTimerInterval);
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformiosWebVideoFullscreenModelh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/ios/WebVideoFullscreenModel.h (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/ios/WebVideoFullscreenModel.h        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebCore/platform/ios/WebVideoFullscreenModel.h        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -46,13 +46,14 @@
</span><span class="cx">     virtual void beginScanningForward() = 0;
</span><span class="cx">     virtual void beginScanningBackward() = 0;
</span><span class="cx">     virtual void endScanning() = 0;
</span><del>-    virtual void requestExitFullscreen() = 0;
</del><ins>+    virtual void requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenMode) = 0;
</ins><span class="cx">     virtual void setVideoLayerFrame(FloatRect) = 0;
</span><span class="cx">     enum VideoGravity { VideoGravityResize, VideoGravityResizeAspect, VideoGravityResizeAspectFill };
</span><span class="cx">     virtual void setVideoLayerGravity(VideoGravity) = 0;
</span><span class="cx">     virtual void selectAudioMediaOption(uint64_t index) = 0;
</span><span class="cx">     virtual void selectLegibleMediaOption(uint64_t index) = 0;
</span><span class="cx">     virtual void fullscreenModeChanged(HTMLMediaElementEnums::VideoFullscreenMode) = 0;
</span><ins>+    virtual bool isVisible() const = 0;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformiosWebVideoFullscreenModelVideoElementh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/ios/WebVideoFullscreenModelVideoElement.h (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/ios/WebVideoFullscreenModelVideoElement.h        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebCore/platform/ios/WebVideoFullscreenModelVideoElement.h        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -70,12 +70,13 @@
</span><span class="cx">     WEBCORE_EXPORT virtual void beginScanningForward() override;
</span><span class="cx">     WEBCORE_EXPORT virtual void beginScanningBackward() override;
</span><span class="cx">     WEBCORE_EXPORT virtual void endScanning() override;
</span><del>-    WEBCORE_EXPORT virtual void requestExitFullscreen() override;
</del><ins>+    WEBCORE_EXPORT virtual void requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenMode) override;
</ins><span class="cx">     WEBCORE_EXPORT virtual void setVideoLayerFrame(FloatRect) override;
</span><span class="cx">     WEBCORE_EXPORT virtual void setVideoLayerGravity(VideoGravity) override;
</span><span class="cx">     WEBCORE_EXPORT virtual void selectAudioMediaOption(uint64_t index) override;
</span><span class="cx">     WEBCORE_EXPORT virtual void selectLegibleMediaOption(uint64_t index) override;
</span><span class="cx">     WEBCORE_EXPORT virtual void fullscreenModeChanged(HTMLMediaElementEnums::VideoFullscreenMode) override;
</span><ins>+    WEBCORE_EXPORT virtual bool isVisible() const override;
</ins><span class="cx"> 
</span><span class="cx"> protected:
</span><span class="cx">     WEBCORE_EXPORT WebVideoFullscreenModelVideoElement();
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformiosWebVideoFullscreenModelVideoElementmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/ios/WebVideoFullscreenModelVideoElement.mm (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/ios/WebVideoFullscreenModelVideoElement.mm        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebCore/platform/ios/WebVideoFullscreenModelVideoElement.mm        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -112,7 +112,6 @@
</span><span class="cx"> 
</span><span class="cx"> void WebVideoFullscreenModelVideoElement::handleEvent(WebCore::ScriptExecutionContext*, WebCore::Event* event)
</span><span class="cx"> {
</span><del>-    LOG(Media, &quot;handleEvent %s&quot;, event-&gt;type().characters8());
</del><span class="cx">     updateForEventName(event-&gt;type());
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -245,10 +244,10 @@
</span><span class="cx">         m_videoElement-&gt;endScanning();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebVideoFullscreenModelVideoElement::requestExitFullscreen()
</del><ins>+void WebVideoFullscreenModelVideoElement::requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenMode mode)
</ins><span class="cx"> {
</span><del>-    if (m_videoElement &amp;&amp; m_videoElement-&gt;isFullscreen())
-        m_videoElement-&gt;exitFullscreen();
</del><ins>+    if (m_videoElement &amp;&amp; m_videoElement-&gt;fullscreenMode() != mode)
+        m_videoElement-&gt;setFullscreenMode(mode);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebVideoFullscreenModelVideoElement::setVideoLayerFrame(FloatRect rect)
</span><span class="lines">@@ -389,4 +388,15 @@
</span><span class="cx">         m_videoElement-&gt;fullscreenModeChanged(videoFullscreenMode);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool WebVideoFullscreenModelVideoElement::isVisible() const
+{
+    if (!m_videoElement)
+        return false;
+
+    if (Page* page = m_videoElement-&gt;document().page())
+        return page-&gt;isVisible();
+
+    return false;
+}
+
</ins><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformspicocoaAVKitSPIh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/spi/cocoa/AVKitSPI.h (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/spi/cocoa/AVKitSPI.h        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebCore/platform/spi/cocoa/AVKitSPI.h        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -96,6 +96,8 @@
</span><span class="cx"> - (void)stopPictureInPicture;
</span><span class="cx"> 
</span><span class="cx"> @property (nonatomic, strong) AVPlayerController *playerController;
</span><ins>+@property (nonatomic, readonly, getter=isPictureInPictureActive) BOOL pictureInPictureActive;
+@property (nonatomic, readonly) BOOL pictureInPictureWasStartedWhenEnteringBackground;
</ins><span class="cx"> @end
</span><span class="cx"> 
</span><span class="cx"> #endif // USE(APPLE_INTERNAL_SDK)
</span></span></pre></div>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebKit2/ChangeLog        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -1,3 +1,32 @@
</span><ins>+2015-12-03  Jer Noble  &lt;jer.noble@apple.com&gt;
+
+        [iOS] Fullscreen -&gt; PiP should resume to Fullscreen, not inline
+        https://bugs.webkit.org/show_bug.cgi?id=150906
+
+        Reviewed by Simon Fraser.
+
+        Add methods to pass isVisible(), applicationDidBecomeActive(), and requestFullscreenMode()
+        to and from models and interfaces.
+
+        * UIProcess/ios/WebPageProxyIOS.mm:
+        (WebKit::WebPageProxy::applicationDidBecomeActive): Notify the fullscreen manager, if preset.
+
+        * UIProcess/ios/WebVideoFullscreenManagerProxy.mm:
+        (WebKit::WebVideoFullscreenModelContext::requestFullscreenMode): Renamed from requestExitFullscreen(); takes a mode enum.
+        (WebKit::WebVideoFullscreenModelContext::isVisible): Added.
+        (WebKit::WebVideoFullscreenManagerProxy::applicationDidBecomeActive): Pass to all existing interfaces.
+        (WebKit::WebVideoFullscreenManagerProxy::setupFullscreenWithID): Do not re-create the view if it already exists.
+        (WebKit::WebVideoFullscreenManagerProxy::requestFullscreenMode): Renamed from requestExitFullscreen(); takes a mode enum.
+        (WebKit::WebVideoFullscreenManagerProxy::isVisible): Added. Query Page::isVisible() and Page::isInWindow(), the latter because
+                PiP will cause Page::isVisible() to be TRUE even when the containing tab is backgrounded.
+        (WebKit::WebVideoFullscreenModelContext::requestExitFullscreen): Deleted.
+        (WebKit::WebVideoFullscreenManagerProxy::requestExitFullscreen): Deleted.
+        * WebProcess/ios/WebVideoFullscreenManager.mm:
+        (WebKit::WebVideoFullscreenManager::enterVideoFullscreenForVideoElement): Only resize the view if we are not already in one of
+                the fullscreen modes.
+        (WebKit::WebVideoFullscreenManager::requestFullscreenMode): Renamed from requestExitFullscreen().
+        (WebKit::WebVideoFullscreenManager::requestExitFullscreen): Deleted.
+
</ins><span class="cx"> 2015-12-02  Antti Koivisto  &lt;antti@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Move ResourceLoadScheduler to WebKit1
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessiosWebPageProxyIOSmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebKit2/UIProcess/ios/WebPageProxyIOS.mm        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -632,6 +632,8 @@
</span><span class="cx"> 
</span><span class="cx"> void WebPageProxy::applicationDidBecomeActive()
</span><span class="cx"> {
</span><ins>+    if (m_videoFullscreenManager)
+        m_videoFullscreenManager-&gt;applicationDidBecomeActive();
</ins><span class="cx">     m_process-&gt;send(Messages::WebPage::ApplicationDidBecomeActive(), m_pageID);
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessiosWebVideoFullscreenManagerProxyh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ios/WebVideoFullscreenManagerProxy.h (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ios/WebVideoFullscreenManagerProxy.h        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebKit2/UIProcess/ios/WebVideoFullscreenManagerProxy.h        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -75,12 +75,13 @@
</span><span class="cx">     virtual void beginScanningForward() override;
</span><span class="cx">     virtual void beginScanningBackward() override;
</span><span class="cx">     virtual void endScanning() override;
</span><del>-    virtual void requestExitFullscreen() override;
</del><ins>+    virtual void requestFullscreenMode(WebCore::HTMLMediaElementEnums::VideoFullscreenMode) override;
</ins><span class="cx">     virtual void setVideoLayerFrame(WebCore::FloatRect) override;
</span><span class="cx">     virtual void setVideoLayerGravity(VideoGravity) override;
</span><span class="cx">     virtual void selectAudioMediaOption(uint64_t) override;
</span><span class="cx">     virtual void selectLegibleMediaOption(uint64_t) override;
</span><span class="cx">     virtual void fullscreenModeChanged(WebCore::HTMLMediaElementEnums::VideoFullscreenMode) override;
</span><ins>+    virtual bool isVisible() const override;
</ins><span class="cx"> 
</span><span class="cx">     // WebVideoFullscreenChangeObserver
</span><span class="cx">     virtual void didSetupFullscreen() override;
</span><span class="lines">@@ -104,6 +105,8 @@
</span><span class="cx">     void requestHideAndExitFullscreen();
</span><span class="cx">     bool hasMode(WebCore::HTMLMediaElementEnums::VideoFullscreenMode) const;
</span><span class="cx">     bool mayAutomaticallyShowVideoPictureInPicture() const;
</span><ins>+    void applicationDidBecomeActive();
+    bool isVisible() const;
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     friend class WebVideoFullscreenModelContext;
</span><span class="lines">@@ -147,7 +150,7 @@
</span><span class="cx">     void beginScanningForward(uint64_t contextId);
</span><span class="cx">     void beginScanningBackward(uint64_t contextId);
</span><span class="cx">     void endScanning(uint64_t contextId);
</span><del>-    void requestExitFullscreen(uint64_t contextId);
</del><ins>+    void requestFullscreenMode(uint64_t contextId, WebCore::HTMLMediaElementEnums::VideoFullscreenMode);
</ins><span class="cx">     void didSetupFullscreen(uint64_t contextId);
</span><span class="cx">     void didExitFullscreen(uint64_t contextId);
</span><span class="cx">     void didEnterFullscreen(uint64_t contextId);
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessiosWebVideoFullscreenManagerProxymm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ios/WebVideoFullscreenManagerProxy.mm (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ios/WebVideoFullscreenManagerProxy.mm        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebKit2/UIProcess/ios/WebVideoFullscreenManagerProxy.mm        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -156,10 +156,10 @@
</span><span class="cx">         m_manager-&gt;endScanning(m_contextId);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebVideoFullscreenModelContext::requestExitFullscreen()
</del><ins>+void WebVideoFullscreenModelContext::requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenMode mode)
</ins><span class="cx"> {
</span><span class="cx">     if (m_manager)
</span><del>-        m_manager-&gt;requestExitFullscreen(m_contextId);
</del><ins>+        m_manager-&gt;requestFullscreenMode(m_contextId, mode);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebVideoFullscreenModelContext::setVideoLayerFrame(WebCore::FloatRect frame)
</span><span class="lines">@@ -192,6 +192,11 @@
</span><span class="cx">         m_manager-&gt;fullscreenModeChanged(m_contextId, mode);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool WebVideoFullscreenModelContext::isVisible() const
+{
+    return m_manager ? m_manager-&gt;isVisible() : false;
+}
+
</ins><span class="cx"> void WebVideoFullscreenModelContext::didSetupFullscreen()
</span><span class="cx"> {
</span><span class="cx">     if (m_manager)
</span><span class="lines">@@ -284,6 +289,12 @@
</span><span class="cx">     return false;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebVideoFullscreenManagerProxy::applicationDidBecomeActive()
+{
+    for (auto&amp; tuple : m_contextMap.values())
+        std::get&lt;1&gt;(tuple)-&gt;applicationDidBecomeActive();
+}
+
</ins><span class="cx"> WebVideoFullscreenManagerProxy::ModelInterfaceTuple WebVideoFullscreenManagerProxy::createModelAndInterface(uint64_t contextId)
</span><span class="cx"> {
</span><span class="cx">     Ref&lt;WebVideoFullscreenModelContext&gt; model = WebVideoFullscreenModelContext::create(*this, contextId);
</span><span class="lines">@@ -323,13 +334,16 @@
</span><span class="cx"> 
</span><span class="cx">     std::tie(model, interface) = ensureModelAndInterface(contextId);
</span><span class="cx"> 
</span><del>-    RetainPtr&lt;WebLayerHostView&gt; view = adoptNS([[WebLayerHostView alloc] init]);
</del><ins>+    RetainPtr&lt;WebLayerHostView&gt; view = static_cast&lt;WebLayerHostView*&gt;(model-&gt;layerHostView());
+    if (!view) {
+        view = adoptNS([[WebLayerHostView alloc] init]);
+        model-&gt;setLayerHostView(view);
+    }
</ins><span class="cx">     [view setContextID:videoLayerID];
</span><del>-    model-&gt;setLayerHostView(view);
</del><span class="cx">     if (hostingDeviceScaleFactor != 1) {
</span><span class="cx">         // Invert the scale transform added in the WebProcess to fix &lt;rdar://problem/18316542&gt;.
</span><span class="cx">         float inverseScale = 1 / hostingDeviceScaleFactor;
</span><del>-        [[model-&gt;layerHostView() layer] setSublayerTransform:CATransform3DMakeScale(inverseScale, inverseScale, 1)];
</del><ins>+        [[view layer] setSublayerTransform:CATransform3DMakeScale(inverseScale, inverseScale, 1)];
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     UIView *parentView = downcast&lt;RemoteLayerTreeDrawingAreaProxy&gt;(*m_page-&gt;drawingArea()).remoteLayerTreeHost().rootLayer();
</span><span class="lines">@@ -494,9 +508,9 @@
</span><span class="cx">     m_page-&gt;send(Messages::WebVideoFullscreenManager::EndScanning(contextId), m_page-&gt;pageID());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebVideoFullscreenManagerProxy::requestExitFullscreen(uint64_t contextId)
</del><ins>+void WebVideoFullscreenManagerProxy::requestFullscreenMode(uint64_t contextId, WebCore::HTMLMediaElementEnums::VideoFullscreenMode mode)
</ins><span class="cx"> {
</span><del>-    m_page-&gt;send(Messages::WebVideoFullscreenManager::RequestExitFullscreen(contextId), m_page-&gt;pageID());
</del><ins>+    m_page-&gt;send(Messages::WebVideoFullscreenManager::RequestFullscreenMode(contextId, mode), m_page-&gt;pageID());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebVideoFullscreenManagerProxy::didSetupFullscreen(uint64_t contextId)
</span><span class="lines">@@ -557,6 +571,11 @@
</span><span class="cx">     m_page-&gt;send(Messages::WebVideoFullscreenManager::FullscreenModeChanged(contextId, mode), m_page-&gt;pageID());
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+bool WebVideoFullscreenManagerProxy::isVisible() const
+{
+    return m_page-&gt;isViewVisible() &amp;&amp; m_page-&gt;isInWindow();
+}
+
</ins><span class="cx"> void WebVideoFullscreenManagerProxy::fullscreenMayReturnToInline(uint64_t contextId)
</span><span class="cx"> {
</span><span class="cx">     bool isViewVisible = m_page-&gt;isViewVisible();
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessiosWebVideoFullscreenManagerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.h (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.h        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.h        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -153,7 +153,7 @@
</span><span class="cx">     void beginScanningForward(uint64_t contextId);
</span><span class="cx">     void beginScanningBackward(uint64_t contextId);
</span><span class="cx">     void endScanning(uint64_t contextId);
</span><del>-    void requestExitFullscreen(uint64_t contextId);
</del><ins>+    void requestFullscreenMode(uint64_t contextId, WebCore::HTMLMediaElementEnums::VideoFullscreenMode);
</ins><span class="cx">     void didSetupFullscreen(uint64_t contextId);
</span><span class="cx">     void didExitFullscreen(uint64_t contextId);
</span><span class="cx">     void didEnterFullscreen(uint64_t contextId);
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessiosWebVideoFullscreenManagermessagesin"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.messages.in (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.messages.in        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.messages.in        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -32,7 +32,7 @@
</span><span class="cx">     BeginScanningForward(uint64_t contextId)
</span><span class="cx">     BeginScanningBackward(uint64_t contextId)
</span><span class="cx">     EndScanning(uint64_t contextId)
</span><del>-    RequestExitFullscreen(uint64_t contextId)
</del><ins>+    RequestFullscreenMode(uint64_t contextId, WebCore::HTMLMediaElementEnums::VideoFullscreenMode videoFullscreenMode)
</ins><span class="cx">     DidSetupFullscreen(uint64_t contextId)
</span><span class="cx">     DidExitFullscreen(uint64_t contextId)
</span><span class="cx">     DidEnterFullscreen(uint64_t contextId)
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessiosWebVideoFullscreenManagermm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.mm (193339 => 193340)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.mm        2015-12-03 19:12:25 UTC (rev 193339)
+++ trunk/Source/WebKit2/WebProcess/ios/WebVideoFullscreenManager.mm        2015-12-03 19:12:26 UTC (rev 193340)
</span><span class="lines">@@ -240,11 +240,13 @@
</span><span class="cx"> 
</span><span class="cx">     FloatRect clientRect = clientRectForElement(&amp;videoElement);
</span><span class="cx">     FloatRect videoLayerFrame = FloatRect(0, 0, clientRect.width(), clientRect.height());
</span><del>-    
</del><ins>+
+    HTMLMediaElementEnums::VideoFullscreenMode oldMode = interface-&gt;fullscreenMode();
</ins><span class="cx">     interface-&gt;setTargetIsFullscreen(true);
</span><span class="cx">     interface-&gt;setFullscreenMode(mode);
</span><span class="cx">     model-&gt;setVideoElement(&amp;videoElement);
</span><del>-    model-&gt;setVideoLayerFrame(videoLayerFrame);
</del><ins>+    if (oldMode == HTMLMediaElementEnums::VideoFullscreenModeNone)
+        model-&gt;setVideoLayerFrame(videoLayerFrame);
</ins><span class="cx"> 
</span><span class="cx">     if (interface-&gt;isAnimating())
</span><span class="cx">         return;
</span><span class="lines">@@ -394,9 +396,9 @@
</span><span class="cx">     ensureModel(contextId).endScanning();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebVideoFullscreenManager::requestExitFullscreen(uint64_t contextId)
</del><ins>+void WebVideoFullscreenManager::requestFullscreenMode(uint64_t contextId, WebCore::HTMLMediaElementEnums::VideoFullscreenMode mode)
</ins><span class="cx"> {
</span><del>-    ensureModel(contextId).requestExitFullscreen();
</del><ins>+    ensureModel(contextId).requestFullscreenMode(mode);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebVideoFullscreenManager::selectAudioMediaOption(uint64_t contextId, uint64_t index)
</span></span></pre>
</div>
</div>

</body>
</html>