WebDownloadCFNet.cpp [plain text]
#include "config.h"
#include "WebKitDLL.h"
#include "WebDownload.h"
#include "DefaultDownloadDelegate.h"
#include "MarshallingHelpers.h"
#include "WebError.h"
#include "WebKit.h"
#include "WebKitLogging.h"
#include "WebMutableURLRequest.h"
#include "WebURLAuthenticationChallenge.h"
#include "WebURLCredential.h"
#include "WebURLResponse.h"
#include <wtf/platform.h>
#include <wtf/text/CString.h>
#include <io.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <WebCore/AuthenticationCF.h>
#include <WebCore/BString.h>
#include <WebCore/CredentialStorage.h>
#include <WebCore/DownloadBundle.h>
#include <WebCore/LoaderRunLoopCF.h>
#include <WebCore/ResourceError.h>
#include <WebCore/ResourceHandle.h>
#include <WebCore/ResourceRequest.h>
#include <WebCore/ResourceResponse.h>
#include <wtf/CurrentTime.h>
using namespace WebCore;
static void didStartCallback(CFURLDownloadRef download, const void *clientInfo);
static CFURLRequestRef willSendRequestCallback(CFURLDownloadRef download, CFURLRequestRef request, CFURLResponseRef redirectionResponse, const void *clientInfo);
static void didReceiveAuthenticationChallengeCallback(CFURLDownloadRef download, CFURLAuthChallengeRef challenge, const void *clientInfo);
static void didReceiveResponseCallback(CFURLDownloadRef download, CFURLResponseRef response, const void *clientInfo);
static void willResumeWithResponseCallback(CFURLDownloadRef download, CFURLResponseRef response, UInt64 startingByte, const void *clientInfo);
static void didReceiveDataCallback(CFURLDownloadRef download, CFIndex length, const void *clientInfo);
static Boolean shouldDecodeDataOfMIMETypeCallback(CFURLDownloadRef download, CFStringRef encodingType, const void *clientInfo);
static void decideDestinationWithSuggestedObjectNameCallback(CFURLDownloadRef download, CFStringRef objectName, const void *clientInfo);
static void didCreateDestinationCallback(CFURLDownloadRef download, CFURLRef path, const void *clientInfo);
static void didFinishCallback(CFURLDownloadRef download, const void *clientInfo);
static void didFailCallback(CFURLDownloadRef download, CFErrorRef error, const void *clientInfo);
void WebDownload::init(ResourceHandle* handle, const ResourceRequest& request, const ResourceResponse& response, IWebDownloadDelegate* delegate)
{
m_delegate = delegate ? delegate : DefaultDownloadDelegate::sharedInstance();
CFURLConnectionRef connection = handle->connection();
if (!connection) {
LOG_ERROR("WebDownload::WebDownload(ResourceHandle*,...) called with an inactive ResourceHandle");
return;
}
CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
m_request.adoptRef(WebMutableURLRequest::createInstance(request));
m_download = adoptCF(CFURLDownloadCreateAndStartWithLoadingConnection(0, connection, request.cfURLRequest(UpdateHTTPBody), response.cfURLResponse(), &client));
if (!m_download) {
ASSERT_NOT_REACHED();
LOG_ERROR("WebDownload - Failed to create WebDownload from existing connection (%s)", request.url().string().utf8().data());
} else
LOG(Download, "WebDownload - Created WebDownload %p from existing connection (%s)", this, request.url().string().utf8().data());
handle->releaseConnectionForDownload();
CFRelease(connection);
}
void WebDownload::init(const KURL& url, IWebDownloadDelegate* delegate)
{
m_delegate = delegate ? delegate : DefaultDownloadDelegate::sharedInstance();
LOG_ERROR("Delegate is %p", m_delegate.get());
ResourceRequest request(url);
CFURLRequestRef cfRequest = request.cfURLRequest(UpdateHTTPBody);
CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
m_request.adoptRef(WebMutableURLRequest::createInstance(request));
m_download = adoptCF(CFURLDownloadCreate(0, cfRequest, &client));
CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());
CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), loaderRunLoop(), kCFRunLoopDefaultMode);
LOG(Download, "WebDownload - Initialized download of url %s in WebDownload %p", url.string().utf8().data(), this);
}
HRESULT STDMETHODCALLTYPE WebDownload::initWithRequest(
IWebURLRequest* request,
IWebDownloadDelegate* delegate)
{
COMPtr<WebMutableURLRequest> webRequest;
if (!request || FAILED(request->QueryInterface(&webRequest))) {
LOG(Download, "WebDownload - initWithRequest failed - not a WebMutableURLRequest");
return E_FAIL;
}
if (!delegate)
return E_FAIL;
m_delegate = delegate;
LOG(Download, "Delegate is %p", m_delegate.get());
RetainPtr<CFURLRequestRef> cfRequest = webRequest->resourceRequest().cfURLRequest(UpdateHTTPBody);
CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
m_request.adoptRef(WebMutableURLRequest::createInstance(webRequest.get()));
m_download = adoptCF(CFURLDownloadCreate(0, cfRequest.get(), &client));
if (!m_download) {
m_request = 0;
return E_FAIL;
}
CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());
CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), loaderRunLoop(), kCFRunLoopDefaultMode);
LOG(Download, "WebDownload - initWithRequest complete, started download of url %s", webRequest->resourceRequest().url().string().utf8().data());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::initToResumeWithBundle(
BSTR bundlePath,
IWebDownloadDelegate* delegate)
{
LOG(Download, "Attempting resume of download bundle %s", String(bundlePath, SysStringLen(bundlePath)).ascii().data());
RetainPtr<CFDataRef> resumeData = adoptCF(DownloadBundle::extractResumeData(String(bundlePath, SysStringLen(bundlePath))));
if (!resumeData)
return E_FAIL;
if (!delegate)
return E_FAIL;
m_delegate = delegate;
LOG(Download, "Delegate is %p", m_delegate.get());
CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
RetainPtr<CFURLRef> pathURL = adoptCF(MarshallingHelpers::PathStringToFileCFURLRef(String(bundlePath, SysStringLen(bundlePath))));
ASSERT(pathURL);
m_download = adoptCF(CFURLDownloadCreateWithResumeData(0, resumeData.get(), pathURL.get(), &client));
if (!m_download) {
LOG(Download, "Failed to create CFURLDownloadRef for resume");
return E_FAIL;
}
m_bundlePath = String(bundlePath, SysStringLen(bundlePath));
if (m_bundlePath.endsWith(DownloadBundle::fileExtension(), false)) {
m_destination = m_bundlePath.isolatedCopy();
m_destination.truncate(m_destination.length() - DownloadBundle::fileExtension().length());
} else
m_destination = String();
CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());
CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), loaderRunLoop(), kCFRunLoopDefaultMode);
LOG(Download, "WebDownload - initWithRequest complete, resumed download of bundle %s", String(bundlePath, SysStringLen(bundlePath)).ascii().data());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::start()
{
LOG(Download, "WebDownload - Starting download (%p)", this);
if (!m_download)
return E_FAIL;
CFURLDownloadStart(m_download.get());
didStart();
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::cancel()
{
LOG(Download, "WebDownload - Cancelling download (%p)", this);
if (!m_download)
return E_FAIL;
CFURLDownloadCancel(m_download.get());
m_download = 0;
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::cancelForResume()
{
LOG(Download, "WebDownload - Cancelling download (%p), writing resume information to file if possible", this);
ASSERT(m_download);
if (!m_download)
return E_FAIL;
HRESULT hr = S_OK;
RetainPtr<CFDataRef> resumeData;
if (m_destination.isEmpty()) {
CFURLDownloadCancel(m_download.get());
goto exit;
}
CFURLDownloadSetDeletesUponFailure(m_download.get(), false);
CFURLDownloadCancel(m_download.get());
resumeData = adoptCF(CFURLDownloadCopyResumeData(m_download.get()));
if (!resumeData) {
LOG(Download, "WebDownload - Unable to create resume data for download (%p)", this);
goto exit;
}
DownloadBundle::appendResumeData(resumeData.get(), m_bundlePath);
exit:
m_download = 0;
return hr;
}
HRESULT STDMETHODCALLTYPE WebDownload::deletesFileUponFailure(
BOOL* result)
{
if (!m_download)
return E_FAIL;
*result = CFURLDownloadDeletesUponFailure(m_download.get());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::setDeletesFileUponFailure(
BOOL deletesFileUponFailure)
{
if (!m_download)
return E_FAIL;
CFURLDownloadSetDeletesUponFailure(m_download.get(), !!deletesFileUponFailure);
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::setDestination(
BSTR path,
BOOL allowOverwrite)
{
if (!m_download)
return E_FAIL;
m_destination = String(path, SysStringLen(path));
m_bundlePath = m_destination + DownloadBundle::fileExtension();
CFURLRef pathURL = MarshallingHelpers::PathStringToFileCFURLRef(m_bundlePath);
CFURLDownloadSetDestination(m_download.get(), pathURL, !!allowOverwrite);
CFRelease(pathURL);
LOG(Download, "WebDownload - Set destination to %s", m_bundlePath.ascii().data());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::cancelAuthenticationChallenge(
IWebURLAuthenticationChallenge*)
{
if (m_download) {
CFURLDownloadCancel(m_download.get());
m_download = 0;
}
ResourceError error(String(WebURLErrorDomain), WebURLErrorUserCancelledAuthentication, "", "");
COMPtr<WebError> webError(AdoptCOM, WebError::createInstance(error));
m_delegate->didFailWithError(this, webError.get());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::continueWithoutCredentialForAuthenticationChallenge(
IWebURLAuthenticationChallenge* challenge)
{
COMPtr<WebURLAuthenticationChallenge> webChallenge(Query, challenge);
if (!webChallenge)
return E_NOINTERFACE;
if (m_download)
CFURLDownloadUseCredential(m_download.get(), 0, webChallenge->authenticationChallenge().cfURLAuthChallengeRef());
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebDownload::useCredential(
IWebURLCredential* credential,
IWebURLAuthenticationChallenge* challenge)
{
COMPtr<WebURLAuthenticationChallenge> webChallenge(Query, challenge);
if (!webChallenge)
return E_NOINTERFACE;
COMPtr<WebURLCredential> webCredential(Query, credential);
if (!webCredential)
return E_NOINTERFACE;
RetainPtr<CFURLCredentialRef> cfCredential = adoptCF(createCF(webCredential->credential()));
if (m_download)
CFURLDownloadUseCredential(m_download.get(), cfCredential.get(), webChallenge->authenticationChallenge().cfURLAuthChallengeRef());
return S_OK;
}
void WebDownload::didStart()
{
#ifndef NDEBUG
m_startTime = m_dataTime = currentTime();
m_received = 0;
LOG(Download, "DOWNLOAD - Started %p at %.3f seconds", this, m_startTime);
#endif
if (FAILED(m_delegate->didBegin(this)))
LOG_ERROR("DownloadDelegate->didBegin failed");
}
CFURLRequestRef WebDownload::willSendRequest(CFURLRequestRef request, CFURLResponseRef response)
{
COMPtr<WebMutableURLRequest> webRequest(AdoptCOM, WebMutableURLRequest::createInstance(ResourceRequest(request)));
COMPtr<WebURLResponse> webResponse(AdoptCOM, WebURLResponse::createInstance(ResourceResponse(response)));
COMPtr<IWebMutableURLRequest> finalRequest;
if (FAILED(m_delegate->willSendRequest(this, webRequest.get(), webResponse.get(), &finalRequest)))
LOG_ERROR("DownloadDelegate->willSendRequest failed");
if (!finalRequest)
return 0;
COMPtr<WebMutableURLRequest> finalWebRequest(AdoptCOM, WebMutableURLRequest::createInstance(finalRequest.get()));
m_request = finalWebRequest.get();
CFURLRequestRef result = finalWebRequest->resourceRequest().cfURLRequest(UpdateHTTPBody);
CFRetain(result);
return result;
}
void WebDownload::didReceiveAuthenticationChallenge(CFURLAuthChallengeRef challenge)
{
if (!CFURLAuthChallengeGetPreviousFailureCount(challenge)) {
Credential credential = CredentialStorage::get(core(CFURLAuthChallengeGetProtectionSpace(challenge)));
if (!credential.isEmpty()) {
RetainPtr<CFURLCredentialRef> cfCredential = adoptCF(createCF(credential));
CFURLDownloadUseCredential(m_download.get(), cfCredential.get(), challenge);
return;
}
}
COMPtr<IWebURLAuthenticationChallenge> webChallenge(AdoptCOM,
WebURLAuthenticationChallenge::createInstance(AuthenticationChallenge(challenge, 0), this));
if (SUCCEEDED(m_delegate->didReceiveAuthenticationChallenge(this, webChallenge.get())))
return;
cancelAuthenticationChallenge(webChallenge.get());
}
void WebDownload::didReceiveResponse(CFURLResponseRef response)
{
COMPtr<WebURLResponse> webResponse(AdoptCOM, WebURLResponse::createInstance(ResourceResponse(response)));
if (FAILED(m_delegate->didReceiveResponse(this, webResponse.get())))
LOG_ERROR("DownloadDelegate->didReceiveResponse failed");
}
void WebDownload::willResumeWithResponse(CFURLResponseRef response, UInt64 fromByte)
{
COMPtr<WebURLResponse> webResponse(AdoptCOM, WebURLResponse::createInstance(ResourceResponse(response)));
if (FAILED(m_delegate->willResumeWithResponse(this, webResponse.get(), fromByte)))
LOG_ERROR("DownloadDelegate->willResumeWithResponse failed");
}
void WebDownload::didReceiveData(CFIndex length)
{
#ifndef NDEBUG
m_received += length;
double current = currentTime();
if (current - m_dataTime > 2.0)
LOG(Download, "DOWNLOAD - %p hanged for %.3f seconds - Received %i bytes for a total of %i", this, current - m_dataTime, length, m_received);
m_dataTime = current;
#endif
if (FAILED(m_delegate->didReceiveDataOfLength(this, length)))
LOG_ERROR("DownloadDelegate->didReceiveData failed");
}
bool WebDownload::shouldDecodeDataOfMIMEType(CFStringRef mimeType)
{
BOOL result;
if (FAILED(m_delegate->shouldDecodeSourceDataOfMIMEType(this, BString(mimeType), &result))) {
LOG_ERROR("DownloadDelegate->shouldDecodeSourceDataOfMIMEType failed");
return false;
}
return !!result;
}
void WebDownload::decideDestinationWithSuggestedObjectName(CFStringRef name)
{
if (FAILED(m_delegate->decideDestinationWithSuggestedFilename(this, BString(name))))
LOG_ERROR("DownloadDelegate->decideDestinationWithSuggestedObjectName failed");
}
void WebDownload::didCreateDestination(CFURLRef destination)
{
String createdDestination = MarshallingHelpers::FileCFURLRefToPathString(destination);
ASSERT(createdDestination == m_bundlePath);
ASSERT(!m_destination.isEmpty());
BString path(m_destination);
if (FAILED(m_delegate->didCreateDestination(this, path)))
LOG_ERROR("DownloadDelegate->didCreateDestination failed");
}
void WebDownload::didFinish()
{
#ifndef NDEBUG
LOG(Download, "DOWNLOAD - Finished %p after %i bytes and %.3f seconds", this, m_received, currentTime() - m_startTime);
#endif
ASSERT(!m_bundlePath.isEmpty() && !m_destination.isEmpty());
LOG(Download, "WebDownload - Moving file from bundle %s to destination %s", m_bundlePath.ascii().data(), m_destination.ascii().data());
if (!MoveFileEx(m_bundlePath.charactersWithNullTermination(), m_destination.charactersWithNullTermination(), 0)) {
LOG_ERROR("Failed to move bundle %s to %s on completion\nError - %i", m_bundlePath.ascii().data(), m_destination.ascii().data(), GetLastError());
bool reportBundlePathAsFinalPath = true;
BString destinationBSTR(m_destination.characters(), m_destination.length());
if (FAILED(m_delegate->decideDestinationWithSuggestedFilename(this, destinationBSTR)))
LOG_ERROR("delegate->decideDestinationWithSuggestedFilename() failed");
if (!m_destination.isEmpty())
if (MoveFileEx(m_bundlePath.charactersWithNullTermination(), m_destination.charactersWithNullTermination(), 0))
reportBundlePathAsFinalPath = false;
if (reportBundlePathAsFinalPath) {
BString bundleBSTR(m_bundlePath);
m_delegate->didCreateDestination(this, bundleBSTR);
} else {
BString finalDestinationBSTR = BString(m_destination);
m_delegate->didCreateDestination(this, finalDestinationBSTR);
}
}
COMPtr<WebDownload> protect = this;
if (FAILED(m_delegate->didFinish(this)))
LOG_ERROR("DownloadDelegate->didFinish failed");
m_download = 0;
}
void WebDownload::didFail(CFErrorRef error)
{
COMPtr<WebError> webError(AdoptCOM, WebError::createInstance(ResourceError(error)));
if (FAILED(m_delegate->didFailWithError(this, webError.get())))
LOG_ERROR("DownloadDelegate->didFailWithError failed");
}
void didStartCallback(CFURLDownloadRef, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didStart(); }
CFURLRequestRef willSendRequestCallback(CFURLDownloadRef, CFURLRequestRef request, CFURLResponseRef redirectionResponse, const void *clientInfo)
{ return ((WebDownload*)clientInfo)->willSendRequest(request, redirectionResponse); }
void didReceiveAuthenticationChallengeCallback(CFURLDownloadRef, CFURLAuthChallengeRef challenge, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didReceiveAuthenticationChallenge(challenge); }
void didReceiveResponseCallback(CFURLDownloadRef, CFURLResponseRef response, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didReceiveResponse(response); }
void willResumeWithResponseCallback(CFURLDownloadRef, CFURLResponseRef response, UInt64 startingByte, const void *clientInfo)
{ ((WebDownload*)clientInfo)->willResumeWithResponse(response, startingByte); }
void didReceiveDataCallback(CFURLDownloadRef, CFIndex length, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didReceiveData(length); }
Boolean shouldDecodeDataOfMIMETypeCallback(CFURLDownloadRef, CFStringRef encodingType, const void *clientInfo)
{ return ((WebDownload*)clientInfo)->shouldDecodeDataOfMIMEType(encodingType); }
void decideDestinationWithSuggestedObjectNameCallback(CFURLDownloadRef, CFStringRef objectName, const void *clientInfo)
{ ((WebDownload*)clientInfo)->decideDestinationWithSuggestedObjectName(objectName); }
void didCreateDestinationCallback(CFURLDownloadRef, CFURLRef path, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didCreateDestination(path); }
void didFinishCallback(CFURLDownloadRef, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didFinish(); }
void didFailCallback(CFURLDownloadRef, CFErrorRef error, const void *clientInfo)
{ ((WebDownload*)clientInfo)->didFail(error); }