Title: [282110] trunk/Source/WebKit
Revision
282110
Author
[email protected]
Date
2021-09-07 15:14:26 -0700 (Tue, 07 Sep 2021)

Log Message

Replace PrivateClickMeasurementNetworkLoader::start with an implementation that doesn't need NetworkSession
https://bugs.webkit.org/show_bug.cgi?id=230015

Reviewed by John Wilander.

This is needed to run this code in a process that doesn't have NetworkSession, which is a class tied closely to WKWebsiteDataStore.
This replaces it with a simple implementation that does the same things:
1. Uses an ephemeral stateless session that doesn't accept cookies or use credentials.
2. Does not allow redirects.
3. Only accepts JSON mime types.
4. Calls processPCMRequest

We still need to call NetworkSessionCocoa::allowsSpecificHTTPSCertificateForHost to get tests to pass.
Covered by existing tests.

* NetworkProcess/NetworkLoadParameters.h:
* NetworkProcess/NetworkResourceLoadParameters.cpp:
(WebKit::NetworkResourceLoadParameters::encode const):
(WebKit::NetworkResourceLoadParameters::decode):
* NetworkProcess/NetworkSession.h:
(WebKit::NetworkSession::addPrivateClickMeasurementNetworkLoader): Deleted.
(WebKit::NetworkSession::removePrivateClickMeasurementNetworkLoader): Deleted.
* NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClient.h:
(WebKit::PCM::Client::~Client):
* NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClientImpl.cpp:
(WebKit::PCM::ClientImpl::loadFromNetwork): Deleted.
* NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClientImpl.h:
* NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementManager.cpp:
(WebKit::PrivateClickMeasurementManager::getTokenPublicKey):
(WebKit::PrivateClickMeasurementManager::getSignedUnlinkableToken):
(WebKit::PrivateClickMeasurementManager::fireConversionRequestImpl):
* NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementNetworkLoader.cpp:
(WebKit::PCM::NetworkLoader::start):
(WebKit::generateNetworkLoadParameters): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::start): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::PrivateClickMeasurementNetworkLoader): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::~PrivateClickMeasurementNetworkLoader): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::fail): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::cancel): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::willSendRedirectedRequest): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::didReceiveResponse): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::didReceiveBuffer): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::didFinishLoading): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::didFailLoading): Deleted.
(WebKit::PrivateClickMeasurementNetworkLoader::didComplete): Deleted.
* NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementNetworkLoader.h:
(): Deleted.
* NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementNetworkLoaderCocoa.mm: Added.
(processPCMRequest):
(-[WKNetworkSessionDelegateAllowingOnlyNonRedirectedJSON URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:]):
(-[WKNetworkSessionDelegateAllowingOnlyNonRedirectedJSON URLSession:dataTask:didReceiveResponse:completionHandler:]):
(-[WKNetworkSessionDelegateAllowingOnlyNonRedirectedJSON URLSession:task:didReceiveChallenge:completionHandler:]):
(WebKit::PCM::taskMap):
(WebKit::PCM::statelessSessionWithoutRedirects):
(WebKit::PCM::NetworkLoader::start):
* NetworkProcess/cocoa/NetworkDataTaskCocoa.h:
* NetworkProcess/cocoa/NetworkDataTaskCocoa.mm:
(WebKit::NetworkDataTaskCocoa::NetworkDataTaskCocoa):
(processPCMRequest): Deleted.
* SourcesCocoa.txt:
* WebKit.xcodeproj/project.pbxproj:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (282109 => 282110)


--- trunk/Source/WebKit/ChangeLog	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/ChangeLog	2021-09-07 22:14:26 UTC (rev 282110)
@@ -1,5 +1,69 @@
 2021-09-07  Alex Christensen  <[email protected]>
 
