Add optional logging of per-resource cookie information
[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 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
153     bool shouldLogCookieInformation() const { return m_logCookieInformation; }
154 #endif
155
156 private:
157     NetworkProcess();
158     ~NetworkProcess();
159
160     void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
161
162     void terminate() override;
163     void platformTerminate();
164
165     void lowMemoryHandler(Critical);
166
167     enum class ShouldAcknowledgeWhenReadyToSuspend { No, Yes };
168     void actualPrepareToSuspend(ShouldAcknowledgeWhenReadyToSuspend);
169
170     // ChildProcess
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;
176
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;
181
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;
189 #endif
190
191     // Message Handlers
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);
197
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);
201
202     void clearCachedCredentials();
203
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);
206
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);
212 #endif
213 #if USE(NETWORK_SESSION)
214     void continueWillSendRequest(DownloadID, WebCore::ResourceRequest&&);
215 #endif
216     void continueDecidePendingDownloadDestination(DownloadID, String destination, SandboxExtension::Handle&&, bool allowOverwrite);
217
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();
225
226     void didGrantSandboxExtensionsToStorageProcessForBlobs(uint64_t requestID);
227
228 #if USE(SOUP)
229     void setIgnoreTLSErrors(bool);
230     void userPreferredLanguagesChanged(const Vector<String>&);
231     void setNetworkProxySettings(const WebCore::SoupNetworkProxySettings&);
232 #endif
233
234     // Platform Helpers
235     void platformSetURLCacheSize(unsigned urlCacheMemoryCapacity, uint64_t urlCacheDiskCapacity);
236
237     // Connections to WebProcesses.
238     Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
239
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 };
252 #endif
253
254     RefPtr<NetworkCache::Cache> m_cache;
255
256     typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
257     NetworkProcessSupplementMap m_supplements;
258
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;
263 #endif
264
265 #if PLATFORM(COCOA)
266     void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
267     void setCookieStoragePartitioningEnabled(bool);
268     void setStorageAccessAPIEnabled(bool);
269
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;
274 #endif
275
276 #if PLATFORM(IOS)
277     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
278 #endif
279 };
280
281 } // namespace WebKit