BlobRegistryImpl.cpp [plain text]
#include "config.h"
#include "BlobRegistryImpl.h"
#if ENABLE(BLOB)
#include "BlobResourceHandle.h"
#include "BlobStorageData.h"
#include "ResourceError.h"
#include "ResourceHandle.h"
#include "ResourceRequest.h"
#include "ResourceResponse.h"
#include <wtf/MainThread.h>
#include <wtf/StdLibExtras.h>
#if PLATFORM(IOS)
#include "WebCoreThread.h"
#endif
namespace WebCore {
BlobRegistryImpl::~BlobRegistryImpl()
{
}
static PassRefPtr<ResourceHandle> createResourceHandle(const ResourceRequest& request, ResourceHandleClient* client)
{
return static_cast<BlobRegistryImpl&>(blobRegistry()).createResourceHandle(request, client);
}
static void loadResourceSynchronously(NetworkingContext*, const ResourceRequest& request, StoredCredentials, ResourceError& error, ResourceResponse& response, Vector<char>& data)
{
BlobStorageData* blobData = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(request.url());
BlobResourceHandle::loadResourceSynchronously(blobData, request, error, response, data);
}
static void registerBlobResourceHandleConstructor()
{
static bool didRegister = false;
if (!didRegister) {
ResourceHandle::registerBuiltinConstructor("blob", createResourceHandle);
ResourceHandle::registerBuiltinSynchronousLoader("blob", loadResourceSynchronously);
didRegister = true;
}
}
PassRefPtr<ResourceHandle> BlobRegistryImpl::createResourceHandle(const ResourceRequest& request, ResourceHandleClient* client)
{
RefPtr<BlobResourceHandle> handle = BlobResourceHandle::createAsync(getBlobDataFromURL(request.url()), request, client);
if (!handle)
return 0;
handle->start();
return handle.release();
}
void BlobRegistryImpl::appendStorageItems(BlobStorageData* blobStorageData, const BlobDataItemList& items)
{
for (BlobDataItemList::const_iterator iter = items.begin(); iter != items.end(); ++iter) {
if (iter->type == BlobDataItem::Data)
blobStorageData->m_data.appendData(iter->data, iter->offset, iter->length);
#if ENABLE(FILE_SYSTEM)
else if (iter->type == BlobDataItem::URL)
blobStorageData->m_data.appendURL(iter->url, iter->offset, iter->length, iter->expectedModificationTime);
#endif
else {
ASSERT(iter->type == BlobDataItem::File);
blobStorageData->m_data.appendFile(iter->path, iter->offset, iter->length, iter->expectedModificationTime);
}
}
}
void BlobRegistryImpl::appendStorageItems(BlobStorageData* blobStorageData, const BlobDataItemList& items, long long offset, long long length)
{
ASSERT(length != BlobDataItem::toEndOfFile);
BlobDataItemList::const_iterator iter = items.begin();
if (offset) {
for (; iter != items.end(); ++iter) {
if (offset >= iter->length)
offset -= iter->length;
else
break;
}
}
for (; iter != items.end() && length > 0; ++iter) {
long long currentLength = iter->length - offset;
long long newLength = currentLength > length ? length : currentLength;
if (iter->type == BlobDataItem::Data)
blobStorageData->m_data.appendData(iter->data, iter->offset + offset, newLength);
#if ENABLE(FILE_SYSTEM)
else if (iter->type == BlobDataItem::URL)
blobStorageData->m_data.appendURL(iter->url, iter->offset + offset, newLength, iter->expectedModificationTime);
#endif
else {
ASSERT(iter->type == BlobDataItem::File);
blobStorageData->m_data.appendFile(iter->path, iter->offset + offset, newLength, iter->expectedModificationTime);
}
length -= newLength;
offset = 0;
}
}
void BlobRegistryImpl::registerBlobURL(const KURL& url, PassOwnPtr<BlobData> blobData)
{
#if !PLATFORM(IOS)
ASSERT(isMainThread());
#else
ASSERT((isMainThread() || pthread_main_np()) && WebThreadIsLockedOrDisabled());
#endif // !PLATFORM(IOS)
registerBlobResourceHandleConstructor();
RefPtr<BlobStorageData> blobStorageData = BlobStorageData::create(blobData->contentType(), blobData->contentDisposition());
for (BlobDataItemList::const_iterator iter = blobData->items().begin(); iter != blobData->items().end(); ++iter) {
switch (iter->type) {
case BlobDataItem::Data:
blobStorageData->m_data.appendData(iter->data, 0, iter->data->length());
break;
case BlobDataItem::File:
blobStorageData->m_data.appendFile(iter->path, iter->offset, iter->length, iter->expectedModificationTime);
break;
#if ENABLE(FILE_SYSTEM)
case BlobDataItem::URL:
blobStorageData->m_data.appendURL(iter->url, iter->offset, iter->length, iter->expectedModificationTime);
break;
#endif
case BlobDataItem::Blob:
if (m_blobs.contains(iter->url.string()))
appendStorageItems(blobStorageData.get(), m_blobs.get(iter->url.string())->items(), iter->offset, iter->length);
break;
}
}
m_blobs.set(url.string(), blobStorageData);
}
void BlobRegistryImpl::registerBlobURL(const KURL& url, const KURL& srcURL)
{
#if !PLATFORM(IOS)
ASSERT(isMainThread());
#else
ASSERT((isMainThread() || pthread_main_np()) && WebThreadIsLockedOrDisabled());
#endif // !PLATFORM(IOS)
registerBlobResourceHandleConstructor();
RefPtr<BlobStorageData> src = m_blobs.get(srcURL.string());
ASSERT(src);
if (!src)
return;
m_blobs.set(url.string(), src);
}
void BlobRegistryImpl::unregisterBlobURL(const KURL& url)
{
#if !PLATFORM(IOS)
ASSERT(isMainThread());
#else
ASSERT((isMainThread() || pthread_main_np()) && WebThreadIsLockedOrDisabled());
#endif // !PLATFORM(IOS)
m_blobs.remove(url.string());
}
BlobStorageData* BlobRegistryImpl::getBlobDataFromURL(const KURL& url) const
{
#if !PLATFORM(IOS)
ASSERT(isMainThread());
#else
ASSERT((isMainThread() || pthread_main_np()) && WebThreadIsLockedOrDisabled());
#endif // !PLATFORM(IOS)
return m_blobs.get(url.string());
}
}
#endif