2017-02-16 20:26:38 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_MemoryBlobImpl_h
|
|
|
|
#define mozilla_dom_MemoryBlobImpl_h
|
|
|
|
|
|
|
|
#include "mozilla/dom/BaseBlobImpl.h"
|
|
|
|
#include "mozilla/LinkedList.h"
|
|
|
|
#include "mozilla/StaticMutex.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2017-03-29 11:40:39 +03:00
|
|
|
#include "nsICloneableInputStream.h"
|
2017-03-29 11:40:38 +03:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
2017-02-16 20:26:38 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class MemoryBlobImpl final : public BaseBlobImpl {
|
|
|
|
public:
|
2018-02-12 23:44:40 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING_INHERITED(MemoryBlobImpl, BaseBlobImpl)
|
2017-02-16 20:26:38 +03:00
|
|
|
|
2020-03-05 00:25:46 +03:00
|
|
|
// File constructor.
|
2020-03-05 00:26:03 +03:00
|
|
|
static already_AddRefed<MemoryBlobImpl> CreateWithLastModifiedNow(
|
|
|
|
void* aMemoryBuffer, uint64_t aLength, const nsAString& aName,
|
|
|
|
const nsAString& aContentType);
|
|
|
|
|
|
|
|
// File constructor with custom lastModified attribue value. You should
|
|
|
|
// probably use CreateWithLastModifiedNow() instead of this one.
|
|
|
|
static already_AddRefed<MemoryBlobImpl> CreateWithCustomLastModified(
|
|
|
|
void* aMemoryBuffer, uint64_t aLength, const nsAString& aName,
|
|
|
|
const nsAString& aContentType, int64_t aLastModifiedDate);
|
2017-02-16 20:26:38 +03:00
|
|
|
|
2020-03-05 00:25:46 +03:00
|
|
|
// Blob constructor.
|
2017-02-16 20:26:38 +03:00
|
|
|
MemoryBlobImpl(void* aMemoryBuffer, uint64_t aLength,
|
|
|
|
const nsAString& aContentType)
|
2018-12-15 23:40:05 +03:00
|
|
|
: BaseBlobImpl(NS_LITERAL_STRING("MemoryBlobImpl"), aContentType,
|
|
|
|
aLength),
|
2017-02-16 20:26:38 +03:00
|
|
|
mDataOwner(new DataOwner(aMemoryBuffer, aLength)) {
|
|
|
|
MOZ_ASSERT(mDataOwner && mDataOwner->mData, "must have data");
|
|
|
|
}
|
|
|
|
|
2017-10-02 14:53:12 +03:00
|
|
|
virtual void CreateInputStream(nsIInputStream** aStream,
|
2017-02-16 20:26:38 +03:00
|
|
|
ErrorResult& aRv) override;
|
|
|
|
|
|
|
|
virtual already_AddRefed<BlobImpl> CreateSlice(uint64_t aStart,
|
|
|
|
uint64_t aLength,
|
|
|
|
const nsAString& aContentType,
|
|
|
|
ErrorResult& aRv) override;
|
|
|
|
|
|
|
|
virtual bool IsMemoryFile() const override { return true; }
|
|
|
|
|
2017-09-12 12:46:51 +03:00
|
|
|
size_t GetAllocationSize() const override { return mLength; }
|
|
|
|
|
2018-06-18 18:35:46 +03:00
|
|
|
size_t GetAllocationSize(
|
|
|
|
FallibleTArray<BlobImpl*>& aVisitedBlobImpls) const override {
|
|
|
|
return GetAllocationSize();
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
class DataOwner final : public mozilla::LinkedListElement<DataOwner> {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DataOwner)
|
|
|
|
DataOwner(void* aMemoryBuffer, uint64_t aLength)
|
|
|
|
: mData(aMemoryBuffer), mLength(aLength) {
|
|
|
|
mozilla::StaticMutexAutoLock lock(sDataOwnerMutex);
|
|
|
|
|
|
|
|
if (!sDataOwners) {
|
|
|
|
sDataOwners = new mozilla::LinkedList<DataOwner>();
|
|
|
|
EnsureMemoryReporterRegistered();
|
|
|
|
}
|
|
|
|
sDataOwners->insertBack(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Private destructor, to discourage deletion outside of Release():
|
|
|
|
~DataOwner() {
|
|
|
|
mozilla::StaticMutexAutoLock lock(sDataOwnerMutex);
|
|
|
|
|
|
|
|
remove();
|
|
|
|
if (sDataOwners->isEmpty()) {
|
|
|
|
// Free the linked list if it's empty.
|
|
|
|
sDataOwners = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(mData);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static void EnsureMemoryReporterRegistered();
|
|
|
|
|
|
|
|
// sDataOwners and sMemoryReporterRegistered may only be accessed while
|
|
|
|
// holding sDataOwnerMutex! You also must hold the mutex while touching
|
|
|
|
// elements of the linked list that DataOwner inherits from.
|
|
|
|
static mozilla::StaticMutex sDataOwnerMutex;
|
|
|
|
static mozilla::StaticAutoPtr<mozilla::LinkedList<DataOwner> > sDataOwners;
|
|
|
|
static bool sMemoryReporterRegistered;
|
|
|
|
|
|
|
|
void* mData;
|
|
|
|
uint64_t mLength;
|
|
|
|
};
|
|
|
|
|
2017-03-29 11:40:39 +03:00
|
|
|
class DataOwnerAdapter final : public nsIInputStream,
|
|
|
|
public nsISeekableStream,
|
|
|
|
public nsIIPCSerializableInputStream,
|
|
|
|
public nsICloneableInputStream {
|
2017-03-29 11:40:38 +03:00
|
|
|
typedef MemoryBlobImpl::DataOwner DataOwner;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-03-29 11:40:38 +03: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->)
|
Bug 1496581 - Split nsISeekableStream in 2 classes: nsISeekableStream and nsITellableStream, f=mayhemer, r=froydnj
In the current code there are 3 main issues:
1. nsFileStream is not really thread-safe. There is nothing to protect the
internal members and we see crashes.
2. nsPipeInputStream doesn't implement ::Seek() method and that caused issues
in devtools when a nsHttpChannel sends POST data using a pipe. In order to fix
this, bug 1494176 added a check in nsHttpChannel: if the stream doesn't
implement ::Seek(), let's clone it. This was an hack around nsPipeInputStream,
and it's bad.
3. When nsHttpChannel sends POST data using a file stream, nsFileStream does
I/O on main-thread because of the issue 2. Plus, ::Seek() is called on the
main-thread causing issue 1.
Note that nsPipeInputStream implements only ::Tell(), of the nsISeekableStream
methods. It doesn't implement ::Seek() and it doesn't implement ::SetEOF().
With this patch I want to fix point 2 and point 3 (and consequentially issue 1
- but we need a separate fix for it - follow up). The patch does:
1. it splits nsISeekableStream in 2 interfaces: nsITellableStream and
nsISeekableStream.
2. nsPipeInputStream implements only nsITellableStream. Doing this, we don't
need the ::Seek() check for point 2 in nsHttpChannel: a simple QI check is
enough.
3. Because we don't call ::Seek() in nsHttpChannel, nsFileStream doesn't do I/O
on the main-thread, and we don't crash doing so.
2018-10-18 14:35:35 +03:00
|
|
|
NS_FORWARD_NSITELLABLESTREAM(mSeekableStream->)
|
2017-03-29 11:40:39 +03:00
|
|
|
NS_FORWARD_NSICLONEABLEINPUTSTREAM(mCloneableInputStream->)
|
2017-03-29 11:40:38 +03:00
|
|
|
|
|
|
|
// 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:
|
2020-02-20 19:11:24 +03:00
|
|
|
~DataOwnerAdapter() = default;
|
2017-03-29 11:40:38 +03:00
|
|
|
|
|
|
|
DataOwnerAdapter(DataOwner* aDataOwner, nsIInputStream* aStream)
|
2017-03-29 11:40:39 +03:00
|
|
|
: mDataOwner(aDataOwner),
|
|
|
|
mStream(aStream),
|
|
|
|
mSeekableStream(do_QueryInterface(aStream)),
|
|
|
|
mSerializableInputStream(do_QueryInterface(aStream)),
|
|
|
|
mCloneableInputStream(do_QueryInterface(aStream)) {
|
2017-03-29 11:40:38 +03:00
|
|
|
MOZ_ASSERT(mSeekableStream, "Somebody gave us the wrong stream!");
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<DataOwner> mDataOwner;
|
|
|
|
nsCOMPtr<nsIInputStream> mStream;
|
|
|
|
nsCOMPtr<nsISeekableStream> mSeekableStream;
|
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> mSerializableInputStream;
|
2017-03-29 11:40:39 +03:00
|
|
|
nsCOMPtr<nsICloneableInputStream> mCloneableInputStream;
|
2017-03-29 11:40:38 +03:00
|
|
|
};
|
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
private:
|
2020-03-05 00:26:03 +03:00
|
|
|
// File constructor.
|
|
|
|
MemoryBlobImpl(void* aMemoryBuffer, uint64_t aLength, const nsAString& aName,
|
|
|
|
const nsAString& aContentType, int64_t aLastModifiedDate)
|
|
|
|
: BaseBlobImpl(NS_LITERAL_STRING("MemoryBlobImpl"), aName, aContentType,
|
|
|
|
aLength, aLastModifiedDate),
|
|
|
|
mDataOwner(new DataOwner(aMemoryBuffer, aLength)) {
|
|
|
|
MOZ_ASSERT(mDataOwner && mDataOwner->mData, "must have data");
|
|
|
|
}
|
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
// Create slice
|
|
|
|
MemoryBlobImpl(const MemoryBlobImpl* aOther, uint64_t aStart,
|
|
|
|
uint64_t aLength, const nsAString& aContentType)
|
2018-12-15 23:40:05 +03:00
|
|
|
: BaseBlobImpl(NS_LITERAL_STRING("MemoryBlobImpl"), aContentType,
|
|
|
|
aOther->mStart + aStart, aLength),
|
2017-02-16 20:26:38 +03:00
|
|
|
mDataOwner(aOther->mDataOwner) {
|
|
|
|
MOZ_ASSERT(mDataOwner && mDataOwner->mData, "must have data");
|
|
|
|
}
|
|
|
|
|
2020-02-20 19:11:24 +03:00
|
|
|
~MemoryBlobImpl() = default;
|
2017-02-16 20:26:38 +03:00
|
|
|
|
|
|
|
// Used when backed by a memory store
|
|
|
|
RefPtr<DataOwner> mDataOwner;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_MemoryBlobImpl_h
|