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