1/*
2 * Copyright (C) 2007 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 WebDownload_h
27#define WebDownload_h
28
29#include "WebKit.h"
30#include <WebCore/COMPtr.h>
31#include <wtf/RetainPtr.h>
32#include <wtf/text/WTFString.h>
33
34#if USE(CFNETWORK)
35#include <CFNetwork/CFURLDownloadPriv.h>
36#elif USE(CURL)
37#include <WebCore/CurlDownload.h>
38#endif
39
40namespace WebCore {
41    class KURL;
42    class ResourceHandle;
43    class ResourceRequest;
44    class ResourceResponse;
45}
46
47class WebDownload
48: public IWebDownload
49, public IWebURLAuthenticationChallengeSender
50#if USE(CURL)
51, public CurlDownloadListener
52#endif
53{
54public:
55    static WebDownload* createInstance(const WebCore::KURL&, IWebDownloadDelegate*);
56    static WebDownload* createInstance(WebCore::ResourceHandle*, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, IWebDownloadDelegate*);
57    static WebDownload* createInstance();
58private:
59    WebDownload();
60    void init(WebCore::ResourceHandle*, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, IWebDownloadDelegate*);
61    void init(const WebCore::KURL&, IWebDownloadDelegate*);
62    ~WebDownload();
63public:
64    // IUnknown
65    virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject);
66    virtual ULONG STDMETHODCALLTYPE AddRef(void);
67    virtual ULONG STDMETHODCALLTYPE Release(void);
68
69    // IWebDownload
70    virtual HRESULT STDMETHODCALLTYPE initWithRequest(
71        /* [in] */ IWebURLRequest* request,
72        /* [in] */ IWebDownloadDelegate* delegate);
73
74    virtual HRESULT STDMETHODCALLTYPE initToResumeWithBundle(
75        /* [in] */ BSTR bundlePath,
76        /* [in] */ IWebDownloadDelegate* delegate);
77
78    virtual HRESULT STDMETHODCALLTYPE canResumeDownloadDecodedWithEncodingMIMEType(
79        /* [in] */ BSTR mimeType,
80        /* [out, retval] */ BOOL* result);
81
82    virtual HRESULT STDMETHODCALLTYPE start();
83
84    virtual HRESULT STDMETHODCALLTYPE cancel();
85
86    virtual HRESULT STDMETHODCALLTYPE cancelForResume();
87
88    virtual HRESULT STDMETHODCALLTYPE deletesFileUponFailure(
89        /* [out, retval] */ BOOL* result);
90
91    virtual HRESULT STDMETHODCALLTYPE bundlePathForTargetPath(
92        /* [in] */ BSTR target,
93        /* [out, retval] */ BSTR* bundle);
94
95    virtual HRESULT STDMETHODCALLTYPE request(
96        /* [out, retval] */ IWebURLRequest** request);
97
98    virtual HRESULT STDMETHODCALLTYPE setDeletesFileUponFailure(
99        /* [in] */ BOOL deletesFileUponFailure);
100
101    virtual HRESULT STDMETHODCALLTYPE setDestination(
102        /* [in] */ BSTR path,
103        /* [in] */ BOOL allowOverwrite);
104
105    // IWebURLAuthenticationChallengeSender
106    virtual HRESULT STDMETHODCALLTYPE cancelAuthenticationChallenge(
107        /* [in] */ IWebURLAuthenticationChallenge* challenge);
108
109    virtual HRESULT STDMETHODCALLTYPE continueWithoutCredentialForAuthenticationChallenge(
110        /* [in] */ IWebURLAuthenticationChallenge* challenge);
111
112    virtual HRESULT STDMETHODCALLTYPE useCredential(
113        /* [in] */ IWebURLCredential* credential,
114        /* [in] */ IWebURLAuthenticationChallenge* challenge);
115
116#if USE(CFNETWORK)
117    // CFURLDownload Callbacks
118    void didStart();
119    CFURLRequestRef willSendRequest(CFURLRequestRef, CFURLResponseRef);
120    void didReceiveAuthenticationChallenge(CFURLAuthChallengeRef);
121    void didReceiveResponse(CFURLResponseRef);
122    void willResumeWithResponse(CFURLResponseRef, UInt64);
123    void didReceiveData(CFIndex);
124    bool shouldDecodeDataOfMIMEType(CFStringRef);
125    void decideDestinationWithSuggestedObjectName(CFStringRef);
126    void didCreateDestination(CFURLRef);
127    void didFinish();
128    void didFail(CFErrorRef);
129#elif USE(CURL)
130    virtual void didReceiveResponse();
131    virtual void didReceiveDataOfLength(int size);
132    virtual void didFinish();
133    virtual void didFail();
134#endif
135
136protected:
137    ULONG m_refCount;
138
139    WTF::String m_destination;
140    WTF::String m_bundlePath;
141#if USE(CFNETWORK)
142    RetainPtr<CFURLDownloadRef> m_download;
143#elif USE(CURL)
144    CurlDownload m_download;
145#endif
146    COMPtr<IWebMutableURLRequest> m_request;
147    COMPtr<IWebDownloadDelegate> m_delegate;
148
149#ifndef NDEBUG
150    double m_startTime;
151    double m_dataTime;
152    int m_received;
153#endif
154};
155
156
157#endif
158