Add types for DownloadID and TaskIdentifier
[WebKit-https.git] / Source / WebKit2 / NetworkProcess / NetworkProcess.h
1 /*
2  * Copyright (C) 2012, 2013 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 #ifndef NetworkProcess_h
27 #define NetworkProcess_h
28
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>
36 #include <memory>
37 #include <wtf/Forward.h>
38 #include <wtf/NeverDestroyed.h>
39
40 #if PLATFORM(IOS)
41 #include "WebSQLiteDatabaseTracker.h"
42 #endif
43
44 namespace WebCore {
45 class CertificateInfo;
46 class NetworkStorageSession;
47 class SecurityOrigin;
48 class SessionID;
49 struct SecurityOriginData;
50 }
51
52 namespace WebKit {
53 class AuthenticationManager;
54 class NetworkConnectionToWebProcess;
55 class NetworkProcessSupplement;
56 struct NetworkProcessCreationParameters;
57
58 class NetworkProcess : public ChildProcess, private DownloadManager::Client {
59     WTF_MAKE_NONCOPYABLE(NetworkProcess);
60     friend class NeverDestroyed<NetworkProcess>;
61     friend class NeverDestroyed<DownloadManager>;
62 public:
63     static NetworkProcess& singleton();
64
65     template <typename T>
66     T* supplement()
67     {
68         return static_cast<T*>(m_supplements.get(T::supplementName()));
69     }
70
71     template <typename T>
72     void addSupplement()
73     {
74         m_supplements.add(T::supplementName(), std::make_unique<T>(this));
75     }
76
77     void removeNetworkConnectionToWebProcess(NetworkConnectionToWebProcess*);
78
79     AuthenticationManager& authenticationManager();
80     DownloadManager& downloadManager();
81     bool canHandleHTTPSServerTrustEvaluation() const { return m_canHandleHTTPSServerTrustEvaluation; }
82
83     void processWillSuspendImminently(bool& handled);
84     void prepareToSuspend();
85     void cancelPrepareToSuspend();
86     void processDidResume();
87
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);
92
93 #if USE(CFURLCACHE)
94     static Vector<Ref<WebCore::SecurityOrigin>> cfURLCacheOrigins();
95     static void clearCFURLCacheForOrigins(const Vector<WebCore::SecurityOriginData>&);
96 #endif
97
98 #if PLATFORM(COCOA)
99     void clearHSTSCache(WebCore::NetworkStorageSession&, std::chrono::system_clock::time_point modifiedSince);
100 #endif
101
102     void prefetchDNS(const String&);
103
104 private:
105     NetworkProcess();
106     ~NetworkProcess();
107
108     void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
109
110     virtual void terminate() override;
111     void platformTerminate();
112
113     void lowMemoryHandler(WebCore::Critical);
114     void platformLowMemoryHandler(WebCore::Critical);
115
116     // ChildProcess
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;
122
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; }
130
131     // DownloadManager::Client
132     virtual void didCreateDownload() override;
133     virtual void didDestroyDownload() override;
134     virtual IPC::Connection* downloadProxyConnection() override;
135     virtual AuthenticationManager& downloadsAuthenticationManager() override;
136
137     // Message Handlers
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);
144
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);
148
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);
151
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);
160
161 #if USE(SOUP)
162     void setIgnoreTLSErrors(bool);
163     void userPreferredLanguagesChanged(const Vector<String>&);
164 #endif
165
166     // Platform Helpers
167     void platformSetCacheModel(CacheModel);
168
169     // Connections to WebProcesses.
170     Vector<RefPtr<NetworkConnectionToWebProcess>> m_webProcessConnections;
171
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;
178
179     typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
180     NetworkProcessSupplementMap m_supplements;
181
182 #if PLATFORM(COCOA)
183     void platformInitializeNetworkProcessCocoa(const NetworkProcessCreationParameters&);
184
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;
189 #endif
190
191 #if PLATFORM(IOS)
192     WebSQLiteDatabaseTracker m_webSQLiteDatabaseTracker;
193 #endif
194 };
195
196 } // namespace WebKit
197
198 #endif // NetworkProcess_h