#pragma once
#include "BlobPropertyBag.h"
#include "ScriptWrappable.h"
#include "URL.h"
#include "URLRegistry.h"
#include <wtf/Variant.h>
namespace JSC {
class ArrayBufferView;
class ArrayBuffer;
}
namespace WebCore {
class Blob;
class ScriptExecutionContext;
class SharedBuffer;
using BlobPartVariant = Variant<RefPtr<JSC::ArrayBufferView>, RefPtr<JSC::ArrayBuffer>, RefPtr<Blob>, String>;
class Blob : public ScriptWrappable, public URLRegistrable, public RefCounted<Blob> {
public:
static Ref<Blob> create()
{
return adoptRef(*new Blob);
}
static Ref<Blob> create(Vector<BlobPartVariant>&& blobPartVariants, const BlobPropertyBag& propertyBag)
{
return adoptRef(*new Blob(WTFMove(blobPartVariants), propertyBag));
}
static Ref<Blob> create(const SharedBuffer& buffer, const String& contentType)
{
return adoptRef(*new Blob(buffer, contentType));
}
static Ref<Blob> create(Vector<uint8_t>&& data, const String& contentType)
{
return adoptRef(*new Blob(WTFMove(data), contentType));
}
static Ref<Blob> deserialize(const URL& srcURL, const String& type, long long size, const String& fileBackedPath)
{
ASSERT(Blob::isNormalizedContentType(type));
return adoptRef(*new Blob(deserializationContructor, srcURL, type, size, fileBackedPath));
}
virtual ~Blob();
const URL& url() const { return m_internalURL; }
const String& type() const { return m_type; }
WEBCORE_EXPORT unsigned long long size() const;
virtual bool isFile() const { return false; }
static bool isValidContentType(const String&);
static String normalizedContentType(const String&);
#if !ASSERT_DISABLED
static bool isNormalizedContentType(const String&);
static bool isNormalizedContentType(const CString&);
#endif
URLRegistry& registry() const override;
Ref<Blob> slice(long long start = 0, long long end = std::numeric_limits<long long>::max(), const String& contentType = String()) const
{
return adoptRef(*new Blob(m_internalURL, start, end, contentType));
}
protected:
WEBCORE_EXPORT Blob();
Blob(Vector<BlobPartVariant>&&, const BlobPropertyBag&);
Blob(const SharedBuffer&, const String& contentType);
Blob(Vector<uint8_t>&&, const String& contentType);
enum ReferencingExistingBlobConstructor { referencingExistingBlobConstructor };
Blob(ReferencingExistingBlobConstructor, const Blob&);
enum UninitializedContructor { uninitializedContructor };
Blob(UninitializedContructor);
enum DeserializationContructor { deserializationContructor };
Blob(DeserializationContructor, const URL& srcURL, const String& type, long long size, const String& fileBackedPath);
Blob(const URL& srcURL, long long start, long long end, const String& contentType);
URL m_internalURL;
String m_type;
mutable long long m_size;
};
}