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