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#include <WebCore/ResourceRequest.h>
41
42#if PLATFORM(COCOA)
43#include "NetworkSessionCocoa.h"
44#endif
45#if USE(SOUP)
46#include "NetworkSessionSoup.h"
47#endif
48#if USE(CURL)
49#include "NetworkSessionCurl.h"
50#endif
51
52namespace WebKit {
53using namespace WebCore;
54
55Ref<NetworkSession> NetworkSession::create(NetworkProcess& networkProcess, NetworkSessionCreationParameters&& parameters)
56{
57#if PLATFORM(COCOA)
58 return NetworkSessionCocoa::create(networkProcess, WTFMove(parameters));
59#endif
60#if USE(SOUP)
61 return NetworkSessionSoup::create(networkProcess, WTFMove(parameters));
62#endif
63#if USE(CURL)
64 return NetworkSessionCurl::create(networkProcess, WTFMove(parameters));
65#endif
66}
67
68NetworkStorageSession& NetworkSession::networkStorageSession() const
69{
70 auto* storageSession = m_networkProcess->storageSession(m_sessionID);
71 RELEASE_ASSERT(storageSession);
72 return *storageSession;
73}
74
75NetworkSession::NetworkSession(NetworkProcess& networkProcess, PAL::SessionID sessionID)
76 : m_sessionID(sessionID)
77 , m_networkProcess(networkProcess)
78 , m_adClickAttribution(makeUniqueRef<AdClickAttributionManager>(sessionID))
79{
80 m_adClickAttribution->setPingLoadFunction([this, weakThis = makeWeakPtr(this)](NetworkResourceLoadParameters&& loadParameters, CompletionHandler<void(const WebCore::ResourceError&, const WebCore::ResourceResponse&)>&& completionHandler) {
81 if (!weakThis)
82 return;
83 // PingLoad manages its own lifetime, deleting itself when its purpose has been fulfilled.
84 new PingLoad(m_networkProcess, WTFMove(loadParameters), WTFMove(completionHandler));
85 });
86}
87
88NetworkSession::~NetworkSession()
89{
90}
91
92void NetworkSession::invalidateAndCancel()
93{
94 for (auto* task : m_dataTaskSet)
95 task->invalidateAndCancel();
96}
97
98#if ENABLE(RESOURCE_LOAD_STATISTICS)
99void NetworkSession::setResourceLoadStatisticsEnabled(bool enable)
100{
101 if (!enable) {
102 m_resourceLoadStatistics = nullptr;
103 return;
104 }
105
106 if (m_resourceLoadStatistics)
107 return;
108
109 // FIXME(193728): Support ResourceLoadStatistics for ephemeral sessions, too.
110 if (m_sessionID.isEphemeral())
111 return;
112
113 m_resourceLoadStatistics = WebResourceLoadStatisticsStore::create(*this, m_resourceLoadStatisticsDirectory, m_shouldIncludeLocalhostInResourceLoadStatistics);
114
115 if (m_enableResourceLoadStatisticsDebugMode == EnableResourceLoadStatisticsDebugMode::Yes)
116 m_resourceLoadStatistics->setResourceLoadStatisticsDebugMode(true, [] { });
117 // This should always be forwarded since debug mode may be enabled at runtime.
118 if (!m_resourceLoadStatisticsManualPrevalentResource.isEmpty())
119 m_resourceLoadStatistics->setPrevalentResourceForDebugMode(m_resourceLoadStatisticsManualPrevalentResource, [] { });
120}
121
122void NetworkSession::notifyResourceLoadStatisticsProcessed()
123{
124 m_networkProcess->parentProcessConnection()->send(Messages::NetworkProcessProxy::NotifyResourceLoadStatisticsProcessed(), 0);
125}
126
127void NetworkSession::logDiagnosticMessageWithValue(const String& message, const String& description, unsigned value, unsigned significantFigures, WebCore::ShouldSample shouldSample)
128{
129 m_networkProcess->parentProcessConnection()->send(Messages::WebPageProxy::LogDiagnosticMessageWithValue(message, description, value, significantFigures, shouldSample), 0);
130}
131
132void NetworkSession::notifyPageStatisticsTelemetryFinished(unsigned totalPrevalentResources, unsigned totalPrevalentResourcesWithUserInteraction, unsigned top3SubframeUnderTopFrameOrigins)
133{
134 m_networkProcess->parentProcessConnection()->send(Messages::NetworkProcessProxy::NotifyResourceLoadStatisticsTelemetryFinished(totalPrevalentResources, totalPrevalentResourcesWithUserInteraction, top3SubframeUnderTopFrameOrigins), 0);
135}
136
137void NetworkSession::deleteWebsiteDataForRegistrableDomains(OptionSet<WebsiteDataType> dataTypes, HashMap<RegistrableDomain, WebsiteDataToRemove>&& domains, bool shouldNotifyPage, CompletionHandler<void(const HashSet<RegistrableDomain>&)>&& completionHandler)
138{
139 m_networkProcess->deleteWebsiteDataForRegistrableDomains(m_sessionID, dataTypes, WTFMove(domains), shouldNotifyPage, WTFMove(completionHandler));
140}
141
142void NetworkSession::registrableDomainsWithWebsiteData(OptionSet<WebsiteDataType> dataTypes, bool shouldNotifyPage, CompletionHandler<void(HashSet<RegistrableDomain>&&)>&& completionHandler)
143{
144 m_networkProcess->registrableDomainsWithWebsiteData(m_sessionID, dataTypes, shouldNotifyPage, WTFMove(completionHandler));
145}
146#endif // ENABLE(RESOURCE_LOAD_STATISTICS)
147
148void NetworkSession::storeAdClickAttribution(WebCore::AdClickAttribution&& adClickAttribution)
149{
150 m_adClickAttribution->storeUnconverted(WTFMove(adClickAttribution));
151}
152
153void NetworkSession::handleAdClickAttributionConversion(AdClickAttribution::Conversion&& conversion, const URL& requestURL, const WebCore::ResourceRequest& redirectRequest)
154{
155 m_adClickAttribution->handleConversion(WTFMove(conversion), requestURL, redirectRequest);
156}
157
158void NetworkSession::dumpAdClickAttribution(CompletionHandler<void(String)>&& completionHandler)
159{
160 m_adClickAttribution->toString(WTFMove(completionHandler));
161}
162
163void NetworkSession::clearAdClickAttribution()
164{
165 m_adClickAttribution->clear();
166}
167
168void NetworkSession::clearAdClickAttributionForRegistrableDomain(WebCore::RegistrableDomain&& domain)
169{
170 m_adClickAttribution->clearForRegistrableDomain(WTFMove(domain));
171}
172
173void NetworkSession::setAdClickAttributionOverrideTimerForTesting(bool value)
174{
175 m_adClickAttribution->setOverrideTimerForTesting(value);
176}
177
178void NetworkSession::setAdClickAttributionConversionURLForTesting(URL&& url)
179{
180 m_adClickAttribution->setConversionURLForTesting(WTFMove(url));
181}
182
183void NetworkSession::markAdClickAttributionsAsExpiredForTesting()
184{
185 m_adClickAttribution->markAllUnconvertedAsExpiredForTesting();
186}
187
188void NetworkSession::addKeptAliveLoad(Ref<NetworkResourceLoader>&& loader)
189{
190 ASSERT(m_sessionID == loader->sessionID());
191 ASSERT(!m_keptAliveLoads.contains(loader));
192 m_keptAliveLoads.add(WTFMove(loader));
193}
194
195void NetworkSession::removeKeptAliveLoad(NetworkResourceLoader& loader)
196{
197 ASSERT(m_sessionID == loader.sessionID());
198 ASSERT(m_keptAliveLoads.contains(loader));
199 m_keptAliveLoads.remove(loader);
200}
201
202} // namespace WebKit
203