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"
|
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
|
|
|
|
|
|
|
virtual void BlobStoreCompleted(MutableBlobStorage* aBlobStorage,
|
|
|
|
Blob* aBlob,
|
|
|
|
nsresult aRv) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This class is main-thread only.
|
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
|
2016-09-29 08:12:00 +03:00
|
|
|
{
|
2016-09-29 08:12:56 +03:00
|
|
|
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.
|
|
|
|
// The callback will be called when the Blob is ready.
|
2017-09-29 10:19:13 +03:00
|
|
|
void GetBlobWhenReady(nsISupports* aParent,
|
|
|
|
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.
|
|
|
|
size_t SizeOfCurrentMemoryBuffer() const;
|
|
|
|
|
2017-10-30 19:40:23 +03:00
|
|
|
PRFileDesc* GetFD() const;
|
|
|
|
|
|
|
|
void CloseFD();
|
|
|
|
|
2016-09-29 08:12:00 +03:00
|
|
|
private:
|
2016-09-29 08:12:56 +03:00
|
|
|
~MutableBlobStorage();
|
|
|
|
|
2016-09-29 08:12:00 +03:00
|
|
|
bool ExpandBufferSize(uint64_t aSize);
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
bool ShouldBeTemporaryStorage(uint64_t aSize) const;
|
|
|
|
|
2017-10-25 09:45:52 +03:00
|
|
|
bool MaybeCreateTemporaryFile();
|
2016-09-29 08:12:56 +03:00
|
|
|
|
2017-11-15 09:58:03 +03:00
|
|
|
MOZ_MUST_USE nsresult
|
|
|
|
DispatchToIOThread(already_AddRefed<nsIRunnable> aRunnable);
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
// All these variables are touched on the main thread only.
|
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
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> mPendingParent;
|
|
|
|
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
|