webengine/osswebengine/WebKit/win/DefaultDownloadDelegate.cpp
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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 #include "config.h"
       
    26 #include "WebKitDLL.h"
       
    27 #include "DefaultDownloadDelegate.h"
       
    28 
       
    29 #include "MarshallingHelpers.h"
       
    30 #include "WebKit.h"
       
    31 #include "WebKitLogging.h"
       
    32 #include "WebMutableURLRequest.h"
       
    33 
       
    34 #include <shlobj.h>
       
    35 #include <tchar.h>
       
    36 
       
    37 #pragma warning(push, 0)
       
    38 #include <WebCore/BString.h>
       
    39 #pragma warning(pop)
       
    40 
       
    41 using namespace WebCore;
       
    42 
       
    43 
       
    44 // DefaultDownloadDelegate ----------------------------------------------------------------
       
    45 
       
    46 DefaultDownloadDelegate::DefaultDownloadDelegate()
       
    47     : m_refCount(0)
       
    48 {
       
    49     gClassCount++;
       
    50 }
       
    51 
       
    52 DefaultDownloadDelegate::~DefaultDownloadDelegate()
       
    53 {
       
    54     gClassCount--;
       
    55     HashSet<IWebDownload*>::iterator i = m_downloads.begin();
       
    56     for (;i != m_downloads.end(); ++i)
       
    57         (*i)->Release();
       
    58 }
       
    59 
       
    60 DefaultDownloadDelegate* DefaultDownloadDelegate::sharedInstance()
       
    61 {
       
    62     static COMPtr<DefaultDownloadDelegate> shared;
       
    63     if (!shared)
       
    64         shared.adoptRef(DefaultDownloadDelegate::createInstance());
       
    65     return shared.get();
       
    66 }
       
    67 
       
    68 DefaultDownloadDelegate* DefaultDownloadDelegate::createInstance()
       
    69 {
       
    70     DefaultDownloadDelegate* instance = new DefaultDownloadDelegate();
       
    71     instance->AddRef();
       
    72     return instance;
       
    73 }
       
    74 
       
    75 // IUnknown -------------------------------------------------------------------
       
    76 
       
    77 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::QueryInterface(REFIID riid, void** ppvObject)
       
    78 {
       
    79     *ppvObject = 0;
       
    80     if (IsEqualGUID(riid, IID_IUnknown))
       
    81         *ppvObject = static_cast<IUnknown*>(this);
       
    82     else if (IsEqualGUID(riid, IID_IWebDownloadDelegate))
       
    83         *ppvObject = static_cast<IWebDownloadDelegate*>(this);
       
    84     else
       
    85         return E_NOINTERFACE;
       
    86 
       
    87     AddRef();
       
    88     return S_OK;
       
    89 }
       
    90 
       
    91 ULONG STDMETHODCALLTYPE DefaultDownloadDelegate::AddRef()
       
    92 {
       
    93     return ++m_refCount;
       
    94 }
       
    95 
       
    96 ULONG STDMETHODCALLTYPE DefaultDownloadDelegate::Release()
       
    97 {
       
    98     ULONG newRef = --m_refCount;
       
    99     if (!newRef)
       
   100         delete(this);
       
   101 
       
   102     return newRef;
       
   103 }
       
   104 
       
   105 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::decideDestinationWithSuggestedFilename(IWebDownload *download, BSTR filename)
       
   106 {
       
   107     LOG(Download, "DefaultDownloadDelegate %p - decideDestinationWithSuggestedFilename %s", download, String(filename, SysStringLen(filename)).ascii().data());
       
   108 
       
   109     TCHAR pathChars[MAX_PATH];
       
   110     if (FAILED(SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY  | CSIDL_FLAG_CREATE, 0, 0, pathChars))) {
       
   111         if (FAILED(download->setDestination(filename, true))) {
       
   112             LOG_ERROR("Failed to set destination on file");
       
   113             return E_FAIL;
       
   114         }  
       
   115         return S_OK;
       
   116     }
       
   117 
       
   118     size_t fullLength = _tcslen(pathChars) + SysStringLen(filename) + 2;
       
   119     BSTR full = SysAllocStringLen(0, (UINT)fullLength);
       
   120     if (!full)
       
   121         return E_OUTOFMEMORY;
       
   122 
       
   123     _tcscpy_s(full, fullLength, pathChars);
       
   124     _tcscat_s(full, fullLength, _T("\\"));
       
   125     _tcscat_s(full, fullLength, filename);
       
   126     BString fullPath;
       
   127     fullPath.adoptBSTR(full);
       
   128 
       
   129 #ifndef NDEBUG
       
   130     String debug((BSTR)fullPath, SysStringLen(BSTR(fullPath)));
       
   131     LOG(Download, "Setting path to %s", debug.ascii().data());
       
   132 #endif
       
   133 
       
   134     if (FAILED(download->setDestination(fullPath, true))) {
       
   135         LOG_ERROR("Failed to set destination on file");
       
   136         return E_FAIL;
       
   137     }
       
   138     return S_OK;
       
   139 }
       
   140 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didCancelAuthenticationChallenge(IWebDownload* download, IWebURLAuthenticationChallenge* challenge)
       
   141 {
       
   142     LOG(Download, "DefaultDownloadDelegate %p - didCancelAuthenticationChallenge %p", download, challenge);
       
   143     download = 0;
       
   144     challenge = 0;
       
   145     return S_OK;
       
   146 }
       
   147 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didCreateDestination(IWebDownload* download, BSTR destination)
       
   148 {
       
   149     LOG(Download, "DefaultDownloadDelegate %p - didCreateDestination %s", download, String(destination, SysStringLen(destination)).ascii().data());
       
   150     download = 0;
       
   151     destination = 0;
       
   152     return S_OK;
       
   153 }
       
   154 
       
   155 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didReceiveAuthenticationChallenge(IWebDownload* download, IWebURLAuthenticationChallenge* challenge)
       
   156 {
       
   157     LOG(Download, "DefaultDownloadDelegate %p - didReceiveAuthenticationChallenge %p", download, challenge);
       
   158     download = 0;
       
   159     challenge = 0;
       
   160     return S_OK;
       
   161 }
       
   162 
       
   163 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didReceiveDataOfLength(IWebDownload* download, unsigned length)
       
   164 {
       
   165     LOG(Download, "DefaultDownloadDelegate %p - didReceiveDataOfLength %i", download, length);
       
   166     download = 0;
       
   167     length = 0;
       
   168     return S_OK;
       
   169 }
       
   170 
       
   171 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didReceiveResponse(IWebDownload* download, IWebURLResponse* response)
       
   172 {
       
   173     LOG(Download, "DefaultDownloadDelegate %p - didReceiveResponse %p", download, response);
       
   174     download = 0;
       
   175     response = 0;
       
   176     return S_OK;
       
   177 }
       
   178 
       
   179 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::shouldDecodeSourceDataOfMIMEType(IWebDownload* download, BSTR encodingType, BOOL* shouldDecode)
       
   180 {
       
   181     LOG(Download, "DefaultDownloadDelegate %p - shouldDecodeSourceDataOfMIMEType %s", download, String(encodingType, SysStringLen(encodingType)).ascii().data());
       
   182     download = 0;
       
   183     encodingType = 0;
       
   184     *shouldDecode = false;
       
   185     return S_OK;
       
   186 }
       
   187 
       
   188 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::willResumeWithResponse(IWebDownload* download, IWebURLResponse* response, long long fromByte)
       
   189 {
       
   190     LOG(Download, "DefaultDownloadDelegate %p - willResumeWithResponse %p, %q", download, response, fromByte);
       
   191     download = 0;
       
   192     response = 0;
       
   193     fromByte = 0;
       
   194     return S_OK;
       
   195 }
       
   196 
       
   197 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::willSendRequest(IWebDownload* download, IWebMutableURLRequest* request,  
       
   198                                                                  IWebURLResponse* redirectResponse, IWebMutableURLRequest** finalRequest)
       
   199 {
       
   200     LOG(Download, "DefaultDownloadDelegate %p - willSendRequest %p %p", download, request, redirectResponse);
       
   201     download = 0;
       
   202     redirectResponse = 0;
       
   203     *finalRequest = request;
       
   204     return S_OK;
       
   205 }
       
   206 
       
   207 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didBegin(IWebDownload* download)
       
   208 {
       
   209     LOG(Download, "DefaultDownloadDelegate %p - didBegin", download);
       
   210     registerDownload(download);
       
   211     return S_OK;
       
   212 }
       
   213 
       
   214 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didFinish(IWebDownload* download)
       
   215 {
       
   216     LOG(Download, "DefaultDownloadDelegate %p - didFinish", download);
       
   217     unregisterDownload(download);
       
   218     return S_OK;
       
   219 }
       
   220 
       
   221 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didFailWithError(IWebDownload* download, IWebError* error)
       
   222 {
       
   223     LOG(Download, "DefaultDownloadDelegate %p - didFailWithError %p", download, error);
       
   224     unregisterDownload(download);
       
   225     error = 0;
       
   226     return S_OK;
       
   227 }
       
   228 
       
   229 void DefaultDownloadDelegate::registerDownload(IWebDownload* download)
       
   230 {
       
   231     if (m_downloads.contains(download))
       
   232         return;
       
   233     download->AddRef();
       
   234     m_downloads.add(download);
       
   235 }
       
   236 
       
   237 void DefaultDownloadDelegate::unregisterDownload(IWebDownload* download)
       
   238 {
       
   239     if (m_downloads.contains(download)) {
       
   240         download->Release();
       
   241         m_downloads.remove(download);
       
   242     }
       
   243 }