BlobDownloadClient.cpp [plain text]
#include "config.h"
#include "BlobDownloadClient.h"
#if !USE(NETWORK_SESSION)
#include "DataReference.h"
#include "Download.h"
#include "WebErrors.h"
#include <WebCore/MIMETypeRegistry.h>
#include <WebCore/ResourceError.h>
#include <WebCore/ResourceResponse.h>
#include <WebCore/SharedBuffer.h>
#include <wtf/CompletionHandler.h>
namespace WebKit {
using namespace WebCore;
BlobDownloadClient::BlobDownloadClient(Download& download)
: m_download(download)
{
}
void BlobDownloadClient::didReceiveResponseAsync(ResourceHandle*, ResourceResponse&& response)
{
m_download.didReceiveResponse(WTFMove(response));
String suggestedFilename = MIMETypeRegistry::appendFileExtensionIfNecessary(m_download.suggestedName().isEmpty() ? ASCIILiteral("unknown") : m_download.suggestedName(), response.mimeType());
m_download.decideDestinationWithSuggestedFilenameAsync(suggestedFilename);
}
void BlobDownloadClient::didDecideDownloadDestination(const String& destinationPath, bool allowOverwrite)
{
ASSERT(!destinationPath.isEmpty());
if (FileSystem::fileExists(destinationPath)) {
if (!allowOverwrite) {
didFail(nullptr, cancelledError(m_download.request()));
return;
}
FileSystem::deleteFile(destinationPath);
}
m_destinationPath = destinationPath;
m_destinationFile = FileSystem::openFile(m_destinationPath, FileSystem::FileOpenMode::Write);
m_download.didCreateDestination(m_destinationPath);
m_download.continueDidReceiveResponse();
}
void BlobDownloadClient::willSendRequestAsync(ResourceHandle*, ResourceRequest&& request, ResourceResponse&&, CompletionHandler<void(ResourceRequest&&)>&& completionHandler)
{
ASSERT_NOT_REACHED();
completionHandler(WTFMove(request));
}
#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
void BlobDownloadClient::canAuthenticateAgainstProtectionSpaceAsync(ResourceHandle*, const ProtectionSpace&)
{
ASSERT_NOT_REACHED();
}
#endif
void BlobDownloadClient::didReceiveBuffer(ResourceHandle*, Ref<SharedBuffer>&& buffer, int)
{
FileSystem::writeToFile(m_destinationFile, buffer->data(), buffer->size());
m_download.didReceiveData(buffer->size());
}
void BlobDownloadClient::didFinishLoading(ResourceHandle*)
{
FileSystem::closeFile(m_destinationFile);
m_download.didFinish();
}
void BlobDownloadClient::didCancel()
{
FileSystem::closeFile(m_destinationFile);
if (!m_destinationPath.isEmpty())
FileSystem::deleteFile(m_destinationPath);
m_download.didCancel(IPC::DataReference());
}
void BlobDownloadClient::didFail(ResourceHandle*, const ResourceError& error)
{
FileSystem::closeFile(m_destinationFile);
if (!m_destinationPath.isEmpty())
FileSystem::deleteFile(m_destinationPath);
m_download.didFail(error, IPC::DataReference());
}
}
#endif // !USE(NETWORK_SESSION)