Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebKit / NetworkProcess / NetworkResourceLoader.h
1 /*
2  * Copyright (C) 2012-2015 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 #ifndef NetworkResourceLoader_h
27 #define NetworkResourceLoader_h
28
29 #include "DownloadID.h"
30 #include "MessageSender.h"
31 #include "NetworkConnectionToWebProcessMessages.h"
32 #include "NetworkLoadClient.h"
33 #include "NetworkResourceLoadParameters.h"
34 #include "ShareableResource.h"
35 #include <JavaScriptCore/ConsoleTypes.h>
36 #include <WebCore/ContentSecurityPolicyClient.h>
37 #include <WebCore/ResourceResponse.h>
38 #include <WebCore/SecurityPolicyViolationEvent.h>
39 #include <WebCore/Timer.h>
40
41 namespace WebCore {
42 class BlobDataFileReference;
43 class FormData;
44 class NetworkStorageSession;
45 class ResourceRequest;
46 }
47
48 namespace WebKit {
49
50 class NetworkConnectionToWebProcess;
51 class NetworkLoad;
52 class NetworkLoadChecker;
53 class SandboxExtension;
54
55 namespace NetworkCache {
56 class Entry;
57 }
58
59 class NetworkResourceLoader final
60     : public RefCounted<NetworkResourceLoader>
61     , public NetworkLoadClient
62     , public IPC::MessageSender
63     , public WebCore::ContentSecurityPolicyClient {
64 public:
65     static Ref<NetworkResourceLoader> create(NetworkResourceLoadParameters&& parameters, NetworkConnectionToWebProcess& connection, Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply&& reply = nullptr)
66     {
67         return adoptRef(*new NetworkResourceLoader(WTFMove(parameters), connection, WTFMove(reply)));
68     }
69     virtual ~NetworkResourceLoader();
70
71     const WebCore::ResourceRequest& originalRequest() const { return m_parameters.request; }
72
73     NetworkLoad* networkLoad() const { return m_networkLoad.get(); }
74
75     void start();
76     void abort();
77
78     void setDefersLoading(bool);
79
80     // Message handlers.
81     void didReceiveNetworkResourceLoaderMessage(IPC::Connection&, IPC::Decoder&);
82
83     void continueWillSendRequest(WebCore::ResourceRequest&& newRequest, bool isAllowedToAskUserForCredentials);
84
85     const WebCore::ResourceResponse& response() const { return m_response; }
86
87     NetworkConnectionToWebProcess& connectionToWebProcess() { return m_connection; }
88     PAL::SessionID sessionID() const { return m_parameters.sessionID; }
89     ResourceLoadIdentifier identifier() const { return m_parameters.identifier; }
90     uint64_t frameID() const { return m_parameters.webFrameID; }
91     uint64_t pageID() const { return m_parameters.webPageID; }
92
93     struct SynchronousLoadData;
94
95     // NetworkLoadClient.
96     void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) override;
97     bool isSynchronous() const override;
98     bool isAllowedToAskUserForCredentials() const override { return m_isAllowedToAskUserForCredentials; }
99     void willSendRedirectedRequest(WebCore::ResourceRequest&&, WebCore::ResourceRequest&& redirectRequest, WebCore::ResourceResponse&&) override;
100     void didReceiveResponse(WebCore::ResourceResponse&&, ResponseCompletionHandler&&) override;
101     void didReceiveBuffer(Ref<WebCore::SharedBuffer>&&, int reportedEncodedDataLength) override;
102     void didFinishLoading(const WebCore::NetworkLoadMetrics&) override;
103     void didFailLoading(const WebCore::ResourceError&) override;
104     void didBlockAuthenticationChallenge() override;
105     bool shouldCaptureExtraNetworkLoadMetrics() const override;
106
107     void convertToDownload(DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
108
109     bool isMainResource() const { return m_parameters.request.requester() == WebCore::ResourceRequest::Requester::Main; }
110     bool isMainFrameLoad() const { return isMainResource() && m_parameters.frameAncestorOrigins.isEmpty(); }
111
112     bool isAlwaysOnLoggingAllowed() const;
113
114 #if ENABLE(RESOURCE_LOAD_STATISTICS) && !RELEASE_LOG_DISABLED
115     static bool shouldLogCookieInformation(const PAL::SessionID&);
116     static void logCookieInformation(const String& label, const void* loggedObject, const WebCore::NetworkStorageSession&, const URL& firstParty, const WebCore::SameSiteInfo&, const URL&, const String& referrer, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, std::optional<uint64_t> identifier);
117 #endif
118
119 private:
120     NetworkResourceLoader(NetworkResourceLoadParameters&&, NetworkConnectionToWebProcess&, Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply&&);
121
122     // IPC::MessageSender
123     IPC::Connection* messageSenderConnection() override;
124     uint64_t messageSenderDestinationID() override { return m_parameters.identifier; }
125
126     bool canUseCache(const WebCore::ResourceRequest&) const;
127     bool canUseCachedRedirect(const WebCore::ResourceRequest&) const;
128
129     void tryStoreAsCacheEntry();
130     void retrieveCacheEntry(const WebCore::ResourceRequest&);
131     void didRetrieveCacheEntry(std::unique_ptr<NetworkCache::Entry>);
132     void sendResultForCacheEntry(std::unique_ptr<NetworkCache::Entry>);
133     void validateCacheEntry(std::unique_ptr<NetworkCache::Entry>);
134     void dispatchWillSendRequestForCacheEntry(WebCore::ResourceRequest&&, std::unique_ptr<NetworkCache::Entry>&&);
135     void continueProcessingCachedEntryAfterDidReceiveResponse(std::unique_ptr<NetworkCache::Entry>);
136
137     bool shouldInterruptLoadForXFrameOptions(const String&, const URL&);
138     bool shouldInterruptLoadForCSPFrameAncestorsOrXFrameOptions(const WebCore::ResourceResponse&);
139
140     enum class FirstLoad { No, Yes };
141     void startNetworkLoad(WebCore::ResourceRequest&&, FirstLoad);
142     void restartNetworkLoad(WebCore::ResourceRequest&&);
143     void continueDidReceiveResponse();
144
145     enum class LoadResult {
146         Unknown,
147         Success,
148         Failure,
149         Cancel
150     };
151     void cleanup(LoadResult);
152     
153     void platformDidReceiveResponse(const WebCore::ResourceResponse&);
154
155     void startBufferingTimerIfNeeded();
156     void bufferingTimerFired();
157     void sendBuffer(WebCore::SharedBuffer&, size_t encodedDataLength);
158
159     void consumeSandboxExtensions();
160     void invalidateSandboxExtensions();
161
162 #if ENABLE(RESOURCE_LOAD_STATISTICS) && !RELEASE_LOG_DISABLED
163     void logCookieInformation() const;
164 #endif
165
166     void continueWillSendRedirectedRequest(WebCore::ResourceRequest&& request, WebCore::ResourceRequest&& redirectRequest, WebCore::ResourceResponse&&);
167     void didFinishWithRedirectResponse(WebCore::ResourceResponse&&);
168     WebCore::ResourceResponse sanitizeResponseIfPossible(WebCore::ResourceResponse&&, WebCore::ResourceResponse::SanitizationType);
169
170     // ContentSecurityPolicyClient
171     void addConsoleMessage(MessageSource, MessageLevel, const String&, unsigned long) final;
172     void sendCSPViolationReport(URL&&, Ref<WebCore::FormData>&&) final;
173     void enqueueSecurityPolicyViolationEvent(WebCore::SecurityPolicyViolationEvent::Init&&) final;
174
175     void logSlowCacheRetrieveIfNeeded(const NetworkCache::Cache::RetrieveInfo&);
176
177     std::optional<Seconds> validateCacheEntryForMaxAgeCapValidation(const WebCore::ResourceRequest&, const WebCore::ResourceRequest& redirectRequest, const WebCore::ResourceResponse&);
178
179     const NetworkResourceLoadParameters m_parameters;
180
181     Ref<NetworkConnectionToWebProcess> m_connection;
182
183     std::unique_ptr<NetworkLoad> m_networkLoad;
184
185     WebCore::ResourceResponse m_response;
186
187     size_t m_bytesReceived { 0 };
188     size_t m_bufferedDataEncodedDataLength { 0 };
189     RefPtr<WebCore::SharedBuffer> m_bufferedData;
190     unsigned m_redirectCount { 0 };
191
192     std::unique_ptr<SynchronousLoadData> m_synchronousLoadData;
193     Vector<RefPtr<WebCore::BlobDataFileReference>> m_fileReferences;
194
195     bool m_wasStarted { false };
196     bool m_didConsumeSandboxExtensions { false };
197     bool m_defersLoading { false };
198     bool m_isAllowedToAskUserForCredentials { false };
199     size_t m_numBytesReceived { 0 };
200
201     unsigned m_retrievedDerivedDataCount { 0 };
202
203     WebCore::Timer m_bufferingTimer;
204     RefPtr<NetworkCache::Cache> m_cache;
205     RefPtr<WebCore::SharedBuffer> m_bufferedDataForCache;
206     std::unique_ptr<NetworkCache::Entry> m_cacheEntryForValidation;
207     std::unique_ptr<NetworkCache::Entry> m_cacheEntryForMaxAgeCapValidation;
208     bool m_isWaitingContinueWillSendRequestForCachedRedirect { false };
209     std::unique_ptr<NetworkCache::Entry> m_cacheEntryWaitingForContinueDidReceiveResponse;
210     std::unique_ptr<NetworkLoadChecker> m_networkLoadChecker;
211     bool m_shouldRestartLoad { false };
212     ResponseCompletionHandler m_responseCompletionHandler;
213
214     std::optional<NetworkActivityTracker> m_networkActivityTracker;
215 };
216
217 } // namespace WebKit
218
219 #endif // NetworkResourceLoader_h