<http://webkit.org/b/91015> Remove BUILDING_ON / TARGETING macros in favor of system...
[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 #endif
38
39 #if USE(CF)
40 typedef const struct __CFData * CFDataRef;
41 #endif
42
43 #if USE(WININET)
44 typedef unsigned long DWORD;
45 typedef unsigned long DWORD_PTR;
46 typedef void* LPVOID;
47 typedef LPVOID HINTERNET;
48 #endif
49
50 #if PLATFORM(MAC) || USE(CFURLSTORAGESESSIONS)
51 #include <wtf/RetainPtr.h>
52 #endif
53
54 #if PLATFORM(MAC)
55 OBJC_CLASS NSData;
56 OBJC_CLASS NSError;
57 OBJC_CLASS NSURLConnection;
58 OBJC_CLASS WebCoreResourceHandleAsDelegate;
59 #ifndef __OBJC__
60 typedef struct objc_object *id;
61 #endif
62 #endif
63
64 #if USE(CFNETWORK)
65 typedef struct _CFURLConnection* CFURLConnectionRef;
66 typedef int CFHTTPCookieStorageAcceptPolicy;
67 typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
68 #endif
69
70 #if USE(CFURLSTORAGESESSIONS) && PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED <= 1060
71 typedef struct __CFURLStorageSession* CFURLStorageSessionRef;
72 #elif USE(CFURLSTORAGESESSIONS)
73 typedef const struct __CFURLStorageSession* CFURLStorageSessionRef;
74 #endif
75
76 namespace WebCore {
77
78 class AuthenticationChallenge;
79 class Credential;
80 class Frame;
81 class KURL;
82 class ProtectionSpace;
83 class ResourceError;
84 class ResourceHandleClient;
85 class ResourceHandleInternal;
86 class ResourceRequest;
87 class ResourceResponse;
88 class SchedulePair;
89 class SharedBuffer;
90
91 enum StoredCredentials {
92     AllowStoredCredentials,
93     DoNotAllowStoredCredentials
94 };
95
96 template <typename T> class Timer;
97
98 class ResourceHandle : public RefCounted<ResourceHandle>
99 #if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL)
100     , public AuthenticationClient
101 #endif
102     {
103 public:
104     static PassRefPtr<ResourceHandle> create(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
105     static void loadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
106
107     static bool willLoadFromCache(ResourceRequest&, Frame*);
108     static void cacheMetadata(const ResourceResponse&, const Vector<char>&);
109
110     virtual ~ResourceHandle();
111
112 #if PLATFORM(MAC) || USE(CFNETWORK)
113     void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
114     bool shouldUseCredentialStorage();
115 #endif
116 #if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL)
117     void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
118     virtual void receivedCredential(const AuthenticationChallenge&, const Credential&);
119     virtual void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&);
120     virtual void receivedCancellation(const AuthenticationChallenge&);
121 #endif
122
123 #if PLATFORM(MAC)
124 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
125     bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
126 #endif
127 #if !USE(CFNETWORK)
128     void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
129     NSURLConnection *connection() const;
130     WebCoreResourceHandleAsDelegate *delegate();
131     void releaseDelegate();
132     id releaseProxy();
133 #endif
134
135     void schedule(SchedulePair*);
136     void unschedule(SchedulePair*);
137 #endif
138 #if USE(CFNETWORK)
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     static SoupSession* defaultSession();
171 #endif
172
173     // Used to work around the fact that you don't get any more NSURLConnection callbacks until you return from the one you're in.
174     static bool loadsBlocked();    
175
176     bool hasAuthenticationChallenge() const;
177     void clearAuthentication();
178     virtual void cancel();
179
180     // The client may be 0, in which case no callbacks will be made.
181     ResourceHandleClient* client() const;
182     void setClient(ResourceHandleClient*);
183
184     void setDefersLoading(bool);
185
186 #if PLATFORM(BLACKBERRY)
187     void pauseLoad(bool);
188 #endif
189       
190     ResourceRequest& firstRequest();
191     const String& lastHTTPMethod() const;
192
193     void fireFailure(Timer<ResourceHandle>*);
194
195 #if USE(CFURLSTORAGESESSIONS)
196     static CFURLStorageSessionRef currentStorageSession();
197     static void setDefaultStorageSession(CFURLStorageSessionRef);
198     static CFURLStorageSessionRef defaultStorageSession();
199     static void setPrivateBrowsingEnabled(bool);
200
201     static void setPrivateBrowsingStorageSessionIdentifierBase(const String&);
202     static RetainPtr<CFURLStorageSessionRef> createPrivateBrowsingStorageSession(CFStringRef identifier);
203 #endif // USE(CFURLSTORAGESESSIONS)
204
205     using RefCounted<ResourceHandle>::ref;
206     using RefCounted<ResourceHandle>::deref;
207
208 #if PLATFORM(MAC) || USE(CFNETWORK)
209     static CFStringRef synchronousLoadRunLoopMode();
210 #endif
211
212 #if HAVE(NETWORK_CFDATA_ARRAY_CALLBACK)
213     void handleDataArray(CFArrayRef dataArray);
214 #endif
215
216     typedef PassRefPtr<ResourceHandle> (*BuiltinConstructor)(const ResourceRequest& request, ResourceHandleClient* client);
217     static void registerBuiltinConstructor(const AtomicString& protocol, BuiltinConstructor);
218
219 protected:
220     ResourceHandle(const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
221
222 private:
223     enum FailureType {
224         NoFailure,
225         BlockedFailure,
226         InvalidURLFailure
227     };
228
229     void platformSetDefersLoading(bool);
230
231     void scheduleFailure(FailureType);
232
233     bool start(NetworkingContext*);
234
235     virtual void refAuthenticationClient() { ref(); }
236     virtual void derefAuthenticationClient() { deref(); }
237
238 #if PLATFORM(MAC) && !USE(CFNETWORK)
239     void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff);
240 #elif USE(CF)
241     void createCFURLConnection(bool shouldUseCredentialStorage, bool shouldContentSniff);
242 #endif
243
244 #if USE(CFURLSTORAGESESSIONS)
245     static String privateBrowsingStorageSessionIdentifierDefaultBase();
246     static CFURLStorageSessionRef privateBrowsingStorageSession();
247 #endif
248
249     friend class ResourceHandleInternal;
250     OwnPtr<ResourceHandleInternal> d;
251 };
252
253 }
254
255 #endif // ResourceHandle_h