<!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>[55570] 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/55570">55570</a></dd>
<dt>Author</dt> <dd>ukai@chromium.org</dd>
<dt>Date</dt> <dd>2010-03-04 21:50:38 -0800 (Thu, 04 Mar 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>WebSocket crash bug when reloading the page while the WebSocket is busy
https://bugs.webkit.org/show_bug.cgi?id=35732

Reviewed by Alexey Proskuryakov.

WebCore:

* websockets/WebSocketChannel.cpp:
(WebCore::WebSocketChannel::disconnect): clear m_context from WebSocketChannel and WebSocketHandshake.
(WebCore::WebSocketChannel::didOpen): check m_context
(WebCore::WebSocketChannel::didClose): clear m_context
(WebCore::WebSocketChannel::didReceiveData): check m_context, and check m_client early before appending to buffer
* websockets/WebSocketHandshake.cpp:
(WebCore::WebSocketHandshake::clearScriptExecutionContext): Added.
* websockets/WebSocketHandshake.h:

LayoutTests:

* websocket/tests/reload-crash-expected.txt: Added.
* websocket/tests/reload-crash.html: Added.
* websocket/tests/resoures/reload-crash-iframe.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkWebCoreChangeLog">trunk/WebCore/ChangeLog</a></li>
<li><a href="#trunkWebCorewebsocketsWebSocketChannelcpp">trunk/WebCore/websockets/WebSocketChannel.cpp</a></li>
<li><a href="#trunkWebCorewebsocketsWebSocketHandshakecpp">trunk/WebCore/websockets/WebSocketHandshake.cpp</a></li>
<li><a href="#trunkWebCorewebsocketsWebSocketHandshakeh">trunk/WebCore/websockets/WebSocketHandshake.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestswebsockettestsreloadcrashexpectedtxt">trunk/LayoutTests/websocket/tests/reload-crash-expected.txt</a></li>
<li><a href="#trunkLayoutTestswebsockettestsreloadcrashhtml">trunk/LayoutTests/websocket/tests/reload-crash.html</a></li>
<li><a href="#trunkLayoutTestswebsockettestsresourcesreloadcrashiframehtml">trunk/LayoutTests/websocket/tests/resources/reload-crash-iframe.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (55569 => 55570)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2010-03-05 05:35:39 UTC (rev 55569)
+++ trunk/LayoutTests/ChangeLog        2010-03-05 05:50:38 UTC (rev 55570)
</span><span class="lines">@@ -1,3 +1,14 @@
</span><ins>+2010-03-04  Fumitoshi Ukai  &lt;ukai@chromium.org&gt;
+
+        Reviewed by Alexey Proskuryakov.
+
+        WebSocket crash bug when reloading the page while the WebSocket is busy
+        https://bugs.webkit.org/show_bug.cgi?id=35732
+
+        * websocket/tests/reload-crash-expected.txt: Added.
+        * websocket/tests/reload-crash.html: Added.
+        * websocket/tests/resoures/reload-crash-iframe.html: Added.
+
</ins><span class="cx"> 2010-03-04  MORITA Hajime &lt;morrita@google.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Reviewed by Alexey Proskuryakov.
</span></span></pre></div>
<a id="trunkLayoutTestswebsockettestsreloadcrashexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/websocket/tests/reload-crash-expected.txt (0 => 55570)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/websocket/tests/reload-crash-expected.txt                                (rev 0)
+++ trunk/LayoutTests/websocket/tests/reload-crash-expected.txt        2010-03-05 05:50:38 UTC (rev 55570)
</span><span class="lines">@@ -0,0 +1,11 @@
</span><ins>+Test if it doesn't crash when reloading while Web Socket is busy
+
+On success, you will see a series of &quot;PASS&quot; messages, followed by &quot;TEST COMPLETE&quot;.
+
+PASS insert a iframe
+PASS iframe is ready.
+PASS reloaded iframe while WebSocket is busy
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
</ins></span></pre></div>
<a id="trunkLayoutTestswebsockettestsreloadcrashhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/websocket/tests/reload-crash.html (0 => 55570)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/websocket/tests/reload-crash.html                                (rev 0)
+++ trunk/LayoutTests/websocket/tests/reload-crash.html        2010-03-05 05:50:38 UTC (rev 55570)
</span><span class="lines">@@ -0,0 +1,48 @@
</span><ins>+&lt;!DOCTYPE HTML PUBLIC &quot;-//IETF//DTD HTML//EN&quot;&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;link rel=&quot;stylesheet&quot; href=&quot;../../fast/js/resources/js-test-style.css&quot;&gt;
+&lt;script src=&quot;../../fast/js/resources/js-test-pre.js&quot;&gt;&lt;/script&gt;
+&lt;script src=&quot;../../fast/js/resources/js-test-post-function.js&quot;&gt;&lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;div id=&quot;description&quot;&gt;&lt;/div&gt;
+&lt;div id=&quot;console&quot;&gt;&lt;/div&gt;
+&lt;script type=&quot;text/javascript&quot;&gt;
+description(&quot;Test if it doesn't crash when reloading while Web Socket is busy&quot;);
+
+if (window.layoutTestController)
+    layoutTestController.waitUntilDone();
+
+var frameDiv;
+var reloadCount = 0;
+
+function endTest()
+{
+    isSuccessfullyParsed();
+    if (window.layoutTestController)
+        layoutTestController.notifyDone();
+};
+
+document.iframeReady = function ()
+{
+    if (reloadCount == 0)
+       debug(&quot;PASS iframe is ready.&quot;);
+    if (reloadCount == 1) {
+       debug(&quot;PASS reloaded iframe while WebSocket is busy&quot;);
+       endTest();
+       return;
+    }
+    reloadCount += 1;
+};
+
+frameDiv = document.createElement(&quot;iframe&quot;);
+frameDiv.src = &quot;resources/reload-crash-iframe.html&quot;;
+document.body.appendChild(frameDiv);
+debug(&quot;PASS insert a iframe&quot;);
+
+var successfullyParsed = true;
+&lt;/script&gt;
+
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkLayoutTestswebsockettestsresourcesreloadcrashiframehtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/websocket/tests/resources/reload-crash-iframe.html (0 => 55570)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/websocket/tests/resources/reload-crash-iframe.html                                (rev 0)
+++ trunk/LayoutTests/websocket/tests/resources/reload-crash-iframe.html        2010-03-05 05:50:38 UTC (rev 55570)
</span><span class="lines">@@ -0,0 +1,15 @@
</span><ins>+&lt;html&gt;
+&lt;head&gt;
+&lt;script src=&quot;../../../fast/js/resources/js-test-pre.js&quot;&gt;&lt;/script&gt;
+&lt;script src=&quot;../../../fast/js/resources/js-test-post-function.js&quot;&gt;&lt;/script&gt;
+&lt;/head&gt;
+&lt;body&gt;
+&lt;div id=&quot;description&quot;&gt;&lt;/div&gt;
+&lt;div id=&quot;console&quot;&gt;&lt;/div&gt;
+&lt;script type=&quot;text/javascript&quot;&gt;
+parent.document.iframeReady();
+var ws = new WebSocket(&quot;ws://127.0.0.1:8880/websocket/tests/close-on-unload&quot;);
+location.reload();
+&lt;/script&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/WebCore/ChangeLog (55569 => 55570)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebCore/ChangeLog        2010-03-05 05:35:39 UTC (rev 55569)
+++ trunk/WebCore/ChangeLog        2010-03-05 05:50:38 UTC (rev 55570)
</span><span class="lines">@@ -1,3 +1,19 @@
</span><ins>+2010-03-04  Fumitoshi Ukai  &lt;ukai@chromium.org&gt;
+
+        Reviewed by Alexey Proskuryakov.
+
+        WebSocket crash bug when reloading the page while the WebSocket is busy
+        https://bugs.webkit.org/show_bug.cgi?id=35732
+
+        * websockets/WebSocketChannel.cpp:
+        (WebCore::WebSocketChannel::disconnect): clear m_context from WebSocketChannel and WebSocketHandshake.
+        (WebCore::WebSocketChannel::didOpen): check m_context
+        (WebCore::WebSocketChannel::didClose): clear m_context
+        (WebCore::WebSocketChannel::didReceiveData): check m_context, and check m_client early before appending to buffer
+        * websockets/WebSocketHandshake.cpp:
+        (WebCore::WebSocketHandshake::clearScriptExecutionContext): Added.
+        * websockets/WebSocketHandshake.h:
+
</ins><span class="cx"> 2010-03-04  MORITA Hajime &lt;morrita@google.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Reviewed by Alexey Proskuryakov.
</span></span></pre></div>
<a id="trunkWebCorewebsocketsWebSocketChannelcpp"></a>
<div class="modfile"><h4>Modified: trunk/WebCore/websockets/WebSocketChannel.cpp (55569 => 55570)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebCore/websockets/WebSocketChannel.cpp        2010-03-05 05:35:39 UTC (rev 55569)
+++ trunk/WebCore/websockets/WebSocketChannel.cpp        2010-03-05 05:50:38 UTC (rev 55570)
</span><span class="lines">@@ -103,7 +103,9 @@
</span><span class="cx"> void WebSocketChannel::disconnect()
</span><span class="cx"> {
</span><span class="cx">     LOG(Network, &quot;WebSocketChannel %p disconnect&quot;, this);
</span><ins>+    m_handshake.clearScriptExecutionContext();
</ins><span class="cx">     m_client = 0;
</span><ins>+    m_context = 0;
</ins><span class="cx">     if (m_handle)
</span><span class="cx">         m_handle-&gt;close();
</span><span class="cx"> }
</span><span class="lines">@@ -112,6 +114,8 @@
</span><span class="cx"> {
</span><span class="cx">     LOG(Network, &quot;WebSocketChannel %p didOpen&quot;, this);
</span><span class="cx">     ASSERT(handle == m_handle);
</span><ins>+    if (!m_context)
+        return;
</ins><span class="cx">     const CString&amp; handshakeMessage = m_handshake.clientHandshakeMessage();
</span><span class="cx">     if (!handle-&gt;send(handshakeMessage.data(), handshakeMessage.length())) {
</span><span class="cx">         m_context-&gt;addMessage(ConsoleDestination, JSMessageSource, LogMessageType, ErrorMessageLevel, &quot;Error sending handshake message.&quot;, 0, m_handshake.clientOrigin());
</span><span class="lines">@@ -127,6 +131,7 @@
</span><span class="cx">         unsigned long unhandledBufferedAmount = m_handle-&gt;bufferedAmount();
</span><span class="cx">         WebSocketChannelClient* client = m_client;
</span><span class="cx">         m_client = 0;
</span><ins>+        m_context = 0;
</ins><span class="cx">         m_handle = 0;
</span><span class="cx">         if (client)
</span><span class="cx">             client-&gt;didClose(unhandledBufferedAmount);
</span><span class="lines">@@ -139,14 +144,17 @@
</span><span class="cx">     LOG(Network, &quot;WebSocketChannel %p didReceiveData %d&quot;, this, len);
</span><span class="cx">     RefPtr&lt;WebSocketChannel&gt; protect(this); // The client can close the channel, potentially removing the last reference.
</span><span class="cx">     ASSERT(handle == m_handle);
</span><del>-    if (!appendToBuffer(data, len)) {
-        handle-&gt;close();
</del><ins>+    if (!m_context) {
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx">     if (!m_client) {
</span><span class="cx">         handle-&gt;close();
</span><span class="cx">         return;
</span><span class="cx">     }
</span><ins>+    if (!appendToBuffer(data, len)) {
+        handle-&gt;close();
+        return;
+    }
</ins><span class="cx">     if (m_handshake.mode() == WebSocketHandshake::Incomplete) {
</span><span class="cx">         int headerLength = m_handshake.readServerHandshake(m_buffer, m_bufferSize);
</span><span class="cx">         if (headerLength &lt;= 0)
</span></span></pre></div>
<a id="trunkWebCorewebsocketsWebSocketHandshakecpp"></a>
<div class="modfile"><h4>Modified: trunk/WebCore/websockets/WebSocketHandshake.cpp (55569 => 55570)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebCore/websockets/WebSocketHandshake.cpp        2010-03-05 05:35:39 UTC (rev 55569)
+++ trunk/WebCore/websockets/WebSocketHandshake.cpp        2010-03-05 05:50:38 UTC (rev 55570)
</span><span class="lines">@@ -221,6 +221,11 @@
</span><span class="cx">     m_setCookie2 = String();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebSocketHandshake::clearScriptExecutionContext()
+{
+    m_context = 0;
+}
+
</ins><span class="cx"> int WebSocketHandshake::readServerHandshake(const char* header, size_t len)
</span><span class="cx"> {
</span><span class="cx">     m_mode = Incomplete;
</span></span></pre></div>
<a id="trunkWebCorewebsocketsWebSocketHandshakeh"></a>
<div class="modfile"><h4>Modified: trunk/WebCore/websockets/WebSocketHandshake.h (55569 => 55570)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/WebCore/websockets/WebSocketHandshake.h        2010-03-05 05:35:39 UTC (rev 55569)
+++ trunk/WebCore/websockets/WebSocketHandshake.h        2010-03-05 05:50:38 UTC (rev 55570)
</span><span class="lines">@@ -67,6 +67,7 @@
</span><span class="cx">         WebSocketHandshakeRequest clientHandshakeRequest() const;
</span><span class="cx"> 
</span><span class="cx">         void reset();
</span><ins>+        void clearScriptExecutionContext();
</ins><span class="cx"> 
</span><span class="cx">         int readServerHandshake(const char* header, size_t len);
</span><span class="cx">         Mode mode() const;
</span></span></pre>
</div>
</div>

</body>
</html>