Resource Load Statistics: Immediately forward cookie access at user interaction when...
[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 <WebCore/DiagnosticLoggingClient.h>
33 #include <memory>
34 #include <pal/SessionID.h>
35 #include <wtf/Forward.h>
36 #include <wtf/Function.h>
37 #include <wtf/HashSet.h>
38 #include <wtf/MemoryPressureHandler.h>
39 #include <wtf/NeverDestroyed.h>
40 #include <wtf/RetainPtr.h>
41 #include <wtf/WeakPtr.h>
42
43 #if PLATFORM(IOS)
44 #include "WebSQLiteDatabaseTracker.h"
45 #endif
46
47 namespace PAL {
48 class SessionID;
49 }
50
51 namespace WebCore {
52 class DownloadID;
53 class CertificateInfo;
54 class NetworkStorageSession;
55 class ProtectionSpace;
56 class SecurityOrigin;
57 struct SecurityOriginData;
58 struct SoupNetworkProxySettings;
59 enum class StoredCredentialsPolicy;
60 class URL;
61 }
62
63 namespace WebKit {
64 class AuthenticationManager;
65 #if ENABLE(SERVER_PRECONNECT)
66 class PreconnectTask;
67 #endif
68 class NetworkConnectionToWebProcess;
69 class NetworkProcessSupplement;
70 class NetworkResourceLoader;
71 enum class WebsiteDataFetchOption;
72 enum class WebsiteDataType;
73 struct NetworkProcessCreationParameters;
74 struct WebsiteDataStoreParameters;
75
76 namespace NetworkCache {
77 class Cache;
78 }
79
80 class NetworkProcess : public ChildProcess, private DownloadManager::Client {
81     WTF_MAKE_NONCOPYABLE(NetworkProcess);
82     friend NeverDestroyed<NetworkProcess>;
83     friend NeverDestroyed<DownloadManager>;
84 public:
85     static NetworkProcess& singleton();
86
87     template <typename T>
88     T* supplement()
89     {
90         return static_cast<T*>(m_supplements.get(T::supplementName()));
91     }
92
93     template <typename T>
94     void addSupplement()
95     {
96         m_supplements.add(T::supplementName(), std::make_unique<T>(*this));
97     }
98
99     void removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess*);
100
101     AuthenticationManager& authenticationManager();
102     DownloadManager& downloadManager();
103
104     NetworkCache::Cache* cache() { return m_cache.get(); }
105
106     bool canHandleHTTPSServerTrustEvaluation() const { return m_canHandleHTTPSServerTrustEvaluation; }
107
108     void processWillSuspendImminently(bool& handled);
109     void prepareToSuspend();
110     void cancelPrepareToSuspend();
111     void processDidResume();
112
113     // Diagnostic messages logging.
114     void logDiagnosticMessage(uint64_t webPageID, const String& message, const String& description, WebCore::ShouldSample);
115     void logDiagnosticMessageWithResult(uint64_t webPageID, const String& message, const String& description, WebCore::DiagnosticLoggingResultType, WebCore::ShouldSample);
116     void logDiagnosticMessageWithValue(uint64_t webPageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
117
118 #if PLATFORM(COCOA)
119     RetainPtr<CFDataRef> sourceApplicationAuditData() const;
120     void clearHSTSCache(WebCore::NetworkStorageSession&, WallTime modifiedSince);
121 #endif
122
123     void findPendingDownloadLocation(NetworkDataTask&, ResponseCompletionHandler&&, const WebCore::ResourceResponse&);
124
125 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
126     void canAuthenticateAgainstProtectionSpace(NetworkResourceLoader&, const WebCore::ProtectionSpace&);
127 #if ENABLE(SERVER_PRECONNECT)
128     void canAuthenticateAgainstProtectionSpace(PreconnectTask&, const WebCore::ProtectionSpace&);
129 #endif
130 #endif
131
132     void prefetchDNS(const String&);
133
134     void addWebsiteDataStore(WebsiteDataStoreParameters&&);
135
136     void grantSandboxExtensionsToStorageProcessForBlobs(const Vector<String>& filenames, Function<void ()>&& completionHandler);
137
138 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
139     void updatePrevalentDomainsToPartitionOrBlockCookies(PAL::SessionID, const Vector<String>& domainsToPartition, const Vector<String>& domainsToBlock, const Vector<String>& domainsToNeitherPartitionNorBlock, bool shouldClearFirst);
140     void hasStorageAccessForFrame(PAL::SessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, uint64_t contextId);
141     void getAllStorageAccessEntries(PAL::SessionID, uint64_t contextId);
142     void grantStorageAccess(PAL::SessionID, const String& resourceDomain, const String& firstPartyDomain, std::optional<uint64_t> frameID, uint64_t pageID, uint64_t contextId);
143     void removePrevalentDomains(PAL::SessionID, const Vector<String>& domains);
144 #endif
145
146     Seconds loadThrottleLatency() const { return m_loadThrottleLatency; }
147     String cacheStorageDirectory(PAL::SessionID) const;
148     uint64_t cacheStoragePerOriginQuota() const;
149
150     void preconnectTo(const WebCore::URL&, WebCore::StoredCredentialsPolicy);
151
152 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
153     bool shouldLogCookieInformation() const { return m_logCookieInformation; }
154 #endif
155
156     void setSessionIsControlledByAutomation(PAL::SessionID, bool);
157     bool sessionIsControlledByAutomation(PAL::SessionID) const;
158
159 private:
160     NetworkProcess();
161     ~NetworkProcess();
162
163     void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
164
165     void terminate() override;
166     void platformTerminate();
167
168     void lowMemoryHandler(Critical);
169
170     enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
171     void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
172
173     // ChildProcess
174     void initializeProcess(const ChildProcessInitializationParameters&) override;
175     void initializeProcessName(const ChildProcessInitializationParameters&) override;
176     void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
177     void initializeConnection(IPC::Connection*) override;
178     bool shouldTerminate() override;
179
180     // IPC::Connection::Client
181     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
182     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
183     void didClose(IPC::Connection&) override;
184
185     // DownloadManager::Client
186     void didCreateDownload() override;
187     void didDestroyDownload() override;
188     IPC::Connection* downloadProxyConnection() override;
189     AuthenticationManager& downloadsAuthenticationManager() override;
190     void pendingDownloadCanceled(DownloadID) override;
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     void continueWillSendRequest(DownloadID, WebCore::ResourceRequest&&);
215     void continueDecidePendingDownloadDestination(DownloadID, String destination, SandboxExtension::Handle&&, bool allowOverwrite);
216
217     void setCacheModel(uint32_t);
218     void allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo&, const String& host);
219     void setCanHandleHTTPSServerTrustEvaluation(bool);
220     void getNetworkProcessStatistics(uint64_t callbackID);
221     void clearCacheForAllOrigins(uint32_t cachesToClear);
222     void setAllowsAnySSLCertificateForWebSocket(bool);
223     void syncAllCookies();
224
225     void didGrantSandboxExtensionsToStorageProcessForBlobs(uint64_t requestID);
226
227     void writeBlobToFilePath(const WebCore::URL&, const String& path, SandboxExtension::Handle&&, 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 #if PLATFORM(MAC)
239     static void setSharedHTTPCookieStorage(const Vector<uint8_t>& identifier);
240 #endif
241
242     // Connections to WebProcesses.
243     Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
244
245     String m_cacheStorageDirectory;
246     uint64_t m_cacheStoragePerOriginQuota { 0 };
247     String m_diskCacheDirectory;
248     bool m_hasSetCacheModel;
249     CacheModel m_cacheModel;
250     int64_t m_diskCacheSizeOverride { -1 };
251     bool m_suppressMemoryPressureHandler { false };
252     bool m_diskCacheIsDisabledForTesting;
253     bool m_canHandleHTTPSServerTrustEvaluation;
254     Seconds m_loadThrottleLatency;
255 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
256     bool m_logCookieInformation { false };
257 #endif
258
259     RefPtr<NetworkCache::Cache> m_cache;
260
261     typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
262     NetworkProcessSupplementMap m_supplements;
263
264     HashMap<uint64_t, Function<void ()>> m_sandboxExtensionForBlobsCompletionHandlers;
265     HashMap<uint64_t, Ref<NetworkResourceLoader>> m_waitingNetworkResourceLoaders;
266 #if ENABLE(SERVER_PRECONNECT)
267     HashMap<uint64_t, WeakPtr<PreconnectTask>> m_waitingPreconnectTasks;
268 #endif
269     HashSet<PAL::SessionID> m_sessionsControlledByAutomation;
270
271 #if PLATFORM(COCOA)
272     void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
273     void setCookieStoragePartitioningEnabled(bool);
274     void setStorageAccessAPIEnabled(bool);
275
276     // FIXME: We'd like to be able to do this without the #ifdef, but WorkQueue + BinarySemaphore isn't good enough since
277     // multiple requests to clear the cache can come in before previous requests complete, and we need to wait for all of them.
278     // In the future using WorkQueue and a counting semaphore would work, as would WorkQueue supporting the libdispatch concept of "work groups".
279     dispatch_group_t m_clearCacheDispatchGroup;
280 #endif
281
282 #if PLATFORM(IOS)
283     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
284 #endif
285 };
286
287 } // namespace WebKit