+        Replace PrivateClickMeasurementNetworkLoader::start with an implementation that doesn't need NetworkSession
+        https://bugs.webkit.org/show_bug.cgi?id=230015
+
+        Reviewed by John Wilander.
+
+        This is needed to run this code in a process that doesn't have NetworkSession, which is a class tied closely to WKWebsiteDataStore.
+        This replaces it with a simple implementation that does the same things:
+        1. Uses an ephemeral stateless session that doesn't accept cookies or use credentials.
+        2. Does not allow redirects.
+        3. Only accepts JSON mime types.
+        4. Calls processPCMRequest
+
+        We still need to call NetworkSessionCocoa::allowsSpecificHTTPSCertificateForHost to get tests to pass.
+        Covered by existing tests.
+
+        * NetworkProcess/NetworkLoadParameters.h:
+        * NetworkProcess/NetworkResourceLoadParameters.cpp:
+        (WebKit::NetworkResourceLoadParameters::encode const):
+        (WebKit::NetworkResourceLoadParameters::decode):
+        * NetworkProcess/NetworkSession.h:
+        (WebKit::NetworkSession::addPrivateClickMeasurementNetworkLoader): Deleted.
+        (WebKit::NetworkSession::removePrivateClickMeasurementNetworkLoader): Deleted.
+        * NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClient.h:
+        (WebKit::PCM::Client::~Client):
+        * NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClientImpl.cpp:
+        (WebKit::PCM::ClientImpl::loadFromNetwork): Deleted.
+        * NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClientImpl.h:
+        * NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementManager.cpp:
+        (WebKit::PrivateClickMeasurementManager::getTokenPublicKey):
+        (WebKit::PrivateClickMeasurementManager::getSignedUnlinkableToken):
+        (WebKit::PrivateClickMeasurementManager::fireConversionRequestImpl):
+        * NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementNetworkLoader.cpp:
+        (WebKit::PCM::NetworkLoader::start):
+        (WebKit::generateNetworkLoadParameters): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::start): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::PrivateClickMeasurementNetworkLoader): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::~PrivateClickMeasurementNetworkLoader): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::fail): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::cancel): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::willSendRedirectedRequest): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::didReceiveResponse): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::didReceiveBuffer): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::didFinishLoading): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::didFailLoading): Deleted.
+        (WebKit::PrivateClickMeasurementNetworkLoader::didComplete): Deleted.
+        * NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementNetworkLoader.h:
+        (): Deleted.
+        * NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementNetworkLoaderCocoa.mm: Added.
+        (processPCMRequest):
+        (-[WKNetworkSessionDelegateAllowingOnlyNonRedirectedJSON URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:]):
+        (-[WKNetworkSessionDelegateAllowingOnlyNonRedirectedJSON URLSession:dataTask:didReceiveResponse:completionHandler:]):
+        (-[WKNetworkSessionDelegateAllowingOnlyNonRedirectedJSON URLSession:task:didReceiveChallenge:completionHandler:]):
+        (WebKit::PCM::taskMap):
+        (WebKit::PCM::statelessSessionWithoutRedirects):
+        (WebKit::PCM::NetworkLoader::start):
+        * NetworkProcess/cocoa/NetworkDataTaskCocoa.h:
+        * NetworkProcess/cocoa/NetworkDataTaskCocoa.mm:
+        (WebKit::NetworkDataTaskCocoa::NetworkDataTaskCocoa):
+        (processPCMRequest): Deleted.
+        * SourcesCocoa.txt:
+        * WebKit.xcodeproj/project.pbxproj:
+
+2021-09-07  Alex Christensen  <[email protected]>
+
         Simplify PrivateClickMeasurementManager interface
         https://bugs.webkit.org/show_bug.cgi?id=230014
 

Modified: trunk/Source/WebKit/NetworkProcess/NetworkLoadParameters.h (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/NetworkLoadParameters.h	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/NetworkLoadParameters.h	2021-09-07 22:14:26 UTC (rev 282110)
@@ -67,7 +67,6 @@
     PreconnectOnly shouldPreconnectOnly { PreconnectOnly::No };
     std::optional<NetworkActivityTracker> networkActivityTracker;
     std::optional<NavigatingToAppBoundDomain> isNavigatingToAppBoundDomain { NavigatingToAppBoundDomain::No };
-    std::optional<WebCore::PrivateClickMeasurement::PcmDataCarried> pcmDataCarried;
 };
 
 } // namespace WebKit

Modified: trunk/Source/WebKit/NetworkProcess/NetworkResourceLoadParameters.cpp (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/NetworkResourceLoadParameters.cpp	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/NetworkResourceLoadParameters.cpp	2021-09-07 22:14:26 UTC (rev 282110)
@@ -127,7 +127,6 @@
 #endif
     
     encoder << isNavigatingToAppBoundDomain;
-    encoder << pcmDataCarried;
 }
 
 std::optional<NetworkResourceLoadParameters> NetworkResourceLoadParameters::decode(IPC::Decoder& decoder)
@@ -329,12 +328,6 @@
     if (!isNavigatingToAppBoundDomain)
         return std::nullopt;
     result.isNavigatingToAppBoundDomain = *isNavigatingToAppBoundDomain;
