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/. */
|
|
|
|
|
2018-06-02 16:51:42 +03:00
|
|
|
#include "EmptyBlobImpl.h"
|
2017-02-16 20:26:38 +03:00
|
|
|
#include "MutableBlobStorage.h"
|
|
|
|
#include "MemoryBlobImpl.h"
|
2016-09-29 08:12:00 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2019-04-26 21:01:53 +03:00
|
|
|
#include "mozilla/dom/TemporaryIPCBlobChild.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2017-10-05 08:41:42 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
2017-02-16 20:26:38 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2017-03-28 12:05:58 +03:00
|
|
|
#include "mozilla/TaskQueue.h"
|
2017-02-16 20:26:38 +03:00
|
|
|
#include "File.h"
|
2016-09-29 08:12:56 +03:00
|
|
|
#include "nsAnonymousTemporaryFile.h"
|
|
|
|
#include "nsNetCID.h"
|
2016-12-01 17:12:42 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2016-09-29 08:12:56 +03:00
|
|
|
|
|
|
|
#define BLOB_MEMORY_TEMPORARY_FILE 1048576
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This class uses the callback to inform when the Blob is created or when the
|
|
|
|
// error must be propagated.
|
|
|
|
class BlobCreationDoneRunnable final : public Runnable {
|
|
|
|
public:
|
|
|
|
BlobCreationDoneRunnable(MutableBlobStorage* aBlobStorage,
|
2019-10-21 08:33:33 +03:00
|
|
|
MutableBlobStorageCallback* aCallback,
|
|
|
|
BlobImpl* aBlobImpl, nsresult aRv)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::BlobCreationDoneRunnable"),
|
|
|
|
mBlobStorage(aBlobStorage),
|
2016-09-29 08:12:56 +03:00
|
|
|
mCallback(aCallback),
|
2019-10-21 08:33:33 +03:00
|
|
|
mBlobImpl(aBlobImpl),
|
2016-09-29 08:12:56 +03:00
|
|
|
mRv(aRv) {
|
|
|
|
MOZ_ASSERT(aBlobStorage);
|
|
|
|
MOZ_ASSERT(aCallback);
|
2019-10-21 08:33:33 +03:00
|
|
|
MOZ_ASSERT((NS_FAILED(aRv) && !aBlobImpl) ||
|
|
|
|
(NS_SUCCEEDED(aRv) && aBlobImpl));
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-09 13:29:46 +03:00
|
|
|
MOZ_ASSERT(mBlobStorage);
|
2019-10-21 08:33:33 +03:00
|
|
|
mCallback->BlobStoreCompleted(mBlobStorage, mBlobImpl, mRv);
|
2016-09-29 08:12:56 +03:00
|
|
|
mCallback = nullptr;
|
2019-10-21 08:33:33 +03:00
|
|
|
mBlobImpl = nullptr;
|
2016-09-29 08:12:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-05-06 12:00:27 +03:00
|
|
|
~BlobCreationDoneRunnable() override {
|
2017-05-09 13:29:46 +03:00
|
|
|
MOZ_ASSERT(mBlobStorage);
|
2016-09-29 08:12:56 +03:00
|
|
|
// If something when wrong, we still have to release these objects in the
|
|
|
|
// correct thread.
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ProxyRelease("BlobCreationDoneRunnable::mCallback",
|
|
|
|
mBlobStorage->EventTarget(), mCallback.forget());
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MutableBlobStorage> mBlobStorage;
|
|
|
|
RefPtr<MutableBlobStorageCallback> mCallback;
|
2019-10-21 08:33:33 +03:00
|
|
|
RefPtr<BlobImpl> mBlobImpl;
|
2016-09-29 08:12:56 +03:00
|
|
|
nsresult mRv;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Simple runnable to propagate the error to the BlobStorage.
|
|
|
|
class ErrorPropagationRunnable final : public Runnable {
|
|
|
|
public:
|
|
|
|
ErrorPropagationRunnable(MutableBlobStorage* aBlobStorage, nsresult aRv)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::ErrorPropagationRunnable"),
|
|
|
|
mBlobStorage(aBlobStorage),
|
2016-09-29 08:12:56 +03:00
|
|
|
mRv(aRv) {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
mBlobStorage->ErrorPropagated(mRv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<MutableBlobStorage> mBlobStorage;
|
|
|
|
nsresult mRv;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This runnable moves a buffer to the IO thread and there, it writes it into
|
2017-10-30 19:40:23 +03:00
|
|
|
// the temporary file, if its File Descriptor has not been already closed.
|
2016-09-29 08:12:56 +03:00
|
|
|
class WriteRunnable final : public Runnable {
|
|
|
|
public:
|
2017-10-30 19:40:23 +03:00
|
|
|
static WriteRunnable* CopyBuffer(MutableBlobStorage* aBlobStorage,
|
2016-09-29 08:12:56 +03:00
|
|
|
const void* aData, uint32_t aLength) {
|
|
|
|
MOZ_ASSERT(aBlobStorage);
|
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
|
|
|
|
// We have to take a copy of this buffer.
|
|
|
|
void* data = malloc(aLength);
|
|
|
|
if (!data) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy((char*)data, aData, aLength);
|
2017-10-30 19:40:23 +03:00
|
|
|
return new WriteRunnable(aBlobStorage, data, aLength);
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
2017-10-30 19:40:23 +03:00
|
|
|
static WriteRunnable* AdoptBuffer(MutableBlobStorage* aBlobStorage,
|
|
|
|
void* aData, uint32_t aLength) {
|
2016-09-29 08:12:56 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aBlobStorage);
|
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
|
2017-10-30 19:40:23 +03:00
|
|
|
return new WriteRunnable(aBlobStorage, aData, aLength);
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-05-09 13:29:46 +03:00
|
|
|
MOZ_ASSERT(mBlobStorage);
|
2016-09-29 08:12:56 +03:00
|
|
|
|
2017-10-30 19:40:23 +03:00
|
|
|
PRFileDesc* fd = mBlobStorage->GetFD();
|
|
|
|
if (!fd) {
|
|
|
|
// The file descriptor has been closed in the meantime.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t written = PR_Write(fd, mData, mLength);
|
2016-09-29 08:12:56 +03:00
|
|
|
if (NS_WARN_IF(written < 0 || uint32_t(written) != mLength)) {
|
2017-10-30 19:40:23 +03:00
|
|
|
mBlobStorage->CloseFD();
|
2017-05-09 13:29:46 +03:00
|
|
|
return mBlobStorage->EventTarget()->Dispatch(
|
|
|
|
new ErrorPropagationRunnable(mBlobStorage, NS_ERROR_FAILURE),
|
|
|
|
NS_DISPATCH_NORMAL);
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-10-30 19:40:23 +03:00
|
|
|
WriteRunnable(MutableBlobStorage* aBlobStorage, void* aData, uint32_t aLength)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::WriteRunnable"),
|
|
|
|
mBlobStorage(aBlobStorage),
|
2016-09-29 08:12:56 +03:00
|
|
|
mData(aData),
|
|
|
|
mLength(aLength) {
|
|
|
|
MOZ_ASSERT(mBlobStorage);
|
|
|
|
MOZ_ASSERT(aData);
|
|
|
|
}
|
|
|
|
|
2022-05-06 12:00:27 +03:00
|
|
|
~WriteRunnable() override { free(mData); }
|
2016-09-29 08:12:56 +03:00
|
|
|
|
|
|
|
RefPtr<MutableBlobStorage> mBlobStorage;
|
|
|
|
void* mData;
|
|
|
|
uint32_t mLength;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This runnable closes the FD in case something goes wrong or the temporary
|
|
|
|
// file is not needed anymore.
|
|
|
|
class CloseFileRunnable final : public Runnable {
|
|
|
|
public:
|
|
|
|
explicit CloseFileRunnable(PRFileDesc* aFD)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::CloseFileRunnable"), mFD(aFD) {}
|
2016-09-29 08:12:56 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
PR_Close(mFD);
|
|
|
|
mFD = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-05-06 12:00:27 +03:00
|
|
|
~CloseFileRunnable() override {
|
2016-09-29 08:12:56 +03:00
|
|
|
if (mFD) {
|
|
|
|
PR_Close(mFD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRFileDesc* mFD;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This runnable is dispatched to the main-thread from the IO thread and its
|
|
|
|
// task is to create the blob and inform the callback.
|
|
|
|
class CreateBlobRunnable final : public Runnable,
|
2017-10-05 08:41:42 +03:00
|
|
|
public TemporaryIPCBlobChildCallback {
|
2016-09-29 08:12:56 +03:00
|
|
|
public:
|
2018-02-12 23:44:40 +03:00
|
|
|
// We need to always declare refcounting because
|
|
|
|
// TemporaryIPCBlobChildCallback has pure-virtual refcounting.
|
2017-10-05 08:41:42 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
CreateBlobRunnable(MutableBlobStorage* aBlobStorage,
|
|
|
|
const nsACString& aContentType,
|
|
|
|
already_AddRefed<MutableBlobStorageCallback> aCallback)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::CreateBlobRunnable"),
|
|
|
|
mBlobStorage(aBlobStorage),
|
2016-09-29 08:12:56 +03:00
|
|
|
mContentType(aContentType),
|
|
|
|
mCallback(aCallback) {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-05-09 13:29:46 +03:00
|
|
|
MOZ_ASSERT(aBlobStorage);
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-09 13:29:46 +03:00
|
|
|
MOZ_ASSERT(mBlobStorage);
|
2017-10-05 08:41:42 +03:00
|
|
|
mBlobStorage->AskForBlob(this, mContentType);
|
2016-09-29 08:12:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-05 08:41:42 +03:00
|
|
|
void OperationSucceeded(BlobImpl* aBlobImpl) override {
|
2018-05-30 22:15:35 +03:00
|
|
|
RefPtr<MutableBlobStorageCallback> callback(std::move(mCallback));
|
2019-10-21 08:33:33 +03:00
|
|
|
callback->BlobStoreCompleted(mBlobStorage, aBlobImpl, NS_OK);
|
2017-10-05 08:41:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void OperationFailed(nsresult aRv) override {
|
2018-05-30 22:15:35 +03:00
|
|
|
RefPtr<MutableBlobStorageCallback> callback(std::move(mCallback));
|
2017-10-05 08:41:42 +03:00
|
|
|
callback->BlobStoreCompleted(mBlobStorage, nullptr, aRv);
|
|
|
|
}
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
private:
|
2022-05-06 12:00:27 +03:00
|
|
|
~CreateBlobRunnable() override {
|
2017-05-09 13:29:46 +03:00
|
|
|
MOZ_ASSERT(mBlobStorage);
|
2016-09-29 08:12:56 +03:00
|
|
|
// If something when wrong, we still have to release data in the correct
|
|
|
|
// thread.
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ProxyRelease("CreateBlobRunnable::mCallback",
|
|
|
|
mBlobStorage->EventTarget(), mCallback.forget());
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MutableBlobStorage> mBlobStorage;
|
|
|
|
nsCString mContentType;
|
|
|
|
RefPtr<MutableBlobStorageCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
2017-10-05 08:41:42 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(CreateBlobRunnable, Runnable)
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
// This task is used to know when the writing is completed. From the IO thread
|
|
|
|
// it dispatches a CreateBlobRunnable to the main-thread.
|
|
|
|
class LastRunnable final : public Runnable {
|
|
|
|
public:
|
2019-10-21 08:33:33 +03:00
|
|
|
LastRunnable(MutableBlobStorage* aBlobStorage, const nsACString& aContentType,
|
2016-09-29 08:12:56 +03:00
|
|
|
MutableBlobStorageCallback* aCallback)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::LastRunnable"),
|
|
|
|
mBlobStorage(aBlobStorage),
|
2016-09-29 08:12:56 +03:00
|
|
|
mContentType(aContentType),
|
|
|
|
mCallback(aCallback) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mBlobStorage);
|
|
|
|
MOZ_ASSERT(aCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-10-05 08:41:42 +03:00
|
|
|
|
2019-10-21 08:33:33 +03:00
|
|
|
RefPtr<Runnable> runnable =
|
|
|
|
new CreateBlobRunnable(mBlobStorage, mContentType, mCallback.forget());
|
2017-05-09 13:29:46 +03:00
|
|
|
return mBlobStorage->EventTarget()->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-05-06 12:00:27 +03:00
|
|
|
~LastRunnable() override {
|
2017-05-09 13:29:46 +03:00
|
|
|
MOZ_ASSERT(mBlobStorage);
|
2016-09-29 08:12:56 +03:00
|
|
|
// If something when wrong, we still have to release data in the correct
|
|
|
|
// thread.
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ProxyRelease("LastRunnable::mCallback", mBlobStorage->EventTarget(),
|
|
|
|
mCallback.forget());
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MutableBlobStorage> mBlobStorage;
|
|
|
|
nsCString mContentType;
|
|
|
|
RefPtr<MutableBlobStorageCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-05-09 13:29:46 +03:00
|
|
|
MutableBlobStorage::MutableBlobStorage(MutableBlobStorageType aType,
|
2017-10-05 08:40:04 +03:00
|
|
|
nsIEventTarget* aEventTarget,
|
|
|
|
uint32_t aMaxMemory)
|
2018-02-14 15:23:01 +03:00
|
|
|
: mMutex("MutableBlobStorage::mMutex"),
|
|
|
|
mData(nullptr),
|
2016-09-29 08:12:56 +03:00
|
|
|
mDataLen(0),
|
|
|
|
mDataBufferLen(0),
|
|
|
|
mStorageState(aType == eOnlyInMemory ? eKeepInMemory : eInMemory),
|
|
|
|
mFD(nullptr),
|
|
|
|
mErrorResult(NS_OK),
|
2017-05-09 13:29:46 +03:00
|
|
|
mEventTarget(aEventTarget),
|
2017-10-05 08:40:04 +03:00
|
|
|
mMaxMemory(aMaxMemory) {
|
2016-09-29 08:12:56 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-05-09 13:29:46 +03:00
|
|
|
|
|
|
|
if (!mEventTarget) {
|
2023-01-17 02:14:12 +03:00
|
|
|
mEventTarget = GetMainThreadSerialEventTarget();
|
2017-05-09 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
2017-10-05 08:40:04 +03:00
|
|
|
if (aMaxMemory == 0 && aType == eCouldBeInTemporaryFile) {
|
|
|
|
mMaxMemory = Preferences::GetUint("dom.blob.memoryToTemporaryFile",
|
|
|
|
BLOB_MEMORY_TEMPORARY_FILE);
|
|
|
|
}
|
|
|
|
|
2017-05-09 13:29:46 +03:00
|
|
|
MOZ_ASSERT(mEventTarget);
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MutableBlobStorage::~MutableBlobStorage() {
|
|
|
|
free(mData);
|
|
|
|
|
|
|
|
if (mFD) {
|
2017-03-28 12:05:58 +03:00
|
|
|
RefPtr<Runnable> runnable = new CloseFileRunnable(mFD);
|
2023-05-22 15:21:43 +03:00
|
|
|
(void)DispatchToIOThread(runnable.forget());
|
2017-03-28 12:05:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mTaskQueue) {
|
|
|
|
mTaskQueue->BeginShutdown();
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
2017-10-05 08:41:42 +03:00
|
|
|
|
|
|
|
if (mActor) {
|
|
|
|
NS_ProxyRelease("MutableBlobStorage::mActor", EventTarget(),
|
|
|
|
mActor.forget());
|
|
|
|
}
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
2019-10-21 08:33:33 +03:00
|
|
|
void MutableBlobStorage::GetBlobImplWhenReady(
|
|
|
|
const nsACString& aContentType, MutableBlobStorageCallback* aCallback) {
|
2016-09-29 08:12:56 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aCallback);
|
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
// GetBlob can be called just once.
|
|
|
|
MOZ_ASSERT(mStorageState != eClosed);
|
|
|
|
StorageState previousState = mStorageState;
|
|
|
|
mStorageState = eClosed;
|
|
|
|
|
|
|
|
if (previousState == eInTemporaryFile) {
|
|
|
|
if (NS_FAILED(mErrorResult)) {
|
2017-10-05 08:41:42 +03:00
|
|
|
MOZ_ASSERT(!mActor);
|
|
|
|
|
2017-03-28 12:05:58 +03:00
|
|
|
RefPtr<Runnable> runnable =
|
|
|
|
new BlobCreationDoneRunnable(this, aCallback, nullptr, mErrorResult);
|
2017-05-09 13:29:46 +03:00
|
|
|
EventTarget()->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
2017-09-29 10:19:13 +03:00
|
|
|
return;
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
2017-10-05 08:41:42 +03:00
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
// We want to wait until all the WriteRunnable are completed. The way we do
|
|
|
|
// this is to go to the I/O thread and then we come back: the runnables are
|
|
|
|
// executed in order and this LastRunnable will be... the last one.
|
2017-10-05 08:41:42 +03:00
|
|
|
// This Runnable will also close the FD on the I/O thread.
|
2019-10-21 08:33:33 +03:00
|
|
|
RefPtr<Runnable> runnable = new LastRunnable(this, aContentType, aCallback);
|
2017-11-15 09:58:03 +03:00
|
|
|
|
|
|
|
// If the dispatching fails, we are shutting down and it's fine to do not
|
|
|
|
// run the callback.
|
2023-05-22 15:21:43 +03:00
|
|
|
(void)DispatchToIOThread(runnable.forget());
|
2017-09-29 10:19:13 +03:00
|
|
|
return;
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
2017-06-20 14:27:59 +03:00
|
|
|
// If we are waiting for the temporary file, it's better to wait...
|
|
|
|
if (previousState == eWaitingForTemporaryFile) {
|
|
|
|
mPendingContentType = aContentType;
|
|
|
|
mPendingCallback = aCallback;
|
2017-09-29 10:19:13 +03:00
|
|
|
return;
|
2017-06-20 14:27:59 +03:00
|
|
|
}
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
RefPtr<BlobImpl> blobImpl;
|
|
|
|
|
|
|
|
if (mData) {
|
2017-02-16 20:26:38 +03:00
|
|
|
blobImpl = new MemoryBlobImpl(mData, mDataLen,
|
2016-09-29 08:12:56 +03:00
|
|
|
NS_ConvertUTF8toUTF16(aContentType));
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2017-02-16 20:26:38 +03:00
|
|
|
mData = nullptr; // The MemoryBlobImpl takes ownership of the buffer
|
2016-09-29 08:12:56 +03:00
|
|
|
mDataLen = 0;
|
|
|
|
mDataBufferLen = 0;
|
|
|
|
} else {
|
|
|
|
blobImpl = new EmptyBlobImpl(NS_ConvertUTF8toUTF16(aContentType));
|
2016-09-29 08:12:00 +03:00
|
|
|
}
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
RefPtr<BlobCreationDoneRunnable> runnable =
|
2019-10-21 08:33:33 +03:00
|
|
|
new BlobCreationDoneRunnable(this, aCallback, blobImpl, NS_OK);
|
2016-09-29 08:12:56 +03:00
|
|
|
|
2017-05-09 13:29:46 +03:00
|
|
|
nsresult error =
|
|
|
|
EventTarget()->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
2016-09-29 08:12:56 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(error))) {
|
2017-09-29 10:19:13 +03:00
|
|
|
return;
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
2016-09-29 08:12:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult MutableBlobStorage::Append(const void* aData, uint32_t aLength) {
|
2018-02-14 15:23:01 +03:00
|
|
|
// This method can be called on any thread.
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
2016-09-29 08:12:56 +03:00
|
|
|
MOZ_ASSERT(mStorageState != eClosed);
|
2016-09-29 08:12:00 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aData);
|
2016-09-29 08:12:56 +03:00
|
|
|
|
2016-09-29 08:12:00 +03:00
|
|
|
if (!aLength) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
// If eInMemory is the current Storage state, we could maybe migrate to
|
|
|
|
// a temporary file.
|
2018-02-14 15:23:01 +03:00
|
|
|
if (mStorageState == eInMemory && ShouldBeTemporaryStorage(lock, aLength) &&
|
|
|
|
!MaybeCreateTemporaryFile(lock)) {
|
2017-10-25 09:45:52 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are already in the temporaryFile mode, we have to dispatch a
|
|
|
|
// runnable.
|
|
|
|
if (mStorageState == eInTemporaryFile) {
|
2017-10-31 01:33:18 +03:00
|
|
|
// If a previous operation failed, let's return that error now.
|
|
|
|
if (NS_FAILED(mErrorResult)) {
|
|
|
|
return mErrorResult;
|
|
|
|
}
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
RefPtr<WriteRunnable> runnable =
|
2017-10-30 19:40:23 +03:00
|
|
|
WriteRunnable::CopyBuffer(this, aData, aLength);
|
2016-09-29 08:12:56 +03:00
|
|
|
if (NS_WARN_IF(!runnable)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2017-11-15 09:58:03 +03:00
|
|
|
nsresult rv = DispatchToIOThread(runnable.forget());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-09-29 08:12:56 +03:00
|
|
|
|
|
|
|
mDataLen += aLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// By default, we store in memory.
|
|
|
|
|
2016-09-29 08:12:00 +03:00
|
|
|
uint64_t offset = mDataLen;
|
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
if (!ExpandBufferSize(lock, aLength)) {
|
2016-09-29 08:12:00 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy((char*)mData + offset, aData, aLength);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
bool MutableBlobStorage::ExpandBufferSize(const MutexAutoLock& aProofOfLock,
|
|
|
|
uint64_t aSize) {
|
2016-09-29 08:12:56 +03:00
|
|
|
MOZ_ASSERT(mStorageState < eInTemporaryFile);
|
|
|
|
|
2016-09-29 08:12:00 +03:00
|
|
|
if (mDataBufferLen >= mDataLen + aSize) {
|
|
|
|
mDataLen += aSize;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start at 1 or we'll loop forever.
|
|
|
|
CheckedUint32 bufferLen =
|
|
|
|
std::max<uint32_t>(static_cast<uint32_t>(mDataBufferLen), 1);
|
|
|
|
while (bufferLen.isValid() && bufferLen.value() < mDataLen + aSize) {
|
|
|
|
bufferLen *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bufferLen.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* data = realloc(mData, bufferLen.value());
|
|
|
|
if (!data) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mData = data;
|
|
|
|
mDataBufferLen = bufferLen.value();
|
|
|
|
mDataLen += aSize;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
bool MutableBlobStorage::ShouldBeTemporaryStorage(
|
|
|
|
const MutexAutoLock& aProofOfLock, uint64_t aSize) const {
|
2016-09-29 08:12:56 +03:00
|
|
|
MOZ_ASSERT(mStorageState == eInMemory);
|
|
|
|
|
|
|
|
CheckedUint32 bufferSize = mDataLen;
|
|
|
|
bufferSize += aSize;
|
|
|
|
|
|
|
|
if (!bufferSize.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-05 08:40:04 +03:00
|
|
|
return bufferSize.value() >= mMaxMemory;
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
bool MutableBlobStorage::MaybeCreateTemporaryFile(
|
|
|
|
const MutexAutoLock& aProofOfLock) {
|
2017-10-24 13:02:39 +03:00
|
|
|
mStorageState = eWaitingForTemporaryFile;
|
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
RefPtr<MutableBlobStorage> self = this;
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"MutableBlobStorage::MaybeCreateTemporaryFile", [self]() {
|
2018-12-19 17:48:29 +03:00
|
|
|
MutexAutoLock lock(self->mMutex);
|
|
|
|
self->MaybeCreateTemporaryFileOnMainThread(lock);
|
|
|
|
if (!self->mActor) {
|
|
|
|
self->ErrorPropagated(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
EventTarget()->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
|
|
|
|
return true;
|
2018-02-14 15:23:01 +03:00
|
|
|
}
|
|
|
|
|
2018-12-19 17:48:29 +03:00
|
|
|
MaybeCreateTemporaryFileOnMainThread(aProofOfLock);
|
2018-02-14 15:23:01 +03:00
|
|
|
return !!mActor;
|
|
|
|
}
|
|
|
|
|
2018-12-19 17:48:29 +03:00
|
|
|
void MutableBlobStorage::MaybeCreateTemporaryFileOnMainThread(
|
|
|
|
const MutexAutoLock& aProofOfLock) {
|
2018-02-14 15:23:01 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mActor);
|
|
|
|
|
2017-10-24 13:02:39 +03:00
|
|
|
mozilla::ipc::PBackgroundChild* actorChild =
|
|
|
|
mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread();
|
|
|
|
if (NS_WARN_IF(!actorChild)) {
|
2018-02-14 15:23:01 +03:00
|
|
|
return;
|
2017-10-05 08:41:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mActor = new TemporaryIPCBlobChild(this);
|
2017-10-24 13:02:39 +03:00
|
|
|
actorChild->SendPTemporaryIPCBlobConstructor(mActor);
|
2017-10-05 08:41:42 +03:00
|
|
|
|
|
|
|
// We need manually to increase the reference for this actor because the
|
|
|
|
// IPC allocator method is not triggered. The Release() is called by IPDL
|
|
|
|
// when the actor is deleted.
|
|
|
|
mActor.get()->AddRef();
|
|
|
|
|
|
|
|
// The actor will call us when the FileDescriptor is received.
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MutableBlobStorage::TemporaryFileCreated(PRFileDesc* aFD) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-02-14 15:23:01 +03:00
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
2016-09-29 08:12:56 +03:00
|
|
|
MOZ_ASSERT(mStorageState == eWaitingForTemporaryFile ||
|
|
|
|
mStorageState == eClosed);
|
2017-06-20 14:27:59 +03:00
|
|
|
MOZ_ASSERT_IF(mPendingCallback, mStorageState == eClosed);
|
2017-10-05 08:41:42 +03:00
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
MOZ_ASSERT(aFD);
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2017-06-20 14:27:59 +03:00
|
|
|
// If the object has been already closed and we don't need to execute a
|
|
|
|
// callback, we need just to close the file descriptor in the correct thread.
|
|
|
|
if (mStorageState == eClosed && !mPendingCallback) {
|
2017-03-28 12:05:58 +03:00
|
|
|
RefPtr<Runnable> runnable = new CloseFileRunnable(aFD);
|
2017-11-15 09:58:03 +03:00
|
|
|
|
|
|
|
// If this dispatching fails, CloseFileRunnable will close the FD in the
|
|
|
|
// DTOR on the current thread.
|
2023-05-22 15:21:43 +03:00
|
|
|
(void)DispatchToIOThread(runnable.forget());
|
2017-10-05 08:41:42 +03:00
|
|
|
|
|
|
|
// Let's inform the parent that we have nothing else to do.
|
2017-12-15 00:51:40 +03:00
|
|
|
mActor->SendOperationFailed();
|
2017-10-05 08:41:42 +03:00
|
|
|
mActor = nullptr;
|
2016-09-29 08:12:56 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-09-29 08:12:00 +03:00
|
|
|
|
2017-06-20 14:27:59 +03:00
|
|
|
// If we still receiving data, we can proceed in temporary-file mode.
|
|
|
|
if (mStorageState == eWaitingForTemporaryFile) {
|
|
|
|
mStorageState = eInTemporaryFile;
|
|
|
|
}
|
|
|
|
|
2016-09-29 08:12:56 +03:00
|
|
|
mFD = aFD;
|
2017-10-31 01:33:18 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mErrorResult));
|
2016-09-29 08:12:56 +03:00
|
|
|
|
2017-06-20 14:27:59 +03:00
|
|
|
// This runnable takes the ownership of mData and it will write this buffer
|
|
|
|
// into the temporary file.
|
2016-09-29 08:12:56 +03:00
|
|
|
RefPtr<WriteRunnable> runnable =
|
2017-10-30 19:40:23 +03:00
|
|
|
WriteRunnable::AdoptBuffer(this, mData, mDataLen);
|
2016-09-29 08:12:56 +03:00
|
|
|
MOZ_ASSERT(runnable);
|
|
|
|
|
|
|
|
mData = nullptr;
|
|
|
|
|
2017-11-15 09:58:03 +03:00
|
|
|
nsresult rv = DispatchToIOThread(runnable.forget());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
// Shutting down, we cannot continue.
|
|
|
|
return;
|
|
|
|
}
|
2017-06-20 14:27:59 +03:00
|
|
|
|
2019-10-21 08:33:33 +03:00
|
|
|
// If we are closed, it means that GetBlobImplWhenReady() has been called when
|
|
|
|
// we were already waiting for a temporary file-descriptor. Finally we are
|
|
|
|
// here, AdoptBuffer runnable is going to write the current buffer into this
|
|
|
|
// file. After that, there is nothing else to write, and we dispatch
|
|
|
|
// LastRunnable which ends up calling mPendingCallback via CreateBlobRunnable.
|
2017-06-20 14:27:59 +03:00
|
|
|
if (mStorageState == eClosed) {
|
|
|
|
MOZ_ASSERT(mPendingCallback);
|
|
|
|
|
2019-10-21 08:33:33 +03:00
|
|
|
RefPtr<Runnable> runnable =
|
|
|
|
new LastRunnable(this, mPendingContentType, mPendingCallback);
|
2023-05-22 15:21:43 +03:00
|
|
|
(void)DispatchToIOThread(runnable.forget());
|
2017-06-20 14:27:59 +03:00
|
|
|
|
|
|
|
mPendingCallback = nullptr;
|
|
|
|
}
|
2016-09-29 08:12:00 +03:00
|
|
|
}
|
|
|
|
|
2017-10-05 08:41:42 +03:00
|
|
|
void MutableBlobStorage::AskForBlob(TemporaryIPCBlobChildCallback* aCallback,
|
|
|
|
const nsACString& aContentType) {
|
2016-09-29 08:12:56 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-02-14 15:23:01 +03:00
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
2016-09-29 08:12:56 +03:00
|
|
|
MOZ_ASSERT(mStorageState == eClosed);
|
2017-12-15 00:51:40 +03:00
|
|
|
MOZ_ASSERT(mFD);
|
2017-10-05 08:41:42 +03:00
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
MOZ_ASSERT(aCallback);
|
2016-09-29 08:12:56 +03:00
|
|
|
|
2017-12-15 00:51:40 +03:00
|
|
|
// Let's pass the FileDescriptor to the parent actor in order to keep the file
|
|
|
|
// locked on windows.
|
|
|
|
mActor->AskForBlob(aCallback, aContentType, mFD);
|
|
|
|
|
|
|
|
// The previous operation has duplicated the file descriptor. Now we can close
|
|
|
|
// mFD. The parent will take care of closing the duplicated file descriptor on
|
|
|
|
// its side.
|
|
|
|
RefPtr<Runnable> runnable = new CloseFileRunnable(mFD);
|
2023-05-22 15:21:43 +03:00
|
|
|
(void)DispatchToIOThread(runnable.forget());
|
2017-12-15 00:51:40 +03:00
|
|
|
|
|
|
|
mFD = nullptr;
|
2017-10-05 08:41:42 +03:00
|
|
|
mActor = nullptr;
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MutableBlobStorage::ErrorPropagated(nsresult aRv) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-02-14 15:23:01 +03:00
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
2016-09-29 08:12:56 +03:00
|
|
|
mErrorResult = aRv;
|
2017-10-05 08:41:42 +03:00
|
|
|
|
2017-10-27 10:04:28 +03:00
|
|
|
if (mActor) {
|
2017-12-15 00:51:40 +03:00
|
|
|
mActor->SendOperationFailed();
|
2017-10-27 10:04:28 +03:00
|
|
|
mActor = nullptr;
|
|
|
|
}
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
2017-03-28 12:05:58 +03:00
|
|
|
nsresult MutableBlobStorage::DispatchToIOThread(
|
|
|
|
already_AddRefed<nsIRunnable> aRunnable) {
|
|
|
|
if (!mTaskQueue) {
|
|
|
|
nsCOMPtr<nsIEventTarget> target =
|
|
|
|
do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
|
|
|
|
MOZ_ASSERT(target);
|
2016-09-29 08:12:56 +03:00
|
|
|
|
2022-05-02 23:37:34 +03:00
|
|
|
mTaskQueue = TaskQueue::Create(target.forget(), "BlobStorage");
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
2017-03-28 12:05:58 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(aRunnable);
|
2017-11-15 09:58:03 +03:00
|
|
|
nsresult rv = mTaskQueue->Dispatch(runnable.forget());
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2016-09-29 08:12:56 +03:00
|
|
|
}
|
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
size_t MutableBlobStorage::SizeOfCurrentMemoryBuffer() {
|
2017-10-05 08:40:04 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-02-14 15:23:01 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-10-05 08:40:04 +03:00
|
|
|
return mStorageState < eInTemporaryFile ? mDataLen : 0;
|
|
|
|
}
|
|
|
|
|
2018-02-14 15:23:01 +03:00
|
|
|
PRFileDesc* MutableBlobStorage::GetFD() {
|
2017-10-30 19:40:23 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2018-02-14 15:23:01 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-10-30 19:40:23 +03:00
|
|
|
return mFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MutableBlobStorage::CloseFD() {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2018-02-14 15:23:01 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-10-30 19:40:23 +03:00
|
|
|
MOZ_ASSERT(mFD);
|
|
|
|
|
|
|
|
PR_Close(mFD);
|
|
|
|
mFD = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|