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