Home | History | Annotate | Download | only in win
      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 "COMPtr.h"
     30 #include "WebKit.h"
     31 #include <WebCore/PlatformString.h>
     32 #include <wtf/RetainPtr.h>
     33 
     34 #if USE(CFNETWORK)
     35 #include <CFNetwork/CFURLDownloadPriv.h>
     36 #endif
     37 
     38 namespace WebCore {
     39     class KURL;
     40     class ResourceHandle;
     41     class ResourceRequest;
     42     class ResourceResponse;
     43 }
     44 
     45 class WebDownload : public IWebDownload, public IWebURLAuthenticationChallengeSender
     46 {
     47 public:
     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();
     51 private:
     52     WebDownload();
     53     void init(WebCore::ResourceHandle*, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&, IWebDownloadDelegate*);
     54     void init(const WebCore::KURL&, IWebDownloadDelegate*);
     55     ~WebDownload();
     56 public:
     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 
    124 protected:
    125     static CFDataRef extractResumeDataFromBundle(const WebCore::String&);
    126     static HRESULT appendResumeDataToBundle(CFDataRef, const WebCore::String&);
    127     static const WebCore::String& bundleExtension();
    128     static UInt32 bundleMagicNumber();
    129 
    130     ULONG m_refCount;
    131 
    132     WebCore::String m_destination;
    133     WebCore::String m_bundlePath;
    134 #if USE(CFNETWORK)
    135     RetainPtr<CFURLDownloadRef> m_download;
    136 #endif
    137     COMPtr<IWebMutableURLRequest> m_request;
    138     COMPtr<IWebDownloadDelegate> m_delegate;
    139 
    140 #ifndef NDEBUG
    141     double m_startTime;
    142     double m_dataTime;
    143     int m_received;
    144 #endif
    145 };
    146 
    147 
    148 #endif
    149