2 * Copyright (C) 2012-2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
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>
39 class BlobDataFileReference;
42 class ResourceRequest;
45 enum class IncludeSecureCookies;
50 class NetworkConnectionToWebProcess;
51 class NetworkLoadParameters;
52 class NetworkResourceLoader;
53 class NetworkSocketStream;
54 class SyncNetworkResourceLoader;
55 typedef uint64_t ResourceLoadIdentifier;
57 namespace NetworkCache {
61 class NetworkConnectionToWebProcess : public RefCounted<NetworkConnectionToWebProcess>, IPC::Connection::Client {
63 static Ref<NetworkConnectionToWebProcess> create(IPC::Connection::Identifier);
64 virtual ~NetworkConnectionToWebProcess();
66 IPC::Connection& connection() { return m_connection.get(); }
68 void didCleanupResourceLoader(NetworkResourceLoader&);
69 void didFinishPingLoad(uint64_t pingLoadIdentifier, const WebCore::ResourceError&, const WebCore::ResourceResponse&);
70 void setOnLineState(bool);
72 bool captureExtraNetworkLoadMetricsEnabled() const { return m_captureExtraNetworkLoadMetricsEnabled; }
74 RefPtr<WebCore::BlobDataFileReference> getBlobDataFileReferenceForPath(const String& path);
76 void cleanupForSuspension(Function<void()>&&);
79 // FIXME: We should store all redirected request/responses.
80 struct NetworkLoadInformation {
81 WebCore::ResourceResponse response;
82 WebCore::NetworkLoadMetrics metrics;
85 void takeNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, WebCore::ResourceResponse& response)
87 response = m_networkLoadInformationByID.get(identifier).response;
90 void takeNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, WebCore::NetworkLoadMetrics& metrics)
92 metrics = m_networkLoadInformationByID.take(identifier).metrics;
95 void addNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, const WebCore::ResourceResponse& response)
97 ASSERT(!m_networkLoadInformationByID.contains(identifier));
98 m_networkLoadInformationByID.add(identifier, NetworkLoadInformation { response, { } });
101 void addNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, const WebCore::NetworkLoadMetrics& metrics)
103 ASSERT(m_networkLoadInformationByID.contains(identifier));
104 m_networkLoadInformationByID.ensure(identifier, [] {
105 return NetworkLoadInformation { };
106 }).iterator->value.metrics = metrics;
109 void removeNetworkLoadInformation(ResourceLoadIdentifier identifier)
111 m_networkLoadInformationByID.remove(identifier);
115 NetworkConnectionToWebProcess(IPC::Connection::Identifier);
117 void didFinishPreconnection(uint64_t preconnectionIdentifier, const WebCore::ResourceError&);
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;
126 void didReceiveNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&);
127 void didReceiveSyncNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
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&&);
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&);
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);
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);
158 void storeDerivedDataToCache(const WebKit::NetworkCache::DataKey&, const IPC::DataReference&);
160 void setCaptureExtraNetworkLoadMetricsEnabled(bool);
162 void createSocketStream(WebCore::URL&&, PAL::SessionID, String cachePartition, uint64_t);
163 void destroySocketStream(uint64_t);
165 void ensureLegacyPrivateBrowsingSession();
168 NetworkRTCProvider& rtcProvider();
171 NetworkMDNSRegister& mdnsRegister() { return m_mdnsRegister; }
174 CacheStorageEngineConnection& cacheStorageConnection();
176 void removeStorageAccessForFrame(PAL::SessionID, uint64_t frameID, uint64_t pageID);
177 void removeStorageAccessForAllFramesOnPage(PAL::SessionID, uint64_t pageID);
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();
183 Ref<IPC::Connection> m_connection;
185 HashMap<uint64_t, RefPtr<NetworkSocketStream>> m_networkSocketStreams;
186 HashMap<ResourceLoadIdentifier, RefPtr<NetworkResourceLoader>> m_networkResourceLoaders;
187 HashMap<String, RefPtr<WebCore::BlobDataFileReference>> m_blobDataFileReferences;
189 HashMap<ResourceLoadIdentifier, NetworkLoadInformation> m_networkLoadInformationByID;
193 RefPtr<NetworkRTCProvider> m_rtcProvider;
196 NetworkMDNSRegister m_mdnsRegister;
199 bool m_captureExtraNetworkLoadMetricsEnabled { false };
201 RefPtr<CacheStorageEngineConnection> m_cacheStorageConnection;
204 } // namespace WebKit