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