Skip to content

Commit

Permalink
Cherry-pick 616aa28. rdar://120988239
Browse files Browse the repository at this point in the history
    WebsiteDataStore should know about network process when it is added to the process
    https://bugs.webkit.org/show_bug.cgi?id=270241
    rdar://120988239

    Reviewed by Chris Dumez.

    UI process sends all WebsiteDataStores to network process when creating it, but does not notify WebsiteDataStore about
    the creation of network process. The result is that WebsiteDataStore does not know network process already exists and
    will skip some operations, see the places where m_networkProcess is checked in WebsiteDataStore.cpp. This has caused
    problems like tracking prevention flag is not updated correctly in network process.

    API test: ResourceLoadStatistics.EnableResourceLoadStatisticsAfterNetworkProcessCreation

    * Source/WebKit/UIProcess/Network/NetworkProcessProxy.cpp:
    (WebKit::NetworkProcessProxy::sendCreationParametersToNewProcess):
    * Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.cpp:
    (WebKit::WebsiteDataStore::setNetworkProcess):
    * Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.h:
    * Tools/TestWebKitAPI/Tests/WebKitCocoa/ResourceLoadStatistics.mm:
    (TEST):

    Canonical link: https://commits.webkit.org/275506@main

Identifier: 272448.675@safari-7618-branch
  • Loading branch information
szewai authored and Dan Robson committed Mar 4, 2024
1 parent 79d2d49 commit bbafbac
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Source/WebKit/UIProcess/Network/NetworkProcessProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ void NetworkProcessProxy::sendCreationParametersToNewProcess()
parameters.websiteDataStoreParameters = WebsiteDataStore::parametersFromEachWebsiteDataStore();
WebsiteDataStore::forEachWebsiteDataStore([&](auto& websiteDataStore) {
addSession(websiteDataStore, SendParametersToNetworkProcess::No);
// Notify WebsiteDataStore that they have been added to the network process.
websiteDataStore.setNetworkProcess(*this);
});
#endif

Expand Down
6 changes: 6 additions & 0 deletions Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ static Ref<NetworkProcessProxy> networkProcessForSession(PAL::SessionID sessionI
#endif
}

void WebsiteDataStore::setNetworkProcess(NetworkProcessProxy& networkProcess)
{
ASSERT(!m_networkProcess);
m_networkProcess = &networkProcess;
}

NetworkProcessProxy& WebsiteDataStore::networkProcess()
{
if (!m_networkProcess) {
Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class WebsiteDataStore : public API::ObjectImpl<API::Object::Type::WebsiteDataSt
NetworkProcessProxy& networkProcess();
Ref<NetworkProcessProxy> protectedNetworkProcess() const;
NetworkProcessProxy* networkProcessIfExists() { return m_networkProcess.get(); }
void setNetworkProcess(NetworkProcessProxy&);

static WebsiteDataStore* existingDataStoreForSessionID(PAL::SessionID);

Expand Down
61 changes: 61 additions & 0 deletions Tools/TestWebKitAPI/Tests/WebKitCocoa/ResourceLoadStatistics.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1670,3 +1670,64 @@ HTTPServer httpServer({
}];
TestWebKitAPI::Util::run(&done);
}

TEST(ResourceLoadStatistics, EnableResourceLoadStatisticsAfterNetworkProcessCreation)
{
__block bool done = false;
@autoreleasepool {
// Create WebsiteDataStore to leave ITP data on disk.
auto websiteDataStore = [WKWebsiteDataStore dataStoreForIdentifier:[[NSUUID alloc] initWithUUIDString:@"68753a44-4d6f-1226-9c60-0050e4c00067"]];
[websiteDataStore _setResourceLoadStatisticsEnabled:YES];
[websiteDataStore removeDataOfTypes:[WKWebsiteDataStore _allWebsiteDataTypesIncludingPrivate] modifiedSince:[NSDate distantPast] completionHandler:^ {
done = true;
}];
TestWebKitAPI::Util::run(&done);

auto *sharedProcessPool = [WKProcessPool _sharedProcessPool];
auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
configuration.get().processPool = sharedProcessPool;
configuration.get().websiteDataStore = websiteDataStore;
auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
[webView loadHTMLString:@"" baseURL:[NSURL URLWithString:@"http://webkit.org"]];
[webView _test_waitForDidFinishNavigation];

done = false;
[sharedProcessPool _seedResourceLoadStatisticsForTestingWithFirstParty:[NSURL URLWithString:@"http://webkit.org"] thirdParty:[NSURL URLWithString:@"http://evil.com"] shouldScheduleNotification:NO completionHandler: ^() {
done = true;
}];
TestWebKitAPI::Util::run(&done);

done = false;
[websiteDataStore _getResourceLoadStatisticsDataSummary:^(NSArray<_WKResourceLoadStatisticsThirdParty *> *thirdPartyData) {
EXPECT_EQ([thirdPartyData count], 1u);
done = true;
}];
TestWebKitAPI::Util::run(&done);

pid_t networkProcessIdentifier = [websiteDataStore _networkProcessIdentifier];
EXPECT_NE(networkProcessIdentifier, 0);
kill(networkProcessIdentifier, SIGKILL);
}

auto websiteDataStore1 = [WKWebsiteDataStore dataStoreForIdentifier:[[NSUUID alloc] initWithUUIDString:@"940e7729-738e-439f-a366-1a8719e23b2d"]];
[websiteDataStore1 _setResourceLoadStatisticsEnabled:NO];
auto websiteDataStore2 = [WKWebsiteDataStore dataStoreForIdentifier:[[NSUUID alloc] initWithUUIDString:@"68753a44-4d6f-1226-9c60-0050e4c00067"]];
[websiteDataStore2 _setResourceLoadStatisticsEnabled:NO];

// Wait until new network process is launched.
done = false;
[websiteDataStore1 removeDataOfTypes:[WKWebsiteDataStore _allWebsiteDataTypesIncludingPrivate] modifiedSince:[NSDate distantPast] completionHandler:^ {
done = true;
}];
TestWebKitAPI::Util::run(&done);
while (![websiteDataStore1 _networkProcessIdentifier])
TestWebKitAPI::Util::spinRunLoop(10);

[websiteDataStore2 _setResourceLoadStatisticsEnabled:YES];
done = false;
[websiteDataStore2 _getResourceLoadStatisticsDataSummary:^(NSArray<_WKResourceLoadStatisticsThirdParty *> *thirdPartyData) {
EXPECT_EQ([thirdPartyData count], 1u);
done = true;
}];
TestWebKitAPI::Util::run(&done);
}

0 comments on commit bbafbac

Please sign in to comment.