Title: [244802] trunk
Revision
244802
Author
[email protected]
Date
2019-04-30 14:11:07 -0700 (Tue, 30 Apr 2019)

Log Message

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:]):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (244801 => 244802)


--- trunk/Source/WebKit/ChangeLog	2019-04-30 20:21:11 UTC (rev 244801)
+++ trunk/Source/WebKit/ChangeLog	2019-04-30 21:11:07 UTC (rev 244802)
@@ -1,5 +1,31 @@
 2019-04-30  Chris Dumez  <[email protected]>
 
+        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):
+
+2019-04-30  Chris Dumez  <[email protected]>
+
         Only use a related page's process if that page has not been closed yet
         https://bugs.webkit.org/show_bug.cgi?id=197393
         <rdar://problem/50302423>

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (244801 => 244802)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2019-04-30 20:21:11 UTC (rev 244801)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp	2019-04-30 21:11:07 UTC (rev 244802)
@@ -6741,7 +6741,7 @@
             automationSession->terminate();
     }
 
-    stopAllURLSchemeTasks();
+    stopAllURLSchemeTasks(m_process.ptr());
 }
 
 void WebPageProxy::provisionalProcessDidTerminate()
@@ -6801,7 +6801,7 @@
     m_recentCrashCount = 0;
 }
 
-void WebPageProxy::stopAllURLSchemeTasks()
+void WebPageProxy::stopAllURLSchemeTasks(WebProcessProxy* process)
 {
     HashSet<WebURLSchemeHandler*> handlers;
     for (auto& handler : m_urlSchemeHandlersByScheme.values())
@@ -6808,7 +6808,7 @@
         handlers.add(handler.ptr());
 
     for (auto* handler : handlers)
-        handler->stopAllTasksForPage(*this);
+        handler->stopAllTasksForPage(*this, process);
 }
 
 #if PLATFORM(IOS_FAMILY)

Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (244801 => 244802)


--- trunk/Source/WebKit/UIProcess/WebPageProxy.h	2019-04-30 20:21:11 UTC (rev 244801)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h	2019-04-30 21:11:07 UTC (rev 244802)
@@ -2001,7 +2001,7 @@
 
     void viewIsBecomingVisible();
 
-    void stopAllURLSchemeTasks();
+    void stopAllURLSchemeTasks(WebProcessProxy* = nullptr);
 
     void clearInspectorTargets();
     void createInspectorTargets();

Modified: trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.cpp (244801 => 244802)


--- trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.cpp	2019-04-30 20:21:11 UTC (rev 244801)
+++ trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.cpp	2019-04-30 21:11:07 UTC (rev 244802)
@@ -60,17 +60,31 @@
     platformStartTask(page, result.iterator->value);
 }
 
-void WebURLSchemeHandler::stopAllTasksForPage(WebPageProxy& page)
+WebProcessProxy* WebURLSchemeHandler::processForTaskIdentifier(uint64_t taskIdentifier) const
 {
+    auto iterator = m_tasks.find(taskIdentifier);
+    if (iterator == m_tasks.end())
+        return nullptr;
+    return iterator->value->process();
+}
+
+void WebURLSchemeHandler::stopAllTasksForPage(WebPageProxy& page, WebProcessProxy* process)
+{
     auto iterator = m_tasksByPageIdentifier.find(page.pageID());
     if (iterator == m_tasksByPageIdentifier.end())
         return;
 
     auto& tasksByPage = iterator->value;
-    while (!tasksByPage.isEmpty())
-        stopTask(page, *tasksByPage.begin());
+    Vector<uint64_t> taskIdentifiersToStop;
+    taskIdentifiersToStop.reserveInitialCapacity(tasksByPage.size());
+    for (auto taskIdentifier : tasksByPage) {
+        if (!process || processForTaskIdentifier(taskIdentifier) == process)
+            taskIdentifiersToStop.uncheckedAppend(taskIdentifier);
+    }
 
-    ASSERT(m_tasksByPageIdentifier.find(page.pageID()) == m_tasksByPageIdentifier.end());
+    for (auto& taskIdentifier : taskIdentifiersToStop)
+        stopTask(page, taskIdentifier);
+
 }
 
 void WebURLSchemeHandler::stopTask(WebPageProxy& page, uint64_t taskIdentifier)

