1/*
2 * Copyright (C) 2004-2013 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 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 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 ResourceHandleCFURLConnectionDelegate_h
27#define ResourceHandleCFURLConnectionDelegate_h
28
29#if USE(CFNETWORK)
30
31#include "ResourceRequest.h"
32#include <CFNetwork/CFURLConnectionPriv.h>
33#include <wtf/RetainPtr.h>
34#include <wtf/ThreadSafeRefCounted.h>
35
36namespace WebCore {
37
38class ResourceHandle;
39
40class ResourceHandleCFURLConnectionDelegate : public ThreadSafeRefCounted<ResourceHandleCFURLConnectionDelegate> {
41public:
42    ResourceHandleCFURLConnectionDelegate(ResourceHandle*);
43    virtual ~ResourceHandleCFURLConnectionDelegate();
44
45    CFURLConnectionClient_V6 makeConnectionClient() const;
46    virtual void setupRequest(CFMutableURLRequestRef) = 0;
47    virtual void setupConnectionScheduling(CFURLConnectionRef) = 0;
48    void releaseHandle();
49
50    virtual void continueWillSendRequest(CFURLRequestRef) = 0;
51    virtual void continueDidReceiveResponse() = 0;
52    virtual void continueWillCacheResponse(CFCachedURLResponseRef) = 0;
53#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
54    virtual void continueCanAuthenticateAgainstProtectionSpace(bool) = 0;
55#endif // USE(PROTECTION_SPACE_AUTH_CALLBACK)
56
57protected:
58    RetainPtr<CFURLResponseRef> synthesizeRedirectResponseIfNecessary(CFURLRequestRef, CFURLResponseRef);
59    ResourceRequest createResourceRequest(CFURLRequestRef, CFURLResponseRef);
60
61private:
62    static CFURLRequestRef willSendRequestCallback(CFURLConnectionRef, CFURLRequestRef, CFURLResponseRef, const void* clientInfo);
63    static void didReceiveResponseCallback(CFURLConnectionRef, CFURLResponseRef, const void* clientInfo);
64    static void didReceiveDataCallback(CFURLConnectionRef, CFDataRef, CFIndex originalLength, const void* clientInfo);
65    static void didFinishLoadingCallback(CFURLConnectionRef, const void* clientInfo);
66    static void didFailCallback(CFURLConnectionRef, CFErrorRef, const void* clientInfo);
67    static CFCachedURLResponseRef willCacheResponseCallback(CFURLConnectionRef, CFCachedURLResponseRef, const void* clientInfo);
68    static void didReceiveChallengeCallback(CFURLConnectionRef, CFURLAuthChallengeRef, const void* clientInfo);
69    static void didSendBodyDataCallback(CFURLConnectionRef, CFIndex, CFIndex totalBytesWritten, CFIndex totalBytesExpectedToWrite, const void *clientInfo);
70    static Boolean shouldUseCredentialStorageCallback(CFURLConnectionRef, const void* clientInfo);
71#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
72    static Boolean canRespondToProtectionSpaceCallback(CFURLConnectionRef, CFURLProtectionSpaceRef, const void* clientInfo);
73#endif // USE(PROTECTION_SPACE_AUTH_CALLBACK)
74#if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
75    static void didReceiveDataArrayCallback(CFURLConnectionRef, CFArrayRef, const void* clientInfo);
76#endif // USE(NETWORK_CFDATA_ARRAY_CALLBACK)
77
78    virtual CFURLRequestRef willSendRequest(CFURLRequestRef, CFURLResponseRef) = 0;
79    virtual void didReceiveResponse(CFURLConnectionRef, CFURLResponseRef) = 0;
80    virtual void didReceiveData(CFDataRef, CFIndex originalLength) = 0;
81    virtual void didFinishLoading() = 0;
82    virtual void didFail(CFErrorRef) = 0;
83    virtual CFCachedURLResponseRef willCacheResponse(CFCachedURLResponseRef) = 0;
84    virtual void didReceiveChallenge(CFURLAuthChallengeRef) = 0;
85    virtual void didSendBodyData(CFIndex totalBytesWritten, CFIndex totalBytesExpectedToWrite) = 0;
86    virtual Boolean shouldUseCredentialStorage() = 0;
87#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
88    virtual Boolean canRespondToProtectionSpace(CFURLProtectionSpaceRef) = 0;
89#endif // USE(PROTECTION_SPACE_AUTH_CALLBACK)
90#if USE(NETWORK_CFDATA_ARRAY_CALLBACK)
91    virtual void didReceiveDataArray(CFArrayRef dataArray) = 0;
92#endif // USE(NETWORK_CFDATA_ARRAY_CALLBACK)
93
94protected:
95    ResourceHandle* m_handle;
96    RetainPtr<CFStringRef> m_originalScheme;
97};
98
99} // namespace WebCore.
100
101#endif // USE(CFNETWORK)
102
103#endif // ResourceHandleCFURLConnectionDelegate_h
104