2 * Copyright (C) 2012-2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "CacheModel.h"
29 #include "ChildProcess.h"
30 #include "DownloadManager.h"
31 #include "MessageReceiverMap.h"
32 #include <WebCore/DiagnosticLoggingClient.h>
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>
43 #include "WebSQLiteDatabaseTracker.h"
52 class CertificateInfo;
53 class NetworkStorageSession;
54 class ProtectionSpace;
56 struct SecurityOriginData;
57 struct SoupNetworkProxySettings;
58 enum class StoredCredentialsPolicy;
63 class AuthenticationManager;
64 #if ENABLE(SERVER_PRECONNECT)
67 class NetworkConnectionToWebProcess;
68 class NetworkProcessSupplement;
69 class NetworkResourceLoader;
70 enum class WebsiteDataFetchOption;
71 enum class WebsiteDataType;
72 struct NetworkProcessCreationParameters;
73 struct WebsiteDataStoreParameters;
75 namespace NetworkCache {
79 class NetworkProcess : public ChildProcess, private DownloadManager::Client {
80 WTF_MAKE_NONCOPYABLE(NetworkProcess);
81 friend class NeverDestroyed<NetworkProcess>;
82 friend class NeverDestroyed<DownloadManager>;
84 static NetworkProcess& singleton();
89 return static_cast<T*>(m_supplements.get(T::supplementName()));
95 m_supplements.add(T::supplementName(), std::make_unique<T>(*this));
98 void removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess*);
100 AuthenticationManager& authenticationManager();
101 DownloadManager& downloadManager();
103 NetworkCache::Cache* cache() { return m_cache.get(); }
105 bool canHandleHTTPSServerTrustEvaluation() const { return m_canHandleHTTPSServerTrustEvaluation; }
107 void processWillSuspendImminently(bool& handled);
108 void prepareToSuspend();
109 void cancelPrepareToSuspend();
110 void processDidResume();
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);
118 RetainPtr<CFDataRef> sourceApplicationAuditData() const;
119 void clearHSTSCache(WebCore::NetworkStorageSession&, std::chrono::system_clock::time_point modifiedSince);
122 #if USE(NETWORK_SESSION)
123 void findPendingDownloadLocation(NetworkDataTask&, ResponseCompletionHandler&&, const WebCore::ResourceResponse&);
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&);
133 void prefetchDNS(const String&);
135 void addWebsiteDataStore(WebsiteDataStoreParameters&&);
137 void grantSandboxExtensionsToStorageProcessForBlobs(const Vector<String>& filenames, Function<void ()>&& completionHandler);
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);
146 Seconds loadThrottleLatency() const { return m_loadThrottleLatency; }
147 String cacheStorageDirectory(PAL::SessionID) const;
148 uint64_t cacheStoragePerOriginQuota() const;
150 void preconnectTo(const WebCore::URL&, WebCore::StoredCredentialsPolicy);
152 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
153 bool shouldLogCookieInformation() const { return m_logCookieInformation; }
160 void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
162 void terminate() override;
163 void platformTerminate();
165 void lowMemoryHandler(Critical);
167 enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
168 void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
171 void initializeProcess(const ChildProcessInitializationParameters&) override;
172 void initializeProcessName(const ChildProcessInitializationParameters&) override;
173 void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
174 void initializeConnection(IPC::Connection*) override;
175 bool shouldTerminate() override;
177 // IPC::Connection::Client
178 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
179 void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
180 void didClose(IPC::Connection&) override;
182 // DownloadManager::Client
183 void didCreateDownload() override;
184 void didDestroyDownload() override;
185 IPC::Connection* downloadProxyConnection() override;
186 AuthenticationManager& downloadsAuthenticationManager() override;
187 #if USE(NETWORK_SESSION)
188 void pendingDownloadCanceled(DownloadID) override;
192 void didReceiveNetworkProcessMessage(IPC::Connection&, IPC::Decoder&);
193 void didReceiveSyncNetworkProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
194 void initializeNetworkProcess(NetworkProcessCreationParameters&&);
195 void createNetworkConnectionToWebProcess();
196 void destroySession(PAL::SessionID);
198 void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, uint64_t callbackID);
199 void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID);
200 void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, uint64_t callbackID);
202 void clearCachedCredentials();
204 // FIXME: This should take a session ID so we can identify which disk cache to delete.
205 void clearDiskCache(std::chrono::system_clock::time_point modifiedSince, Function<void ()>&& completionHandler);
207 void downloadRequest(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedFilename);
208 void resumeDownload(PAL::SessionID, DownloadID, const IPC::DataReference& resumeData, const String& path, SandboxExtension::Handle&&);
209 void cancelDownload(DownloadID);
210 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
211 void continueCanAuthenticateAgainstProtectionSpace(uint64_t resourceLoadIdentifier, bool canAuthenticate);
213 #if USE(NETWORK_SESSION)
214 void continueWillSendRequest(DownloadID, WebCore::ResourceRequest&&);
216 void continueDecidePendingDownloadDestination(DownloadID, String destination, SandboxExtension::Handle&&, bool allowOverwrite);
218 void setCacheModel(uint32_t);
219 void allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo&, const String& host);
220 void setCanHandleHTTPSServerTrustEvaluation(bool);
221 void getNetworkProcessStatistics(uint64_t callbackID);
222 void clearCacheForAllOrigins(uint32_t cachesToClear);
223 void setAllowsAnySSLCertificateForWebSocket(bool);
224 void syncAllCookies();
226 void didGrantSandboxExtensionsToStorageProcessForBlobs(uint64_t requestID);
229 void setIgnoreTLSErrors(bool);
230 void userPreferredLanguagesChanged(const Vector<String>&);
231 void setNetworkProxySettings(const WebCore::SoupNetworkProxySettings&);
235 void platformSetURLCacheSize(unsigned urlCacheMemoryCapacity, uint64_t urlCacheDiskCapacity);
237 // Connections to WebProcesses.
238 Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
240 String m_cacheStorageDirectory;
241 uint64_t m_cacheStoragePerOriginQuota { 0 };
242 String m_diskCacheDirectory;
243 bool m_hasSetCacheModel;
244 CacheModel m_cacheModel;
245 int64_t m_diskCacheSizeOverride { -1 };
246 bool m_suppressMemoryPressureHandler { false };
247 bool m_diskCacheIsDisabledForTesting;
248 bool m_canHandleHTTPSServerTrustEvaluation;
249 Seconds m_loadThrottleLatency;
250 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
251 bool m_logCookieInformation { false };
254 RefPtr<NetworkCache::Cache> m_cache;
256 typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
257 NetworkProcessSupplementMap m_supplements;
259 HashMap<uint64_t, Function<void ()>> m_sandboxExtensionForBlobsCompletionHandlers;
260 HashMap<uint64_t, Ref<NetworkResourceLoader>> m_waitingNetworkResourceLoaders;
261 #if ENABLE(SERVER_PRECONNECT)
262 HashMap<uint64_t, WeakPtr<PreconnectTask>> m_waitingPreconnectTasks;
266 void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
267 void setCookieStoragePartitioningEnabled(bool);
268 void setStorageAccessAPIEnabled(bool);
270 // FIXME: We'd like to be able to do this without the #ifdef, but WorkQueue + BinarySemaphore isn't good enough since
271 // multiple requests to clear the cache can come in before previous requests complete, and we need to wait for all of them.
272 // In the future using WorkQueue and a counting semaphore would work, as would WorkQueue supporting the libdispatch concept of "work groups".
273 dispatch_group_t m_clearCacheDispatchGroup;
277 WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
281 } // namespace WebKit