ThreadableBlobRegistry.cpp [plain text]
#include "config.h"
#include "ThreadableBlobRegistry.h"
#include "BlobData.h"
#include "BlobRegistry.h"
#include "BlobURL.h"
#include "SecurityOrigin.h"
#include <wtf/HashMap.h>
#include <wtf/MainThread.h>
#include <wtf/RefPtr.h>
#include <wtf/ThreadSpecific.h>
#include <wtf/text/StringHash.h>
using WTF::ThreadSpecific;
namespace WebCore {
struct BlobRegistryContext {
WTF_MAKE_FAST_ALLOCATED;
public:
BlobRegistryContext(const KURL& url, PassOwnPtr<BlobData> blobData)
: url(url.copy())
, blobData(blobData)
{
this->blobData->detachFromCurrentThread();
}
BlobRegistryContext(const KURL& url, const KURL& srcURL)
: url(url.copy())
, srcURL(srcURL.copy())
{
}
BlobRegistryContext(const KURL& url)
: url(url.copy())
{
}
KURL url;
KURL srcURL;
OwnPtr<BlobData> blobData;
};
#if ENABLE(BLOB)
typedef HashMap<String, RefPtr<SecurityOrigin> > BlobUrlOriginMap;
static ThreadSpecific<BlobUrlOriginMap>& originMap()
{
AtomicallyInitializedStatic(ThreadSpecific<BlobUrlOriginMap>*, map = new ThreadSpecific<BlobUrlOriginMap>);
return *map;
}
static void registerBlobURLTask(void* context)
{
OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->blobData.release());
}
void ThreadableBlobRegistry::registerBlobURL(const KURL& url, PassOwnPtr<BlobData> blobData)
{
#if !PLATFORM(IOS)
if (isMainThread())
#else
if (isMainThread() || pthread_main_np())
#endif // !PLATFORM(IOS)
blobRegistry().registerBlobURL(url, blobData);
else {
OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, blobData));
callOnMainThread(®isterBlobURLTask, context.leakPtr());
}
}
static void registerBlobURLFromTask(void* context)
{
OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->srcURL);
}
void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin* origin, const KURL& url, const KURL& srcURL)
{
if (origin && BlobURL::getOrigin(url) == "null")
originMap()->add(url.string(), origin);
#if !PLATFORM(IOS)
if (isMainThread())
#else
if (isMainThread() || pthread_main_np())
#endif // !PLATFORM(IOS)
blobRegistry().registerBlobURL(url, srcURL);
else {
OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, srcURL));
callOnMainThread(®isterBlobURLFromTask, context.leakPtr());
}
}
static void unregisterBlobURLTask(void* context)
{
OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context));
blobRegistry().unregisterBlobURL(blobRegistryContext->url);
}
void ThreadableBlobRegistry::unregisterBlobURL(const KURL& url)
{
if (BlobURL::getOrigin(url) == "null")
originMap()->remove(url.string());
#if !PLATFORM(IOS)
if (isMainThread())
#else
if (isMainThread() || pthread_main_np())
#endif // !PLATFORM(IOS)
blobRegistry().unregisterBlobURL(url);
else {
OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url));
callOnMainThread(&unregisterBlobURLTask, context.leakPtr());
}
}
PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const KURL& url)
{
return originMap()->get(url.string());
}
#else
void ThreadableBlobRegistry::registerBlobURL(const KURL&, PassOwnPtr<BlobData>)
{
}
void ThreadableBlobRegistry::registerBlobURL(SecurityOrigin*, const KURL&, const KURL&)
{
}
void ThreadableBlobRegistry::unregisterBlobURL(const KURL&)
{
}
PassRefPtr<SecurityOrigin> ThreadableBlobRegistry::getCachedOrigin(const KURL&)
{
return 0;
}
#endif // ENABL(BLOB)
}