#include "config.h"
#include "Blob.h"
#include "BlobBuilder.h"
#include "BlobPart.h"
#include "BlobURL.h"
#include "File.h"
#include "ScriptExecutionContext.h"
#include "SharedBuffer.h"
#include "ThreadableBlobRegistry.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/text/CString.h>
namespace WebCore {
class BlobURLRegistry final : public URLRegistry {
public:
void registerURL(SecurityOrigin*, const URL&, URLRegistrable&) override;
void unregisterURL(const URL&) override;
static URLRegistry& registry();
};
void BlobURLRegistry::registerURL(SecurityOrigin* origin, const URL& publicURL, URLRegistrable& blob)
{
ASSERT(&blob.registry() == this);
ThreadableBlobRegistry::registerBlobURL(origin, publicURL, static_cast<Blob&>(blob).url());
}
void BlobURLRegistry::unregisterURL(const URL& url)
{
ThreadableBlobRegistry::unregisterBlobURL(url);
}
URLRegistry& BlobURLRegistry::registry()
{
static NeverDestroyed<BlobURLRegistry> instance;
return instance;
}
Blob::Blob(UninitializedContructor)
{
}
Blob::Blob()
: m_size(0)
{
m_internalURL = BlobURL::createInternalURL();
ThreadableBlobRegistry::registerBlobURL(m_internalURL, { }, { });
}
Blob::Blob(Vector<BlobPartVariant>&& blobPartVariants, const BlobPropertyBag& propertyBag)
: m_internalURL(BlobURL::createInternalURL())
, m_type(normalizedContentType(propertyBag.type))
, m_size(-1)
{
BlobBuilder builder(propertyBag.endings);
for (auto& blobPartVariant : blobPartVariants) {
WTF::switchOn(blobPartVariant,
[&] (auto& part) {
builder.append(WTFMove(part));
}
);
}
ThreadableBlobRegistry::registerBlobURL(m_internalURL, builder.finalize(), m_type);
}
Blob::Blob(const SharedBuffer& buffer, const String& contentType)
: m_type(contentType)
, m_size(buffer.size())
{
Vector<uint8_t> data;
data.append(buffer.data(), buffer.size());
Vector<BlobPart> blobParts;
blobParts.append(BlobPart(WTFMove(data)));
m_internalURL = BlobURL::createInternalURL();
ThreadableBlobRegistry::registerBlobURL(m_internalURL, WTFMove(blobParts), contentType);
}
Blob::Blob(Vector<uint8_t>&& data, const String& contentType)
: m_type(contentType)
, m_size(data.size())
{
Vector<BlobPart> blobParts;
blobParts.append(BlobPart(WTFMove(data)));
m_internalURL = BlobURL::createInternalURL();
ThreadableBlobRegistry::registerBlobURL(m_internalURL, WTFMove(blobParts), contentType);
}
Blob::Blob(ReferencingExistingBlobConstructor, const Blob& blob)
: m_internalURL(BlobURL::createInternalURL())
, m_type(blob.type())
, m_size(blob.size())
{
ThreadableBlobRegistry::registerBlobURL(m_internalURL, { BlobPart(blob.url()) } , m_type);
}
Blob::Blob(DeserializationContructor, const URL& srcURL, const String& type, long long size, const String& fileBackedPath)
: m_type(normalizedContentType(type))
, m_size(size)
{
m_internalURL = BlobURL::createInternalURL();
if (fileBackedPath.isEmpty())
ThreadableBlobRegistry::registerBlobURL(nullptr, m_internalURL, srcURL);
else
ThreadableBlobRegistry::registerBlobURLOptionallyFileBacked(m_internalURL, srcURL, fileBackedPath, m_type);
}
Blob::Blob(const URL& srcURL, long long start, long long end, const String& type)
: m_type(normalizedContentType(type))
, m_size(-1) {
m_internalURL = BlobURL::createInternalURL();
ThreadableBlobRegistry::registerBlobURLForSlice(m_internalURL, srcURL, start, end);
}
Blob::~Blob()
{
ThreadableBlobRegistry::unregisterBlobURL(m_internalURL);
}
unsigned long long Blob::size() const
{
if (m_size < 0) {
unsigned long long actualSize = ThreadableBlobRegistry::blobSize(m_internalURL);
m_size = WTF::isInBounds<long long>(actualSize) ? static_cast<long long>(actualSize) : 0;
}
return static_cast<unsigned long long>(m_size);
}
bool Blob::isValidContentType(const String& contentType)
{
unsigned length = contentType.length();
for (unsigned i = 0; i < length; ++i) {
if (contentType[i] < 0x20 || contentType[i] > 0x7e)
return false;
}
return true;
}
String Blob::normalizedContentType(const String& contentType)
{
if (!isValidContentType(contentType))
return emptyString();
return contentType.convertToASCIILowercase();
}
#if !ASSERT_DISABLED
bool Blob::isNormalizedContentType(const String& contentType)
{
unsigned length = contentType.length();
for (size_t i = 0; i < length; ++i) {
if (contentType[i] < 0x20 || contentType[i] > 0x7e)
return false;
if (isASCIIUpper(contentType[i]))
return false;
}
return true;
}
bool Blob::isNormalizedContentType(const CString& contentType)
{
size_t length = contentType.length();
const char* characters = contentType.data();
for (size_t i = 0; i < length; ++i) {
if (characters[i] < 0x20 || characters[i] > 0x7e)
return false;
if (isASCIIUpper(characters[i]))
return false;
}
return true;
}
#endif
URLRegistry& Blob::registry() const
{
return BlobURLRegistry::registry();
}
}