2 * Copyright (C) 2012, 2013 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.
26 #ifndef NetworkProcess_h
27 #define NetworkProcess_h
29 #include "CacheModel.h"
30 #include "ChildProcess.h"
31 #include "DownloadManager.h"
32 #include "MessageReceiverMap.h"
33 #include <WebCore/DiagnosticLoggingClient.h>
34 #include <WebCore/MemoryPressureHandler.h>
35 #include <WebCore/SessionID.h>
37 #include <wtf/Forward.h>
38 #include <wtf/NeverDestroyed.h>
41 #include "WebSQLiteDatabaseTracker.h"
45 class CertificateInfo;
46 class NetworkStorageSession;
49 struct SecurityOriginData;
53 class AuthenticationManager;
54 class NetworkConnectionToWebProcess;
55 class NetworkProcessSupplement;
56 struct NetworkProcessCreationParameters;
58 class NetworkProcess : public ChildProcess, private DownloadManager::Client {
59 WTF_MAKE_NONCOPYABLE(NetworkProcess);
60 friend class NeverDestroyed<NetworkProcess>;
61 friend class NeverDestroyed<DownloadManager>;
63 static NetworkProcess& singleton();
68 return static_cast<T*>(m_supplements.get(T::supplementName()));
74 m_supplements.add(T::supplementName(), std::make_unique<T>(this));
77 void removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess*);
79 AuthenticationManager& authenticationManager();
80 DownloadManager& downloadManager();
81 bool canHandleHTTPSServerTrustEvaluation() const { return m_canHandleHTTPSServerTrustEvaluation; }
83 void processWillSuspendImminently(bool& handled);
84 void prepareToSuspend();
85 void cancelPrepareToSuspend();
86 void processDidResume();
88 // Diagnostic messages logging.
89 void logDiagnosticMessage(uint64_t webPageID, const String& message, const String& description, WebCore::ShouldSample);
90 void logDiagnosticMessageWithResult(uint64_t webPageID, const String& message, const String& description, WebCore::DiagnosticLoggingResultType, WebCore::ShouldSample);
91 void logDiagnosticMessageWithValue(uint64_t webPageID, const String& message, const String& description, const String& value, WebCore::ShouldSample);
94 static Vector<Ref<WebCore::SecurityOrigin>> cfURLCacheOrigins();
95 static void clearCFURLCacheForOrigins(const Vector<WebCore::SecurityOriginData>&);
99 void clearHSTSCache(WebCore::NetworkStorageSession&, std::chrono::system_clock::time_point modifiedSince);
102 void prefetchDNS(const String&);
108 void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
110 virtual void terminate() override;
111 void platformTerminate();
113 void lowMemoryHandler(WebCore::Critical);
114 void platformLowMemoryHandler(WebCore::Critical);
117 virtual void initializeProcess(const ChildProcessInitializationParameters&) override;
118 virtual void initializeProcessName(const ChildProcessInitializationParameters&) override;
119 virtual void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
120 virtual void initializeConnection(IPC::Connection*) override;
121 virtual bool shouldTerminate() override;
123 // IPC::Connection::Client
124 virtual void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
125 virtual void didReceiveSyncMessage(IPC::Connection&, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&) override;
126 virtual void didClose(IPC::Connection&) override;
127 virtual void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
128 virtual IPC::ProcessType localProcessType() override { return IPC::ProcessType::Network; }
129 virtual IPC::ProcessType remoteProcessType() override { return IPC::ProcessType::UI; }
131 // DownloadManager::Client
132 virtual void didCreateDownload() override;
133 virtual void didDestroyDownload() override;
134 virtual IPC::Connection* downloadProxyConnection() override;
135 virtual AuthenticationManager& downloadsAuthenticationManager() override;
138 void didReceiveNetworkProcessMessage(IPC::Connection&, IPC::MessageDecoder&);
139 void didReceiveSyncNetworkProcessMessage(IPC::Connection&, IPC::MessageDecoder&, std::unique_ptr<IPC::MessageEncoder>&);
140 void initializeNetworkProcess(const NetworkProcessCreationParameters&);
141 void createNetworkConnectionToWebProcess();
142 void ensurePrivateBrowsingSession(WebCore::SessionID);
143 void destroyPrivateBrowsingSession(WebCore::SessionID);
145 void fetchWebsiteData(WebCore::SessionID, uint64_t websiteDataTypes, uint64_t callbackID);
146 void deleteWebsiteData(WebCore::SessionID, uint64_t websiteDataTypes, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID);
147 void deleteWebsiteDataForOrigins(WebCore::SessionID, uint64_t websiteDataTypes, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, uint64_t callbackID);
149 // FIXME: This should take a session ID so we can identify which disk cache to delete.
150 void clearDiskCache(std::chrono::system_clock::time_point modifiedSince, std::function<void ()> completionHandler);
152 void downloadRequest(WebCore::SessionID, DownloadID, const WebCore::ResourceRequest&);
153 void resumeDownload(WebCore::SessionID, DownloadID, const IPC::DataReference& resumeData, const String& path, const SandboxExtension::Handle&);
154 void cancelDownload(DownloadID);
155 void setCacheModel(uint32_t);
156 void allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo&, const String& host);
157 void setCanHandleHTTPSServerTrustEvaluation(bool);
158 void getNetworkProcessStatistics(uint64_t callbackID);
159 void clearCacheForAllOrigins(uint32_t cachesToClear);
162 void setIgnoreTLSErrors(bool);
163 void userPreferredLanguagesChanged(const Vector<String>&);
167 void platformSetCacheModel(CacheModel);
169 // Connections to WebProcesses.
170 Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
172 String m_diskCacheDirectory;
173 bool m_hasSetCacheModel;
174 CacheModel m_cacheModel;
175 int64_t m_diskCacheSizeOverride { -1 };
176 bool m_diskCacheIsDisabledForTesting;
177 bool m_canHandleHTTPSServerTrustEvaluation;
179 typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
180 NetworkProcessSupplementMap m_supplements;
183 void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
185 // FIXME: We'd like to be able to do this without the #ifdef, but WorkQueue + BinarySemaphore isn't good enough since
186 // multiple requests to clear the cache can come in before previous requests complete, and we need to wait for all of them.
187 // In the future using WorkQueue and a counting semaphore would work, as would WorkQueue supporting the libdispatch concept of "work groups".
188 dispatch_group_t m_clearCacheDispatchGroup;
192 WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
196 } // namespace WebKit
198 #endif // NetworkProcess_h