Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkConnectionToWebProcess.h
1 /*
2  * Copyright (C) 2012-2017 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 "BlockingResponseMap.h"
29 #include "CacheStorageEngineConnection.h"
30 #include "Connection.h"
31 #include "DownloadID.h"
32 #include "NetworkActivityTracker.h"
33 #include "NetworkConnectionToWebProcessMessages.h"
34 #include "NetworkMDNSRegister.h"
35 #include "NetworkRTCProvider.h"
36 #include <WebCore/NetworkLoadInformation.h>
37 #include <WebCore/ResourceLoadPriority.h>
38 #include <wtf/RefCounted.h>
39
40 namespace PAL {
41 class SessionID;
42 }
43
44 namespace WebCore {
45 class BlobDataFileReference;
46 class HTTPHeaderMap;
47 class ResourceError;
48 class ResourceRequest;
49 struct SameSiteInfo;
50
51 enum class IncludeSecureCookies : bool;
52 }
53
54 namespace WebKit {
55
56 class NetworkConnectionToWebProcess;
57 class NetworkLoadParameters;
58 class NetworkResourceLoader;
59 class NetworkSocketStream;
60 class SyncNetworkResourceLoader;
61 class WebIDBConnectionToClient;
62 class WebSWServerConnection;
63 class WebSWServerToContextConnection;
64 typedef uint64_t ResourceLoadIdentifier;
65
66 namespace NetworkCache {
67 struct DataKey;
68 }
69
70 class NetworkConnectionToWebProcess : public RefCounted<NetworkConnectionToWebProcess>, IPC::Connection::Client {
71 public:
72     static Ref<NetworkConnectionToWebProcess> create(IPC::Connection::Identifier);
73     virtual ~NetworkConnectionToWebProcess();
74
75     IPC::Connection& connection() { return m_connection.get(); }
76
77     void didCleanupResourceLoader(NetworkResourceLoader&);
78     void setOnLineState(bool);
79
80     bool captureExtraNetworkLoadMetricsEnabled() const { return m_captureExtraNetworkLoadMetricsEnabled; }
81
82     RefPtr<WebCore::BlobDataFileReference> getBlobDataFileReferenceForPath(const String& path);
83
84     void cleanupForSuspension(Function<void()>&&);
85     void endSuspension();
86
87     void getNetworkLoadInformationRequest(ResourceLoadIdentifier identifier, WebCore::ResourceRequest& request)
88     {
89         request = m_networkLoadInformationByID.get(identifier).request;
90     }
91
92     void getNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, WebCore::ResourceResponse& response)
93     {
94         response = m_networkLoadInformationByID.get(identifier).response;
95     }
96
97     void getNetworkLoadIntermediateInformation(ResourceLoadIdentifier identifier, Vector<WebCore::NetworkTransactionInformation>& information)
98     {
99         information = m_networkLoadInformationByID.get(identifier).transactions;
100     }
101
102     void takeNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, WebCore::NetworkLoadMetrics& metrics)
103     {
104         metrics = m_networkLoadInformationByID.take(identifier).metrics;
105     }
106
107     void addNetworkLoadInformation(ResourceLoadIdentifier identifier, WebCore::NetworkLoadInformation&& information)
108     {
109         ASSERT(!m_networkLoadInformationByID.contains(identifier));
110         m_networkLoadInformationByID.add(identifier, WTFMove(information));
111     }
112
113     void addNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, const WebCore::NetworkLoadMetrics& metrics)
114     {
115         ASSERT(m_networkLoadInformationByID.contains(identifier));
116         m_networkLoadInformationByID.ensure(identifier, [] {
117             return WebCore::NetworkLoadInformation { };
118         }).iterator->value.metrics = metrics;
119     }
120
121     void removeNetworkLoadInformation(ResourceLoadIdentifier identifier)
122     {
123         m_networkLoadInformationByID.remove(identifier);
124     }
125
126     std::optional<NetworkActivityTracker> startTrackingResourceLoad(uint64_t pageID, ResourceLoadIdentifier resourceID, bool isMainResource, const PAL::SessionID&);
127     void stopTrackingResourceLoad(ResourceLoadIdentifier resourceID, NetworkActivityTracker::CompletionCode);
128
129 private:
130     NetworkConnectionToWebProcess(IPC::Connection::Identifier);
131
132     void didFinishPreconnection(uint64_t preconnectionIdentifier, const WebCore::ResourceError&);
133
134     // IPC::Connection::Client
135     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
136     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
137     void didClose(IPC::Connection&) override;
138     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
139
140     // Message handlers.
141     void didReceiveNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&);
142     void didReceiveSyncNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
143
144     void scheduleResourceLoad(NetworkResourceLoadParameters&&);
145     void performSynchronousLoad(NetworkResourceLoadParameters&&, Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply&&);
146     void loadPing(NetworkResourceLoadParameters&&);
147     void prefetchDNS(const String&);
148     void preconnectTo(uint64_t preconnectionIdentifier, NetworkResourceLoadParameters&&);
149
150     void removeLoadIdentifier(ResourceLoadIdentifier);
151     void pageLoadCompleted(uint64_t webPageID);
152     void setDefersLoading(ResourceLoadIdentifier, bool);
153     void crossOriginRedirectReceived(ResourceLoadIdentifier, const URL& redirectURL);
154     void startDownload(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedName = { });
155     void convertMainResourceLoadToDownload(PAL::SessionID, uint64_t mainResourceLoadIdentifier, DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
156
157     void cookiesForDOM(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, WebCore::IncludeSecureCookies, String& cookieString, bool& secureCookiesAccessed);
158     void setCookiesFromDOM(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, const String&);
159     void cookiesEnabled(PAL::SessionID, bool& result);
160     void cookieRequestHeaderFieldValue(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, WebCore::IncludeSecureCookies, String& cookieString, bool& secureCookiesAccessed);
161     void getRawCookies(PAL::SessionID, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, Vector<WebCore::Cookie>&);
162     void deleteCookie(PAL::SessionID, const URL&, const String& cookieName);
163
164     void registerFileBlobURL(const URL&, const String& path, SandboxExtension::Handle&&, const String& contentType);
165     void registerBlobURL(const URL&, Vector<WebCore::BlobPart>&&, const String& contentType);
166     void registerBlobURLFromURL(const URL&, const URL& srcURL, bool shouldBypassConnectionCheck);
167     void registerBlobURLOptionallyFileBacked(const URL&, const URL& srcURL, const String& fileBackedPath, const String& contentType);
168     void registerBlobURLForSlice(const URL&, const URL& srcURL, int64_t start, int64_t end);
169     void blobSize(const URL&, uint64_t& resultSize);
170     void unregisterBlobURL(const URL&);
171     void writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, CompletionHandler<void(Vector<String>&&)>&&);
172
173     void storeDerivedDataToCache(const WebKit::NetworkCache::DataKey&, const IPC::DataReference&);
174
175     void setCaptureExtraNetworkLoadMetricsEnabled(bool);
176
177     void createSocketStream(URL&&, PAL::SessionID, String cachePartition, uint64_t);
178     void destroySocketStream(uint64_t);
179     
180     void ensureLegacyPrivateBrowsingSession();
181
182 #if ENABLE(INDEXED_DATABASE)
183     // Messages handlers (Modern IDB).
184     void establishIDBConnectionToServer(PAL::SessionID, uint64_t& serverConnectionIdentifier);
185     void removeIDBConnectionToServer(uint64_t serverConnectionIdentifier);
186 #endif
187
188 #if ENABLE(SERVICE_WORKER)
189     void establishSWServerConnection(PAL::SessionID, WebCore::SWServerConnectionIdentifier&);
190     void unregisterSWConnections();
191 #endif
192
193 #if USE(LIBWEBRTC)
194     NetworkRTCProvider& rtcProvider();
195 #endif
196 #if ENABLE(WEB_RTC)
197     NetworkMDNSRegister& mdnsRegister() { return m_mdnsRegister; }
198 #endif
199
200     CacheStorageEngineConnection& cacheStorageConnection();
201
202     void removeStorageAccessForFrame(PAL::SessionID, uint64_t frameID, uint64_t pageID);
203     void removeStorageAccessForAllFramesOnPage(PAL::SessionID, uint64_t pageID);
204
205     void addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
206     void removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
207     void resetOriginAccessWhitelists();
208
209     struct ResourceNetworkActivityTracker {
210         ResourceNetworkActivityTracker() = default;
211         ResourceNetworkActivityTracker(const ResourceNetworkActivityTracker&) = default;
212         ResourceNetworkActivityTracker(ResourceNetworkActivityTracker&&) = default;
213         ResourceNetworkActivityTracker(uint64_t pageID)
214             : pageID { pageID }
215             , isRootActivity { true }
216             , networkActivity { NetworkActivityTracker::Label::LoadPage }
217         {
218         }
219
220         ResourceNetworkActivityTracker(uint64_t pageID, ResourceLoadIdentifier resourceID)
221             : pageID { pageID }
222             , resourceID { resourceID }
223             , networkActivity { NetworkActivityTracker::Label::LoadResource }
224         {
225         }
226
227         uint64_t pageID { 0 };
228         ResourceLoadIdentifier resourceID { 0 };
229         bool isRootActivity { false };
230         NetworkActivityTracker networkActivity;
231     };
232
233     void stopAllNetworkActivityTracking();
234     void stopAllNetworkActivityTrackingForPage(uint64_t pageID);
235     size_t findRootNetworkActivity(uint64_t pageID);
236     size_t findNetworkActivityTracker(ResourceLoadIdentifier resourceID);
237
238     Ref<IPC::Connection> m_connection;
239
240     HashMap<uint64_t, RefPtr<NetworkSocketStream>> m_networkSocketStreams;
241     HashMap<ResourceLoadIdentifier, Ref<NetworkResourceLoader>> m_networkResourceLoaders;
242     HashMap<String, RefPtr<WebCore::BlobDataFileReference>> m_blobDataFileReferences;
243     Vector<ResourceNetworkActivityTracker> m_networkActivityTrackers;
244
245     HashMap<ResourceLoadIdentifier, WebCore::NetworkLoadInformation> m_networkLoadInformationByID;
246
247
248 #if USE(LIBWEBRTC)
249     RefPtr<NetworkRTCProvider> m_rtcProvider;
250 #endif
251 #if ENABLE(WEB_RTC)
252     NetworkMDNSRegister m_mdnsRegister;
253 #endif
254
255     bool m_captureExtraNetworkLoadMetricsEnabled { false };
256
257     RefPtr<CacheStorageEngineConnection> m_cacheStorageConnection;
258
259 #if ENABLE(INDEXED_DATABASE)
260     HashMap<uint64_t, RefPtr<WebIDBConnectionToClient>> m_webIDBConnections;
261 #endif
262     
263 #if ENABLE(SERVICE_WORKER)
264     HashMap<WebCore::SWServerConnectionIdentifier, WeakPtr<WebSWServerConnection>> m_swConnections;
265 #endif
266 };
267
268 } // namespace WebKit