Share more code between NETWORK_SESSION and non-NETWORK_SESSION NetworkResourceLoaders
[WebKit-https.git] / Source / WebKit2 / NetworkProcess / NetworkResourceLoader.h
1 /*
2  * Copyright (C) 2012-2014 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 #if ENABLE(NETWORK_PROCESS)
30
31 #include "MessageSender.h"
32 #include "NetworkCache.h"
33 #include "NetworkConnectionToWebProcessMessages.h"
34 #include "NetworkResourceLoadParameters.h"
35 #include "ShareableResource.h"
36 #include <WebCore/CacheValidation.h>
37 #include <WebCore/ResourceError.h>
38 #include <WebCore/ResourceLoaderOptions.h>
39 #include <WebCore/ResourceRequest.h>
40 #include <WebCore/ResourceResponse.h>
41 #include <WebCore/SessionID.h>
42 #include <WebCore/Timer.h>
43 #include <wtf/MainThread.h>
44 #include <wtf/RunLoop.h>
45
46 #if USE(NETWORK_SESSION)
47 #include "NetworkSession.h"
48 #else
49 #include <WebCore/ResourceHandleClient.h>
50 #endif
51
52 typedef const struct _CFCachedURLResponse* CFCachedURLResponseRef;
53
54 namespace WebCore {
55 class BlobDataFileReference;
56 #if !USE(NETWORK_SESSION)
57 class ResourceHandle;
58 #endif
59 class ResourceRequest;
60 }
61
62 namespace WebKit {
63
64 class NetworkConnectionToWebProcess;
65 class NetworkResourceLoadParameters;
66 class RemoteNetworkingContext;
67 class SandboxExtension;
68
69 class NetworkResourceLoader
70     : public RefCounted<NetworkResourceLoader>
71 #if USE(NETWORK_SESSION)
72     , public NetworkSessionTaskClient
73 #else
74     , public WebCore::ResourceHandleClient
75 #endif
76     , public IPC::MessageSender {
77 public:
78     static Ref<NetworkResourceLoader> create(const NetworkResourceLoadParameters& parameters, NetworkConnectionToWebProcess* connection)
79     {
80         return adoptRef(*new NetworkResourceLoader(parameters, connection, nullptr));
81     }
82     
83     static Ref<NetworkResourceLoader> create(const NetworkResourceLoadParameters& parameters, NetworkConnectionToWebProcess* connection, PassRefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply> reply)
84     {
85         return adoptRef(*new NetworkResourceLoader(parameters, connection, reply));
86     }    
87     ~NetworkResourceLoader();
88
89     const WebCore::ResourceRequest& originalRequest() const { return m_parameters.request; }
90
91     // Changes with redirects.
92     WebCore::ResourceRequest& currentRequest() { return m_currentRequest; }
93
94 #if !USE(NETWORK_SESSION)
95     WebCore::ResourceHandle* handle() const { return m_handle.get(); }
96 #endif
97     void didConvertHandleToDownload();
98
99     void start();
100     void abort();
101
102     void setDefersLoading(bool);
103
104 #if PLATFORM(COCOA)
105     static size_t fileBackedResourceMinimumSize();
106 #endif
107     // Message handlers.
108     void didReceiveNetworkResourceLoaderMessage(IPC::Connection&, IPC::MessageDecoder&);
109
110 #if PLATFORM(COCOA)
111     static void tryGetShareableHandleFromCFURLCachedResponse(ShareableResource::Handle&, CFCachedURLResponseRef);
112     static void tryGetShareableHandleFromSharedBuffer(ShareableResource::Handle&, WebCore::SharedBuffer&);
113 #endif
114
115 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
116     void continueCanAuthenticateAgainstProtectionSpace(bool);
117 #endif
118     void continueWillSendRequest(const WebCore::ResourceRequest& newRequest);
119
120     WebCore::SharedBuffer* bufferedData() { return m_bufferedData.get(); }
121     const WebCore::ResourceResponse& response() const { return m_response; }
122
123     NetworkConnectionToWebProcess* connectionToWebProcess() const { return m_connection.get(); }
124     WebCore::SessionID sessionID() const { return m_parameters.sessionID; }
125     ResourceLoadIdentifier identifier() const { return m_parameters.identifier; }
126
127     struct SynchronousLoadData;
128
129 private:
130     NetworkResourceLoader(const NetworkResourceLoadParameters&, NetworkConnectionToWebProcess*, PassRefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply>);
131
132     // IPC::MessageSender
133     virtual IPC::Connection* messageSenderConnection() override;
134     virtual uint64_t messageSenderDestinationID() override { return m_parameters.identifier; }
135
136     enum ShouldContinueDidReceiveResponse {
137         Yes,
138         No
139     };
140     ShouldContinueDidReceiveResponse sharedDidReceiveResponse(const WebCore::ResourceResponse&);
141         void sharedWillSendRedirectedRequest(const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
142     void sharedDidReceiveBuffer(RefPtr<WebCore::SharedBuffer>&&, int reportedEncodedDataLength);
143     void sharedDidFail(const WebCore::ResourceError&);
144     void sharedDidFinishLoading(double finishTime);
145
146 #if USE(NETWORK_SESSION)
147     // NetworkSessionTaskClient.
148     virtual void willPerformHTTPRedirection(const WebCore::ResourceResponse&, const WebCore::ResourceRequest&, std::function<void(const WebCore::ResourceRequest&)>) final override;
149     virtual void didReceiveChallenge(const WebCore::AuthenticationChallenge&, std::function<void(AuthenticationChallengeDisposition, const WebCore::Credential&)>) final override;
150     virtual void didReceiveResponse(const WebCore::ResourceResponse&, std::function<void(ResponseDisposition)>) final override;
151     virtual void didReceiveData(RefPtr<WebCore::SharedBuffer>&&) final override;
152     virtual void didCompleteWithError(const WebCore::ResourceError&) final override;
153 #else
154     // ResourceHandleClient
155     virtual void willSendRequestAsync(WebCore::ResourceHandle*, const WebCore::ResourceRequest&, const WebCore::ResourceResponse& redirectResponse) override;
156     virtual void didSendData(WebCore::ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) override;
157     virtual void didReceiveResponseAsync(WebCore::ResourceHandle*, const WebCore::ResourceResponse&) override;
158     virtual void didReceiveData(WebCore::ResourceHandle*, const char*, unsigned, int encodedDataLength) override;
159     virtual void didReceiveBuffer(WebCore::ResourceHandle*, PassRefPtr<WebCore::SharedBuffer>, int encodedDataLength) override;
160     virtual void didFinishLoading(WebCore::ResourceHandle*, double finishTime) override;
161     virtual void didFail(WebCore::ResourceHandle*, const WebCore::ResourceError&) override;
162     virtual void wasBlocked(WebCore::ResourceHandle*) override;
163     virtual void cannotShowURL(WebCore::ResourceHandle*) override;
164     virtual bool shouldUseCredentialStorage(WebCore::ResourceHandle*) override;
165     virtual void didReceiveAuthenticationChallenge(WebCore::ResourceHandle*, const WebCore::AuthenticationChallenge&) override;
166     virtual void didCancelAuthenticationChallenge(WebCore::ResourceHandle*, const WebCore::AuthenticationChallenge&) override;
167     virtual void receivedCancellation(WebCore::ResourceHandle*, const WebCore::AuthenticationChallenge&) override;
168     virtual bool usesAsyncCallbacks() override { return true; }
169     virtual bool loadingSynchronousXHR() override { return isSynchronous(); }
170
171 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
172     virtual void canAuthenticateAgainstProtectionSpaceAsync(WebCore::ResourceHandle*, const WebCore::ProtectionSpace&) override;
173 #endif
174 #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
175     virtual bool supportsDataArray() override;
176     virtual void didReceiveDataArray(WebCore::ResourceHandle*, CFArrayRef) override;
177 #endif
178 #if PLATFORM(COCOA)
179 #if USE(CFNETWORK)
180     virtual void willCacheResponseAsync(WebCore::ResourceHandle*, CFCachedURLResponseRef) override;
181 #else
182     virtual void willCacheResponseAsync(WebCore::ResourceHandle*, NSCachedURLResponse *) override;
183 #endif
184 #endif
185 #endif // USE(NETWORK_SESSION)
186
187 #if ENABLE(NETWORK_CACHE)
188     void didRetrieveCacheEntry(std::unique_ptr<NetworkCache::Entry>);
189     void validateCacheEntry(std::unique_ptr<NetworkCache::Entry>);
190 #endif
191
192     void startNetworkLoad();
193     void continueDidReceiveResponse();
194
195     void cleanup();
196     
197     void platformDidReceiveResponse(const WebCore::ResourceResponse&);
198
199     void startBufferingTimerIfNeeded();
200     void bufferingTimerFired();
201     bool sendBufferMaybeAborting(const WebCore::SharedBuffer&, size_t encodedDataLength);
202
203     bool isSynchronous() const;
204
205     void consumeSandboxExtensions();
206     void invalidateSandboxExtensions();
207
208     template<typename T> bool sendAbortingOnFailure(T&& message, unsigned messageSendFlags = 0);
209
210     const NetworkResourceLoadParameters m_parameters;
211
212     RefPtr<NetworkConnectionToWebProcess> m_connection;
213
214     RefPtr<RemoteNetworkingContext> m_networkingContext;
215 #if USE(NETWORK_SESSION)
216     RefPtr<NetworkingDataTask> m_task;
217 #else
218     RefPtr<WebCore::ResourceHandle> m_handle;
219 #endif
220
221     WebCore::ResourceRequest m_currentRequest;
222     WebCore::ResourceResponse m_response;
223
224     size_t m_bytesReceived;
225     size_t m_bufferedDataEncodedDataLength;
226     RefPtr<WebCore::SharedBuffer> m_bufferedData;
227
228     std::unique_ptr<SynchronousLoadData> m_synchronousLoadData;
229     Vector<RefPtr<WebCore::BlobDataFileReference>> m_fileReferences;
230
231     bool m_didConvertHandleToDownload;
232     bool m_didConsumeSandboxExtensions;
233     bool m_defersLoading;
234
235     WebCore::Timer m_bufferingTimer;
236 #if ENABLE(NETWORK_CACHE)
237     RefPtr<WebCore::SharedBuffer> m_bufferedDataForCache;
238     std::unique_ptr<NetworkCache::Entry> m_cacheEntryForValidation;
239
240     WebCore::RedirectChainCacheStatus m_redirectChainCacheStatus;
241 #endif
242 };
243
244 } // namespace WebKit
245
246 #endif // ENABLE(NETWORK_PROCESS)
247
248 #endif // NetworkResourceLoader_h