Modified: trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.h (244801 => 244802)


--- trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.h	2019-04-30 20:21:11 UTC (rev 244801)
+++ trunk/Source/WebKit/UIProcess/WebURLSchemeHandler.h	2019-04-30 21:11:07 UTC (rev 244802)
@@ -55,7 +55,7 @@
 
     void startTask(WebPageProxy&, WebProcessProxy&, uint64_t taskIdentifier, WebCore::ResourceRequest&&, SyncLoadCompletionHandler&&);
     void stopTask(WebPageProxy&, uint64_t taskIdentifier);
-    void stopAllTasksForPage(WebPageProxy&);
+    void stopAllTasksForPage(WebPageProxy&, WebProcessProxy*);
     void taskCompleted(WebURLSchemeTask&);
 
 protected:
@@ -67,6 +67,7 @@
     virtual void platformTaskCompleted(WebURLSchemeTask&) = 0;
 
     void removeTaskFromPageMap(uint64_t pageID, uint64_t taskID);
+    WebProcessProxy* processForTaskIdentifier(uint64_t) const;
 
     uint64_t m_identifier;
 

Modified: trunk/Source/WebKit/UIProcess/WebURLSchemeTask.h (244801 => 244802)


--- trunk/Source/WebKit/UIProcess/WebURLSchemeTask.h	2019-04-30 20:21:11 UTC (rev 244801)
+++ trunk/Source/WebKit/UIProcess/WebURLSchemeTask.h	2019-04-30 21:11:07 UTC (rev 244802)
@@ -58,6 +58,7 @@
 
     uint64_t identifier() const { return m_identifier; }
     uint64_t pageID() const { return m_pageIdentifier; }
+    WebProcessProxy* process() const { return m_process.get(); }
 
     const WebCore::ResourceRequest& request() const { return m_request; }
 

Modified: trunk/Tools/ChangeLog (244801 => 244802)


--- trunk/Tools/ChangeLog	2019-04-30 20:21:11 UTC (rev 244801)
+++ trunk/Tools/ChangeLog	2019-04-30 21:11:07 UTC (rev 244802)
@@ -1,5 +1,20 @@
 2019-04-30  Chris Dumez  <[email protected]>
 
+        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  <[email protected]>
+
         Only use a related page's process if that page has not been closed yet
         https://bugs.webkit.org/show_bug.cgi?id=197393
         <rdar://problem/50302423>

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm (244801 => 244802)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm	2019-04-30 20:21:11 UTC (rev 244801)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm	2019-04-30 21:11:07 UTC (rev 244802)
@@ -48,6 +48,7 @@
 #import <WebKit/_WKProcessPoolConfiguration.h>
 #import <WebKit/_WKWebsiteDataStoreConfiguration.h>
 #import <WebKit/_WKWebsitePolicies.h>
+#import <wtf/BlockPtr.h>
 #import <wtf/Deque.h>
 #import <wtf/HashMap.h>
 #import <wtf/HashSet.h>
@@ -238,6 +239,8 @@
     const char* _bytes;
     HashMap<String, String> _redirects;
     HashMap<String, RetainPtr<NSData>> _dataMappings;
+    HashSet<id <WKURLSchemeTask>> _runningTasks;
+    bool _shouldRespondAsynchronously;
 }
 - (instancetype)initWithBytes:(const char*)bytes;
 - (void)addRedirectFromURLString:(NSString *)sourceURLString toURLString:(NSString *)destinationURLString;
@@ -263,8 +266,29 @@
     _dataMappings.set(urlString, [NSData dataWithBytesNoCopy:(void*)data length:strlen(data) freeWhenDone:NO]);
 }
 
