1 | /*
|
---|
2 | * Copyright (C) 2005-2017 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 | *
|
---|
8 | * 1. Redistributions of source code must retain the above copyright
|
---|
9 | * notice, this list of conditions and the following disclaimer.
|
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer in the
|
---|
12 | * documentation and/or other materials provided with the distribution.
|
---|
13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of
|
---|
14 | * its contributors may be used to endorse or promote products derived
|
---|
15 | * from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
|
---|
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
|
---|
21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #pragma once
|
---|
30 |
|
---|
31 | #include "ResourceHandleClient.h"
|
---|
32 | #include "ResourceLoadTiming.h"
|
---|
33 | #include "ResourceLoaderIdentifier.h"
|
---|
34 | #include "ResourceLoaderOptions.h"
|
---|
35 | #include "ResourceLoaderTypes.h"
|
---|
36 | #include "ResourceRequest.h"
|
---|
37 | #include "ResourceResponse.h"
|
---|
38 | #include "SharedBuffer.h"
|
---|
39 | #include <wtf/Forward.h>
|
---|
40 | #include <wtf/WeakPtr.h>
|
---|
41 |
|
---|
42 | #if ENABLE(CONTENT_EXTENSIONS)
|
---|
43 | #include "ResourceLoadInfo.h"
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | namespace WTF {
|
---|
47 | class SchedulePair;
|
---|
48 | }
|
---|
49 |
|
---|
50 | namespace WebCore {
|
---|
51 |
|
---|
52 | class AuthenticationChallenge;
|
---|
53 | class CachedResource;
|
---|
54 | class DocumentLoader;
|
---|
55 | class Frame;
|
---|
56 | class FrameLoader;
|
---|
57 | class LegacyPreviewLoader;
|
---|
58 | class NetworkLoadMetrics;
|
---|
59 |
|
---|
60 | DECLARE_ALLOCATOR_WITH_HEAP_IDENTIFIER(ResourceLoader);
|
---|
61 | class ResourceLoader : public CanMakeWeakPtr<ResourceLoader>, public RefCounted<ResourceLoader>, protected ResourceHandleClient {
|
---|
62 | WTF_MAKE_FAST_ALLOCATED_WITH_HEAP_IDENTIFIER(ResourceLoader);
|
---|
63 | public:
|
---|
64 | virtual ~ResourceLoader() = 0;
|
---|
65 |
|
---|
66 | WEBCORE_EXPORT void cancel();
|
---|
67 |
|
---|
68 | virtual void init(ResourceRequest&&, CompletionHandler<void(bool)>&&);
|
---|
69 |
|
---|
70 | void deliverResponseAndData(const ResourceResponse&, RefPtr<FragmentedSharedBuffer>&&);
|
---|
71 |
|
---|
72 | #if PLATFORM(IOS_FAMILY)
|
---|
73 | virtual void startLoading()
|
---|
74 | {
|
---|
75 | start();
|
---|
76 | }
|
---|
77 |
|
---|
78 | virtual const ResourceRequest& iOSOriginalRequest() const { return request(); }
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | WEBCORE_EXPORT FrameLoader* frameLoader() const;
|
---|
82 | DocumentLoader* documentLoader() const { return m_documentLoader.get(); }
|
---|
83 | const ResourceRequest& originalRequest() const { return m_originalRequest; }
|
---|
84 |
|
---|
85 | WEBCORE_EXPORT void start();
|
---|
86 | WEBCORE_EXPORT void cancel(const ResourceError&);
|
---|
87 | WEBCORE_EXPORT ResourceError cancelledError();
|
---|
88 | WEBCORE_EXPORT ResourceError blockedError();
|
---|
89 | ResourceError blockedByContentBlockerError();
|
---|
90 | ResourceError cannotShowURLError();
|
---|
91 |
|
---|
92 | virtual void setDefersLoading(bool);
|
---|
93 | bool defersLoading() const { return m_defersLoading; }
|
---|
94 |
|
---|
95 | ResourceLoaderIdentifier identifier() const { return m_identifier; }
|
---|
96 |
|
---|
97 | bool wasAuthenticationChallengeBlocked() const { return m_wasAuthenticationChallengeBlocked; }
|
---|
98 |
|
---|
99 | virtual void releaseResources();
|
---|
100 | const ResourceResponse& response() const { return m_response; }
|
---|
101 |
|
---|
102 | const FragmentedSharedBuffer* resourceData() const;
|
---|
103 | void clearResourceData();
|
---|
104 |
|
---|
105 | virtual bool isSubresourceLoader() const;
|
---|
106 |
|
---|
107 | virtual void willSendRequest(ResourceRequest&&, const ResourceResponse& redirectResponse, CompletionHandler<void(ResourceRequest&&)>&& callback);
|
---|
108 | virtual void didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent);
|
---|
109 | virtual void didReceiveResponse(const ResourceResponse&, CompletionHandler<void()>&& policyCompletionHandler);
|
---|
110 | virtual void didReceiveData(const SharedBuffer&, long long encodedDataLength, DataPayloadType);
|
---|
111 | virtual void didReceiveBuffer(const FragmentedSharedBuffer&, long long encodedDataLength, DataPayloadType);
|
---|
112 | virtual void didFinishLoading(const NetworkLoadMetrics&);
|
---|
113 | virtual void didFail(const ResourceError&);
|
---|
114 |
|
---|
115 | WEBCORE_EXPORT void didBlockAuthenticationChallenge();
|
---|
116 |
|
---|
117 | virtual bool shouldUseCredentialStorage();
|
---|
118 | #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
|
---|
119 | virtual bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
|
---|
120 | #endif
|
---|
121 | virtual void receivedCancellation(const AuthenticationChallenge&);
|
---|
122 |
|
---|
123 | #if USE(QUICK_LOOK)
|
---|
124 | bool isQuickLookResource() const;
|
---|
125 | virtual void didReceivePreviewResponse(const ResourceResponse&) { };
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | const URL& url() const { return m_request.url(); }
|
---|
129 | ResourceHandle* handle() const { return m_handle.get(); }
|
---|
130 | bool shouldSendResourceLoadCallbacks() const { return m_options.sendLoadCallbacks == SendCallbackPolicy::SendCallbacks; }
|
---|
131 | void setSendCallbackPolicy(SendCallbackPolicy sendLoadCallbacks) { m_options.sendLoadCallbacks = sendLoadCallbacks; }
|
---|
132 | bool shouldSniffContent() const { return m_options.sniffContent == ContentSniffingPolicy::SniffContent; }
|
---|
133 | bool shouldSniffContentEncoding() const { return m_options.sniffContentEncoding == ContentEncodingSniffingPolicy::Sniff; }
|
---|
134 | WEBCORE_EXPORT bool isAllowedToAskUserForCredentials() const;
|
---|
135 | WEBCORE_EXPORT bool shouldIncludeCertificateInfo() const;
|
---|
136 |
|
---|
137 | virtual CachedResource* cachedResource() const { return nullptr; }
|
---|
138 |
|
---|
139 | bool reachedTerminalState() const { return m_reachedTerminalState; }
|
---|
140 |
|
---|
141 | const ResourceRequest& request() const { return m_request; }
|
---|
142 | void setRequest(ResourceRequest&& request) { m_request = WTFMove(request); }
|
---|
143 |
|
---|
144 | void setDataBufferingPolicy(DataBufferingPolicy);
|
---|
145 |
|
---|
146 | void willSwitchToSubstituteResource();
|
---|
147 |
|
---|
148 | const ResourceLoadTiming& loadTiming() { return m_loadTiming; }
|
---|
149 |
|
---|
150 | #if PLATFORM(COCOA)
|
---|
151 | void schedule(WTF::SchedulePair&);
|
---|
152 | void unschedule(WTF::SchedulePair&);
|
---|
153 | #endif
|
---|
154 |
|
---|
155 | const Frame* frame() const { return m_frame.get(); }
|
---|
156 |
|
---|
157 | const ResourceLoaderOptions& options() const { return m_options; }
|
---|
158 |
|
---|
159 | const ResourceRequest& deferredRequest() const { return m_deferredRequest; }
|
---|
160 | ResourceRequest takeDeferredRequest() { return std::exchange(m_deferredRequest, { }); }
|
---|
161 |
|
---|
162 | bool isPDFJSResourceLoad() const;
|
---|
163 |
|
---|
164 | protected:
|
---|
165 | ResourceLoader(Frame&, ResourceLoaderOptions);
|
---|
166 |
|
---|
167 | void didFinishLoadingOnePart(const NetworkLoadMetrics&);
|
---|
168 | void cleanupForError(const ResourceError&);
|
---|
169 |
|
---|
170 | bool wasCancelled() const { return m_cancellationStatus >= Cancelled; }
|
---|
171 |
|
---|
172 | void setReferrerPolicy(ReferrerPolicy referrerPolicy) { m_options.referrerPolicy = referrerPolicy; }
|
---|
173 | ReferrerPolicy referrerPolicy() const { return m_options.referrerPolicy; }
|
---|
174 |
|
---|
175 | #if PLATFORM(COCOA)
|
---|
176 | void willCacheResponseAsync(ResourceHandle*, NSCachedURLResponse*, CompletionHandler<void(NSCachedURLResponse *)>&&) override;
|
---|
177 | #endif
|
---|
178 |
|
---|
179 | virtual void willSendRequestInternal(ResourceRequest&&, const ResourceResponse& redirectResponse, CompletionHandler<void(ResourceRequest&&)>&&);
|
---|
180 |
|
---|
181 | RefPtr<ResourceHandle> m_handle;
|
---|
182 | RefPtr<Frame> m_frame;
|
---|
183 | RefPtr<DocumentLoader> m_documentLoader;
|
---|
184 | ResourceResponse m_response;
|
---|
185 | ResourceLoadTiming m_loadTiming;
|
---|
186 | #if USE(QUICK_LOOK)
|
---|
187 | std::unique_ptr<LegacyPreviewLoader> m_previewLoader;
|
---|
188 | #endif
|
---|
189 | bool m_canCrossOriginRequestsAskUserForCredentials { true };
|
---|
190 |
|
---|
191 | private:
|
---|
192 | virtual void willCancel(const ResourceError&) = 0;
|
---|
193 | virtual void didCancel(const ResourceError&) = 0;
|
---|
194 |
|
---|
195 | void addBuffer(const FragmentedSharedBuffer&, DataPayloadType);
|
---|
196 | void loadDataURL();
|
---|
197 | void finishNetworkLoad();
|
---|
198 |
|
---|
199 | bool shouldAllowResourceToAskForCredentials() const;
|
---|
200 |
|
---|
201 | // ResourceHandleClient
|
---|
202 | void didSendData(ResourceHandle*, unsigned long long bytesSent, unsigned long long totalBytesToBeSent) override;
|
---|
203 | void didReceiveResponseAsync(ResourceHandle*, ResourceResponse&&, CompletionHandler<void()>&&) override;
|
---|
204 | void willSendRequestAsync(ResourceHandle*, ResourceRequest&&, ResourceResponse&&, CompletionHandler<void(ResourceRequest&&)>&&) override;
|
---|
205 | void didReceiveData(ResourceHandle*, const SharedBuffer&, int encodedDataLength) override;
|
---|
206 | void didReceiveBuffer(ResourceHandle*, const FragmentedSharedBuffer&, int encodedDataLength) override;
|
---|
207 | void didFinishLoading(ResourceHandle*, const NetworkLoadMetrics&) override;
|
---|
208 | void didFail(ResourceHandle*, const ResourceError&) override;
|
---|
209 | void wasBlocked(ResourceHandle*) override;
|
---|
210 | void cannotShowURL(ResourceHandle*) override;
|
---|
211 | bool shouldUseCredentialStorage(ResourceHandle*) override { return shouldUseCredentialStorage(); }
|
---|
212 | void didReceiveAuthenticationChallenge(ResourceHandle*, const AuthenticationChallenge&) override;
|
---|
213 | #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
|
---|
214 | void canAuthenticateAgainstProtectionSpaceAsync(ResourceHandle*, const ProtectionSpace&, CompletionHandler<void(bool)>&&) override;
|
---|
215 | #endif
|
---|
216 | void receivedCancellation(ResourceHandle*, const AuthenticationChallenge& challenge) override { receivedCancellation(challenge); }
|
---|
217 | #if PLATFORM(IOS_FAMILY)
|
---|
218 | RetainPtr<CFDictionaryRef> connectionProperties(ResourceHandle*) override;
|
---|
219 | #endif
|
---|
220 | #if USE(CFURLCONNECTION)
|
---|
221 | // FIXME: Windows should use willCacheResponse - <https://bugs.webkit.org/show_bug.cgi?id=57257>.
|
---|
222 | bool shouldCacheResponse(ResourceHandle*, CFCachedURLResponseRef) override;
|
---|
223 | #endif
|
---|
224 |
|
---|
225 | #if USE(SOUP)
|
---|
226 | void loadGResource();
|
---|
227 | #endif
|
---|
228 |
|
---|
229 | ResourceRequest m_request;
|
---|
230 | ResourceRequest m_originalRequest; // Before redirects.
|
---|
231 | SharedBufferBuilder m_resourceData;
|
---|
232 |
|
---|
233 | ResourceLoaderIdentifier m_identifier;
|
---|
234 |
|
---|
235 | bool m_reachedTerminalState { false };
|
---|
236 | bool m_notifiedLoadComplete { false };
|
---|
237 |
|
---|
238 | enum CancellationStatus {
|
---|
239 | NotCancelled,
|
---|
240 | CalledWillCancel,
|
---|
241 | Cancelled,
|
---|
242 | FinishedCancel
|
---|
243 | };
|
---|
244 | CancellationStatus m_cancellationStatus { NotCancelled };
|
---|
245 |
|
---|
246 | bool m_defersLoading;
|
---|
247 | bool m_wasAuthenticationChallengeBlocked { false };
|
---|
248 | ResourceRequest m_deferredRequest;
|
---|
249 | ResourceLoaderOptions m_options;
|
---|
250 |
|
---|
251 | #if ENABLE(CONTENT_EXTENSIONS)
|
---|
252 | protected:
|
---|
253 | OptionSet<ContentExtensions::ResourceType> m_resourceType;
|
---|
254 | #endif
|
---|
255 | };
|
---|
256 |
|
---|
257 | } // namespace WebCore
|
---|