Backed out changeset 51d87c2129d2 (bug 1865372) for causing RunWatchdog crashes in...
[gecko.git] / dom / file / FileBlobImpl.h
blob84565dbd4be1af873b0c13335c12d07fd4ecae70
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"
14 #include "nsCOMPtr.h"
16 class nsIFile;
18 namespace mozilla::dom {
20 class FileBlobImpl : public BlobImpl {
21 public:
22 NS_INLINE_DECL_REFCOUNTING_INHERITED(FileBlobImpl, BlobImpl)
24 // Create as a file
25 explicit FileBlobImpl(nsIFile* aFile);
27 // Create as a file
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");
40 aName = mName;
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");
47 aPath = mPath;
50 void SetDOMPath(const nsAString& aPath) override {
51 MOZ_ASSERT(mIsFile, "Should only be called on files");
52 mPath = aPath;
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 {
82 return nullptr;
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 {
92 mName = aName;
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);
117 protected:
118 ~FileBlobImpl() override = default;
120 // Create slice
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;
138 nsString mName;
139 nsString mPath; // The path relative to a directory chosen by the user
140 nsString mMozFullPath;
142 const uint64_t mSerialNumber;
143 uint64_t mStart;
145 int64_t mFileId;
147 Maybe<uint64_t> mLength;
149 Maybe<int64_t> mLastModified;
151 bool mIsFile;
152 bool mWholeFile;
155 } // namespace mozilla::dom
157 #endif // mozilla_dom_FileBlobImpl_h