Use normal loading path for ping loads
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkSession.cpp
1 /*
2  * Copyright (C) 2015-2019 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "NetworkSession.h"
28
29 #include "AdClickAttributionManager.h"
30 #include "NetworkProcess.h"
31 #include "NetworkProcessProxyMessages.h"
32 #include "NetworkResourceLoadParameters.h"
33 #include "NetworkResourceLoader.h"
34 #include "PingLoad.h"
35 #include "WebPageProxy.h"
36 #include "WebPageProxyMessages.h"
37 #include "WebProcessProxy.h"
38 #include <WebCore/CookieJar.h>
39 #include <WebCore/NetworkStorageSession.h>
40
41 #if PLATFORM(COCOA)
42 #include "NetworkSessionCocoa.h"
43 #endif
44 #if USE(SOUP)
45 #include "NetworkSessionSoup.h"
46 #endif
47 #if USE(CURL)
48 #include "NetworkSessionCurl.h"
49 #endif
50
51 namespace WebKit {
52 using namespace WebCore;
53
54 Ref<NetworkSession> NetworkSession::create(NetworkProcess& networkProcess, NetworkSessionCreationParameters&& parameters)
55 {
56 #if PLATFORM(COCOA)
57     return NetworkSessionCocoa::create(networkProcess, WTFMove(parameters));
58 #endif
59 #if USE(SOUP)
60     return NetworkSessionSoup::create(networkProcess, WTFMove(parameters));
61 #endif
62 #if USE(CURL)
63     return NetworkSessionCurl::create(networkProcess, WTFMove(parameters));
64 #endif
65 }
66
67 NetworkStorageSession& NetworkSession::networkStorageSession() const
68 {
69     auto* storageSession = m_networkProcess->storageSession(m_sessionID);
70     RELEASE_ASSERT(storageSession);
71     return *storageSession;
72 }
73
74 NetworkSession::NetworkSession(NetworkProcess& networkProcess, PAL::SessionID sessionID)
75     : m_sessionID(sessionID)
76     , m_networkProcess(networkProcess)
77     , m_adClickAttribution(makeUniqueRef<AdClickAttributionManager>())
78 {
79     m_adClickAttribution->setPingLoadFunction([this, weakThis = makeWeakPtr(this)](NetworkResourceLoadParameters&& loadParameters, CompletionHandler<void(const WebCore::ResourceError&, const WebCore::ResourceResponse&)>&& completionHandler) {
80         if (!weakThis)
81             return;
82         // PingLoad manages its own lifetime, deleting itself when its purpose has been fulfilled.
83         new PingLoad(m_networkProcess, WTFMove(loadParameters), WTFMove(completionHandler));
84     });
85 }
86
87 NetworkSession::~NetworkSession()
88 {
89 }
90
91 void NetworkSession::invalidateAndCancel()
92 {
93     for (auto* task : m_dataTaskSet)
94         task->invalidateAndCancel();
95 }
96
97 #if ENABLE(RESOURCE_LOAD_STATISTICS)
98 void NetworkSession::setResourceLoadStatisticsEnabled(bool enable)
99 {
100     if (!enable) {
101         m_resourceLoadStatistics = nullptr;
102         return;
103     }
104
105     if (m_resourceLoadStatistics)
106         return;
107
108     // FIXME(193728): Support ResourceLoadStatistics for ephemeral sessions, too.
109     if (m_sessionID.isEphemeral())
110         return;
111
112     m_resourceLoadStatistics = WebResourceLoadStatisticsStore::create(*this, m_resourceLoadStatisticsDirectory, m_shouldIncludeLocalhostInResourceLoadStatistics);
113
114     if (m_enableResourceLoadStatisticsDebugMode == EnableResourceLoadStatisticsDebugMode::Yes)
115         m_resourceLoadStatistics->setResourceLoadStatisticsDebugMode(true, [] { });
116     // This should always be forwarded since debug mode may be enabled at runtime.
117     if (!m_resourceLoadStatisticsManualPrevalentResource.isEmpty())
118         m_resourceLoadStatistics->setPrevalentResourceForDebugMode(m_resourceLoadStatisticsManualPrevalentResource, [] { });
119 }
120
121 void NetworkSession::notifyResourceLoadStatisticsProcessed()
122 {
123     m_networkProcess->parentProcessConnection()->send(Messages::NetworkProcessProxy::NotifyResourceLoadStatisticsProcessed(), 0);
124 }
125
126 void NetworkSession::logDiagnosticMessageWithValue(const String& message, const String& description, unsigned value, unsigned significantFigures, WebCore::ShouldSample shouldSample)
127 {
128     m_networkProcess->parentProcessConnection()->send(Messages::WebPageProxy::LogDiagnosticMessageWithValue(message, description, value, significantFigures, shouldSample), 0);
129 }
130
131 void NetworkSession::notifyPageStatisticsTelemetryFinished(unsigned totalPrevalentResources, unsigned totalPrevalentResourcesWithUserInteraction, unsigned top3SubframeUnderTopFrameOrigins)
132 {
133     m_networkProcess->parentProcessConnection()->send(Messages::NetworkProcessProxy::NotifyResourceLoadStatisticsTelemetryFinished(totalPrevalentResources, totalPrevalentResourcesWithUserInteraction, top3SubframeUnderTopFrameOrigins), 0);
134 }
135
136 void NetworkSession::deleteWebsiteDataForRegistrableDomains(OptionSet<WebsiteDataType> dataTypes, HashMap<RegistrableDomain, WebsiteDataToRemove>&& domains, bool shouldNotifyPage, CompletionHandler<void(const HashSet<RegistrableDomain>&)>&& completionHandler)
137 {
138     m_networkProcess->deleteWebsiteDataForRegistrableDomains(m_sessionID, dataTypes, WTFMove(domains), shouldNotifyPage, WTFMove(completionHandler));
139 }
140
141 void NetworkSession::registrableDomainsWithWebsiteData(OptionSet<WebsiteDataType> dataTypes, bool shouldNotifyPage, CompletionHandler<void(HashSet<RegistrableDomain>&&)>&& completionHandler)
142 {
143     m_networkProcess->registrableDomainsWithWebsiteData(m_sessionID, dataTypes, shouldNotifyPage, WTFMove(completionHandler));
144 }
145 #endif // ENABLE(RESOURCE_LOAD_STATISTICS)
146
147 void NetworkSession::storeAdClickAttribution(WebCore::AdClickAttribution&& adClickAttribution)
148 {
149     m_adClickAttribution->storeUnconverted(WTFMove(adClickAttribution));
150 }
151
152 void NetworkSession::convertAdClickAttribution(const WebCore::AdClickAttribution::Source& source, const WebCore::AdClickAttribution::Destination& destination, WebCore::AdClickAttribution::Conversion&& conversion)
153 {
154     m_adClickAttribution->convert(source, destination, WTFMove(conversion));
155 }
156
157 void NetworkSession::dumpAdClickAttribution(CompletionHandler<void(String)>&& completionHandler)
158 {
159     m_adClickAttribution->toString(WTFMove(completionHandler));
160 }
161
162 void NetworkSession::clearAdClickAttribution()
163 {
164     m_adClickAttribution->clear();
165 }
166
167 void NetworkSession::clearAdClickAttributionForRegistrableDomain(WebCore::RegistrableDomain&& domain)
168 {
169     m_adClickAttribution->clearForRegistrableDomain(WTFMove(domain));
170 }
171
172 void NetworkSession::setAdClickAttributionOverrideTimerForTesting(bool value)
173 {
174     m_adClickAttribution->setOverrideTimerForTesting(value);
175 }
176
177 void NetworkSession::setAdClickAttributionConversionURLForTesting(URL&& url)
178 {
179     m_adClickAttribution->setConversionURLForTesting(WTFMove(url));
180 }
181
182 void NetworkSession::markAdClickAttributionsAsExpiredForTesting()
183 {
184     m_adClickAttribution->markAllUnconvertedAsExpiredForTesting();
185 }
186
187 void NetworkSession::addKeptAliveLoad(Ref<NetworkResourceLoader>&& loader)
188 {
189     ASSERT(m_sessionID == loader->sessionID());
190     ASSERT(!m_keptAliveLoads.contains(loader));
191     m_keptAliveLoads.add(WTFMove(loader));
192 }
193
194 void NetworkSession::removeKeptAliveLoad(NetworkResourceLoader& loader)
195 {
196     ASSERT(m_sessionID == loader.sessionID());
197     ASSERT(m_keptAliveLoads.contains(loader));
198     m_keptAliveLoads.remove(loader);
199 }
200
201 } // namespace WebKit