-
-    std::optional<std::optional<WebCore::PrivateClickMeasurement::PcmDataCarried>> pcmDataCarried;
-    decoder >> pcmDataCarried;
-    if (!pcmDataCarried)
-        return std::nullopt;
-    result.pcmDataCarried = *pcmDataCarried;
     
     return result;
 }

Modified: trunk/Source/WebKit/NetworkProcess/NetworkSession.h (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/NetworkSession.h	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/NetworkSession.h	2021-09-07 22:14:26 UTC (rev 282110)
@@ -29,7 +29,6 @@
 #include "NavigatingToAppBoundDomain.h"
 #include "NetworkResourceLoadIdentifier.h"
 #include "PrefetchCache.h"
-#include "PrivateClickMeasurementNetworkLoader.h"
 #include "SandboxExtension.h"
 #include "ServiceWorkerSoftUpdateLoader.h"
 #include "WebResourceLoadStatisticsStore.h"
@@ -174,9 +173,6 @@
     AppPrivacyReportTestingData& appPrivacyReportTestingData() { return m_appPrivacyReportTestingData; }
 #endif
 
-    void addPrivateClickMeasurementNetworkLoader(std::unique_ptr<PrivateClickMeasurementNetworkLoader>&& loader) { m_privateClickMeasurementNetworkLoaders.add(WTFMove(loader)); }
-    void removePrivateClickMeasurementNetworkLoader(PrivateClickMeasurementNetworkLoader* loader) { m_privateClickMeasurementNetworkLoaders.remove(loader); }
-
     virtual void removeNetworkWebsiteData(std::optional<WallTime>, std::optional<HashSet<WebCore::RegistrableDomain>>&&, CompletionHandler<void()>&& completionHandler) { completionHandler(); }
 
     virtual void addWebPageNetworkParameters(WebPageProxyIdentifier, WebPageNetworkParameters&&) { }
@@ -248,7 +244,6 @@
     AppPrivacyReportTestingData m_appPrivacyReportTestingData;
 #endif
 
-    HashSet<std::unique_ptr<PrivateClickMeasurementNetworkLoader>> m_privateClickMeasurementNetworkLoaders;
     HashMap<WebPageProxyIdentifier, String> m_attributedBundleIdentifierFromPageIdentifiers;
 };
 

