Rename requestCacheStorageSpace to requestStorageSpace
[WebKit-https.git] / Source / WebKit / UIProcess / Network / NetworkProcessProxy.h
1 /*
2  * Copyright (C) 2012-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 "APIWebsiteDataStore.h"
29 #include "AuxiliaryProcessProxy.h"
30 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
31 #include "LegacyCustomProtocolManagerProxy.h"
32 #endif
33 #include "NetworkProcessProxyMessages.h"
34 #include "ProcessLauncher.h"
35 #include "ProcessThrottler.h"
36 #include "ProcessThrottlerClient.h"
37 #include "UserContentControllerIdentifier.h"
38 #include "WebProcessProxyMessages.h"
39 #include <WebCore/RegistrableDomain.h>
40 #include <memory>
41 #include <wtf/Deque.h>
42
43 namespace PAL {
44 class SessionID;
45 }
46
47 namespace WebCore {
48 class AuthenticationChallenge;
49 class ProtectionSpace;
50 class ResourceRequest;
51 enum class ShouldSample : bool;
52 class SecurityOrigin;
53 struct SecurityOriginData;
54 }
55
56 namespace WebKit {
57
58 class DownloadProxy;
59 class DownloadProxyMap;
60 class WebProcessPool;
61 enum class ShouldGrandfatherStatistics : bool;
62 enum class StorageAccessStatus : uint8_t;
63 enum class WebsiteDataFetchOption;
64 enum class WebsiteDataType;
65 struct NetworkProcessCreationParameters;
66 class WebUserContentControllerProxy;
67 struct WebsiteData;
68
69 class NetworkProcessProxy final : public AuxiliaryProcessProxy, private ProcessThrottlerClient {
70 public:
71     using RegistrableDomain = WebCore::RegistrableDomain;
72     using TopFrameDomain = WebCore::RegistrableDomain;
73     using SubFrameDomain = WebCore::RegistrableDomain;
74     using SubResourceDomain = WebCore::RegistrableDomain;
75     using RedirectDomain = WebCore::RegistrableDomain;
76     using RedirectedFromDomain = WebCore::RegistrableDomain;
77     using RedirectedToDomain = WebCore::RegistrableDomain;
78     using NavigatedFromDomain = WebCore::RegistrableDomain;
79     using NavigatedToDomain = WebCore::RegistrableDomain;
80     using DomainInNeedOfStorageAccess = WebCore::RegistrableDomain;
81     using OpenerDomain = WebCore::RegistrableDomain;
82     using OpenerPageID = uint64_t;
83     using PageID = uint64_t;
84     using FrameID = uint64_t;
85
86     explicit NetworkProcessProxy(WebProcessPool&);
87     ~NetworkProcessProxy();
88
89     void getNetworkProcessConnection(WebProcessProxy&, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply&&);
90
91     DownloadProxy* createDownloadProxy(const WebCore::ResourceRequest&);
92
93     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, CompletionHandler<void(WebsiteData)>&&);
94     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WallTime modifiedSince, CompletionHandler<void()>&& completionHandler);
95     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebKit::WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, const Vector<String>& HSTSCacheHostNames, CompletionHandler<void()>&&);
96
97 #if ENABLE(RESOURCE_LOAD_STATISTICS)
98     void clearPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
99     void clearUserInteraction(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
100     void dumpResourceLoadStatistics(PAL::SessionID, CompletionHandler<void(String)>&&);
101     void updatePrevalentDomainsToBlockCookiesFor(PAL::SessionID, const Vector<RegistrableDomain>&, CompletionHandler<void()>&&);
102     void hasHadUserInteraction(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
103     void isGrandfathered(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
104     void isPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
105     void isRegisteredAsRedirectingTo(PAL::SessionID, const RedirectedFromDomain&, const RedirectedToDomain&, CompletionHandler<void(bool)>&&);
106     void isRegisteredAsSubFrameUnder(PAL::SessionID, const SubFrameDomain&, const TopFrameDomain&, CompletionHandler<void(bool)>&&);
107     void isRegisteredAsSubresourceUnder(PAL::SessionID, const SubResourceDomain&, const TopFrameDomain&, CompletionHandler<void(bool)>&&);
108     void isVeryPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
109     void logUserInteraction(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
110     void scheduleStatisticsAndDataRecordsProcessing(PAL::SessionID, CompletionHandler<void()>&&);
111     void setLastSeen(PAL::SessionID, const RegistrableDomain&, Seconds, CompletionHandler<void()>&&);
112     void setAgeCapForClientSideCookies(PAL::SessionID, Optional<Seconds>, CompletionHandler<void()>&&);
113     void setCacheMaxAgeCap(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
114     void setGrandfathered(PAL::SessionID, const RegistrableDomain&, bool isGrandfathered, CompletionHandler<void()>&&);
115     void setNotifyPagesWhenDataRecordsWereScanned(PAL::SessionID, bool, CompletionHandler<void()>&&);
116     void setNotifyPagesWhenTelemetryWasCaptured(PAL::SessionID, bool, CompletionHandler<void()>&&);
117     void setSubframeUnderTopFrameDomain(PAL::SessionID, const SubFrameDomain&, const TopFrameDomain&, CompletionHandler<void()>&&);
118     void setSubresourceUnderTopFrameDomain(PAL::SessionID, const SubResourceDomain&, const TopFrameDomain&, CompletionHandler<void()>&&);
119     void setSubresourceUniqueRedirectTo(PAL::SessionID, const SubResourceDomain&, const RedirectedToDomain&, CompletionHandler<void()>&&);
120     void setSubresourceUniqueRedirectFrom(PAL::SessionID, const SubResourceDomain&, const RedirectedFromDomain&, CompletionHandler<void()>&&);
121     void setTimeToLiveUserInteraction(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
122     void setTopFrameUniqueRedirectTo(PAL::SessionID, const TopFrameDomain&, const RedirectedToDomain&, CompletionHandler<void()>&&);
123     void setTopFrameUniqueRedirectFrom(PAL::SessionID, const TopFrameDomain&, const RedirectedFromDomain&, CompletionHandler<void()>&&);
124     void setPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
125     void setPrevalentResourceForDebugMode(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
126     void setVeryPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
127     void hasStorageAccessForFrame(PAL::SessionID, const RegistrableDomain&, const TopFrameDomain&, FrameID, PageID, CompletionHandler<void(bool)>&&);
128     void getAllStorageAccessEntries(PAL::SessionID, CompletionHandler<void(Vector<String> domains)>&&);
129     void grantStorageAccess(PAL::SessionID, const RegistrableDomain&, const TopFrameDomain&, Optional<FrameID>, PageID, bool userWasPrompted, CompletionHandler<void(bool)>&&);
130     void hasStorageAccess(PAL::SessionID, const RegistrableDomain&, const TopFrameDomain&, Optional<FrameID>, PageID, CompletionHandler<void(bool)>&&);
131     void requestStorageAccess(PAL::SessionID, const SubFrameDomain&, const TopFrameDomain&, Optional<FrameID>, PageID, bool promptEnabled, CompletionHandler<void(StorageAccessStatus)>&&);
132     void requestStorageAccessConfirm(PageID, FrameID, const SubFrameDomain&, const TopFrameDomain&, CompletionHandler<void(bool)>&&);
133     void resetParametersToDefaultValues(PAL::SessionID, CompletionHandler<void()>&&);
134     void removeAllStorageAccess(PAL::SessionID, CompletionHandler<void()>&&);
135     void scheduleClearInMemoryAndPersistent(PAL::SessionID, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
136     void scheduleClearInMemoryAndPersistent(PAL::SessionID, Optional<WallTime> modifiedSince, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
137     void scheduleCookieBlockingUpdate(PAL::SessionID, CompletionHandler<void()>&&);
138     void submitTelemetry(PAL::SessionID, CompletionHandler<void()>&&);
139     void setCacheMaxAgeCapForPrevalentResources(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
140     void setGrandfatheringTime(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
141     void setMaxStatisticsEntries(PAL::SessionID, size_t maximumEntryCount, CompletionHandler<void()>&&);
142     void setMinimumTimeBetweenDataRecordsRemoval(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
143     void setPruneEntriesDownTo(PAL::SessionID, size_t pruneTargetCount, CompletionHandler<void()>&&);
144     void setResourceLoadStatisticsDebugMode(PAL::SessionID, bool debugMode, CompletionHandler<void()>&&);
145     void setShouldClassifyResourcesBeforeDataRecordsRemoval(PAL::SessionID, bool, CompletionHandler<void()>&&);
146     void resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID, CompletionHandler<void()>&&);
147     void resetCrossSiteLoadsWithLinkDecorationForTesting(PAL::SessionID, CompletionHandler<void()>&&);
148 #endif
149
150     void processReadyToSuspend();
151     
152     void sendProcessDidTransitionToForeground();
153     void sendProcessDidTransitionToBackground();
154
155     void setIsHoldingLockedFiles(bool);
156     void setIsIDBDatabaseHoldingLockedFiles(bool);
157
158     void syncAllCookies();
159     void didSyncAllCookies();
160
161     ProcessThrottler& throttler() { return m_throttler; }
162     WebProcessPool& processPool() { return m_processPool; }
163
164 #if ENABLE(CONTENT_EXTENSIONS)
165     void didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy&);
166 #endif
167
168     void dumpAdClickAttribution(PAL::SessionID, CompletionHandler<void(const String&)>&&);
169     void clearAdClickAttribution(PAL::SessionID, CompletionHandler<void()>&&);
170
171     void addSession(Ref<WebsiteDataStore>&&);
172     void removeSession(PAL::SessionID);
173
174 private:
175     // AuxiliaryProcessProxy
176     void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
177     void connectionWillOpen(IPC::Connection&) override;
178     void processWillShutDown(IPC::Connection&) override;
179
180     void networkProcessCrashed();
181     void clearCallbackStates();
182
183     // ProcessThrottlerClient
184     void sendProcessWillSuspendImminently() override;
185     void sendPrepareToSuspend() override;
186     void sendCancelPrepareToSuspend() override;
187     void sendProcessDidResume() override;
188     void didSetAssertionState(AssertionState) override;
189
190     // IPC::Connection::Client
191     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
192     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
193     void didClose(IPC::Connection&) override;
194     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
195
196     // Message handlers
197     void didReceiveNetworkProcessProxyMessage(IPC::Connection&, IPC::Decoder&);
198     void didCreateNetworkConnectionToWebProcess(const IPC::Attachment&);
199     void didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, WebCore::AuthenticationChallenge&&, uint64_t challengeID);
200     void didFetchWebsiteData(uint64_t callbackID, const WebsiteData&);
201     void didDeleteWebsiteData(uint64_t callbackID);
202     void didDeleteWebsiteDataForOrigins(uint64_t callbackID);
203     void logDiagnosticMessage(uint64_t pageID, const String& message, const String& description, WebCore::ShouldSample);
204     void logDiagnosticMessageWithResult(uint64_t pageID, const String& message, const String& description, uint32_t result, WebCore::ShouldSample);
205     void logDiagnosticMessageWithValue(uint64_t pageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
206     void logGlobalDiagnosticMessageWithValue(const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
207 #if ENABLE(RESOURCE_LOAD_STATISTICS)
208     void logTestingEvent(PAL::SessionID, const String& event);
209     void notifyResourceLoadStatisticsProcessed();
210     void notifyWebsiteDataDeletionForRegistrableDomainsFinished();
211     void notifyWebsiteDataScanForRegistrableDomainsFinished();
212     void notifyResourceLoadStatisticsTelemetryFinished(unsigned totalPrevalentResources, unsigned totalPrevalentResourcesWithUserInteraction, unsigned top3SubframeUnderTopFrameOrigins);
213 #endif
214     void retrieveCacheStorageParameters(PAL::SessionID);
215
216 #if ENABLE(CONTENT_EXTENSIONS)
217     void contentExtensionRules(UserContentControllerIdentifier);
218 #endif
219
220 #if ENABLE(SANDBOX_EXTENSIONS)
221     void getSandboxExtensionsForBlobFiles(const Vector<String>& paths, Messages::NetworkProcessProxy::GetSandboxExtensionsForBlobFiles::AsyncReply&&);
222 #endif
223
224 #if ENABLE(SERVICE_WORKER)
225     void establishWorkerContextConnectionToNetworkProcess(WebCore::SecurityOriginData&&);
226     void establishWorkerContextConnectionToNetworkProcessForExplicitSession(WebCore::SecurityOriginData&&, PAL::SessionID);
227 #endif
228
229     void requestStorageSpace(PAL::SessionID, const WebCore::ClientOrigin&, uint64_t quota, uint64_t currentSize, uint64_t spaceRequired, CompletionHandler<void(Optional<uint64_t> quota)>&&);
230
231     WebsiteDataStore* websiteDataStoreFromSessionID(PAL::SessionID);
232
233     // ProcessLauncher::Client
234     void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
235
236     WebProcessPool& m_processPool;
237     
238     unsigned m_numPendingConnectionRequests;
239     Deque<std::pair<WeakPtr<WebProcessProxy>, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>> m_pendingConnectionReplies;
240
241     HashMap<uint64_t, CompletionHandler<void(WebsiteData)>> m_pendingFetchWebsiteDataCallbacks;
242     HashMap<uint64_t, CompletionHandler<void()>> m_pendingDeleteWebsiteDataCallbacks;
243     HashMap<uint64_t, CompletionHandler<void()>> m_pendingDeleteWebsiteDataForOriginsCallbacks;
244
245     std::unique_ptr<DownloadProxyMap> m_downloadProxyMap;
246 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
247     LegacyCustomProtocolManagerProxy m_customProtocolManagerProxy;
248 #endif
249     ProcessThrottler m_throttler;
250     ProcessThrottler::BackgroundActivityToken m_tokenForHoldingLockedFiles;
251     ProcessThrottler::BackgroundActivityToken m_tokenForIDBDatabaseHoldingLockedFiles;
252     ProcessThrottler::BackgroundActivityToken m_syncAllCookiesToken;
253     
254     unsigned m_syncAllCookiesCounter { 0 };
255
256 #if ENABLE(CONTENT_EXTENSIONS)
257     HashSet<WebUserContentControllerProxy*> m_webUserContentControllerProxies;
258 #endif
259
260     HashMap<PAL::SessionID, RefPtr<WebsiteDataStore>> m_websiteDataStores;
261 };
262
263 } // namespace WebKit