StorageAccess API calls should be direct to the Network Process
[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 requestStorageAccessConfirm(uint64_t pageID, uint64_t frameID, const String& subFrameHost, const String& topFrameHost, CompletionHandler<void(bool)>&&);
117     void resetParametersToDefaultValues(PAL::SessionID, CompletionHandler<void()>&&);
118     void removeAllStorageAccess(PAL::SessionID, CompletionHandler<void()>&&);
119     void scheduleClearInMemoryAndPersistent(PAL::SessionID, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
120     void scheduleClearInMemoryAndPersistent(PAL::SessionID, Optional<WallTime> modifiedSince, ShouldGrandfatherStatistics, CompletionHandler<void()>&&);
121     void scheduleCookieBlockingUpdate(PAL::SessionID, CompletionHandler<void()>&&);
122     void submitTelemetry(PAL::SessionID, CompletionHandler<void()>&&);
123     void setCacheMaxAgeCapForPrevalentResources(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
124     void setGrandfatheringTime(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
125     void setMaxStatisticsEntries(PAL::SessionID, size_t maximumEntryCount, CompletionHandler<void()>&&);
126     void setMinimumTimeBetweenDataRecordsRemoval(PAL::SessionID, Seconds, CompletionHandler<void()>&&);
127     void setPruneEntriesDownTo(PAL::SessionID, size_t pruneTargetCount, CompletionHandler<void()>&&);
128     void setResourceLoadStatisticsDebugMode(PAL::SessionID, bool debugMode, CompletionHandler<void()>&&);
129     void setShouldClassifyResourcesBeforeDataRecordsRemoval(PAL::SessionID, bool, CompletionHandler<void()>&&);
130     void resetCacheMaxAgeCapForPrevalentResources(PAL::SessionID, CompletionHandler<void()>&&);
131 #endif
132
133     void writeBlobToFilePath(const URL&, const String& path, CompletionHandler<void(bool)>&&);
134
135     void processReadyToSuspend();
136     
137     void sendProcessDidTransitionToForeground();
138     void sendProcessDidTransitionToBackground();
139
140     void setIsHoldingLockedFiles(bool);
141     
142     void syncAllCookies();
143     void didSyncAllCookies();
144
145     ProcessThrottler& throttler() { return m_throttler; }
146     WebProcessPool& processPool() { return m_processPool; }
147
148 #if ENABLE(CONTENT_EXTENSIONS)
149     void didDestroyWebUserContentControllerProxy(WebUserContentControllerProxy&);
150 #endif
151
152     void addSession(Ref<WebsiteDataStore>&&);
153     void removeSession(PAL::SessionID);
154
155 private:
156     // ChildProcessProxy
157     void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
158     void connectionWillOpen(IPC::Connection&) override;
159     void processWillShutDown(IPC::Connection&) override;
160
161     void networkProcessCrashed();
162     void clearCallbackStates();
163
164     // ProcessThrottlerClient
165     void sendProcessWillSuspendImminently() override;
166     void sendPrepareToSuspend() override;
167     void sendCancelPrepareToSuspend() override;
168     void sendProcessDidResume() override;
169     void didSetAssertionState(AssertionState) override;
170
171     // IPC::Connection::Client
172     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
173     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
174     void didClose(IPC::Connection&) override;
175     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
176
177     // Message handlers
178     void didReceiveNetworkProcessProxyMessage(IPC::Connection&, IPC::Decoder&);
179     void didCreateNetworkConnectionToWebProcess(const IPC::Attachment&);
180     void didReceiveAuthenticationChallenge(uint64_t pageID, uint64_t frameID, WebCore::AuthenticationChallenge&&, uint64_t challengeID);
181     void didFetchWebsiteData(uint64_t callbackID, const WebsiteData&);
182     void didDeleteWebsiteData(uint64_t callbackID);
183     void didDeleteWebsiteDataForOrigins(uint64_t callbackID);
184     void logDiagnosticMessage(uint64_t pageID, const String& message, const String& description, WebCore::ShouldSample);
185     void logDiagnosticMessageWithResult(uint64_t pageID, const String& message, const String& description, uint32_t result, WebCore::ShouldSample);
186     void logDiagnosticMessageWithValue(uint64_t pageID, const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
187     void logGlobalDiagnosticMessageWithValue(const String& message, const String& description, double value, unsigned significantFigures, WebCore::ShouldSample);
188 #if ENABLE(RESOURCE_LOAD_STATISTICS)
189     void logTestingEvent(PAL::SessionID, const String& event);
190     void notifyResourceLoadStatisticsProcessed();
191     void notifyWebsiteDataDeletionForTopPrivatelyOwnedDomainsFinished();
192     void notifyWebsiteDataScanForTopPrivatelyControlledDomainsFinished();
193     void notifyResourceLoadStatisticsTelemetryFinished(unsigned totalPrevalentResources, unsigned totalPrevalentResourcesWithUserInteraction, unsigned top3SubframeUnderTopFrameOrigins);
194 #endif
195     void retrieveCacheStorageParameters(PAL::SessionID);
196
197 #if ENABLE(CONTENT_EXTENSIONS)
198     void contentExtensionRules(UserContentControllerIdentifier);
199 #endif
200
201 #if ENABLE(SANDBOX_EXTENSIONS)
202     void getSandboxExtensionsForBlobFiles(const Vector<String>& paths, Messages::NetworkProcessProxy::GetSandboxExtensionsForBlobFiles::AsyncReply&&);
203 #endif
204
205 #if ENABLE(SERVICE_WORKER)
206     void establishWorkerContextConnectionToNetworkProcess(WebCore::SecurityOriginData&&);
207     void establishWorkerContextConnectionToNetworkProcessForExplicitSession(WebCore::SecurityOriginData&&, PAL::SessionID);
208 #endif
209
210     void requestCacheStorageSpace(PAL::SessionID, const WebCore::ClientOrigin&, uint64_t quota, uint64_t currentSize, uint64_t spaceRequired, CompletionHandler<void(Optional<uint64_t> quota)>&&);
211
212     WebsiteDataStore* websiteDataStoreFromSessionID(PAL::SessionID);
213
214     // ProcessLauncher::Client
215     void didFinishLaunching(ProcessLauncher*, IPC::Connection::Identifier) override;
216
217     WebProcessPool& m_processPool;
218     
219     unsigned m_numPendingConnectionRequests;
220     Deque<std::pair<WeakPtr<WebProcessProxy>, Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply>> m_pendingConnectionReplies;
221
222     HashMap<uint64_t, CompletionHandler<void(WebsiteData)>> m_pendingFetchWebsiteDataCallbacks;
223     HashMap<uint64_t, CompletionHandler<void()>> m_pendingDeleteWebsiteDataCallbacks;
224     HashMap<uint64_t, CompletionHandler<void()>> m_pendingDeleteWebsiteDataForOriginsCallbacks;
225
226     std::unique_ptr<DownloadProxyMap> m_downloadProxyMap;
227 #if ENABLE(LEGACY_CUSTOM_PROTOCOL_MANAGER)
228     LegacyCustomProtocolManagerProxy m_customProtocolManagerProxy;
229 #endif
230     ProcessThrottler m_throttler;
231     ProcessThrottler::BackgroundActivityToken m_tokenForHoldingLockedFiles;
232     ProcessThrottler::BackgroundActivityToken m_syncAllCookiesToken;
233     
234     unsigned m_syncAllCookiesCounter { 0 };
235
236 #if ENABLE(CONTENT_EXTENSIONS)
237     HashSet<WebUserContentControllerProxy*> m_webUserContentControllerProxies;
238 #endif
239
240     HashMap<PAL::SessionID, RefPtr<WebsiteDataStore>> m_websiteDataStores;
241 };
242
243 } // namespace WebKit