<!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>[244819] 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/244819">244819</a></dd>
<dt>Author</dt> <dd>cdumez@apple.com</dd>
<dt>Date</dt> <dd>2019-04-30 19:45:10 -0700 (Tue, 30 Apr 2019)</dd>
</dl>

<h3>Log Message</h3>
<pre>Regression(PSON) URL scheme handlers can no longer respond asynchronously
https://bugs.webkit.org/show_bug.cgi?id=197426
<rdar://problem/50256169>

Reviewed by Brady Eidson.

Source/WebKit:

The issue was that when committing the provisional process, we would call WebPageProxy::processDidTerminate()
which would call WebPageProxy::stopAllURLSchemeTasks(). This would terminate all URL scheme tasks associated
with the page, including the one associated with the provisisional page / process.

To address the issue, pass m_process to stopAllURLSchemeTasks() in processDidTerminate() and only stop the
tasks associated with the m_process (which is the process we're about to swap away from).

* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::processDidTerminate):
(WebKit::WebPageProxy::stopAllURLSchemeTasks):
* UIProcess/WebPageProxy.h:
* UIProcess/WebURLSchemeHandler.cpp:
(WebKit::WebURLSchemeHandler::processForTaskIdentifier):
(WebKit::WebURLSchemeHandler::stopAllTasksForPage):
* UIProcess/WebURLSchemeHandler.h:
* UIProcess/WebURLSchemeTask.h:
(WebKit::WebURLSchemeTask::process const):

Tools:

Add API test coverage.

* TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm:
(-[PSONScheme setShouldRespondAsynchronously:]):
(-[PSONScheme webView:startURLSchemeTask:]):
(-[PSONScheme webView:stopURLSchemeTask:]):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKitChangeLog">trunk/Source/WebKit/ChangeLog</a></li>
<li><a href="#trunkSourceWebKitUIProcessWebPageProxycpp">trunk/Source/WebKit/UIProcess/WebPageProxy.cpp</a></li>
<li><a href="#trunkSourceWebKitUIProcessWebPageProxyh">trunk/Source/WebKit/UIProcess/WebPageProxy.h</a></li>
<li><a href="#trunkSourceWebKitUIProcessWebURLSchemeHandlercpp">trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.cpp</a></li>
<li><a href="#trunkSourceWebKitUIProcessWebURLSchemeHandlerh">trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.h</a></li>
<li><a href="#trunkSourceWebKitUIProcessWebURLSchemeTaskh">trunk/Source/WebKit/UIProcess/WebURLSchemeTask.h</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKitCocoaProcessSwapOnNavigationmm">trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKitChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/ChangeLog (244818 => 244819)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/ChangeLog    2019-05-01 02:28:07 UTC (rev 244818)
+++ trunk/Source/WebKit/ChangeLog       2019-05-01 02:45:10 UTC (rev 244819)
</span><span class="lines">@@ -1,3 +1,29 @@
</span><ins>+2019-04-30  Chris Dumez  <cdumez@apple.com>
+
+        Regression(PSON) URL scheme handlers can no longer respond asynchronously
+        https://bugs.webkit.org/show_bug.cgi?id=197426
+        <rdar://problem/50256169>
+
+        Reviewed by Brady Eidson.
+
+        The issue was that when committing the provisional process, we would call WebPageProxy::processDidTerminate()
+        which would call WebPageProxy::stopAllURLSchemeTasks(). This would terminate all URL scheme tasks associated
+        with the page, including the one associated with the provisisional page / process.
+
+        To address the issue, pass m_process to stopAllURLSchemeTasks() in processDidTerminate() and only stop the
+        tasks associated with the m_process (which is the process we're about to swap away from).
+
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::processDidTerminate):
+        (WebKit::WebPageProxy::stopAllURLSchemeTasks):
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/WebURLSchemeHandler.cpp:
+        (WebKit::WebURLSchemeHandler::processForTaskIdentifier):
+        (WebKit::WebURLSchemeHandler::stopAllTasksForPage):
+        * UIProcess/WebURLSchemeHandler.h:
+        * UIProcess/WebURLSchemeTask.h:
+        (WebKit::WebURLSchemeTask::process const):
+
</ins><span class="cx"> 2019-04-30  John Wilander  <wilander@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Add logging of Ad Click Attribution errors and events to a dedicated channel
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessWebPageProxycpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (244818 => 244819)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp   2019-05-01 02:28:07 UTC (rev 244818)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp      2019-05-01 02:45:10 UTC (rev 244819)
</span><span class="lines">@@ -6725,6 +6725,7 @@
</span><span class="cx">     PageLoadState::Transaction transaction = m_pageLoadState.transaction();
</span><span class="cx"> 
</span><span class="cx">     resetStateAfterProcessExited(reason);
</span><ins>+    stopAllURLSchemeTasks(m_process.ptr());
</ins><span class="cx"> 
</span><span class="cx">     // For bringup of process swapping, NavigationSwap termination will not go out to clients.
</span><span class="cx">     // If it does *during* process swapping, and the client triggers a reload, that causes bizarre WebKit re-entry.
</span><span class="lines">@@ -6740,8 +6741,6 @@
</span><span class="cx">         if (auto* automationSession = process().processPool().automationSession())
</span><span class="cx">             automationSession->terminate();
</span><span class="cx">     }
</span><del>-
-    stopAllURLSchemeTasks();
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebPageProxy::provisionalProcessDidTerminate()
</span><span class="lines">@@ -6801,7 +6800,7 @@
</span><span class="cx">     m_recentCrashCount = 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebPageProxy::stopAllURLSchemeTasks()
</del><ins>+void WebPageProxy::stopAllURLSchemeTasks(WebProcessProxy* process)
</ins><span class="cx"> {
</span><span class="cx">     HashSet<WebURLSchemeHandler*> handlers;
</span><span class="cx">     for (auto& handler : m_urlSchemeHandlersByScheme.values())
</span><span class="lines">@@ -6808,7 +6807,7 @@
</span><span class="cx">         handlers.add(handler.ptr());
</span><span class="cx"> 
</span><span class="cx">     for (auto* handler : handlers)
</span><del>-        handler->stopAllTasksForPage(*this);
</del><ins>+        handler->stopAllTasksForPage(*this, process);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(IOS_FAMILY)
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessWebPageProxyh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (244818 => 244819)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/WebPageProxy.h     2019-05-01 02:28:07 UTC (rev 244818)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h        2019-05-01 02:45:10 UTC (rev 244819)
</span><span class="lines">@@ -2001,7 +2001,7 @@
</span><span class="cx"> 
</span><span class="cx">     void viewIsBecomingVisible();
</span><span class="cx"> 
</span><del>-    void stopAllURLSchemeTasks();
</del><ins>+    void stopAllURLSchemeTasks(WebProcessProxy* = nullptr);
</ins><span class="cx"> 
</span><span class="cx">     void clearInspectorTargets();
</span><span class="cx">     void createInspectorTargets();
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessWebURLSchemeHandlercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.cpp (244818 => 244819)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.cpp    2019-05-01 02:28:07 UTC (rev 244818)
+++ trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.cpp       2019-05-01 02:45:10 UTC (rev 244819)
</span><span class="lines">@@ -60,17 +60,31 @@
</span><span class="cx">     platformStartTask(page, result.iterator->value);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void WebURLSchemeHandler::stopAllTasksForPage(WebPageProxy& page)
</del><ins>+WebProcessProxy* WebURLSchemeHandler::processForTaskIdentifier(uint64_t taskIdentifier) const
</ins><span class="cx"> {
</span><ins>+    auto iterator = m_tasks.find(taskIdentifier);
+    if (iterator == m_tasks.end())
+        return nullptr;
+    return iterator->value->process();
+}
+
+void WebURLSchemeHandler::stopAllTasksForPage(WebPageProxy& page, WebProcessProxy* process)
+{
</ins><span class="cx">     auto iterator = m_tasksByPageIdentifier.find(page.pageID());
</span><span class="cx">     if (iterator == m_tasksByPageIdentifier.end())
</span><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     auto& tasksByPage = iterator->value;
</span><del>-    while (!tasksByPage.isEmpty())
-        stopTask(page, *tasksByPage.begin());
</del><ins>+    Vector<uint64_t> taskIdentifiersToStop;
+    taskIdentifiersToStop.reserveInitialCapacity(tasksByPage.size());
+    for (auto taskIdentifier : tasksByPage) {
+        if (!process || processForTaskIdentifier(taskIdentifier) == process)
+            taskIdentifiersToStop.uncheckedAppend(taskIdentifier);
+    }
</ins><span class="cx"> 
</span><del>-    ASSERT(m_tasksByPageIdentifier.find(page.pageID()) == m_tasksByPageIdentifier.end());
</del><ins>+    for (auto& taskIdentifier : taskIdentifiersToStop)
+        stopTask(page, taskIdentifier);
+
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebURLSchemeHandler::stopTask(WebPageProxy& page, uint64_t taskIdentifier)
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessWebURLSchemeHandlerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.h (244818 => 244819)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.h      2019-05-01 02:28:07 UTC (rev 244818)
+++ trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.h 2019-05-01 02:45:10 UTC (rev 244819)
</span><span class="lines">@@ -55,7 +55,7 @@
</span><span class="cx"> 
</span><span class="cx">     void startTask(WebPageProxy&, WebProcessProxy&, uint64_t taskIdentifier, WebCore::ResourceRequest&&, SyncLoadCompletionHandler&&);
</span><span class="cx">     void stopTask(WebPageProxy&, uint64_t taskIdentifier);
</span><del>-    void stopAllTasksForPage(WebPageProxy&);
</del><ins>+    void stopAllTasksForPage(WebPageProxy&, WebProcessProxy*);
</ins><span class="cx">     void taskCompleted(WebURLSchemeTask&);
</span><span class="cx"> 
</span><span class="cx"> protected:
</span><span class="lines">@@ -67,6 +67,7 @@
</span><span class="cx">     virtual void platformTaskCompleted(WebURLSchemeTask&) = 0;
</span><span class="cx"> 
</span><span class="cx">     void removeTaskFromPageMap(uint64_t pageID, uint64_t taskID);
</span><ins>+    WebProcessProxy* processForTaskIdentifier(uint64_t) const;
</ins><span class="cx"> 
</span><span class="cx">     uint64_t m_identifier;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessWebURLSchemeTaskh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/WebURLSchemeTask.h (244818 => 244819)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/WebURLSchemeTask.h 2019-05-01 02:28:07 UTC (rev 244818)
+++ trunk/Source/WebKit/UIProcess/WebURLSchemeTask.h    2019-05-01 02:45:10 UTC (rev 244819)
</span><span class="lines">@@ -58,6 +58,7 @@
</span><span class="cx"> 
</span><span class="cx">     uint64_t identifier() const { return m_identifier; }
</span><span class="cx">     uint64_t pageID() const { return m_pageIdentifier; }
</span><ins>+    WebProcessProxy* process() const { return m_process.get(); }
</ins><span class="cx"> 
</span><span class="cx">     const WebCore::ResourceRequest& request() const { return m_request; }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (244818 => 244819)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2019-05-01 02:28:07 UTC (rev 244818)
+++ trunk/Tools/ChangeLog       2019-05-01 02:45:10 UTC (rev 244819)
</span><span class="lines">@@ -1,5 +1,20 @@
</span><span class="cx"> 2019-04-30  Chris Dumez  <cdumez@apple.com>
</span><span class="cx"> 
</span><ins>+        Regression(PSON) URL scheme handlers can no longer respond asynchronously
+        https://bugs.webkit.org/show_bug.cgi?id=197426
+        <rdar://problem/50256169>
+
+        Reviewed by Brady Eidson.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm:
+        (-[PSONScheme setShouldRespondAsynchronously:]):
+        (-[PSONScheme webView:startURLSchemeTask:]):
+        (-[PSONScheme webView:stopURLSchemeTask:]):
+
+2019-04-30  Chris Dumez  <cdumez@apple.com>
+
</ins><span class="cx">         Unreviewed, rolling out r244802.
</span><span class="cx"> 
</span><span class="cx">         Caused an API test failure
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKitCocoaProcessSwapOnNavigationmm"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm (244818 => 244819)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm   2019-05-01 02:28:07 UTC (rev 244818)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm      2019-05-01 02:45:10 UTC (rev 244819)
</span><span class="lines">@@ -48,6 +48,7 @@
</span><span class="cx"> #import <WebKit/_WKProcessPoolConfiguration.h>
</span><span class="cx"> #import <WebKit/_WKWebsiteDataStoreConfiguration.h>
</span><span class="cx"> #import <WebKit/_WKWebsitePolicies.h>
</span><ins>+#import <wtf/BlockPtr.h>
</ins><span class="cx"> #import <wtf/Deque.h>
</span><span class="cx"> #import <wtf/HashMap.h>
</span><span class="cx"> #import <wtf/HashSet.h>
</span><span class="lines">@@ -238,6 +239,8 @@
</span><span class="cx">     const char* _bytes;
</span><span class="cx">     HashMap<String, String> _redirects;
</span><span class="cx">     HashMap<String, RetainPtr<NSData>> _dataMappings;
</span><ins>+    HashSet<id <WKURLSchemeTask>> _runningTasks;
+    bool _shouldRespondAsynchronously;
</ins><span class="cx"> }
</span><span class="cx"> - (instancetype)initWithBytes:(const char*)bytes;
</span><span class="cx"> - (void)addRedirectFromURLString:(NSString *)sourceURLString toURLString:(NSString *)destinationURLString;
</span><span class="lines">@@ -263,8 +266,29 @@
</span><span class="cx">     _dataMappings.set(urlString, [NSData dataWithBytesNoCopy:(void*)data length:strlen(data) freeWhenDone:NO]);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+- (void)setShouldRespondAsynchronously:(BOOL)value
+{
+    _shouldRespondAsynchronously = value;
+}
+
</ins><span class="cx"> - (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)task
</span><span class="cx"> {
</span><ins>+    if ([(id<WKURLSchemeTaskPrivate>)task _requestOnlyIfCached]) {
+        [task didFailWithError:[NSError errorWithDomain:@"TestWebKitAPI" code:1 userInfo:nil]];
+        return;
+    }
+
+    _runningTasks.add(task);
+
+    auto doAsynchronouslyIfNecessary = [self, strongSelf = retainPtr(self), task = retainPtr(task)](Function<void(id <WKURLSchemeTask>)>&& f, double delay) {
+        if (!_shouldRespondAsynchronously)
+            return f(task.get());
+        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC), dispatch_get_main_queue(), makeBlockPtr([self, strongSelf, task, f = WTFMove(f)] {
+            if (_runningTasks.contains(task.get()))
+                f(task.get());
+        }).get());
+    };
+
</ins><span class="cx">     NSURL *finalURL = task.request.URL;
</span><span class="cx">     auto target = _redirects.get(task.request.URL.absoluteString);
</span><span class="cx">     if (!target.isEmpty()) {
</span><span class="lines">@@ -276,27 +300,30 @@
</span><span class="cx">         [(id<WKURLSchemeTaskPrivate>)task _didPerformRedirection:redirectResponse.get() newRequest:request.get()];
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if ([(id<WKURLSchemeTaskPrivate>)task _requestOnlyIfCached]) {
-        [task didFailWithError:[NSError errorWithDomain:@"TestWebKitAPI" code:1 userInfo:nil]];
-        return;
-    }
</del><ins>+    doAsynchronouslyIfNecessary([finalURL = retainPtr(finalURL)](id <WKURLSchemeTask> task) {
+        RetainPtr<NSURLResponse> response = adoptNS([[NSURLResponse alloc] initWithURL:finalURL.get() MIMEType:@"text/html" expectedContentLength:1 textEncodingName:nil]);
+        [task didReceiveResponse:response.get()];
+    }, 0.1);
</ins><span class="cx"> 
</span><del>-    RetainPtr<NSURLResponse> response = adoptNS([[NSURLResponse alloc] initWithURL:finalURL MIMEType:@"text/html" expectedContentLength:1 textEncodingName:nil]);
-    [task didReceiveResponse:response.get()];
</del><ins>+    doAsynchronouslyIfNecessary([self, finalURL = retainPtr(finalURL)](id <WKURLSchemeTask> task) {
+        if (auto data = _dataMappings.get([finalURL absoluteString]))
+            [task didReceiveData:data.get()];
+        else if (_bytes) {
+            RetainPtr<NSData> data = adoptNS([[NSData alloc] initWithBytesNoCopy:(void *)_bytes length:strlen(_bytes) freeWhenDone:NO]);
+            [task didReceiveData:data.get()];
+        } else
+            [task didReceiveData:[@"Hello" dataUsingEncoding:NSUTF8StringEncoding]];
+    }, 0.2);
</ins><span class="cx"> 
</span><del>-    if (auto data = _dataMappings.get([finalURL absoluteString]))
-        [task didReceiveData:data.get()];
-    else if (_bytes) {
-        RetainPtr<NSData> data = adoptNS([[NSData alloc] initWithBytesNoCopy:(void *)_bytes length:strlen(_bytes) freeWhenDone:NO]);
-        [task didReceiveData:data.get()];
-    } else
-        [task didReceiveData:[@"Hello" dataUsingEncoding:NSUTF8StringEncoding]];
-
-    [task didFinish];
</del><ins>+    doAsynchronouslyIfNecessary([self](id <WKURLSchemeTask> task) {
+        [task didFinish];
+        _runningTasks.remove(task);
+    }, 0.3);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> - (void)webView:(WKWebView *)webView stopURLSchemeTask:(id <WKURLSchemeTask>)task
</span><span class="cx"> {
</span><ins>+    _runningTasks.remove(task);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> @end
</span><span class="lines">@@ -471,7 +498,8 @@
</span><span class="cx">     return processPoolConfiguration;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-TEST(ProcessSwap, Basic)
</del><ins>+enum class SchemeHandlerShouldBeAsync { No, Yes };
+static void runBasicTest(SchemeHandlerShouldBeAsync schemeHandlerShouldBeAsync)
</ins><span class="cx"> {
</span><span class="cx">     auto processPoolConfiguration = psonProcessPoolConfiguration();
</span><span class="cx">     auto processPool = adoptNS([[WKProcessPool alloc] _initWithConfiguration:processPoolConfiguration.get()]);
</span><span class="lines">@@ -479,6 +507,7 @@
</span><span class="cx">     auto webViewConfiguration = adoptNS([[WKWebViewConfiguration alloc] init]);
</span><span class="cx">     [webViewConfiguration setProcessPool:processPool.get()];
</span><span class="cx">     auto handler = adoptNS([[PSONScheme alloc] init]);
</span><ins>+    [handler setShouldRespondAsynchronously:(schemeHandlerShouldBeAsync == SchemeHandlerShouldBeAsync::Yes)];
</ins><span class="cx">     [webViewConfiguration setURLSchemeHandler:handler.get() forURLScheme:@"PSON"];
</span><span class="cx"> 
</span><span class="cx">     auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:webViewConfiguration.get()]);
</span><span class="lines">@@ -516,6 +545,16 @@
</span><span class="cx">     EXPECT_EQ(numberOfDecidePolicyCalls, 3);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+TEST(ProcessSwap, Basic)
+{
+    runBasicTest(SchemeHandlerShouldBeAsync::No);
+}
+
+TEST(ProcessSwap, BasicWithAsyncSchemeHandler)
+{
+    runBasicTest(SchemeHandlerShouldBeAsync::Yes);
+}
+
</ins><span class="cx"> TEST(ProcessSwap, LoadAfterPolicyDecision)
</span><span class="cx"> {
</span><span class="cx">     auto processPoolConfiguration = psonProcessPoolConfiguration();
</span></span></pre>
</div>
</div>

</body>
</html>