Modified: trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClient.h (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClient.h	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClient.h	2021-09-07 22:14:26 UTC (rev 282110)
@@ -46,8 +46,6 @@
 class Client {
 public:
     virtual ~Client() { }
-    using NetworkLoadCallback = CompletionHandler<void(const WebCore::ResourceError&, const WebCore::ResourceResponse&, const RefPtr<JSON::Object>&)>;
-    virtual void loadFromNetwork(URL&&, RefPtr<JSON::Object>&&, WebCore::PrivateClickMeasurement::PcmDataCarried, NetworkLoadCallback&&) = 0;
     virtual void broadcastConsoleMessage(JSC::MessageLevel, const String&) = 0;
     virtual bool featureEnabled() const = 0;
     virtual bool debugModeEnabled() const = 0;

Modified: trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClientImpl.cpp (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClientImpl.cpp	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClientImpl.cpp	2021-09-07 22:14:26 UTC (rev 282110)
@@ -37,14 +37,6 @@
     : m_networkSession(makeWeakPtr(session))
     , m_networkProcess(networkProcess) { }
 
-void ClientImpl::loadFromNetwork(URL&& url, RefPtr<JSON::Object>&& jsonPayload, WebCore::PrivateClickMeasurement::PcmDataCarried pcmDataCarried, NetworkLoadCallback&& completionHandler)
-{
-    if (!featureEnabled())
-        return completionHandler(WebCore::ResourceError(WebCore::ResourceError::Type::Cancellation), { }, { });
-
-    PrivateClickMeasurementNetworkLoader::start(*m_networkSession, WTFMove(url), WTFMove(jsonPayload), pcmDataCarried, WTFMove(completionHandler));
-}
-
 void ClientImpl::broadcastConsoleMessage(JSC::MessageLevel messageLevel, const String& message)
 {
     if (!featureEnabled())

Modified: trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClientImpl.h (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClientImpl.h	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementClientImpl.h	2021-09-07 22:14:26 UTC (rev 282110)
@@ -43,7 +43,6 @@
     ClientImpl(NetworkSession&, NetworkProcess&);
 
 private:
-    void loadFromNetwork(URL&&, RefPtr<JSON::Object>&&, WebCore::PrivateClickMeasurement::PcmDataCarried, NetworkLoadCallback&&) final;
     void broadcastConsoleMessage(JSC::MessageLevel, const String&) final;
     bool featureEnabled() const final;
     bool debugModeEnabled() const final;

Modified: trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementManager.cpp (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementManager.cpp	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementManager.cpp	2021-09-07 22:14:26 UTC (rev 282110)
@@ -29,6 +29,7 @@
 #include "Logging.h"
 #include "NetworkSession.h"
 #include "PrivateClickMeasurementDebugInfo.h"
+#include "PrivateClickMeasurementNetworkLoader.h"
 #include <_javascript_Core/ConsoleTypes.h>
 #include <WebCore/FetchOptions.h>
 #include <WebCore/FormData.h>
@@ -131,7 +132,7 @@
     RELEASE_LOG_INFO(PrivateClickMeasurement, "About to fire a token public key request.");
     m_client->broadcastConsoleMessage(MessageLevel::Log, "[Private Click Measurement] About to fire a token public key request."_s);
 
-    m_client->loadFromNetwork(WTFMove(tokenPublicKeyURL), nullptr, pcmDataCarried, [weakThis = makeWeakPtr(*this), this, attribution = WTFMove(attribution), callback = WTFMove(callback)] (auto& error, auto& response, auto& jsonObject) mutable {
+    PCM::NetworkLoader::start(WTFMove(tokenPublicKeyURL), nullptr, pcmDataCarried, [weakThis = makeWeakPtr(*this), this, attribution = WTFMove(attribution), callback = WTFMove(callback)] (auto& error, auto& response, auto& jsonObject) mutable {
         if (!weakThis)
             return;
 
@@ -149,7 +150,6 @@
 
         callback(WTFMove(attribution), jsonObject->getString("token_public_key"_s));
     });
-
 }
 
 void PrivateClickMeasurementManager::getSignedUnlinkableToken(PrivateClickMeasurement&& measurement)
@@ -175,7 +175,7 @@
     RELEASE_LOG_INFO(PrivateClickMeasurement, "About to fire a unlinkable token signing request.");
     m_client->broadcastConsoleMessage(MessageLevel::Log, "[Private Click Measurement] About to fire a unlinkable token signing request."_s);
 
-    m_client->loadFromNetwork(WTFMove(tokenSignatureURL), measurement.tokenSignatureJSON(), pcmDataCarried, [weakThis = makeWeakPtr(*this), this, measurement = WTFMove(measurement)] (auto& error, auto& response, auto& jsonObject) mutable {
+    PCM::NetworkLoader::start(WTFMove(tokenSignatureURL), measurement.tokenSignatureJSON(), pcmDataCarried, [weakThis = makeWeakPtr(*this), this, measurement = WTFMove(measurement)] (auto& error, auto& response, auto& jsonObject) mutable {
         if (!weakThis)
             return;
 
@@ -357,7 +357,7 @@
     RELEASE_LOG_INFO(PrivateClickMeasurement, "About to fire an attribution request.");
     m_client->broadcastConsoleMessage(MessageLevel::Log, "[Private Click Measurement] About to fire an attribution request."_s);
 
-    m_client->loadFromNetwork(WTFMove(attributionURL), attribution.attributionReportJSON(), pcmDataCarried, [weakThis = makeWeakPtr(*this), this](auto& error, auto& response, auto&) {
+    PCM::NetworkLoader::start(WTFMove(attributionURL), attribution.attributionReportJSON(), pcmDataCarried, [weakThis = makeWeakPtr(*this), this](auto& error, auto& response, auto&) {
         if (!weakThis)
             return;
 

Modified: trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementNetworkLoader.cpp (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementNetworkLoader.cpp	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementNetworkLoader.cpp	2021-09-07 22:14:26 UTC (rev 282110)
@@ -26,124 +26,20 @@
 #include "config.h"
 #include "PrivateClickMeasurementNetworkLoader.h"
 
-#include "NetworkLoad.h"
-#include "NetworkSession.h"
-#include <WebCore/HTTPHeaderValues.h>
-#include <WebCore/MIMETypeRegistry.h>
-#include <WebCore/RuntimeApplicationChecks.h>
-#include <WebCore/TextResourceDecoder.h>
+#include <WebCore/NotImplemented.h>
 
-#include <wtf/JSONValues.h>
-
 namespace WebKit {
 
-using namespace WebCore;
+namespace PCM {
 
-static NetworkLoadParameters generateNetworkLoadParameters(URL&& url, RefPtr<JSON::Object>&& jsonPayload, PrivateClickMeasurement::PcmDataCarried pcmDataCarried)
+#if !PLATFORM(COCOA)
+void NetworkLoader::start(URL&&, RefPtr<JSON::Object>&&, PrivateClickMeasurement::PcmDataCarried, Callback&& completionHandler)
 {
-    String httpMethod = jsonPayload ? "POST"_s : "GET"_s;
-
-    ResourceRequest request { WTFMove(url) };
-    request.setHTTPMethod(httpMethod);
-    request.setHTTPHeaderField(HTTPHeaderName::CacheControl, WebCore::HTTPHeaderValues::maxAge0());
-    if (jsonPayload) {
-        request.setHTTPContentType(WebCore::HTTPHeaderValues::applicationJSONContentType());
-        request.setHTTPBody(WebCore::FormData::create(jsonPayload->toJSONString().utf8()));
-    }
-
-    NetworkLoadParameters loadParameters;
-    loadParameters.request = request;
-    loadParameters.parentPID = presentingApplicationPID();
-    loadParameters.storedCredentialsPolicy = StoredCredentialsPolicy::EphemeralStateless;
-    loadParameters.shouldClearReferrerOnHTTPSToHTTPRedirect = true;
-    loadParameters.pcmDataCarried = pcmDataCarried;
-
-    return loadParameters;
+    notImplemented();
+    completionHandler({ }, { }, { });
 }
+#endif
 
-void PrivateClickMeasurementNetworkLoader::start(NetworkSession& session, URL&& url, RefPtr<JSON::Object>&& jsonPayload, PrivateClickMeasurement::PcmDataCarried pcmDataCarried, Callback&& completionHandler)
-{
-    auto parameters = generateNetworkLoadParameters(WTFMove(url), WTFMove(jsonPayload), pcmDataCarried);
-    auto loader = std::unique_ptr<PrivateClickMeasurementNetworkLoader>(new PrivateClickMeasurementNetworkLoader(session, WTFMove(parameters), WTFMove(completionHandler)));
-    session.addPrivateClickMeasurementNetworkLoader(WTFMove(loader));
-}
+} // namespace PCM
 
-PrivateClickMeasurementNetworkLoader::PrivateClickMeasurementNetworkLoader(NetworkSession& session, NetworkLoadParameters&& parameters, Callback&& completionHandler)
-    : m_session(makeWeakPtr(session))
-    , m_completionHandler(WTFMove(completionHandler))
-{
-    m_networkLoad = makeUnique<NetworkLoad>(*this, nullptr, WTFMove(parameters), *m_session);
-    m_networkLoad->start();
-}
-
-PrivateClickMeasurementNetworkLoader::~PrivateClickMeasurementNetworkLoader()
-{
-    cancel();
-}
-
-void PrivateClickMeasurementNetworkLoader::fail(ResourceError&& error)
-{
-    if (!m_completionHandler)
-        return;
-
-    m_completionHandler(WTFMove(error), { }, nullptr);
-    didComplete();
-}
-
-void PrivateClickMeasurementNetworkLoader::cancel()
-{
-    fail(ResourceError { ResourceError::Type::Cancellation });
-}
-
-void PrivateClickMeasurementNetworkLoader::willSendRedirectedRequest(ResourceRequest&&, ResourceRequest&&, ResourceResponse&&)
-{
-    cancel();
-}
-
-void PrivateClickMeasurementNetworkLoader::didReceiveResponse(ResourceResponse&& response, ResponseCompletionHandler&& completionHandler)
-{
-    if (!MIMETypeRegistry::isSupportedJSONMIMEType(response.mimeType())) {
-        fail({ errorDomainWebKitInternal, 0, response.url(), "MIME Type is not a JSON MIME type"_s });
-        completionHandler(PolicyAction::Ignore);
-        return;
-    }
-
-    m_response = WTFMove(response);
-    completionHandler(PolicyAction::Use);
-}
-
-void PrivateClickMeasurementNetworkLoader::didReceiveBuffer(Ref<SharedBuffer>&& buffer, int reportedEncodedDataLength)
-{
-    if (!m_decoder)
-        m_decoder = TextResourceDecoder::create("application/json"_s, m_response.textEncodingName().isEmpty() ? WebCore::TextEncoding("UTF-8") : WebCore::TextEncoding(m_response.textEncodingName()));
-
-    if (auto size = buffer->size())
-        m_jsonString.append(m_decoder->decode(buffer->data(), size));
-}
-
-void PrivateClickMeasurementNetworkLoader::didFinishLoading(const WebCore::NetworkLoadMetrics&)
-{
-    if (m_decoder)
-        m_jsonString.append(m_decoder->flush());
-
-    if (auto jsonValue = JSON::Value::parseJSON(m_jsonString.toString()))
-        m_completionHandler({ }, m_response, jsonValue->asObject());
-    else
-        m_completionHandler({ }, m_response, nullptr);
-
-    didComplete();
-}
-
-void PrivateClickMeasurementNetworkLoader::didFailLoading(const ResourceError& error)
-{
-    fail(ResourceError(error));
-}
-
-void PrivateClickMeasurementNetworkLoader::didComplete()
-{
-    m_networkLoad = nullptr;
-    if (m_session)
-        m_session->removePrivateClickMeasurementNetworkLoader(this);
-}
-
 } // namespace WebKit

Modified: trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementNetworkLoader.h (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementNetworkLoader.h	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/PrivateClickMeasurementNetworkLoader.h	2021-09-07 22:14:26 UTC (rev 282110)
@@ -25,49 +25,25 @@
 
 #pragma once
 
-#include "NetworkLoad.h"
-#include "NetworkLoadClient.h"
-#include "NetworkLoadParameters.h"
-#include <WebCore/TextResourceDecoder.h>
 #include <wtf/CompletionHandler.h>
-#include <wtf/WeakPtr.h>
+#include <wtf/JSONValues.h>
 
+namespace WebCore {
+class ResourceError;
+class ResourceResponse;
+}
+
 namespace WebKit {
 
-class NetworkSession;
+namespace PCM {
 
-class PrivateClickMeasurementNetworkLoader final : public NetworkLoadClient {
+class NetworkLoader {
     WTF_MAKE_FAST_ALLOCATED;
 public:
     using Callback = CompletionHandler<void(const WebCore::ResourceError&, const WebCore::ResourceResponse&, const RefPtr<JSON::Object>&)>;
-    static void start(NetworkSession&, URL&&, RefPtr<JSON::Object>&&, WebCore::PrivateClickMeasurement::PcmDataCarried, Callback&&);
+    static void start(URL&&, RefPtr<JSON::Object>&&, WebCore::PrivateClickMeasurement::PcmDataCarried, Callback&&);
+};
 
-    ~PrivateClickMeasurementNetworkLoader();
+} // namespace PCM
 
-private:
-    PrivateClickMeasurementNetworkLoader(NetworkSession&, NetworkLoadParameters&&, Callback&&);
-
-    // NetworkLoadClient.
-    void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) final { }
-    bool isSynchronous() const final { return false; }
-    bool isAllowedToAskUserForCredentials() const final { return false; }
-    void willSendRedirectedRequest(WebCore::ResourceRequest&&, WebCore::ResourceRequest&& redirectRequest, WebCore::ResourceResponse&& redirectResponse) final;
-    void didReceiveResponse(WebCore::ResourceResponse&&, ResponseCompletionHandler&&) final;
-    void didReceiveBuffer(Ref<WebCore::SharedBuffer>&&, int reportedEncodedDataLength) final;
-    void didFinishLoading(const WebCore::NetworkLoadMetrics&) final;
-    void didFailLoading(const WebCore::ResourceError&) final;
-
-    void fail(WebCore::ResourceError&&);
-    void cancel();
-    void didComplete();
-
-    WeakPtr<NetworkSession> m_session;
-    std::unique_ptr<NetworkLoad> m_networkLoad;
-    Callback m_completionHandler;
-
-    WebCore::ResourceResponse m_response;
-    RefPtr<WebCore::TextResourceDecoder> m_decoder;
-    StringBuilder m_jsonString;
-};
-
 } // namespace WebKit

Added: trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementNetworkLoaderCocoa.mm (0 => 282110)


--- trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementNetworkLoaderCocoa.mm	                        (rev 0)
+++ trunk/Source/WebKit/NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementNetworkLoaderCocoa.mm	2021-09-07 22:14:26 UTC (rev 282110)
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2021 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+#import "PrivateClickMeasurementNetworkLoader.h"
+
+#import "NetworkSessionCocoa.h"
+#import <WebCore/HTTPHeaderValues.h>
+#import <WebCore/MIMETypeRegistry.h>
+#import <pal/spi/cf/CFNetworkSPI.h>
+#import <wtf/NeverDestroyed.h>
+
+#if USE(APPLE_INTERNAL_SDK)
+#import <WebKitAdditions/NetworkDataTaskCocoaAdditions.h>
+#else
+static void processPCMRequest(WebCore::PrivateClickMeasurement::PcmDataCarried, NSMutableURLRequest *) { }
+#endif
+
+@interface WKNetworkSessionDelegateAllowingOnlyNonRedirectedJSON : NSObject <NSURLSessionDataDelegate>
+@end
+
+@implementation WKNetworkSessionDelegateAllowingOnlyNonRedirectedJSON
+
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler
+{
+    completionHandler(nil);
+}
+
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
+{
+    if (MIMETypeRegistry::isSupportedJSONMIMEType(response.MIMEType))
+        return completionHandler(NSURLSessionResponseAllow);
+    completionHandler(NSURLSessionResponseCancel);
+}
+
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
+{
+    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]
+        && NetworkSessionCocoa::allowsSpecificHTTPSCertificateForHost(challenge))
+        return completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
+    completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
+}
+
+@end
+
+namespace WebKit {
+
+namespace PCM {
+
+enum LoadTaskIdentifierType { };
+using LoadTaskIdentifier = ObjectIdentifier<LoadTaskIdentifierType>;
+static HashMap<LoadTaskIdentifier, RetainPtr<NSURLSessionDataTask>>& taskMap()
+{
+    static NeverDestroyed<HashMap<LoadTaskIdentifier, RetainPtr<NSURLSessionDataTask>>> map;
+    return map.get();
+}
+
+static NSURLSession *statelessSessionWithoutRedirects()
+{
+    static NeverDestroyed<RetainPtr<WKNetworkSessionDelegateAllowingOnlyNonRedirectedJSON>> delegate = adoptNS([WKNetworkSessionDelegateAllowingOnlyNonRedirectedJSON new]);
+    static NeverDestroyed<RetainPtr<NSURLSession>> session = [&] {
+        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
+        configuration.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever;
+        configuration.URLCredentialStorage = nil;
+        configuration.URLCache = nil;
+        configuration.HTTPCookieStorage = nil;
+        configuration._shouldSkipPreferredClientCertificateLookup = YES;
+        return adoptNS([NSURLSession sessionWithConfiguration:configuration delegate:delegate.get().get() delegateQueue:[NSOperationQueue mainQueue]]);
+    }();
+    return session.get().get();
+}
+
+void NetworkLoader::start(URL&& url, RefPtr<JSON::Object>&& jsonPayload, WebCore::PrivateClickMeasurement::PcmDataCarried pcmDataCarried, Callback&& callback)
+{
+    auto request = adoptNS([[NSMutableURLRequest alloc] initWithURL:url]);
+    [request setValue:WebCore::HTTPHeaderValues::maxAge0() forHTTPHeaderField:@"Cache-Control"];
+    if (jsonPayload) {
+        request.get().HTTPMethod = @"POST";
+        [request setValue:WebCore::HTTPHeaderValues::applicationJSONContentType() forHTTPHeaderField:@"Content-Type"];
+        auto body = jsonPayload->toJSONString().utf8();
+        request.get().HTTPBody = adoptNS([[NSData alloc] initWithBytes:body.data() length:body.length()]).get();
+    }
+
+    processPCMRequest(pcmDataCarried, request.get());
+
+    auto identifier = LoadTaskIdentifier::generate();
+    NSURLSessionDataTask *task = [statelessSessionWithoutRedirects() dataTaskWithRequest:request.get() completionHandler:makeBlockPtr([callback = WTFMove(callback), identifier](NSData *data, NSURLResponse *response, NSError *error) mutable {
+        taskMap().remove(identifier);
+        if (error)
+            return callback(error, { }, { });
+        if (auto jsonValue = JSON::Value::parseJSON(String::fromUTF8(static_cast<const LChar*>(data.bytes), data.length)))
+            return callback({ }, response, jsonValue->asObject());
+        callback({ }, response, nullptr);
+    }).get()];
+    [task resume];
+    taskMap().add(identifier, task);
+}
+
+} // namespace PCM
+
+} // namespace WebKit

Modified: trunk/Source/WebKit/NetworkProcess/cocoa/NetworkDataTaskCocoa.h (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/cocoa/NetworkDataTaskCocoa.h	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/cocoa/NetworkDataTaskCocoa.h	2021-09-07 22:14:26 UTC (rev 282110)
@@ -29,10 +29,12 @@
 #include "NetworkDataTask.h"
 #include "NetworkLoadParameters.h"
 #include <WebCore/NetworkLoadMetrics.h>
+#include <WebCore/PrivateClickMeasurement.h>
 #include <wtf/RetainPtr.h>
 
 OBJC_CLASS NSHTTPCookieStorage;
 OBJC_CLASS NSURLSessionDataTask;
+OBJC_CLASS NSMutableURLRequest;
 
 namespace WebCore {
 class RegistrableDomain;

Modified: trunk/Source/WebKit/NetworkProcess/cocoa/NetworkDataTaskCocoa.mm (282109 => 282110)


--- trunk/Source/WebKit/NetworkProcess/cocoa/NetworkDataTaskCocoa.mm	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/NetworkProcess/cocoa/NetworkDataTaskCocoa.mm	2021-09-07 22:14:26 UTC (rev 282110)
@@ -57,7 +57,6 @@
 #if USE(APPLE_INTERNAL_SDK)
 #import <WebKitAdditions/NetworkDataTaskCocoaAdditions.h>
 #else
-static void processPCMRequest(WebCore::PrivateClickMeasurement::PcmDataCarried, NSMutableURLRequest *) { }
 static void overrideAttributionContext(NSMutableURLRequest *) { }
 #endif
 
@@ -347,9 +346,6 @@
 
     overrideAttributionContext(mutableRequest.get());
 
-    if (parameters.pcmDataCarried)
-        processPCMRequest(*parameters.pcmDataCarried, mutableRequest.get());
-
     nsRequest = mutableRequest;
 
 #if ENABLE(APP_PRIVACY_REPORT)

Modified: trunk/Source/WebKit/SourcesCocoa.txt (282109 => 282110)


--- trunk/Source/WebKit/SourcesCocoa.txt	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/SourcesCocoa.txt	2021-09-07 22:14:26 UTC (rev 282110)
@@ -41,6 +41,8 @@
 NetworkProcess/EntryPoint/Cocoa/Daemon/DaemonEntryPoint.mm
 NetworkProcess/EntryPoint/Cocoa/XPCService/NetworkServiceEntryPoint.mm
 
+NetworkProcess/PrivateClickMeasurement/cocoa/PrivateClickMeasurementNetworkLoaderCocoa.mm
+
 NetworkProcess/WebStorage/ios/LocalStorageDatabaseTrackerIOS.mm
 
 NetworkProcess/ios/NetworkConnectionToWebProcessIOS.mm

Modified: trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj (282109 => 282110)


--- trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj	2021-09-07 22:13:17 UTC (rev 282109)
+++ trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj	2021-09-07 22:14:26 UTC (rev 282110)
@@ -4555,6 +4555,7 @@
 		5CB930BF26E0542E0032B1C0 /* PrivateClickMeasurementClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrivateClickMeasurementClient.h; sourceTree = "<group>"; };
 		5CB930C026E0542F0032B1C0 /* PrivateClickMeasurementClientImpl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PrivateClickMeasurementClientImpl.cpp; sourceTree = "<group>"; };
 		5CB930C126E054B80032B1C0 /* PrivateClickMeasurementClientImpl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrivateClickMeasurementClientImpl.h; sourceTree = "<group>"; };
+		5CB930F226E7EEE00032B1C0 /* PrivateClickMeasurementNetworkLoaderCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = PrivateClickMeasurementNetworkLoaderCocoa.mm; sourceTree = "<group>"; };
 		5CBC9B891C6524A500A8FDCF /* NetworkDataTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkDataTask.h; sourceTree = "<group>"; };
 		5CBC9B8B1C65257300A8FDCF /* NetworkDataTaskCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = NetworkDataTaskCocoa.mm; sourceTree = "<group>"; };
 		5CBE908F26D7FB62005A2E95 /* PrivateClickMeasurementDebugInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PrivateClickMeasurementDebugInfo.h; sourceTree = "<group>"; };
@@ -9260,6 +9261,7 @@
 		5C01BC3526D46AD400FEB42F /* PrivateClickMeasurement */ = {
 			isa = PBXGroup;
 			children = (
+				5CB930F126E7EE890032B1C0 /* cocoa */,
 				5CB930BF26E0542E0032B1C0 /* PrivateClickMeasurementClient.h */,
 				5CB930C026E0542F0032B1C0 /* PrivateClickMeasurementClientImpl.cpp */,
 				5CB930C126E054B80032B1C0 /* PrivateClickMeasurementClientImpl.h */,
@@ -9353,6 +9355,14 @@
 			path = Cocoa;
 			sourceTree = "<group>";
 		};
+		5CB930F126E7EE890032B1C0 /* cocoa */ = {
+			isa = PBXGroup;
+			children = (
+				5CB930F226E7EEE00032B1C0 /* PrivateClickMeasurementNetworkLoaderCocoa.mm */,
+			);
+			path = cocoa;
+			sourceTree = "<group>";
+		};
 		6BE969BF1E54D452008B7483 /* ResourceLoadStatistics */ = {
 			isa = PBXGroup;
 			children = (
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to