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