eb6f55c6fe9e3b9d124af2a715f35ab13fe33e5e
[WebKit.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 "NetworkConnectionToWebProcessMessagesReplies.h"
33 #include "NetworkMDNSRegister.h"
34 #include "NetworkRTCProvider.h"
35 #include "NetworkResourceLoadMap.h"
36 #include "SandboxExtension.h"
37 #include "WebPageProxyIdentifier.h"
38 #include "WebPaymentCoordinatorProxy.h"
39 #include "WebResourceLoadObserver.h"
40 #include "WebSocketIdentifier.h"
41 #include <WebCore/FrameIdentifier.h>
42 #include <WebCore/MessagePortChannelProvider.h>
43 #include <WebCore/MessagePortIdentifier.h>
44 #include <WebCore/NetworkLoadInformation.h>
45 #include <WebCore/PageIdentifier.h>
46 #include <WebCore/ProcessIdentifier.h>
47 #include <WebCore/RegistrableDomain.h>
48 #include <wtf/RefCounted.h>
49
50 namespace PAL {
51 class SessionID;
52 }
53
54 namespace WebCore {
55 class BlobDataFileReference;
56 class BlobPart;
57 class BlobRegistryImpl;
58 class ResourceError;
59 class ResourceRequest;
60 enum class StorageAccessPromptWasShown : bool;
61 enum class StorageAccessWasGranted : bool;
62 enum class ShouldAskITP : bool;
63 struct SameSiteInfo;
64
65 enum class HTTPCookieAcceptPolicy : uint8_t;
66 enum class IncludeSecureCookies : bool;
67 }
68
69 namespace WebKit {
70
71 class NetworkSchemeRegistry;
72 class NetworkProcess;
73 class NetworkResourceLoader;
74 class NetworkResourceLoadParameters;
75 class NetworkSession;
76 class NetworkSocketChannel;
77 class NetworkSocketStream;
78 class WebSWServerConnection;
79 class WebSWServerToContextConnection;
80 typedef uint64_t ResourceLoadIdentifier;
81
82 namespace NetworkCache {
83 struct DataKey;
84 }
85
86 class NetworkConnectionToWebProcess
87     : public RefCounted<NetworkConnectionToWebProcess>
88 #if ENABLE(APPLE_PAY_REMOTE_UI)
89     , public WebPaymentCoordinatorProxy::Client
90 #endif
91     , IPC::Connection::Client {
92 public:
93     using RegistrableDomain = WebCore::RegistrableDomain;
94
95     static Ref<NetworkConnectionToWebProcess> create(NetworkProcess&, WebCore::ProcessIdentifier, PAL::SessionID, IPC::Connection::Identifier);
96     virtual ~NetworkConnectionToWebProcess();
97     
98     PAL::SessionID sessionID() const { return m_sessionID; }
99     NetworkSession* networkSession();
100
101     IPC::Connection& connection() { return m_connection.get(); }
102     NetworkProcess& networkProcess() { return m_networkProcess.get(); }
103
104     void didCleanupResourceLoader(NetworkResourceLoader&);
105     void transferKeptAliveLoad(NetworkResourceLoader&);
106     void setOnLineState(bool);
107
108     bool captureExtraNetworkLoadMetricsEnabled() const { return m_captureExtraNetworkLoadMetricsEnabled; }
109
110     RefPtr<WebCore::BlobDataFileReference> getBlobDataFileReferenceForPath(const String& path);
111
112     void cleanupForSuspension(Function<void()>&&);
113     void endSuspension();
114
115     void getNetworkLoadInformationRequest(ResourceLoadIdentifier identifier, CompletionHandler<void(const WebCore::ResourceRequest&)>&& completionHandler)
116     {
117         completionHandler(m_networkLoadInformationByID.get(identifier).request);
118     }
119
120     void getNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, CompletionHandler<void(const WebCore::ResourceResponse&)>&& completionHandler)
121     {
122         completionHandler(m_networkLoadInformationByID.get(identifier).response);
123     }
124
125     void getNetworkLoadIntermediateInformation(ResourceLoadIdentifier identifier, CompletionHandler<void(const Vector<WebCore::NetworkTransactionInformation>&)>&& completionHandler)
126     {
127         completionHandler(m_networkLoadInformationByID.get(identifier).transactions);
128     }
129
130     void takeNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, CompletionHandler<void(const WebCore::NetworkLoadMetrics&)>&& completionHandler)
131     {
132         completionHandler(m_networkLoadInformationByID.take(identifier).metrics);
133     }
134
135     void addNetworkLoadInformation(ResourceLoadIdentifier identifier, WebCore::NetworkLoadInformation&& information)
136     {
137         ASSERT(!m_networkLoadInformationByID.contains(identifier));
138         m_networkLoadInformationByID.add(identifier, WTFMove(information));
139     }
140
141     void addNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, const WebCore::NetworkLoadMetrics& metrics)
142     {
143         ASSERT(m_networkLoadInformationByID.contains(identifier));
144         m_networkLoadInformationByID.ensure(identifier, [] {
145             return WebCore::NetworkLoadInformation { };
146         }).iterator->value.metrics = metrics;
147     }
148
149     void removeNetworkLoadInformation(ResourceLoadIdentifier identifier)
150     {
151         m_networkLoadInformationByID.remove(identifier);
152     }
153
154     Optional<NetworkActivityTracker> startTrackingResourceLoad(WebCore::PageIdentifier, ResourceLoadIdentifier resourceID, bool isTopResource);
155     void stopTrackingResourceLoad(ResourceLoadIdentifier resourceID, NetworkActivityTracker::CompletionCode);
156
157     Vector<RefPtr<WebCore::BlobDataFileReference>> resolveBlobReferences(const NetworkResourceLoadParameters&);
158
159     void removeSocketChannel(WebSocketIdentifier);
160
161     WebCore::ProcessIdentifier webProcessIdentifier() const { return m_webProcessIdentifier; }
162
163     void checkProcessLocalPortForActivity(const WebCore::MessagePortIdentifier&, CompletionHandler<void(WebCore::MessagePortChannelProvider::HasActivity)>&&);
164
165 #if ENABLE(SERVICE_WORKER)
166     void serverToContextConnectionNoLongerNeeded();
167     WebSWServerConnection& swConnection();
168 #endif
169
170     NetworkSchemeRegistry& schemeRegistry() { return m_schemeRegistry.get(); }
171
172     void cookieAcceptPolicyChanged(WebCore::HTTPCookieAcceptPolicy);
173
174 private:
175     NetworkConnectionToWebProcess(NetworkProcess&, WebCore::ProcessIdentifier, PAL::SessionID, IPC::Connection::Identifier);
176
177     void didFinishPreconnection(uint64_t preconnectionIdentifier, const WebCore::ResourceError&);
178     WebCore::NetworkStorageSession* storageSession();
179
180     // IPC::Connection::Client
181     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
182     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
183     void didClose(IPC::Connection&) override;
184     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
185
186     // Message handlers.
187     void didReceiveNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&);
188     void didReceiveSyncNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
189
190     void scheduleResourceLoad(NetworkResourceLoadParameters&&);
191     void performSynchronousLoad(NetworkResourceLoadParameters&&, Messages::NetworkConnectionToWebProcess::PerformSynchronousLoadDelayedReply&&);
192     void testProcessIncomingSyncMessagesWhenWaitingForSyncReply(WebPageProxyIdentifier, Messages::NetworkConnectionToWebProcess::TestProcessIncomingSyncMessagesWhenWaitingForSyncReplyDelayedReply&&);
193     void loadPing(NetworkResourceLoadParameters&&);
194     void prefetchDNS(const String&);
195     void preconnectTo(Optional<uint64_t> preconnectionIdentifier, NetworkResourceLoadParameters&&);
196
197     void removeLoadIdentifier(ResourceLoadIdentifier);
198     void pageLoadCompleted(WebCore::PageIdentifier);
199     void crossOriginRedirectReceived(ResourceLoadIdentifier, const URL& redirectURL);
200     void startDownload(DownloadID, const WebCore::ResourceRequest&, const String& suggestedName = { });
201     void convertMainResourceLoadToDownload(uint64_t mainResourceLoadIdentifier, DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
202
203     void registerURLSchemesAsCORSEnabled(Vector<String>&& schemes);
204
205     void cookiesForDOM(const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, WebCore::FrameIdentifier, WebCore::PageIdentifier, WebCore::IncludeSecureCookies, WebCore::ShouldAskITP, CompletionHandler<void(String cookieString, bool secureCookiesAccessed)>&&);
206     void setCookiesFromDOM(const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, WebCore::FrameIdentifier, WebCore::PageIdentifier, WebCore::ShouldAskITP, const String&);
207     void cookieRequestHeaderFieldValue(const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, Optional<WebCore::FrameIdentifier>, Optional<WebCore::PageIdentifier>, WebCore::IncludeSecureCookies, WebCore::ShouldAskITP, CompletionHandler<void(String cookieString, bool secureCookiesAccessed)>&&);
208     void getRawCookies(const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, Optional<WebCore::FrameIdentifier>, Optional<WebCore::PageIdentifier>, WebCore::ShouldAskITP, CompletionHandler<void(Vector<WebCore::Cookie>&&)>&&);
209     void deleteCookie(const URL&, const String& cookieName);
210
211     void registerFileBlobURL(const URL&, const String& path, SandboxExtension::Handle&&, const String& contentType);
212     void registerBlobURL(const URL&, Vector<WebCore::BlobPart>&&, const String& contentType);
213     void registerBlobURLFromURL(const URL&, const URL& srcURL);
214     void registerBlobURLOptionallyFileBacked(const URL&, const URL& srcURL, const String& fileBackedPath, const String& contentType);
215     void registerBlobURLForSlice(const URL&, const URL& srcURL, int64_t start, int64_t end);
216     void blobSize(const URL&, CompletionHandler<void(uint64_t)>&&);
217     void unregisterBlobURL(const URL&);
218     void writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, CompletionHandler<void(Vector<String>&&)>&&);
219
220     void setCaptureExtraNetworkLoadMetricsEnabled(bool);
221
222     void createSocketStream(URL&&, String cachePartition, WebSocketIdentifier);
223
224     void createSocketChannel(const WebCore::ResourceRequest&, const String& protocol, WebSocketIdentifier);
225     void updateQuotaBasedOnSpaceUsageForTesting(const WebCore::ClientOrigin&);
226
227 #if ENABLE(SERVICE_WORKER)
228     void establishSWServerConnection();
229     void establishSWContextConnection(WebCore::RegistrableDomain&&, CompletionHandler<void()>&&);
230     void closeSWContextConnection();
231     void unregisterSWConnection();
232 #endif
233
234     void createNewMessagePortChannel(const WebCore::MessagePortIdentifier& port1, const WebCore::MessagePortIdentifier& port2);
235     void entangleLocalPortInThisProcessToRemote(const WebCore::MessagePortIdentifier& local, const WebCore::MessagePortIdentifier& remote);
236     void messagePortDisentangled(const WebCore::MessagePortIdentifier&);
237     void messagePortClosed(const WebCore::MessagePortIdentifier&);
238     void takeAllMessagesForPort(const WebCore::MessagePortIdentifier&, CompletionHandler<void(Vector<WebCore::MessageWithMessagePorts>&&, uint64_t)>&&);
239     void postMessageToRemote(WebCore::MessageWithMessagePorts&&, const WebCore::MessagePortIdentifier&);
240     void checkRemotePortForActivity(const WebCore::MessagePortIdentifier, CompletionHandler<void(bool)>&&);
241     void didDeliverMessagePortMessages(uint64_t messageBatchIdentifier);
242
243 #if USE(LIBWEBRTC)
244     NetworkRTCProvider& rtcProvider();
245 #endif
246 #if ENABLE(WEB_RTC)
247     NetworkMDNSRegister& mdnsRegister() { return m_mdnsRegister; }
248 #endif
249
250     CacheStorageEngineConnection& cacheStorageConnection();
251
252 #if ENABLE(RESOURCE_LOAD_STATISTICS)
253     void removeStorageAccessForFrame(WebCore::FrameIdentifier, WebCore::PageIdentifier);
254     void clearPageSpecificDataForResourceLoadStatistics(WebCore::PageIdentifier);
255
256     void logUserInteraction(const RegistrableDomain&);
257     void resourceLoadStatisticsUpdated(Vector<WebCore::ResourceLoadStatistics>&&);
258     void hasStorageAccess(const RegistrableDomain& subFrameDomain, const RegistrableDomain& topFrameDomain, WebCore::FrameIdentifier, WebCore::PageIdentifier, CompletionHandler<void(bool)>&&);
259     void requestStorageAccess(const RegistrableDomain& subFrameDomain, const RegistrableDomain& topFrameDomain, WebCore::FrameIdentifier, WebCore::PageIdentifier, WebPageProxyIdentifier, CompletionHandler<void(WebCore::StorageAccessWasGranted, WebCore::StorageAccessPromptWasShown)>&&);
260     void requestStorageAccessUnderOpener(WebCore::RegistrableDomain&& domainInNeedOfStorageAccess, WebCore::PageIdentifier openerPageID, WebCore::RegistrableDomain&& openerDomain);
261     void isPrevalentSubresourceLoad(RegistrableDomain&&, CompletionHandler<void(bool)>&&);
262 #endif
263
264     void addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
265     void removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
266     void resetOriginAccessWhitelists();
267
268     uint64_t nextMessageBatchIdentifier(Function<void()>&&);
269
270     struct ResourceNetworkActivityTracker {
271         ResourceNetworkActivityTracker() = default;
272         ResourceNetworkActivityTracker(const ResourceNetworkActivityTracker&) = default;
273         ResourceNetworkActivityTracker(ResourceNetworkActivityTracker&&) = default;
274         ResourceNetworkActivityTracker(WebCore::PageIdentifier pageID)
275             : pageID { pageID }
276             , isRootActivity { true }
277             , networkActivity { NetworkActivityTracker::Label::LoadPage }
278         {
279         }
280
281         ResourceNetworkActivityTracker(WebCore::PageIdentifier pageID, ResourceLoadIdentifier resourceID)
282             : pageID { pageID }
283             , resourceID { resourceID }
284             , networkActivity { NetworkActivityTracker::Label::LoadResource }
285         {
286         }
287
288         WebCore::PageIdentifier pageID;
289         ResourceLoadIdentifier resourceID { 0 };
290         bool isRootActivity { false };
291         NetworkActivityTracker networkActivity;
292     };
293
294     void stopAllNetworkActivityTracking();
295     void stopAllNetworkActivityTrackingForPage(WebCore::PageIdentifier);
296     size_t findRootNetworkActivity(WebCore::PageIdentifier);
297     size_t findNetworkActivityTracker(ResourceLoadIdentifier resourceID);
298
299 #if ENABLE(APPLE_PAY_REMOTE_UI)
300     WebPaymentCoordinatorProxy& paymentCoordinator();
301
302     // WebPaymentCoordinatorProxy::Client
303     IPC::Connection* paymentCoordinatorConnection(const WebPaymentCoordinatorProxy&) final;
304     UIViewController *paymentCoordinatorPresentingViewController(const WebPaymentCoordinatorProxy&) final;
305     const String& paymentCoordinatorBoundInterfaceIdentifier(const WebPaymentCoordinatorProxy&) final;
306     const String& paymentCoordinatorCTDataConnectionServiceType(const WebPaymentCoordinatorProxy&) final;
307     const String& paymentCoordinatorSourceApplicationBundleIdentifier(const WebPaymentCoordinatorProxy&) final;
308     const String& paymentCoordinatorSourceApplicationSecondaryIdentifier(const WebPaymentCoordinatorProxy&) final;
309     std::unique_ptr<PaymentAuthorizationPresenter> paymentCoordinatorAuthorizationPresenter(WebPaymentCoordinatorProxy&, PKPaymentRequest *) final;
310     void paymentCoordinatorAddMessageReceiver(WebPaymentCoordinatorProxy&, const IPC::StringReference&, IPC::MessageReceiver&) final;
311     void paymentCoordinatorRemoveMessageReceiver(WebPaymentCoordinatorProxy&, const IPC::StringReference&) final;
312 #endif
313
314     Ref<IPC::Connection> m_connection;
315     Ref<NetworkProcess> m_networkProcess;
316     PAL::SessionID m_sessionID;
317
318     HashMap<WebSocketIdentifier, RefPtr<NetworkSocketStream>> m_networkSocketStreams;
319     HashMap<WebSocketIdentifier, std::unique_ptr<NetworkSocketChannel>> m_networkSocketChannels;
320     NetworkResourceLoadMap m_networkResourceLoaders;
321     HashMap<String, RefPtr<WebCore::BlobDataFileReference>> m_blobDataFileReferences;
322     Vector<ResourceNetworkActivityTracker> m_networkActivityTrackers;
323
324     HashMap<ResourceLoadIdentifier, WebCore::NetworkLoadInformation> m_networkLoadInformationByID;
325
326
327 #if USE(LIBWEBRTC)
328     RefPtr<NetworkRTCProvider> m_rtcProvider;
329 #endif
330 #if ENABLE(WEB_RTC)
331     NetworkMDNSRegister m_mdnsRegister;
332 #endif
333
334     bool m_captureExtraNetworkLoadMetricsEnabled { false };
335
336     RefPtr<CacheStorageEngineConnection> m_cacheStorageConnection;
337
338 #if ENABLE(SERVICE_WORKER)
339     WeakPtr<WebSWServerConnection> m_swConnection;
340     std::unique_ptr<WebSWServerToContextConnection> m_swContextConnection;
341 #endif
342
343 #if ENABLE(APPLE_PAY_REMOTE_UI)
344     std::unique_ptr<WebPaymentCoordinatorProxy> m_paymentCoordinator;
345 #endif
346     const WebCore::ProcessIdentifier m_webProcessIdentifier;
347
348     HashSet<WebCore::MessagePortIdentifier> m_processEntangledPorts;
349     HashMap<uint64_t, Function<void()>> m_messageBatchDeliveryCompletionHandlers;
350     Ref<NetworkSchemeRegistry> m_schemeRegistry;
351 };
352
353 } // namespace WebKit