Add facility for tracking times and results of page and resource loading
[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     enum class LoadResult {
138         Unknown,
139         Success,
140         Failure,
141         Cancel
142     };
143     void cleanup(LoadResult);
144     
145     void platformDidReceiveResponse(const WebCore::ResourceResponse&);
146
147     void startBufferingTimerIfNeeded();
148     void bufferingTimerFired();
149     void sendBuffer(WebCore::SharedBuffer&, size_t encodedDataLength);
150
151     void consumeSandboxExtensions();
152     void invalidateSandboxExtensions();
153
154 #if HAVE(CFNETWORK_STORAGE_PARTITIONING) && !RELEASE_LOG_DISABLED
155     void logCookieInformation() const;
156 #endif
157
158     void continueWillSendRedirectedRequest(WebCore::ResourceRequest&& request, WebCore::ResourceRequest&& redirectRequest, WebCore::ResourceResponse&&);
159
160     WebCore::ResourceResponse sanitizeResponseIfPossible(WebCore::ResourceResponse&&, WebCore::ResourceResponse::SanitizationType);
161
162     const NetworkResourceLoadParameters m_parameters;
163
164     Ref<NetworkConnectionToWebProcess> m_connection;
165
166     std::unique_ptr<NetworkLoad> m_networkLoad;
167
168     WebCore::ResourceResponse m_response;
169
170     size_t m_bytesReceived { 0 };
171     size_t m_bufferedDataEncodedDataLength { 0 };
172     RefPtr<WebCore::SharedBuffer> m_bufferedData;
173     unsigned m_redirectCount { 0 };
174
175     std::unique_ptr<SynchronousLoadData> m_synchronousLoadData;
176     Vector<RefPtr<WebCore::BlobDataFileReference>> m_fileReferences;
177
178     bool m_wasStarted { false };
179     bool m_didConsumeSandboxExtensions { false };
180     bool m_defersLoading { false };
181     bool m_isAllowedToAskUserForCredentials { false };
182     size_t m_numBytesReceived { 0 };
183
184     unsigned m_retrievedDerivedDataCount { 0 };
185
186     WebCore::Timer m_bufferingTimer;
187     RefPtr<NetworkCache::Cache> m_cache;
188     RefPtr<WebCore::SharedBuffer> m_bufferedDataForCache;
189     std::unique_ptr<NetworkCache::Entry> m_cacheEntryForValidation;
190     bool m_isWaitingContinueWillSendRequestForCachedRedirect { false };
191     std::unique_ptr<NetworkCache::Entry> m_cacheEntryWaitingForContinueDidReceiveResponse;
192     RefPtr<NetworkLoadChecker> m_networkLoadChecker;
193
194     std::optional<NetworkActivityTracker> m_networkActivityTracker;
195 };
196
197 } // namespace WebKit
198
199 #endif // NetworkResourceLoader_h