Title: [246097] trunk
Revision
246097
Author
[email protected]
Date
2019-06-04 22:00:23 -0700 (Tue, 04 Jun 2019)

Log Message

Cookies set via [WKHTTPCookieStore setCookie:] on store right after constructing WKWebView get lost
https://bugs.webkit.org/show_bug.cgi?id=198553
<rdar://problem/51317144>

Reviewed by Geoffrey Garen.

Source/WebKit:

If you call [WKHTTPCookieStore setCookie:] right after you construct the WKWebView and before the
WebContent process has finished launching, then WebsiteDataStore::processPoolForCookieStorageOperations()
would return null, even though there is already a view/page/WebProcessProxy/WebProcessPool for this data
store. As a result, the cookie would get added to the WebsiteDataStore's m_pendingCookies but it will
not be used since we've already previously launched a network process when we constructed the web view.

The reason processPoolForCookieStorageOperations() would return null is because WebsiteDataStore::processPools()
relies on WebProcessLifetimeObserver::processes() but processes only register themselves with the
WebProcessLifetimeObservers when they have pages *and* after they are finished launching.

This patch updates processPoolForCookieStorageOperations() to fallback to iterating over all process pools
and find a process pool with a process using the current data store and which has pages. This way, even if
the process is still launching, we'll properly find its associated process pool.

* UIProcess/WebsiteData/WebsiteDataStore.cpp:
(WebKit::WebsiteDataStore::processPoolForCookieStorageOperations):

Tools:

Add API test coverage.

* TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:
(-[CheckSessionCookieUIDelegate webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:]):
(TEST):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (246096 => 246097)


--- trunk/Source/WebKit/ChangeLog	2019-06-05 04:15:21 UTC (rev 246096)
+++ trunk/Source/WebKit/ChangeLog	2019-06-05 05:00:23 UTC (rev 246097)
@@ -1,3 +1,28 @@
+2019-06-04  Chris Dumez  <[email protected]>
+
+        Cookies set via [WKHTTPCookieStore setCookie:] on store right after constructing WKWebView get lost
+        https://bugs.webkit.org/show_bug.cgi?id=198553
+        <rdar://problem/51317144>
+
+        Reviewed by Geoffrey Garen.
+
+        If you call [WKHTTPCookieStore setCookie:] right after you construct the WKWebView and before the
+        WebContent process has finished launching, then WebsiteDataStore::processPoolForCookieStorageOperations()
+        would return null, even though there is already a view/page/WebProcessProxy/WebProcessPool for this data
+        store. As a result, the cookie would get added to the WebsiteDataStore's m_pendingCookies but it will
+        not be used since we've already previously launched a network process when we constructed the web view.
+
+        The reason processPoolForCookieStorageOperations() would return null is because WebsiteDataStore::processPools()
+        relies on WebProcessLifetimeObserver::processes() but processes only register themselves with the
+        WebProcessLifetimeObservers when they have pages *and* after they are finished launching.
+
+        This patch updates processPoolForCookieStorageOperations() to fallback to iterating over all process pools
+        and find a process pool with a process using the current data store and which has pages. This way, even if
+        the process is still launching, we'll properly find its associated process pool.
+
+        * UIProcess/WebsiteData/WebsiteDataStore.cpp:
+        (WebKit::WebsiteDataStore::processPoolForCookieStorageOperations):
+
 2019-06-04  Michael Catanzaro  <[email protected]>
 
         Fix miscellaneous build warnings

Modified: trunk/Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.cpp (246096 => 246097)


--- trunk/Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.cpp	2019-06-05 04:15:21 UTC (rev 246096)
+++ trunk/Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.cpp	2019-06-05 05:00:23 UTC (rev 246097)
@@ -166,7 +166,17 @@
 WebProcessPool* WebsiteDataStore::processPoolForCookieStorageOperations()
 {
     auto pools = processPools(1, false);
-    return pools.isEmpty() ? nullptr : pools.begin()->get();
+    if (!pools.isEmpty())
+        return pools.begin()->get();
+
+    for (auto* processPool : WebProcessPool::allProcessPools()) {
+        for (auto& process : processPool->processes()) {
+            if (process->pageCount() && &process->websiteDataStore() == this)
+                return processPool;
+        }
+    }
+
+    return nullptr;
 }
 
 void WebsiteDataStore::resolveDirectoriesIfNecessary()

Modified: trunk/Tools/ChangeLog (246096 => 246097)


--- trunk/Tools/ChangeLog	2019-06-05 04:15:21 UTC (rev 246096)
+++ trunk/Tools/ChangeLog	2019-06-05 05:00:23 UTC (rev 246097)
@@ -1,3 +1,17 @@
+2019-06-04  Chris Dumez  <[email protected]>
+
+        Cookies set via [WKHTTPCookieStore setCookie:] on store right after constructing WKWebView get lost
+        https://bugs.webkit.org/show_bug.cgi?id=198553
+        <rdar://problem/51317144>
+
+        Reviewed by Geoffrey Garen.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm:
+        (-[CheckSessionCookieUIDelegate webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:completionHandler:]):
+        (TEST):
+
 2019-06-04  Michael Catanzaro  <[email protected]>
 
         Fix miscellaneous build warnings

Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm (246096 => 246097)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm	2019-06-05 04:15:21 UTC (rev 246096)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKHTTPCookieStore.mm	2019-06-05 05:00:23 UTC (rev 246097)
@@ -586,3 +586,44 @@
 }
 
 #endif // PLATFORM(MAC)
+
+@interface CheckSessionCookieUIDelegate : NSObject <WKUIDelegate>
+@end
+
+@implementation CheckSessionCookieUIDelegate
+- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler
+{
+    EXPECT_STREQ("SessionCookieName=CookieValue", message.UTF8String);
+    finished = true;
+    completionHandler();
+}
+@end
+
+TEST(WebKit, WKHTTPCookieStoreWithoutProcessPoolEphemeralSession)
+{
+    RetainPtr<WKWebsiteDataStore> ephemeralStoreWithCookies = [WKWebsiteDataStore nonPersistentDataStore];
+    
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    configuration.get().websiteDataStore = ephemeralStoreWithCookies.get();
+    
+    auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+    auto delegate = adoptNS([[CheckSessionCookieUIDelegate alloc] init]);
+    webView.get().UIDelegate = delegate.get();
+    
+    RetainPtr<NSHTTPCookie> sessionCookie = [NSHTTPCookie cookieWithProperties:@{
+        NSHTTPCookiePath: @"/",
+        NSHTTPCookieName: @"SessionCookieName",
+        NSHTTPCookieValue: @"CookieValue",
+        NSHTTPCookieDomain: @"127.0.0.1",
+    }];
+    
+    [ephemeralStoreWithCookies.get().httpCookieStore setCookie:sessionCookie.get() completionHandler:^{
+        finished = true;
+    }];
+    TestWebKitAPI::Util::run(&finished);
+    finished = false;
+    
+    NSString *alertCookieHTML = @"<script>var cookies = document.cookie.split(';'); for (let i = 0; i < cookies.length; i ++) { cookies[i] = cookies[i].trim(); } cookies.sort(); alert(cookies.join('; '));</script>";
+    [webView loadHTMLString:alertCookieHTML baseURL:[NSURL URLWithString:@"http://127.0.0.1"]];
+    TestWebKitAPI::Util::run(&finished);
+}
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to