Bug 1619518 - part 4 - FileBlobImpl inherits BlobImpl directly, r=ssengupta,smaug

Differential Revision: https://phabricator.services.mozilla.com/D65166

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Andrea Marchesini 2020-03-04 21:27:43 +00:00
Родитель 8d8aabb99b
Коммит 64ed039230
8 изменённых файлов: 165 добавлений и 113 удалений

Просмотреть файл

@ -53,40 +53,6 @@ int64_t BaseBlobImpl::GetLastModified(ErrorResult& aRv) {
int64_t BaseBlobImpl::GetFileId() { return -1; }
nsresult BaseBlobImpl::GetSendInfo(nsIInputStream** aBody,
uint64_t* aContentLength,
nsACString& aContentType,
nsACString& aCharset) {
MOZ_ASSERT(aContentLength);
ErrorResult rv;
nsCOMPtr<nsIInputStream> stream;
CreateInputStream(getter_AddRefs(stream), rv);
if (NS_WARN_IF(rv.Failed())) {
return rv.StealNSResult();
}
*aContentLength = GetSize(rv);
if (NS_WARN_IF(rv.Failed())) {
return rv.StealNSResult();
}
nsAutoString contentType;
GetType(contentType);
if (contentType.IsEmpty()) {
aContentType.SetIsVoid(true);
} else {
CopyUTF16toUTF8(contentType, aContentType);
}
aCharset.Truncate();
stream.forget(aBody);
return NS_OK;
}
/* static */
uint64_t BaseBlobImpl::NextSerialNumber() {
static Atomic<uint64_t> nextSerialNumber;

Просмотреть файл

@ -12,7 +12,11 @@
namespace mozilla {
namespace dom {
class FileBlobImpl;
class BaseBlobImpl : public BlobImpl {
friend class FileBlobImpl;
public:
// File constructor.
BaseBlobImpl(const nsAString& aBlobImplType, const nsAString& aName,
@ -104,10 +108,6 @@ class BaseBlobImpl : public BlobImpl {
virtual int64_t GetFileId() override;
virtual nsresult GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
nsACString& aContentType,
nsACString& aCharset) override;
virtual void SetLazyData(const nsAString& aName,
const nsAString& aContentType, uint64_t aLength,
int64_t aLastModifiedDate) override {

Просмотреть файл

@ -61,6 +61,38 @@ already_AddRefed<BlobImpl> BlobImpl::Slice(const Optional<int64_t>& aStart,
return CreateSlice((uint64_t)start, (uint64_t)(end - start), type, aRv);
}
nsresult BlobImpl::GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
nsACString& aContentType, nsACString& aCharset) {
MOZ_ASSERT(aContentLength);
ErrorResult rv;
nsCOMPtr<nsIInputStream> stream;
CreateInputStream(getter_AddRefs(stream), rv);
if (NS_WARN_IF(rv.Failed())) {
return rv.StealNSResult();
}
*aContentLength = GetSize(rv);
if (NS_WARN_IF(rv.Failed())) {
return rv.StealNSResult();
}
nsAutoString contentType;
GetType(contentType);
if (contentType.IsEmpty()) {
aContentType.SetIsVoid(true);
} else {
CopyUTF16toUTF8(contentType, aContentType);
}
aCharset.Truncate();
stream.forget(aBody);
return NS_OK;
}
NS_IMPL_ISUPPORTS(BlobImpl, BlobImpl)
} // namespace dom

Просмотреть файл

@ -83,9 +83,8 @@ class BlobImpl : public nsISupports {
virtual int64_t GetFileId() = 0;
virtual nsresult GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
nsACString& aContentType,
nsACString& aCharset) = 0;
nsresult GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength,
nsACString& aContentType, nsACString& aCharset);
virtual void SetLazyData(const nsAString& aName,
const nsAString& aContentType, uint64_t aLength,

Просмотреть файл

@ -19,14 +19,12 @@ namespace mozilla {
namespace dom {
FileBlobImpl::FileBlobImpl(nsIFile* aFile)
: BaseBlobImpl(NS_LITERAL_STRING("FileBlobImpl"), EmptyString(),
EmptyString(),
// We pass 0 as lastModified and length because FileblobImpl
// has a different way to compute those values.
0, 0),
mMutex("FileBlobImpl::mMutex"),
: mMutex("FileBlobImpl::mMutex"),
mFile(aFile),
mSerialNumber(BaseBlobImpl::NextSerialNumber()),
mStart(0),
mFileId(-1),
mIsFile(true),
mWholeFile(true) {
MOZ_ASSERT(mFile, "must have file");
MOZ_ASSERT(XRE_IsParentProcess());
@ -39,14 +37,15 @@ FileBlobImpl::FileBlobImpl(nsIFile* aFile)
FileBlobImpl::FileBlobImpl(const nsAString& aName,
const nsAString& aContentType, uint64_t aLength,
nsIFile* aFile)
: BaseBlobImpl(NS_LITERAL_STRING("FileBlobImpl"), aName, aContentType,
// We pass 0 as lastModified and length because FileblobImpl
// has a different way to compute those values.
0, 0),
mMutex("FileBlobImpl::mMutex"),
: mMutex("FileBlobImpl::mMutex"),
mFile(aFile),
mLength(Some(aLength)),
mContentType(aContentType),
mName(aName),
mSerialNumber(BaseBlobImpl::NextSerialNumber()),
mStart(0),
mFileId(-1),
mLength(Some(aLength)),
mIsFile(true),
mWholeFile(true) {
MOZ_ASSERT(mFile, "must have file");
MOZ_ASSERT(XRE_IsParentProcess());
@ -56,15 +55,16 @@ FileBlobImpl::FileBlobImpl(const nsAString& aName,
FileBlobImpl::FileBlobImpl(const nsAString& aName,
const nsAString& aContentType, uint64_t aLength,
nsIFile* aFile, int64_t aLastModificationDate)
: BaseBlobImpl(NS_LITERAL_STRING("FileBlobImpl"), aName, aContentType,
// We pass 0 as lastModified and length because FileblobImpl
// has a different way to compute those values.
0, 0),
mMutex("FileBlobImpl::mMutex"),
: mMutex("FileBlobImpl::mMutex"),
mFile(aFile),
mContentType(aContentType),
mName(aName),
mSerialNumber(BaseBlobImpl::NextSerialNumber()),
mStart(0),
mFileId(-1),
mLength(Some(aLength)),
mLastModified(Some(aLastModificationDate)),
mFileId(-1),
mIsFile(true),
mWholeFile(true) {
MOZ_ASSERT(mFile, "must have file");
MOZ_ASSERT(XRE_IsParentProcess());
@ -72,15 +72,15 @@ FileBlobImpl::FileBlobImpl(const nsAString& aName,
}
FileBlobImpl::FileBlobImpl(nsIFile* aFile, const nsAString& aName,
const nsAString& aContentType,
const nsAString& aBlobImplType)
: BaseBlobImpl(aBlobImplType, aName, aContentType,
// We pass 0 as lastModified and length because FileblobImpl
// has a different way to compute those values.
0, 0),
mMutex("FileBlobImpl::mMutex"),
const nsAString& aContentType)
: mMutex("FileBlobImpl::mMutex"),
mFile(aFile),
mContentType(aContentType),
mName(aName),
mSerialNumber(BaseBlobImpl::NextSerialNumber()),
mStart(0),
mFileId(-1),
mIsFile(true),
mWholeFile(true) {
MOZ_ASSERT(mFile, "must have file");
MOZ_ASSERT(XRE_IsParentProcess());
@ -94,15 +94,14 @@ FileBlobImpl::FileBlobImpl(nsIFile* aFile, const nsAString& aName,
FileBlobImpl::FileBlobImpl(const FileBlobImpl* aOther, uint64_t aStart,
uint64_t aLength, const nsAString& aContentType)
: BaseBlobImpl(NS_LITERAL_STRING("FileBlobImpl"), aContentType,
aOther->mStart + aStart,
// We pass 0 as length because FileblobImpl has a different
// way to compute the value.
0),
mMutex("FileBlobImpl::mMutex"),
: mMutex("FileBlobImpl::mMutex"),
mFile(aOther->mFile),
mLength(Some(aLength)),
mContentType(aContentType),
mSerialNumber(BaseBlobImpl::NextSerialNumber()),
mStart(aOther->mStart + aStart),
mFileId(-1),
mLength(Some(aLength)),
mIsFile(false),
mWholeFile(false) {
MOZ_ASSERT(mFile, "must have file");
MOZ_ASSERT(XRE_IsParentProcess());
@ -238,6 +237,10 @@ void FileBlobImpl::GetTypeInternal(nsAString& aType,
aType = mContentType;
}
void FileBlobImpl::GetBlobImplType(nsAString& aBlobImplType) const {
aBlobImplType = NS_LITERAL_STRING("FileBlobImpl");
}
int64_t FileBlobImpl::GetLastModified(ErrorResult& aRv) {
MOZ_ASSERT(mIsFile, "Should only be called on files");

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef mozilla_dom_FileBlobImpl_h
#define mozilla_dom_FileBlobImpl_h
#include "mozilla/dom/BaseBlobImpl.h"
#include "mozilla/dom/BlobImpl.h"
#include "mozilla/Maybe.h"
#include "mozilla/Mutex.h"
@ -16,9 +16,9 @@ class nsIFile;
namespace mozilla {
namespace dom {
class FileBlobImpl : public BaseBlobImpl {
class FileBlobImpl : public BlobImpl {
public:
NS_INLINE_DECL_REFCOUNTING_INHERITED(FileBlobImpl, BaseBlobImpl)
NS_INLINE_DECL_REFCOUNTING_INHERITED(FileBlobImpl, BlobImpl)
// Create as a file
explicit FileBlobImpl(nsIFile* aFile);
@ -31,35 +31,78 @@ class FileBlobImpl : public BaseBlobImpl {
uint64_t aLength, nsIFile* aFile, int64_t aLastModificationDate);
// Create as a file with custom name
FileBlobImpl(
nsIFile* aFile, const nsAString& aName, const nsAString& aContentType,
const nsAString& aBlobImplType = NS_LITERAL_STRING("FileBlobImpl"));
FileBlobImpl(nsIFile* aFile, const nsAString& aName,
const nsAString& aContentType);
// Overrides
virtual uint64_t GetSize(ErrorResult& aRv) override;
virtual void GetType(nsAString& aType) override;
virtual int64_t GetLastModified(ErrorResult& aRv) override;
virtual void GetMozFullPathInternal(nsAString& aFullPath,
ErrorResult& aRv) override;
virtual void CreateInputStream(nsIInputStream** aInputStream,
ErrorResult& aRv) override;
virtual bool IsDirectory() const override;
virtual void SetLazyData(const nsAString& aName,
const nsAString& aContentType, uint64_t aLength,
int64_t aLastModifiedDate) override {
BaseBlobImpl::SetLazyData(aName, aContentType, 0, 0);
mLength.emplace(aLength);
mLastModified.emplace(aLastModifiedDate);
void GetName(nsAString& aName) const override {
MOZ_ASSERT(mIsFile, "Should only be called on files");
aName = mName;
}
void SetName(const nsAString& aName) { mName = aName; }
void SetType(const nsAString& aType) { mContentType = aType; }
void GetDOMPath(nsAString& aPath) const override {
MOZ_ASSERT(mIsFile, "Should only be called on files");
aPath = mPath;
}
void SetDOMPath(const nsAString& aPath) override {
MOZ_ASSERT(mIsFile, "Should only be called on files");
mPath = aPath;
}
int64_t GetLastModified(ErrorResult& aRv) override;
void GetMozFullPath(nsAString& aFileName, SystemCallerGuarantee /* unused */,
ErrorResult& aRv) override {
MOZ_ASSERT(mIsFile, "Should only be called on files");
GetMozFullPathInternal(aFileName, aRv);
}
void GetMozFullPathInternal(nsAString& aFilename, ErrorResult& aRv) override;
uint64_t GetSize(ErrorResult& aRv) override;
void GetType(nsAString& aType) override;
virtual void GetBlobImplType(nsAString& aBlobImplType) const override;
size_t GetAllocationSize() const override { return 0; }
size_t GetAllocationSize(
FallibleTArray<BlobImpl*>& aVisitedBlobImpls) const override {
return GetAllocationSize();
}
uint64_t GetSerialNumber() const override { return mSerialNumber; }
const nsTArray<RefPtr<BlobImpl>>* GetSubBlobImpls() const override {
return nullptr;
}
virtual void CreateInputStream(nsIInputStream** aInputStream,
ErrorResult& aRv) override;
int64_t GetFileId() override { return mFileId; }
void SetLazyData(const nsAString& aName, const nsAString& aContentType,
uint64_t aLength, int64_t aLastModifiedDate) override {
mName = aName;
mContentType = aContentType;
mIsFile = !aName.IsVoid();
mLength.emplace(aLength);
mLastModified.emplace(aLastModifiedDate);
}
bool IsMemoryFile() const override { return false; }
bool IsFile() const override { return mIsFile; }
bool IsDirectory() const override;
void SetType(const nsAString& aType) { mContentType = aType; }
void SetFileId(int64_t aFileId) { mFileId = aFileId; }
void SetEmptySize() { mLength.emplace(0); }
@ -71,34 +114,40 @@ class FileBlobImpl : public BaseBlobImpl {
}
protected:
virtual ~FileBlobImpl() = default;
~FileBlobImpl() = default;
// Create slice
FileBlobImpl(const FileBlobImpl* aOther, uint64_t aStart, uint64_t aLength,
const nsAString& aContentType);
virtual already_AddRefed<BlobImpl> CreateSlice(uint64_t aStart,
uint64_t aLength,
const nsAString& aContentType,
ErrorResult& aRv) override;
already_AddRefed<BlobImpl> CreateSlice(uint64_t aStart, uint64_t aLength,
const nsAString& aContentType,
ErrorResult& aRv) override;
class GetTypeRunnable;
void GetTypeInternal(nsAString& aType, const MutexAutoLock& aProofOfLock);
// FileBlobImpl is the only BlobImpl with a few getter methods with lazy
// initialization. Because any BlobImpl must work thread-safe, we use a mutex.
// This is the list of the getter methods and the corresponding lazy members:
// - GetMozFullPathInternal - mMozFullPath
// - GetSize - mLength
// - GetType - mContentType
// - GetLastModified - mLastModifed
// FileBlobImpl has getter methods with lazy initialization. Because any
// BlobImpl must work thread-safe, we use a mutex.
Mutex mMutex;
nsCOMPtr<nsIFile> mFile;
nsString mContentType;
nsString mName;
nsString mPath; // The path relative to a directory chosen by the user
nsString mMozFullPath;
Maybe<uint64_t> mLength;
Maybe<int64_t> mLastModified;
const uint64_t mSerialNumber;
uint64_t mStart;
int64_t mFileId;
Maybe<uint64_t> mLength;
Maybe<int64_t> mLastModified;
bool mIsFile;
bool mWholeFile;
};

Просмотреть файл

@ -96,8 +96,7 @@ class TemporaryFileInputStream final : public nsFileInputStream {
TemporaryFileBlobImpl::TemporaryFileBlobImpl(nsIFile* aFile,
const nsAString& aContentType)
: FileBlobImpl(aFile, EmptyString(), aContentType,
NS_LITERAL_STRING("TemporaryBlobImpl"))
: FileBlobImpl(aFile, EmptyString(), aContentType)
#ifdef DEBUG
,
mInputStreamCreated(false)

Просмотреть файл

@ -30,6 +30,10 @@ class TemporaryFileBlobImpl final : public FileBlobImpl {
virtual void CreateInputStream(nsIInputStream** aInputStream,
ErrorResult& aRv) override;
void GetBlobImplType(nsAString& aBlobImplType) const override {
aBlobImplType = NS_LITERAL_STRING("TemporaryFileBlobImpl");
}
protected:
virtual ~TemporaryFileBlobImpl();