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