2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-10-07 21:16:11 +04: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 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2016-12-01 17:12:42 +03:00
|
|
|
#include "ipc/nsIRemoteBlob.h"
|
2015-05-12 15:11:03 +03:00
|
|
|
#include "MultipartBlobImpl.h"
|
2014-10-07 21:16:11 +04: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 "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 20:15:22 +04:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
|
|
|
#include "mozilla/dom/DOMError.h"
|
|
|
|
#include "mozilla/dom/FileBinding.h"
|
2017-02-06 13:07:54 +03:00
|
|
|
#include "mozilla/dom/FileSystemUtils.h"
|
2017-02-08 12:18:32 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-10-08 20:15:22 +04:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2016-03-31 10:32:58 +03:00
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
2014-10-07 21:16:11 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2016-09-21 13:27:26 +03:00
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "SlicedInputStream.h"
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-03-31 10:32:58 +03:00
|
|
|
using namespace workers;
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
// XXXkhuey the input stream that we pass out of a File
|
|
|
|
// can outlive the actual File object. Thus, we must
|
2014-10-07 21:16:11 +04: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 19:28:04 +03:00
|
|
|
class DataOwnerAdapter final : public nsIInputStream,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsISeekableStream,
|
|
|
|
public nsIIPCSerializableInputStream
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-05-12 15:11:03 +03:00
|
|
|
typedef BlobImplMemory::DataOwner DataOwner;
|
2014-10-07 21:16:11 +04: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))
|
|
|
|
{
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mSeekableStream, "Somebody gave us the wrong stream!");
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataOwner> mDataOwner;
|
2014-10-07 21:16:11 +04: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;
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(aDataOwner, "Uh ...");
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
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 15:09:51 +03:00
|
|
|
// mozilla::dom::Blob implementation
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Blob)
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Blob)
|
2014-10-08 20:15:22 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2014-10-07 21:16:11 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Blob)
|
2014-10-08 20:15:22 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent)
|
2014-10-07 21:16:11 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Blob)
|
2014-10-08 20:15:22 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Blob)
|
2014-10-07 21:16:11 +04:00
|
|
|
// This class should not receive any nsIRemoteBlob QI!
|
|
|
|
MOZ_ASSERT(!aIID.Equals(NS_GET_IID(nsIRemoteBlob)));
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2015-05-12 15:09:51 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMBlob)
|
2014-10-07 21:16:11 +04: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 15:09:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Blob)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Blob)
|
2015-05-11 20:50:50 +03:00
|
|
|
|
2016-11-28 23:58:15 +03:00
|
|
|
// A utility function that enforces the spec constraints on the type of a blob:
|
|
|
|
// no codepoints outside the ASCII range (otherwise type becomes empty) and
|
|
|
|
// lowercase ASCII only. We can't just use our existing nsContentUtils
|
|
|
|
// ASCII-related helpers because we need the "outside ASCII range" check, and we
|
|
|
|
// can't use NS_IsAscii because its definition of "ASCII" (chars all <= 0x7E)
|
|
|
|
// differs from the file API definition (which excludes control chars).
|
|
|
|
static void
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
/* static */ Blob*
|
2015-05-12 15:11:03 +03:00
|
|
|
Blob::Create(nsISupports* aParent, BlobImpl* aImpl)
|
2015-05-11 20:50:50 +03:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
MOZ_ASSERT(aImpl);
|
2015-05-11 20:50:50 +03:00
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
return aImpl->IsFile() ? new File(aParent, aImpl)
|
|
|
|
: new Blob(aParent, aImpl);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2016-10-25 08:53:54 +03:00
|
|
|
/* static */ already_AddRefed<Blob>
|
|
|
|
Blob::CreateStringBlob(nsISupports* aParent, const nsACString& aData,
|
|
|
|
const nsAString& aContentType)
|
|
|
|
{
|
2016-11-02 12:35:57 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl = BlobImplString::Create(aData, aContentType);
|
|
|
|
RefPtr<Blob> blob = Blob::Create(aParent, blobImpl);
|
2016-10-25 08:53:54 +03:00
|
|
|
MOZ_ASSERT(!blob->mImpl->IsFile());
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
/* static */ already_AddRefed<Blob>
|
|
|
|
Blob::CreateMemoryBlob(nsISupports* aParent, void* aMemoryBuffer,
|
|
|
|
uint64_t aLength, const nsAString& aContentType)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Blob> blob = Blob::Create(aParent,
|
2015-05-12 15:11:03 +03:00
|
|
|
new BlobImplMemory(aMemoryBuffer, aLength, aContentType));
|
2015-05-12 15:09:51 +03: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 08:24:48 +03:00
|
|
|
RefPtr<Blob> blob = Blob::Create(aParent,
|
2015-05-12 15:11:03 +03:00
|
|
|
new BlobImplTemporaryBlob(aFD, aStartPos, aLength, aContentType));
|
2015-05-12 15:09:51 +03:00
|
|
|
MOZ_ASSERT(!blob->mImpl->IsFile());
|
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
Blob::Blob(nsISupports* aParent, BlobImpl* aImpl)
|
2015-05-12 15:09:51 +03:00
|
|
|
: mImpl(aImpl)
|
|
|
|
, mParent(aParent)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mImpl);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(aParent);
|
2015-05-12 15:09:51 +03:00
|
|
|
if (win) {
|
|
|
|
MOZ_ASSERT(win->IsInnerWindow());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Blob::IsFile() const
|
|
|
|
{
|
|
|
|
return mImpl->IsFile();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
const nsTArray<RefPtr<BlobImpl>>*
|
2015-05-12 15:09:51 +03:00
|
|
|
Blob::GetSubBlobImpls() const
|
|
|
|
{
|
|
|
|
return mImpl->GetSubBlobImpls();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<File>
|
|
|
|
Blob::ToFile()
|
|
|
|
{
|
|
|
|
if (!mImpl->IsFile()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file;
|
2015-05-12 15:09:51 +03:00
|
|
|
if (HasFileInterface()) {
|
|
|
|
file = static_cast<File*>(this);
|
|
|
|
} else {
|
|
|
|
file = new File(mParent, mImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<File>
|
2015-12-05 00:15:46 +03:00
|
|
|
Blob::ToFile(const nsAString& aName, ErrorResult& aRv) const
|
2015-05-12 15:09:51 +03:00
|
|
|
{
|
2016-08-09 17:21:26 +03:00
|
|
|
AutoTArray<RefPtr<BlobImpl>, 1> blobImpls({mImpl});
|
2015-05-12 15:09:51 +03:00
|
|
|
|
|
|
|
nsAutoString contentType;
|
|
|
|
mImpl->GetType(contentType);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MultipartBlobImpl> impl =
|
2016-09-21 04:29:28 +03:00
|
|
|
MultipartBlobImpl::Create(Move(blobImpls), aName, contentType, aRv);
|
2015-12-05 00:15:46 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-12 15:09:51 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file = new File(mParent, impl);
|
2015-05-11 18:43:59 +03:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
already_AddRefed<Blob>
|
|
|
|
Blob::CreateSlice(uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> impl = mImpl->CreateSlice(aStart, aLength,
|
2016-09-21 13:27:26 +03:00
|
|
|
aContentType, aRv);
|
2015-05-12 15:09:51 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Blob> blob = Blob::Create(mParent, impl);
|
2015-05-12 15:09:51 +03:00
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
|
|
|
Blob::GetSize(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
return mImpl->GetSize(aRv);
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
void
|
2015-05-12 15:09:51 +03: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 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> impl =
|
2015-05-12 15:09:51 +03:00
|
|
|
mImpl->Slice(aStart, aEnd, aContentType, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Blob> blob = Blob::Create(mParent, impl);
|
2015-05-12 15:09:51 +03: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 13:33:09 +03:00
|
|
|
Blob::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Optional<Sequence<BlobPart>>& aData,
|
|
|
|
const BlobPropertyBag& aBag,
|
|
|
|
ErrorResult& aRv)
|
2015-05-12 15:09:51 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl();
|
2015-05-12 15:09:51 +03:00
|
|
|
|
2016-06-01 13:33:09 +03:00
|
|
|
if (aData.WasPassed()) {
|
2016-11-28 23:58:15 +03:00
|
|
|
nsAutoString type(aBag.mType);
|
|
|
|
MakeValidBlobType(type);
|
|
|
|
impl->InitializeBlob(aGlobal.Context(), aData.Value(), type,
|
2016-06-01 13:33:09 +03:00
|
|
|
aBag.mEndings == EndingTypes::Native, aRv);
|
|
|
|
} else {
|
|
|
|
impl->InitializeBlob(aRv);
|
2015-12-05 00:15:46 +03:00
|
|
|
}
|
|
|
|
|
2016-06-01 13:33:09 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-05-12 15:09:51 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-06-01 13:33:09 +03:00
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
MOZ_ASSERT(!impl->IsFile());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Blob> blob = Blob::Create(aGlobal.GetAsSupports(), impl);
|
2015-05-12 15:09:51 +03:00
|
|
|
return blob.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
int64_t
|
2015-05-12 15:09:51 +03:00
|
|
|
Blob::GetFileId()
|
|
|
|
{
|
|
|
|
return mImpl->GetFileId();
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
bool
|
|
|
|
Blob::IsMemoryFile() const
|
2015-05-12 15:09:51 +03:00
|
|
|
{
|
|
|
|
return mImpl->IsMemoryFile();
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
void
|
|
|
|
Blob::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
|
2015-05-12 15:09:51 +03:00
|
|
|
{
|
2015-05-19 17:36:37 +03:00
|
|
|
mImpl->GetInternalStream(aStream, aRv);
|
2015-05-12 15:09:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// mozilla::dom::File implementation
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
File::File(nsISupports* aParent, BlobImpl* aImpl)
|
2015-05-12 15:09:51 +03:00
|
|
|
: Blob(aParent, aImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl->IsFile());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ File*
|
2015-05-12 15:11:03 +03:00
|
|
|
File::Create(nsISupports* aParent, BlobImpl* aImpl)
|
2015-05-12 15:09:51 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
MOZ_ASSERT(aImpl->IsFile());
|
|
|
|
|
|
|
|
return new File(aParent, aImpl);
|
|
|
|
}
|
|
|
|
|
2015-05-11 18:43:59 +03:00
|
|
|
/* static */ already_AddRefed<File>
|
2015-05-12 15:09:51 +03:00
|
|
|
File::Create(nsISupports* aParent, const nsAString& aName,
|
|
|
|
const nsAString& aContentType, uint64_t aLength,
|
2016-03-20 13:56:10 +03:00
|
|
|
int64_t aLastModifiedDate)
|
2015-05-11 18:43:59 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file = new File(aParent,
|
2016-03-20 13:56:10 +03:00
|
|
|
new BlobImplBase(aName, aContentType, aLength, aLastModifiedDate));
|
2015-05-11 18:43:59 +03:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 00:54:02 +03:00
|
|
|
/* static */ already_AddRefed<File>
|
2015-05-12 15:09:51 +03:00
|
|
|
File::CreateMemoryFile(nsISupports* aParent, void* aMemoryBuffer,
|
|
|
|
uint64_t aLength, const nsAString& aName,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
int64_t aLastModifiedDate)
|
2015-05-12 00:54:02 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file = new File(aParent,
|
2015-05-12 15:11:03 +03:00
|
|
|
new BlobImplMemory(aMemoryBuffer, aLength, aName,
|
2015-05-12 15:09:51 +03:00
|
|
|
aContentType, aLastModifiedDate));
|
2015-05-12 00:54:02 +03:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
/* static */ already_AddRefed<File>
|
2017-01-24 14:11:07 +03:00
|
|
|
File::CreateFromFile(nsISupports* aParent, nsIFile* aFile)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2017-01-24 14:11:07 +03:00
|
|
|
RefPtr<File> file = new File(aParent, new BlobImplFile(aFile));
|
2014-10-07 21:16:11 +04:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
/* static */ already_AddRefed<File>
|
|
|
|
File::CreateFromFile(nsISupports* aParent, nsIFile* aFile,
|
|
|
|
const nsAString& aName, const nsAString& aContentType)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file = new File(aParent,
|
2015-05-12 15:11:03 +03:00
|
|
|
new BlobImplFile(aFile, aName, aContentType));
|
2014-10-07 21:16:11 +04:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
JSObject*
|
|
|
|
File::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
return FileBinding::Wrap(aCx, this, aGivenProto);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2015-05-18 16:51:54 +03:00
|
|
|
void
|
2016-04-01 13:50:16 +03:00
|
|
|
File::GetName(nsAString& aFileName) const
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
mImpl->GetName(aFileName);
|
2015-05-11 18:43:59 +03:00
|
|
|
}
|
|
|
|
|
2015-05-18 16:51:54 +03:00
|
|
|
void
|
2017-02-06 13:07:54 +03:00
|
|
|
File::GetRelativePath(nsAString& aPath) const
|
2015-05-11 18:43:59 +03:00
|
|
|
{
|
2017-02-06 13:07:54 +03:00
|
|
|
aPath.Truncate();
|
2016-05-13 14:11:38 +03:00
|
|
|
|
2017-02-06 13:07:54 +03:00
|
|
|
nsAutoString path;
|
|
|
|
mImpl->GetDOMPath(path);
|
|
|
|
|
|
|
|
// WebkitRelativePath doesn't start with '/'
|
|
|
|
if (!path.IsEmpty()) {
|
|
|
|
MOZ_ASSERT(path[0] == FILESYSTEM_DOM_PATH_SEPARATOR_CHAR);
|
|
|
|
aPath.Assign(Substring(path, 1));
|
|
|
|
}
|
2015-05-12 00:54:02 +03:00
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
Date
|
|
|
|
File::GetLastModifiedDate(ErrorResult& aRv)
|
2015-05-12 00:54:02 +03:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
int64_t value = GetLastModified(aRv);
|
2014-10-08 20:15:22 +04:00
|
|
|
if (aRv.Failed()) {
|
2015-05-12 15:09:51 +03:00
|
|
|
return Date();
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-02 05:12:52 +03:00
|
|
|
return Date(JS::TimeClip(value));
|
2015-05-12 00:54:02 +03:00
|
|
|
}
|
|
|
|
|
2015-05-12 15:09:51 +03:00
|
|
|
int64_t
|
|
|
|
File::GetLastModified(ErrorResult& aRv)
|
2015-05-12 00:54:02 +03:00
|
|
|
{
|
2015-05-12 15:09:51 +03:00
|
|
|
return mImpl->GetLastModified(aRv);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
void
|
2017-02-01 23:43:37 +03:00
|
|
|
File::GetMozFullPath(nsAString& aFilename, SystemCallerGuarantee aGuarantee,
|
|
|
|
ErrorResult& aRv) const
|
2014-10-08 20:15:22 +04:00
|
|
|
{
|
2017-02-01 23:43:37 +03:00
|
|
|
mImpl->GetMozFullPath(aFilename, aGuarantee, aRv);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2015-05-18 16:51:54 +03:00
|
|
|
void
|
2015-06-03 03:11:16 +03:00
|
|
|
File::GetMozFullPathInternal(nsAString& aFileName, ErrorResult& aRv) const
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-05-18 16:51:54 +03:00
|
|
|
mImpl->GetMozFullPathInternal(aFileName, aRv);
|
2014-10-07 21:16:11 +04: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 20:15:23 +04:00
|
|
|
/* static */ already_AddRefed<File>
|
2016-06-01 13:33:09 +03:00
|
|
|
File::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const Sequence<BlobPart>& aData,
|
|
|
|
const nsAString& aName,
|
|
|
|
const FilePropertyBag& aBag,
|
|
|
|
ErrorResult& aRv)
|
2014-10-08 20:15:22 +04:00
|
|
|
{
|
2016-12-01 17:14:45 +03:00
|
|
|
// Normalizing the filename
|
|
|
|
nsString name(aName);
|
|
|
|
name.ReplaceChar('/', ':');
|
|
|
|
|
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(name);
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2016-11-28 23:58:15 +03:00
|
|
|
nsAutoString type(aBag.mType);
|
|
|
|
MakeValidBlobType(type);
|
|
|
|
impl->InitializeBlob(aGlobal.Context(), aData, type, false, aRv);
|
2014-10-08 20:15:22 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
2014-10-08 20:15:22 +04:00
|
|
|
MOZ_ASSERT(impl->IsFile());
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-02-07 21:29:22 +03:00
|
|
|
if (aBag.mLastModified.WasPassed()) {
|
|
|
|
impl->SetLastModified(aBag.mLastModified.Value());
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<File> file = new File(aGlobal.GetAsSupports(), impl);
|
2014-10-08 20:15:22 +04:00
|
|
|
return file.forget();
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:18:32 +03:00
|
|
|
/* static */ already_AddRefed<Promise>
|
2016-11-11 20:56:44 +03:00
|
|
|
File::CreateFromNsIFile(const GlobalObject& aGlobal,
|
|
|
|
nsIFile* aData,
|
|
|
|
const ChromeFilePropertyBag& aBag,
|
2017-02-01 23:43:37 +03:00
|
|
|
SystemCallerGuarantee aGuarantee,
|
2016-11-11 20:56:44 +03:00
|
|
|
ErrorResult& aRv)
|
2014-10-08 20:15:22 +04:00
|
|
|
{
|
2015-01-30 21:54:43 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2017-02-01 21:07:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
|
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(EmptyString());
|
2017-02-01 23:43:37 +03:00
|
|
|
impl->InitializeChromeFile(window, aData, aBag, true, aGuarantee, aRv);
|
2017-02-01 21:07:36 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(impl->IsFile());
|
|
|
|
|
|
|
|
if (aBag.mLastModified.WasPassed()) {
|
|
|
|
impl->SetLastModified(aBag.mLastModified.Value());
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:18:32 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
RefPtr<Promise> promise = Promise::Create(global, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-01 21:07:36 +03:00
|
|
|
RefPtr<File> domFile = new File(aGlobal.GetAsSupports(), impl);
|
2017-02-08 12:18:32 +03:00
|
|
|
promise->MaybeResolve(domFile);
|
|
|
|
|
|
|
|
return promise.forget();
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
|
|
|
|
2017-02-08 12:18:32 +03:00
|
|
|
/* static */ already_AddRefed<Promise>
|
2016-11-11 20:56:44 +03:00
|
|
|
File::CreateFromFileName(const GlobalObject& aGlobal,
|
2017-02-01 21:07:36 +03:00
|
|
|
const nsAString& aData,
|
2016-11-11 20:56:44 +03:00
|
|
|
const ChromeFilePropertyBag& aBag,
|
2017-02-01 23:43:37 +03:00
|
|
|
SystemCallerGuarantee aGuarantee,
|
2016-11-11 20:56:44 +03:00
|
|
|
ErrorResult& aRv)
|
2014-10-08 20:15:22 +04:00
|
|
|
{
|
2017-02-01 21:07:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
|
|
|
|
RefPtr<MultipartBlobImpl> impl = new MultipartBlobImpl(EmptyString());
|
2017-02-01 23:43:37 +03:00
|
|
|
impl->InitializeChromeFile(window, aData, aBag, aGuarantee, aRv);
|
2017-02-01 21:07:36 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(impl->IsFile());
|
|
|
|
|
|
|
|
if (aBag.mLastModified.WasPassed()) {
|
|
|
|
impl->SetLastModified(aBag.mLastModified.Value());
|
|
|
|
}
|
|
|
|
|
2017-02-08 12:18:32 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
RefPtr<Promise> promise = Promise::Create(global, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-01 21:07:36 +03:00
|
|
|
RefPtr<File> domFile = new File(aGlobal.GetAsSupports(), impl);
|
2017-02-08 12:18:32 +03:00
|
|
|
promise->MaybeResolve(domFile);
|
|
|
|
|
|
|
|
return promise.forget();
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 15:11:03 +03:00
|
|
|
// mozilla::dom::BlobImpl implementation
|
2014-10-08 20:15:22 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
BlobImpl::Slice(const Optional<int64_t>& aStart,
|
2014-10-08 20:15:23 +04:00
|
|
|
const Optional<int64_t>& aEnd,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
2014-10-08 20:15:22 +04: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);
|
|
|
|
|
2016-11-28 23:58:15 +03:00
|
|
|
nsAutoString type(aContentType);
|
|
|
|
MakeValidBlobType(type);
|
|
|
|
return CreateSlice((uint64_t)start, (uint64_t)(end - start), type, aRv);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 15:11:03 +03:00
|
|
|
// BlobImpl implementation
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
NS_IMPL_ISUPPORTS(BlobImpl, BlobImpl)
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 15:11:03 +03:00
|
|
|
// BlobImplFile implementation
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(BlobImplFile, BlobImpl)
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
void
|
2016-04-01 13:50:16 +03:00
|
|
|
BlobImplBase::GetName(nsAString& aName) const
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mIsFile, "Should only be called on files");
|
2014-10-07 21:16:11 +04:00
|
|
|
aName = mName;
|
|
|
|
}
|
|
|
|
|
2015-05-18 16:51:54 +03:00
|
|
|
void
|
2017-02-06 13:07:54 +03:00
|
|
|
BlobImplBase::GetDOMPath(nsAString& aPath) const
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mIsFile, "Should only be called on files");
|
2014-10-07 21:16:11 +04:00
|
|
|
aPath = mPath;
|
|
|
|
}
|
|
|
|
|
2016-05-13 14:11:38 +03:00
|
|
|
void
|
2017-02-06 13:07:54 +03:00
|
|
|
BlobImplBase::SetDOMPath(const nsAString& aPath)
|
2016-05-13 14:11:38 +03:00
|
|
|
{
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mIsFile, "Should only be called on files");
|
2016-05-13 14:11:38 +03:00
|
|
|
mPath = aPath;
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
void
|
2017-02-01 23:43:37 +03:00
|
|
|
BlobImplBase::GetMozFullPath(nsAString& aFileName,
|
|
|
|
SystemCallerGuarantee /* unused */,
|
|
|
|
ErrorResult& aRv) const
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mIsFile, "Should only be called on files");
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2017-02-01 23:43:37 +03:00
|
|
|
GetMozFullPathInternal(aFileName, aRv);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
void
|
2015-06-03 03:11:16 +03:00
|
|
|
BlobImplBase::GetMozFullPathInternal(nsAString& aFileName, ErrorResult& aRv) const
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
|
|
|
if (!mIsFile) {
|
2014-10-08 20:15:22 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
aFileName.Truncate();
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
void
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplBase::GetType(nsAString& aType)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
|
|
|
aType = mContentType;
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
int64_t
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplBase::GetLastModified(ErrorResult& aRv)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mIsFile, "Should only be called on files");
|
2014-10-07 21:16:11 +04:00
|
|
|
if (IsDateUnknown()) {
|
|
|
|
mLastModificationDate = PR_Now();
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
return mLastModificationDate / PR_USEC_PER_MSEC;
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2015-02-07 21:29:22 +03:00
|
|
|
void
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplBase::SetLastModified(int64_t aLastModified)
|
2015-02-07 21:29:22 +03:00
|
|
|
{
|
|
|
|
mLastModificationDate = aLastModified * PR_USEC_PER_MSEC;
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:16:11 +04:00
|
|
|
int64_t
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplBase::GetFileId()
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-09-09 14:15:14 +03:00
|
|
|
return -1;
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplBase::GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
|
2014-10-08 20:15:23 +04:00
|
|
|
nsACString& aContentType, nsACString& aCharset)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2014-10-08 20:15:22 +04:00
|
|
|
MOZ_ASSERT(aContentLength);
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
ErrorResult rv;
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2015-05-19 17:36:37 +03:00
|
|
|
GetInternalStream(getter_AddRefs(stream), rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
*aContentLength = GetSize(rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
nsAutoString contentType;
|
|
|
|
GetType(contentType);
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2016-07-11 21:27:36 +03:00
|
|
|
if (contentType.IsEmpty()) {
|
|
|
|
aContentType.SetIsVoid(true);
|
|
|
|
} else {
|
|
|
|
CopyUTF16toUTF8(contentType, aContentType);
|
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
aCharset.Truncate();
|
|
|
|
|
|
|
|
stream.forget(aBody);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplBase::GetMutable(bool* aMutable) const
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
|
|
|
*aMutable = !mImmutable;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplBase::SetMutable(bool aMutable)
|
2014-10-07 21:16:11 +04: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 20:15:22 +04:00
|
|
|
nsAutoString dummyString;
|
|
|
|
GetType(dummyString);
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
ErrorResult error;
|
|
|
|
GetSize(error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return error.StealNSResult();
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mImmutable = !aMutable;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-05-21 04:49:50 +03:00
|
|
|
/* static */ uint64_t
|
|
|
|
BlobImplBase::NextSerialNumber()
|
|
|
|
{
|
|
|
|
static Atomic<uint64_t> nextSerialNumber;
|
|
|
|
return nextSerialNumber++;
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:16:11 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 15:11:03 +03:00
|
|
|
// BlobImplFile implementation
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
BlobImplFile::CreateSlice(uint64_t aStart, uint64_t aLength,
|
2014-10-08 20:15:23 +04:00
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> impl =
|
2015-05-12 15:11:03 +03:00
|
|
|
new BlobImplFile(this, aStart, aLength, aContentType);
|
2014-10-07 21:16:11 +04:00
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
void
|
2015-06-03 03:11:16 +03:00
|
|
|
BlobImplFile::GetMozFullPathInternal(nsAString& aFilename, ErrorResult& aRv) const
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mIsFile, "Should only be called on files");
|
2014-10-08 20:15:22 +04:00
|
|
|
aRv = mFile->GetPath(aFilename);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
uint64_t
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplFile::GetSize(ErrorResult& aRv)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-12-05 00:15:46 +03:00
|
|
|
if (BlobImplBase::IsSizeUnknown()) {
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mWholeFile,
|
2014-10-07 21:16:11 +04:00
|
|
|
"Should only use lazy size when using the whole file");
|
|
|
|
int64_t fileSize;
|
2014-10-08 20:15:22 +04:00
|
|
|
aRv = mFile->GetFileSize(&fileSize);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
if (fileSize < 0) {
|
2014-10-08 20:15:22 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return 0;
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mLength = fileSize;
|
|
|
|
}
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
return mLength;
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2016-03-31 10:32:58 +03:00
|
|
|
namespace {
|
|
|
|
|
2016-05-03 10:09:47 +03:00
|
|
|
class GetTypeRunnable final : public WorkerMainThreadRunnable
|
2016-03-31 10:32:58 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
GetTypeRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
BlobImpl* aBlobImpl)
|
2016-05-03 10:09:47 +03:00
|
|
|
: WorkerMainThreadRunnable(aWorkerPrivate,
|
|
|
|
NS_LITERAL_CSTRING("BlobImplFile :: GetType"))
|
2016-03-31 10:32:58 +03:00
|
|
|
, mBlobImpl(aBlobImpl)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aBlobImpl);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2016-05-03 10:09:47 +03:00
|
|
|
bool
|
|
|
|
MainThreadRun() override
|
2016-03-31 10:32:58 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsAutoString type;
|
|
|
|
mBlobImpl->GetType(type);
|
2016-05-03 10:09:47 +03:00
|
|
|
return true;
|
2016-03-31 10:32:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~GetTypeRunnable()
|
|
|
|
{}
|
|
|
|
|
|
|
|
RefPtr<BlobImpl> mBlobImpl;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-10-08 20:15:22 +04:00
|
|
|
void
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplFile::GetType(nsAString& aType)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2016-03-31 10:32:58 +03:00
|
|
|
aType.Truncate();
|
|
|
|
|
2014-10-07 21:16:11 +04:00
|
|
|
if (mContentType.IsVoid()) {
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mWholeFile,
|
|
|
|
"Should only use lazy ContentType when using the whole file");
|
2016-03-31 10:32:58 +03: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 10:09:47 +03:00
|
|
|
new GetTypeRunnable(workerPrivate, this);
|
2016-03-31 10:32:58 +03:00
|
|
|
|
2016-05-03 10:09:47 +03:00
|
|
|
ErrorResult rv;
|
2017-01-05 12:05:32 +03:00
|
|
|
runnable->Dispatch(Terminating, rv);
|
2016-07-22 17:50:10 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
|
|
|
}
|
2016-03-31 10:32:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:16:11 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeService =
|
|
|
|
do_GetService(NS_MIMESERVICE_CONTRACTID, &rv);
|
2014-10-08 20:15:22 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-07 21:16:11 +04: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 20:15:22 +04:00
|
|
|
int64_t
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplFile::GetLastModified(ErrorResult& aRv)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2016-12-13 04:25:37 +03:00
|
|
|
MOZ_ASSERT(mIsFile, "Should only be called on files");
|
2015-12-05 00:15:46 +03:00
|
|
|
if (BlobImplBase::IsDateUnknown()) {
|
2014-10-07 21:16:11 +04:00
|
|
|
PRTime msecs;
|
2014-10-08 20:15:22 +04:00
|
|
|
aRv = mFile->GetLastModifiedTime(&msecs);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:16:11 +04:00
|
|
|
mLastModificationDate = msecs;
|
|
|
|
}
|
2014-10-08 20:15:22 +04:00
|
|
|
|
|
|
|
return mLastModificationDate;
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2015-02-07 21:29:22 +03:00
|
|
|
void
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplFile::SetLastModified(int64_t aLastModified)
|
2015-02-07 21:29:22 +03:00
|
|
|
{
|
|
|
|
MOZ_CRASH("SetLastModified of a real file is not allowed!");
|
|
|
|
}
|
|
|
|
|
2014-10-07 21:16:11 +04:00
|
|
|
const uint32_t sFileStreamFlags =
|
|
|
|
nsIFileInputStream::CLOSE_ON_EOF |
|
|
|
|
nsIFileInputStream::REOPEN_ON_REWIND |
|
|
|
|
nsIFileInputStream::DEFER_OPEN |
|
|
|
|
nsIFileInputStream::SHARE_DELETE;
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
void
|
|
|
|
BlobImplFile::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-05-19 17:36:37 +03: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 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
2016-07-29 14:41:38 +03:00
|
|
|
bool
|
|
|
|
BlobImplFile::IsDirectory() const
|
|
|
|
{
|
|
|
|
bool isDirectory = false;
|
|
|
|
if (mFile) {
|
|
|
|
mFile->IsDirectory(&isDirectory);
|
|
|
|
}
|
|
|
|
return isDirectory;
|
|
|
|
}
|
|
|
|
|
2016-01-08 11:35:30 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2016-01-20 20:25:03 +03:00
|
|
|
// EmptyBlobImpl implementation
|
2016-01-08 11:35:30 +03:00
|
|
|
|
2016-01-20 20:25:03 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(EmptyBlobImpl, BlobImpl)
|
2016-01-08 11:35:30 +03:00
|
|
|
|
|
|
|
already_AddRefed<BlobImpl>
|
2016-01-20 20:25:03 +03:00
|
|
|
EmptyBlobImpl::CreateSlice(uint64_t aStart, uint64_t aLength,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
2016-01-08 11:35:30 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aStart && !aLength);
|
2016-01-20 20:25:03 +03:00
|
|
|
RefPtr<BlobImpl> impl = new EmptyBlobImpl(aContentType);
|
2016-04-11 21:31:53 +03:00
|
|
|
|
|
|
|
DebugOnly<bool> isMutable;
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(impl->GetMutable(&isMutable)));
|
|
|
|
MOZ_ASSERT(!isMutable);
|
|
|
|
|
2016-01-08 11:35:30 +03:00
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-20 20:25:03 +03:00
|
|
|
EmptyBlobImpl::GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv)
|
2016-01-08 11:35:30 +03:00
|
|
|
{
|
2016-04-11 21:31:53 +03:00
|
|
|
if (NS_WARN_IF(!aStream)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-08 11:35:30 +03:00
|
|
|
nsresult rv = NS_NewCStringInputStream(aStream, EmptyCString());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-25 08:53:54 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BlobImplString implementation
|
|
|
|
|
2016-11-02 12:35:57 +03: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 08:53:54 +03: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 12:35:57 +03: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 21:16:11 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 15:11:03 +03:00
|
|
|
// BlobImplMemory implementation
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(BlobImplMemory, BlobImpl)
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
BlobImplMemory::CreateSlice(uint64_t aStart, uint64_t aLength,
|
2014-10-08 20:15:23 +04:00
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> impl =
|
2015-05-12 15:11:03 +03:00
|
|
|
new BlobImplMemory(this, aStart, aLength, aContentType);
|
2014-10-07 21:16:11 +04:00
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
void
|
|
|
|
BlobImplMemory::GetInternalStream(nsIInputStream** aStream, ErrorResult& aRv)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-05-19 17:36:37 +03:00
|
|
|
if (mLength > INT32_MAX) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
aRv = DataOwnerAdapter::Create(mDataOwner, mStart, mLength, aStream);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ StaticMutex
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplMemory::DataOwner::sDataOwnerMutex;
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
/* static */ StaticAutoPtr<LinkedList<BlobImplMemory::DataOwner>>
|
|
|
|
BlobImplMemory::DataOwner::sDataOwners;
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
/* static */ bool
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplMemory::DataOwner::sMemoryReporterRegistered = false;
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2014-10-08 20:15:23 +04:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(MemoryFileDataOwnerMallocSizeOf)
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
class BlobImplMemoryDataOwnerMemoryReporter final
|
2014-10-07 21:16:11 +04:00
|
|
|
: public nsIMemoryReporter
|
|
|
|
{
|
2015-05-12 15:11:03 +03:00
|
|
|
~BlobImplMemoryDataOwnerMemoryReporter() {}
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aData, bool aAnonymize) override
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2015-05-12 15:11:03 +03:00
|
|
|
typedef BlobImplMemory::DataOwner DataOwner;
|
2014-10-07 21:16:11 +04: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 20:15:23 +04:00
|
|
|
size_t size = MemoryFileDataOwnerMallocSizeOf(owner->mData);
|
2014-10-07 21:16:11 +04: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 08:23:45 +03:00
|
|
|
aHandleReport->Callback(
|
2014-10-07 21:16:11 +04: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 08:23:45 +03:00
|
|
|
aData);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (smallObjectsTotal > 0) {
|
2016-08-24 08:23:45 +03:00
|
|
|
aHandleReport->Callback(
|
2014-10-07 21:16:11 +04:00
|
|
|
/* process */ NS_LITERAL_CSTRING(""),
|
|
|
|
NS_LITERAL_CSTRING("explicit/dom/memory-file-data/small"),
|
|
|
|
KIND_HEAP, UNITS_BYTES, smallObjectsTotal,
|
|
|
|
nsPrintfCString(
|
2016-09-05 07:14:00 +03: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 21:16:11 +04: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 07:14:00 +03:00
|
|
|
"memory.", LARGE_OBJECT_MIN_SIZE),
|
2016-08-24 08:23:45 +03:00
|
|
|
aData);
|
2014-10-07 21:16:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
NS_IMPL_ISUPPORTS(BlobImplMemoryDataOwnerMemoryReporter, nsIMemoryReporter)
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
/* static */ void
|
2015-05-12 15:11:03 +03:00
|
|
|
BlobImplMemory::DataOwner::EnsureMemoryReporterRegistered()
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
|
|
|
sDataOwnerMutex.AssertCurrentThreadOwns();
|
|
|
|
if (sMemoryReporterRegistered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
RegisterStrongMemoryReporter(new BlobImplMemoryDataOwnerMemoryReporter());
|
2014-10-07 21:16:11 +04:00
|
|
|
|
|
|
|
sMemoryReporterRegistered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-05-12 15:11:03 +03:00
|
|
|
// BlobImplTemporaryBlob implementation
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(BlobImplTemporaryBlob, BlobImpl)
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
already_AddRefed<BlobImpl>
|
|
|
|
BlobImplTemporaryBlob::CreateSlice(uint64_t aStart, uint64_t aLength,
|
2015-05-12 15:09:51 +03:00
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
2014-10-08 20:15:22 +04:00
|
|
|
if (aStart + aLength > mLength) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2014-10-07 21:16:11 +04:00
|
|
|
return nullptr;
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
2014-10-07 21:16:11 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> impl =
|
2015-05-12 15:11:03 +03:00
|
|
|
new BlobImplTemporaryBlob(this, aStart + mStartPos,
|
2015-05-12 15:09:51 +03:00
|
|
|
aLength, aContentType);
|
2014-10-07 21:16:11 +04:00
|
|
|
return impl.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
void
|
|
|
|
BlobImplTemporaryBlob::GetInternalStream(nsIInputStream** aStream,
|
|
|
|
ErrorResult& aRv)
|
2014-10-07 21:16:11 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> stream =
|
|
|
|
new nsTemporaryFileInputStream(mFileDescOwner, mStartPos, mStartPos + mLength);
|
|
|
|
stream.forget(aStream);
|
|
|
|
}
|
|
|
|
|
2016-09-21 13:27:26 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// BlobImplStream implementation
|
|
|
|
|
2016-11-04 09:52:04 +03: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 13:27:26 +03: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 09:52:04 +03:00
|
|
|
{
|
|
|
|
UnregisterWeakMemoryReporter(this);
|
|
|
|
}
|
2016-09-21 13:27:26 +03: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 09:52:04 +03: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 10:45:42 +03:00
|
|
|
nsCOMPtr<nsIStringInputStream> stringInputStream =
|
|
|
|
do_QueryInterface(mInputStream);
|
|
|
|
if (!stringInputStream) {
|
|
|
|
return NS_OK;
|
2016-11-04 09:52:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"explicit/dom/memory-file-data/stream", KIND_HEAP, UNITS_BYTES,
|
2016-11-07 10:45:42 +03:00
|
|
|
stringInputStream->SizeOfIncludingThis(MallocSizeOf),
|
2016-11-04 09:52:04 +03:00
|
|
|
"Memory used to back a File/Blob based on an input stream.");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|