8769708c0a200dd261bf16ff92669e8a2364548a
[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 <WebCore/ResourceResponse.h>
36 #include <WebCore/Timer.h>
37
38 namespace WebCore {
39 class BlobDataFileReference;
40 class NetworkStorageSession;
41 class ResourceRequest;
42 }
43
44 namespace WebKit {
45
46 class NetworkConnectionToWebProcess;
47 class NetworkLoad;
48 class NetworkLoadChecker;
49 class SandboxExtension;
50
51 namespace NetworkCache {
52 class Entry;
53 }
54
55 class NetworkResourceLoader final : public RefCounted<NetworkResourceLoader>, public NetworkLoadClient, public IPC::MessageSender {
56 public:
57     static Ref<NetworkResourceLoader> create(NetworkResourceLoadParameters&& parameters, NetworkConnectionToWebProcess& connection, RefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&& reply = nullptr)
58     {
59         return adoptRef(*new NetworkResourceLoader(WTFMove(parameters), connection, WTFMove(reply)));
60     }
61     virtual ~NetworkResourceLoader();
62
63     const WebCore::ResourceRequest& originalRequest() const { return m_parameters.request; }
64
65     NetworkLoad* networkLoad() const { return m_networkLoad.get(); }
66
67     void start();
68     void abort();
69
70     void setDefersLoading(bool);
71
72     // Message handlers.
73     void didReceiveNetworkResourceLoaderMessage(IPC::Connection&, IPC::Decoder&);
74
75 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
76     void continueCanAuthenticateAgainstProtectionSpace(bool);
77 #endif
78     void continueWillSendRequest(WebCore::ResourceRequest&& newRequest, bool isAllowedToAskUserForCredentials);
79
80     const WebCore::ResourceResponse& response() const { return m_response; }
81
82     NetworkConnectionToWebProcess& connectionToWebProcess() { return m_connection; }
83     PAL::SessionID sessionID() const { return m_parameters.sessionID; }
84     ResourceLoadIdentifier identifier() const { return m_parameters.identifier; }
85     uint64_t frameID() const { return m_parameters.webFrameID; }
86     uint64_t pageID() const { return m_parameters.webPageID; }
87
88     struct SynchronousLoadData;
89
90     // NetworkLoadClient.
91     void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) override;
92 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
93     void canAuthenticateAgainstProtectionSpaceAsync(const WebCore::ProtectionSpace&) override;
94 #endif
95     bool isSynchronous() const override;
96     bool isAllowedToAskUserForCredentials() const override { return m_isAllowedToAskUserForCredentials; }
97     void willSendRedirectedRequest(WebCore::ResourceRequest&&, WebCore::ResourceRequest&& redirectRequest, WebCore::ResourceResponse&&) override;
98     ShouldContinueDidReceiveResponse didReceiveResponse(WebCore::ResourceResponse&&) override;
99     void didReceiveBuffer(Ref<WebCore::SharedBuffer>&&, int reportedEncodedDataLength) override;
100     void didFinishLoading(const WebCore::NetworkLoadMetrics&) override;
101     void didFailLoading(const WebCore::ResourceError&) override;
102     void didBlockAuthenticationChallenge() override;
103     bool shouldCaptureExtraNetworkLoadMetrics() const override;
104
105     void convertToDownload(DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
106
107     bool isMainResource() const { return m_parameters.request.requester() == WebCore::ResourceRequest::Requester::Main; }
108     bool isAlwaysOnLoggingAllowed() const;
109
110 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
111     static bool shouldLogCookieInformation();
112     static void logCookieInformation(const String& label, const void* loggedObject, const WebCore::NetworkStorageSession&, const WebCore::URL& firstParty, const WebCore::SameSiteInfo&, const WebCore::URL&, const String& referrer, std::optional<uint64_t> frameID, std::optional<uint64_t> pageID, std::optional<uint64_t> identifier);
113 #endif
114
115 private:
116     NetworkResourceLoader(NetworkResourceLoadParameters&&, NetworkConnectionToWebProcess&, RefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&&);
117
118     // IPC::MessageSender
119     IPC::Connection* messageSenderConnection() override;
120     uint64_t messageSenderDestinationID() override { return m_parameters.identifier; }
121
122     bool canUseCache(const WebCore::ResourceRequest&) const;
123     bool canUseCachedRedirect(const WebCore::ResourceRequest&) const;
124
125     void tryStoreAsCacheEntry();
126     void retrieveCacheEntry(const WebCore::ResourceRequest&);
127     void didRetrieveCacheEntry(std::unique_ptr<NetworkCache::Entry>);
128     void sendResultForCacheEntry(std::unique_ptr<NetworkCache::Entry>);
129     void validateCacheEntry(std::unique_ptr<NetworkCache::Entry>);
130     void dispatchWillSendRequestForCacheEntry(std::unique_ptr<NetworkCache::Entry>);
131     void continueProcessingCachedEntryAfterDidReceiveResponse(std::unique_ptr<NetworkCache::Entry>);
132
133     enum class FirstLoad { No, Yes };
134     void startNetworkLoad(WebCore::ResourceRequest&&, FirstLoad);
135     void continueDidReceiveResponse();
136
137     void cleanup();
138     
139     void platformDidReceiveResponse(const WebCore::ResourceResponse&);
140
141     void startBufferingTimerIfNeeded();
142     void bufferingTimerFired();
143     void sendBuffer(WebCore::SharedBuffer&, size_t encodedDataLength);
144
145     void consumeSandboxExtensions();
146     void invalidateSandboxExtensions();
147
148 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
149     void logCookieInformation() const;
150 #endif
151
152     void continueWillSendRedirectedRequest(WebCore::ResourceRequest&& request, WebCore::ResourceRequest&& redirectRequest, WebCore::ResourceResponse&&);
153
154     WebCore::ResourceResponse sanitizeResponseIfPossible(WebCore::ResourceResponse&&, WebCore::ResourceResponse::SanitizationType);
155
156     const NetworkResourceLoadParameters m_parameters;
157
158     Ref<NetworkConnectionToWebProcess> m_connection;
159
160     std::unique_ptr<NetworkLoad> m_networkLoad;
161
162     WebCore::ResourceResponse m_response;
163
164     size_t m_bytesReceived { 0 };
165     size_t m_bufferedDataEncodedDataLength { 0 };
166     RefPtr<WebCore::SharedBuffer> m_bufferedData;
167     unsigned m_redirectCount { 0 };
168
169     std::unique_ptr<SynchronousLoadData> m_synchronousLoadData;
170     Vector<RefPtr<WebCore::BlobDataFileReference>> m_fileReferences;
171
172     bool m_wasStarted { false };
173     bool m_didConsumeSandboxExtensions { false };
174     bool m_defersLoading { false };
175     bool m_isAllowedToAskUserForCredentials { false };
176     size_t m_numBytesReceived { 0 };
177
178     unsigned m_retrievedDerivedDataCount { 0 };
179
180     WebCore::Timer m_bufferingTimer;
181     RefPtr<NetworkCache::Cache> m_cache;
182     RefPtr<WebCore::SharedBuffer> m_bufferedDataForCache;
183     std::unique_ptr<NetworkCache::Entry> m_cacheEntryForValidation;
184     bool m_isWaitingContinueWillSendRequestForCachedRedirect { false };
185     std::unique_ptr<NetworkCache::Entry> m_cacheEntryWaitingForContinueDidReceiveResponse;
186     RefPtr<NetworkLoadChecker> m_networkLoadChecker;
187 };
188
189 } // namespace WebKit
190
191 #endif // NetworkResourceLoader_h