2 * Copyright (C) 2012-2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef NetworkResourceLoader_h
27 #define NetworkResourceLoader_h
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/Timer.h>
38 class BlobDataFileReference;
39 class ResourceRequest;
44 class NetworkConnectionToWebProcess;
46 class SandboxExtension;
48 namespace NetworkCache {
52 class NetworkResourceLoader final : public RefCounted<NetworkResourceLoader>, public NetworkLoadClient, public IPC::MessageSender {
54 static Ref<NetworkResourceLoader> create(const NetworkResourceLoadParameters& parameters, NetworkConnectionToWebProcess& connection, RefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&& reply = nullptr)
56 return adoptRef(*new NetworkResourceLoader(parameters, connection, WTFMove(reply)));
58 virtual ~NetworkResourceLoader();
60 const WebCore::ResourceRequest& originalRequest() const { return m_parameters.request; }
62 NetworkLoad* networkLoad() const { return m_networkLoad.get(); }
67 void setDefersLoading(bool);
70 void didReceiveNetworkResourceLoaderMessage(IPC::Connection&, IPC::Decoder&);
72 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
73 void continueCanAuthenticateAgainstProtectionSpace(bool);
75 void continueWillSendRequest(WebCore::ResourceRequest&& newRequest, bool isAllowedToAskUserForCredentials);
77 const WebCore::ResourceResponse& response() const { return m_response; }
79 NetworkConnectionToWebProcess& connectionToWebProcess() { return m_connection; }
80 PAL::SessionID sessionID() const { return m_parameters.sessionID; }
81 ResourceLoadIdentifier identifier() const { return m_parameters.identifier; }
82 uint64_t frameID() const { return m_parameters.webFrameID; }
83 uint64_t pageID() const { return m_parameters.webPageID; }
85 struct SynchronousLoadData;
88 void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent) override;
89 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
90 void canAuthenticateAgainstProtectionSpaceAsync(const WebCore::ProtectionSpace&) override;
92 bool isSynchronous() const override;
93 bool isAllowedToAskUserForCredentials() const override { return m_isAllowedToAskUserForCredentials; }
94 void willSendRedirectedRequest(WebCore::ResourceRequest&&, WebCore::ResourceRequest&& redirectRequest, WebCore::ResourceResponse&&) override;
95 ShouldContinueDidReceiveResponse didReceiveResponse(WebCore::ResourceResponse&&) override;
96 void didReceiveBuffer(Ref<WebCore::SharedBuffer>&&, int reportedEncodedDataLength) override;
97 void didFinishLoading(const WebCore::NetworkLoadMetrics&) override;
98 void didFailLoading(const WebCore::ResourceError&) override;
99 bool shouldCaptureExtraNetworkLoadMetrics() const override;
101 void convertToDownload(DownloadID, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
103 bool isMainResource() const { return m_parameters.request.requester() == WebCore::ResourceRequest::Requester::Main; }
104 bool isAlwaysOnLoggingAllowed() const;
107 NetworkResourceLoader(const NetworkResourceLoadParameters&, NetworkConnectionToWebProcess&, RefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>&&);
109 // IPC::MessageSender
110 IPC::Connection* messageSenderConnection() override;
111 uint64_t messageSenderDestinationID() override { return m_parameters.identifier; }
113 bool canUseCache(const WebCore::ResourceRequest&) const;
114 bool canUseCachedRedirect(const WebCore::ResourceRequest&) const;
116 void tryStoreAsCacheEntry();
117 void retrieveCacheEntry(const WebCore::ResourceRequest&);
118 void didRetrieveCacheEntry(std::unique_ptr<NetworkCache::Entry>);
119 void sendResultForCacheEntry(std::unique_ptr<NetworkCache::Entry>);
120 void validateCacheEntry(std::unique_ptr<NetworkCache::Entry>);
121 void dispatchWillSendRequestForCacheEntry(std::unique_ptr<NetworkCache::Entry>);
123 void startNetworkLoad(const WebCore::ResourceRequest&);
124 void continueDidReceiveResponse();
128 void platformDidReceiveResponse(const WebCore::ResourceResponse&);
130 void startBufferingTimerIfNeeded();
131 void bufferingTimerFired();
132 void sendBuffer(WebCore::SharedBuffer&, size_t encodedDataLength);
134 void consumeSandboxExtensions();
135 void invalidateSandboxExtensions();
137 const NetworkResourceLoadParameters m_parameters;
139 Ref<NetworkConnectionToWebProcess> m_connection;
141 std::unique_ptr<NetworkLoad> m_networkLoad;
143 WebCore::ResourceResponse m_response;
145 size_t m_bytesReceived { 0 };
146 size_t m_bufferedDataEncodedDataLength { 0 };
147 RefPtr<WebCore::SharedBuffer> m_bufferedData;
148 unsigned m_redirectCount { 0 };
150 std::unique_ptr<SynchronousLoadData> m_synchronousLoadData;
151 Vector<RefPtr<WebCore::BlobDataFileReference>> m_fileReferences;
153 bool m_wasStarted { false };
154 bool m_didConsumeSandboxExtensions { false };
155 bool m_defersLoading { false };
156 bool m_isAllowedToAskUserForCredentials { false };
157 size_t m_numBytesReceived { 0 };
159 unsigned m_retrievedDerivedDataCount { 0 };
161 WebCore::Timer m_bufferingTimer;
162 RefPtr<NetworkCache::Cache> m_cache;
163 RefPtr<WebCore::SharedBuffer> m_bufferedDataForCache;
164 std::unique_ptr<NetworkCache::Entry> m_cacheEntryForValidation;
165 bool m_isWaitingContinueWillSendRequestForCachedRedirect { false };
168 } // namespace WebKit
170 #endif // NetworkResourceLoader_h