<!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>[182517] trunk</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/182517">182517</a></dd>
<dt>Author</dt> <dd>cdumez@apple.com</dd>
<dt>Date</dt> <dd>2015-04-07 21:24:24 -0700 (Tue, 07 Apr 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Open WebSockets should not prevent a page from entering PageCache
https://bugs.webkit.org/show_bug.cgi?id=143505
&lt;rdar://problem/19923085&gt;

Reviewed by Alexey Proskuryakov.

Source/WebCore:

Open WebSockets should not prevent a page from entering PageCache. This
is currently causing mobile.nytimes.com to not be page-cacheable.

In this patch, We close open WebSockets when entering the page cache
and fire the &quot;close&quot; events after resuming, similarly to what we did
for XMLHttpRequest in <a href="http://trac.webkit.org/projects/webkit/changeset/181480">r181480</a>. This gives a chance for the content to
handle the 'close' event (with wasClean being false and code being
1006) in order to reopen the connection if necessary.

Test: http/tests/websocket/tests/hybi/closed-when-entering-page-cache.html

* Modules/websockets/WebSocket.cpp:
(WebCore::WebSocket::WebSocket):
(WebCore::WebSocket::canSuspend):
(WebCore::WebSocket::suspend):
(WebCore::WebSocket::resume):
(WebCore::WebSocket::resumeTimerFired):
(WebCore::WebSocket::didClose):
* Modules/websockets/WebSocket.h:

LayoutTests:

Add a layout test to check that an open WebSocket does not prevent a
page from entering page cache and that a 'close' event is fired after
resuming (restoring from the page cache).

* http/tests/websocket/tests/hybi/closed-when-entering-page-cache-expected.txt: Added.
* http/tests/websocket/tests/hybi/closed-when-entering-page-cache.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModuleswebsocketsWebSocketcpp">trunk/Source/WebCore/Modules/websockets/WebSocket.cpp</a></li>
<li><a href="#trunkSourceWebCoreModuleswebsocketsWebSocketh">trunk/Source/WebCore/Modules/websockets/WebSocket.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestshttptestswebsockettestshybiclosedwhenenteringpagecacheexpectedtxt">trunk/LayoutTests/http/tests/websocket/tests/hybi/closed-when-entering-page-cache-expected.txt</a></li>
<li><a href="#trunkLayoutTestshttptestswebsockettestshybiclosedwhenenteringpagecachehtml">trunk/LayoutTests/http/tests/websocket/tests/hybi/closed-when-entering-page-cache.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (182516 => 182517)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2015-04-08 04:15:01 UTC (rev 182516)
+++ trunk/LayoutTests/ChangeLog        2015-04-08 04:24:24 UTC (rev 182517)
</span><span class="lines">@@ -1,3 +1,18 @@
</span><ins>+2015-04-07  Chris Dumez  &lt;cdumez@apple.com&gt;
+
+        Open WebSockets should not prevent a page from entering PageCache
+        https://bugs.webkit.org/show_bug.cgi?id=143505
+        &lt;rdar://problem/19923085&gt;
+
+        Reviewed by Alexey Proskuryakov.
+
+        Add a layout test to check that an open WebSocket does not prevent a
+        page from entering page cache and that a 'close' event is fired after
+        resuming (restoring from the page cache).
+
+        * http/tests/websocket/tests/hybi/closed-when-entering-page-cache-expected.txt: Added.
+        * http/tests/websocket/tests/hybi/closed-when-entering-page-cache.html: Added.
+
</ins><span class="cx"> 2015-04-07  Commit Queue  &lt;commit-queue@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed, rolling out r182511.
</span></span></pre></div>
<a id="trunkLayoutTestshttptestswebsockettestshybiclosedwhenenteringpagecacheexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/websocket/tests/hybi/closed-when-entering-page-cache-expected.txt (0 => 182517)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/websocket/tests/hybi/closed-when-entering-page-cache-expected.txt                                (rev 0)
+++ trunk/LayoutTests/http/tests/websocket/tests/hybi/closed-when-entering-page-cache-expected.txt        2015-04-08 04:24:24 UTC (rev 182517)
</span><span class="lines">@@ -0,0 +1,18 @@
</span><ins>+CONSOLE MESSAGE: WebSocket connection to 'ws://127.0.0.1:8880/websocket/tests/hybi/echo' failed: WebSocket is closed due to suspension.
+Test that an open WebSocket does not prevent a page from entering page cache.
+
+On success, you will see a series of &quot;PASS&quot; messages, followed by &quot;TEST COMPLETE&quot;.
+
+
+pageshow - not from cache
+pagehide - entering cache
+pageshow - from cache
+PASS Page did enter and was restored from the page cache
+PASS WebSocket was closed
+PASS wasRestoredFromPageCache is true
+PASS closeEvent.wasClean is false
+PASS closeEvent.code is 1006
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestswebsockettestshybiclosedwhenenteringpagecachehtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/websocket/tests/hybi/closed-when-entering-page-cache.html (0 => 182517)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/websocket/tests/hybi/closed-when-entering-page-cache.html                                (rev 0)
+++ trunk/LayoutTests/http/tests/websocket/tests/hybi/closed-when-entering-page-cache.html        2015-04-08 04:24:24 UTC (rev 182517)
</span><span class="lines">@@ -0,0 +1,53 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;body&gt;
+&lt;script src=&quot;/js-test-resources/js-test-pre.js&quot;&gt;&lt;/script&gt;
+&lt;script&gt;
+description(&quot;Test that an open WebSocket does not prevent a page from entering page cache.&quot;);
+
+window.jsTestIsAsync = true;
+if (window.testRunner)
+    testRunner.overridePreference(&quot;WebKitUsesPageCachePreferenceKey&quot;, 1);
+
+var wasRestoredFromPageCache = false;
+
+window.addEventListener(&quot;pageshow&quot;, function(event) {
+    debug(&quot;pageshow - &quot; + (event.persisted ? &quot;&quot; : &quot;not &quot;) + &quot;from cache&quot;);
+
+    if (event.persisted) {
+        testPassed(&quot;Page did enter and was restored from the page cache&quot;);
+        wasRestoredFromPageCache = true;
+    }
+}, false);
+
+window.addEventListener(&quot;pagehide&quot;, function(event) {
+    debug(&quot;pagehide - &quot; + (event.persisted ? &quot;&quot; : &quot;not &quot;) + &quot;entering cache&quot;);
+    if (!event.persisted) {
+        testFailed(&quot;Page did not enter the page cache.&quot;);
+        finishJSTest();
+    }
+}, false);
+
+window.addEventListener('load', function() {
+    ws = new WebSocket(&quot;ws://127.0.0.1:8880/websocket/tests/hybi/echo&quot;);
+    ws.onclose = function(ev) {
+        testPassed(&quot;WebSocket was closed&quot;);
+        shouldBeTrue(&quot;wasRestoredFromPageCache&quot;);
+        closeEvent = ev;
+        shouldBeFalse(&quot;closeEvent.wasClean&quot;);
+        shouldBe(&quot;closeEvent.code&quot;, &quot;1006&quot;);
+        finishJSTest();
+    };
+
+    // This needs to happen in a setTimeout because a navigation inside the onload handler would
+    // not create a history entry.
+    setTimeout(function() {
+      // Force a back navigation back to this page.
+      window.location.href = &quot;/navigation/resources/page-cache-helper.html&quot;;
+    }, 0);
+}, false);
+
+&lt;/script&gt;
+&lt;script src=&quot;/js-test-resources/js-test-post.js&quot;&gt;&lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (182516 => 182517)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-04-08 04:15:01 UTC (rev 182516)
+++ trunk/Source/WebCore/ChangeLog        2015-04-08 04:24:24 UTC (rev 182517)
</span><span class="lines">@@ -1,3 +1,31 @@
</span><ins>+2015-04-07  Chris Dumez  &lt;cdumez@apple.com&gt;
+
+        Open WebSockets should not prevent a page from entering PageCache
+        https://bugs.webkit.org/show_bug.cgi?id=143505
+        &lt;rdar://problem/19923085&gt;
+
+        Reviewed by Alexey Proskuryakov.
+
+        Open WebSockets should not prevent a page from entering PageCache. This
+        is currently causing mobile.nytimes.com to not be page-cacheable.
+
+        In this patch, We close open WebSockets when entering the page cache
+        and fire the &quot;close&quot; events after resuming, similarly to what we did
+        for XMLHttpRequest in r181480. This gives a chance for the content to
+        handle the 'close' event (with wasClean being false and code being
+        1006) in order to reopen the connection if necessary.
+
+        Test: http/tests/websocket/tests/hybi/closed-when-entering-page-cache.html
+
+        * Modules/websockets/WebSocket.cpp:
+        (WebCore::WebSocket::WebSocket):
+        (WebCore::WebSocket::canSuspend):
+        (WebCore::WebSocket::suspend):
+        (WebCore::WebSocket::resume):
+        (WebCore::WebSocket::resumeTimerFired):
+        (WebCore::WebSocket::didClose):
+        * Modules/websockets/WebSocket.h:
+
</ins><span class="cx"> 2015-04-07  Simon Fraser  &lt;simon.fraser@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Add experimental code to use custom font dilation when rendering into non-opaque contexts
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebsocketsWebSocketcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/websockets/WebSocket.cpp (182516 => 182517)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/websockets/WebSocket.cpp        2015-04-08 04:15:01 UTC (rev 182516)
+++ trunk/Source/WebCore/Modules/websockets/WebSocket.cpp        2015-04-08 04:24:24 UTC (rev 182517)
</span><span class="lines">@@ -146,6 +146,7 @@
</span><span class="cx">     , m_binaryType(BinaryTypeBlob)
</span><span class="cx">     , m_subprotocol(&quot;&quot;)
</span><span class="cx">     , m_extensions(&quot;&quot;)
</span><ins>+    , m_resumeTimer(*this, &amp;WebSocket::resumeTimerFired)
</ins><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -462,21 +463,41 @@
</span><span class="cx"> 
</span><span class="cx"> bool WebSocket::canSuspend() const
</span><span class="cx"> {
</span><del>-    return !m_channel;
</del><ins>+    return true;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebSocket::suspend(ReasonForSuspension)
</del><ins>+void WebSocket::suspend(ReasonForSuspension reason)
</ins><span class="cx"> {
</span><del>-    if (m_channel)
-        m_channel-&gt;suspend();
</del><ins>+    if (m_resumeTimer.isActive())
+        m_resumeTimer.stop();
+
+    if (m_channel) {
+        if (reason == ActiveDOMObject::DocumentWillBecomeInactive) {
+            // The page will enter the page cache, close the channel but only fire the close event after resuming.
+            m_shouldDelayCloseEvent = true;
+            // This will cause didClose() to be called.
+            m_channel-&gt;fail(&quot;WebSocket is closed due to suspension.&quot;);
+        } else
+            m_channel-&gt;suspend();
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebSocket::resume()
</span><span class="cx"> {
</span><span class="cx">     if (m_channel)
</span><span class="cx">         m_channel-&gt;resume();
</span><ins>+    else if (m_pendingCloseEvent &amp;&amp; !m_resumeTimer.isActive()) {
+        // Fire the close event in a timer as we are not allowed to execute arbitrary JS from resume().
+        m_resumeTimer.startOneShot(0);
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebSocket::resumeTimerFired()
+{
+    ASSERT(m_pendingCloseEvent);
+    dispatchEvent(WTF::move(m_pendingCloseEvent));
+}
+
</ins><span class="cx"> void WebSocket::stop()
</span><span class="cx"> {
</span><span class="cx">     bool pending = hasPendingActivity();
</span><span class="lines">@@ -565,11 +586,17 @@
</span><span class="cx">     m_state = CLOSED;
</span><span class="cx">     m_bufferedAmount = unhandledBufferedAmount;
</span><span class="cx">     ASSERT(scriptExecutionContext());
</span><ins>+
</ins><span class="cx">     RefPtr&lt;CloseEvent&gt; event = CloseEvent::create(wasClean, code, reason);
</span><del>-    dispatchEvent(event);
</del><ins>+    if (m_shouldDelayCloseEvent) {
+        m_pendingCloseEvent = WTF::move(event);
+        m_shouldDelayCloseEvent = false;
+    } else
+        dispatchEvent(event);
+
</ins><span class="cx">     if (m_channel) {
</span><span class="cx">         m_channel-&gt;disconnect();
</span><del>-        m_channel = 0;
</del><ins>+        m_channel = nullptr;
</ins><span class="cx">     }
</span><span class="cx">     if (hasPendingActivity())
</span><span class="cx">         ActiveDOMObject::unsetPendingActivity(this);
</span></span></pre></div>
<a id="trunkSourceWebCoreModuleswebsocketsWebSocketh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/websockets/WebSocket.h (182516 => 182517)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/websockets/WebSocket.h        2015-04-08 04:15:01 UTC (rev 182516)
+++ trunk/Source/WebCore/Modules/websockets/WebSocket.h        2015-04-08 04:24:24 UTC (rev 182517)
</span><span class="lines">@@ -46,6 +46,7 @@
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="cx"> class Blob;
</span><ins>+class CloseEvent;
</ins><span class="cx"> class ThreadableWebSocketChannel;
</span><span class="cx"> 
</span><span class="cx"> class WebSocket final : public RefCounted&lt;WebSocket&gt;, public EventTargetWithInlineData, public ActiveDOMObject, public WebSocketChannelClient {
</span><span class="lines">@@ -108,6 +109,8 @@
</span><span class="cx"> private:
</span><span class="cx">     explicit WebSocket(ScriptExecutionContext&amp;);
</span><span class="cx"> 
</span><ins>+    void resumeTimerFired();
+
</ins><span class="cx">     // ActiveDOMObject API.
</span><span class="cx">     void contextDestroyed() override;
</span><span class="cx">     bool canSuspend() const override;
</span><span class="lines">@@ -135,6 +138,10 @@
</span><span class="cx">     BinaryType m_binaryType;
</span><span class="cx">     String m_subprotocol;
</span><span class="cx">     String m_extensions;
</span><ins>+
+    Timer m_resumeTimer;
+    bool m_shouldDelayCloseEvent { false };
+    RefPtr&lt;CloseEvent&gt; m_pendingCloseEvent;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre>
</div>
</div>

</body>
</html>