Clean up usesAsyncCallbacks handling in ResourceHandle
[WebKit-https.git] / Source / WebCore / platform / network / ResourceHandle.h
1 /*
2  * Copyright (C) 2004, 2006, 2011, 2013 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. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #ifndef ResourceHandle_h
27 #define ResourceHandle_h
28
29 #include "AuthenticationClient.h"
30 #include "HTTPHeaderMap.h"
31 #include "ResourceHandleTypes.h"
32 #include "ResourceLoadPriority.h"
33 #include <wtf/RefCounted.h>
34
35 #if PLATFORM(COCOA) || USE(CFNETWORK)
36 #include <wtf/RetainPtr.h>
37 #endif
38
39 #if USE(QUICK_LOOK)
40 #include "QuickLook.h"
41 #endif
42
43 #if USE(SOUP)
44 typedef struct _GTlsCertificate GTlsCertificate;
45 typedef struct _SoupSession SoupSession;
46 typedef struct _SoupRequest SoupRequest;
47 #endif
48
49 #if USE(CF)
50 typedef const struct __CFData * CFDataRef;
51 #endif
52
53 #if PLATFORM(COCOA)
54 OBJC_CLASS NSCachedURLResponse;
55 OBJC_CLASS NSData;
56 OBJC_CLASS NSDictionary;
57 OBJC_CLASS NSError;
58 OBJC_CLASS NSURLConnection;
59 #ifndef __OBJC__
60 typedef struct objc_object *id;
61 #endif
62 #endif
63
64 #if USE(CFNETWORK)
65 typedef const struct _CFCachedURLResponse* CFCachedURLResponseRef;
66 typedef struct _CFURLConnection* CFURLConnectionRef;
67 typedef int CFHTTPCookieStorageAcceptPolicy;
68 typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
69 #endif
70
71 #if PLATFORM(COCOA) || USE(CFNETWORK)
72 typedef const struct __CFURLStorageSession* CFURLStorageSessionRef;
73 #endif
74
75 namespace WTF {
76 class SchedulePair;
77 }
78
79 namespace WebCore {
80
81 class AuthenticationChallenge;
82 class Credential;
83 class Frame;
84 class URL;
85 class NetworkingContext;
86 class ProtectionSpace;
87 class ResourceError;
88 class ResourceHandleClient;
89 class ResourceHandleInternal;
90 class ResourceLoadTiming;
91 class ResourceRequest;
92 class ResourceResponse;
93 class SharedBuffer;
94 class Timer;
95
96 class ResourceHandle : public RefCounted<ResourceHandle>
97 #if PLATFORM(COCOA) || USE(CFNETWORK) || USE(CURL) || USE(SOUP)
98     , public AuthenticationClient
99 #endif
100     {
101 public:
102     WEBCORE_EXPORT static PassRefPtr<ResourceHandle> create(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
103     WEBCORE_EXPORT static void loadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
104
105     WEBCORE_EXPORT virtual ~ResourceHandle();
106
107 #if PLATFORM(COCOA) || USE(CFNETWORK)
108     void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
109 #endif
110
111 #if PLATFORM(COCOA) || USE(CFNETWORK) || USE(CURL) || USE(SOUP)
112     bool shouldUseCredentialStorage();
113     void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
114     virtual void receivedCredential(const AuthenticationChallenge&, const Credential&) override;
115     virtual void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&) override;
116     virtual void receivedCancellation(const AuthenticationChallenge&) override;
117     virtual void receivedRequestToPerformDefaultHandling(const AuthenticationChallenge&) override;
118     virtual void receivedChallengeRejection(const AuthenticationChallenge&) override;
119 #endif
120
121 #if PLATFORM(COCOA) || USE(CFNETWORK)
122     bool tryHandlePasswordBasedAuthentication(const AuthenticationChallenge&);
123 #endif
124
125 #if PLATFORM(COCOA) && USE(PROTECTION_SPACE_AUTH_CALLBACK)
126     bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
127 #endif
128
129 #if PLATFORM(COCOA) && !USE(CFNETWORK)
130     void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
131     WEBCORE_EXPORT NSURLConnection *connection() const;
132     id makeDelegate(bool);
133     id delegate();
134     void releaseDelegate();
135 #endif
136         
137 #if PLATFORM(COCOA) && ENABLE(WEB_TIMING)
138     void setCollectsTimingData();
139 #if USE(CFNETWORK)
140     static void getConnectionTimingData(CFURLConnectionRef, ResourceLoadTiming&);
141 #else
142     static void getConnectionTimingData(NSURLConnection *, ResourceLoadTiming&);
143 #endif
144 #endif
145         
146 #if PLATFORM(COCOA)
147     void schedule(WTF::SchedulePair&);
148     void unschedule(WTF::SchedulePair&);
149 #endif
150
151 #if USE(CFNETWORK)
152     CFURLStorageSessionRef storageSession() const;
153     CFURLConnectionRef connection() const;
154     WEBCORE_EXPORT RetainPtr<CFURLConnectionRef> releaseConnectionForDownload();
155     const ResourceRequest& currentRequest() const;
156     static void setHostAllowsAnyHTTPSCertificate(const String&);
157     static void setClientCertificate(const String& host, CFDataRef);
158 #endif
159
160 #if USE(QUICK_LOOK)
161     QuickLookHandle* quickLookHandle() { return m_quickLook.get(); }
162     void setQuickLookHandle(std::unique_ptr<QuickLookHandle> handle) { m_quickLook = WTF::move(handle); }
163 #endif
164
165 #if PLATFORM(WIN) && USE(CURL)
166     static void setHostAllowsAnyHTTPSCertificate(const String&);
167     static void setClientCertificateInfo(const String&, const String&, const String&);
168 #endif
169
170 #if PLATFORM(WIN) && USE(CURL) && USE(CF)
171     static void setClientCertificate(const String& host, CFDataRef);
172 #endif
173
174     bool shouldContentSniff() const;
175     static bool shouldContentSniffURL(const URL&);
176
177     WEBCORE_EXPORT static void forceContentSniffing();
178
179 #if USE(CURL) || USE(SOUP)
180     ResourceHandleInternal* getInternal() { return d.get(); }
181 #endif
182
183 #if USE(SOUP)
184     RefPtr<ResourceHandle> releaseForDownload(ResourceHandleClient*);
185     void continueDidReceiveAuthenticationChallenge(const Credential& credentialFromPersistentStorage);
186     void sendPendingRequest();
187     bool cancelledOrClientless();
188     void ensureReadBuffer();
189     size_t currentStreamPosition() const;
190     void didStartRequest();
191     static void setHostAllowsAnyHTTPSCertificate(const String&);
192     static void setClientCertificate(const String& host, GTlsCertificate*);
193     static void setIgnoreSSLErrors(bool);
194     double m_requestTime;
195 #endif
196
197     bool hasAuthenticationChallenge() const;
198     void clearAuthentication();
199     WEBCORE_EXPORT virtual void cancel();
200
201     // The client may be 0, in which case no callbacks will be made.
202     ResourceHandleClient* client() const;
203     WEBCORE_EXPORT void clearClient();
204
205     // Called in response to ResourceHandleClient::willSendRequestAsync().
206     WEBCORE_EXPORT void continueWillSendRequest(const ResourceRequest&);
207
208     // Called in response to ResourceHandleClient::didReceiveResponseAsync().
209     WEBCORE_EXPORT virtual void continueDidReceiveResponse();
210
211 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
212     // Called in response to ResourceHandleClient::canAuthenticateAgainstProtectionSpaceAsync().
213     WEBCORE_EXPORT void continueCanAuthenticateAgainstProtectionSpace(bool);
214 #endif
215
216     // Called in response to ResourceHandleClient::willCacheResponseAsync().
217 #if USE(CFNETWORK)
218     WEBCORE_EXPORT void continueWillCacheResponse(CFCachedURLResponseRef);
219 #endif
220 #if PLATFORM(COCOA) && !USE(CFNETWORK)
221     WEBCORE_EXPORT void continueWillCacheResponse(NSCachedURLResponse *);
222 #endif
223
224     WEBCORE_EXPORT void setDefersLoading(bool);
225
226     WEBCORE_EXPORT ResourceRequest& firstRequest();
227     const String& lastHTTPMethod() const;
228
229     void failureTimerFired();
230
231     NetworkingContext* context() const;
232
233     using RefCounted<ResourceHandle>::ref;
234     using RefCounted<ResourceHandle>::deref;
235
236 #if PLATFORM(COCOA) || USE(CFNETWORK)
237     WEBCORE_EXPORT static CFStringRef synchronousLoadRunLoopMode();
238 #endif
239
240 #if PLATFORM(IOS) && USE(CFNETWORK)
241     static CFMutableDictionaryRef createSSLPropertiesFromNSURLRequest(const ResourceRequest&);
242 #endif
243
244     typedef PassRefPtr<ResourceHandle> (*BuiltinConstructor)(const ResourceRequest& request, ResourceHandleClient* client);
245     static void registerBuiltinConstructor(const AtomicString& protocol, BuiltinConstructor);
246
247     typedef void (*BuiltinSynchronousLoader)(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
248     static void registerBuiltinSynchronousLoader(const AtomicString& protocol, BuiltinSynchronousLoader);
249
250 protected:
251     ResourceHandle(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
252
253     bool usesAsyncCallbacks() const;
254
255 private:
256     enum FailureType {
257         NoFailure,
258         BlockedFailure,
259         InvalidURLFailure
260     };
261
262     void platformSetDefersLoading(bool);
263
264     void scheduleFailure(FailureType);
265
266     bool start();
267     static void platformLoadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
268
269     virtual void refAuthenticationClient() override { ref(); }
270     virtual void derefAuthenticationClient() override { deref(); }
271
272 #if PLATFORM(COCOA) || USE(CFNETWORK)
273     enum class SchedulingBehavior { Asynchronous, Synchronous };
274 #endif
275
276 #if USE(CFNETWORK)
277     void createCFURLConnection(bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior, CFDictionaryRef clientProperties);
278 #endif
279
280 #if PLATFORM(MAC) && !USE(CFNETWORK)
281     void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior);
282 #endif
283
284 #if PLATFORM(IOS) && !USE(CFNETWORK)
285     void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior, NSDictionary *connectionProperties);
286 #endif
287
288 #if PLATFORM(COCOA) && ENABLE(WEB_TIMING)
289 static void getConnectionTimingData(NSDictionary *timingData, ResourceLoadTiming&);
290 #endif
291
292     friend class ResourceHandleInternal;
293     std::unique_ptr<ResourceHandleInternal> d;
294
295 #if USE(QUICK_LOOK)
296     std::unique_ptr<QuickLookHandle> m_quickLook;
297 #endif
298 };
299
300 }
301
302 #endif // ResourceHandle_h