Move NetworkCache ownership from NetworkProcess to NetworkSession
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkProcess.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 "AuxiliaryProcess.h"
29 #include "CacheModel.h"
30 #include "DownloadManager.h"
31 #include "LocalStorageDatabaseTracker.h"
32 #include "NetworkBlobRegistry.h"
33 #include "NetworkContentRuleListManager.h"
34 #include "NetworkHTTPSUpgradeChecker.h"
35 #include "SandboxExtension.h"
36 #include "WebResourceLoadStatisticsStore.h"
37 #include "WebsiteData.h"
38 #include <WebCore/AdClickAttribution.h>
39 #include <WebCore/ClientOrigin.h>
40 #include <WebCore/CrossSiteNavigationDataTransfer.h>
41 #include <WebCore/DiagnosticLoggingClient.h>
42 #include <WebCore/FetchIdentifier.h>
43 #include <WebCore/IDBKeyData.h>
44 #include <WebCore/IDBServer.h>
45 #include <WebCore/PageIdentifier.h>
46 #include <WebCore/RegistrableDomain.h>
47 #include <WebCore/ServiceWorkerIdentifier.h>
48 #include <WebCore/ServiceWorkerTypes.h>
49 #include <memory>
50 #include <wtf/CrossThreadTask.h>
51 #include <wtf/Function.h>
52 #include <wtf/HashSet.h>
53 #include <wtf/MemoryPressureHandler.h>
54 #include <wtf/NeverDestroyed.h>
55 #include <wtf/RetainPtr.h>
56 #include <wtf/WeakPtr.h>
57
58 #if PLATFORM(IOS_FAMILY)
59 #include "WebSQLiteDatabaseTracker.h"
60 #endif
61
62 #if PLATFORM(COCOA)
63 typedef struct OpaqueCFHTTPCookieStorage*  CFHTTPCookieStorageRef;
64 #endif
65
66 namespace IPC {
67 class FormDataReference;
68 }
69
70 namespace PAL {
71 class SessionID;
72 }
73
74 namespace WebCore {
75 class CertificateInfo;
76 class CurlProxySettings;
77 class DownloadID;
78 class ProtectionSpace;
79 class StorageQuotaManager;
80 class NetworkStorageSession;
81 class ResourceError;
82 class SWServer;
83 enum class IncludeHttpOnlyCookies : bool;
84 enum class StoredCredentialsPolicy : uint8_t;
85 enum class StorageAccessPromptWasShown : bool;
86 enum class StorageAccessWasGranted : bool;
87 struct ClientOrigin;
88 struct MessageWithMessagePorts;
89 struct SecurityOriginData;
90 struct SoupNetworkProxySettings;
91 struct ServiceWorkerClientIdentifier;
92 }
93
94 namespace WebKit {
95
96 class AuthenticationManager;
97 class NetworkConnectionToWebProcess;
98 class NetworkProcessSupplement;
99 class NetworkProximityManager;
100 class NetworkResourceLoader;
101 class WebSWServerConnection;
102 class WebSWServerToContextConnection;
103 enum class ShouldGrandfatherStatistics : bool;
104 enum class StorageAccessStatus : uint8_t;
105 enum class WebsiteDataFetchOption;
106 enum class WebsiteDataType;
107 struct NetworkProcessCreationParameters;
108 struct WebsiteDataStoreParameters;
109
110 #if ENABLE(SERVICE_WORKER)
111 class WebSWOriginStore;
112 #endif
113
114 namespace NetworkCache {
115 enum class CacheOption : uint8_t;
116 }
117
118 namespace CacheStorage {
119 class Engine;
120 }
121
122 class NetworkProcess : public AuxiliaryProcess, private DownloadManager::Client, public ThreadSafeRefCounted<NetworkProcess>
123 #if ENABLE(INDEXED_DATABASE)
124     , public WebCore::IDBServer::IDBBackingStoreTemporaryFileHandler
125 #endif
126     , public CanMakeWeakPtr<NetworkProcess>
127 {
128     WTF_MAKE_NONCOPYABLE(NetworkProcess);
129 public:
130     using RegistrableDomain = WebCore::RegistrableDomain;
131     using TopFrameDomain = WebCore::RegistrableDomain;
132     using SubFrameDomain = WebCore::RegistrableDomain;
133     using SubResourceDomain = WebCore::RegistrableDomain;
134     using RedirectDomain = WebCore::RegistrableDomain;
135     using RedirectedFromDomain = WebCore::RegistrableDomain;
136     using RedirectedToDomain = WebCore::RegistrableDomain;
137     using NavigatedFromDomain = WebCore::RegistrableDomain;
138     using NavigatedToDomain = WebCore::RegistrableDomain;
139     using DomainInNeedOfStorageAccess = WebCore::RegistrableDomain;
140     using OpenerDomain = WebCore::RegistrableDomain;
141     using FrameID = uint64_t;
142
143     NetworkProcess(AuxiliaryProcessInitializationParameters&&);
144     ~NetworkProcess();
145     static constexpr ProcessType processType = ProcessType::Network;
146
147     template <typename T>
148     T* supplement()
149     {
150         return static_cast<T*>(m_supplements.get(T::supplementName()));
151     }
152
153     template <typename T>
154     void addSupplement()
155     {
156         m_supplements.add(T::supplementName(), std::make_unique<T>(*this));
157     }
158
159     void removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess&);
160
161     AuthenticationManager& authenticationManager();
162     DownloadManager& downloadManager();
163
164     void setSession(const PAL::SessionID&, Ref<NetworkSession>&&);
165     NetworkSession* networkSession(const PAL::SessionID&) const final;
166     NetworkSession* networkSessionByConnection(IPC::Connection&) const;
167     void destroySession(const PAL::SessionID&);
168
169     // Needed for test infrastructure
170     HashMap<PAL::SessionID, Ref<NetworkSession>>& networkSessions() { return m_networkSessions; }
171
172     void forEachNetworkStorageSession(const Function<void(WebCore::NetworkStorageSession&)>&);
173     WebCore::NetworkStorageSession* storageSession(const PAL::SessionID&) const;
174     WebCore::NetworkStorageSession& defaultStorageSession() const;
175     void switchToNewTestingSession();
176 #if PLATFORM(COCOA)
177     void ensureSession(const PAL::SessionID&, const String& identifier, RetainPtr<CFHTTPCookieStorageRef>&&);
178 #else
179     void ensureSession(const PAL::SessionID&, const String& identifier);
180 #endif
181
182     bool canHandleHTTPSServerTrustEvaluation() const { return m_canHandleHTTPSServerTrustEvaluation; }
183
184     void processWillSuspendImminently();
185     void prepareToSuspend();
186     void cancelPrepareToSuspend();
187     void processDidResume();
188     void resume();
189
190     // Diagnostic messages logging.
191     void logDiagnosticMessage(WebCore::PageIdentifier, const String& message, const String& description, WebCore::ShouldSample);
192     void logDiagnosticMessageWithResult(WebCore::PageIdentifier, const String& message, const String& description, WebCore::DiagnosticLoggingResultType, WebCore::ShouldSample);
193     void logDiagnosticMessageWithValue(WebCore::PageIdentifier, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
194
195 #if PLATFORM(COCOA)
196     RetainPtr<CFDataRef> sourceApplicationAuditData() const;
197     void getHostNamesWithHSTSCache(WebCore::NetworkStorageSession&, HashSet<String>&);
198     void deleteHSTSCacheForHostNames(WebCore::NetworkStorageSession&, const Vector<String>&);
199     void clearHSTSCache(WebCore::NetworkStorageSession&, WallTime modifiedSince);
200     bool suppressesConnectionTerminationOnSystemChange() const { return m_suppressesConnectionTerminationOnSystemChange; }
201 #endif
202
203     void findPendingDownloadLocation(NetworkDataTask&, ResponseCompletionHandler&&, const WebCore::ResourceResponse&);
204
205     void prefetchDNS(const String&);
206
207     void addWebsiteDataStore(WebsiteDataStoreParameters&&);
208
209 #if ENABLE(RESOURCE_LOAD_STATISTICS)
210     void clearPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
211     void clearUserInteraction(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
212     void deleteWebsiteDataForRegistrableDomains(PAL::SessionID, OptionSet<WebsiteDataType>, HashMap<RegistrableDomain, WebsiteDataToRemove>&&, bool shouldNotifyPage, CompletionHandler<void(const HashSet<RegistrableDomain>&)>&&);
213     void deleteCookiesForTesting(PAL::SessionID, RegistrableDomain, bool includeHttpOnlyCookies, CompletionHandler<void()>&&);
214     void dumpResourceLoadStatistics(PAL::SessionID, CompletionHandler<void(String)>&&);
215     void updatePrevalentDomainsToBlockCookiesFor(PAL::SessionID, const Vector<RegistrableDomain>& domainsToBlock, CompletionHandler<void()>&&);
216     void isGrandfathered(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
217     void isPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
218     void isVeryPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
219     void setAgeCapForClientSideCookies(PAL::SessionID, Optional<Seconds>, CompletionHandler<void()>&&);
220     void isRegisteredAsRedirectingTo(PAL::SessionID, const RedirectedFromDomain&, const RedirectedToDomain&, CompletionHandler<void(bool)>&&);
221     void isRegisteredAsSubFrameUnder(PAL::SessionID, const SubFrameDomain&, const TopFrameDomain&, CompletionHandler<void(bool)>&&);
222     void isRegisteredAsSubresourceUnder(PAL::SessionID, const SubResourceDomain&, const TopFrameDomain&, CompletionHandler<void(bool)>&&);
223     void setGrandfathered(PAL::SessionID, const RegistrableDomain&, bool isGrandfathered, CompletionHandler<void()>&&);
224     void setMaxStatisticsEntries(PAL::SessionID, uint64_t maximumEntryCount, CompletionHandler<void()>&&);
225     void setPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
226     void setPrevalentResourceForDebugMode(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
227     void setVeryPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
228     void setPruneEntriesDownTo(PAL::SessionID, uint64_t pruneTargetCount, CompletionHandler<void()>&&);
229     void hadUserInteraction(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
230     void hasLocalStorage(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
231     void getAllStorageAccessEntries(PAL::SessionID, CompletionHandler<void(Vector<String> domains)>&&);
232     void logFrameNavigation(PAL::SessionID, const NavigatedToDomain&, const TopFrameDomain&, const NavigatedFromDomain&, bool isRedirect, bool isMainFrame);
233     void logUserInteraction(PAL::SessionID, const TopFrameDomain&, CompletionHandler<void()>&&);
234     void removePrevalentDomains(PAL::SessionID, const Vector<RegistrableDomain>&);
235     void resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID, CompletionHandler<void()>&&);
236     void resetParametersToDefaultValues(PAL::SessionID, CompletionHandler<void()>&&);
237     void scheduleClearInMemoryAndPersistent(PAL::SessionID, Optional<WallTime> modifiedSince, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
238     void scheduleCookieBlockingUpdate(PAL::SessionID, CompletionHandler<void()>&&);
239     void scheduleStatisticsAndDataRecordsProcessing(PAL::SessionID, CompletionHandler<void()>&&);
240     void submitTelemetry(PAL::SessionID, CompletionHandler<void()>&&);
241     void setCacheMaxAgeCapForPrevalentResources(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
242     void setGrandfatheringTime(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
243     void setLastSeen(PAL::SessionID, const RegistrableDomain&, Seconds, CompletionHandler<void()>&&);
244     void setMinimumTimeBetweenDataRecordsRemoval(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
245     void setNotifyPagesWhenDataRecordsWereScanned(PAL::SessionID, bool value, CompletionHandler<void()>&&);
246     void setIsRunningResourceLoadStatisticsTest(PAL::SessionID, bool value, CompletionHandler<void()>&&);
247     void setNotifyPagesWhenTelemetryWasCaptured(PAL::SessionID, bool value, CompletionHandler<void()>&&);
248     void setResourceLoadStatisticsEnabled(bool);
249     void setResourceLoadStatisticsDebugMode(PAL::SessionID, bool debugMode, CompletionHandler<void()>&&d);
250     void setShouldClassifyResourcesBeforeDataRecordsRemoval(PAL::SessionID, bool value, CompletionHandler<void()>&&);
251     void setSubframeUnderTopFrameDomain(PAL::SessionID, const SubFrameDomain&, const TopFrameDomain&, CompletionHandler<void()>&&);
252     void setSubresourceUnderTopFrameDomain(PAL::SessionID, const SubResourceDomain&, const TopFrameDomain&, CompletionHandler<void()>&&);
253     void setSubresourceUniqueRedirectTo(PAL::SessionID, const SubResourceDomain&, const RedirectedToDomain&, CompletionHandler<void()>&&);
254     void setSubresourceUniqueRedirectFrom(PAL::SessionID, const SubResourceDomain&, const RedirectedFromDomain&, CompletionHandler<void()>&&);
255     void setTimeToLiveUserInteraction(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
256     void setTopFrameUniqueRedirectTo(PAL::SessionID, const TopFrameDomain&, const RedirectedToDomain&, CompletionHandler<void()>&&);
257     void setTopFrameUniqueRedirectFrom(PAL::SessionID, const TopFrameDomain&, const RedirectedFromDomain&, CompletionHandler<void()>&&);
258     void registrableDomainsWithWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, bool shouldNotifyPage, CompletionHandler<void(HashSet<RegistrableDomain>&&)>&&);
259     void didCommitCrossSiteLoadWithDataTransfer(PAL::SessionID, const RegistrableDomain& fromDomain, const RegistrableDomain& toDomain, OptionSet<WebCore::CrossSiteNavigationDataTransfer::Flag>, WebCore::PageIdentifier);
260     void setCrossSiteLoadWithLinkDecorationForTesting(PAL::SessionID, const RegistrableDomain& fromDomain, const RegistrableDomain& toDomain, CompletionHandler<void()>&&);
261     void resetCrossSiteLoadsWithLinkDecorationForTesting(PAL::SessionID, CompletionHandler<void()>&&);
262 #endif
263
264     using CacheStorageRootPathCallback = CompletionHandler<void(String&&)>;
265     void cacheStorageRootPath(PAL::SessionID, CacheStorageRootPathCallback&&);
266
267     void preconnectTo(const URL&, WebCore::StoredCredentialsPolicy);
268
269     void setSessionIsControlledByAutomation(PAL::SessionID, bool);
270     bool sessionIsControlledByAutomation(PAL::SessionID) const;
271
272     void webPageWasAdded(IPC::Connection&, PAL::SessionID, WebCore::PageIdentifier, WebCore::PageIdentifier);
273     void webPageWasRemoved(IPC::Connection&, PAL::SessionID, WebCore::PageIdentifier);
274     void webProcessWasDisconnected(IPC::Connection&);
275     void webProcessSessionChanged(IPC::Connection&, PAL::SessionID, const Vector<WebCore::PageIdentifier>&);
276     void getLocalStorageOriginDetails(PAL::SessionID, CompletionHandler<void(Vector<LocalStorageDatabaseTracker::OriginDetails>&&)>&&);
277
278 #if ENABLE(CONTENT_EXTENSIONS)
279     NetworkContentRuleListManager& networkContentRuleListManager() { return m_networkContentRuleListManager; }
280 #endif
281
282 #if ENABLE(INDEXED_DATABASE)
283     WebCore::IDBServer::IDBServer& idbServer(PAL::SessionID);
284     // WebCore::IDBServer::IDBBackingStoreFileHandler.
285     void accessToTemporaryFileComplete(const String& path) final;
286     void setIDBPerOriginQuota(uint64_t);
287 #endif
288     void updateQuotaBasedOnSpaceUsageForTesting(PAL::SessionID, const WebCore::ClientOrigin&);
289
290 #if ENABLE(SANDBOX_EXTENSIONS)
291     void getSandboxExtensionsForBlobFiles(const Vector<String>& filenames, CompletionHandler<void(SandboxExtension::HandleArray&&)>&&);
292 #endif
293
294     void didReceiveNetworkProcessMessage(IPC::Connection&, IPC::Decoder&);
295
296 #if ENABLE(SERVICE_WORKER)
297     WebSWServerToContextConnection* serverToContextConnectionForRegistrableDomain(const WebCore::RegistrableDomain&);
298     void createServerToContextConnection(const WebCore::RegistrableDomain&, Optional<PAL::SessionID>);
299     
300     WebCore::SWServer& swServerForSession(PAL::SessionID);
301     void registerSWServerConnection(WebSWServerConnection&);
302     void unregisterSWServerConnection(WebSWServerConnection&);
303     
304     void swContextConnectionMayNoLongerBeNeeded(WebSWServerToContextConnection&);
305     
306     WebSWServerToContextConnection* connectionToContextProcessFromIPCConnection(IPC::Connection&);
307     void connectionToContextProcessWasClosed(Ref<WebSWServerToContextConnection>&&);
308 #endif
309
310 #if PLATFORM(IOS_FAMILY)
311     bool parentProcessHasServiceWorkerEntitlement() const;
312 #else
313     bool parentProcessHasServiceWorkerEntitlement() const { return true; }
314 #endif
315
316 #if PLATFORM(COCOA)
317     NetworkHTTPSUpgradeChecker& networkHTTPSUpgradeChecker();
318 #endif
319
320     const String& uiProcessBundleIdentifier() const { return m_uiProcessBundleIdentifier; }
321
322     void ref() const override { ThreadSafeRefCounted<NetworkProcess>::ref(); }
323     void deref() const override { ThreadSafeRefCounted<NetworkProcess>::deref(); }
324
325     CacheStorage::Engine* findCacheEngine(const PAL::SessionID&);
326     CacheStorage::Engine& ensureCacheEngine(const PAL::SessionID&, Function<Ref<CacheStorage::Engine>()>&&);
327     void removeCacheEngine(const PAL::SessionID&);
328     void requestStorageSpace(PAL::SessionID, const WebCore::ClientOrigin&, uint64_t quota, uint64_t currentSize, uint64_t spaceRequired, CompletionHandler<void(Optional<uint64_t>)>&&);
329
330     NetworkBlobRegistry& networkBlobRegistry() override { return m_networkBlobRegistry; }
331
332     void storeAdClickAttribution(PAL::SessionID, WebCore::AdClickAttribution&&);
333     void dumpAdClickAttribution(PAL::SessionID, CompletionHandler<void(String)>&&);
334     void clearAdClickAttribution(PAL::SessionID, CompletionHandler<void()>&&);
335     void setAdClickAttributionOverrideTimerForTesting(PAL::SessionID, bool value, CompletionHandler<void()>&&);
336     void setAdClickAttributionConversionURLForTesting(PAL::SessionID, URL&&, CompletionHandler<void()>&&);
337     void markAdClickAttributionsAsExpiredForTesting(PAL::SessionID, CompletionHandler<void()>&&);
338
339     WebCore::StorageQuotaManager& storageQuotaManager(PAL::SessionID, const WebCore::ClientOrigin&);
340
341     void addKeptAliveLoad(Ref<NetworkResourceLoader>&&);
342     void removeKeptAliveLoad(NetworkResourceLoader&);
343
344     const String& diskCacheDirectory() const { return m_diskCacheDirectory; }
345     const OptionSet<NetworkCache::CacheOption>& cacheOptions() const { return m_cacheOptions; }
346     
347 private:
348     void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
349     std::unique_ptr<WebCore::NetworkStorageSession> platformCreateDefaultStorageSession() const;
350
351     void terminate() override;
352     void platformTerminate();
353
354     void lowMemoryHandler(Critical);
355     
356     void processDidTransitionToForeground();
357     void processDidTransitionToBackground();
358     void platformProcessDidTransitionToForeground();
359     void platformProcessDidTransitionToBackground();
360
361     enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
362     void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
363     void platformPrepareToSuspend(CompletionHandler<void()>&&);
364     void platformProcessDidResume();
365
366     // AuxiliaryProcess
367     void initializeProcess(const AuxiliaryProcessInitializationParameters&) override;
368     void initializeProcessName(const AuxiliaryProcessInitializationParameters&) override;
369     void initializeSandbox(const AuxiliaryProcessInitializationParameters&, SandboxInitializationParameters&) override;
370     void initializeConnection(IPC::Connection*) override;
371     bool shouldTerminate() override;
372
373     // IPC::Connection::Client
374     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
375     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
376     void didClose(IPC::Connection&) override;
377
378     // DownloadManager::Client
379     void didCreateDownload() override;
380     void didDestroyDownload() override;
381     IPC::Connection* downloadProxyConnection() override;
382     IPC::Connection* parentProcessConnectionForDownloads() override { return parentProcessConnection(); }
383     AuthenticationManager& downloadsAuthenticationManager() override;
384     void pendingDownloadCanceled(DownloadID) override;
385     uint32_t downloadMonitorSpeedMultiplier() const override { return m_downloadMonitorSpeedMultiplier; }
386
387     // Message Handlers
388     void didReceiveSyncNetworkProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
389     void initializeNetworkProcess(NetworkProcessCreationParameters&&);
390     void createNetworkConnectionToWebProcess(bool isServiceWorkerProcess, WebCore::RegistrableDomain&&);
391
392     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, uint64_t callbackID);
393     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WallTime modifiedSince, uint64_t callbackID);
394     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, const Vector<String>& HSTSCacheHostnames, uint64_t callbackID);
395
396     void clearCachedCredentials();
397     void clearPermanentCredentialsForProtectionSpace(const WebCore::ProtectionSpace&, CompletionHandler<void()>&&);
398
399     void setCacheStorageParameters(PAL::SessionID, String&& cacheStorageDirectory, SandboxExtension::Handle&&);
400     void initializeQuotaUsers(WebCore::StorageQuotaManager&, PAL::SessionID, const WebCore::ClientOrigin&);
401
402     // FIXME: This should take a session ID so we can identify which disk cache to delete.
403     void clearDiskCache(WallTime modifiedSince, CompletionHandler<void()>&&);
404
405     void downloadRequest(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedFilename);
406     void resumeDownload(PAL::SessionID, DownloadID, const IPC::DataReference& resumeData, const String& path, SandboxExtension::Handle&&);
407     void cancelDownload(DownloadID);
408 #if PLATFORM(COCOA)
409     void publishDownloadProgress(DownloadID, const URL&, SandboxExtension::Handle&&);
410 #endif
411     void continueWillSendRequest(DownloadID, WebCore::ResourceRequest&&);
412     void continueDecidePendingDownloadDestination(DownloadID, String destination, SandboxExtension::Handle&&, bool allowOverwrite);
413     void applicationDidEnterBackground();
414     void applicationWillEnterForeground();
415
416     void setCacheModel(CacheModel);
417     void allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo&, const String& host);
418     void setCanHandleHTTPSServerTrustEvaluation(bool);
419     void getNetworkProcessStatistics(uint64_t callbackID);
420     void clearCacheForAllOrigins(uint32_t cachesToClear);
421     void setAllowsAnySSLCertificateForWebSocket(bool, CompletionHandler<void()>&&);
422     
423     void syncAllCookies();
424     void didSyncAllCookies();
425
426 #if USE(SOUP)
427     void setIgnoreTLSErrors(bool);
428     void userPreferredLanguagesChanged(const Vector<String>&);
429     void setNetworkProxySettings(const WebCore::SoupNetworkProxySettings&);
430 #endif
431
432 #if USE(CURL)
433     void setNetworkProxySettings(PAL::SessionID, WebCore::CurlProxySettings&&);
434 #endif
435
436 #if PLATFORM(MAC)
437     static void setSharedHTTPCookieStorage(const Vector<uint8_t>& identifier);
438 #endif
439
440     void platformSyncAllCookies(CompletionHandler<void()>&&);
441     
442     void registerURLSchemeAsSecure(const String&) const;
443     void registerURLSchemeAsBypassingContentSecurityPolicy(const String&) const;
444     void registerURLSchemeAsLocal(const String&) const;
445     void registerURLSchemeAsNoAccess(const String&) const;
446     void registerURLSchemeAsDisplayIsolated(const String&) const;
447     void registerURLSchemeAsCORSEnabled(const String&) const;
448     void registerURLSchemeAsCanDisplayOnlyIfCanRequest(const String&) const;
449
450 #if ENABLE(INDEXED_DATABASE)
451     void addIndexedDatabaseSession(PAL::SessionID, String&, SandboxExtension::Handle&);
452     void collectIndexedDatabaseOriginsForVersion(const String&, HashSet<WebCore::SecurityOriginData>&);
453     HashSet<WebCore::SecurityOriginData> indexedDatabaseOrigins(const String& path);
454     Ref<WebCore::IDBServer::IDBServer> createIDBServer(PAL::SessionID);
455 #endif
456
457 #if ENABLE(SERVICE_WORKER)
458     void didCreateWorkerContextProcessConnection(const IPC::Attachment&);
459     
460     void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin);
461     void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destination, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source, WebCore::SWServerConnectionIdentifier);
462     
463     void disableServiceWorkerProcessTerminationDelay();
464     
465     WebSWOriginStore* existingSWOriginStoreForSession(PAL::SessionID) const;
466     bool needsServerToContextConnectionForRegistrableDomain(const WebCore::RegistrableDomain&) const;
467
468     void addServiceWorkerSession(PAL::SessionID, String& serviceWorkerRegistrationDirectory, const SandboxExtension::Handle&);
469 #endif
470
471     void postStorageTask(CrossThreadTask&&);
472     // For execution on work queue thread only.
473     void performNextStorageTask();
474     void ensurePathExists(const String& path);
475
476     void clearStorageQuota(PAL::SessionID);
477     void initializeStorageQuota(const WebsiteDataStoreParameters&);
478
479     // Connections to WebProcesses.
480     Vector<Ref<NetworkConnectionToWebProcess>> m_webProcessConnections;
481
482     String m_diskCacheDirectory;
483     bool m_hasSetCacheModel { false };
484     CacheModel m_cacheModel { CacheModel::DocumentViewer };
485     bool m_suppressMemoryPressureHandler { false };
486     bool m_diskCacheIsDisabledForTesting { false };
487     bool m_canHandleHTTPSServerTrustEvaluation { true };
488     String m_uiProcessBundleIdentifier;
489     DownloadManager m_downloadManager;
490
491     HashMap<PAL::SessionID, Ref<CacheStorage::Engine>> m_cacheEngines;
492
493     typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
494     NetworkProcessSupplementMap m_supplements;
495
496     HashSet<PAL::SessionID> m_sessionsControlledByAutomation;
497     HashMap<PAL::SessionID, Vector<CacheStorageRootPathCallback>> m_cacheStorageParametersCallbacks;
498
499     HashMap<PAL::SessionID, Ref<NetworkSession>> m_networkSessions;
500     HashMap<PAL::SessionID, std::unique_ptr<WebCore::NetworkStorageSession>> m_networkStorageSessions;
501     mutable std::unique_ptr<WebCore::NetworkStorageSession> m_defaultNetworkStorageSession;
502     NetworkBlobRegistry m_networkBlobRegistry;
503
504 #if PLATFORM(COCOA)
505     void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
506     void setStorageAccessAPIEnabled(bool);
507
508     // FIXME: We'd like to be able to do this without the #ifdef, but WorkQueue + BinarySemaphore isn't good enough since
509     // multiple requests to clear the cache can come in before previous requests complete, and we need to wait for all of them.
510     // In the future using WorkQueue and a counting semaphore would work, as would WorkQueue supporting the libdispatch concept of "work groups".
511     dispatch_group_t m_clearCacheDispatchGroup { nullptr };
512
513     bool m_suppressesConnectionTerminationOnSystemChange { false };
514 #endif
515
516 #if ENABLE(CONTENT_EXTENSIONS)
517     NetworkContentRuleListManager m_networkContentRuleListManager;
518 #endif
519
520 #if PLATFORM(IOS_FAMILY)
521     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
522 #endif
523
524     Ref<WorkQueue> m_storageTaskQueue { WorkQueue::create("com.apple.WebKit.StorageTask") };
525
526 #if ENABLE(INDEXED_DATABASE)
527     HashMap<PAL::SessionID, String> m_idbDatabasePaths;
528     HashMap<PAL::SessionID, RefPtr<WebCore::IDBServer::IDBServer>> m_idbServers;
529     uint64_t m_idbPerOriginQuota { WebCore::IDBServer::defaultPerOriginQuota };
530 #endif
531
532     Deque<CrossThreadTask> m_storageTasks;
533     Lock m_storageTaskMutex;
534     
535 #if ENABLE(SERVICE_WORKER)
536     HashMap<WebCore::RegistrableDomain, RefPtr<WebSWServerToContextConnection>> m_serverToContextConnections;
537     bool m_waitingForServerToContextProcessConnection { false };
538     bool m_shouldDisableServiceWorkerProcessTerminationDelay { false };
539     HashMap<PAL::SessionID, String> m_swDatabasePaths;
540     HashMap<PAL::SessionID, std::unique_ptr<WebCore::SWServer>> m_swServers;
541     HashMap<WebCore::SWServerConnectionIdentifier, WebSWServerConnection*> m_swServerConnections;
542 #endif
543
544 #if PLATFORM(COCOA)
545     std::unique_ptr<NetworkHTTPSUpgradeChecker> m_networkHTTPSUpgradeChecker;
546 #endif
547
548     class StorageQuotaManagers {
549     public:
550         uint64_t defaultQuota(const WebCore::ClientOrigin& origin) const { return origin.topOrigin == origin.clientOrigin ? m_defaultQuota : m_defaultThirdPartyQuota; }
551         void setDefaultQuotas(uint64_t defaultQuota, uint64_t defaultThirdPartyQuota)
552         {
553             m_defaultQuota = defaultQuota;
554             m_defaultThirdPartyQuota = defaultThirdPartyQuota;
555         }
556
557         HashMap<WebCore::ClientOrigin, std::unique_ptr<WebCore::StorageQuotaManager>>& managersPerOrigin() { return m_managersPerOrigin; }
558
559     private:
560         uint64_t m_defaultQuota { WebCore::StorageQuotaManager::defaultQuota() };
561         uint64_t m_defaultThirdPartyQuota { WebCore::StorageQuotaManager::defaultThirdPartyQuota() };
562         HashMap<WebCore::ClientOrigin, std::unique_ptr<WebCore::StorageQuotaManager>> m_managersPerOrigin;
563     };
564     HashMap<PAL::SessionID, StorageQuotaManagers> m_storageQuotaManagers;
565     uint32_t m_downloadMonitorSpeedMultiplier { 1 };
566
567     HashMap<IPC::Connection::UniqueID, PAL::SessionID> m_sessionByConnection;
568
569     OptionSet<NetworkCache::CacheOption> m_cacheOptions;
570 };
571
572 } // namespace WebKit