Delayed WebProcessLaunch may break the _relatedWebView SPI
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 22 Apr 2019 18:03:47 +0000 (18:03 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 22 Apr 2019 18:03:47 +0000 (18:03 +0000)
https://bugs.webkit.org/show_bug.cgi?id=197160

Reviewed by Alex Christensen.

Source/WebKit:

Delayed WebProcessLaunch may break the _relatedWebView SPI. The breakage would happen if the client
would relate a WebView to another which has not launched its initial process yet.

To address the issue, when we need a running process for a WebView which has a related view, we need
to make sure the related view has a running process and use that process. Previously, we would share
the "dummy" process instead.

* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::launchProcess):
(WebKit::WebPageProxy::ensureRunningProcess):
* UIProcess/WebPageProxy.h:
* UIProcess/WebProcessPool.cpp:
(WebKit::WebProcessPool::createWebPage):

Tools:

Add API test coverage.

* TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@244502 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/ChangeLog
Source/WebKit/UIProcess/WebPageProxy.cpp
Source/WebKit/UIProcess/WebPageProxy.h
Source/WebKit/UIProcess/WebProcessPool.cpp
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm

index 295b930..6328f1e 100644 (file)
@@ -1,3 +1,24 @@
+2019-04-22  Chris Dumez  <cdumez@apple.com>
+
+        Delayed WebProcessLaunch may break the _relatedWebView SPI
+        https://bugs.webkit.org/show_bug.cgi?id=197160
+
+        Reviewed by Alex Christensen.
+
+        Delayed WebProcessLaunch may break the _relatedWebView SPI. The breakage would happen if the client
+        would relate a WebView to another which has not launched its initial process yet.
+
+        To address the issue, when we need a running process for a WebView which has a related view, we need
+        to make sure the related view has a running process and use that process. Previously, we would share
+        the "dummy" process instead.
+
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::launchProcess):
+        (WebKit::WebPageProxy::ensureRunningProcess):
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/WebProcessPool.cpp:
+        (WebKit::WebProcessPool::createWebPage):
+
 2019-04-22  Ludovico de Nittis  <ludovico.denittis@collabora.com>
 
         [GTK] fix gtk_style_context_set_background deprecation
index b67dc22..bd65434 100644 (file)
@@ -727,7 +727,11 @@ void WebPageProxy::launchProcess(const RegistrableDomain& registrableDomain)
     m_process->removeMessageReceiver(Messages::WebPageProxy::messageReceiverName(), m_pageID);
 
     auto& processPool = m_process->processPool();
-    m_process = processPool.processForRegistrableDomain(m_websiteDataStore.get(), this, registrableDomain);
+
+    if (auto* relatedPage = m_configuration->relatedPage())
+        m_process = relatedPage->ensureRunningProcess();
+    else
+        m_process = processPool.processForRegistrableDomain(m_websiteDataStore.get(), this, registrableDomain);
     m_hasRunningProcess = true;
 
     m_process->addExistingWebPage(*this, WebProcessProxy::BeginsUsingDataStore::Yes);
@@ -1072,6 +1076,14 @@ void WebPageProxy::addPlatformLoadParameters(LoadParameters&)
 }
 #endif
 
