⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webdownloadcfnet.cpp

📁 linux下开源浏览器WebKit的源码,市面上的很多商用浏览器都是移植自WebKit
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2007 Apple Inc.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */#include "config.h"#include "WebKitDLL.h"#include "WebDownload.h"#include "CString.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 <io.h>#include <sys/stat.h>#include <sys/types.h>#pragma warning(push, 0)#include <WebCore/AuthenticationCF.h>#include <WebCore/BString.h>#include <WebCore/NotImplemented.h>#include <WebCore/ResourceError.h>#include <WebCore/ResourceHandle.h>#include <WebCore/ResourceRequest.h>#include <WebCore/ResourceResponse.h>#include <wtf/CurrentTime.h>#pragma warning(pop)using namespace WebCore;// CFURLDownload Callbacks ----------------------------------------------------------------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(), response.cfURLResponse(), &client));    // It is possible for CFURLDownloadCreateAndStartWithLoadingConnection() to fail if the passed in CFURLConnection is not in a "downloadable state"    // However, we should never hit that case    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());    // The CFURLDownload either starts successfully and retains the CFURLConnection,     // or it fails to creating and we have a now-useless connection with a dangling ref.     // Either way, we need to release the connection to balance out ref counts    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();    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(), ResourceHandle::loaderRunLoop(), kCFRunLoopDefaultMode);    LOG(Download, "WebDownload - Initialized download of url %s in WebDownload %p", url.string().utf8().data(), this);}// IWebDownload -------------------------------------------------------------------HRESULT STDMETHODCALLTYPE WebDownload::initWithRequest(        /* [in] */ IWebURLRequest* request,         /* [in] */ 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();    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 for some reason the download failed to create,     // we have particular cleanup to do    if (!m_download) {        m_request = 0;            return E_FAIL;    }    CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());    CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), ResourceHandle::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(        /* [in] */ BSTR bundlePath,         /* [in] */ IWebDownloadDelegate* delegate){    LOG(Download, "Attempting resume of download bundle %s", String(bundlePath, SysStringLen(bundlePath)).ascii().data());    RetainPtr<CFDataRef> resumeData(AdoptCF, extractResumeDataFromBundle(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));    // Attempt to remove the ".download" extension from the bundle for the final file destination    // Failing that, we clear m_destination and will ask the delegate later once the download starts    if (m_bundlePath.endsWith(bundleExtension(), false)) {        m_destination = m_bundlePath.copy();        m_destination.truncate(m_destination.length() - bundleExtension().length());    } else        m_destination = String();        CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());    CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), ResourceHandle::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());    // FIXME: 4950477 - CFURLDownload neglects to make the didStart() client call upon starting the download.    // This is a somewhat critical call, so we'll fake it for now!    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 = CFURLDownloadCopyResumeData(m_download.get());    if (!resumeData) {        LOG(Download, "WebDownload - Unable to create resume data for download (%p)", this);        goto exit;    }    appendResumeDataToBundle(resumeData.get(), m_bundlePath);   exit:    m_download = 0;    return hr;}HRESULT STDMETHODCALLTYPE WebDownload::deletesFileUponFailure(        /* [out, retval] */ BOOL* result){    if (!m_download)        return E_FAIL;    *result = CFURLDownloadDeletesUponFailure(m_download.get());

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -