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