Use normal loading path for ping loads
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkConnectionToWebProcess.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 "CacheStorageEngineConnection.h"
29 #include "Connection.h"
30 #include "DownloadID.h"
31 #include "NetworkActivityTracker.h"
32 #include "NetworkConnectionToWebProcessMessages.h"
33 #include "NetworkMDNSRegister.h"
34 #include "NetworkRTCProvider.h"
35 #include "NetworkResourceLoadMap.h"
36 #include "WebPaymentCoordinatorProxy.h"
37 #include <WebCore/NetworkLoadInformation.h>
38 #include <WebCore/ProcessIdentifier.h>
39 #include <WebCore/RegistrableDomain.h>
40 #include <wtf/RefCounted.h>
41
42 namespace PAL {
43 class SessionID;
44 }
45
46 namespace WebCore {
47 class BlobDataFileReference;
48 class BlobRegistryImpl;
49 class ResourceError;
50 class ResourceRequest;
51 struct SameSiteInfo;
52
53 enum class IncludeSecureCookies : bool;
54 }
55
56 namespace WebKit {
57
58 class NetworkProcess;
59 class NetworkResourceLoader;
60 class NetworkSocketStream;
61 class WebIDBConnectionToClient;
62 class WebSWServerConnection;
63 typedef uint64_t ResourceLoadIdentifier;
64
65 namespace NetworkCache {
66 struct DataKey;
67 }
68
69 class NetworkConnectionToWebProcess
70     : public RefCounted<NetworkConnectionToWebProcess>
71 #if ENABLE(APPLE_PAY_REMOTE_UI)
72     , public WebPaymentCoordinatorProxy::Client
73 #endif
74     , IPC::Connection::Client {
75 public:
76     using RegistrableDomain = WebCore::RegistrableDomain;
77
78     static Ref<NetworkConnectionToWebProcess> create(NetworkProcess&, IPC::Connection::Identifier);
79     virtual ~NetworkConnectionToWebProcess();
80
81     IPC::Connection& connection() { return m_connection.get(); }
82     NetworkProcess& networkProcess() { return m_networkProcess.get(); }
83
84     void didCleanupResourceLoader(NetworkResourceLoader&);
85     void transferKeptAliveLoad(NetworkResourceLoader&);
86     void setOnLineState(bool);
87
88     bool captureExtraNetworkLoadMetricsEnabled() const { return m_captureExtraNetworkLoadMetricsEnabled; }
89
90     RefPtr<WebCore::BlobDataFileReference> getBlobDataFileReferenceForPath(const String& path);
91
92     void cleanupForSuspension(Function<void()>&&);
93     void endSuspension();
94
95     void getNetworkLoadInformationRequest(ResourceLoadIdentifier identifier, CompletionHandler<void(const WebCore::ResourceRequest&)>&& completionHandler)
96     {
97         completionHandler(m_networkLoadInformationByID.get(identifier).request);
98     }
99
100     void getNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, CompletionHandler<void(const WebCore::ResourceResponse&)>&& completionHandler)
101     {
102         completionHandler(m_networkLoadInformationByID.get(identifier).response);
103     }
104
105     void getNetworkLoadIntermediateInformation(ResourceLoadIdentifier identifier, CompletionHandler<void(const Vector<WebCore::NetworkTransactionInformation>&)>&& completionHandler)
106     {
107         completionHandler(m_networkLoadInformationByID.get(identifier).transactions);
108     }
109
110     void takeNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, CompletionHandler<void(const WebCore::NetworkLoadMetrics&)>&& completionHandler)
111     {
112         completionHandler(m_networkLoadInformationByID.take(identifier).metrics);
113     }
114
115     void addNetworkLoadInformation(ResourceLoadIdentifier identifier, WebCore::NetworkLoadInformation&& information)
116     {
117         ASSERT(!m_networkLoadInformationByID.contains(identifier));
118         m_networkLoadInformationByID.add(identifier, WTFMove(information));
119     }
120
121     void addNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, const WebCore::NetworkLoadMetrics& metrics)
122     {
123         ASSERT(m_networkLoadInformationByID.contains(identifier));
124         m_networkLoadInformationByID.ensure(identifier, [] {
125             return WebCore::NetworkLoadInformation { };
126         }).iterator->value.metrics = metrics;
127     }
128
129     void removeNetworkLoadInformation(ResourceLoadIdentifier identifier)
130     {
131         m_networkLoadInformationByID.remove(identifier);
132     }
133
134     Optional<NetworkActivityTracker> startTrackingResourceLoad(uint64_t pageID, ResourceLoadIdentifier resourceID, bool isMainResource, const PAL::SessionID&);
135     void stopTrackingResourceLoad(ResourceLoadIdentifier resourceID, NetworkActivityTracker::CompletionCode);
136
137     WebCore::BlobRegistryImpl& blobRegistry();
138     Vector<RefPtr<WebCore::BlobDataFileReference>> filesInBlob(const URL&);
139     Vector<RefPtr<WebCore::BlobDataFileReference>> resolveBlobReferences(const NetworkResourceLoadParameters&);
140
141     void setWebProcessIdentifier(WebCore::ProcessIdentifier);
142     void setConnectionHasUploads();
143     void clearConnectionHasUploads();
144
145 private:
146     NetworkConnectionToWebProcess(NetworkProcess&, IPC::Connection::Identifier);
147
148     void didFinishPreconnection(uint64_t preconnectionIdentifier, const WebCore::ResourceError&);
149
150     // IPC::Connection::Client
151     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
152     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
153     void didClose(IPC::Connection&) override;
154     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
155
156     // Message handlers.
157     void didReceiveNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&);
158     void didReceiveSyncNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
159
160     void scheduleResourceLoad(NetworkResourceLoadParameters&&);
161     void performSynchronousLoad(NetworkResourceLoadParameters&&, Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply&&);
162     void loadPing(NetworkResourceLoadParameters&&);
163     void prefetchDNS(const String&);
164     void preconnectTo(uint64_t preconnectionIdentifier, NetworkResourceLoadParameters&&);
165
166     void removeLoadIdentifier(ResourceLoadIdentifier);
167     void pageLoadCompleted(uint64_t webPageID);
168     void crossOriginRedirectReceived(ResourceLoadIdentifier, const URL& redirectURL);
169     void startDownload(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedName = { });
170     void convertMainResourceLoadToDownload(PAL::SessionID, uint64_t mainResourceLoadIdentifier, DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
171
172     void cookiesForDOM(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, WebCore::IncludeSecureCookies, CompletionHandler<void(String cookieString, bool secureCookiesAccessed)>&&);
173     void setCookiesFromDOM(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, const String&);
174     void cookiesEnabled(PAL::SessionID, CompletionHandler<void(bool)>&&);
175     void cookieRequestHeaderFieldValue(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, WebCore::IncludeSecureCookies, CompletionHandler<void(String cookieString, bool secureCookiesAccessed)>&&);
176     void getRawCookies(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, Optional<uint64_t> frameID, Optional<uint64_t> pageID, CompletionHandler<void(Vector<WebCore::Cookie>&&)>&&);
177     void deleteCookie(PAL::SessionID, const URL&, const String& cookieName);
178
179     void registerFileBlobURL(const URL&, const String& path, SandboxExtension::Handle&&, const String& contentType);
180     void registerBlobURL(const URL&, Vector<WebCore::BlobPart>&&, const String& contentType);
181     void registerBlobURLFromURL(const URL&, const URL& srcURL, bool shouldBypassConnectionCheck);
182     void registerBlobURLOptionallyFileBacked(const URL&, const URL& srcURL, const String& fileBackedPath, const String& contentType);
183     void registerBlobURLForSlice(const URL&, const URL& srcURL, int64_t start, int64_t end);
184     void blobSize(const URL&, CompletionHandler<void(uint64_t)>&&);
185     void unregisterBlobURL(const URL&);
186     void writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, CompletionHandler<void(Vector<String>&&)>&&);
187
188     void setCaptureExtraNetworkLoadMetricsEnabled(bool);
189
190     void createSocketStream(URL&&, PAL::SessionID, String cachePartition, uint64_t);
191     void destroySocketStream(uint64_t);
192     
193     void ensureLegacyPrivateBrowsingSession();
194
195 #if ENABLE(INDEXED_DATABASE)
196     // Messages handlers (Modern IDB).
197     void establishIDBConnectionToServer(PAL::SessionID, CompletionHandler<void(uint64_t  serverConnectionIdentifier)>&&);
198 #endif
199
200 #if ENABLE(SERVICE_WORKER)
201     void establishSWServerConnection(PAL::SessionID, CompletionHandler<void(WebCore::SWServerConnectionIdentifier&&)>&&);
202     void unregisterSWConnections();
203 #endif
204
205 #if USE(LIBWEBRTC)
206     NetworkRTCProvider& rtcProvider();
207 #endif
208 #if ENABLE(WEB_RTC)
209     NetworkMDNSRegister& mdnsRegister() { return m_mdnsRegister; }
210 #endif
211
212     CacheStorageEngineConnection& cacheStorageConnection();
213
214 #if ENABLE(RESOURCE_LOAD_STATISTICS)
215     void removeStorageAccessForFrame(PAL::SessionID, uint64_t frameID, uint64_t pageID);
216     void clearPageSpecificDataForResourceLoadStatistics(PAL::SessionID, uint64_t pageID);
217
218     void logUserInteraction(PAL::SessionID, const RegistrableDomain&);
219     void logWebSocketLoading(PAL::SessionID, const RegistrableDomain& targetDomain, const RegistrableDomain& topFrameDomain, WallTime lastSeen);
220     void logSubresourceLoading(PAL::SessionID, const RegistrableDomain& targetDomain, const RegistrableDomain& topFrameDomain, WallTime lastSeen);
221     void logSubresourceRedirect(PAL::SessionID, const RegistrableDomain& sourceDomain, const RegistrableDomain& targetDomain);
222     void resourceLoadStatisticsUpdated(Vector<WebCore::ResourceLoadStatistics>&&);
223     void hasStorageAccess(PAL::SessionID, const RegistrableDomain& subFrameDomain, const RegistrableDomain& topFrameDomain, uint64_t frameID, uint64_t pageID, CompletionHandler<void(bool)>&&);
224     void requestStorageAccess(PAL::SessionID, const RegistrableDomain& subFrameDomain, const RegistrableDomain& topFrameDomain, uint64_t frameID, uint64_t pageID, CompletionHandler<void(bool)>&&);
225     void requestStorageAccessUnderOpener(PAL::SessionID, WebCore::RegistrableDomain&& domainInNeedOfStorageAccess, uint64_t openerPageID, WebCore::RegistrableDomain&& openerDomain);
226 #endif
227
228     void addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
229     void removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
230     void resetOriginAccessWhitelists();
231
232     struct ResourceNetworkActivityTracker {
233         ResourceNetworkActivityTracker() = default;
234         ResourceNetworkActivityTracker(const ResourceNetworkActivityTracker&) = default;
235         ResourceNetworkActivityTracker(ResourceNetworkActivityTracker&&) = default;
236         ResourceNetworkActivityTracker(uint64_t pageID)
237             : pageID { pageID }
238             , isRootActivity { true }
239             , networkActivity { NetworkActivityTracker::Label::LoadPage }
240         {
241         }
242
243         ResourceNetworkActivityTracker(uint64_t pageID, ResourceLoadIdentifier resourceID)
244             : pageID { pageID }
245             , resourceID { resourceID }
246             , networkActivity { NetworkActivityTracker::Label::LoadResource }
247         {
248         }
249
250         uint64_t pageID { 0 };
251         ResourceLoadIdentifier resourceID { 0 };
252         bool isRootActivity { false };
253         NetworkActivityTracker networkActivity;
254     };
255
256     void stopAllNetworkActivityTracking();
257     void stopAllNetworkActivityTrackingForPage(uint64_t pageID);
258     size_t findRootNetworkActivity(uint64_t pageID);
259     size_t findNetworkActivityTracker(ResourceLoadIdentifier resourceID);
260
261 #if ENABLE(APPLE_PAY_REMOTE_UI)
262     WebPaymentCoordinatorProxy& paymentCoordinator();
263
264     // WebPaymentCoordinatorProxy::Client
265     IPC::Connection* paymentCoordinatorConnection(const WebPaymentCoordinatorProxy&) final;
266     UIViewController *paymentCoordinatorPresentingViewController(const WebPaymentCoordinatorProxy&) final;
267     const String& paymentCoordinatorCTDataConnectionServiceType(const WebPaymentCoordinatorProxy&, PAL::SessionID) final;
268     const String& paymentCoordinatorSourceApplicationBundleIdentifier(const WebPaymentCoordinatorProxy&, PAL::SessionID) final;
269     const String& paymentCoordinatorSourceApplicationSecondaryIdentifier(const WebPaymentCoordinatorProxy&, PAL::SessionID) final;
270     std::unique_ptr<PaymentAuthorizationPresenter> paymentCoordinatorAuthorizationPresenter(WebPaymentCoordinatorProxy&, PKPaymentRequest *) final;
271     void paymentCoordinatorAddMessageReceiver(WebPaymentCoordinatorProxy&, const IPC::StringReference&, IPC::MessageReceiver&) final;
272     void paymentCoordinatorRemoveMessageReceiver(WebPaymentCoordinatorProxy&, const IPC::StringReference&) final;
273 #endif
274
275     Ref<IPC::Connection> m_connection;
276     Ref<NetworkProcess> m_networkProcess;
277
278     HashMap<uint64_t, RefPtr<NetworkSocketStream>> m_networkSocketStreams;
279     NetworkResourceLoadMap m_networkResourceLoaders;
280     HashMap<String, RefPtr<WebCore::BlobDataFileReference>> m_blobDataFileReferences;
281     Vector<ResourceNetworkActivityTracker> m_networkActivityTrackers;
282
283     HashMap<ResourceLoadIdentifier, WebCore::NetworkLoadInformation> m_networkLoadInformationByID;
284
285
286 #if USE(LIBWEBRTC)
287     RefPtr<NetworkRTCProvider> m_rtcProvider;
288 #endif
289 #if ENABLE(WEB_RTC)
290     NetworkMDNSRegister m_mdnsRegister;
291 #endif
292
293     bool m_captureExtraNetworkLoadMetricsEnabled { false };
294
295     RefPtr<CacheStorageEngineConnection> m_cacheStorageConnection;
296
297 #if ENABLE(INDEXED_DATABASE)
298     HashMap<uint64_t, RefPtr<WebIDBConnectionToClient>> m_webIDBConnections;
299 #endif
300
301 #if ENABLE(SERVICE_WORKER)
302     HashMap<WebCore::SWServerConnectionIdentifier, WeakPtr<WebSWServerConnection>> m_swConnections;
303 #endif
304
305 #if ENABLE(APPLE_PAY_REMOTE_UI)
306     std::unique_ptr<WebPaymentCoordinatorProxy> m_paymentCoordinator;
307 #endif
308
309     WebCore::ProcessIdentifier m_webProcessIdentifier;
310     bool m_connectionHasUploads { false };
311 };
312
313 } // namespace WebKit