[WK] Add missing ENABLE(NETWORK_CACHE) build guards
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkProcess.h
1 /*
2  * Copyright (C) 2012-2017 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 "CacheModel.h"
29 #include "ChildProcess.h"
30 #include "DownloadManager.h"
31 #include "MessageReceiverMap.h"
32 #include <WebCore/DiagnosticLoggingClient.h>
33 #include <memory>
34 #include <pal/SessionID.h>
35 #include <wtf/Forward.h>
36 #include <wtf/Function.h>
37 #include <wtf/MemoryPressureHandler.h>
38 #include <wtf/NeverDestroyed.h>
39 #include <wtf/RetainPtr.h>
40
41 #if PLATFORM(IOS)
42 #include "WebSQLiteDatabaseTracker.h"
43 #endif
44
45 namespace PAL {
46 class SessionID;
47 }
48
49 namespace WebCore {
50 class DownloadID;
51 class CertificateInfo;
52 class NetworkStorageSession;
53 class ProtectionSpace;
54 class SecurityOrigin;
55 struct SecurityOriginData;
56 struct SoupNetworkProxySettings;
57 }
58
59 namespace WebKit {
60 class AuthenticationManager;
61 class NetworkConnectionToWebProcess;
62 class NetworkProcessSupplement;
63 class NetworkResourceLoader;
64 enum class WebsiteDataFetchOption;
65 enum class WebsiteDataType;
66 struct NetworkProcessCreationParameters;
67 struct WebsiteDataStoreParameters;
68
69 namespace NetworkCache {
70 class Cache;
71 }
72
73 class NetworkProcess : public ChildProcess, private DownloadManager::Client {
74     WTF_MAKE_NONCOPYABLE(NetworkProcess);
75     friend class NeverDestroyed<NetworkProcess>;
76     friend class NeverDestroyed<DownloadManager>;
77 public:
78     static NetworkProcess& singleton();
79
80     template <typename T>
81     T* supplement()
82     {
83         return static_cast<T*>(m_supplements.get(T::supplementName()));
84     }
85
86     template <typename T>
87     void addSupplement()
88     {
89         m_supplements.add(T::supplementName(), std::make_unique<T>(this));
90     }
91
92     void removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess*);
93
94     AuthenticationManager& authenticationManager();
95     DownloadManager& downloadManager();
96
97 #if ENABLE(NETWORK_CACHE)
98     NetworkCache::Cache* cache() { return m_cache.get(); }
99 #endif
100
101     bool canHandleHTTPSServerTrustEvaluation() const { return m_canHandleHTTPSServerTrustEvaluation; }
102
103     void processWillSuspendImminently(bool& handled);
104     void prepareToSuspend();
105     void cancelPrepareToSuspend();
106     void processDidResume();
107
108     // Diagnostic messages logging.
109     void logDiagnosticMessage(uint64_t webPageID, const String& message, const String& description, WebCore::ShouldSample);
110     void logDiagnosticMessageWithResult(uint64_t webPageID, const String& message, const String& description, WebCore::DiagnosticLoggingResultType, WebCore::ShouldSample);
111     void logDiagnosticMessageWithValue(uint64_t webPageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
112
113 #if PLATFORM(COCOA)
114     RetainPtr<CFDataRef> sourceApplicationAuditData() const;
115     void clearHSTSCache(WebCore::NetworkStorageSession&, std::chrono::system_clock::time_point modifiedSince);
116 #endif
117
118 #if USE(NETWORK_SESSION)
119     void findPendingDownloadLocation(NetworkDataTask&, ResponseCompletionHandler&&, const WebCore::ResourceResponse&);
120 #endif
121
122 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
123     void canAuthenticateAgainstProtectionSpace(NetworkResourceLoader&, const WebCore::ProtectionSpace&);
124 #endif
125
126     void prefetchDNS(const String&);
127
128     void ensurePrivateBrowsingSession(WebsiteDataStoreParameters&&);
129
130     void grantSandboxExtensionsToStorageProcessForBlobs(const Vector<String>& filenames, Function<void ()>&& completionHandler);
131
132 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
133     void updateCookiePartitioningForTopPrivatelyOwnedDomains(const Vector<String>& domainsToRemove, const Vector<String>& domainsToAdd, bool shouldClearFirst);
134 #endif
135
136     Seconds loadThrottleLatency() const { return m_loadThrottleLatency; }
137
138 private:
139     NetworkProcess();
140     ~NetworkProcess();
141
142     void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
143
144     void terminate() override;
145     void platformTerminate();
146
147     void lowMemoryHandler(Critical);
148
149     enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
150     void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
151
152     // ChildProcess
153     void initializeProcess(const ChildProcessInitializationParameters&) override;
154     void initializeProcessName(const ChildProcessInitializationParameters&) override;
155     void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
156     void initializeConnection(IPC::Connection*) override;
157     bool shouldTerminate() override;
158
159     // IPC::Connection::Client
160     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
161     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
162     void didClose(IPC::Connection&) override;
163
164     // DownloadManager::Client
165     void didCreateDownload() override;
166     void didDestroyDownload() override;
167     IPC::Connection* downloadProxyConnection() override;
168     AuthenticationManager& downloadsAuthenticationManager() override;
169 #if USE(NETWORK_SESSION)
170     void pendingDownloadCanceled(DownloadID) override;
171 #endif
172
173     // Message Handlers
174     void didReceiveNetworkProcessMessage(IPC::Connection&, IPC::Decoder&);
175     void didReceiveSyncNetworkProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
176     void initializeNetworkProcess(NetworkProcessCreationParameters&&);
177     void createNetworkConnectionToWebProcess();
178     void addWebsiteDataStore(WebsiteDataStoreParameters&&);
179     void destroySession(PAL::SessionID);
180
181     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, uint64_t callbackID);
182     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID);
183     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, uint64_t callbackID);
184
185     void clearCachedCredentials();
186
187     // FIXME: This should take a session ID so we can identify which disk cache to delete.
188     void clearDiskCache(std::chrono::system_clock::time_point modifiedSince, Function<void ()>&& completionHandler);
189
190     void downloadRequest(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedFilename);
191     void resumeDownload(PAL::SessionID, DownloadID, const IPC::DataReference& resumeData, const String& path, const SandboxExtension::Handle&);
192     void cancelDownload(DownloadID);
193 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
194     void continueCanAuthenticateAgainstProtectionSpace(uint64_t resourceLoadIdentifier, bool canAuthenticate);
195 #endif
196 #if USE(NETWORK_SESSION)
197 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
198     void continueCanAuthenticateAgainstProtectionSpaceDownload(DownloadID, bool canAuthenticate);
199 #endif
200     void continueWillSendRequest(DownloadID, WebCore::ResourceRequest&&);
201 #endif
202     void continueDecidePendingDownloadDestination(DownloadID, String destination, const SandboxExtension::Handle& sandboxExtensionHandle, bool allowOverwrite);
203
204     void setCacheModel(uint32_t);
205     void allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo&, const String& host);
206     void setCanHandleHTTPSServerTrustEvaluation(bool);
207     void getNetworkProcessStatistics(uint64_t callbackID);
208     void clearCacheForAllOrigins(uint32_t cachesToClear);
209     void setAllowsAnySSLCertificateForWebSocket(bool);
210     void syncAllCookies();
211
212     void didGrantSandboxExtensionsToStorageProcessForBlobs(uint64_t requestID);
213
214 #if USE(SOUP)
215     void setIgnoreTLSErrors(bool);
216     void userPreferredLanguagesChanged(const Vector<String>&);
217     void setNetworkProxySettings(const WebCore::SoupNetworkProxySettings&);
218 #endif
219
220     // Platform Helpers
221     void platformSetURLCacheSize(unsigned urlCacheMemoryCapacity, uint64_t urlCacheDiskCapacity);
222
223     // Connections to WebProcesses.
224     Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
225
226     String m_diskCacheDirectory;
227     bool m_hasSetCacheModel;
228     CacheModel m_cacheModel;
229     int64_t m_diskCacheSizeOverride { -1 };
230     bool m_suppressMemoryPressureHandler { false };
231     bool m_diskCacheIsDisabledForTesting;
232     bool m_canHandleHTTPSServerTrustEvaluation;
233     Seconds m_loadThrottleLatency;
234
235 #if ENABLE(NETWORK_CACHE)
236     RefPtr<NetworkCache::Cache> m_cache;
237 #endif
238
239     typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
240     NetworkProcessSupplementMap m_supplements;
241
242     HashMap<uint64_t, Function<void ()>> m_sandboxExtensionForBlobsCompletionHandlers;
243     HashMap<uint64_t, Ref<NetworkResourceLoader>> m_waitingNetworkResourceLoaders;
244
245 #if PLATFORM(COCOA)
246     void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
247     void setCookieStoragePartitioningEnabled(bool);
248
249     // FIXME: We'd like to be able to do this without the #ifdef, but WorkQueue + BinarySemaphore isn't good enough since
250     // multiple requests to clear the cache can come in before previous requests complete, and we need to wait for all of them.
251     // In the future using WorkQueue and a counting semaphore would work, as would WorkQueue supporting the libdispatch concept of "work groups".
252     dispatch_group_t m_clearCacheDispatchGroup;
253 #endif
254
255 #if PLATFORM(IOS)
256     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
257 #endif
258 };
259
260 } // namespace WebKit