Make MessageSender functions const
[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() const { 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(NetworkConnectionToWebProcess&, const PAL::SessionID&);
113     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);
114 #endif
115
116     void disableExtraNetworkLoadMetricsCapture() { m_shouldCaptureExtraNetworkLoadMetrics = false; }
117
118 private:
119     NetworkResourceLoader(NetworkResourceLoadParameters&&, NetworkConnectionToWebProcess&, Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply&&);
120
121     // IPC::MessageSender
122     IPC::Connection* messageSenderConnection() const override;
123     uint64_t messageSenderDestinationID() const override { return m_parameters.identifier; }
124
125     bool canUseCache(const WebCore::ResourceRequest&) const;
126     bool canUseCachedRedirect(const WebCore::ResourceRequest&) const;
127
128     void tryStoreAsCacheEntry();
129     void retrieveCacheEntry(const WebCore::ResourceRequest&);
130     void didRetrieveCacheEntry(std::unique_ptr<NetworkCache::Entry>);
131     void sendResultForCacheEntry(std::unique_ptr<NetworkCache::Entry>);
132     void validateCacheEntry(std::unique_ptr<NetworkCache::Entry>);
133     void dispatchWillSendRequestForCacheEntry(WebCore::ResourceRequest&&, std::unique_ptr<NetworkCache::Entry>&&);
134
135     bool shouldInterruptLoadForXFrameOptions(const String&, const URL&);
136     bool shouldInterruptLoadForCSPFrameAncestorsOrXFrameOptions(const WebCore::ResourceResponse&);
137
138     enum class FirstLoad { No, Yes };
139     void startNetworkLoad(WebCore::ResourceRequest&&, FirstLoad);
140     void restartNetworkLoad(WebCore::ResourceRequest&&);
141     void continueDidReceiveResponse();
142
143     enum class LoadResult {
144         Unknown,
145         Success,
146         Failure,
147         Cancel
148     };
149     void cleanup(LoadResult);
150     
151     void platformDidReceiveResponse(const WebCore::ResourceResponse&);
152
153     void startBufferingTimerIfNeeded();
154     void bufferingTimerFired();
155     void sendBuffer(WebCore::SharedBuffer&, size_t encodedDataLength);
156
157     void consumeSandboxExtensions();
158     void invalidateSandboxExtensions();
159
160 #if ENABLE(RESOURCE_LOAD_STATISTICS) && !RELEASE_LOG_DISABLED
161     void logCookieInformation() const;
162 #endif
163
164     void continueWillSendRedirectedRequest(WebCore::ResourceRequest&&, WebCore::ResourceRequest&& redirectRequest, WebCore::ResourceResponse&&);
165     void didFinishWithRedirectResponse(WebCore::ResourceResponse&&);
166     WebCore::ResourceResponse sanitizeResponseIfPossible(WebCore::ResourceResponse&&, WebCore::ResourceResponse::SanitizationType);
167
168     // ContentSecurityPolicyClient
169     void addConsoleMessage(MessageSource, MessageLevel, const String&, unsigned long) final;
170     void sendCSPViolationReport(URL&&, Ref<WebCore::FormData>&&) final;
171     void enqueueSecurityPolicyViolationEvent(WebCore::SecurityPolicyViolationEvent::Init&&) final;
172
173     void logSlowCacheRetrieveIfNeeded(const NetworkCache::Cache::RetrieveInfo&);
174
175     Optional<Seconds> validateCacheEntryForMaxAgeCapValidation(const WebCore::ResourceRequest&, const WebCore::ResourceRequest& redirectRequest, const WebCore::ResourceResponse&);
176
177     const NetworkResourceLoadParameters m_parameters;
178
179     Ref<NetworkConnectionToWebProcess> m_connection;
180
181     std::unique_ptr<NetworkLoad> m_networkLoad;
182
183     WebCore::ResourceResponse m_response;
184
185     size_t m_bufferedDataEncodedDataLength { 0 };
186     RefPtr<WebCore::SharedBuffer> m_bufferedData;
187     unsigned m_redirectCount { 0 };
188
189     std::unique_ptr<SynchronousLoadData> m_synchronousLoadData;
190     Vector<RefPtr<WebCore::BlobDataFileReference>> m_fileReferences;
191
192     bool m_wasStarted { false };
193     bool m_didConsumeSandboxExtensions { false };
194     bool m_defersLoading { 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