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