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