1/*
2 * Copyright (C) 2004, 2006 Apple Computer, 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 "ThreadableLoader.h"
34#include <wtf/OwnPtr.h>
35
36#if USE(SOUP)
37typedef struct _SoupSession SoupSession;
38#endif
39
40#if USE(CF)
41typedef const struct __CFData * CFDataRef;
42#endif
43
44#if USE(WININET)
45typedef unsigned long DWORD;
46typedef unsigned long DWORD_PTR;
47typedef void* LPVOID;
48typedef LPVOID HINTERNET;
49#endif
50
51
52#if PLATFORM(MAC)
53#include <wtf/RetainPtr.h>
54#ifdef __OBJC__
55@class NSData;
56@class NSError;
57@class NSURLConnection;
58@class WebCoreResourceHandleAsDelegate;
59#else
60class NSData;
61class NSError;
62class NSURLConnection;
63class WebCoreResourceHandleAsDelegate;
64typedef struct objc_object *id;
65#endif
66#endif
67
68#if USE(CFNETWORK)
69typedef struct _CFURLConnection* CFURLConnectionRef;
70typedef int CFHTTPCookieStorageAcceptPolicy;
71typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
72#endif
73
74#if USE(CFURLSTORAGESESSIONS)
75typedef const struct __CFURLStorageSession* CFURLStorageSessionRef;
76#endif
77
78namespace WebCore {
79
80class AuthenticationChallenge;
81class Credential;
82class FormData;
83class Frame;
84class KURL;
85class ProtectionSpace;
86class ResourceError;
87class ResourceHandleClient;
88class ResourceHandleInternal;
89class ResourceRequest;
90class ResourceResponse;
91class SchedulePair;
92class SharedBuffer;
93
94template <typename T> class Timer;
95
96class ResourceHandle : public RefCounted<ResourceHandle>
97#if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL)
98    , public AuthenticationClient
99#endif
100    {
101public:
102    static PassRefPtr<ResourceHandle> create(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
103    static void loadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
104
105    static void prepareForURL(const KURL&);
106    static bool willLoadFromCache(ResourceRequest&, Frame*);
107    static void cacheMetadata(const ResourceResponse&, const Vector<char>&);
108#if PLATFORM(MAC)
109    static bool didSendBodyDataDelegateExists();
110#endif
111
112    virtual ~ResourceHandle();
113
114#if PLATFORM(MAC) || USE(CFNETWORK)
115    void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
116    bool shouldUseCredentialStorage();
117#endif
118#if PLATFORM(MAC) || USE(CFNETWORK) || USE(CURL)
119    void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
120    virtual void receivedCredential(const AuthenticationChallenge&, const Credential&);
121    virtual void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&);
122    virtual void receivedCancellation(const AuthenticationChallenge&);
123#endif
124
125#if PLATFORM(MAC)
126    void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
127#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
128    bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
129#endif
130    NSURLConnection *connection() const;
131    WebCoreResourceHandleAsDelegate *delegate();
132    void releaseDelegate();
133    id releaseProxy();
134
135    void schedule(SchedulePair*);
136    void unschedule(SchedulePair*);
137#elif USE(CFNETWORK)
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    PassRefPtr<SharedBuffer> bufferedData();
152    static bool supportsBufferedData();
153
154    bool shouldContentSniff() const;
155    static bool shouldContentSniffURL(const KURL&);
156
157    static void forceContentSniffing();
158
159#if USE(WININET)
160    void setSynchronousInternetHandle(HINTERNET);
161    void fileLoadTimer(Timer<ResourceHandle>*);
162    void onRedirect();
163    bool onRequestComplete();
164    static void CALLBACK internetStatusCallback(HINTERNET, DWORD_PTR, DWORD, LPVOID, DWORD);
165#endif
166
167#if PLATFORM(QT) || USE(CURL) || USE(SOUP) || PLATFORM(ANDROID)
168    ResourceHandleInternal* getInternal() { return d.get(); }
169#endif
170
171#if USE(SOUP)
172    static SoupSession* defaultSession();
173#endif
174
175    // Used to work around the fact that you don't get any more NSURLConnection callbacks until you return from the one you're in.
176    static bool loadsBlocked();
177
178    bool hasAuthenticationChallenge() const;
179    void clearAuthentication();
180    virtual void cancel();
181
182    // The client may be 0, in which case no callbacks will be made.
183    ResourceHandleClient* client() const;
184    void setClient(ResourceHandleClient*);
185
186    void setDefersLoading(bool);
187#if PLATFORM(ANDROID)
188// TODO: this needs upstreaming.
189    void pauseLoad(bool);
190#endif
191
192    ResourceRequest& firstRequest();
193    const String& lastHTTPMethod() const;
194
195    void fireFailure(Timer<ResourceHandle>*);
196
197#if USE(CFURLSTORAGESESSIONS)
198    static void setPrivateBrowsingEnabled(bool);
199    static CFURLStorageSessionRef privateBrowsingStorageSession();
200    static void setPrivateBrowsingStorageSessionIdentifierBase(const String&);
201#endif
202
203    using RefCounted<ResourceHandle>::ref;
204    using RefCounted<ResourceHandle>::deref;
205
206protected:
207    ResourceHandle(const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
208
209private:
210    enum FailureType {
211        NoFailure,
212        BlockedFailure,
213        InvalidURLFailure
214    };
215
216    void platformSetDefersLoading(bool);
217
218    void scheduleFailure(FailureType);
219
220    bool start(NetworkingContext*);
221
222    virtual void refAuthenticationClient() { ref(); }
223    virtual void derefAuthenticationClient() { deref(); }
224
225#if PLATFORM(MAC)
226    void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff);
227#elif USE(CF)
228    void createCFURLConnection(bool shouldUseCredentialStorage, bool shouldContentSniff);
229#endif
230
231#if USE(CFURLSTORAGESESSIONS)
232    static RetainPtr<CFURLStorageSessionRef> createPrivateBrowsingStorageSession(CFStringRef identifier);
233    static String privateBrowsingStorageSessionIdentifierDefaultBase();
234#endif
235
236    friend class ResourceHandleInternal;
237    OwnPtr<ResourceHandleInternal> d;
238};
239
240}
241
242#endif // ResourceHandle_h
243