Adopt WebCore::RegistrableDomain in WebCore::ResourceLoadStatistics and WebKit::Netwo...
[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 "NetworkAdClickAttribution.h"
30 #include "NetworkProcess.h"
31 #include "NetworkProcessProxyMessages.h"
32 #include "WebProcessProxy.h"
33 #include "WebResourceLoadStatisticsStore.h"
34 #include <WebCore/AdClickAttribution.h>
35 #include <WebCore/NetworkStorageSession.h>
36
37 #if PLATFORM(COCOA)
38 #include "NetworkSessionCocoa.h"
39 #endif
40 #if USE(SOUP)
41 #include "NetworkSessionSoup.h"
42 #endif
43 #if USE(CURL)
44 #include "NetworkSessionCurl.h"
45 #endif
46
47 namespace WebKit {
48 using namespace WebCore;
49
50 Ref<NetworkSession> NetworkSession::create(NetworkProcess& networkProcess, NetworkSessionCreationParameters&& parameters)
51 {
52 #if PLATFORM(COCOA)
53     return NetworkSessionCocoa::create(networkProcess, WTFMove(parameters));
54 #endif
55 #if USE(SOUP)
56     return NetworkSessionSoup::create(networkProcess, WTFMove(parameters));
57 #endif
58 #if USE(CURL)
59     return NetworkSessionCurl::create(networkProcess, WTFMove(parameters));
60 #endif
61 }
62
63 NetworkStorageSession& NetworkSession::networkStorageSession() const
64 {
65     auto* storageSession = m_networkProcess->storageSession(m_sessionID);
66     RELEASE_ASSERT(storageSession);
67     return *storageSession;
68 }
69
70 NetworkSession::NetworkSession(NetworkProcess& networkProcess, PAL::SessionID sessionID)
71     : m_sessionID(sessionID)
72     , m_networkProcess(networkProcess)
73     , m_adClickAttribution(makeUniqueRef<NetworkAdClickAttribution>())
74 {
75 }
76
77 NetworkSession::~NetworkSession()
78 {
79 }
80
81 void NetworkSession::invalidateAndCancel()
82 {
83     for (auto* task : m_dataTaskSet)
84         task->invalidateAndCancel();
85 }
86
87 #if ENABLE(RESOURCE_LOAD_STATISTICS)
88 void NetworkSession::setResourceLoadStatisticsEnabled(bool enable)
89 {
90     if (!enable) {
91         m_resourceLoadStatistics = nullptr;
92         return;
93     }
94
95     if (m_resourceLoadStatistics)
96         return;
97
98     // FIXME(193728): Support ResourceLoadStatistics for ephemeral sessions, too.
99     if (m_sessionID.isEphemeral())
100         return;
101     
102     m_resourceLoadStatistics = WebResourceLoadStatisticsStore::create(*this, m_resourceLoadStatisticsDirectory);
103 }
104
105 void NetworkSession::notifyResourceLoadStatisticsProcessed()
106 {
107     m_networkProcess->parentProcessConnection()->send(Messages::NetworkProcessProxy::NotifyResourceLoadStatisticsProcessed(), 0);
108 }
109
110 void NetworkSession::logDiagnosticMessageWithValue(const String& message, const String& description, unsigned value, unsigned significantFigures, WebCore::ShouldSample shouldSample)
111 {
112     m_networkProcess->parentProcessConnection()->send(Messages::WebPageProxy::LogDiagnosticMessageWithValue(message, description, value, significantFigures, shouldSample), 0);
113 }
114
115 void NetworkSession::notifyPageStatisticsTelemetryFinished(unsigned totalPrevalentResources, unsigned totalPrevalentResourcesWithUserInteraction, unsigned top3SubframeUnderTopFrameOrigins)
116 {
117     m_networkProcess->parentProcessConnection()->send(Messages::NetworkProcessProxy::NotifyResourceLoadStatisticsTelemetryFinished(totalPrevalentResources, totalPrevalentResourcesWithUserInteraction, top3SubframeUnderTopFrameOrigins), 0);
118 }
119
120 void NetworkSession::deleteWebsiteDataForRegistrableDomainsInAllPersistentDataStores(OptionSet<WebsiteDataType> dataTypes, Vector<RegistrableDomain>&& domains, bool shouldNotifyPage, CompletionHandler<void(const HashSet<RegistrableDomain>&)>&& completionHandler)
121 {
122     m_networkProcess->deleteWebsiteDataForRegistrableDomainsInAllPersistentDataStores(m_sessionID, dataTypes, WTFMove(domains), shouldNotifyPage, WTFMove(completionHandler));
123 }
124
125 void NetworkSession::registrableDomainsWithWebsiteData(OptionSet<WebsiteDataType> dataTypes, bool shouldNotifyPage, CompletionHandler<void(HashSet<RegistrableDomain>&&)>&& completionHandler)
126 {
127     m_networkProcess->registrableDomainsWithWebsiteData(m_sessionID, dataTypes, shouldNotifyPage, WTFMove(completionHandler));
128 }
129 #endif
130
131 void NetworkSession::storeAdClickAttribution(WebCore::AdClickAttribution&& adClickAttribution)
132 {
133     m_adClickAttribution->store(WTFMove(adClickAttribution));
134 }
135
136 void NetworkSession::dumpAdClickAttribution(CompletionHandler<void(String)>&& completionHandler)
137 {
138     m_adClickAttribution->toString(WTFMove(completionHandler));
139 }
140
141 void NetworkSession::clearAdClickAttribution(CompletionHandler<void()>&& completionHandler)
142 {
143     m_adClickAttribution->clear(WTFMove(completionHandler));
144 }
145
146 } // namespace WebKit