Create a WebResourceLoadStatisticsStore attached to the NetworkSession
[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 "NetworkContentRuleListManager.h"
32 #include "NetworkHTTPSUpgradeChecker.h"
33 #include "SandboxExtension.h"
34 #include <WebCore/DiagnosticLoggingClient.h>
35 #include <WebCore/FetchIdentifier.h>
36 #include <WebCore/IDBKeyData.h>
37 #include <WebCore/IDBServer.h>
38 #include <WebCore/ServiceWorkerIdentifier.h>
39 #include <WebCore/ServiceWorkerTypes.h>
40 #include <memory>
41 #include <wtf/CrossThreadTask.h>
42 #include <wtf/Function.h>
43 #include <wtf/HashSet.h>
44 #include <wtf/MemoryPressureHandler.h>
45 #include <wtf/NeverDestroyed.h>
46 #include <wtf/RetainPtr.h>
47
48 namespace IPC {
49 class FormDataReference;
50 }
51
52 namespace PAL {
53 class SessionID;
54 }
55
56 namespace WebCore {
57 class CertificateInfo;
58 class CurlProxySettings;
59 class DownloadID;
60 class NetworkStorageSession;
61 class ResourceError;
62 class SWServer;
63 enum class StoredCredentialsPolicy : bool;
64 struct MessageWithMessagePorts;
65 struct SecurityOriginData;
66 struct SoupNetworkProxySettings;
67 struct ServiceWorkerClientIdentifier;
68 }
69
70 namespace WebKit {
71
72 class AuthenticationManager;
73 class NetworkConnectionToWebProcess;
74 class NetworkProcessSupplement;
75 class NetworkProximityManager;
76 class WebSWServerConnection;
77 class WebSWServerToContextConnection;
78 enum class WebsiteDataFetchOption;
79 enum class WebsiteDataType;
80 struct NetworkProcessCreationParameters;
81 struct WebsiteDataStoreParameters;
82
83 #if ENABLE(SERVICE_WORKER)
84 class WebSWOriginStore;
85 #endif
86
87 namespace CacheStorage {
88 class Engine;
89 }
90
91 namespace NetworkCache {
92 class Cache;
93 }
94
95 class NetworkProcess : public ChildProcess, private DownloadManager::Client, public ThreadSafeRefCounted<NetworkProcess>
96 #if ENABLE(INDEXED_DATABASE)
97     , public WebCore::IDBServer::IDBBackingStoreTemporaryFileHandler
98 #endif
99 {
100     WTF_MAKE_NONCOPYABLE(NetworkProcess);
101 public:
102     ~NetworkProcess();
103     static NetworkProcess& singleton();
104     static constexpr ProcessType processType = ProcessType::Network;
105
106     template <typename T>
107     T* supplement()
108     {
109         return static_cast<T*>(m_supplements.get(T::supplementName()));
110     }
111
112     template <typename T>
113     void addSupplement()
114     {
115         m_supplements.add(T::supplementName(), std::make_unique<T>(*this));
116     }
117
118     void removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess*);
119
120     AuthenticationManager& authenticationManager();
121     DownloadManager& downloadManager();
122 #if ENABLE(PROXIMITY_NETWORKING)
123     NetworkProximityManager& proximityManager();
124 #endif
125
126     NetworkCache::Cache* cache() { return m_cache.get(); }
127
128     void setSession(const PAL::SessionID&, Ref<NetworkSession>&&);
129     NetworkSession* networkSession(const PAL::SessionID&) const override;
130     void destroySession(const PAL::SessionID&);
131     
132     bool canHandleHTTPSServerTrustEvaluation() const { return m_canHandleHTTPSServerTrustEvaluation; }
133
134     void processWillSuspendImminently(bool& handled);
135     void prepareToSuspend();
136     void cancelPrepareToSuspend();
137     void processDidResume();
138
139     // Diagnostic messages logging.
140     void logDiagnosticMessage(uint64_t webPageID, const String& message, const String& description, WebCore::ShouldSample);
141     void logDiagnosticMessageWithResult(uint64_t webPageID, const String& message, const String& description, WebCore::DiagnosticLoggingResultType, WebCore::ShouldSample);
142     void logDiagnosticMessageWithValue(uint64_t webPageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
143
144 #if PLATFORM(COCOA)
145     RetainPtr<CFDataRef> sourceApplicationAuditData() const;
146     void getHostNamesWithHSTSCache(WebCore::NetworkStorageSession&, HashSet<String>&);
147     void deleteHSTSCacheForHostNames(WebCore::NetworkStorageSession&, const Vector<String>&);
148     void clearHSTSCache(WebCore::NetworkStorageSession&, WallTime modifiedSince);
149     bool suppressesConnectionTerminationOnSystemChange() const { return m_suppressesConnectionTerminationOnSystemChange; }
150 #endif
151
152     void findPendingDownloadLocation(NetworkDataTask&, ResponseCompletionHandler&&, const WebCore::ResourceResponse&);
153
154     void prefetchDNS(const String&);
155
156     void addWebsiteDataStore(WebsiteDataStoreParameters&&);
157
158 #if ENABLE(RESOURCE_LOAD_STATISTICS)
159     void updatePrevalentDomainsToBlockCookiesFor(PAL::SessionID, const Vector<String>& domainsToBlock, uint64_t contextId);
160     void setAgeCapForClientSideCookies(PAL::SessionID, Optional<Seconds>, uint64_t contextId);
161     void hasStorageAccessForFrame(PAL::SessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, uint64_t contextId);
162     void getAllStorageAccessEntries(PAL::SessionID, uint64_t contextId);
163     void grantStorageAccess(PAL::SessionID, const String& resourceDomain, const String& firstPartyDomain, Optional<uint64_t> frameID, uint64_t pageID, uint64_t contextId);
164     void logFrameNavigation(PAL::SessionID, const String& targetPrimaryDomain, const String& mainFramePrimaryDomain, const String& sourcePrimaryDomain, const String& targetHost, const String& mainFrameHost, bool isRedirect, bool isMainFrame);
165     void logUserInteraction(PAL::SessionID, const String& targetPrimaryDomain, uint64_t contextId);
166     void removeAllStorageAccess(PAL::SessionID, uint64_t contextId);
167     void removePrevalentDomains(PAL::SessionID, const Vector<String>& domains);
168     void setCacheMaxAgeCapForPrevalentResources(PAL::SessionID, Seconds, uint64_t contextId);
169     void resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID, uint64_t contextId);
170 #endif
171
172     using CacheStorageParametersCallback = CompletionHandler<void(const String&, uint64_t quota)>;
173     void cacheStorageParameters(PAL::SessionID, CacheStorageParametersCallback&&);
174
175     void preconnectTo(const URL&, WebCore::StoredCredentialsPolicy);
176
177     void setSessionIsControlledByAutomation(PAL::SessionID, bool);
178     bool sessionIsControlledByAutomation(PAL::SessionID) const;
179
180 #if ENABLE(CONTENT_EXTENSIONS)
181     NetworkContentRuleListManager& networkContentRuleListManager() { return m_networkContentRuleListManager; }
182 #endif
183
184 #if ENABLE(INDEXED_DATABASE)
185     WebCore::IDBServer::IDBServer& idbServer(PAL::SessionID);
186     // WebCore::IDBServer::IDBBackingStoreFileHandler.
187     void accessToTemporaryFileComplete(const String& path) final;
188     void setIDBPerOriginQuota(uint64_t);
189 #endif
190
191 #if ENABLE(SANDBOX_EXTENSIONS)
192     void getSandboxExtensionsForBlobFiles(const Vector<String>& filenames, CompletionHandler<void(SandboxExtension::HandleArray&&)>&&);
193 #endif
194
195     void didReceiveNetworkProcessMessage(IPC::Connection&, IPC::Decoder&);
196
197 #if ENABLE(SERVICE_WORKER)
198     WebSWServerToContextConnection* serverToContextConnectionForOrigin(const WebCore::SecurityOriginData&);
199     void createServerToContextConnection(const WebCore::SecurityOriginData&, Optional<PAL::SessionID>);
200     
201     WebCore::SWServer& swServerForSession(PAL::SessionID);
202     void registerSWServerConnection(WebSWServerConnection&);
203     void unregisterSWServerConnection(WebSWServerConnection&);
204     
205     void swContextConnectionMayNoLongerBeNeeded(WebSWServerToContextConnection&);
206     
207     WebSWServerToContextConnection* connectionToContextProcessFromIPCConnection(IPC::Connection&);
208     void connectionToContextProcessWasClosed(Ref<WebSWServerToContextConnection>&&);
209 #endif
210
211 #if PLATFORM(IOS_FAMILY)
212     bool parentProcessHasServiceWorkerEntitlement() const;
213 #else
214     bool parentProcessHasServiceWorkerEntitlement() const { return true; }
215 #endif
216
217 #if PLATFORM(COCOA)
218     NetworkHTTPSUpgradeChecker& networkHTTPSUpgradeChecker() { return m_networkHTTPSUpgradeChecker; }
219 #endif
220
221     const String& uiProcessBundleIdentifier() const { return m_uiProcessBundleIdentifier; }
222
223     void ref() const override { ThreadSafeRefCounted<NetworkProcess>::ref(); }
224     void deref() const override { ThreadSafeRefCounted<NetworkProcess>::deref(); }
225     
226     CacheStorage::Engine* findCacheEngine(const PAL::SessionID&);
227     CacheStorage::Engine& ensureCacheEngine(const PAL::SessionID&, Function<Ref<CacheStorage::Engine>()>&&);
228     void removeCacheEngine(const PAL::SessionID&);
229     
230 private:
231     NetworkProcess();
232
233     void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
234
235     void terminate() override;
236     void platformTerminate();
237
238     void lowMemoryHandler(Critical);
239     
240     void processDidTransitionToForeground();
241     void processDidTransitionToBackground();
242     void platformProcessDidTransitionToForeground();
243     void platformProcessDidTransitionToBackground();
244
245     enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
246     void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
247     void platformPrepareToSuspend(CompletionHandler<void()>&&);
248     void platformProcessDidResume();
249
250     // ChildProcess
251     void initializeProcess(const ChildProcessInitializationParameters&) override;
252     void initializeProcessName(const ChildProcessInitializationParameters&) override;
253     void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
254     void initializeConnection(IPC::Connection*) override;
255     bool shouldTerminate() override;
256
257     // IPC::Connection::Client
258     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
259     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
260     void didClose(IPC::Connection&) override;
261
262     // DownloadManager::Client
263     void didCreateDownload() override;
264     void didDestroyDownload() override;
265     IPC::Connection* downloadProxyConnection() override;
266     IPC::Connection* parentProcessConnectionForDownloads() override { return parentProcessConnection(); }
267     AuthenticationManager& downloadsAuthenticationManager() override;
268     void pendingDownloadCanceled(DownloadID) override;
269
270     // Message Handlers
271     void didReceiveSyncNetworkProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
272     void initializeNetworkProcess(NetworkProcessCreationParameters&&);
273     void createNetworkConnectionToWebProcess(bool isServiceWorkerProcess, WebCore::SecurityOriginData&&);
274
275     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, uint64_t callbackID);
276     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WallTime modifiedSince, uint64_t callbackID);
277     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, const Vector<String>& HSTSCacheHostnames, uint64_t callbackID);
278
279     void clearCachedCredentials();
280
281     void setCacheStorageParameters(PAL::SessionID, uint64_t quota, String&& cacheStorageDirectory, SandboxExtension::Handle&&);
282
283     // FIXME: This should take a session ID so we can identify which disk cache to delete.
284     void clearDiskCache(WallTime modifiedSince, CompletionHandler<void()>&&);
285
286     void downloadRequest(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedFilename);
287     void resumeDownload(PAL::SessionID, DownloadID, const IPC::DataReference& resumeData, const String& path, SandboxExtension::Handle&&);
288     void cancelDownload(DownloadID);
289 #if PLATFORM(COCOA)
290     void publishDownloadProgress(DownloadID, const URL&, SandboxExtension::Handle&&);
291 #endif
292     void continueWillSendRequest(DownloadID, WebCore::ResourceRequest&&);
293     void continueDecidePendingDownloadDestination(DownloadID, String destination, SandboxExtension::Handle&&, bool allowOverwrite);
294
295     void setCacheModel(CacheModel);
296     void allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo&, const String& host);
297     void setCanHandleHTTPSServerTrustEvaluation(bool);
298     void getNetworkProcessStatistics(uint64_t callbackID);
299     void clearCacheForAllOrigins(uint32_t cachesToClear);
300     void setAllowsAnySSLCertificateForWebSocket(bool);
301     
302     void syncAllCookies();
303     void didSyncAllCookies();
304
305     void writeBlobToFilePath(const URL&, const String& path, SandboxExtension::Handle&&, CompletionHandler<void(bool)>&&);
306
307 #if USE(SOUP)
308     void setIgnoreTLSErrors(bool);
309     void userPreferredLanguagesChanged(const Vector<String>&);
310     void setNetworkProxySettings(const WebCore::SoupNetworkProxySettings&);
311 #endif
312
313 #if USE(CURL)
314     void setNetworkProxySettings(PAL::SessionID, WebCore::CurlProxySettings&&);
315 #endif
316
317 #if PLATFORM(MAC)
318     static void setSharedHTTPCookieStorage(const Vector<uint8_t>& identifier);
319 #endif
320
321     void platformSyncAllCookies(CompletionHandler<void()>&&);
322
323     void registerURLSchemeAsSecure(const String&) const;
324     void registerURLSchemeAsBypassingContentSecurityPolicy(const String&) const;
325     void registerURLSchemeAsLocal(const String&) const;
326     void registerURLSchemeAsNoAccess(const String&) const;
327     void registerURLSchemeAsDisplayIsolated(const String&) const;
328     void registerURLSchemeAsCORSEnabled(const String&) const;
329     void registerURLSchemeAsCanDisplayOnlyIfCanRequest(const String&) const;
330
331 #if ENABLE(INDEXED_DATABASE)
332     void addIndexedDatabaseSession(PAL::SessionID, String&, SandboxExtension::Handle&);
333     HashSet<WebCore::SecurityOriginData> indexedDatabaseOrigins(const String& path);
334 #endif
335
336 #if ENABLE(SERVICE_WORKER)
337     void didReceiveFetchResponse(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, const WebCore::ResourceResponse&);
338     void didReceiveFetchData(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, const IPC::DataReference&, int64_t encodedDataLength);
339     void didReceiveFetchFormData(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, const IPC::FormDataReference&);
340     void didFinishFetch(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier);
341     void didFailFetch(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, const WebCore::ResourceError&);
342     void didNotHandleFetch(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier);
343
344     void didCreateWorkerContextProcessConnection(const IPC::Attachment&);
345     
346     void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin);
347     void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destination, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source, WebCore::SWServerConnectionIdentifier);
348     
349     void disableServiceWorkerProcessTerminationDelay();
350     
351     WebSWOriginStore& swOriginStoreForSession(PAL::SessionID);
352     WebSWOriginStore* existingSWOriginStoreForSession(PAL::SessionID) const;
353     bool needsServerToContextConnectionForOrigin(const WebCore::SecurityOriginData&) const;
354
355     void addServiceWorkerSession(PAL::SessionID, String& serviceWorkerRegistrationDirectory, const SandboxExtension::Handle&);
356 #endif
357
358     void postStorageTask(CrossThreadTask&&);
359     // For execution on work queue thread only.
360     void performNextStorageTask();
361     void ensurePathExists(const String& path);
362
363     // Connections to WebProcesses.
364     Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
365
366     String m_diskCacheDirectory;
367     bool m_hasSetCacheModel;
368     CacheModel m_cacheModel;
369     bool m_suppressMemoryPressureHandler { false };
370     bool m_diskCacheIsDisabledForTesting;
371     bool m_canHandleHTTPSServerTrustEvaluation;
372     String m_uiProcessBundleIdentifier;
373     DownloadManager m_downloadManager;
374
375     HashMap<PAL::SessionID, Ref<CacheStorage::Engine>> m_cacheEngines;
376
377     RefPtr<NetworkCache::Cache> m_cache;
378
379     typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
380     NetworkProcessSupplementMap m_supplements;
381
382     HashSet<PAL::SessionID> m_sessionsControlledByAutomation;
383     HashMap<PAL::SessionID, Vector<CacheStorageParametersCallback>> m_cacheStorageParametersCallbacks;
384     HashMap<PAL::SessionID, Ref<NetworkSession>> m_networkSessions;
385
386 #if PLATFORM(COCOA)
387     void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
388     void setStorageAccessAPIEnabled(bool);
389
390     // FIXME: We'd like to be able to do this without the #ifdef, but WorkQueue + BinarySemaphore isn't good enough since
391     // multiple requests to clear the cache can come in before previous requests complete, and we need to wait for all of them.
392     // In the future using WorkQueue and a counting semaphore would work, as would WorkQueue supporting the libdispatch concept of "work groups".
393     dispatch_group_t m_clearCacheDispatchGroup;
394
395     bool m_suppressesConnectionTerminationOnSystemChange { false };
396 #endif
397
398 #if ENABLE(CONTENT_EXTENSIONS)
399     NetworkContentRuleListManager m_networkContentRuleListManager;
400 #endif
401
402     Ref<WorkQueue> m_storageTaskQueue;
403
404 #if ENABLE(INDEXED_DATABASE)
405     HashMap<PAL::SessionID, String> m_idbDatabasePaths;
406     HashMap<PAL::SessionID, RefPtr<WebCore::IDBServer::IDBServer>> m_idbServers;
407     uint64_t m_idbPerOriginQuota;
408 #endif
409
410     Deque<CrossThreadTask> m_storageTasks;
411     Lock m_storageTaskMutex;
412     
413 #if ENABLE(SERVICE_WORKER)
414     HashMap<WebCore::SecurityOriginData, RefPtr<WebSWServerToContextConnection>> m_serverToContextConnections;
415     bool m_waitingForServerToContextProcessConnection { false };
416     bool m_shouldDisableServiceWorkerProcessTerminationDelay { false };
417     HashMap<PAL::SessionID, String> m_swDatabasePaths;
418     HashMap<PAL::SessionID, std::unique_ptr<WebCore::SWServer>> m_swServers;
419     HashMap<WebCore::SWServerConnectionIdentifier, WebSWServerConnection*> m_swServerConnections;
420 #endif
421
422 #if PLATFORM(COCOA)
423     NetworkHTTPSUpgradeChecker m_networkHTTPSUpgradeChecker;
424 #endif
425 };
426
427 } // namespace WebKit