b94feb5bdc682ecfc8879a7c5991cae7c2f97b37
[WebKit.git] / Source / WebKit2 / 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 <WebCore/SessionID.h>
34 #include <memory>
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
41 #if PLATFORM(IOS)
42 #include "WebSQLiteDatabaseTracker.h"
43 #endif
44
45 namespace WebCore {
46 class DownloadID;
47 class CertificateInfo;
48 class NetworkStorageSession;
49 class ProtectionSpace;
50 class SecurityOrigin;
51 class SessionID;
52 struct SecurityOriginData;
53 struct SoupNetworkProxySettings;
54 }
55
56 namespace WebKit {
57 class AuthenticationManager;
58 class NetworkConnectionToWebProcess;
59 class NetworkProcessSupplement;
60 class NetworkResourceLoader;
61 enum class WebsiteDataFetchOption;
62 enum class WebsiteDataType;
63 struct NetworkProcessCreationParameters;
64
65 class NetworkProcess : public ChildProcess, private DownloadManager::Client {
66     WTF_MAKE_NONCOPYABLE(NetworkProcess);
67     friend class NeverDestroyed<NetworkProcess>;
68     friend class NeverDestroyed<DownloadManager>;
69 public:
70     static NetworkProcess& singleton();
71
72     template <typename T>
73     T* supplement()
74     {
75         return static_cast<T*>(m_supplements.get(T::supplementName()));
76     }
77
78     template <typename T>
79     void addSupplement()
80     {
81         m_supplements.add(T::supplementName(), std::make_unique<T>(this));
82     }
83
84     void removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess*);
85
86     AuthenticationManager& authenticationManager();
87     DownloadManager& downloadManager();
88     bool canHandleHTTPSServerTrustEvaluation() const { return m_canHandleHTTPSServerTrustEvaluation; }
89
90     void processWillSuspendImminently(bool& handled);
91     void prepareToSuspend();
92     void cancelPrepareToSuspend();
93     void processDidResume();
94
95     // Diagnostic messages logging.
96     void logDiagnosticMessage(uint64_t webPageID, const String& message, const String& description, WebCore::ShouldSample);
97     void logDiagnosticMessageWithResult(uint64_t webPageID, const String& message, const String& description, WebCore::DiagnosticLoggingResultType, WebCore::ShouldSample);
98     void logDiagnosticMessageWithValue(uint64_t webPageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
99
100 #if PLATFORM(COCOA)
101     RetainPtr<CFDataRef> sourceApplicationAuditData() const;
102     void clearHSTSCache(WebCore::NetworkStorageSession&, std::chrono::system_clock::time_point modifiedSince);
103 #endif
104
105 #if USE(NETWORK_SESSION)
106     void findPendingDownloadLocation(NetworkDataTask&, ResponseCompletionHandler&&, const WebCore::ResourceResponse&);
107 #endif
108
109 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
110     void canAuthenticateAgainstProtectionSpace(NetworkResourceLoader&, const WebCore::ProtectionSpace&);
111 #endif
112
113     void prefetchDNS(const String&);
114
115     void ensurePrivateBrowsingSession(WebCore::SessionID);
116
117     void grantSandboxExtensionsToDatabaseProcessForBlobs(const Vector<String>& filenames, Function<void ()>&& completionHandler);
118
119     std::chrono::milliseconds loadThrottleLatency() const { return m_loadThrottleLatency; }
120
121 private:
122     NetworkProcess();
123     ~NetworkProcess();
124
125     void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
126
127     void terminate() override;
128     void platformTerminate();
129
130     void lowMemoryHandler(Critical);
131
132     // ChildProcess
133     void initializeProcess(const ChildProcessInitializationParameters&) override;
134     void initializeProcessName(const ChildProcessInitializationParameters&) override;
135     void initializeSandbox(const ChildProcessInitializationParameters&, SandboxInitializationParameters&) override;
136     void initializeConnection(IPC::Connection*) override;
137     bool shouldTerminate() override;
138
139     // IPC::Connection::Client
140     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
141     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
142     void didClose(IPC::Connection&) override;
143
144     // DownloadManager::Client
145     void didCreateDownload() override;
146     void didDestroyDownload() override;
147     IPC::Connection* downloadProxyConnection() override;
148     AuthenticationManager& downloadsAuthenticationManager() override;
149 #if USE(NETWORK_SESSION)
150     void pendingDownloadCanceled(DownloadID) override;
151 #endif
152
153     // Message Handlers
154     void didReceiveNetworkProcessMessage(IPC::Connection&, IPC::Decoder&);
155     void didReceiveSyncNetworkProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
156     void initializeNetworkProcess(NetworkProcessCreationParameters&&);
157     void createNetworkConnectionToWebProcess();
158     void destroyPrivateBrowsingSession(WebCore::SessionID);
159
160     void fetchWebsiteData(WebCore::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, uint64_t callbackID);
161     void deleteWebsiteData(WebCore::SessionID, OptionSet<WebsiteDataType>, std::chrono::system_clock::time_point modifiedSince, uint64_t callbackID);
162     void deleteWebsiteDataForOrigins(WebCore::SessionID, OptionSet<WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, uint64_t callbackID);
163
164     void clearCachedCredentials();
165
166     // FIXME: This should take a session ID so we can identify which disk cache to delete.
167     void clearDiskCache(std::chrono::system_clock::time_point modifiedSince, std::function<void ()> completionHandler);
168
169     void downloadRequest(WebCore::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedFilename);
170     void resumeDownload(WebCore::SessionID, DownloadID, const IPC::DataReference& resumeData, const String& path, const SandboxExtension::Handle&);
171     void cancelDownload(DownloadID);
172 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
173     void continueCanAuthenticateAgainstProtectionSpace(uint64_t resourceLoadIdentifier, bool canAuthenticate);
174 #endif
175 #if USE(NETWORK_SESSION)
176 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
177     void continueCanAuthenticateAgainstProtectionSpaceDownload(DownloadID, bool canAuthenticate);
178 #endif
179     void continueWillSendRequest(DownloadID, WebCore::ResourceRequest&&);
180 #endif
181     void continueDecidePendingDownloadDestination(DownloadID, String destination, const SandboxExtension::Handle& sandboxExtensionHandle, bool allowOverwrite);
182
183     void setCacheModel(uint32_t);
184     void allowSpecificHTTPSCertificateForHost(const WebCore::CertificateInfo&, const String& host);
185     void setCanHandleHTTPSServerTrustEvaluation(bool);
186     void getNetworkProcessStatistics(uint64_t callbackID);
187     void clearCacheForAllOrigins(uint32_t cachesToClear);
188
189     void didGrantSandboxExtensionsToDatabaseProcessForBlobs(uint64_t requestID);
190
191 #if USE(SOUP)
192     void setIgnoreTLSErrors(bool);
193     void userPreferredLanguagesChanged(const Vector<String>&);
194     void setNetworkProxySettings(const WebCore::SoupNetworkProxySettings&);
195 #endif
196
197     // Platform Helpers
198     void platformSetURLCacheSize(unsigned urlCacheMemoryCapacity, uint64_t urlCacheDiskCapacity);
199
200     // Connections to WebProcesses.
201     Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
202
203     String m_diskCacheDirectory;
204     bool m_hasSetCacheModel;
205     CacheModel m_cacheModel;
206     int64_t m_diskCacheSizeOverride { -1 };
207     bool m_suppressMemoryPressureHandler { false };
208     bool m_diskCacheIsDisabledForTesting;
209     bool m_canHandleHTTPSServerTrustEvaluation;
210     std::chrono::milliseconds m_loadThrottleLatency;
211
212     typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
213     NetworkProcessSupplementMap m_supplements;
214
215     HashMap<uint64_t, Function<void ()>> m_sandboxExtensionForBlobsCompletionHandlers;
216     HashMap<uint64_t, Ref<NetworkResourceLoader>> m_waitingNetworkResourceLoaders;
217
218 #if PLATFORM(COCOA)
219     void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
220     void setCookieStoragePartitioningEnabled(bool);
221
222     // FIXME: We'd like to be able to do this without the #ifdef, but WorkQueue + BinarySemaphore isn't good enough since
223     // multiple requests to clear the cache can come in before previous requests complete, and we need to wait for all of them.
224     // In the future using WorkQueue and a counting semaphore would work, as would WorkQueue supporting the libdispatch concept of "work groups".
225     dispatch_group_t m_clearCacheDispatchGroup;
226 #endif
227
228 #if PLATFORM(IOS)
229     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
230 #endif
231 };
232
233 } // namespace WebKit