Bug 1362944 - Part 1: Accept nsIEventTarget in the CTOR of MutableBlobStorage to support runnable labeling. r=baku

1. Accept nsIEventTarget in the CTOR of MutableBlobStorage/MutableBlobStreamListener.
2. Do ProxyRelease and runnable dispatching via this event target.
This commit is contained in:
Bevis Tseng 2017-05-09 18:29:46 +08:00
parent 89fee9ade2
commit 32c0a177ae
4 changed files with 61 additions and 18 deletions

View File

@ -46,6 +46,7 @@ public:
Run() override
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mBlobStorage);
mCallback->BlobStoreCompleted(mBlobStorage, mBlob, mRv);
mCallback = nullptr;
mBlob = nullptr;
@ -55,10 +56,11 @@ public:
private:
~BlobCreationDoneRunnable()
{
MOZ_ASSERT(mBlobStorage);
// If something when wrong, we still have to release these objects in the
// correct thread.
NS_ReleaseOnMainThread(mCallback.forget());
NS_ReleaseOnMainThread(mBlob.forget());
NS_ProxyRelease(mBlobStorage->EventTarget(), mCallback.forget());
NS_ProxyRelease(mBlobStorage->EventTarget(), mBlob.forget());
}
RefPtr<MutableBlobStorage> mBlobStorage;
@ -120,6 +122,7 @@ public:
{
MOZ_ASSERT(!NS_IsMainThread());
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(mBlobStorage);
PRFileDesc* tempFD = nullptr;
nsresult rv = NS_OpenAnonymousTemporaryFile(&tempFD);
@ -128,7 +131,8 @@ public:
}
// The ownership of the tempFD is moved to the FileCreatedRunnable.
return NS_DispatchToMainThread(new FileCreatedRunnable(mBlobStorage, tempFD));
return mBlobStorage->EventTarget()->Dispatch(
new FileCreatedRunnable(mBlobStorage, tempFD), NS_DISPATCH_NORMAL);
}
private:
@ -196,11 +200,13 @@ public:
Run() override
{
MOZ_ASSERT(!NS_IsMainThread());
MOZ_ASSERT(mBlobStorage);
int32_t written = PR_Write(mFD, mData, mLength);
if (NS_WARN_IF(written < 0 || uint32_t(written) != mLength)) {
return NS_DispatchToMainThread(
new ErrorPropagationRunnable(mBlobStorage, NS_ERROR_FAILURE));
return mBlobStorage->EventTarget()->Dispatch(
new ErrorPropagationRunnable(mBlobStorage, NS_ERROR_FAILURE),
NS_DISPATCH_NORMAL);
}
return NS_OK;
@ -275,12 +281,14 @@ public:
, mCallback(aCallback)
{
MOZ_ASSERT(!NS_IsMainThread());
MOZ_ASSERT(aBlobStorage);
}
NS_IMETHOD
Run() override
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mBlobStorage);
mBlobStorage->CreateBlobAndRespond(mParent.forget(), mContentType,
mCallback.forget());
return NS_OK;
@ -289,10 +297,11 @@ public:
private:
~CreateBlobRunnable()
{
MOZ_ASSERT(mBlobStorage);
// If something when wrong, we still have to release data in the correct
// thread.
NS_ReleaseOnMainThread(mParent.forget());
NS_ReleaseOnMainThread(mCallback.forget());
NS_ProxyRelease(mBlobStorage->EventTarget(), mParent.forget());
NS_ProxyRelease(mBlobStorage->EventTarget(), mCallback.forget());
}
RefPtr<MutableBlobStorage> mBlobStorage;
@ -324,19 +333,21 @@ public:
Run() override
{
MOZ_ASSERT(!NS_IsMainThread());
MOZ_ASSERT(mBlobStorage);
RefPtr<Runnable> runnable =
new CreateBlobRunnable(mBlobStorage, mParent.forget(),
mContentType, mCallback.forget());
return NS_DispatchToMainThread(runnable);
return mBlobStorage->EventTarget()->Dispatch(runnable, NS_DISPATCH_NORMAL);
}
private:
~LastRunnable()
{
MOZ_ASSERT(mBlobStorage);
// If something when wrong, we still have to release data in the correct
// thread.
NS_ReleaseOnMainThread(mParent.forget());
NS_ReleaseOnMainThread(mCallback.forget());
NS_ProxyRelease(mBlobStorage->EventTarget(), mParent.forget());
NS_ProxyRelease(mBlobStorage->EventTarget(), mCallback.forget());
}
RefPtr<MutableBlobStorage> mBlobStorage;
@ -347,15 +358,23 @@ private:
} // anonymous namespace
MutableBlobStorage::MutableBlobStorage(MutableBlobStorageType aType)
MutableBlobStorage::MutableBlobStorage(MutableBlobStorageType aType,
nsIEventTarget* aEventTarget)
: mData(nullptr)
, mDataLen(0)
, mDataBufferLen(0)
, mStorageState(aType == eOnlyInMemory ? eKeepInMemory : eInMemory)
, mFD(nullptr)
, mErrorResult(NS_OK)
, mEventTarget(aEventTarget)
{
MOZ_ASSERT(NS_IsMainThread());
if (!mEventTarget) {
mEventTarget = do_GetMainThread();
}
MOZ_ASSERT(mEventTarget);
}
MutableBlobStorage::~MutableBlobStorage()
@ -391,7 +410,7 @@ MutableBlobStorage::GetBlobWhenReady(nsISupports* aParent,
if (NS_FAILED(mErrorResult)) {
RefPtr<Runnable> runnable =
new BlobCreationDoneRunnable(this, aCallback, nullptr, mErrorResult);
NS_DispatchToMainThread(runnable.forget());
EventTarget()->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
return 0;
}
@ -421,7 +440,7 @@ MutableBlobStorage::GetBlobWhenReady(nsISupports* aParent,
RefPtr<BlobCreationDoneRunnable> runnable =
new BlobCreationDoneRunnable(this, aCallback, blob, NS_OK);
nsresult error = NS_DispatchToMainThread(runnable);
nsresult error = EventTarget()->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
if (NS_WARN_IF(NS_FAILED(error))) {
return 0;
}
@ -539,7 +558,8 @@ MutableBlobStorage::MaybeCreateTemporaryFile()
AsyncOpenAnonymousTemporaryFile([self](PRFileDesc* prfile) {
if (prfile) {
// The ownership of the prfile is moved to the FileCreatedRunnable.
NS_DispatchToMainThread(new FileCreatedRunnable(self, prfile));
self->EventTarget()->Dispatch(
new FileCreatedRunnable(self, prfile), NS_DISPATCH_NORMAL);
}
});
}

View File

@ -10,6 +10,8 @@
#include "mozilla/RefPtr.h"
#include "prio.h"
class nsIEventTarget;
namespace mozilla {
class TaskQueue;
@ -42,7 +44,8 @@ public:
eCouldBeInTemporaryFile,
};
explicit MutableBlobStorage(MutableBlobStorageType aType);
explicit MutableBlobStorage(MutableBlobStorageType aType,
nsIEventTarget* aEventTarget = nullptr);
nsresult Append(const void* aData, uint32_t aLength);
@ -61,6 +64,12 @@ public:
void ErrorPropagated(nsresult aRv);
nsIEventTarget* EventTarget()
{
MOZ_ASSERT(mEventTarget);
return mEventTarget;
}
private:
~MutableBlobStorage();
@ -93,6 +102,7 @@ private:
nsresult mErrorResult;
RefPtr<TaskQueue> mTaskQueue;
nsCOMPtr<nsIEventTarget> mEventTarget;
};
} // namespace dom

