📄 webdownloadcfnet.cpp
字号:
return S_OK;}HRESULT STDMETHODCALLTYPE WebDownload::setDeletesFileUponFailure( /* [in] */ BOOL deletesFileUponFailure){ if (!m_download) return E_FAIL; CFURLDownloadSetDeletesUponFailure(m_download.get(), !!deletesFileUponFailure); return S_OK;}HRESULT STDMETHODCALLTYPE WebDownload::setDestination( /* [in] */ BSTR path, /* [in] */ BOOL allowOverwrite){ if (!m_download) return E_FAIL; m_destination = String(path, SysStringLen(path)); m_bundlePath = m_destination + bundleExtension(); 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;}// IWebURLAuthenticationChallengeSender -------------------------------------------------------------------HRESULT STDMETHODCALLTYPE WebDownload::cancelAuthenticationChallenge( /* [in] */ IWebURLAuthenticationChallenge*){ if (m_download) { CFURLDownloadCancel(m_download.get()); m_download = 0; } // FIXME: Do we need a URL or description for this error code? 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( /* [in] */ 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( /* [in] */ IWebURLCredential* credential, /* [in] */ 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;}// CFURLDownload Callbacks -------------------------------------------------------------------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(); CFRetain(result); return result;}void WebDownload::didReceiveAuthenticationChallenge(CFURLAuthChallengeRef challenge){ 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){ // The concept of the ".download bundle" is internal to the WebDownload, so therefore // we try to mask the delegate from its existence as much as possible by telling it the final // destination was created, when in reality the bundle was created String createdDestination = MarshallingHelpers::FileCFURLRefToPathString(destination); // At this point in receiving CFURLDownload callbacks, we should definitely have the bundle path stored locally // and it should match with the file that CFURLDownload created ASSERT(createdDestination == m_bundlePath); // And we should also always have the final-destination stored 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()); // We try to rename the bundle to the final file name. If that fails, we give the delegate one more chance to chose // the final file name, then we just leave it 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"); // The call to m_delegate->decideDestinationWithSuggestedFilename() should have changed our destination, so we'll try the move // one last time. if (!m_destination.isEmpty()) if (MoveFileEx(m_bundlePath.charactersWithNullTermination(), m_destination.charactersWithNullTermination(), 0)) reportBundlePathAsFinalPath = false; // We either need to tell the delegate our final filename is the bundle filename, or is the file name they just told us to use if (reportBundlePathAsFinalPath) { BString bundleBSTR(m_bundlePath); m_delegate->didCreateDestination(this, bundleBSTR); } else { BString finalDestinationBSTR = BString(m_destination); m_delegate->didCreateDestination(this, finalDestinationBSTR); } } // It's extremely likely the call to delegate->didFinish() will deref this, so lets not let that cause our destruction just yet 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");}// CFURLDownload Callbacks ----------------------------------------------------------------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); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -