2016-09-29 08:12:00 +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_MutableBlobStorage_h
|
|
|
|
#define mozilla_dom_MutableBlobStorage_h
|
|
|
|
|
|
|
|
#include "mozilla/RefPtr.h"
|
2018-02-14 15:23:01 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2018-06-02 16:51:42 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2021-03-25 13:19:44 +03:00
|
|
|
#include "nsString.h"
|
2016-09-29 08:12:56 +03:00
|
|
|
#include "prio.h"
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2017-05-09 13:29:46 +03:00
|
|
|
class nsIEventTarget;
|
2017-10-05 08:41:41 +03:00
|
|
|
class nsIRunnable;
|
2017-05-09 13:29:46 +03:00
|
|
|
|
2016-09-29 08:12:00 +03:00
|
|
|
namespace mozilla {
|
2017-03-28 12:05:58 +03:00
|
|
|
|
|
|
|
class TaskQueue;
|
|
|
|
|
2016-09-29 08:12:00 +03:00
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class Blob;
|
|
|
|
class BlobImpl;
|
2016-09-29 08:12:56 +03:00
|
|
|
class MutableBlobStorage;
|
2017-10-05 08:41:42 +03:00
|
|
|
class TemporaryIPCBlobChild;
|
|
|
|
class TemporaryIPCBlobChildCallback;
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
class MutableBlobStorageCallback {
|
|
|
|
public:
|
2017-01-25 22:51:34 +03:00
|
|
|
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
|
2016-09-29 08:12:56 +03:00
|
|
|
|
2019-10-21 08:33:33 +03:00
|
|
|
virtual void BlobStoreCompleted(MutableBlobStorage* aBlobStorage,
|
|
|
|
BlobImpl* aBlob, nsresult aRv) = 0;
|
2016-09-29 08:12:56 +03:00
|
|
|
};
|
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
// This class is must be created and used on main-thread, except for Append()
|
|
|
|
// that can be called on any thread.
|
2017-10-24 13:02:39 +03:00
|
|
|
class MutableBlobStorage final {
|
2016-09-29 08:12:00 +03:00
|
|
|
public:
|
2017-10-24 13:02:39 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MutableBlobStorage)
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
enum MutableBlobStorageType {
|
|
|
|
eOnlyInMemory,
|
|
|
|
eCouldBeInTemporaryFile,
|
|
|
|
};
|
|
|
|
|
2017-05-09 13:29:46 +03:00
|
|
|
explicit MutableBlobStorage(MutableBlobStorageType aType,
|
2017-10-05 08:40:04 +03:00
|
|
|
nsIEventTarget* aEventTarget = nullptr,
|
|
|
|
uint32_t aMaxMemory = 0);
|
2016-09-29 08:12:00 +03:00
|
|
|
|
|
|
|
nsresult Append(const void* aData, uint32_t aLength);
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
// This method can be called just once.
|
2019-10-21 08:33:33 +03:00
|
|
|
// The callback will be called when the BlobImpl is ready.
|
|
|
|
void GetBlobImplWhenReady(const nsACString& aContentType,
|
|
|
|
MutableBlobStorageCallback* aCallback);
|
2016-09-29 08:12:56 +03:00
|
|
|
|
|
|
|
void TemporaryFileCreated(PRFileDesc* aFD);
|
|
|
|
|
2017-10-05 08:41:42 +03:00
|
|
|
void AskForBlob(TemporaryIPCBlobChildCallback* aCallback,
|
|
|
|
const nsACString& aContentType);
|
2016-09-29 08:12:56 +03:00
|
|
|
|
|
|
|
void ErrorPropagated(nsresult aRv);
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2017-05-09 13:29:46 +03:00
|
|
|
nsIEventTarget* EventTarget() {
|
|
|
|
MOZ_ASSERT(mEventTarget);
|
|
|
|
return mEventTarget;
|
|
|
|
}
|
|
|
|
|
2017-10-05 08:40:04 +03:00
|
|
|
// Returns the heap size in bytes of our internal buffers.
|
|
|
|
// Note that this intentionally ignores the data in the temp file.
|
2018-02-14 15:23:01 +03:00
|
|
|
size_t SizeOfCurrentMemoryBuffer();
|
2017-10-05 08:40:04 +03:00
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
PRFileDesc* GetFD();
|
2017-10-30 19:40:23 +03:00
|
|
|
|
|
|
|
void CloseFD();
|
|
|
|
|
2016-09-29 08:12:00 +03:00
|
|
|
private:
|
2016-09-29 08:12:56 +03:00
|
|
|
~MutableBlobStorage();
|
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
bool ExpandBufferSize(const MutexAutoLock& aProofOfLock, uint64_t aSize);
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
bool ShouldBeTemporaryStorage(const MutexAutoLock& aProofOfLock,
|
|
|
|
uint64_t aSize) const;
|
2016-09-29 08:12:56 +03:00
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
bool MaybeCreateTemporaryFile(const MutexAutoLock& aProofOfLock);
|
2018-12-19 17:48:29 +03:00
|
|
|
void MaybeCreateTemporaryFileOnMainThread(const MutexAutoLock& aProofOfLock);
|
2016-09-29 08:12:56 +03:00
|
|
|
|
2020-09-08 18:15:19 +03:00
|
|
|
[[nodiscard]] nsresult DispatchToIOThread(
|
|
|
|
already_AddRefed<nsIRunnable> aRunnable);
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2022-03-16 21:47:08 +03:00
|
|
|
Mutex mMutex MOZ_UNANNOTATED;
|
2018-02-14 15:23:01 +03:00
|
|
|
|
|
|
|
// All these variables are touched on the main thread only or in the
|
|
|
|
// retargeted thread when used by Append(). They are protected by mMutex.
|
2016-09-29 08:12:00 +03:00
|
|
|
|
|
|
|
void* mData;
|
|
|
|
uint64_t mDataLen;
|
|
|
|
uint64_t mDataBufferLen;
|
2016-09-29 08:12:56 +03:00
|
|
|
|
|
|
|
enum StorageState {
|
|
|
|
eKeepInMemory,
|
|
|
|
eInMemory,
|
|
|
|
eWaitingForTemporaryFile,
|
|
|
|
eInTemporaryFile,
|
|
|
|
eClosed
|
|
|
|
};
|
|
|
|
|
|
|
|
StorageState mStorageState;
|
|
|
|
|
|
|
|
PRFileDesc* mFD;
|
|
|
|
|
|
|
|
nsresult mErrorResult;
|
2017-03-28 12:05:58 +03:00
|
|
|
|
|
|
|
RefPtr<TaskQueue> mTaskQueue;
|
2017-05-09 13:29:46 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> mEventTarget;
|
2017-06-20 14:27:59 +03:00
|
|
|
|
|
|
|
nsCString mPendingContentType;
|
|
|
|
RefPtr<MutableBlobStorageCallback> mPendingCallback;
|
2017-10-05 08:40:04 +03:00
|
|
|
|
2017-10-05 08:41:42 +03:00
|
|
|
RefPtr<TemporaryIPCBlobChild> mActor;
|
|
|
|
|
2017-10-05 08:40:04 +03:00
|
|
|
// This value is used when we go from eInMemory to eWaitingForTemporaryFile
|
|
|
|
// and eventually eInTemporaryFile. If the size of the buffer is >=
|
|
|
|
// mMaxMemory, the creation of the temporary file will start.
|
|
|
|
// It's not used if mStorageState is eKeepInMemory.
|
|
|
|
uint32_t mMaxMemory;
|
2016-09-29 08:12:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_MutableBlobStorage_h
|