d71dc3c81607f877b1fd0ad3fbc1414e10cdd17b
[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 "NetworkConnectionToWebProcessMessages.h"
33 #include "NetworkMDNSRegister.h"
34 #include "NetworkRTCProvider.h"
35 #include <WebCore/ResourceLoadPriority.h>
36 #include <wtf/RefCounted.h>
37
38 namespace WebCore {
39 class BlobDataFileReference;
40 class HTTPHeaderMap;
41 class ResourceError;
42 class ResourceRequest;
43 struct SameSiteInfo;
44
45 enum class IncludeSecureCookies;
46 }
47
48 namespace WebKit {
49
50 class NetworkConnectionToWebProcess;
51 class NetworkLoadParameters;
52 class NetworkResourceLoader;
53 class NetworkSocketStream;
54 class SyncNetworkResourceLoader;
55 typedef uint64_t ResourceLoadIdentifier;
56
57 namespace NetworkCache {
58 struct DataKey;
59 }
60
61 class NetworkConnectionToWebProcess : public RefCounted<NetworkConnectionToWebProcess>, IPC::Connection::Client {
62 public:
63     static Ref<NetworkConnectionToWebProcess> create(IPC::Connection::Identifier);
64     virtual ~NetworkConnectionToWebProcess();
65
66     IPC::Connection& connection() { return m_connection.get(); }
67
68     void didCleanupResourceLoader(NetworkResourceLoader&);
69     void didFinishPingLoad(uint64_t pingLoadIdentifier, const WebCore::ResourceError&, const WebCore::ResourceResponse&);
70     void setOnLineState(bool);
71
72     bool captureExtraNetworkLoadMetricsEnabled() const { return m_captureExtraNetworkLoadMetricsEnabled; }
73
74     RefPtr<WebCore::BlobDataFileReference> getBlobDataFileReferenceForPath(const String& path);
75
76     void cleanupForSuspension(Function<void()>&&);
77     void endSuspension();
78
79     // FIXME: We should store all redirected request/responses.
80     struct NetworkLoadInformation {
81         WebCore::ResourceResponse response;
82         WebCore::NetworkLoadMetrics metrics;
83     };
84
85     void takeNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, WebCore::ResourceResponse& response)
86     {
87         response = m_networkLoadInformationByID.get(identifier).response;
88     }
89
90     void takeNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, WebCore::NetworkLoadMetrics& metrics)
91     {
92         metrics = m_networkLoadInformationByID.take(identifier).metrics;
93     }
94
95     void addNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, const WebCore::ResourceResponse& response)
96     {
97         ASSERT(!m_networkLoadInformationByID.contains(identifier));
98         m_networkLoadInformationByID.add(identifier, NetworkLoadInformation { response, { } });
99     }
100
101     void addNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, const WebCore::NetworkLoadMetrics& metrics)
102     {
103         ASSERT(m_networkLoadInformationByID.contains(identifier));
104         m_networkLoadInformationByID.ensure(identifier, [] {
105             return NetworkLoadInformation { };
106         }).iterator->value.metrics = metrics;
107     }
108
109     void removeNetworkLoadInformation(ResourceLoadIdentifier identifier)
110     {
111         m_networkLoadInformationByID.remove(identifier);
112     }
113
114 private:
115     NetworkConnectionToWebProcess(IPC::Connection::Identifier);
116
117     void didFinishPreconnection(uint64_t preconnectionIdentifier, const WebCore::ResourceError&);
118
119     // IPC::Connection::Client
120     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
121     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
122     void didClose(IPC::Connection&) override;
123     void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
124
125     // Message handlers.
126     void didReceiveNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&);
127     void didReceiveSyncNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
128
129     void scheduleResourceLoad(NetworkResourceLoadParameters&&);
130     void performSynchronousLoad(NetworkResourceLoadParameters&&, Ref<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&&);
131     void loadPing(NetworkResourceLoadParameters&&);
132     void prefetchDNS(const String&);
133     void preconnectTo(uint64_t preconnectionIdentifier, NetworkResourceLoadParameters&&);
134
135     void removeLoadIdentifier(ResourceLoadIdentifier);
136     void setDefersLoading(ResourceLoadIdentifier, bool);
137     void crossOriginRedirectReceived(ResourceLoadIdentifier, const WebCore::URL& redirectURL);
138     void startDownload(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedName = { });
139     void convertMainResourceLoadToDownload(PAL::SessionID, uint64_t mainResourceLoadIdentifier, DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
140
141     void cookiesForDOM(PAL::SessionID, const WebCore::URL& firstParty, const WebCore::SameSiteInfo&, const WebCore::URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, WebCore::IncludeSecureCookies, String& cookieString, bool& secureCookiesAccessed);
142     void setCookiesFromDOM(PAL::SessionID, const WebCore::URL& firstParty, const WebCore::SameSiteInfo&, const WebCore::URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, const String&);
143     void cookiesEnabled(PAL::SessionID, bool& result);
144     void cookieRequestHeaderFieldValue(PAL::SessionID, const WebCore::URL& firstParty, const WebCore::SameSiteInfo&, const WebCore::URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, WebCore::IncludeSecureCookies, String& cookieString, bool& secureCookiesAccessed);
145     void getRawCookies(PAL::SessionID, const WebCore::URL& firstParty, const WebCore::SameSiteInfo&, const WebCore::URL&, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, Vector<WebCore::Cookie>&);
146     void deleteCookie(PAL::SessionID, const WebCore::URL&, const String& cookieName);
147
148     void registerFileBlobURL(const WebCore::URL&, const String& path, SandboxExtension::Handle&&, const String& contentType);
149     void registerBlobURL(const WebCore::URL&, Vector<WebCore::BlobPart>&&, const String& contentType);
150     void registerBlobURLFromURL(const WebCore::URL&, const WebCore::URL& srcURL, bool shouldBypassConnectionCheck);
151     void preregisterSandboxExtensionsForOptionallyFileBackedBlob(const Vector<String>& fileBackedPath, SandboxExtension::HandleArray&&);
152     void registerBlobURLOptionallyFileBacked(const WebCore::URL&, const WebCore::URL& srcURL, const String& fileBackedPath, const String& contentType);
153     void registerBlobURLForSlice(const WebCore::URL&, const WebCore::URL& srcURL, int64_t start, int64_t end);
154     void blobSize(const WebCore::URL&, uint64_t& resultSize);
155     void unregisterBlobURL(const WebCore::URL&);
156     void writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, uint64_t requestIdentifier);
157
158     void storeDerivedDataToCache(const WebKit::NetworkCache::DataKey&, const IPC::DataReference&);
159
160     void setCaptureExtraNetworkLoadMetricsEnabled(bool);
161
162     void createSocketStream(WebCore::URL&&, PAL::SessionID, String cachePartition, uint64_t);
163     void destroySocketStream(uint64_t);
164     
165     void ensureLegacyPrivateBrowsingSession();
166
167 #if USE(LIBWEBRTC)
168     NetworkRTCProvider& rtcProvider();
169 #endif
170 #if ENABLE(WEB_RTC)
171     NetworkMDNSRegister& mdnsRegister() { return m_mdnsRegister; }
172 #endif
173
174     CacheStorageEngineConnection& cacheStorageConnection();
175
176     void removeStorageAccessForFrame(PAL::SessionID, uint64_t frameID, uint64_t pageID);
177     void removeStorageAccessForAllFramesOnPage(PAL::SessionID, uint64_t pageID);
178
179     void addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
180     void removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
181     void resetOriginAccessWhitelists();
182
183     Ref<IPC::Connection> m_connection;
184
185     HashMap<uint64_t, RefPtr<NetworkSocketStream>> m_networkSocketStreams;
186     HashMap<ResourceLoadIdentifier, RefPtr<NetworkResourceLoader>> m_networkResourceLoaders;
187     HashMap<String, RefPtr<WebCore::BlobDataFileReference>> m_blobDataFileReferences;
188
189     HashMap<ResourceLoadIdentifier, NetworkLoadInformation> m_networkLoadInformationByID;
190
191
192 #if USE(LIBWEBRTC)
193     RefPtr<NetworkRTCProvider> m_rtcProvider;
194 #endif
195 #if ENABLE(WEB_RTC)
196     NetworkMDNSRegister m_mdnsRegister;
197 #endif
198
199     bool m_captureExtraNetworkLoadMetricsEnabled { false };
200
201     RefPtr<CacheStorageEngineConnection> m_cacheStorageConnection;
202 };
203
204 } // namespace WebKit