1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_FileBlobImpl_h
8 #define mozilla_dom_FileBlobImpl_h
10 #include "mozilla/dom/BindingDeclarations.h"
11 #include "mozilla/dom/BlobImpl.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/Mutex.h"
18 namespace mozilla::dom
{
20 class FileBlobImpl
: public BlobImpl
{
22 NS_INLINE_DECL_REFCOUNTING_INHERITED(FileBlobImpl
, BlobImpl
)
25 explicit FileBlobImpl(nsIFile
* aFile
);
28 FileBlobImpl(const nsAString
& aName
, const nsAString
& aContentType
,
29 uint64_t aLength
, nsIFile
* aFile
);
31 FileBlobImpl(const nsAString
& aName
, const nsAString
& aContentType
,
32 uint64_t aLength
, nsIFile
* aFile
, int64_t aLastModificationDate
);
34 // Create as a file with custom name
35 FileBlobImpl(nsIFile
* aFile
, const nsAString
& aName
,
36 const nsAString
& aContentType
);
38 void GetName(nsAString
& aName
) const override
{
39 MOZ_ASSERT(mIsFile
, "Should only be called on files");
43 void SetName(const nsAString
& aName
) { mName
= aName
; }
45 void GetDOMPath(nsAString
& aPath
) const override
{
46 MOZ_ASSERT(mIsFile
, "Should only be called on files");
50 void SetDOMPath(const nsAString
& aPath
) override
{
51 MOZ_ASSERT(mIsFile
, "Should only be called on files");
55 int64_t GetLastModified(ErrorResult
& aRv
) override
;
57 void GetMozFullPath(nsAString
& aFileName
, SystemCallerGuarantee
/* unused */,
58 ErrorResult
& aRv
) override
{
59 MOZ_ASSERT(mIsFile
, "Should only be called on files");
61 GetMozFullPathInternal(aFileName
, aRv
);
64 void GetMozFullPathInternal(nsAString
& aFilename
, ErrorResult
& aRv
) override
;
66 uint64_t GetSize(ErrorResult
& aRv
) override
;
68 void GetType(nsAString
& aType
) override
;
70 void GetBlobImplType(nsAString
& aBlobImplType
) const override
;
72 size_t GetAllocationSize() const override
{ return 0; }
74 size_t GetAllocationSize(
75 FallibleTArray
<BlobImpl
*>& aVisitedBlobImpls
) const override
{
76 return GetAllocationSize();
79 uint64_t GetSerialNumber() const override
{ return mSerialNumber
; }
81 const nsTArray
<RefPtr
<BlobImpl
>>* GetSubBlobImpls() const override
{
85 void CreateInputStream(nsIInputStream
** aStream
,
86 ErrorResult
& aRv
) const override
;
88 int64_t GetFileId() const override
{ return mFileId
; }
90 void SetLazyData(const nsAString
& aName
, const nsAString
& aContentType
,
91 uint64_t aLength
, int64_t aLastModifiedDate
) override
{
93 mContentType
= aContentType
;
94 mIsFile
= !aName
.IsVoid();
95 mLength
.emplace(aLength
);
96 mLastModified
.emplace(aLastModifiedDate
);
99 bool IsMemoryFile() const override
{ return false; }
101 bool IsFile() const override
{ return mIsFile
; }
103 bool IsDirectory() const override
;
105 void SetType(const nsAString
& aType
) { mContentType
= aType
; }
107 void SetFileId(int64_t aFileId
) { mFileId
= aFileId
; }
109 void SetEmptySize() { mLength
.emplace(0); }
111 void SetMozFullPath(const nsAString
& aPath
) { mMozFullPath
= aPath
; }
113 void SetLastModified(int64_t aLastModified
) {
114 mLastModified
.emplace(aLastModified
);
118 ~FileBlobImpl() override
= default;
121 FileBlobImpl(const FileBlobImpl
* aOther
, uint64_t aStart
, uint64_t aLength
,
122 const nsAString
& aContentType
);
124 already_AddRefed
<BlobImpl
> CreateSlice(uint64_t aStart
, uint64_t aLength
,
125 const nsAString
& aContentType
,
126 ErrorResult
& aRv
) const override
;
128 class GetTypeRunnable
;
129 void GetTypeInternal(nsAString
& aType
, const MutexAutoLock
& aProofOfLock
);
131 // FileBlobImpl has getter methods with lazy initialization. Because any
132 // BlobImpl must work thread-safe, we use a mutex.
133 Mutex mMutex MOZ_UNANNOTATED
;
135 nsCOMPtr
<nsIFile
> mFile
;
137 nsString mContentType
;
139 nsString mPath
; // The path relative to a directory chosen by the user
140 nsString mMozFullPath
;
142 const uint64_t mSerialNumber
;
147 Maybe
<uint64_t> mLength
;
149 Maybe
<int64_t> mLastModified
;
155 } // namespace mozilla::dom
157 #endif // mozilla_dom_FileBlobImpl_h