diff --git a/dom/file/MutableBlobStorage.cpp b/dom/file/MutableBlobStorage.cpp index 1549880d646f..6050a2af2e62 100644 --- a/dom/file/MutableBlobStorage.cpp +++ b/dom/file/MutableBlobStorage.cpp @@ -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 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 mBlobStorage; @@ -324,19 +333,21 @@ public: Run() override { MOZ_ASSERT(!NS_IsMainThread()); + MOZ_ASSERT(mBlobStorage); RefPtr 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 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 = 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 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); } }); } diff --git a/dom/file/MutableBlobStorage.h b/dom/file/MutableBlobStorage.h index 422f941a6902..a948918a1ceb 100644 --- a/dom/file/MutableBlobStorage.h +++ b/dom/file/MutableBlobStorage.h @@ -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 mTaskQueue; + nsCOMPtr mEventTarget; }; } // namespace dom diff --git a/dom/file/MutableBlobStreamListener.cpp b/dom/file/MutableBlobStreamListener.cpp index 6769ad3e66f5..152429fd0add 100644 --- a/dom/file/MutableBlobStreamListener.cpp +++ b/dom/file/MutableBlobStreamListener.cpp @@ -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; } diff --git a/dom/file/MutableBlobStreamListener.h b/dom/file/MutableBlobStreamListener.h index 1dc044c61d83..47b36f044d8c 100644 --- a/dom/file/MutableBlobStreamListener.h +++ b/dom/file/MutableBlobStreamListener.h @@ -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 mParent; MutableBlobStorage::MutableBlobStorageType mStorageType; nsCString mContentType; + nsCOMPtr mEventTarget; }; } // namespace dom