1/*
2 * Copyright (C) 2012-2019 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 "AuxiliaryProcessProxy.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 <WebCore/CrossSiteNavigationDataTransfer.h>
40#include <WebCore/RegistrableDomain.h>
41#include <memory>
42#include <wtf/Deque.h>
43
44namespace PAL {
45class SessionID;
46}
47
48namespace WebCore {
49class AuthenticationChallenge;
50class ProtectionSpace;
51class ResourceRequest;
52enum class ShouldSample : bool;
53enum class StorageAccessPromptWasShown : bool;
54enum class StorageAccessWasGranted : bool;
55class SecurityOrigin;
56struct SecurityOriginData;
57}
58
59namespace WebKit {
60
61class DownloadProxy;
62class DownloadProxyMap;
63class WebProcessPool;
64enum class ShouldGrandfatherStatistics : bool;
65enum class StorageAccessStatus : uint8_t;
66enum class WebsiteDataFetchOption;
67enum class WebsiteDataType;
68struct NetworkProcessCreationParameters;
69class WebUserContentControllerProxy;
70struct WebsiteData;
71
72class NetworkProcessProxy final : public AuxiliaryProcessProxy, private ProcessThrottlerClient, public CanMakeWeakPtr<NetworkProcessProxy> {
73public:
74 using RegistrableDomain = WebCore::RegistrableDomain;
75 using TopFrameDomain = WebCore::RegistrableDomain;
76 using SubFrameDomain = WebCore::RegistrableDomain;
77 using SubResourceDomain = WebCore::RegistrableDomain;
78 using RedirectDomain = WebCore::RegistrableDomain;
79 using RedirectedFromDomain = WebCore::RegistrableDomain;
80 using RedirectedToDomain = WebCore::RegistrableDomain;
81 using NavigatedFromDomain = WebCore::RegistrableDomain;
82 using NavigatedToDomain = WebCore::RegistrableDomain;
83 using DomainInNeedOfStorageAccess = WebCore::RegistrableDomain;
84 using OpenerDomain = WebCore::RegistrableDomain;
85 using FrameID = uint64_t;
86
87 explicit NetworkProcessProxy(WebProcessPool&);
88 ~NetworkProcessProxy();
89
90 void getNetworkProcessConnection(WebProcessProxy&, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply&&);
91
92 DownloadProxy& createDownloadProxy(const WebCore::ResourceRequest&);
93
94 void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, CompletionHandler<void(WebsiteData)>&&);
95 void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WallTime modifiedSince, CompletionHandler<void()>&& completionHandler);
96 void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebKit::WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, const Vector<String>& HSTSCacheHostNames, CompletionHandler<void()>&&);
97
98 void getLocalStorageDetails(PAL::SessionID, CompletionHandler<void(Vector<LocalStorageDatabaseTracker::OriginDetails>&&)>&&);
99
100#if ENABLE(RESOURCE_LOAD_STATISTICS)
101 void clearPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
102 void clearUserInteraction(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
103 void dumpResourceLoadStatistics(PAL::SessionID, CompletionHandler<void(String)>&&);
104 void updatePrevalentDomainsToBlockCookiesFor(PAL::SessionID, const Vector<RegistrableDomain>&, CompletionHandler<void()>&&);
105 void hasHadUserInteraction(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
106 void hasLocalStorage(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
107 void isGrandfathered(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
108 void isPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
109 void isRegisteredAsRedirectingTo(PAL::SessionID, const RedirectedFromDomain&, const RedirectedToDomain&, CompletionHandler<void(bool)>&&);
110 void isRegisteredAsSubFrameUnder(PAL::SessionID, const SubFrameDomain&, const TopFrameDomain&, CompletionHandler<void(bool)>&&);
111 void isRegisteredAsSubresourceUnder(PAL::SessionID, const SubResourceDomain&, const TopFrameDomain&, CompletionHandler<void(bool)>&&);
112 void isVeryPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void(bool)>&&);
113 void logUserInteraction(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
114 void scheduleStatisticsAndDataRecordsProcessing(PAL::SessionID, CompletionHandler<void()>&&);
115 void setLastSeen(PAL::SessionID, const RegistrableDomain&, Seconds, CompletionHandler<void()>&&);
116 void setAgeCapForClientSideCookies(PAL::SessionID, Optional<Seconds>, CompletionHandler<void()>&&);
117 void setCacheMaxAgeCap(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
118 void setGrandfathered(PAL::SessionID, const RegistrableDomain&, bool isGrandfathered, CompletionHandler<void()>&&);
119 void setNotifyPagesWhenDataRecordsWereScanned(PAL::SessionID, bool, CompletionHandler<void()>&&);
120 void setIsRunningResourceLoadStatisticsTest(PAL::SessionID, bool, CompletionHandler<void()>&&);
121 void setNotifyPagesWhenTelemetryWasCaptured(PAL::SessionID, bool, CompletionHandler<void()>&&);
122 void setSubframeUnderTopFrameDomain(PAL::SessionID, const SubFrameDomain&, const TopFrameDomain&, CompletionHandler<void()>&&);
123 void setSubresourceUnderTopFrameDomain(PAL::SessionID, const SubResourceDomain&, const TopFrameDomain&, CompletionHandler<void()>&&);
124 void setSubresourceUniqueRedirectTo(PAL::SessionID, const SubResourceDomain&, const RedirectedToDomain&, CompletionHandler<void()>&&);
125 void setSubresourceUniqueRedirectFrom(PAL::SessionID, const SubResourceDomain&, const RedirectedFromDomain&, CompletionHandler<void()>&&);
126 void setTimeToLiveUserInteraction(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
127 void setTopFrameUniqueRedirectTo(PAL::SessionID, const TopFrameDomain&, const RedirectedToDomain&, CompletionHandler<void()>&&);
128 void setTopFrameUniqueRedirectFrom(PAL::SessionID, const TopFrameDomain&, const RedirectedFromDomain&, CompletionHandler<void()>&&);
129 void setPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
130 void setPrevalentResourceForDebugMode(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
131 void setVeryPrevalentResource(PAL::SessionID, const RegistrableDomain&, CompletionHandler<void()>&&);
132 void getAllStorageAccessEntries(PAL::SessionID, CompletionHandler<void(Vector<String> domains)>&&);
133 void requestStorageAccessConfirm(WebCore::PageIdentifier, FrameID, const SubFrameDomain&, const TopFrameDomain&, CompletionHandler<void(bool)>&&);
134 void resetParametersToDefaultValues(PAL::SessionID, CompletionHandler<void()>&&);
135 void scheduleClearInMemoryAndPersistent(PAL::SessionID, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
136 void scheduleClearInMemoryAndPersistent(PAL::SessionID, Optional<WallTime> modifiedSince, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
137 void scheduleCookieBlockingUpdate(PAL::SessionID, CompletionHandler<void()>&&);
138 void submitTelemetry(PAL::SessionID, CompletionHandler<void()>&&);
139 void setCacheMaxAgeCapForPrevalentResources(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
140 void setGrandfatheringTime(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
141 void setMaxStatisticsEntries(PAL::SessionID, size_t maximumEntryCount, CompletionHandler<void()>&&);
142 void setMinimumTimeBetweenDataRecordsRemoval(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
143 void setPruneEntriesDownTo(PAL::SessionID, size_t pruneTargetCount, CompletionHandler<void()>&&);
144 void setResourceLoadStatisticsDebugMode(PAL::SessionID, bool debugMode, CompletionHandler<void()>&&);
145 void setShouldClassifyResourcesBeforeDataRecordsRemoval(PAL::SessionID, bool, CompletionHandler<void()>&&);
146 void resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID, CompletionHandler<void()>&&);
147 void didCommitCrossSiteLoadWithDataTransfer(PAL::SessionID, const NavigatedFromDomain&, const NavigatedToDomain&, OptionSet<WebCore::CrossSiteNavigationDataTransfer::Flag>, WebCore::PageIdentifier);
148 void didCommitCrossSiteLoadWithDataTransferFromPrevalentResource(WebCore::PageIdentifier);
149 void setCrossSiteLoadWithLinkDecorationForTesting(PAL::SessionID, const NavigatedFromDomain&, const NavigatedToDomain&, CompletionHandler<void()>&&);
150 void resetCrossSiteLoadsWithLinkDecorationForTesting(PAL::SessionID, CompletionHandler<void()>&&);
151 void deleteCookiesForTesting(PAL::SessionID, const RegistrableDomain&, bool includeHttpOnlyCookies, CompletionHandler<void()>&&);
152 void deleteWebsiteDataInUIProcessForRegistrableDomains(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, Vector<RegistrableDomain>, CompletionHandler<void(HashSet<WebCore::RegistrableDomain>&&)>&&);
153#endif
154
155 void processReadyToSuspend();
156
157 void sendProcessDidTransitionToForeground();
158 void sendProcessDidTransitionToBackground();
159 void synthesizeAppIsBackground(bool background);
160
161 void setIsHoldingLockedFiles(bool);
162
163 void syncAllCookies();
164 void didSyncAllCookies();
165
166 ProcessThrottler& throttler() { return m_throttler; }
167 WebProcessPool& processPool() { return m_processPool; }
168
169#if ENABLE(CONTENT_EXTENSIONS)
170 void didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy&);
171#endif
172
173 void addSession(Ref<WebsiteDataStore>&&);
174 void removeSession(PAL::SessionID);
175
176 void takeUploadAssertion();
177 void clearUploadAssertion();
178
179#if ENABLE(INDEXED_DATABASE)
180 void createSymLinkForFileUpgrade(const String& indexedDatabaseDirectory);
181#endif
182
183 // ProcessThrottlerClient
184 void sendProcessWillSuspendImminently() final;
185 void sendProcessDidResume() final;
186
187private:
188 // AuxiliaryProcessProxy
189 void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
190 void connectionWillOpen(IPC::Connection&) override;
191 void processWillShutDown(IPC::Connection&) override;
192
193 void networkProcessCrashed();
194 void clearCallbackStates();
195
196 // ProcessThrottlerClient
197 void sendPrepareToSuspend() final;
198 void sendCancelPrepareToSuspend() final;
199 void didSetAssertionState(AssertionState) final;
200
201 // IPC::Connection::Client
202 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
203 void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
204 void didClose(IPC::Connection&) override;
205 void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
206
207 // Message handlers
208 void didReceiveNetworkProcessProxyMessage(IPC::Connection&, IPC::Decoder&);
209 void didCreateNetworkConnectionToWebProcess(const IPC::Attachment&);
210 void didReceiveAuthenticationChallenge(WebCore::PageIdentifier, uint64_t frameID, WebCore::AuthenticationChallenge&&, uint64_t challengeID);
211 void didFetchWebsiteData(uint64_t callbackID, const WebsiteData&);
212 void didDeleteWebsiteData(uint64_t callbackID);
213 void didDeleteWebsiteDataForOrigins(uint64_t callbackID);
214 void logDiagnosticMessage(WebCore::PageIdentifier, const String& message, const String& description, WebCore::ShouldSample);
215 void logDiagnosticMessageWithResult(WebCore::PageIdentifier, const String& message, const String& description, uint32_t result, WebCore::ShouldSample);
216 void logDiagnosticMessageWithValue(WebCore::PageIdentifier, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
217 void logGlobalDiagnosticMessageWithValue(const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
218#if ENABLE(RESOURCE_LOAD_STATISTICS)
219 void logTestingEvent(PAL::SessionID, const String& event);
220 void notifyResourceLoadStatisticsProcessed();
221 void notifyWebsiteDataDeletionForRegistrableDomainsFinished();
222 void notifyWebsiteDataScanForRegistrableDomainsFinished();
223 void notifyResourceLoadStatisticsTelemetryFinished(unsigned totalPrevalentResources, unsigned totalPrevalentResourcesWithUserInteraction, unsigned top3SubframeUnderTopFrameOrigins);
224#endif
225 void retrieveCacheStorageParameters(PAL::SessionID);
226
227#if ENABLE(CONTENT_EXTENSIONS)
228 void contentExtensionRules(UserContentControllerIdentifier);
229#endif
230
231#if ENABLE(SANDBOX_EXTENSIONS)
232 void getSandboxExtensionsForBlobFiles(const Vector<String>& paths, Messages::NetworkProcessProxy::GetSandboxExtensionsForBlobFiles::AsyncReply&&);
233#endif
234
235#if ENABLE(SERVICE_WORKER)
236 void establishWorkerContextConnectionToNetworkProcess(WebCore::RegistrableDomain&&);
237 void establishWorkerContextConnectionToNetworkProcessForExplicitSession(WebCore::RegistrableDomain&&, PAL::SessionID);
238#endif
239
240 void requestStorageSpace(PAL::SessionID, const WebCore::ClientOrigin&, uint64_t quota, uint64_t currentSize, uint64_t spaceRequired, CompletionHandler<void(Optional<uint64_t> quota)>&&);
241
242 WebsiteDataStore* websiteDataStoreFromSessionID(PAL::SessionID);
243
244 // ProcessLauncher::Client
245 void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
246
247 WebProcessPool& m_processPool;
248
249 unsigned m_numPendingConnectionRequests;
250 Deque<std::pair<WeakPtr<WebProcessProxy>, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>> m_pendingConnectionReplies;
251
252 HashMap<uint64_t, CompletionHandler<void(WebsiteData)>> m_pendingFetchWebsiteDataCallbacks;
253 HashMap<uint64_t, CompletionHandler<void()>> m_pendingDeleteWebsiteDataCallbacks;
254 HashMap<uint64_t, CompletionHandler<void()>> m_pendingDeleteWebsiteDataForOriginsCallbacks;
255
256 std::unique_ptr<DownloadProxyMap> m_downloadProxyMap;
257#if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
258 LegacyCustomProtocolManagerProxy m_customProtocolManagerProxy;
259#endif
260 ProcessThrottler m_throttler;
261 ProcessThrottler::BackgroundActivityToken m_tokenForHoldingLockedFiles;
262 ProcessThrottler::BackgroundActivityToken m_syncAllCookiesToken;
263
264 unsigned m_syncAllCookiesCounter { 0 };
265
266#if ENABLE(CONTENT_EXTENSIONS)
267 HashSet<WebUserContentControllerProxy*> m_webUserContentControllerProxies;
268#endif
269
270 HashMap<PAL::SessionID, RefPtr<WebsiteDataStore>> m_websiteDataStores;
271
272 std::unique_ptr<ProcessAssertion> m_uploadAssertion;
273};
274
275} // namespace WebKit
276