4f684beec2559b651eaabf50a894521386a21041
[WebKit-https.git] / Source / WebKit / UIProcess / Network / NetworkProcessProxy.h
1 /*
2  * Copyright (C) 2012 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 "APIWebsiteDataStore.h"
29 #include "ChildProcessProxy.h"
30 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
31 #include "LegacyCustomProtocolManagerProxy.h"
32 #endif
33 #include "NetworkProcessProxyMessages.h"
34 #include "ProcessLauncher.h"
35 #include "ProcessThrottler.h"
36 #include "ProcessThrottlerClient.h"
37 #include "UserContentControllerIdentifier.h"
38 #include "WebProcessProxyMessages.h"
39 #include <memory>
40 #include <wtf/Deque.h>
41
42 namespace PAL {
43 class SessionID;
44 }
45
46 namespace WebCore {
47 class AuthenticationChallenge;
48 class ProtectionSpace;
49 class ResourceRequest;
50 enum class ShouldSample : bool;
51 class SecurityOrigin;
52 class URL;
53 struct SecurityOriginData;
54 }
55
56 namespace WebKit {
57
58 class DownloadProxy;
59 class DownloadProxyMap;
60 class WebProcessPool;
61 enum class WebsiteDataFetchOption;
62 enum class WebsiteDataType;
63 struct NetworkProcessCreationParameters;
64 class WebUserContentControllerProxy;
65 struct WebsiteData;
66
67 class NetworkProcessProxy final : public ChildProcessProxy, private ProcessThrottlerClient {
68 public:
69     explicit NetworkProcessProxy(WebProcessPool&);
70     ~NetworkProcessProxy();
71
72     void getNetworkProcessConnection(WebProcessProxy&, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply&&);
73
74     DownloadProxy* createDownloadProxy(const WebCore::ResourceRequest&);
75
76     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, CompletionHandler<void(WebsiteData)>&&);
77     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WallTime modifiedSince, CompletionHandler<void()>&& completionHandler);
78     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebKit::WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, const Vector<String>& HSTSCacheHostNames, CompletionHandler<void()>&&);
79
80 #if ENABLE(RESOURCE_LOAD_STATISTICS)
81     void updatePrevalentDomainsToBlockCookiesFor(PAL::SessionID, const Vector<String>& domainsToBlock, ShouldClearFirst, CompletionHandler<void()>&&);
82     void hasStorageAccessForFrame(PAL::SessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, CompletionHandler<void(bool)>&& callback);
83     void getAllStorageAccessEntries(PAL::SessionID, CompletionHandler<void(Vector<String>&& domains)>&&);
84     void grantStorageAccess(PAL::SessionID, const String& resourceDomain, const String& firstPartyDomain, std::optional<uint64_t> frameID, uint64_t pageID, CompletionHandler<void(bool)>&& callback);
85     void removeAllStorageAccess(PAL::SessionID, CompletionHandler<void()>&&);
86     void setCacheMaxAgeCapForPrevalentResources(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
87     void resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID, CompletionHandler<void()>&&);
88 #endif
89
90     void writeBlobToFilePath(const WebCore::URL&, const String& path, CompletionHandler<void(bool)>&& callback);
91
92     void processReadyToSuspend();
93     
94     void sendProcessDidTransitionToForeground();
95     void sendProcessDidTransitionToBackground();
96
97     void setIsHoldingLockedFiles(bool);
98     
99     void syncAllCookies();
100     void didSyncAllCookies();
101
102     ProcessThrottler& throttler() { return m_throttler; }
103     WebProcessPool& processPool() { return m_processPool; }
104
105 #if ENABLE(CONTENT_EXTENSIONS)
106     void didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy&);
107 #endif
108
109     void addSession(Ref<WebsiteDataStore>&&);
110     void removeSession(PAL::SessionID);
111
112 private:
113     // ChildProcessProxy
114     void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
115     void connectionWillOpen(IPC::Connection&) override;
116     void processWillShutDown(IPC::Connection&) override;
117
118     void networkProcessCrashed();
119     void clearCallbackStates();
120
121     // ProcessThrottlerClient
122     void sendProcessWillSuspendImminently() override;
123     void sendPrepareToSuspend() override;
124     void sendCancelPrepareToSuspend() override;
125     void sendProcessDidResume() override;
126     void didSetAssertionState(AssertionState) override;
127
128     // IPC::Connection::Client
129     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
130     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
131     void didClose(IPC::Connection&) override;
132     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
133
134     // Message handlers
135     void didReceiveNetworkProcessProxyMessage(IPC::Connection&, IPC::Decoder&);
136     void didCreateNetworkConnectionToWebProcess(const IPC::Attachment&);
137     void didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, WebCore::AuthenticationChallenge&&, uint64_t challengeID);
138     void didFetchWebsiteData(uint64_t callbackID, const WebsiteData&);
139     void didDeleteWebsiteData(uint64_t callbackID);
140     void didDeleteWebsiteDataForOrigins(uint64_t callbackID);
141     void didWriteBlobToFilePath(bool success, uint64_t callbackID);
142     void logDiagnosticMessage(uint64_t pageID, const String& message, const String& description, WebCore::ShouldSample);
143     void logDiagnosticMessageWithResult(uint64_t pageID, const String& message, const String& description, uint32_t result, WebCore::ShouldSample);
144     void logDiagnosticMessageWithValue(uint64_t pageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
145 #if ENABLE(RESOURCE_LOAD_STATISTICS)
146     void didUpdateBlockCookies(uint64_t contextId);
147     void storageAccessRequestResult(bool wasGranted, uint64_t contextId);
148     void allStorageAccessEntriesResult(Vector<String>&& domains, uint64_t contextId);
149     void didRemoveAllStorageAccess(uint64_t contextId);
150     void didSetCacheMaxAgeCapForPrevalentResources(uint64_t contextId);
151     void didResetCacheMaxAgeCapForPrevalentResources(uint64_t contextId);
152 #endif
153     void retrieveCacheStorageParameters(PAL::SessionID);
154
155 #if ENABLE(CONTENT_EXTENSIONS)
156     void contentExtensionRules(UserContentControllerIdentifier);
157 #endif
158
159 #if ENABLE(SANDBOX_EXTENSIONS)
160     void getSandboxExtensionsForBlobFiles(const Vector<String>& paths, Messages::NetworkProcessProxy::GetSandboxExtensionsForBlobFiles::AsyncReply&&);
161 #endif
162
163 #if ENABLE(SERVICE_WORKER)
164     void establishWorkerContextConnectionToNetworkProcess(WebCore::SecurityOriginData&&);
165     void establishWorkerContextConnectionToNetworkProcessForExplicitSession(WebCore::SecurityOriginData&&, PAL::SessionID);
166 #endif
167
168     WebsiteDataStore* websiteDataStoreFromSessionID(PAL::SessionID);
169
170     // ProcessLauncher::Client
171     void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
172
173     WebProcessPool& m_processPool;
174     
175     unsigned m_numPendingConnectionRequests;
176     Deque<std::pair<RefPtr<WebProcessProxy>, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>> m_pendingConnectionReplies;
177
178     HashMap<uint64_t, CompletionHandler<void(WebsiteData)>> m_pendingFetchWebsiteDataCallbacks;
179     HashMap<uint64_t, CompletionHandler<void()>> m_pendingDeleteWebsiteDataCallbacks;
180     HashMap<uint64_t, CompletionHandler<void()>> m_pendingDeleteWebsiteDataForOriginsCallbacks;
181
182     std::unique_ptr<DownloadProxyMap> m_downloadProxyMap;
183 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
184     LegacyCustomProtocolManagerProxy m_customProtocolManagerProxy;
185 #endif
186     ProcessThrottler m_throttler;
187     ProcessThrottler::BackgroundActivityToken m_tokenForHoldingLockedFiles;
188     ProcessThrottler::BackgroundActivityToken m_syncAllCookiesToken;
189     
190     unsigned m_syncAllCookiesCounter { 0 };
191
192     HashMap<uint64_t, CompletionHandler<void(bool success)>> m_writeBlobToFilePathCallbackMap;
193     HashMap<uint64_t, CompletionHandler<void()>> m_updateBlockCookiesCallbackMap;
194     HashMap<uint64_t, CompletionHandler<void(bool wasGranted)>> m_storageAccessResponseCallbackMap;
195     HashMap<uint64_t, CompletionHandler<void()>> m_removeAllStorageAccessCallbackMap;
196     HashMap<uint64_t, CompletionHandler<void(Vector<String>&& domains)>> m_allStorageAccessEntriesCallbackMap;
197
198     HashMap<uint64_t, CompletionHandler<void()>> m_updateRuntimeSettingsCallbackMap;
199
200 #if ENABLE(CONTENT_EXTENSIONS)
201     HashSet<WebUserContentControllerProxy*> m_webUserContentControllerProxies;
202 #endif
203
204     HashMap<PAL::SessionID, RefPtr<WebsiteDataStore>> m_websiteDataStores;
205 };
206
207 } // namespace WebKit