<!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>[184846] 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/184846">184846</a></dd>
<dt>Author</dt> <dd>weinig@apple.com</dd>
<dt>Date</dt> <dd>2015-05-24 16:22:06 -0700 (Sun, 24 May 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Crash when using a removed ScriptMessageHandler
&lt;rdar://problem/20888499&gt;
https://bugs.webkit.org/show_bug.cgi?id=145359

Reviewed by Dan Bernstein.

Source/WebCore:

Added tests:
    WKUserContentController.ScriptMessageHandlerBasicRemove
    WKUserContentController.ScriptMessageHandlerCallRemovedHandler

* page/UserMessageHandler.cpp:
(WebCore::UserMessageHandler::~UserMessageHandler):
(WebCore::UserMessageHandler::postMessage):
(WebCore::UserMessageHandler::name):
* page/UserMessageHandler.h:
(WebCore::UserMessageHandler::create):
* page/UserMessageHandler.idl:
* page/UserMessageHandlerDescriptor.cpp:
(WebCore::UserMessageHandlerDescriptor::UserMessageHandlerDescriptor):
* page/UserMessageHandlerDescriptor.h:
(WebCore::UserMessageHandlerDescriptor::client):
(WebCore::UserMessageHandlerDescriptor::invalidateClient):
Add support for invalidating the descriptor and throw an exception if someone tries
to post a message using an invalidated descriptor.

* page/UserMessageHandlersNamespace.cpp:
(WebCore::UserMessageHandlersNamespace::handler):
Add logic to remove message handlers if their descriptor has been invalidated.

Source/WebKit2:

* WebProcess/UserContent/WebUserContentController.cpp:
(WebKit::WebUserMessageHandlerDescriptorProxy::~WebUserMessageHandlerDescriptorProxy):
Invalidate the descriptor when the message handler client (as implemented by WebUserMessageHandlerDescriptorProxy)
goes away. This will happen if a script message handler is removed at the API level or the WebUserContentController
is destroyed (which will happen if all the pages get destroyed).

Tools:

* TestWebKitAPI/Tests/WebKit2Cocoa/UserContentController.mm:
Add tests for removing script message handlers.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorebindingsgobjectWebKitDOMCustomcpp">trunk/Source/WebCore/bindings/gobject/WebKitDOMCustom.cpp</a></li>
<li><a href="#trunkSourceWebCorepageUserMessageHandlercpp">trunk/Source/WebCore/page/UserMessageHandler.cpp</a></li>
<li><a href="#trunkSourceWebCorepageUserMessageHandlerh">trunk/Source/WebCore/page/UserMessageHandler.h</a></li>
<li><a href="#trunkSourceWebCorepageUserMessageHandleridl">trunk/Source/WebCore/page/UserMessageHandler.idl</a></li>
<li><a href="#trunkSourceWebCorepageUserMessageHandlerDescriptorcpp">trunk/Source/WebCore/page/UserMessageHandlerDescriptor.cpp</a></li>
<li><a href="#trunkSourceWebCorepageUserMessageHandlerDescriptorh">trunk/Source/WebCore/page/UserMessageHandlerDescriptor.h</a></li>
<li><a href="#trunkSourceWebCorepageUserMessageHandlersNamespacecpp">trunk/Source/WebCore/page/UserMessageHandlersNamespace.cpp</a></li>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2WebProcessUserContentWebUserContentControllercpp">trunk/Source/WebKit2/WebProcess/UserContent/WebUserContentController.cpp</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKit2CocoaUserContentControllermm">trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/UserContentController.mm</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKit2GtkWebExtensionTestcpp">trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/WebExtensionTest.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Source/WebCore/ChangeLog        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -1,3 +1,34 @@
</span><ins>+2015-05-24  Sam Weinig  &lt;sam@webkit.org&gt;
+
+        Crash when using a removed ScriptMessageHandler
+        &lt;rdar://problem/20888499&gt;
+        https://bugs.webkit.org/show_bug.cgi?id=145359
+
+        Reviewed by Dan Bernstein.
+
+        Added tests:
+            WKUserContentController.ScriptMessageHandlerBasicRemove
+            WKUserContentController.ScriptMessageHandlerCallRemovedHandler
+
+        * page/UserMessageHandler.cpp:
+        (WebCore::UserMessageHandler::~UserMessageHandler):
+        (WebCore::UserMessageHandler::postMessage):
+        (WebCore::UserMessageHandler::name):
+        * page/UserMessageHandler.h:
+        (WebCore::UserMessageHandler::create):
+        * page/UserMessageHandler.idl:
+        * page/UserMessageHandlerDescriptor.cpp:
+        (WebCore::UserMessageHandlerDescriptor::UserMessageHandlerDescriptor):
+        * page/UserMessageHandlerDescriptor.h:
+        (WebCore::UserMessageHandlerDescriptor::client):
+        (WebCore::UserMessageHandlerDescriptor::invalidateClient):
+        Add support for invalidating the descriptor and throw an exception if someone tries
+        to post a message using an invalidated descriptor.
+
+        * page/UserMessageHandlersNamespace.cpp:
+        (WebCore::UserMessageHandlersNamespace::handler):
+        Add logic to remove message handlers if their descriptor has been invalidated.
+
</ins><span class="cx"> 2015-05-23  Dan Bernstein  &lt;mitz@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Remove unused definitions of WEBKIT_VERSION_MIN_REQUIRED
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsgobjectWebKitDOMCustomcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/gobject/WebKitDOMCustom.cpp (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/gobject/WebKitDOMCustom.cpp        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Source/WebCore/bindings/gobject/WebKitDOMCustom.cpp        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -86,7 +86,11 @@
</span><span class="cx">         return FALSE;
</span><span class="cx"> 
</span><span class="cx">     WebCore::JSMainThreadNullState state;
</span><del>-    handler-&gt;postMessage(WebCore::SerializedScriptValue::create(String::fromUTF8(message)));
</del><ins>+    WebCore::ExceptionCode ec = 0;
+    handler-&gt;postMessage(WebCore::SerializedScriptValue::create(String::fromUTF8(message)), ec);
+    if (ec)
+        return FALSE;
+
</ins><span class="cx">     return TRUE;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorepageUserMessageHandlercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/UserMessageHandler.cpp (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/UserMessageHandler.cpp        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Source/WebCore/page/UserMessageHandler.cpp        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -28,6 +28,7 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(USER_MESSAGE_HANDLERS)
</span><span class="cx"> 
</span><ins>+#include &quot;ExceptionCode.h&quot;
</ins><span class="cx"> #include &quot;Frame.h&quot;
</span><span class="cx"> #include &quot;SerializedScriptValue.h&quot;
</span><span class="cx"> 
</span><span class="lines">@@ -43,9 +44,16 @@
</span><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void UserMessageHandler::postMessage(PassRefPtr&lt;SerializedScriptValue&gt; value)
</del><ins>+void UserMessageHandler::postMessage(PassRefPtr&lt;SerializedScriptValue&gt; value, ExceptionCode&amp; ec)
</ins><span class="cx"> {
</span><del>-    m_descriptor-&gt;client().didPostMessage(*this, value.get());
</del><ins>+    // Check to see if the descriptor has been removed. This can happen if the host application has
+    // removed the named message handler at the WebKit2 API level.
+    if (!m_descriptor-&gt;client()) {
+        ec = INVALID_ACCESS_ERR;
+        return;
+    }
+
+    m_descriptor-&gt;client()-&gt;didPostMessage(*this, value.get());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> const AtomicString&amp; UserMessageHandler::name()
</span></span></pre></div>
<a id="trunkSourceWebCorepageUserMessageHandlerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/UserMessageHandler.h (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/UserMessageHandler.h        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Source/WebCore/page/UserMessageHandler.h        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -34,6 +34,8 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><ins>+typedef int ExceptionCode;
+
</ins><span class="cx"> class UserMessageHandler : public RefCounted&lt;UserMessageHandler&gt;, public FrameDestructionObserver {
</span><span class="cx"> public:
</span><span class="cx">     static Ref&lt;UserMessageHandler&gt; create(Frame&amp; frame, UserMessageHandlerDescriptor&amp; descriptor)
</span><span class="lines">@@ -42,7 +44,7 @@
</span><span class="cx">     }
</span><span class="cx">     virtual ~UserMessageHandler();
</span><span class="cx"> 
</span><del>-    void postMessage(PassRefPtr&lt;SerializedScriptValue&gt;);
</del><ins>+    void postMessage(PassRefPtr&lt;SerializedScriptValue&gt;, ExceptionCode&amp;);
</ins><span class="cx"> 
</span><span class="cx">     const AtomicString&amp; name();
</span><span class="cx">     DOMWrapperWorld&amp; world();
</span></span></pre></div>
<a id="trunkSourceWebCorepageUserMessageHandleridl"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/UserMessageHandler.idl (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/UserMessageHandler.idl        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Source/WebCore/page/UserMessageHandler.idl        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -26,5 +26,5 @@
</span><span class="cx"> [
</span><span class="cx">     Conditional=USER_MESSAGE_HANDLERS
</span><span class="cx"> ] interface UserMessageHandler {
</span><del>-    void postMessage(SerializedScriptValue message);
</del><ins>+    [RaisesException] void postMessage(SerializedScriptValue message);
</ins><span class="cx"> };
</span></span></pre></div>
<a id="trunkSourceWebCorepageUserMessageHandlerDescriptorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/UserMessageHandlerDescriptor.cpp (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/UserMessageHandlerDescriptor.cpp        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Source/WebCore/page/UserMessageHandlerDescriptor.cpp        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -35,7 +35,7 @@
</span><span class="cx"> UserMessageHandlerDescriptor::UserMessageHandlerDescriptor(const AtomicString&amp; name, DOMWrapperWorld&amp; world, Client&amp; client)
</span><span class="cx">     : m_name(name)
</span><span class="cx">     , m_world(world)
</span><del>-    , m_client(client)
</del><ins>+    , m_client(&amp;client)
</ins><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorepageUserMessageHandlerDescriptorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/UserMessageHandlerDescriptor.h (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/UserMessageHandlerDescriptor.h        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Source/WebCore/page/UserMessageHandlerDescriptor.h        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -56,15 +56,16 @@
</span><span class="cx"> 
</span><span class="cx">     const AtomicString&amp; name();
</span><span class="cx">     DOMWrapperWorld&amp; world();
</span><del>-    
-    Client&amp; client() const { return m_client; }
</del><span class="cx"> 
</span><ins>+    Client* client() const { return m_client; }
+    void invalidateClient() { m_client = nullptr; }
+
</ins><span class="cx"> private:
</span><span class="cx">     WEBCORE_EXPORT explicit UserMessageHandlerDescriptor(const AtomicString&amp;, DOMWrapperWorld&amp;, Client&amp;);
</span><del>-    
</del><ins>+
</ins><span class="cx">     AtomicString m_name;
</span><span class="cx">     Ref&lt;DOMWrapperWorld&gt; m_world;
</span><del>-    Client&amp; m_client;
</del><ins>+    Client* m_client;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCorepageUserMessageHandlersNamespacecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/UserMessageHandlersNamespace.cpp (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/UserMessageHandlersNamespace.cpp        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Source/WebCore/page/UserMessageHandlersNamespace.cpp        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -46,13 +46,6 @@
</span><span class="cx"> 
</span><span class="cx"> UserMessageHandler* UserMessageHandlersNamespace::handler(const AtomicString&amp; name, DOMWrapperWorld&amp; world)
</span><span class="cx"> {
</span><del>-    // First, check if we have a handler instance already.
-    for (auto&amp; handler : m_messageHandlers) {
-        if (handler-&gt;name() == name &amp;&amp; &amp;handler-&gt;world() == &amp;world)
-            return &amp;handler.get();
-    }
-
-    // Second, attempt to create a handler instance from a descriptor.
</del><span class="cx">     if (!frame())
</span><span class="cx">         return nullptr;
</span><span class="cx"> 
</span><span class="lines">@@ -69,9 +62,18 @@
</span><span class="cx">         return nullptr;
</span><span class="cx"> 
</span><span class="cx">     RefPtr&lt;UserMessageHandlerDescriptor&gt; descriptor = userMessageHandlerDescriptors-&gt;get(std::make_pair(name, &amp;world));
</span><del>-    if (!descriptor)
</del><ins>+    if (!descriptor) {
+        m_messageHandlers.removeFirstMatching([&amp;name, &amp;world](Ref&lt;UserMessageHandler&gt;&amp; handler) {
+            return handler-&gt;name() == name &amp;&amp; &amp;handler-&gt;world() == &amp;world;
+        });
</ins><span class="cx">         return nullptr;
</span><ins>+    }
</ins><span class="cx"> 
</span><ins>+    for (auto&amp; handler : m_messageHandlers) {
+        if (handler-&gt;name() == name &amp;&amp; &amp;handler-&gt;world() == &amp;world)
+            return &amp;handler.get();
+    }
+
</ins><span class="cx">     m_messageHandlers.append(UserMessageHandler::create(*frame(), *descriptor));
</span><span class="cx">     return &amp;m_messageHandlers.last().get();
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Source/WebKit2/ChangeLog        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -1,3 +1,17 @@
</span><ins>+2015-05-24  Sam Weinig  &lt;sam@webkit.org&gt;
+
+        Crash when using a removed ScriptMessageHandler
+        &lt;rdar://problem/20888499&gt;
+        https://bugs.webkit.org/show_bug.cgi?id=145359
+
+        Reviewed by Dan Bernstein.
+
+        * WebProcess/UserContent/WebUserContentController.cpp:
+        (WebKit::WebUserMessageHandlerDescriptorProxy::~WebUserMessageHandlerDescriptorProxy):
+        Invalidate the descriptor when the message handler client (as implemented by WebUserMessageHandlerDescriptorProxy)
+        goes away. This will happen if a script message handler is removed at the API level or the WebUserContentController
+        is destroyed (which will happen if all the pages get destroyed).
+
</ins><span class="cx"> 2015-05-23  Dan Bernstein  &lt;mitz@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         &lt;rdar://problem/21090327&gt; /S/L/PrivateFrameworks/WebKit.framework is missing Headers and PrivateHeaders symlinks
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessUserContentWebUserContentControllercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/UserContent/WebUserContentController.cpp (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/UserContent/WebUserContentController.cpp        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Source/WebKit2/WebProcess/UserContent/WebUserContentController.cpp        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -117,6 +117,7 @@
</span><span class="cx"> 
</span><span class="cx">     virtual ~WebUserMessageHandlerDescriptorProxy()
</span><span class="cx">     {
</span><ins>+        m_descriptor-&gt;invalidateClient();
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // WebCore::UserMessageHandlerDescriptor::Client
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Tools/ChangeLog        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -1,3 +1,14 @@
</span><ins>+2015-05-24  Sam Weinig  &lt;sam@webkit.org&gt;
+
+        Crash when using a removed ScriptMessageHandler
+        &lt;rdar://problem/20888499&gt;
+        https://bugs.webkit.org/show_bug.cgi?id=145359
+
+        Reviewed by Dan Bernstein.
+
+        * TestWebKitAPI/Tests/WebKit2Cocoa/UserContentController.mm:
+        Add tests for removing script message handlers.
+
</ins><span class="cx"> 2015-05-23  Dan Bernstein  &lt;mitz@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Remove unused definitions of WEBKIT_VERSION_MIN_REQUIRED
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKit2CocoaUserContentControllermm"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/UserContentController.mm (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/UserContentController.mm        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/UserContentController.mm        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -62,7 +62,7 @@
</span><span class="cx"> 
</span><span class="cx"> @end
</span><span class="cx"> 
</span><del>-TEST(WKUserContentController, ScriptMessageHandlerSimple)
</del><ins>+TEST(WKUserContentController, ScriptMessageHandlerBasicPost)
</ins><span class="cx"> {
</span><span class="cx">     RetainPtr&lt;ScriptMessageHandler&gt; handler = adoptNS([[ScriptMessageHandler alloc] init]);
</span><span class="cx">     RetainPtr&lt;WKWebViewConfiguration&gt; configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
</span><span class="lines">@@ -87,6 +87,91 @@
</span><span class="cx">     EXPECT_WK_STREQ(@&quot;Hello&quot;, (NSString *)[lastScriptMessage body]);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+TEST(WKUserContentController, ScriptMessageHandlerBasicRemove)
+{
+    RetainPtr&lt;ScriptMessageHandler&gt; handler = adoptNS([[ScriptMessageHandler alloc] init]);
+    RetainPtr&lt;WKWebViewConfiguration&gt; configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    RetainPtr&lt;WKUserContentController&gt; userContentController = [configuration userContentController];
+    [userContentController addScriptMessageHandler:handler.get() name:@&quot;handlerToRemove&quot;];
+    [userContentController addScriptMessageHandler:handler.get() name:@&quot;handlerToPost&quot;];
+
+    RetainPtr&lt;WKWebView&gt; webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+
+    RetainPtr&lt;SimpleNavigationDelegate&gt; delegate = adoptNS([[SimpleNavigationDelegate alloc] init]);
+    [webView setNavigationDelegate:delegate.get()];
+
+    NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@&quot;simple&quot; withExtension:@&quot;html&quot; subdirectory:@&quot;TestWebKitAPI.resources&quot;]];
+
+    [webView loadRequest:request];
+
+    TestWebKitAPI::Util::run(&amp;isDoneWithNavigation);
+
+    // Test that handlerToRemove was succesfully added.
+    [webView evaluateJavaScript:
+        @&quot;if (window.webkit.messageHandlers.handlerToRemove) {&quot;
+         &quot;    window.webkit.messageHandlers.handlerToPost.postMessage('PASS');&quot;
+         &quot;} else {&quot;
+         &quot;    window.webkit.messageHandlers.handlerToPost.postMessage('FAIL');&quot;
+         &quot;}&quot; completionHandler:nil];
+
+    TestWebKitAPI::Util::run(&amp;receivedScriptMessage);
+    receivedScriptMessage = false;
+
+    EXPECT_WK_STREQ(@&quot;PASS&quot;, (NSString *)[lastScriptMessage body]);
+
+    [userContentController removeScriptMessageHandlerForName:@&quot;handlerToRemove&quot;];
+
+    // Test that handlerToRemove has been removed.
+    [webView evaluateJavaScript:
+        @&quot;if (window.webkit.messageHandlers.handlerToRemove) {&quot;
+         &quot;    window.webkit.messageHandlers.handlerToPost.postMessage('FAIL');&quot;
+         &quot;} else {&quot;
+         &quot;    window.webkit.messageHandlers.handlerToPost.postMessage('PASS');&quot;
+         &quot;}&quot; completionHandler:nil];
+
+    TestWebKitAPI::Util::run(&amp;receivedScriptMessage);
+    receivedScriptMessage = false;
+
+    EXPECT_WK_STREQ(@&quot;PASS&quot;, (NSString *)[lastScriptMessage body]);
+}
+
+TEST(WKUserContentController, ScriptMessageHandlerCallRemovedHandler)
+{
+    RetainPtr&lt;ScriptMessageHandler&gt; handler = adoptNS([[ScriptMessageHandler alloc] init]);
+    RetainPtr&lt;WKWebViewConfiguration&gt; configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    RetainPtr&lt;WKUserContentController&gt; userContentController = [configuration userContentController];
+    [userContentController addScriptMessageHandler:handler.get() name:@&quot;handlerToRemove&quot;];
+    [userContentController addScriptMessageHandler:handler.get() name:@&quot;handlerToPost&quot;];
+
+    RetainPtr&lt;WKWebView&gt; webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+
+    RetainPtr&lt;SimpleNavigationDelegate&gt; delegate = adoptNS([[SimpleNavigationDelegate alloc] init]);
+    [webView setNavigationDelegate:delegate.get()];
+
+    NSURLRequest *request = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@&quot;simple&quot; withExtension:@&quot;html&quot; subdirectory:@&quot;TestWebKitAPI.resources&quot;]];
+
+    [webView loadRequest:request];
+
+    TestWebKitAPI::Util::run(&amp;isDoneWithNavigation);
+
+    [webView evaluateJavaScript:@&quot;var handlerToRemove = window.webkit.messageHandlers.handlerToRemove;&quot; completionHandler:nil];
+
+    [userContentController removeScriptMessageHandlerForName:@&quot;handlerToRemove&quot;];
+
+    // Test that we throw an exception if you try to use a message handler that has been removed.
+    [webView evaluateJavaScript:
+        @&quot;try {&quot;
+         &quot;    handlerToRemove.postMessage('FAIL');&quot;
+         &quot;} catch (e) {&quot;
+         &quot;    window.webkit.messageHandlers.handlerToPost.postMessage('PASS');&quot;
+         &quot;}&quot; completionHandler:nil];
+
+    TestWebKitAPI::Util::run(&amp;receivedScriptMessage);
+    receivedScriptMessage = false;
+
+    EXPECT_WK_STREQ(@&quot;PASS&quot;, (NSString *)[lastScriptMessage body]);
+}
+
</ins><span class="cx"> #if !PLATFORM(IOS) // FIXME: hangs in the iOS simulator
</span><span class="cx"> TEST(WKUserContentController, ScriptMessageHandlerWithNavigation)
</span><span class="cx"> {
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKit2GtkWebExtensionTestcpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/WebExtensionTest.cpp (184845 => 184846)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/WebExtensionTest.cpp        2015-05-24 19:30:49 UTC (rev 184845)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKit2Gtk/WebExtensionTest.cpp        2015-05-24 23:22:06 UTC (rev 184846)
</span><span class="lines">@@ -104,7 +104,7 @@
</span><span class="cx">     if (WebKitDOMWebKitNamespace* webkit = webkit_dom_dom_window_get_webkit_namespace(window.get())) {
</span><span class="cx">         WebKitDOMUserMessageHandlersNamespace* messageHandlers = webkit_dom_webkit_namespace_get_message_handlers(webkit);
</span><span class="cx">         if (WebKitDOMUserMessageHandler* handler = webkit_dom_user_message_handlers_namespace_get_handler(messageHandlers, &quot;dom&quot;))
</span><del>-            webkit_dom_user_message_handler_post_message(handler, &quot;DocumentLoaded&quot;);
</del><ins>+            webkit_dom_user_message_handler_post_message(handler, &quot;DocumentLoaded&quot;, nullptr);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     webkit_dom_dom_window_webkit_message_handlers_post_message(window.get(), &quot;dom-convenience&quot;, &quot;DocumentLoaded&quot;);
</span></span></pre>
</div>
</div>

</body>
</html>