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