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_MutableBlobStorage_h
8 #define mozilla_dom_MutableBlobStorage_h
10 #include "mozilla/RefPtr.h"
11 #include "mozilla/Mutex.h"
27 class MutableBlobStorage
;
28 class TemporaryIPCBlobChild
;
29 class TemporaryIPCBlobChildCallback
;
31 class MutableBlobStorageCallback
{
33 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
35 virtual void BlobStoreCompleted(MutableBlobStorage
* aBlobStorage
,
36 BlobImpl
* aBlob
, nsresult aRv
) = 0;
39 // This class is must be created and used on main-thread, except for Append()
40 // that can be called on any thread.
41 class MutableBlobStorage final
{
43 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MutableBlobStorage
)
45 enum MutableBlobStorageType
{
47 eCouldBeInTemporaryFile
,
50 explicit MutableBlobStorage(MutableBlobStorageType aType
,
51 nsIEventTarget
* aEventTarget
= nullptr,
52 uint32_t aMaxMemory
= 0);
54 nsresult
Append(const void* aData
, uint32_t aLength
);
56 // This method can be called just once.
57 // The callback will be called when the BlobImpl is ready.
58 void GetBlobImplWhenReady(const nsACString
& aContentType
,
59 MutableBlobStorageCallback
* aCallback
);
61 void TemporaryFileCreated(PRFileDesc
* aFD
);
63 void AskForBlob(TemporaryIPCBlobChildCallback
* aCallback
,
64 const nsACString
& aContentType
);
66 void ErrorPropagated(nsresult aRv
);
68 nsIEventTarget
* EventTarget() {
69 MOZ_ASSERT(mEventTarget
);
73 // Returns the heap size in bytes of our internal buffers.
74 // Note that this intentionally ignores the data in the temp file.
75 size_t SizeOfCurrentMemoryBuffer();
82 ~MutableBlobStorage();
84 bool ExpandBufferSize(const MutexAutoLock
& aProofOfLock
, uint64_t aSize
);
86 bool ShouldBeTemporaryStorage(const MutexAutoLock
& aProofOfLock
,
87 uint64_t aSize
) const;
89 bool MaybeCreateTemporaryFile(const MutexAutoLock
& aProofOfLock
);
90 void MaybeCreateTemporaryFileOnMainThread(const MutexAutoLock
& aProofOfLock
);
92 [[nodiscard
]] nsresult
DispatchToIOThread(
93 already_AddRefed
<nsIRunnable
> aRunnable
);
95 Mutex mMutex MOZ_UNANNOTATED
;
97 // All these variables are touched on the main thread only or in the
98 // retargeted thread when used by Append(). They are protected by mMutex.
102 uint64_t mDataBufferLen
;
107 eWaitingForTemporaryFile
,
112 StorageState mStorageState
;
116 nsresult mErrorResult
;
118 RefPtr
<TaskQueue
> mTaskQueue
;
119 nsCOMPtr
<nsIEventTarget
> mEventTarget
;
121 nsCString mPendingContentType
;
122 RefPtr
<MutableBlobStorageCallback
> mPendingCallback
;
124 RefPtr
<TemporaryIPCBlobChild
> mActor
;
126 // This value is used when we go from eInMemory to eWaitingForTemporaryFile
127 // and eventually eInTemporaryFile. If the size of the buffer is >=
128 // mMaxMemory, the creation of the temporary file will start.
129 // It's not used if mStorageState is eKeepInMemory.
134 } // namespace mozilla
136 #endif // mozilla_dom_MutableBlobStorage_h