2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-10-07 17:16:11 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
#include "MultipartBlobImpl.h"
|
2014-10-07 17:16:11 +00:00
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsContentCID.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsICharsetDetector.h"
|
|
|
|
#include "nsIConverterInputStream.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsIUnicharInputStream.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsIRemoteBlob.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIUUIDGenerator.h"
|
|
|
|
#include "nsHostObjectProtocolHandler.h"
|
|
|
|
#include "nsStringStream.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "mozilla/SHA1.h"
|
|
|
|
#include "mozilla/CheckedInt.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2014-10-08 16:15:22 +00:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
|
|
|
#include "mozilla/dom/DOMError.h"
|
|
|
|
#include "mozilla/dom/FileBinding.h"
|
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2016-03-31 07:32:58 +00:00
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
2014-10-07 17:16:11 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2016-09-21 10:27:26 +00:00
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "SlicedInputStream.h"
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-03-31 07:32:58 +00:00
|
|
|
using namespace workers;
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
// XXXkhuey the input stream that we pass out of a File
|
|
|
|
// can outlive the actual File object. Thus, we must
|
2014-10-07 17:16:11 +00:00
|
|
|
// ensure that the buffer underlying the stream we get
|
|
|
|
// from NS_NewByteInputStream is held alive as long as the
|
|
|
|
// stream is. We do that by passing back this class instead.
|
2015-03-21 16:28:04 +00:00
|
|
|
class DataOwnerAdapter final : public nsIInputStream,
|
2015-03-27 18:52:19 +00:00
|
|
|
public nsISeekableStream,
|
|
|
|
public nsIIPCSerializableInputStream
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-05-12 12:11:03 +00:00
|
|
|
typedef BlobImplMemory::DataOwner DataOwner;
|
2014-10-07 17:16:11 +00:00
|
|
|
public:
|
|
|
|
static nsresult Create(DataOwner* aDataOwner,
|
|
|
|
uint32_t aStart,
|
|
|
|
uint32_t aLength,
|
|
|
|
nsIInputStream** _retval);
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
// These are mandatory.
|
|
|
|
NS_FORWARD_NSIINPUTSTREAM(mStream->)
|
|
|
|
NS_FORWARD_NSISEEKABLESTREAM(mSeekableStream->)
|
|
|
|
|
|
|
|
// This is optional. We use a conditional QI to keep it from being called
|
|
|
|
// if the underlying stream doesn't support it.
|
|
|
|
NS_FORWARD_NSIIPCSERIALIZABLEINPUTSTREAM(mSerializableInputStream->)
|
|
|
|
|
|
|
|
private:
|
|
|
|
~DataOwnerAdapter() {}
|
|
|
|
|
|
|
|
DataOwnerAdapter(DataOwner* aDataOwner,
|
|
|
|
nsIInputStream* aStream)
|
|
|
|
: mDataOwner(aDataOwner), mStream(aStream),
|
|
|
|
mSeekableStream(do_QueryInterface(aStream)),
|
|
|
|
mSerializableInputStream(do_QueryInterface(aStream))
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mSeekableStream, "Somebody gave us the wrong stream!");
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DataOwner> mDataOwner;
|
2014-10-07 17:16:11 +00:00
|
|
|
nsCOMPtr<nsIInputStream> mStream;
|
|
|
|
nsCOMPtr<nsISeekableStream> mSeekableStream;
|
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> mSerializableInputStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(DataOwnerAdapter)
|
|
|
|
NS_IMPL_RELEASE(DataOwnerAdapter)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(DataOwnerAdapter)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISeekableStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIIPCSerializableInputStream,
|
|
|
|
mSerializableInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
nsresult DataOwnerAdapter::Create(DataOwner* aDataOwner,
|
|
|
|
uint32_t aStart,
|
|
|
|
uint32_t aLength,
|
|
|
|
nsIInputStream** _retval)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
NS_ASSERTION(aDataOwner, "Uh ...");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
|
|
|
|
rv = NS_NewByteInputStream(getter_AddRefs(stream),
|
|
|
|
static_cast<const char*>(aDataOwner->mData) +
|
|
|
|
aStart,
|
|
|
|
(int32_t)aLength,
|
|
|
|
NS_ASSIGNMENT_DEPEND);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ADDREF(*_retval = new DataOwnerAdapter(aDataOwner, stream));
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 12:09:51 +00:00
|
|
|
// mozilla::dom::Blob implementation
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Blob)
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Blob)
|
2014-10-08 16:15:22 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2014-10-07 17:16:11 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Blob)
|
2014-10-08 16:15:22 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2014-10-07 17:16:11 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Blob)
|
2014-10-08 16:15:22 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Blob)
|
2014-10-07 17:16:11 +00:00
|
|
|
// This class should not receive any nsIRemoteBlob QI!
|
|
|
|
MOZ_ASSERT(!aIID.Equals(NS_GET_IID(nsIRemoteBlob)));
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2015-05-12 12:09:51 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMBlob)
|
2014-10-07 17:16:11 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMBlob)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIXHRSendable)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutable)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Blob)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Blob)
|
2015-05-11 17:50:50 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
/* static */ Blob*
|
2015-05-12 12:11:03 +00:00
|
|
|
Blob::Create(nsISupports* aParent, BlobImpl* aImpl)
|
2015-05-11 17:50:50 +00:00
|
|
|
{
|
2015-05-12 12:09:51 +00:00
|
|
|
MOZ_ASSERT(aImpl);
|
2015-05-11 17:50:50 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
return aImpl->IsFile() ? new File(aParent, aImpl)
|
|
|
|
: new Blob(aParent, aImpl);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
/* static */ already_AddRefed<Blob>
|
|
|
|
Blob::Create(nsISupports* aParent, const nsAString& aContentType,
|
2015-05-11 21:54:02 +00:00
|
|
|
uint64_t aLength)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> blob = Blob::Create(aParent,
|
2015-05-12 12:11:03 +00:00
|
|
|
new BlobImplBase(aContentType, aLength));
|
2015-05-12 12:09:51 +00:00
|
|
|
MOZ_ASSERT(!blob->mImpl->IsFile());
|
|
|
|
return blob.forget();
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
/* static */ already_AddRefed<Blob>
|
|
|
|
Blob::Create(nsISupports* aParent, const nsAString& aContentType,
|
|
|
|
uint64_t aStart, uint64_t aLength)
|
2015-05-11 15:43:59 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> blob = Blob::Create(aParent,
|
2015-05-12 12:11:03 +00:00
|
|
|
new BlobImplBase(aContentType, aStart, aLength));
|
2015-05-12 12:09:51 +00:00
|
|
|
MOZ_ASSERT(!blob->mImpl->IsFile());
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
2016-10-25 05:53:54 +00:00
|
|
|
/* static */ already_AddRefed<Blob>
|
|
|
|
Blob::CreateStringBlob(nsISupports* aParent, const nsACString& aData,
|
|
|
|
const nsAString& aContentType)
|
|
|
|
{
|
2016-11-02 09:35:57 +00:00
|
|
|
RefPtr<BlobImpl> blobImpl = BlobImplString::Create(aData, aContentType);
|
|
|
|
RefPtr<Blob> blob = Blob::Create(aParent, blobImpl);
|
2016-10-25 05:53:54 +00:00
|
|
|
MOZ_ASSERT(!blob->mImpl->IsFile());
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
/* static */ already_AddRefed<Blob>
|
|
|
|
Blob::CreateMemoryBlob(nsISupports* aParent, void* aMemoryBuffer,
|
|
|
|
uint64_t aLength, const nsAString& aContentType)
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> blob = Blob::Create(aParent,
|
2015-05-12 12:11:03 +00:00
|
|
|
new BlobImplMemory(aMemoryBuffer, aLength, aContentType));
|
2015-05-12 12:09:51 +00:00
|
|
|
MOZ_ASSERT(!blob->mImpl->IsFile());
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Blob>
|
|
|
|
Blob::CreateTemporaryBlob(nsISupports* aParent, PRFileDesc* aFD,
|
|
|
|
uint64_t aStartPos, uint64_t aLength,
|
|
|
|
const nsAString& aContentType)
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> blob = Blob::Create(aParent,
|
2015-05-12 12:11:03 +00:00
|
|
|
new BlobImplTemporaryBlob(aFD, aStartPos, aLength, aContentType));
|
2015-05-12 12:09:51 +00:00
|
|
|
MOZ_ASSERT(!blob->mImpl->IsFile());
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
Blob::Blob(nsISupports* aParent, BlobImpl* aImpl)
|
2015-05-12 12:09:51 +00:00
|
|
|
: mImpl(aImpl)
|
|
|
|
, mParent(aParent)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aParent);
|
2015-05-12 12:09:51 +00:00
|
|
|
if (win) {
|
|
|
|
MOZ_ASSERT(win->IsInnerWindow());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Blob::IsFile() const
|
|
|
|
{
|
|
|
|
return mImpl->IsFile();
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
const nsTArray<RefPtr<BlobImpl>>*
|
2015-05-12 12:09:51 +00:00
|
|
|
Blob::GetSubBlobImpls() const
|
|
|
|
{
|
|
|
|
return mImpl->GetSubBlobImpls();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<File>
|
|
|
|
Blob::ToFile()
|
|
|
|
{
|
|
|
|
if (!mImpl->IsFile()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> file;
|
2015-05-12 12:09:51 +00:00
|
|
|
if (HasFileInterface()) {
|
|
|
|
file = static_cast<File*>(this);
|
|
|
|
} else {
|
|
|
|
file = new File(mParent, mImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<File>
|
2015-12-04 21:15:46 +00:00
|
|
|
Blob::ToFile(const nsAString& aName, ErrorResult& aRv) const
|
2015-05-12 12:09:51 +00:00
|
|
|
{
|
2016-08-09 14:21:26 +00:00
|
|
|
AutoTArray<RefPtr<BlobImpl>, 1> blobImpls({mImpl});
|
2015-05-12 12:09:51 +00:00
|
|
|
|
|
|
|
nsAutoString contentType;
|
|
|
|
mImpl->GetType(contentType);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MultipartBlobImpl> impl =
|
2016-09-21 01:29:28 +00:00
|
|
|
MultipartBlobImpl::Create(Move(blobImpls), aName, contentType, aRv);
|
2015-12-04 21:15:46 +00:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-12 12:09:51 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> file = new File(mParent, impl);
|
2015-05-11 15:43:59 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
already_AddRefed<Blob>
|
|
|
|
Blob::CreateSlice(uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl> impl = mImpl->CreateSlice(aStart, aLength,
|
2016-09-21 10:27:26 +00:00
|
|
|
aContentType, aRv);
|
2015-05-12 12:09:51 +00:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> blob = Blob::Create(mParent, impl);
|
2015-05-12 12:09:51 +00:00
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
Blob::GetSize(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return mImpl->GetSize(aRv);
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
void
|
2015-05-12 12:09:51 +00:00
|
|
|
Blob::GetType(nsAString &aType)
|
|
|
|
{
|
|
|
|
mImpl->GetType(aType);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Blob>
|
|
|
|
Blob::Slice(const Optional<int64_t>& aStart,
|
|
|
|
const Optional<int64_t>& aEnd,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl> impl =
|
2015-05-12 12:09:51 +00:00
|
|
|
mImpl->Slice(aStart, aEnd, aContentType, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> blob = Blob::Create(mParent, impl);
|
2015-05-12 12:09:51 +00:00
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Blob::GetSendInfo(nsIInputStream** aBody,
|
|
|
|
uint64_t* aContentLength,
|
|
|
|
nsACString& aContentType,
|
|
|
|
nsACString& aCharset)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return BlobBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Blob>
|
2016-06-01 10:33:09 +00:00
|
|
|
Blob::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Optional<Sequence<BlobPart>>& aData,
|
|
|
|
const BlobPropertyBag& aBag,
|
|
|
|
ErrorResult& aRv)
|
2015-05-12 12:09:51 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl();
|
2015-05-12 12:09:51 +00:00
|
|
|
|
2016-06-01 10:33:09 +00:00
|
|
|
if (aData.WasPassed()) {
|
|
|
|
impl->InitializeBlob(aGlobal.Context(), aData.Value(), aBag.mType,
|
|
|
|
aBag.mEndings == EndingTypes::Native, aRv);
|
|
|
|
} else {
|
|
|
|
impl->InitializeBlob(aRv);
|
2015-12-04 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
2016-06-01 10:33:09 +00:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-05-12 12:09:51 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-06-01 10:33:09 +00:00
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
MOZ_ASSERT(!impl->IsFile());
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Blob> blob = Blob::Create(aGlobal.GetAsSupports(), impl);
|
2015-05-12 12:09:51 +00:00
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
int64_t
|
2015-05-12 12:09:51 +00:00
|
|
|
Blob::GetFileId()
|
|
|
|
{
|
|
|
|
return mImpl->GetFileId();
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
bool
|
|
|
|
Blob::IsMemoryFile() const
|
2015-05-12 12:09:51 +00:00
|
|
|
{
|
|
|
|
return mImpl->IsMemoryFile();
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
void
|
|
|
|
Blob::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
|
2015-05-12 12:09:51 +00:00
|
|
|
{
|
2015-05-19 14:36:37 +00:00
|
|
|
mImpl->GetInternalStream(aStream, aRv);
|
2015-05-12 12:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// mozilla::dom::File implementation
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
File::File(nsISupports* aParent, BlobImpl* aImpl)
|
2015-05-12 12:09:51 +00:00
|
|
|
: Blob(aParent, aImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl->IsFile());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ File*
|
2015-05-12 12:11:03 +00:00
|
|
|
File::Create(nsISupports* aParent, BlobImpl* aImpl)
|
2015-05-12 12:09:51 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
MOZ_ASSERT(aImpl->IsFile());
|
|
|
|
|
|
|
|
return new File(aParent, aImpl);
|
|
|
|
}
|
|
|
|
|
2015-05-11 15:43:59 +00:00
|
|
|
/* static */ already_AddRefed<File>
|
2015-05-12 12:09:51 +00:00
|
|
|
File::Create(nsISupports* aParent, const nsAString& aName,
|
|
|
|
const nsAString& aContentType, uint64_t aLength,
|
2016-03-20 10:56:10 +00:00
|
|
|
int64_t aLastModifiedDate)
|
2015-05-11 15:43:59 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> file = new File(aParent,
|
2016-03-20 10:56:10 +00:00
|
|
|
new BlobImplBase(aName, aContentType, aLength, aLastModifiedDate));
|
2015-05-11 15:43:59 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-11 21:54:02 +00:00
|
|
|
/* static */ already_AddRefed<File>
|
2015-05-12 12:09:51 +00:00
|
|
|
File::CreateMemoryFile(nsISupports* aParent, void* aMemoryBuffer,
|
|
|
|
uint64_t aLength, const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate)
|
2015-05-11 21:54:02 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> file = new File(aParent,
|
2015-05-12 12:11:03 +00:00
|
|
|
new BlobImplMemory(aMemoryBuffer, aLength, aName,
|
2015-05-12 12:09:51 +00:00
|
|
|
aContentType, aLastModifiedDate));
|
2015-05-11 21:54:02 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
/* static */ already_AddRefed<File>
|
2014-10-15 05:55:14 +00:00
|
|
|
File::CreateFromFile(nsISupports* aParent, nsIFile* aFile, bool aTemporary)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> file = new File(aParent, new BlobImplFile(aFile, aTemporary));
|
2014-10-07 17:16:11 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
/* static */ already_AddRefed<File>
|
|
|
|
File::CreateFromFile(nsISupports* aParent, nsIFile* aFile,
|
|
|
|
const nsAString& aName, const nsAString& aContentType)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> file = new File(aParent,
|
2015-05-12 12:11:03 +00:00
|
|
|
new BlobImplFile(aFile, aName, aContentType));
|
2014-10-07 17:16:11 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
JSObject*
|
|
|
|
File::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-05-12 12:09:51 +00:00
|
|
|
return FileBinding::Wrap(aCx, this, aGivenProto);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 13:51:54 +00:00
|
|
|
void
|
2016-04-01 10:50:16 +00:00
|
|
|
File::GetName(nsAString& aFileName) const
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-05-12 12:09:51 +00:00
|
|
|
mImpl->GetName(aFileName);
|
2015-05-11 15:43:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 13:51:54 +00:00
|
|
|
void
|
2016-05-13 11:11:38 +00:00
|
|
|
File::GetPath(nsAString& aPath) const
|
2015-05-11 15:43:59 +00:00
|
|
|
{
|
2016-05-13 11:11:38 +00:00
|
|
|
mImpl->GetPath(aPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
File::SetPath(const nsAString& aPath)
|
|
|
|
{
|
|
|
|
mImpl->SetPath(aPath);
|
2015-05-11 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
Date
|
|
|
|
File::GetLastModifiedDate(ErrorResult& aRv)
|
2015-05-11 21:54:02 +00:00
|
|
|
{
|
2015-05-12 12:09:51 +00:00
|
|
|
int64_t value = GetLastModified(aRv);
|
2014-10-08 16:15:22 +00:00
|
|
|
if (aRv.Failed()) {
|
2015-05-12 12:09:51 +00:00
|
|
|
return Date();
|
2014-10-08 16:15:22 +00:00
|
|
|
}
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-02 02:12:52 +00:00
|
|
|
return Date(JS::TimeClip(value));
|
2015-05-11 21:54:02 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 12:09:51 +00:00
|
|
|
int64_t
|
|
|
|
File::GetLastModified(ErrorResult& aRv)
|
2015-05-11 21:54:02 +00:00
|
|
|
{
|
2015-05-12 12:09:51 +00:00
|
|
|
return mImpl->GetLastModified(aRv);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
void
|
2015-06-03 00:11:16 +00:00
|
|
|
File::GetMozFullPath(nsAString& aFilename, ErrorResult& aRv) const
|
2014-10-08 16:15:22 +00:00
|
|
|
{
|
|
|
|
mImpl->GetMozFullPath(aFilename, aRv);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 13:51:54 +00:00
|
|
|
void
|
2015-06-03 00:11:16 +00:00
|
|
|
File::GetMozFullPathInternal(nsAString& aFileName, ErrorResult& aRv) const
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-05-18 13:51:54 +00:00
|
|
|
mImpl->GetMozFullPathInternal(aFileName, aRv);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Makes sure that aStart and aEnd is less then or equal to aSize and greater
|
|
|
|
// than 0
|
|
|
|
static void
|
|
|
|
ParseSize(int64_t aSize, int64_t& aStart, int64_t& aEnd)
|
|
|
|
{
|
|
|
|
CheckedInt64 newStartOffset = aStart;
|
|
|
|
if (aStart < -aSize) {
|
|
|
|
newStartOffset = 0;
|
|
|
|
}
|
|
|
|
else if (aStart < 0) {
|
|
|
|
newStartOffset += aSize;
|
|
|
|
}
|
|
|
|
else if (aStart > aSize) {
|
|
|
|
newStartOffset = aSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckedInt64 newEndOffset = aEnd;
|
|
|
|
if (aEnd < -aSize) {
|
|
|
|
newEndOffset = 0;
|
|
|
|
}
|
|
|
|
else if (aEnd < 0) {
|
|
|
|
newEndOffset += aSize;
|
|
|
|
}
|
|
|
|
else if (aEnd > aSize) {
|
|
|
|
newEndOffset = aSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!newStartOffset.isValid() || !newEndOffset.isValid() ||
|
|
|
|
newStartOffset.value() >= newEndOffset.value()) {
|
|
|
|
aStart = aEnd = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aStart = newStartOffset.value();
|
|
|
|
aEnd = newEndOffset.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
/* static */ already_AddRefed<File>
|
2016-06-01 10:33:09 +00:00
|
|
|
File::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Sequence<BlobPart>& aData,
|
|
|
|
const nsAString& aName,
|
|
|
|
const FilePropertyBag& aBag,
|
|
|
|
ErrorResult& aRv)
|
2014-10-08 16:15:22 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(aName);
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
impl->InitializeBlob(aGlobal.Context(), aData, aBag.mType, false, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
2014-10-08 16:15:22 +00:00
|
|
|
MOZ_ASSERT(impl->IsFile());
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-02-07 18:29:22 +00:00
|
|
|
if (aBag.mLastModified.WasPassed()) {
|
|
|
|
impl->SetLastModified(aBag.mLastModified.Value());
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> file = new File(aGlobal.GetAsSupports(), impl);
|
2014-10-08 16:15:22 +00:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
/* static */ already_AddRefed<File>
|
|
|
|
File::Constructor(const GlobalObject& aGlobal,
|
2015-05-12 12:09:51 +00:00
|
|
|
Blob& aData,
|
2014-10-08 16:15:23 +00:00
|
|
|
const ChromeFilePropertyBag& aBag,
|
2014-10-08 16:15:23 +00:00
|
|
|
ErrorResult& aRv)
|
2014-10-08 16:15:22 +00:00
|
|
|
{
|
2015-01-30 18:54:43 +00:00
|
|
|
if (!nsContentUtils::ThreadsafeIsCallerChrome()) {
|
2016-07-25 20:42:31 +00:00
|
|
|
aRv.ThrowTypeError<MSG_NOT_SEQUENCE>(NS_LITERAL_STRING("Argument 1 of File.constructor"));
|
2014-10-08 16:15:22 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(EmptyString());
|
2014-10-08 16:15:22 +00:00
|
|
|
impl->InitializeChromeFile(aData, aBag, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(impl->IsFile());
|
|
|
|
|
2015-02-10 08:31:00 +00:00
|
|
|
if (aBag.mLastModified.WasPassed()) {
|
|
|
|
impl->SetLastModified(aBag.mLastModified.Value());
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> domFile = new File(aGlobal.GetAsSupports(), impl);
|
2014-10-08 16:15:22 +00:00
|
|
|
return domFile.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
/* static */ already_AddRefed<File>
|
|
|
|
File::Constructor(const GlobalObject& aGlobal,
|
|
|
|
nsIFile* aData,
|
2014-10-08 16:15:23 +00:00
|
|
|
const ChromeFilePropertyBag& aBag,
|
2014-10-08 16:15:23 +00:00
|
|
|
ErrorResult& aRv)
|
2014-10-08 16:15:22 +00:00
|
|
|
{
|
2015-01-30 18:54:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-10-08 16:15:22 +00:00
|
|
|
if (!nsContentUtils::IsCallerChrome()) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2014-10-08 16:15:22 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(EmptyString());
|
2014-10-08 16:15:22 +00:00
|
|
|
impl->InitializeChromeFile(window, aData, aBag, true, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(impl->IsFile());
|
|
|
|
|
2015-02-10 08:31:00 +00:00
|
|
|
if (aBag.mLastModified.WasPassed()) {
|
|
|
|
impl->SetLastModified(aBag.mLastModified.Value());
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> domFile = new File(aGlobal.GetAsSupports(), impl);
|
2014-10-08 16:15:22 +00:00
|
|
|
return domFile.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
/* static */ already_AddRefed<File>
|
|
|
|
File::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aData,
|
2014-10-08 16:15:23 +00:00
|
|
|
const ChromeFilePropertyBag& aBag,
|
2014-10-08 16:15:23 +00:00
|
|
|
ErrorResult& aRv)
|
2014-10-08 16:15:22 +00:00
|
|
|
{
|
2015-01-30 18:54:43 +00:00
|
|
|
if (!nsContentUtils::ThreadsafeIsCallerChrome()) {
|
2016-04-19 02:32:43 +00:00
|
|
|
aRv.ThrowTypeError<MSG_MISSING_ARGUMENTS>(NS_LITERAL_STRING("File"));
|
2014-10-08 16:15:22 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2014-10-08 16:15:22 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(EmptyString());
|
2014-10-08 16:15:22 +00:00
|
|
|
impl->InitializeChromeFile(window, aData, aBag, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(impl->IsFile());
|
|
|
|
|
2015-02-10 08:31:00 +00:00
|
|
|
if (aBag.mLastModified.WasPassed()) {
|
|
|
|
impl->SetLastModified(aBag.mLastModified.Value());
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<File> domFile = new File(aGlobal.GetAsSupports(), impl);
|
2014-10-08 16:15:22 +00:00
|
|
|
return domFile.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 12:11:03 +00:00
|
|
|
// mozilla::dom::BlobImpl implementation
|
2014-10-08 16:15:22 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
BlobImpl::Slice(const Optional<int64_t>& aStart,
|
2014-10-08 16:15:23 +00:00
|
|
|
const Optional<int64_t>& aEnd,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
2014-10-08 16:15:22 +00:00
|
|
|
{
|
|
|
|
// Truncate aStart and aEnd so that we stay within this file.
|
|
|
|
uint64_t thisLength = GetSize(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t start = aStart.WasPassed() ? aStart.Value() : 0;
|
|
|
|
int64_t end = aEnd.WasPassed() ? aEnd.Value() : (int64_t)thisLength;
|
|
|
|
|
|
|
|
ParseSize((int64_t)thisLength, start, end);
|
|
|
|
|
|
|
|
return CreateSlice((uint64_t)start, (uint64_t)(end - start),
|
|
|
|
aContentType, aRv);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 12:11:03 +00:00
|
|
|
// BlobImpl implementation
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
NS_IMPL_ISUPPORTS(BlobImpl, BlobImpl)
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 12:11:03 +00:00
|
|
|
// BlobImplFile implementation
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(BlobImplFile, BlobImpl)
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
void
|
2016-04-01 10:50:16 +00:00
|
|
|
BlobImplBase::GetName(nsAString& aName) const
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mIsFile, "Should only be called on files");
|
|
|
|
aName = mName;
|
|
|
|
}
|
|
|
|
|
2015-05-18 13:51:54 +00:00
|
|
|
void
|
2016-05-13 11:11:38 +00:00
|
|
|
BlobImplBase::GetPath(nsAString& aPath) const
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mIsFile, "Should only be called on files");
|
|
|
|
aPath = mPath;
|
|
|
|
}
|
|
|
|
|
2016-05-13 11:11:38 +00:00
|
|
|
void
|
|
|
|
BlobImplBase::SetPath(const nsAString& aPath)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mIsFile, "Should only be called on files");
|
|
|
|
mPath = aPath;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
void
|
2015-06-03 00:11:16 +00:00
|
|
|
BlobImplBase::GetMozFullPath(nsAString& aFileName, ErrorResult& aRv) const
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2014-10-08 16:15:22 +00:00
|
|
|
NS_ASSERTION(mIsFile, "Should only be called on files");
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
aFileName.Truncate();
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
if (NS_IsMainThread()) {
|
2014-09-25 09:04:45 +00:00
|
|
|
if (nsContentUtils::LegacyIsCallerChromeOrNativeCode()) {
|
2014-10-08 16:15:22 +00:00
|
|
|
GetMozFullPathInternal(aFileName, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2016-03-31 07:32:58 +00:00
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
2014-10-08 16:15:22 +00:00
|
|
|
MOZ_ASSERT(workerPrivate);
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
if (workerPrivate->UsesSystemPrincipal()) {
|
|
|
|
GetMozFullPathInternal(aFileName, aRv);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
void
|
2015-06-03 00:11:16 +00:00
|
|
|
BlobImplBase::GetMozFullPathInternal(nsAString& aFileName, ErrorResult& aRv) const
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
if (!mIsFile) {
|
2014-10-08 16:15:22 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aFileName.Truncate();
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
void
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase::GetType(nsAString& aType)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
aType = mContentType;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
int64_t
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase::GetLastModified(ErrorResult& aRv)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mIsFile, "Should only be called on files");
|
|
|
|
if (IsDateUnknown()) {
|
|
|
|
mLastModificationDate = PR_Now();
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
return mLastModificationDate / PR_USEC_PER_MSEC;
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 18:29:22 +00:00
|
|
|
void
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase::SetLastModified(int64_t aLastModified)
|
2015-02-07 18:29:22 +00:00
|
|
|
{
|
|
|
|
mLastModificationDate = aLastModified * PR_USEC_PER_MSEC;
|
|
|
|
}
|
|
|
|
|
2014-10-07 17:16:11 +00:00
|
|
|
int64_t
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase::GetFileId()
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-09-09 11:15:14 +00:00
|
|
|
return -1;
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase::GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
|
2014-10-08 16:15:23 +00:00
|
|
|
nsACString& aContentType, nsACString& aCharset)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2014-10-08 16:15:22 +00:00
|
|
|
MOZ_ASSERT(aContentLength);
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
ErrorResult rv;
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2015-05-19 14:36:37 +00:00
|
|
|
GetInternalStream(getter_AddRefs(stream), rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
*aContentLength = GetSize(rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
2014-10-08 16:15:22 +00:00
|
|
|
}
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
nsAutoString contentType;
|
|
|
|
GetType(contentType);
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2016-07-11 18:27:36 +00:00
|
|
|
if (contentType.IsEmpty()) {
|
|
|
|
aContentType.SetIsVoid(true);
|
|
|
|
} else {
|
|
|
|
CopyUTF16toUTF8(contentType, aContentType);
|
|
|
|
}
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
aCharset.Truncate();
|
|
|
|
|
|
|
|
stream.forget(aBody);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase::GetMutable(bool* aMutable) const
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
*aMutable = !mImmutable;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplBase::SetMutable(bool aMutable)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(!mImmutable || !aMutable);
|
|
|
|
|
|
|
|
if (!mImmutable && !aMutable) {
|
|
|
|
// Force the content type and size to be cached
|
2014-10-08 16:15:22 +00:00
|
|
|
nsAutoString dummyString;
|
|
|
|
GetType(dummyString);
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
ErrorResult error;
|
|
|
|
GetSize(error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 13:18:51 +00:00
|
|
|
return error.StealNSResult();
|
2014-10-08 16:15:22 +00:00
|
|
|
}
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mImmutable = !aMutable;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-05-21 01:49:50 +00:00
|
|
|
/* static */ uint64_t
|
|
|
|
BlobImplBase::NextSerialNumber()
|
|
|
|
{
|
|
|
|
static Atomic<uint64_t> nextSerialNumber;
|
|
|
|
return nextSerialNumber++;
|
|
|
|
}
|
|
|
|
|
2014-10-07 17:16:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 12:11:03 +00:00
|
|
|
// BlobImplFile implementation
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
BlobImplFile::CreateSlice(uint64_t aStart, uint64_t aLength,
|
2014-10-08 16:15:23 +00:00
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl> impl =
|
2015-05-12 12:11:03 +00:00
|
|
|
new BlobImplFile(this, aStart, aLength, aContentType);
|
2014-10-07 17:16:11 +00:00
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
void
|
2015-06-03 00:11:16 +00:00
|
|
|
BlobImplFile::GetMozFullPathInternal(nsAString& aFilename, ErrorResult& aRv) const
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mIsFile, "Should only be called on files");
|
2014-10-08 16:15:22 +00:00
|
|
|
aRv = mFile->GetPath(aFilename);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
uint64_t
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplFile::GetSize(ErrorResult& aRv)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-12-04 21:15:46 +00:00
|
|
|
if (BlobImplBase::IsSizeUnknown()) {
|
2014-10-07 17:16:11 +00:00
|
|
|
NS_ASSERTION(mWholeFile,
|
|
|
|
"Should only use lazy size when using the whole file");
|
|
|
|
int64_t fileSize;
|
2014-10-08 16:15:22 +00:00
|
|
|
aRv = mFile->GetFileSize(&fileSize);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
if (fileSize < 0) {
|
2014-10-08 16:15:22 +00:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return 0;
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mLength = fileSize;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
return mLength;
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:32:58 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-05-03 07:09:47 +00:00
|
|
|
class GetTypeRunnable final : public WorkerMainThreadRunnable
|
2016-03-31 07:32:58 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
GetTypeRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
BlobImpl* aBlobImpl)
|
2016-05-03 07:09:47 +00:00
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("BlobImplFile :: GetType"))
|
2016-03-31 07:32:58 +00:00
|
|
|
, mBlobImpl(aBlobImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBlobImpl);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2016-05-03 07:09:47 +00:00
|
|
|
bool
|
|
|
|
MainThreadRun() override
|
2016-03-31 07:32:58 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsAutoString type;
|
|
|
|
mBlobImpl->GetType(type);
|
2016-05-03 07:09:47 +00:00
|
|
|
return true;
|
2016-03-31 07:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~GetTypeRunnable()
|
|
|
|
{}
|
|
|
|
|
|
|
|
RefPtr<BlobImpl> mBlobImpl;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
void
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplFile::GetType(nsAString& aType)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2016-03-31 07:32:58 +00:00
|
|
|
aType.Truncate();
|
|
|
|
|
2014-10-07 17:16:11 +00:00
|
|
|
if (mContentType.IsVoid()) {
|
|
|
|
NS_ASSERTION(mWholeFile,
|
|
|
|
"Should only use lazy ContentType when using the whole file");
|
2016-03-31 07:32:58 +00:00
|
|
|
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
if (!workerPrivate) {
|
|
|
|
// I have no idea in which thread this method is called. We cannot
|
|
|
|
// return any valid value.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<GetTypeRunnable> runnable =
|
2016-05-03 07:09:47 +00:00
|
|
|
new GetTypeRunnable(workerPrivate, this);
|
2016-03-31 07:32:58 +00:00
|
|
|
|
2016-05-03 07:09:47 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
runnable->Dispatch(rv);
|
2016-07-22 14:50:10 +00:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
}
|
2016-03-31 07:32:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-07 17:16:11 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeService =
|
|
|
|
do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
|
2014-10-08 16:15:22 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
nsAutoCString mimeType;
|
|
|
|
rv = mimeService->GetTypeFromFile(mFile, mimeType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mimeType.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendUTF8toUTF16(mimeType, mContentType);
|
|
|
|
mContentType.SetIsVoid(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
aType = mContentType;
|
|
|
|
}
|
|
|
|
|
2014-10-08 16:15:22 +00:00
|
|
|
int64_t
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplFile::GetLastModified(ErrorResult& aRv)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mIsFile, "Should only be called on files");
|
2015-12-04 21:15:46 +00:00
|
|
|
if (BlobImplBase::IsDateUnknown()) {
|
2014-10-07 17:16:11 +00:00
|
|
|
PRTime msecs;
|
2014-10-08 16:15:22 +00:00
|
|
|
aRv = mFile->GetLastModifiedTime(&msecs);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-07 17:16:11 +00:00
|
|
|
mLastModificationDate = msecs;
|
|
|
|
}
|
2014-10-08 16:15:22 +00:00
|
|
|
|
|
|
|
return mLastModificationDate;
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 18:29:22 +00:00
|
|
|
void
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplFile::SetLastModified(int64_t aLastModified)
|
2015-02-07 18:29:22 +00:00
|
|
|
{
|
|
|
|
MOZ_CRASH("SetLastModified of a real file is not allowed!");
|
|
|
|
}
|
|
|
|
|
2014-10-07 17:16:11 +00:00
|
|
|
const uint32_t sFileStreamFlags =
|
|
|
|
nsIFileInputStream::CLOSE_ON_EOF |
|
|
|
|
nsIFileInputStream::REOPEN_ON_REWIND |
|
|
|
|
nsIFileInputStream::DEFER_OPEN |
|
|
|
|
nsIFileInputStream::SHARE_DELETE;
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
void
|
|
|
|
BlobImplFile::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-05-19 14:36:37 +00:00
|
|
|
if (mWholeFile) {
|
|
|
|
aRv = NS_NewLocalFileInputStream(aStream, mFile, -1, -1, sFileStreamFlags);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = NS_NewPartialLocalFileInputStream(aStream, mFile, mStart, mLength,
|
|
|
|
-1, -1, sFileStreamFlags);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 11:41:38 +00:00
|
|
|
bool
|
|
|
|
BlobImplFile::IsDirectory() const
|
|
|
|
{
|
|
|
|
bool isDirectory = false;
|
|
|
|
if (mFile) {
|
|
|
|
mFile->IsDirectory(&isDirectory);
|
|
|
|
}
|
|
|
|
return isDirectory;
|
|
|
|
}
|
|
|
|
|
2016-01-08 08:35:30 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2016-01-20 17:25:03 +00:00
|
|
|
// EmptyBlobImpl implementation
|
2016-01-08 08:35:30 +00:00
|
|
|
|
2016-01-20 17:25:03 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(EmptyBlobImpl, BlobImpl)
|
2016-01-08 08:35:30 +00:00
|
|
|
|
|
|
|
already_AddRefed<BlobImpl>
|
2016-01-20 17:25:03 +00:00
|
|
|
EmptyBlobImpl::CreateSlice(uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
2016-01-08 08:35:30 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aStart && !aLength);
|
2016-01-20 17:25:03 +00:00
|
|
|
RefPtr<BlobImpl> impl = new EmptyBlobImpl(aContentType);
|
2016-04-11 18:31:53 +00:00
|
|
|
|
|
|
|
DebugOnly<bool> isMutable;
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(impl->GetMutable(&isMutable)));
|
|
|
|
MOZ_ASSERT(!isMutable);
|
|
|
|
|
2016-01-08 08:35:30 +00:00
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-20 17:25:03 +00:00
|
|
|
EmptyBlobImpl::GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv)
|
2016-01-08 08:35:30 +00:00
|
|
|
{
|
2016-04-11 18:31:53 +00:00
|
|
|
if (NS_WARN_IF(!aStream)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-08 08:35:30 +00:00
|
|
|
nsresult rv = NS_NewCStringInputStream(aStream, EmptyCString());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 05:53:54 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BlobImplString implementation
|
|
|
|
|
2016-11-02 09:35:57 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(BlobImplString, BlobImpl, nsIMemoryReporter)
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<BlobImplString>
|
|
|
|
BlobImplString::Create(const nsACString& aData, const nsAString& aContentType)
|
|
|
|
{
|
|
|
|
RefPtr<BlobImplString> blobImpl = new BlobImplString(aData, aContentType);
|
|
|
|
RegisterWeakMemoryReporter(blobImpl);
|
|
|
|
return blobImpl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
BlobImplString::BlobImplString(const nsACString& aData,
|
|
|
|
const nsAString& aContentType)
|
|
|
|
: BlobImplBase(aContentType, aData.Length())
|
|
|
|
, mData(aData)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BlobImplString::~BlobImplString()
|
|
|
|
{
|
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
}
|
2016-10-25 05:53:54 +00:00
|
|
|
|
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
BlobImplString::CreateSlice(uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
RefPtr<BlobImpl> impl =
|
|
|
|
new BlobImplString(Substring(mData, aStart, aLength),
|
|
|
|
aContentType);
|
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BlobImplString::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
aRv = NS_NewCStringInputStream(aStream, mData);
|
|
|
|
}
|
|
|
|
|
2016-11-02 09:35:57 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BlobImplString::CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, bool aAnonymize)
|
|
|
|
{
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"explicit/dom/memory-file-data/string", KIND_HEAP, UNITS_BYTES,
|
|
|
|
mData.SizeOfExcludingThisIfUnshared(MallocSizeOf),
|
|
|
|
"Memory used to back a File/Blob based on a string.");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-07 17:16:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 12:11:03 +00:00
|
|
|
// BlobImplMemory implementation
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(BlobImplMemory, BlobImpl)
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
BlobImplMemory::CreateSlice(uint64_t aStart, uint64_t aLength,
|
2014-10-08 16:15:23 +00:00
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl> impl =
|
2015-05-12 12:11:03 +00:00
|
|
|
new BlobImplMemory(this, aStart, aLength, aContentType);
|
2014-10-07 17:16:11 +00:00
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
void
|
|
|
|
BlobImplMemory::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-05-19 14:36:37 +00:00
|
|
|
if (mLength > INT32_MAX) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
aRv = DataOwnerAdapter::Create(mDataOwner, mStart, mLength, aStream);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ StaticMutex
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplMemory::DataOwner::sDataOwnerMutex;
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
/* static */ StaticAutoPtr<LinkedList<BlobImplMemory::DataOwner>>
|
|
|
|
BlobImplMemory::DataOwner::sDataOwners;
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
/* static */ bool
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplMemory::DataOwner::sMemoryReporterRegistered = false;
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(MemoryFileDataOwnerMallocSizeOf)
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
class BlobImplMemoryDataOwnerMemoryReporter final
|
2014-10-07 17:16:11 +00:00
|
|
|
: public nsIMemoryReporter
|
|
|
|
{
|
2015-05-12 12:11:03 +00:00
|
|
|
~BlobImplMemoryDataOwnerMemoryReporter() {}
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
2016-08-24 05:23:45 +00:00
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, bool aAnonymize) override
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2015-05-12 12:11:03 +00:00
|
|
|
typedef BlobImplMemory::DataOwner DataOwner;
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
StaticMutexAutoLock lock(DataOwner::sDataOwnerMutex);
|
|
|
|
|
|
|
|
if (!DataOwner::sDataOwners) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t LARGE_OBJECT_MIN_SIZE = 8 * 1024;
|
|
|
|
size_t smallObjectsTotal = 0;
|
|
|
|
|
|
|
|
for (DataOwner *owner = DataOwner::sDataOwners->getFirst();
|
|
|
|
owner; owner = owner->getNext()) {
|
|
|
|
|
2014-10-08 16:15:23 +00:00
|
|
|
size_t size = MemoryFileDataOwnerMallocSizeOf(owner->mData);
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
if (size < LARGE_OBJECT_MIN_SIZE) {
|
|
|
|
smallObjectsTotal += size;
|
|
|
|
} else {
|
|
|
|
SHA1Sum sha1;
|
|
|
|
sha1.update(owner->mData, owner->mLength);
|
|
|
|
uint8_t digest[SHA1Sum::kHashSize]; // SHA1 digests are 20 bytes long.
|
|
|
|
sha1.finish(digest);
|
|
|
|
|
|
|
|
nsAutoCString digestString;
|
|
|
|
for (size_t i = 0; i < sizeof(digest); i++) {
|
|
|
|
digestString.AppendPrintf("%02x", digest[i]);
|
|
|
|
}
|
|
|
|
|
2016-08-24 05:23:45 +00:00
|
|
|
aHandleReport->Callback(
|
2014-10-07 17:16:11 +00:00
|
|
|
/* process */ NS_LITERAL_CSTRING(""),
|
|
|
|
nsPrintfCString(
|
|
|
|
"explicit/dom/memory-file-data/large/file(length=%llu, sha1=%s)",
|
|
|
|
owner->mLength, aAnonymize ? "<anonymized>" : digestString.get()),
|
|
|
|
KIND_HEAP, UNITS_BYTES, size,
|
|
|
|
nsPrintfCString(
|
|
|
|
"Memory used to back a memory file of length %llu bytes. The file "
|
|
|
|
"has a sha1 of %s.\n\n"
|
|
|
|
"Note that the allocator may round up a memory file's length -- "
|
|
|
|
"that is, an N-byte memory file may take up more than N bytes of "
|
|
|
|
"memory.",
|
|
|
|
owner->mLength, digestString.get()),
|
2016-08-24 05:23:45 +00:00
|
|
|
aData);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smallObjectsTotal > 0) {
|
2016-08-24 05:23:45 +00:00
|
|
|
aHandleReport->Callback(
|
2014-10-07 17:16:11 +00:00
|
|
|
/* process */ NS_LITERAL_CSTRING(""),
|
|
|
|
NS_LITERAL_CSTRING("explicit/dom/memory-file-data/small"),
|
|
|
|
KIND_HEAP, UNITS_BYTES, smallObjectsTotal,
|
|
|
|
nsPrintfCString(
|
2016-09-05 04:14:00 +00:00
|
|
|
"Memory used to back small memory files (i.e. those taking up less "
|
|
|
|
"than %zu bytes of memory each).\n\n"
|
2014-10-07 17:16:11 +00:00
|
|
|
"Note that the allocator may round up a memory file's length -- "
|
|
|
|
"that is, an N-byte memory file may take up more than N bytes of "
|
2016-09-05 04:14:00 +00:00
|
|
|
"memory.", LARGE_OBJECT_MIN_SIZE),
|
2016-08-24 05:23:45 +00:00
|
|
|
aData);
|
2014-10-07 17:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
NS_IMPL_ISUPPORTS(BlobImplMemoryDataOwnerMemoryReporter, nsIMemoryReporter)
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
/* static */ void
|
2015-05-12 12:11:03 +00:00
|
|
|
BlobImplMemory::DataOwner::EnsureMemoryReporterRegistered()
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
sDataOwnerMutex.AssertCurrentThreadOwns();
|
|
|
|
if (sMemoryReporterRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
RegisterStrongMemoryReporter(new BlobImplMemoryDataOwnerMemoryReporter());
|
2014-10-07 17:16:11 +00:00
|
|
|
|
|
|
|
sMemoryReporterRegistered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 12:11:03 +00:00
|
|
|
// BlobImplTemporaryBlob implementation
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(BlobImplTemporaryBlob, BlobImpl)
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-05-12 12:11:03 +00:00
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
BlobImplTemporaryBlob::CreateSlice(uint64_t aStart, uint64_t aLength,
|
2015-05-12 12:09:51 +00:00
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
2014-10-08 16:15:22 +00:00
|
|
|
if (aStart + aLength > mLength) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2014-10-07 17:16:11 +00:00
|
|
|
return nullptr;
|
2014-10-08 16:15:22 +00:00
|
|
|
}
|
2014-10-07 17:16:11 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl> impl =
|
2015-05-12 12:11:03 +00:00
|
|
|
new BlobImplTemporaryBlob(this, aStart + mStartPos,
|
2015-05-12 12:09:51 +00:00
|
|
|
aLength, aContentType);
|
2014-10-07 17:16:11 +00:00
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:36:37 +00:00
|
|
|
void
|
|
|
|
BlobImplTemporaryBlob::GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv)
|
2014-10-07 17:16:11 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> stream =
|
|
|
|
new nsTemporaryFileInputStream(mFileDescOwner, mStartPos, mStartPos + mLength);
|
|
|
|
stream.forget(aStream);
|
|
|
|
}
|
|
|
|
|
2016-09-21 10:27:26 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BlobImplStream implementation
|
|
|
|
|
2016-11-04 06:52:04 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(BlobImplStream, BlobImpl, nsIMemoryReporter)
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<BlobImplStream>
|
|
|
|
BlobImplStream::Create(nsIInputStream* aInputStream,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
uint64_t aLength)
|
|
|
|
{
|
|
|
|
RefPtr<BlobImplStream> blobImplStream =
|
|
|
|
new BlobImplStream(aInputStream, aContentType, aLength);
|
|
|
|
blobImplStream->MaybeRegisterMemoryReporter();
|
|
|
|
return blobImplStream.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<BlobImplStream>
|
|
|
|
BlobImplStream::Create(nsIInputStream* aInputStream,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate,
|
|
|
|
uint64_t aLength)
|
|
|
|
{
|
|
|
|
RefPtr<BlobImplStream> blobImplStream =
|
|
|
|
new BlobImplStream(aInputStream, aName, aContentType, aLastModifiedDate,
|
|
|
|
aLength);
|
|
|
|
blobImplStream->MaybeRegisterMemoryReporter();
|
|
|
|
return blobImplStream.forget();
|
|
|
|
}
|
2016-09-21 10:27:26 +00:00
|
|
|
|
|
|
|
BlobImplStream::BlobImplStream(nsIInputStream* aInputStream,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
uint64_t aLength)
|
|
|
|
: BlobImplBase(aContentType, aLength)
|
|
|
|
, mInputStream(aInputStream)
|
|
|
|
{
|
|
|
|
mImmutable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlobImplStream::BlobImplStream(BlobImplStream* aOther,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
uint64_t aStart, uint64_t aLength)
|
|
|
|
: BlobImplBase(aContentType, aOther->mStart + aStart, aLength)
|
|
|
|
, mInputStream(new SlicedInputStream(aOther->mInputStream, aStart, aLength))
|
|
|
|
{
|
|
|
|
mImmutable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlobImplStream::BlobImplStream(nsIInputStream* aInputStream,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate,
|
|
|
|
uint64_t aLength)
|
|
|
|
: BlobImplBase(aName, aContentType, aLength, aLastModifiedDate)
|
|
|
|
, mInputStream(aInputStream)
|
|
|
|
{
|
|
|
|
mImmutable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BlobImplStream::~BlobImplStream()
|
2016-11-04 06:52:04 +00:00
|
|
|
{
|
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
}
|
2016-09-21 10:27:26 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
BlobImplStream::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> clonedStream;
|
|
|
|
nsCOMPtr<nsIInputStream> replacementStream;
|
|
|
|
|
|
|
|
aRv = NS_CloneInputStream(mInputStream, getter_AddRefs(clonedStream),
|
|
|
|
getter_AddRefs(replacementStream));
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (replacementStream) {
|
|
|
|
mInputStream = replacementStream.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
clonedStream.forget(aStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
BlobImplStream::CreateSlice(uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!aLength) {
|
|
|
|
RefPtr<BlobImpl> impl = new EmptyBlobImpl(aContentType);
|
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<BlobImpl> impl =
|
|
|
|
new BlobImplStream(this, aContentType, aStart, aLength);
|
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
2016-11-04 06:52:04 +00:00
|
|
|
void
|
|
|
|
BlobImplStream::MaybeRegisterMemoryReporter()
|
|
|
|
{
|
|
|
|
// We report only stringInputStream.
|
|
|
|
nsCOMPtr<nsIStringInputStream> stringInputStream =
|
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (!stringInputStream) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegisterWeakMemoryReporter(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BlobImplStream::CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, bool aAnonymize)
|
|
|
|
{
|
2016-11-07 07:45:42 +00:00
|
|
|
nsCOMPtr<nsIStringInputStream> stringInputStream =
|
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (!stringInputStream) {
|
|
|
|
return NS_OK;
|
2016-11-04 06:52:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"explicit/dom/memory-file-data/stream", KIND_HEAP, UNITS_BYTES,
|
2016-11-07 07:45:42 +00:00
|
|
|
stringInputStream->SizeOfIncludingThis(MallocSizeOf),
|
2016-11-04 06:52:04 +00:00
|
|
|
"Memory used to back a File/Blob based on an input stream.");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|