+WebProcessProxy& WebPageProxy::ensureRunningProcess()
+{
+    if (!hasRunningProcess())
+        launchProcess({ });
+
+    return m_process;
+}
+
 RefPtr<API::Navigation> WebPageProxy::loadRequest(ResourceRequest&& request, ShouldOpenExternalURLsPolicy shouldOpenExternalURLsPolicy, API::Object* userData)
 {
     if (m_isClosed)
index 357dc1b..3bc6a37 100644 (file)
@@ -1083,6 +1083,7 @@ public:
     bool isValidKeypressCommandName(const String& name) const { return m_knownKeypressCommandNames.contains(name); }
 #endif
 
+    WebProcessProxy& ensureRunningProcess();
     WebProcessProxy& process() { return m_process; }
     ProcessID processIdentifier() const;
 
index 7cb1bb4..89f83c2 100644 (file)
@@ -1210,13 +1210,18 @@ Ref<WebPageProxy> WebProcessPool::createWebPage(PageClient& pageClient, Ref<API:
     }
 
     RefPtr<WebProcessProxy> process;
-    if (pageConfiguration->relatedPage()) {
-        // Sharing processes, e.g. when creating the page via window.open().
-        process = &pageConfiguration->relatedPage()->process();
-        // We do not support several WebsiteDataStores sharing a single process.
-        ASSERT(process.get() == m_dummyProcessProxy || &pageConfiguration->websiteDataStore()->websiteDataStore() == &process->websiteDataStore());
-        ASSERT(&pageConfiguration->relatedPage()->websiteDataStore() == &pageConfiguration->websiteDataStore()->websiteDataStore());
-    } else if (!m_isDelayedWebProcessLaunchDisabled) {
+
+    if (m_isDelayedWebProcessLaunchDisabled) {
+        if (pageConfiguration->relatedPage()) {
+            // Sharing processes, e.g. when creating the page via window.open().
+            // Make sure the related page's process is not the dummy one.
+            process = &pageConfiguration->relatedPage()->ensureRunningProcess();
+            // We do not support several WebsiteDataStores sharing a single process.
+            ASSERT(process.get() == m_dummyProcessProxy || &pageConfiguration->websiteDataStore()->websiteDataStore() == &process->websiteDataStore());
+            ASSERT(&pageConfiguration->relatedPage()->websiteDataStore() == &pageConfiguration->websiteDataStore()->websiteDataStore());
+        } else
+            process = &processForRegistrableDomain(pageConfiguration->websiteDataStore()->websiteDataStore(), nullptr, { });
+    } else {
         // In the common case, we delay process launch until something is actually loaded in the page.
         if (!m_dummyProcessProxy) {
             auto dummyProcessProxy = WebProcessProxy::create(*this, nullptr, WebProcessProxy::IsPrewarmed::No, WebProcessProxy::ShouldLaunchProcess::No);
@@ -1224,8 +1229,7 @@ Ref<WebPageProxy> WebProcessPool::createWebPage(PageClient& pageClient, Ref<API:
             m_processes.append(WTFMove(dummyProcessProxy));
         }
         process = m_dummyProcessProxy;
-    } else
-        process = &processForRegistrableDomain(pageConfiguration->websiteDataStore()->websiteDataStore(), nullptr, { });
+    }
 
     ASSERT(process);
 
index a0de7e9..dd3f7a2 100644 (file)
@@ -1,3 +1,14 @@
+2019-04-22  Chris Dumez  <cdumez@apple.com>
+
+        Delayed WebProcessLaunch may break the _relatedWebView SPI
+        https://bugs.webkit.org/show_bug.cgi?id=197160
+
+        Reviewed by Alex Christensen.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/ProcessSwapOnNavigation.mm:
+
 2019-04-22  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         REGRESSION(r241289): [GTK] accessibility/removed-continuation-element-causes-crash.html and accessibility/removed-anonymous-block-child-causes-crash.html crashes
index fc4fa6a..6413209 100644 (file)
@@ -4598,6 +4598,46 @@ TEST(ProcessSwap, NoProcessSwapDueToRelatedView)
     runProcessSwapDueToRelatedWebViewTest([NSURL URLWithString:@"pson://www.webkit.org/main1.html"], [NSURL URLWithString:@"pson://www.webkit.org/main2.html"], ExpectSwap::No);
 }
 
+TEST(ProcessSwap, RelatedWebViewBeforeWebProcessLaunch)
+{
+    auto processPoolConfiguration = psonProcessPoolConfiguration();
+    auto processPool = adoptNS([[WKProcessPool alloc] _initWithConfiguration:processPoolConfiguration.get()]);
+
+    auto webView1Configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [webView1Configuration setProcessPool:processPool.get()];
+    auto handler = adoptNS([[PSONScheme alloc] init]);
+    [webView1Configuration setURLSchemeHandler:handler.get() forURLScheme:@"PSON"];
+
+    auto webView1 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:webView1Configuration.get()]);
+    auto delegate = adoptNS([[PSONNavigationDelegate alloc] init]);
+    [webView1 setNavigationDelegate:delegate.get()];
+
+    auto webView2Configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [webView2Configuration setProcessPool:processPool.get()];
+    [webView2Configuration setURLSchemeHandler:handler.get() forURLScheme:@"PSON"];
+    webView2Configuration.get()._relatedWebView = webView1.get(); // webView2 will be related to webView1 and webView1's URL will be used for process swap decision.
+    auto webView2 = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:webView2Configuration.get()]);
+    [webView2 setNavigationDelegate:delegate.get()];
+
+    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"pson://www.webkit.org/main1.html"]];
+    [webView1 loadRequest:request];
+
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+
+    auto pid1 = [webView1 _webProcessIdentifier];
+
+    request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"pson://www.webkit.org/main2.html"]];
+    [webView2 loadRequest:request];
+
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+
+    auto pid2 = [webView2 _webProcessIdentifier];
+
+    EXPECT_EQ(pid1, pid2); // WebViews are related so they should share the same process.
+}
+
 TEST(ProcessSwap, TerminatedSuspendedPageProcess)
 {
     auto processPoolConfiguration = psonProcessPoolConfiguration();