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