2017-02-16 20:26:38 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "Blob.h"
|
|
|
|
#include "File.h"
|
|
|
|
#include "MemoryBlobImpl.h"
|
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
|
|
|
#include "MultipartBlobImpl.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "StreamBlobImpl.h"
|
|
|
|
#include "StringBlobImpl.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Blob)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Blob)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Blob)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Blob)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Blob)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2018-05-22 03:32:44 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMutable)
|
2018-05-22 02:33:18 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONCRETE(Blob)
|
2017-02-16 20:26:38 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutable)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Blob)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Blob)
|
|
|
|
|
|
|
|
void
|
|
|
|
Blob::MakeValidBlobType(nsAString& aType)
|
|
|
|
{
|
|
|
|
char16_t* iter = aType.BeginWriting();
|
|
|
|
char16_t* end = aType.EndWriting();
|
|
|
|
|
|
|
|
for ( ; iter != end; ++iter) {
|
|
|
|
char16_t c = *iter;
|
|
|
|
if (c < 0x20 || c > 0x7E) {
|
|
|
|
// Non-ASCII char, bail out.
|
|
|
|
aType.Truncate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c >= 'A' && c <= 'Z') {
|
|
|
|
*iter = c + ('a' - 'A');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ Blob*
|
|
|
|
Blob::Create(nsISupports* aParent, BlobImpl* aImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
|
|
|
|
return aImpl->IsFile() ? new File(aParent, aImpl)
|
|
|
|
: new Blob(aParent, aImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Blob>
|
|
|
|
Blob::CreateStringBlob(nsISupports* aParent, const nsACString& aData,
|
|
|
|
const nsAString& aContentType)
|
|
|
|
{
|
|
|
|
RefPtr<BlobImpl> blobImpl = StringBlobImpl::Create(aData, aContentType);
|
|
|
|
RefPtr<Blob> blob = Blob::Create(aParent, blobImpl);
|
|
|
|
MOZ_ASSERT(!blob->mImpl->IsFile());
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Blob>
|
|
|
|
Blob::CreateMemoryBlob(nsISupports* aParent, void* aMemoryBuffer,
|
|
|
|
uint64_t aLength, const nsAString& aContentType)
|
|
|
|
{
|
|
|
|
RefPtr<Blob> blob = Blob::Create(aParent,
|
|
|
|
new MemoryBlobImpl(aMemoryBuffer, aLength, aContentType));
|
|
|
|
MOZ_ASSERT(!blob->mImpl->IsFile());
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
Blob::Blob(nsISupports* aParent, BlobImpl* aImpl)
|
|
|
|
: mImpl(aImpl)
|
|
|
|
, mParent(aParent)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
Blob::~Blob()
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Blob::IsFile() const
|
|
|
|
{
|
|
|
|
return mImpl->IsFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsTArray<RefPtr<BlobImpl>>*
|
|
|
|
Blob::GetSubBlobImpls() const
|
|
|
|
{
|
|
|
|
return mImpl->GetSubBlobImpls();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<File>
|
|
|
|
Blob::ToFile()
|
|
|
|
{
|
|
|
|
if (!mImpl->IsFile()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<File> file;
|
|
|
|
if (HasFileInterface()) {
|
|
|
|
file = static_cast<File*>(this);
|
|
|
|
} else {
|
|
|
|
file = new File(mParent, mImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<File>
|
|
|
|
Blob::ToFile(const nsAString& aName, ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
AutoTArray<RefPtr<BlobImpl>, 1> blobImpls({mImpl});
|
|
|
|
|
|
|
|
nsAutoString contentType;
|
|
|
|
mImpl->GetType(contentType);
|
|
|
|
|
|
|
|
RefPtr<MultipartBlobImpl> impl =
|
2018-05-30 22:15:35 +03:00
|
|
|
MultipartBlobImpl::Create(std::move(blobImpls), aName, contentType, aRv);
|
2017-02-16 20:26:38 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<File> file = new File(mParent, impl);
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Blob>
|
|
|
|
Blob::CreateSlice(uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
RefPtr<BlobImpl> impl = mImpl->CreateSlice(aStart, aLength,
|
|
|
|
aContentType, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Blob> blob = Blob::Create(mParent, impl);
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
Blob::GetSize(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return mImpl->GetSize(aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Blob::GetType(nsAString &aType)
|
|
|
|
{
|
|
|
|
mImpl->GetType(aType);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Blob>
|
|
|
|
Blob::Slice(const Optional<int64_t>& aStart,
|
|
|
|
const Optional<int64_t>& aEnd,
|
2017-10-17 14:41:01 +03:00
|
|
|
const Optional<nsAString>& aContentType,
|
2017-02-16 20:26:38 +03:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2017-10-17 14:41:01 +03:00
|
|
|
nsAutoString contentType;
|
|
|
|
if (aContentType.WasPassed()) {
|
|
|
|
contentType = aContentType.Value();
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
RefPtr<BlobImpl> impl =
|
2017-10-17 14:41:01 +03:00
|
|
|
mImpl->Slice(aStart, aEnd, contentType, aRv);
|
2017-02-16 20:26:38 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Blob> blob = Blob::Create(mParent, impl);
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:46:51 +03:00
|
|
|
size_t
|
|
|
|
Blob::GetAllocationSize() const
|
|
|
|
{
|
|
|
|
return mImpl->GetAllocationSize();
|
|
|
|
}
|
|
|
|
|
2017-09-20 04:32:12 +03:00
|
|
|
// contentTypeWithCharset can be set to the contentType or
|
|
|
|
// contentType+charset based on what the spec says.
|
|
|
|
// See: https://fetch.spec.whatwg.org/#concept-bodyinit-extract
|
|
|
|
nsresult
|
2017-02-16 20:26:38 +03:00
|
|
|
Blob::GetSendInfo(nsIInputStream** aBody,
|
|
|
|
uint64_t* aContentLength,
|
|
|
|
nsACString& aContentType,
|
2017-09-20 04:32:12 +03:00
|
|
|
nsACString& aCharset) const
|
2017-02-16 20:26:38 +03:00
|
|
|
{
|
|
|
|
return mImpl->GetSendInfo(aBody, aContentLength, aContentType, aCharset);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Blob::GetMutable(bool* aMutable)
|
|
|
|
{
|
|
|
|
return mImpl->GetMutable(aMutable);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Blob::SetMutable(bool aMutable)
|
|
|
|
{
|
|
|
|
return mImpl->SetMutable(aMutable);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
Blob::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
2018-06-26 00:20:54 +03:00
|
|
|
return Blob_Binding::Wrap(aCx, this, aGivenProto);
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Blob>
|
|
|
|
Blob::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Optional<Sequence<BlobPart>>& aData,
|
|
|
|
const BlobPropertyBag& aBag,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl();
|
|
|
|
|
|
|
|
if (aData.WasPassed()) {
|
|
|
|
nsAutoString type(aBag.mType);
|
|
|
|
MakeValidBlobType(type);
|
2017-04-25 23:30:23 +03:00
|
|
|
impl->InitializeBlob(aData.Value(), type,
|
2017-02-16 20:26:38 +03:00
|
|
|
aBag.mEndings == EndingTypes::Native, aRv);
|
|
|
|
} else {
|
|
|
|
impl->InitializeBlob(aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!impl->IsFile());
|
|
|
|
|
|
|
|
RefPtr<Blob> blob = Blob::Create(aGlobal.GetAsSupports(), impl);
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
Blob::GetFileId()
|
|
|
|
{
|
|
|
|
return mImpl->GetFileId();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Blob::IsMemoryFile() const
|
|
|
|
{
|
|
|
|
return mImpl->IsMemoryFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-10-02 14:53:12 +03:00
|
|
|
Blob::CreateInputStream(nsIInputStream** aStream, ErrorResult& aRv)
|
2017-02-16 20:26:38 +03:00
|
|
|
{
|
2017-10-02 14:53:12 +03:00
|
|
|
mImpl->CreateInputStream(aStream, aRv);
|
2017-02-16 20:26:38 +03:00
|
|
|
}
|
|
|
|
|
2017-09-13 13:21:12 +03:00
|
|
|
size_t
|
2017-09-12 12:46:51 +03:00
|
|
|
BindingJSObjectMallocBytes(Blob* aBlob)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBlob);
|
|
|
|
return aBlob->GetAllocationSize();
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|