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);
156 void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
158 void terminate() override;
159 void platformTerminate();
161 void lowMemoryHandler(Critical);
163 enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
164 void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
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;
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;
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;
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);
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);
198 void clearCachedCredentials();
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);
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);
209 #if USE(NETWORK_SESSION)
210 void continueWillSendRequest(DownloadID, WebCore::ResourceRequest&&);
212 void continueDecidePendingDownloadDestination(DownloadID, String destination, SandboxExtension::Handle&&, bool allowOverwrite);
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();
222 void didGrantSandboxExtensionsToStorageProcessForBlobs(uint64_t requestID);
225 void setIgnoreTLSErrors(bool);
226 void userPreferredLanguagesChanged(const Vector<String>&);
227 void setNetworkProxySettings(const WebCore::SoupNetworkProxySettings&);
231 void platformSetURLCacheSize(unsigned urlCacheMemoryCapacity, uint64_t urlCacheDiskCapacity);
233 // Connections to WebProcesses.
234 Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
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;
247 RefPtr<NetworkCache::Cache> m_cache;
249 typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
250 NetworkProcessSupplementMap m_supplements;
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;
259 void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
260 void setCookieStoragePartitioningEnabled(bool);
261 void setStorageAccessAPIEnabled(bool);
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;
270 WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
274 } // namespace WebKit