8627d91ce1e6fe06d6b0345904bad189b145ffe6
[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 removeAllStorageAccess(PAL::SessionID, uint64_t contextId);
165     void removePrevalentDomains(PAL::SessionID, const Vector<String>& domains);
166     void setCacheMaxAgeCapForPrevalentResources(PAL::SessionID, Seconds, uint64_t contextId);
167     void resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID, uint64_t contextId);
168 #endif
169
170     using CacheStorageParametersCallback = CompletionHandler<void(const String&, uint64_t quota)>;
171     void cacheStorageParameters(PAL::SessionID, CacheStorageParametersCallback&&);
172
173     void preconnectTo(const URL&, WebCore::StoredCredentialsPolicy);
174
175     void setSessionIsControlledByAutomation(PAL::SessionID, bool);
176     bool sessionIsControlledByAutomation(PAL::SessionID) const;
177
178 #if ENABLE(CONTENT_EXTENSIONS)
179     NetworkContentRuleListManager& networkContentRuleListManager() { return m_networkContentRuleListManager; }
180 #endif
181
182 #if ENABLE(INDEXED_DATABASE)
183     WebCore::IDBServer::IDBServer& idbServer(PAL::SessionID);
184     // WebCore::IDBServer::IDBBackingStoreFileHandler.
185     void accessToTemporaryFileComplete(const String& path) final;
186     void setIDBPerOriginQuota(uint64_t);
187 #endif
188
189 #if ENABLE(SANDBOX_EXTENSIONS)
190     void getSandboxExtensionsForBlobFiles(const Vector<String>& filenames, CompletionHandler<void(SandboxExtension::HandleArray&&)>&&);
191 #endif
192
193     void didReceiveNetworkProcessMessage(IPC::Connection&, IPC::Decoder&);
194
195 #if ENABLE(SERVICE_WORKER)
196     WebSWServerToContextConnection* serverToContextConnectionForOrigin(const WebCore::SecurityOriginData&);
197     void createServerToContextConnection(const WebCore::SecurityOriginData&, Optional<PAL::SessionID>);
198     
199     WebCore::SWServer& swServerForSession(PAL::SessionID);
200     void registerSWServerConnection(WebSWServerConnection&);
201     void unregisterSWServerConnection(WebSWServerConnection&);
202     
203     void swContextConnectionMayNoLongerBeNeeded(WebSWServerToContextConnection&);
204     
205     WebSWServerToContextConnection* connectionToContextProcessFromIPCConnection(IPC::Connection&);
206     void connectionToContextProcessWasClosed(Ref<WebSWServerToContextConnection>&&);
207 #endif
208
209 #if PLATFORM(IOS_FAMILY)
210     bool parentProcessHasServiceWorkerEntitlement() const;
211 #else
212     bool parentProcessHasServiceWorkerEntitlement() const { return true; }
213 #endif
214
215 #if PLATFORM(COCOA)
216     NetworkHTTPSUpgradeChecker& networkHTTPSUpgradeChecker() { return m_networkHTTPSUpgradeChecker; }
217 #endif
218
219     const String& uiProcessBundleIdentifier() const { return m_uiProcessBundleIdentifier; }
220
221     void ref() const override { ThreadSafeRefCounted<NetworkProcess>::ref(); }
222     void deref() const override { ThreadSafeRefCounted<NetworkProcess>::deref(); }
223     
224     CacheStorage::Engine* findCacheEngine(const PAL::SessionID&);
225     CacheStorage::Engine& ensureCacheEngine(const PAL::SessionID&, Function<Ref<CacheStorage::Engine>()>&&);
226     void removeCacheEngine(const PAL::SessionID&);
227     
228 private:
229     NetworkProcess();
230
231     void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
232
233     void terminate() override;
234     void platformTerminate();
235
236     void lowMemoryHandler(Critical);
237     
238     void processDidTransitionToForeground();
239     void processDidTransitionToBackground();
240     void platformProcessDidTransitionToForeground();
241     void platformProcessDidTransitionToBackground();
242
243     enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
244     void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
245     void platformPrepareToSuspend(CompletionHandler<void()>&&);
246     void platformProcessDidResume();
247
248     // ChildProcess
249     void initializeProcess(const ChildProcessInitializationParameters&) override;
250     void initializeProcessName(const ChildProcessInitializationParameters&) override;
251     void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
252     void initializeConnection(IPC::Connection*) override;
253     bool shouldTerminate() override;
254
255     // IPC::Connection::Client
256     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
257     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
258     void didClose(IPC::Connection&) override;
259
260     // DownloadManager::Client
261     void didCreateDownload() override;
262     void didDestroyDownload() override;
263     IPC::Connection* downloadProxyConnection() override;
264     IPC::Connection* parentProcessConnectionForDownloads() override { return parentProcessConnection(); }
265     AuthenticationManager& downloadsAuthenticationManager() override;
266     void pendingDownloadCanceled(DownloadID) override;
267
268     // Message Handlers
269     void didReceiveSyncNetworkProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
270     void initializeNetworkProcess(NetworkProcessCreationParameters&&);
271     void createNetworkConnectionToWebProcess(bool isServiceWorkerProcess, WebCore::SecurityOriginData&&);
272
273     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, uint64_t callbackID);
274     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WallTime modifiedSince, uint64_t callbackID);
275     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, const Vector<String>& HSTSCacheHostnames, uint64_t callbackID);
276
277     void clearCachedCredentials();
278
279     void setCacheStorageParameters(PAL::SessionID, uint64_t quota, String&& cacheStorageDirectory, SandboxExtension::Handle&&);
280
281     // FIXME: This should take a session ID so we can identify which disk cache to delete.
282     void clearDiskCache(WallTime modifiedSince, CompletionHandler<void()>&&);
283
284     void downloadRequest(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedFilename);
285     void resumeDownload(PAL::SessionID, DownloadID, const IPC::DataReference& resumeData, const String& path, SandboxExtension::Handle&&);
286     void cancelDownload(DownloadID);
287 #if PLATFORM(COCOA)
288     void publishDownloadProgress(DownloadID, const URL&, SandboxExtension::Handle&&);
289 #endif
290     void continueWillSendRequest(DownloadID, WebCore::ResourceRequest&&);
291     void continueDecidePendingDownloadDestination(DownloadID, String destination, SandboxExtension::Handle&&, bool allowOverwrite);
292
293     void setCacheModel(CacheModel);
294     void allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo&, const String& host);
295     void setCanHandleHTTPSServerTrustEvaluation(bool);
296     void getNetworkProcessStatistics(uint64_t callbackID);
297     void clearCacheForAllOrigins(uint32_t cachesToClear);
298     void setAllowsAnySSLCertificateForWebSocket(bool);
299     
300     void syncAllCookies();
301     void didSyncAllCookies();
302
303     void writeBlobToFilePath(const URL&, const String& path, SandboxExtension::Handle&&, CompletionHandler<void(bool)>&&);
304
305 #if USE(SOUP)
306     void setIgnoreTLSErrors(bool);
307     void userPreferredLanguagesChanged(const Vector<String>&);
308     void setNetworkProxySettings(const WebCore::SoupNetworkProxySettings&);
309 #endif
310
311 #if USE(CURL)
312     void setNetworkProxySettings(PAL::SessionID, WebCore::CurlProxySettings&&);
313 #endif
314
315 #if PLATFORM(MAC)
316     static void setSharedHTTPCookieStorage(const Vector<uint8_t>& identifier);
317 #endif
318
319     void platformSyncAllCookies(CompletionHandler<void()>&&);
320
321     void registerURLSchemeAsSecure(const String&) const;
322     void registerURLSchemeAsBypassingContentSecurityPolicy(const String&) const;
323     void registerURLSchemeAsLocal(const String&) const;
324     void registerURLSchemeAsNoAccess(const String&) const;
325     void registerURLSchemeAsDisplayIsolated(const String&) const;
326     void registerURLSchemeAsCORSEnabled(const String&) const;
327     void registerURLSchemeAsCanDisplayOnlyIfCanRequest(const String&) const;
328
329 #if ENABLE(INDEXED_DATABASE)
330     void addIndexedDatabaseSession(PAL::SessionID, String&, SandboxExtension::Handle&);
331     HashSet<WebCore::SecurityOriginData> indexedDatabaseOrigins(const String& path);
332 #endif
333
334 #if ENABLE(SERVICE_WORKER)
335     void didReceiveFetchResponse(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, const WebCore::ResourceResponse&);
336     void didReceiveFetchData(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, const IPC::DataReference&, int64_t encodedDataLength);
337     void didReceiveFetchFormData(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, const IPC::FormDataReference&);
338     void didFinishFetch(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier);
339     void didFailFetch(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier, const WebCore::ResourceError&);
340     void didNotHandleFetch(WebCore::SWServerConnectionIdentifier, WebCore::FetchIdentifier);
341
342     void didCreateWorkerContextProcessConnection(const IPC::Attachment&);
343     
344     void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, WebCore::MessageWithMessagePorts&&, WebCore::ServiceWorkerIdentifier sourceIdentifier, const String& sourceOrigin);
345     void postMessageToServiceWorker(WebCore::ServiceWorkerIdentifier destination, WebCore::MessageWithMessagePorts&&, const WebCore::ServiceWorkerOrClientIdentifier& source, WebCore::SWServerConnectionIdentifier);
346     
347     void disableServiceWorkerProcessTerminationDelay();
348     
349     WebSWOriginStore& swOriginStoreForSession(PAL::SessionID);
350     WebSWOriginStore* existingSWOriginStoreForSession(PAL::SessionID) const;
351     bool needsServerToContextConnectionForOrigin(const WebCore::SecurityOriginData&) const;
352
353     void addServiceWorkerSession(PAL::SessionID, String& serviceWorkerRegistrationDirectory, const SandboxExtension::Handle&);
354 #endif
355
356     void postStorageTask(CrossThreadTask&&);
357     // For execution on work queue thread only.
358     void performNextStorageTask();
359     void ensurePathExists(const String& path);
360
361     // Connections to WebProcesses.
362     Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
363
364     String m_diskCacheDirectory;
365     bool m_hasSetCacheModel;
366     CacheModel m_cacheModel;
367     bool m_suppressMemoryPressureHandler { false };
368     bool m_diskCacheIsDisabledForTesting;
369     bool m_canHandleHTTPSServerTrustEvaluation;
370     String m_uiProcessBundleIdentifier;
371     DownloadManager m_downloadManager;
372
373     HashMap<PAL::SessionID, Ref<CacheStorage::Engine>> m_cacheEngines;
374
375     RefPtr<NetworkCache::Cache> m_cache;
376
377     typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
378     NetworkProcessSupplementMap m_supplements;
379
380     HashSet<PAL::SessionID> m_sessionsControlledByAutomation;
381     HashMap<PAL::SessionID, Vector<CacheStorageParametersCallback>> m_cacheStorageParametersCallbacks;
382     HashMap<PAL::SessionID, Ref<NetworkSession>> m_networkSessions;
383
384 #if PLATFORM(COCOA)
385     void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
386     void setStorageAccessAPIEnabled(bool);
387
388     // FIXME: We'd like to be able to do this without the #ifdef, but WorkQueue + BinarySemaphore isn't good enough since
389     // multiple requests to clear the cache can come in before previous requests complete, and we need to wait for all of them.
390     // In the future using WorkQueue and a counting semaphore would work, as would WorkQueue supporting the libdispatch concept of "work groups".
391     dispatch_group_t m_clearCacheDispatchGroup;
392
393     bool m_suppressesConnectionTerminationOnSystemChange { false };
394 #endif
395
396 #if ENABLE(CONTENT_EXTENSIONS)
397     NetworkContentRuleListManager m_networkContentRuleListManager;
398 #endif
399
400     Ref<WorkQueue> m_storageTaskQueue;
401
402 #if ENABLE(INDEXED_DATABASE)
403     HashMap<PAL::SessionID, String> m_idbDatabasePaths;
404     HashMap<PAL::SessionID, RefPtr<WebCore::IDBServer::IDBServer>> m_idbServers;
405     uint64_t m_idbPerOriginQuota;
406 #endif
407
408     Deque<CrossThreadTask> m_storageTasks;
409     Lock m_storageTaskMutex;
410     
411 #if ENABLE(SERVICE_WORKER)
412     HashMap<WebCore::SecurityOriginData, RefPtr<WebSWServerToContextConnection>> m_serverToContextConnections;
413     bool m_waitingForServerToContextProcessConnection { false };
414     bool m_shouldDisableServiceWorkerProcessTerminationDelay { false };
415     HashMap<PAL::SessionID, String> m_swDatabasePaths;
416     HashMap<PAL::SessionID, std::unique_ptr<WebCore::SWServer>> m_swServers;
417     HashMap<WebCore::SWServerConnectionIdentifier, WebSWServerConnection*> m_swServerConnections;
418 #endif
419
420 #if PLATFORM(COCOA)
421     NetworkHTTPSUpgradeChecker m_networkHTTPSUpgradeChecker;
422 #endif
423 };
424
425 } // namespace WebKit