2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include "WebKitDLL.h"
27 #include "DefaultDownloadDelegate.h"
29 #include "MarshallingHelpers.h"
31 #include "WebKitLogging.h"
32 #include "WebMutableURLRequest.h"
33 #include <WebCore/COMPtr.h>
34 #include <wtf/text/CString.h>
39 #include <WebCore/BString.h>
41 using namespace WebCore;
44 // DefaultDownloadDelegate ----------------------------------------------------------------
46 DefaultDownloadDelegate::DefaultDownloadDelegate()
50 gClassNameCount.add("DefaultDownloadDelegate");
53 DefaultDownloadDelegate::~DefaultDownloadDelegate()
56 gClassNameCount.remove("DefaultDownloadDelegate");
57 HashSet<IWebDownload*>::iterator i = m_downloads.begin();
58 for (;i != m_downloads.end(); ++i)
62 DefaultDownloadDelegate* DefaultDownloadDelegate::sharedInstance()
64 static COMPtr<DefaultDownloadDelegate> shared;
66 shared.adoptRef(DefaultDownloadDelegate::createInstance());
70 DefaultDownloadDelegate* DefaultDownloadDelegate::createInstance()
72 DefaultDownloadDelegate* instance = new DefaultDownloadDelegate();
77 // IUnknown -------------------------------------------------------------------
79 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::QueryInterface(REFIID riid, void** ppvObject)
82 if (IsEqualGUID(riid, IID_IUnknown))
83 *ppvObject = static_cast<IUnknown*>(this);
84 else if (IsEqualGUID(riid, IID_IWebDownloadDelegate))
85 *ppvObject = static_cast<IWebDownloadDelegate*>(this);
93 ULONG STDMETHODCALLTYPE DefaultDownloadDelegate::AddRef()
98 ULONG STDMETHODCALLTYPE DefaultDownloadDelegate::Release()
100 ULONG newRef = --m_refCount;
107 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::decideDestinationWithSuggestedFilename(IWebDownload *download, BSTR filename)
109 LOG(Download, "DefaultDownloadDelegate %p - decideDestinationWithSuggestedFilename %s", download, String(filename, SysStringLen(filename)).ascii().data());
111 WCHAR pathChars[MAX_PATH];
112 if (FAILED(SHGetFolderPath(0, CSIDL_DESKTOPDIRECTORY | CSIDL_FLAG_CREATE, 0, 0, pathChars))) {
113 if (FAILED(download->setDestination(filename, true))) {
114 LOG_ERROR("Failed to set destination on file");
120 size_t fullLength = wcslen(pathChars) + SysStringLen(filename) + 2;
121 BSTR full = SysAllocStringLen(0, (UINT)fullLength);
123 return E_OUTOFMEMORY;
125 wcscpy_s(full, fullLength, pathChars);
126 wcscat_s(full, fullLength, L"\\");
127 wcscat_s(full, fullLength, filename);
129 fullPath.adoptBSTR(full);
132 String debug((BSTR)fullPath, SysStringLen(BSTR(fullPath)));
133 LOG(Download, "Setting path to %s", debug.ascii().data());
136 if (FAILED(download->setDestination(fullPath, true))) {
137 LOG_ERROR("Failed to set destination on file");
142 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didCancelAuthenticationChallenge(IWebDownload* download, IWebURLAuthenticationChallenge* challenge)
144 LOG(Download, "DefaultDownloadDelegate %p - didCancelAuthenticationChallenge %p", download, challenge);
149 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didCreateDestination(IWebDownload* download, BSTR destination)
151 LOG(Download, "DefaultDownloadDelegate %p - didCreateDestination %s", download, String(destination, SysStringLen(destination)).ascii().data());
157 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didReceiveAuthenticationChallenge(IWebDownload* download, IWebURLAuthenticationChallenge* challenge)
159 LOG(Download, "DefaultDownloadDelegate %p - didReceiveAuthenticationChallenge %p", download, challenge);
165 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didReceiveDataOfLength(IWebDownload* download, unsigned length)
167 LOG(Download, "DefaultDownloadDelegate %p - didReceiveDataOfLength %i", download, length);
173 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didReceiveResponse(IWebDownload* download, IWebURLResponse* response)
175 LOG(Download, "DefaultDownloadDelegate %p - didReceiveResponse %p", download, response);
181 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::shouldDecodeSourceDataOfMIMEType(IWebDownload* download, BSTR encodingType, BOOL* shouldDecode)
183 LOG(Download, "DefaultDownloadDelegate %p - shouldDecodeSourceDataOfMIMEType %s", download, String(encodingType, SysStringLen(encodingType)).ascii().data());
186 *shouldDecode = false;
190 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::willResumeWithResponse(IWebDownload* download, IWebURLResponse* response, long long fromByte)
192 LOG(Download, "DefaultDownloadDelegate %p - willResumeWithResponse %p, %q", download, response, fromByte);
199 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::willSendRequest(IWebDownload* download, IWebMutableURLRequest* request,
200 IWebURLResponse* redirectResponse, IWebMutableURLRequest** finalRequest)
202 LOG(Download, "DefaultDownloadDelegate %p - willSendRequest %p %p", download, request, redirectResponse);
204 redirectResponse = 0;
205 *finalRequest = request;
206 (*finalRequest)->AddRef();
210 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didBegin(IWebDownload* download)
212 LOG(Download, "DefaultDownloadDelegate %p - didBegin", download);
213 registerDownload(download);
217 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didFinish(IWebDownload* download)
219 LOG(Download, "DefaultDownloadDelegate %p - didFinish", download);
220 unregisterDownload(download);
224 HRESULT STDMETHODCALLTYPE DefaultDownloadDelegate::didFailWithError(IWebDownload* download, IWebError* error)
226 LOG(Download, "DefaultDownloadDelegate %p - didFailWithError %p", download, error);
227 unregisterDownload(download);
232 void DefaultDownloadDelegate::registerDownload(IWebDownload* download)
234 if (m_downloads.contains(download))
237 m_downloads.add(download);
240 void DefaultDownloadDelegate::unregisterDownload(IWebDownload* download)
242 if (m_downloads.contains(download)) {
244 m_downloads.remove(download);