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