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