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 "NetworkActivityTracker.h"
33 #include "NetworkConnectionToWebProcessMessages.h"
34 #include "NetworkMDNSRegister.h"
35 #include "NetworkRTCProvider.h"
36 #include <WebCore/ResourceLoadPriority.h>
37 #include <wtf/RefCounted.h>
40 class BlobDataFileReference;
43 class ResourceRequest;
46 enum class IncludeSecureCookies;
51 class NetworkConnectionToWebProcess;
52 class NetworkLoadParameters;
53 class NetworkResourceLoader;
54 class NetworkSocketStream;
55 class SyncNetworkResourceLoader;
56 typedef uint64_t ResourceLoadIdentifier;
58 namespace NetworkCache {
62 class NetworkConnectionToWebProcess : public RefCounted<NetworkConnectionToWebProcess>, IPC::Connection::Client {
64 static Ref<NetworkConnectionToWebProcess> create(IPC::Connection::Identifier);
65 virtual ~NetworkConnectionToWebProcess();
67 IPC::Connection& connection() { return m_connection.get(); }
69 void didCleanupResourceLoader(NetworkResourceLoader&);
70 void didFinishPingLoad(uint64_t pingLoadIdentifier, const WebCore::ResourceError&, const WebCore::ResourceResponse&);
71 void setOnLineState(bool);
73 bool captureExtraNetworkLoadMetricsEnabled() const { return m_captureExtraNetworkLoadMetricsEnabled; }
75 RefPtr<WebCore::BlobDataFileReference> getBlobDataFileReferenceForPath(const String& path);
77 void cleanupForSuspension(Function<void()>&&);
80 // FIXME: We should store all redirected request/responses.
81 struct NetworkLoadInformation {
82 WebCore::ResourceResponse response;
83 WebCore::NetworkLoadMetrics metrics;
86 void takeNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, WebCore::ResourceResponse& response)
88 response = m_networkLoadInformationByID.get(identifier).response;
91 void takeNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, WebCore::NetworkLoadMetrics& metrics)
93 metrics = m_networkLoadInformationByID.take(identifier).metrics;
96 void addNetworkLoadInformationResponse(ResourceLoadIdentifier identifier, const WebCore::ResourceResponse& response)
98 ASSERT(!m_networkLoadInformationByID.contains(identifier));
99 m_networkLoadInformationByID.add(identifier, NetworkLoadInformation { response, { } });
102 void addNetworkLoadInformationMetrics(ResourceLoadIdentifier identifier, const WebCore::NetworkLoadMetrics& metrics)
104 ASSERT(m_networkLoadInformationByID.contains(identifier));
105 m_networkLoadInformationByID.ensure(identifier, [] {
106 return NetworkLoadInformation { };
107 }).iterator->value.metrics = metrics;
110 void removeNetworkLoadInformation(ResourceLoadIdentifier identifier)
112 m_networkLoadInformationByID.remove(identifier);
115 std::optional<NetworkActivityTracker> startTrackingResourceLoad(uint64_t pageID, ResourceLoadIdentifier resourceID, bool isMainResource);
116 void stopTrackingResourceLoad(ResourceLoadIdentifier resourceID, NetworkActivityTracker::CompletionCode);
119 NetworkConnectionToWebProcess(IPC::Connection::Identifier);
121 void didFinishPreconnection(uint64_t preconnectionIdentifier, const WebCore::ResourceError&);
123 // IPC::Connection::Client
124 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
125 void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
126 void didClose(IPC::Connection&) override;
127 void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
130 void didReceiveNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&);
131 void didReceiveSyncNetworkConnectionToWebProcessMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
133 void scheduleResourceLoad(NetworkResourceLoadParameters&&);
134 void performSynchronousLoad(NetworkResourceLoadParameters&&, Ref<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&&);
135 void loadPing(NetworkResourceLoadParameters&&);
136 void prefetchDNS(const String&);
137 void preconnectTo(uint64_t preconnectionIdentifier, NetworkResourceLoadParameters&&);
139 void removeLoadIdentifier(ResourceLoadIdentifier);
140 void pageLoadCompleted(uint64_t webPageID);
141 void setDefersLoading(ResourceLoadIdentifier, bool);
142 void crossOriginRedirectReceived(ResourceLoadIdentifier, const WebCore::URL& redirectURL);
143 void startDownload(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedName = { });
144 void convertMainResourceLoadToDownload(PAL::SessionID, uint64_t mainResourceLoadIdentifier, DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
146 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);
147 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&);
148 void cookiesEnabled(PAL::SessionID, bool& result);
149 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);
150 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>&);
151 void deleteCookie(PAL::SessionID, const WebCore::URL&, const String& cookieName);
153 void registerFileBlobURL(const WebCore::URL&, const String& path, SandboxExtension::Handle&&, const String& contentType);
154 void registerBlobURL(const WebCore::URL&, Vector<WebCore::BlobPart>&&, const String& contentType);
155 void registerBlobURLFromURL(const WebCore::URL&, const WebCore::URL& srcURL, bool shouldBypassConnectionCheck);
156 void preregisterSandboxExtensionsForOptionallyFileBackedBlob(const Vector<String>& fileBackedPath, SandboxExtension::HandleArray&&);
157 void registerBlobURLOptionallyFileBacked(const WebCore::URL&, const WebCore::URL& srcURL, const String& fileBackedPath, const String& contentType);
158 void registerBlobURLForSlice(const WebCore::URL&, const WebCore::URL& srcURL, int64_t start, int64_t end);
159 void blobSize(const WebCore::URL&, uint64_t& resultSize);
160 void unregisterBlobURL(const WebCore::URL&);
161 void writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, uint64_t requestIdentifier);
163 void storeDerivedDataToCache(const WebKit::NetworkCache::DataKey&, const IPC::DataReference&);
165 void setCaptureExtraNetworkLoadMetricsEnabled(bool);
167 void createSocketStream(WebCore::URL&&, PAL::SessionID, String cachePartition, uint64_t);
168 void destroySocketStream(uint64_t);
170 void ensureLegacyPrivateBrowsingSession();
173 NetworkRTCProvider& rtcProvider();
176 NetworkMDNSRegister& mdnsRegister() { return m_mdnsRegister; }
179 CacheStorageEngineConnection& cacheStorageConnection();
181 void removeStorageAccessForFrame(PAL::SessionID, uint64_t frameID, uint64_t pageID);
182 void removeStorageAccessForAllFramesOnPage(PAL::SessionID, uint64_t pageID);
184 void addOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
185 void removeOriginAccessWhitelistEntry(const String& sourceOrigin, const String& destinationProtocol, const String& destinationHost, bool allowDestinationSubdomains);
186 void resetOriginAccessWhitelists();
188 struct ResourceNetworkActivityTracker {
189 ResourceNetworkActivityTracker() = default;
190 ResourceNetworkActivityTracker(const ResourceNetworkActivityTracker&) = default;
191 ResourceNetworkActivityTracker(ResourceNetworkActivityTracker&&) = default;
192 ResourceNetworkActivityTracker(uint64_t pageID)
194 , isRootActivity { true }
195 , networkActivity { NetworkActivityTracker::Label::LoadPage }
199 ResourceNetworkActivityTracker(uint64_t pageID, ResourceLoadIdentifier resourceID)
201 , resourceID { resourceID }
202 , networkActivity { NetworkActivityTracker::Label::LoadResource }
206 uint64_t pageID { 0 };
207 ResourceLoadIdentifier resourceID { 0 };
208 bool isRootActivity { false };
209 NetworkActivityTracker networkActivity;
212 void stopAllNetworkActivityTracking();
213 void stopAllNetworkActivityTrackingForPage(uint64_t pageID);
214 size_t findRootNetworkActivity(uint64_t pageID);
215 size_t findNetworkActivityTracker(ResourceLoadIdentifier resourceID);
217 Ref<IPC::Connection> m_connection;
219 HashMap<uint64_t, RefPtr<NetworkSocketStream>> m_networkSocketStreams;
220 HashMap<ResourceLoadIdentifier, RefPtr<NetworkResourceLoader>> m_networkResourceLoaders;
221 HashMap<String, RefPtr<WebCore::BlobDataFileReference>> m_blobDataFileReferences;
222 Vector<ResourceNetworkActivityTracker> m_networkActivityTrackers;
224 HashMap<ResourceLoadIdentifier, NetworkLoadInformation> m_networkLoadInformationByID;
228 RefPtr<NetworkRTCProvider> m_rtcProvider;
231 NetworkMDNSRegister m_mdnsRegister;
234 bool m_captureExtraNetworkLoadMetricsEnabled { false };
236 RefPtr<CacheStorageEngineConnection> m_cacheStorageConnection;
239 } // namespace WebKit