<rdar://problem/13194263> Crashes in NetworkProcess due to threading issues
[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 NSCachedURLResponse;
59 OBJC_CLASS NSData;
60 OBJC_CLASS NSError;
61 OBJC_CLASS NSURLConnection;
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     id 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     // Called in response to ResourceHandleClient::willSendRequestAsync().
192     void continueWillSendRequest(const ResourceRequest&);
193     // Called in response to ResourceHandleClient::shouldUseCredentialStorageAsync().
194     void continueShouldUseCredentialStorage(bool);
195 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
196     // Called in response to ResourceHandleClient::canAuthenticateAgainstProtectionSpaceAsync().
197     void continueCanAuthenticateAgainstProtectionSpace(bool);
198 #endif
199 #if PLATFORM(MAC)
200     // Called in response to ResourceHandleClient::willCacheResponseAsync().
201     void continueWillCacheResponse(NSCachedURLResponse *);
202 #endif
203
204     void setDefersLoading(bool);
205
206 #if PLATFORM(BLACKBERRY)
207     void pauseLoad(bool); // FIXME: How is this different from setDefersLoading()?
208 #endif
209
210     void didChangePriority(ResourceLoadPriority);
211
212     ResourceRequest& firstRequest();
213     const String& lastHTTPMethod() const;
214
215     void fireFailure(Timer<ResourceHandle>*);
216
217     NetworkingContext* context() const;
218
219     using RefCounted<ResourceHandle>::ref;
220     using RefCounted<ResourceHandle>::deref;
221
222 #if PLATFORM(MAC) || USE(CFNETWORK)
223     static CFStringRef synchronousLoadRunLoopMode();
224 #endif
225
226 #if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
227     void handleDataArray(CFArrayRef dataArray);
228 #endif
229
230     typedef PassRefPtr<ResourceHandle> (*BuiltinConstructor)(const ResourceRequest& request, ResourceHandleClient* client);
231     static void registerBuiltinConstructor(const AtomicString& protocol, BuiltinConstructor);
232
233     typedef void (*BuiltinSynchronousLoader)(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
234     static void registerBuiltinSynchronousLoader(const AtomicString& protocol, BuiltinSynchronousLoader);
235
236 protected:
237     ResourceHandle(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
238
239 private:
240     enum FailureType {
241         NoFailure,
242         BlockedFailure,
243         InvalidURLFailure
244     };
245
246     void platformSetDefersLoading(bool);
247
248     void scheduleFailure(FailureType);
249
250     bool start();
251     static void platformLoadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
252
253     virtual void refAuthenticationClient() { ref(); }
254     virtual void derefAuthenticationClient() { deref(); }
255
256 #if PLATFORM(MAC) && !USE(CFNETWORK)
257     void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff);
258 #elif USE(CFNETWORK)
259     void createCFURLConnection(bool shouldUseCredentialStorage, bool shouldContentSniff);
260 #endif
261
262     friend class ResourceHandleInternal;
263     OwnPtr<ResourceHandleInternal> d;
264 };
265
266 }
267
268 #endif // ResourceHandle_h