View File

@ -12,14 +12,22 @@ namespace dom {
MutableBlobStreamListener::MutableBlobStreamListener(MutableBlobStorage::MutableBlobStorageType aStorageType,
nsISupports* aParent,
const nsACString& aContentType,
MutableBlobStorageCallback* aCallback)
MutableBlobStorageCallback* aCallback,
nsIEventTarget* aEventTarget)
: mCallback(aCallback)
, mParent(aParent)
, mStorageType(aStorageType)
, mContentType(aContentType)
, mEventTarget(aEventTarget)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aCallback);
if (!mEventTarget) {
mEventTarget = do_GetMainThread();
}
MOZ_ASSERT(mEventTarget);
}
MutableBlobStreamListener::~MutableBlobStreamListener()
@ -36,8 +44,9 @@ MutableBlobStreamListener::OnStartRequest(nsIRequest* aRequest, nsISupports* aCo
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(!mStorage);
MOZ_ASSERT(mEventTarget);
mStorage = new MutableBlobStorage(mStorageType);
mStorage = new MutableBlobStorage(mStorageType, mEventTarget);
return NS_OK;
}

View File

@ -10,6 +10,8 @@
#include "nsIStreamListener.h"
#include "mozilla/dom/MutableBlobStorage.h"
class nsIEventTarget;
namespace mozilla {
namespace dom {
@ -24,7 +26,8 @@ public:
MutableBlobStreamListener(MutableBlobStorage::MutableBlobStorageType aType,
nsISupports* aParent,
const nsACString& aContentType,
MutableBlobStorageCallback* aCallback);
MutableBlobStorageCallback* aCallback,
nsIEventTarget* aEventTarget = nullptr);
private:
~MutableBlobStreamListener();
@ -40,6 +43,7 @@ private:
nsCOMPtr<nsISupports> mParent;
MutableBlobStorage::MutableBlobStorageType mStorageType;
nsCString mContentType;
nsCOMPtr<nsIEventTarget> mEventTarget;
};
} // namespace dom