dc5272c8b963d090f20228c55cbb85e00702784b
[WebKit-https.git] / Source / WebKit / UIProcess / Network / NetworkProcessProxy.h
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 "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 struct SecurityOriginData;
53 }
54
55 namespace WebKit {
56
57 class DownloadProxy;
58 class DownloadProxyMap;
59 class WebProcessPool;
60 enum class ShouldGrandfatherStatistics : bool;
61 enum class StorageAccessStatus : uint8_t;
62 enum class WebsiteDataFetchOption;
63 enum class WebsiteDataType;
64 struct NetworkProcessCreationParameters;
65 class WebUserContentControllerProxy;
66 struct WebsiteData;
67
68 class NetworkProcessProxy final : public ChildProcessProxy, private ProcessThrottlerClient {
69 public:
70     explicit NetworkProcessProxy(WebProcessPool&);
71     ~NetworkProcessProxy();
72
73     void getNetworkProcessConnection(WebProcessProxy&, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply&&);
74
75     DownloadProxy* createDownloadProxy(const WebCore::ResourceRequest&);
76
77     void fetchWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, OptionSet<WebsiteDataFetchOption>, CompletionHandler<void(WebsiteData)>&&);
78     void deleteWebsiteData(PAL::SessionID, OptionSet<WebsiteDataType>, WallTime modifiedSince, CompletionHandler<void()>&& completionHandler);
79     void deleteWebsiteDataForOrigins(PAL::SessionID, OptionSet<WebKit::WebsiteDataType>, const Vector<WebCore::SecurityOriginData>& origins, const Vector<String>& cookieHostNames, const Vector<String>& HSTSCacheHostNames, CompletionHandler<void()>&&);
80
81 #if ENABLE(RESOURCE_LOAD_STATISTICS)
82     void clearPrevalentResource(PAL::SessionID, const String& resourceDomain, CompletionHandler<void()>&&);
83     void clearUserInteraction(PAL::SessionID, const String& resourceDomain, CompletionHandler<void()>&&);
84     void dumpResourceLoadStatistics(PAL::SessionID, CompletionHandler<void(String)>&&);
85     void updatePrevalentDomainsToBlockCookiesFor(PAL::SessionID, const Vector<String>& domainsToBlock, CompletionHandler<void()>&&);
86     void hasHadUserInteraction(PAL::SessionID, const String& resourceDomain, CompletionHandler<void(bool)>&&);
87     void isGrandfathered(PAL::SessionID, const String& resourceDomain, CompletionHandler<void(bool)>&&);
88     void isPrevalentResource(PAL::SessionID, const String& resourceDomain, CompletionHandler<void(bool)>&&);
89     void isRegisteredAsRedirectingTo(PAL::SessionID, const String& redirectedFrom, const String& redirectedTo, CompletionHandler<void(bool)>&&);
90     void isRegisteredAsSubFrameUnder(PAL::SessionID, const String& subFrame, const String& topFrame, CompletionHandler<void(bool)>&&);
91     void isRegisteredAsSubresourceUnder(PAL::SessionID, const String& subresource, const String& topFrame, CompletionHandler<void(bool)>&&);
92     void isVeryPrevalentResource(PAL::SessionID, const String& resourceDomain, CompletionHandler<void(bool)>&&);
93     void logUserInteraction(PAL::SessionID, const String& resourceDomain, CompletionHandler<void()>&&);
94     void scheduleStatisticsAndDataRecordsProcessing(PAL::SessionID, CompletionHandler<void()>&&);
95     void setLastSeen(PAL::SessionID, const String& resourceDomain, Seconds, CompletionHandler<void()>&&);
96     void setAgeCapForClientSideCookies(PAL::SessionID, Optional<Seconds>, CompletionHandler<void()>&&);
97     void setCacheMaxAgeCap(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
98     void setGrandfathered(PAL::SessionID, const String& resourceDomain, bool isGrandfathered, CompletionHandler<void()>&&);
99     void setNotifyPagesWhenDataRecordsWereScanned(PAL::SessionID, bool, CompletionHandler<void()>&&);
100     void setNotifyPagesWhenTelemetryWasCaptured(PAL::SessionID, bool, CompletionHandler<void()>&&);
101     void setSubframeUnderTopFrameOrigin(PAL::SessionID, const String& subframe, const String& topFrame, CompletionHandler<void()>&&);
102     void setSubresourceUnderTopFrameOrigin(PAL::SessionID, const String& subresource, const String& topFrame, CompletionHandler<void()>&&);
103     void setSubresourceUniqueRedirectTo(PAL::SessionID, const String& subresource, const String& hostNameRedirectedTo, CompletionHandler<void()>&&);
104     void setSubresourceUniqueRedirectFrom(PAL::SessionID, const String& subresource, const String& hostNameRedirectedFrom, CompletionHandler<void()>&&);
105     void setTimeToLiveUserInteraction(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
106     void setTopFrameUniqueRedirectTo(PAL::SessionID, const String& topFrameHostName, const String& hostNameRedirectedTo, CompletionHandler<void()>&&);
107     void setTopFrameUniqueRedirectFrom(PAL::SessionID, const String& topFrameHostName, const String& hostNameRedirectedFrom, CompletionHandler<void()>&&);
108     void setPrevalentResource(PAL::SessionID, const String& resourceDomain, CompletionHandler<void()>&&);
109     void setPrevalentResourceForDebugMode(PAL::SessionID, const String& resourceDomain, CompletionHandler<void()>&&);
110     void setVeryPrevalentResource(PAL::SessionID, const String& resourceDomain, CompletionHandler<void()>&&);
111     void hasStorageAccessForFrame(PAL::SessionID, const String& resourceDomain, const String& firstPartyDomain, uint64_t frameID, uint64_t pageID, CompletionHandler<void(bool)>&&);
112     void getAllStorageAccessEntries(PAL::SessionID, CompletionHandler<void(Vector<String> domains)>&&);
113     void grantStorageAccess(PAL::SessionID, const String& resourceDomain, const String& firstPartyDomain, Optional<uint64_t> frameID, uint64_t pageID, bool userWasPrompted, CompletionHandler<void(bool)>&&);
114     void hasStorageAccess(PAL::SessionID, const String& subFrameHost, const String& topFrameHost, Optional<uint64_t> frameID, uint64_t pageID, CompletionHandler<void(bool)>&&);
115     void requestStorageAccess(PAL::SessionID, const String& resourceDomain, const String& firstPartyDomain, Optional<uint64_t> frameID, uint64_t pageID, bool promptEnabled, CompletionHandler<void(StorageAccessStatus)>&&);
116     void resetParametersToDefaultValues(PAL::SessionID, CompletionHandler<void()>&&);
117     void removeAllStorageAccess(PAL::SessionID, CompletionHandler<void()>&&);
118     void scheduleClearInMemoryAndPersistent(PAL::SessionID, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
119     void scheduleClearInMemoryAndPersistent(PAL::SessionID, Optional<WallTime> modifiedSince, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
120     void scheduleCookieBlockingUpdate(PAL::SessionID, CompletionHandler<void()>&&);
121     void submitTelemetry(PAL::SessionID, CompletionHandler<void()>&&);
122     void setCacheMaxAgeCapForPrevalentResources(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
123     void setGrandfatheringTime(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
124     void setMaxStatisticsEntries(PAL::SessionID, size_t maximumEntryCount, CompletionHandler<void()>&&);
125     void setMinimumTimeBetweenDataRecordsRemoval(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
126     void setPruneEntriesDownTo(PAL::SessionID, size_t pruneTargetCount, CompletionHandler<void()>&&);
127     void setResourceLoadStatisticsDebugMode(PAL::SessionID, bool debugMode, CompletionHandler<void()>&&);
128     void setShouldClassifyResourcesBeforeDataRecordsRemoval(PAL::SessionID, bool, CompletionHandler<void()>&&);
129     void resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID, CompletionHandler<void()>&&);
130 #endif
131
132     void writeBlobToFilePath(const URL&, const String& path, CompletionHandler<void(bool)>&&);
133
134     void processReadyToSuspend();
135     
136     void sendProcessDidTransitionToForeground();
137     void sendProcessDidTransitionToBackground();
138
139     void setIsHoldingLockedFiles(bool);
140     
141     void syncAllCookies();
142     void didSyncAllCookies();
143
144     ProcessThrottler& throttler() { return m_throttler; }
145     WebProcessPool& processPool() { return m_processPool; }
146
147 #if ENABLE(CONTENT_EXTENSIONS)
148     void didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy&);
149 #endif
150
151     void addSession(Ref<WebsiteDataStore>&&);
152     void removeSession(PAL::SessionID);
153
154 private:
155     // ChildProcessProxy
156     void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
157     void connectionWillOpen(IPC::Connection&) override;
158     void processWillShutDown(IPC::Connection&) override;
159
160     void networkProcessCrashed();
161     void clearCallbackStates();
162
163     // ProcessThrottlerClient
164     void sendProcessWillSuspendImminently() override;
165     void sendPrepareToSuspend() override;
166     void sendCancelPrepareToSuspend() override;
167     void sendProcessDidResume() override;
168     void didSetAssertionState(AssertionState) override;
169
170     // IPC::Connection::Client
171     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
172     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
173     void didClose(IPC::Connection&) override;
174     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
175
176     // Message handlers
177     void didReceiveNetworkProcessProxyMessage(IPC::Connection&, IPC::Decoder&);
178     void didCreateNetworkConnectionToWebProcess(const IPC::Attachment&);
179     void didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, WebCore::AuthenticationChallenge&&, uint64_t challengeID);
180     void didFetchWebsiteData(uint64_t callbackID, const WebsiteData&);
181     void didDeleteWebsiteData(uint64_t callbackID);
182     void didDeleteWebsiteDataForOrigins(uint64_t callbackID);
183     void logDiagnosticMessage(uint64_t pageID, const String& message, const String& description, WebCore::ShouldSample);
184     void logDiagnosticMessageWithResult(uint64_t pageID, const String& message, const String& description, uint32_t result, WebCore::ShouldSample);
185     void logDiagnosticMessageWithValue(uint64_t pageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
186     void logGlobalDiagnosticMessageWithValue(const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
187 #if ENABLE(RESOURCE_LOAD_STATISTICS)
188     void logTestingEvent(PAL::SessionID, const String& event);
189     void notifyResourceLoadStatisticsProcessed();
190     void notifyWebsiteDataDeletionForTopPrivatelyOwnedDomainsFinished();
191     void notifyWebsiteDataScanForTopPrivatelyControlledDomainsFinished();
192     void notifyResourceLoadStatisticsTelemetryFinished(unsigned totalPrevalentResources, unsigned totalPrevalentResourcesWithUserInteraction, unsigned top3SubframeUnderTopFrameOrigins);
193 #endif
194     void retrieveCacheStorageParameters(PAL::SessionID);
195
196 #if ENABLE(CONTENT_EXTENSIONS)
197     void contentExtensionRules(UserContentControllerIdentifier);
198 #endif
199
200 #if ENABLE(SANDBOX_EXTENSIONS)
201     void getSandboxExtensionsForBlobFiles(const Vector<String>& paths, Messages::NetworkProcessProxy::GetSandboxExtensionsForBlobFiles::AsyncReply&&);
202 #endif
203
204 #if ENABLE(SERVICE_WORKER)
205     void establishWorkerContextConnectionToNetworkProcess(WebCore::SecurityOriginData&&);
206     void establishWorkerContextConnectionToNetworkProcessForExplicitSession(WebCore::SecurityOriginData&&, PAL::SessionID);
207 #endif
208
209     void requestCacheStorageSpace(PAL::SessionID, const WebCore::ClientOrigin&, uint64_t quota, uint64_t currentSize, uint64_t spaceRequired, CompletionHandler<void(Optional<uint64_t> quota)>&&);
210
211     WebsiteDataStore* websiteDataStoreFromSessionID(PAL::SessionID);
212
213     // ProcessLauncher::Client
214     void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
215
216     WebProcessPool& m_processPool;
217     
218     unsigned m_numPendingConnectionRequests;
219     Deque<std::pair<WeakPtr<WebProcessProxy>, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>> m_pendingConnectionReplies;
220
221     HashMap<uint64_t, CompletionHandler<void(WebsiteData)>> m_pendingFetchWebsiteDataCallbacks;
222     HashMap<uint64_t, CompletionHandler<void()>> m_pendingDeleteWebsiteDataCallbacks;
223     HashMap<uint64_t, CompletionHandler<void()>> m_pendingDeleteWebsiteDataForOriginsCallbacks;
224
225     std::unique_ptr<DownloadProxyMap> m_downloadProxyMap;
226 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
227     LegacyCustomProtocolManagerProxy m_customProtocolManagerProxy;
228 #endif
229     ProcessThrottler m_throttler;
230     ProcessThrottler::BackgroundActivityToken m_tokenForHoldingLockedFiles;
231     ProcessThrottler::BackgroundActivityToken m_syncAllCookiesToken;
232     
233     unsigned m_syncAllCookiesCounter { 0 };
234
235 #if ENABLE(CONTENT_EXTENSIONS)
236     HashSet<WebUserContentControllerProxy*> m_webUserContentControllerProxies;
237 #endif
238
239     HashMap<PAL::SessionID, RefPtr<WebsiteDataStore>> m_websiteDataStores;
240 };
241
242 } // namespace WebKit