5b61cadba0d493ba5177d86d612611973e94c67c
[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 COMPUTER, 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 COMPUTER, 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/OwnPtr.h>
34 #include <wtf/RefCounted.h>
35
36 #if USE(QUICK_LOOK)
37 #include "QuickLook.h"
38 #endif // USE(QUICK_LOOK)
39
40 #if USE(SOUP)
41 typedef struct _GTlsCertificate GTlsCertificate;
42 typedef struct _SoupSession SoupSession;
43 typedef struct _SoupRequest SoupRequest;
44 #endif
45
46 #if USE(CF)
47 typedef const struct __CFData * CFDataRef;
48 #endif
49
50 #if USE(WININET)
51 typedef unsigned long DWORD;
52 typedef unsigned long DWORD_PTR;
53 typedef void* LPVOID;
54 typedef LPVOID HINTERNET;
55 #endif
56
57 #if PLATFORM(MAC) || USE(CFNETWORK)
58 #include <wtf/RetainPtr.h>
59 #endif
60
61 #if PLATFORM(MAC)
62 OBJC_CLASS NSCachedURLResponse;
63 OBJC_CLASS NSData;
64 OBJC_CLASS NSError;
65 OBJC_CLASS NSURLConnection;
66 #ifndef __OBJC__
67 typedef struct objc_object *id;
68 #endif
69 #endif
70
71 #if USE(CFNETWORK)
72 typedef const struct _CFCachedURLResponse* CFCachedURLResponseRef;
73 typedef struct _CFURLConnection* CFURLConnectionRef;
74 typedef int CFHTTPCookieStorageAcceptPolicy;
75 typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
76 #endif
77
78 #if PLATFORM(MAC) || USE(CFNETWORK)
79 typedef const struct __CFURLStorageSession* CFURLStorageSessionRef;
80 #endif
81
82 namespace WTF {
83 class SchedulePair;
84 }
85
86 namespace WebCore {
87 class AuthenticationChallenge;
88 class Credential;
89 class Frame;
90 class URL;
91 class NetworkingContext;
92 class ProtectionSpace;
93 class ResourceError;
94 class ResourceHandleClient;
95 class ResourceHandleInternal;
96 class ResourceRequest;
97 class ResourceResponse;
98 class SharedBuffer;
99
100 template <typename T> class Timer;
101
102 class ResourceHandle : public RefCounted<ResourceHandle>
103 #if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL) || USE(SOUP)
104     , public AuthenticationClient
105 #endif
106     {
107 public:
108     static PassRefPtr<ResourceHandle> create(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
109     static void loadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
110
111     virtual ~ResourceHandle();
112
113 #if PLATFORM(MAC) || USE(CFNETWORK)
114     void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
115 #endif
116 #if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL) || USE(SOUP)
117     bool shouldUseCredentialStorage();
118     void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
119     virtual void receivedCredential(const AuthenticationChallenge&, const Credential&) OVERRIDE;
120     virtual void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&) OVERRIDE;
121     virtual void receivedCancellation(const AuthenticationChallenge&) OVERRIDE;
122 #endif
123
124 #if PLATFORM(MAC)
125 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
126     bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
127 #endif
128 #if !USE(CFNETWORK)
129     void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
130     NSURLConnection *connection() const;
131     id delegate();
132     void releaseDelegate();
133 #endif
134
135     void schedule(WTF::SchedulePair*);
136     void unschedule(WTF::SchedulePair*);
137 #endif
138 #if USE(CFNETWORK)
139     CFURLStorageSessionRef storageSession() const;
140     CFURLConnectionRef connection() const;
141     CFURLConnectionRef releaseConnectionForDownload();
142     const ResourceRequest& currentRequest() const;
143     static void setHostAllowsAnyHTTPSCertificate(const String&);
144     static void setClientCertificate(const String& host, CFDataRef);
145
146 #if USE(QUICK_LOOK)
147     QuickLookHandle* quickLookHandle() { return m_quickLook.get(); }
148     void setQuickLookHandle(PassOwnPtr<QuickLookHandle> handle) { m_quickLook = handle; }
149 #endif // USE(QUICK_LOOK)
150
151 #endif // USE(CFNETWORK)
152
153 #if (PLATFORM(WIN) || PLATFORM(NIX)) && USE(CURL)
154     static void setHostAllowsAnyHTTPSCertificate(const String&);
155 #endif
156 #if PLATFORM(WIN) && USE(CURL) && USE(CF)
157     static void setClientCertificate(const String& host, CFDataRef);
158 #endif
159
160     bool shouldContentSniff() const;
161     static bool shouldContentSniffURL(const URL&);
162
163     static void forceContentSniffing();
164
165 #if USE(WININET)
166     void setSynchronousInternetHandle(HINTERNET);
167     void fileLoadTimer(Timer<ResourceHandle>*);
168     void onRedirect();
169     bool onRequestComplete();
170     static void CALLBACK internetStatusCallback(HINTERNET, DWORD_PTR, DWORD, LPVOID, DWORD);
171 #endif
172
173 #if USE(CURL) || USE(SOUP)
174     ResourceHandleInternal* getInternal() { return d.get(); }
175 #endif
176
177 #if USE(SOUP)
178     void continueDidReceiveAuthenticationChallenge(const Credential& credentialFromPersistentStorage);
179     void sendPendingRequest();
180     bool cancelledOrClientless();
181     void ensureReadBuffer();
182     size_t currentStreamPosition() const;
183     static SoupSession* defaultSession();
184     static SoupSession* createTestingSession();
185     static SoupSession* createPrivateBrowsingSession();
186     static uint64_t getSoupRequestInitiatingPageID(SoupRequest*);
187     static void setHostAllowsAnyHTTPSCertificate(const String&);
188     static void setClientCertificate(const String& host, GTlsCertificate*);
189     static void setIgnoreSSLErrors(bool);
190 #endif
191
192     // Used to work around the fact that you don't get any more NSURLConnection callbacks until you return from the one you're in.
193     static bool loadsBlocked();    
194
195     bool hasAuthenticationChallenge() const;
196     void clearAuthentication();
197     virtual void cancel();
198
199     // The client may be 0, in which case no callbacks will be made.
200     ResourceHandleClient* client() const;
201     void setClient(ResourceHandleClient*);
202
203     // Called in response to ResourceHandleClient::willSendRequestAsync().
204     void continueWillSendRequest(const ResourceRequest&);
205
206     // Called in response to ResourceHandleClient::didReceiveResponseAsync().
207     void continueDidReceiveResponse();
208
209     // Called in response to ResourceHandleClient::shouldUseCredentialStorageAsync().
210     void continueShouldUseCredentialStorage(bool);
211
212 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
213     // Called in response to ResourceHandleClient::canAuthenticateAgainstProtectionSpaceAsync().
214     void continueCanAuthenticateAgainstProtectionSpace(bool);
215 #endif
216
217     // Called in response to ResourceHandleClient::willCacheResponseAsync().
218 #if USE(CFNETWORK)
219     void continueWillCacheResponse(CFCachedURLResponseRef);
220 #elif PLATFORM(MAC)
221     void continueWillCacheResponse(NSCachedURLResponse *);
222 #endif
223
224     void setDefersLoading(bool);
225
226     void didChangePriority(ResourceLoadPriority);
227
228     ResourceRequest& firstRequest();
229     const String& lastHTTPMethod() const;
230
231     void fireFailure(Timer<ResourceHandle>*);
232
233     NetworkingContext* context() const;
234
235     using RefCounted<ResourceHandle>::ref;
236     using RefCounted<ResourceHandle>::deref;
237
238 #if PLATFORM(MAC) || USE(CFNETWORK)
239     static CFStringRef synchronousLoadRunLoopMode();
240 #endif
241 #if PLATFORM(IOS) && USE(CFNETWORK)
242     static CFMutableDictionaryRef createSSLPropertiesFromNSURLRequest(const ResourceRequest&);
243 #endif
244
245 #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
246     void handleDataArray(CFArrayRef dataArray);
247 #endif
248
249     typedef PassRefPtr<ResourceHandle> (*BuiltinConstructor)(const ResourceRequest& request, ResourceHandleClient* client);
250     static void registerBuiltinConstructor(const AtomicString& protocol, BuiltinConstructor);
251
252     typedef void (*BuiltinSynchronousLoader)(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
253     static void registerBuiltinSynchronousLoader(const AtomicString& protocol, BuiltinSynchronousLoader);
254
255 protected:
256     ResourceHandle(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
257
258 private:
259     enum FailureType {
260         NoFailure,
261         BlockedFailure,
262         InvalidURLFailure
263     };
264
265     void platformSetDefersLoading(bool);
266
267     void scheduleFailure(FailureType);
268
269     bool start();
270     static void platformLoadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
271
272     virtual void refAuthenticationClient() OVERRIDE { ref(); }
273     virtual void derefAuthenticationClient() OVERRIDE { deref(); }
274
275 #if PLATFORM(MAC) && !USE(CFNETWORK)
276     void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff);
277 #elif USE(CFNETWORK)
278     void createCFURLConnection(bool shouldUseCredentialStorage, bool shouldContentSniff, CFDictionaryRef clientProperties);
279 #endif
280
281     friend class ResourceHandleInternal;
282     OwnPtr<ResourceHandleInternal> d;
283
284 #if USE(QUICK_LOOK)
285     OwnPtr<QuickLookHandle> m_quickLook;
286 #endif // USE(QUICK_LOOK)
287 };
288
289 }
290
291 #endif // ResourceHandle_h