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