+- (void)setShouldRespondAsynchronously:(BOOL)value
+{
+    _shouldRespondAsynchronously = value;
+}
+
 - (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)task
 {
+    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());
+    };
+
     NSURL *finalURL = task.request.URL;
     auto target = _redirects.get(task.request.URL.absoluteString);
     if (!target.isEmpty()) {
@@ -276,27 +300,30 @@
         [(id<WKURLSchemeTaskPrivate>)task _didPerformRedirection:redirectResponse.get() newRequest:request.get()];
     }
 
-    if ([(id<WKURLSchemeTaskPrivate>)task _requestOnlyIfCached]) {
-        [task didFailWithError:[NSError errorWithDomain:@"TestWebKitAPI" code:1 userInfo:nil]];
-        return;
-    }
+    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);
 
-    RetainPtr<NSURLResponse> response = adoptNS([[NSURLResponse alloc] initWithURL:finalURL MIMEType:@"text/html" expectedContentLength:1 textEncodingName:nil]);
-    [task didReceiveResponse:response.get()];
+    doAsynchronouslyIfNecessary([self, finalURL = retainPtr(finalURL)](id <WKURLSchemeTask> task) {
+        if (auto data = "" absoluteString]))
+            [task didReceiveData:data.get()];
+        else if (_bytes) {
+            RetainPtr<NSData> data = "" alloc] initWithBytesNoCopy:(void *)_bytes length:strlen(_bytes) freeWhenDone:NO]);
+            [task didReceiveData:data.get()];
+        } else
+            [task didReceiveData:[@"Hello" dataUsingEncoding:NSUTF8StringEncoding]];
+    }, 0.2);
 
-    if (auto data = "" absoluteString]))
-        [task didReceiveData:data.get()];
-    else if (_bytes) {
-        RetainPtr<NSData> data = "" alloc] initWithBytesNoCopy:(void *)_bytes length:strlen(_bytes) freeWhenDone:NO]);
-        [task didReceiveData:data.get()];
-    } else
-        [task didReceiveData:[@"Hello" dataUsingEncoding:NSUTF8StringEncoding]];
-
-    [task didFinish];
+    doAsynchronouslyIfNecessary([self](id <WKURLSchemeTask> task) {
+        [task didFinish];
+        _runningTasks.remove(task);
+    }, 0.3);
 }
 
 - (void)webView:(WKWebView *)webView stopURLSchemeTask:(id <WKURLSchemeTask>)task
 {
+    _runningTasks.remove(task);
 }
 
 @end
@@ -471,7 +498,8 @@
     return processPoolConfiguration;
 }
 
-TEST(ProcessSwap, Basic)
+enum class SchemeHandlerShouldBeAsync { No, Yes };
+static void runBasicTest(SchemeHandlerShouldBeAsync schemeHandlerShouldBeAsync)
 {
     auto processPoolConfiguration = psonProcessPoolConfiguration();
     auto processPool = adoptNS([[WKProcessPool alloc] _initWithConfiguration:processPoolConfiguration.get()]);
@@ -479,6 +507,7 @@
     auto webViewConfiguration = adoptNS([[WKWebViewConfiguration alloc] init]);
     [webViewConfiguration setProcessPool:processPool.get()];
     auto handler = adoptNS([[PSONScheme alloc] init]);
+    [handler setShouldRespondAsynchronously:(schemeHandlerShouldBeAsync == SchemeHandlerShouldBeAsync::Yes)];
     [webViewConfiguration setURLSchemeHandler:handler.get() forURLScheme:@"PSON"];
 
     auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:webViewConfiguration.get()]);
@@ -516,6 +545,16 @@
     EXPECT_EQ(numberOfDecidePolicyCalls, 3);
 }
 
+TEST(ProcessSwap, Basic)
+{
+    runBasicTest(SchemeHandlerShouldBeAsync::No);
+}
+
+TEST(ProcessSwap, BasicWithAsyncSchemeHandler)
+{
+    runBasicTest(SchemeHandlerShouldBeAsync::Yes);
+}
+
 TEST(ProcessSwap, LoadAfterPolicyDecision)
 {
     auto processPoolConfiguration = psonProcessPoolConfiguration();
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to