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