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