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