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#pragma once
27
28#include "PrefetchCache.h"
29#include "SandboxExtension.h"
30#include "WebResourceLoadStatisticsStore.h"
31#include <WebCore/AdClickAttribution.h>
32#include <WebCore/RegistrableDomain.h>
33#include <pal/SessionID.h>
34#include <wtf/HashSet.h>
35#include <wtf/Ref.h>
36#include <wtf/RefCounted.h>
37#include <wtf/Seconds.h>
38#include <wtf/UniqueRef.h>
39#include <wtf/WeakPtr.h>
40#include <wtf/text/WTFString.h>
41
42namespace WebCore {
43class NetworkStorageSession;
44class ResourceRequest;
45enum class IncludeHttpOnlyCookies : bool;
46enum class ShouldSample : bool;
47}
48
49namespace WebKit {
50
51class AdClickAttributionManager;
52class NetworkDataTask;
53class NetworkProcess;
54class NetworkResourceLoader;
55class StorageManager;
56class NetworkSocketChannel;
57class WebResourceLoadStatisticsStore;
58class WebSocketTask;
59struct NetworkSessionCreationParameters;
60
61enum class WebsiteDataType;
62
63class NetworkSession : public RefCounted<NetworkSession>, public CanMakeWeakPtr<NetworkSession> {
64public:
65 static Ref<NetworkSession> create(NetworkProcess&, NetworkSessionCreationParameters&&);
66 virtual ~NetworkSession();
67
68 virtual void invalidateAndCancel();
69 virtual void clearCredentials() { };
70 virtual bool shouldLogCookieInformation() const { return false; }
71 virtual Seconds loadThrottleLatency() const { return { }; }
72
73 PAL::SessionID sessionID() const { return m_sessionID; }
74 NetworkProcess& networkProcess() { return m_networkProcess; }
75 WebCore::NetworkStorageSession& networkStorageSession() const;
76
77 void registerNetworkDataTask(NetworkDataTask& task) { m_dataTaskSet.add(&task); }
78 void unregisterNetworkDataTask(NetworkDataTask& task) { m_dataTaskSet.remove(&task); }
79
80 StorageManager& storageManager() { return m_storageManager.get(); }
81
82#if ENABLE(RESOURCE_LOAD_STATISTICS)
83 WebResourceLoadStatisticsStore* resourceLoadStatistics() const { return m_resourceLoadStatistics.get(); }
84 void setResourceLoadStatisticsEnabled(bool);
85 void notifyResourceLoadStatisticsProcessed();
86 void deleteWebsiteDataForRegistrableDomains(OptionSet<WebsiteDataType>, HashMap<WebCore::RegistrableDomain, WebsiteDataToRemove>&&, bool shouldNotifyPage, CompletionHandler<void(const HashSet<WebCore::RegistrableDomain>&)>&&);
87 void registrableDomainsWithWebsiteData(OptionSet<WebsiteDataType>, bool shouldNotifyPage, CompletionHandler<void(HashSet<WebCore::RegistrableDomain>&&)>&&);
88 void logDiagnosticMessageWithValue(const String& message, const String& description, unsigned value, unsigned significantFigures, WebCore::ShouldSample);
89 void notifyPageStatisticsTelemetryFinished(unsigned totalPrevalentResources, unsigned totalPrevalentResourcesWithUserInteraction, unsigned top3SubframeUnderTopFrameOrigins);
90#endif
91 void storeAdClickAttribution(WebCore::AdClickAttribution&&);
92 void handleAdClickAttributionConversion(WebCore::AdClickAttribution::Conversion&&, const URL& requestURL, const WebCore::ResourceRequest& redirectRequest);
93 void dumpAdClickAttribution(CompletionHandler<void(String)>&&);
94 void clearAdClickAttribution();
95 void clearAdClickAttributionForRegistrableDomain(WebCore::RegistrableDomain&&);
96 void setAdClickAttributionOverrideTimerForTesting(bool value);
97 void setAdClickAttributionConversionURLForTesting(URL&&);
98 void markAdClickAttributionsAsExpiredForTesting();
99
100 void addKeptAliveLoad(Ref<NetworkResourceLoader>&&);
101 void removeKeptAliveLoad(NetworkResourceLoader&);
102
103 PrefetchCache& prefetchCache() { return m_prefetchCache; }
104 void clearPrefetchCache() { m_prefetchCache.clear(); }
105
106 virtual std::unique_ptr<WebSocketTask> createWebSocketTask(NetworkSocketChannel&, const WebCore::ResourceRequest&, const String& protocol);
107 virtual void removeWebSocketTask(WebSocketTask&) { }
108 virtual void addWebSocketTask(WebSocketTask&) { }
109
110protected:
111 NetworkSession(NetworkProcess&, PAL::SessionID, const String& localStorageDirectory, SandboxExtension::Handle&);
112
113 PAL::SessionID m_sessionID;
114 Ref<NetworkProcess> m_networkProcess;
115 HashSet<NetworkDataTask*> m_dataTaskSet;
116 String m_resourceLoadStatisticsDirectory;
117#if ENABLE(RESOURCE_LOAD_STATISTICS)
118 RefPtr<WebResourceLoadStatisticsStore> m_resourceLoadStatistics;
119 ShouldIncludeLocalhost m_shouldIncludeLocalhostInResourceLoadStatistics { ShouldIncludeLocalhost::Yes };
120 EnableResourceLoadStatisticsDebugMode m_enableResourceLoadStatisticsDebugMode { EnableResourceLoadStatisticsDebugMode::No };
121 WebCore::RegistrableDomain m_resourceLoadStatisticsManualPrevalentResource;
122#endif
123 UniqueRef<AdClickAttributionManager> m_adClickAttribution;
124
125 HashSet<Ref<NetworkResourceLoader>> m_keptAliveLoads;
126
127 PrefetchCache m_prefetchCache;
128
129 Ref<StorageManager> m_storageManager;
130#if !ASSERT_DISABLED
131 bool m_isInvalidated { false };
132#endif
133};
134
135} // namespace WebKit
136