From 53a373287d35610a17d463ea58aec96137d83d7a Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Mon, 18 Jun 2018 16:37:21 -0400 Subject: [PATCH] Bug 1455256 - Port more components to WorkerRef - part 2 - IPCStream, r=asuth --- ipc/glue/IPCStreamSource.cpp | 39 +++++++++++------------------------- ipc/glue/IPCStreamSource.h | 14 +++---------- 2 files changed, 15 insertions(+), 38 deletions(-) diff --git a/ipc/glue/IPCStreamSource.cpp b/ipc/glue/IPCStreamSource.cpp index 48995e381b5f..8587d142e59e 100644 --- a/ipc/glue/IPCStreamSource.cpp +++ b/ipc/glue/IPCStreamSource.cpp @@ -14,8 +14,6 @@ #include "nsStreamUtils.h" #include "nsThreadUtils.h" -using mozilla::dom::WorkerPrivate; -using mozilla::dom::WorkerStatus; using mozilla::wr::ByteBuffer; namespace mozilla { @@ -43,7 +41,7 @@ public: // If this fails, then it means the owning thread is a Worker that has // been shutdown. Its ok to lose the event in this case because the - // IPCStreamChild listens for this event through the WorkerHolder. + // IPCStreamChild listens for this event through the WorkerRef. nsresult rv = mOwningEventTarget->Dispatch(this, nsIThread::DISPATCH_NORMAL); if (NS_FAILED(rv)) { NS_WARNING("Failed to dispatch stream readable event to owning thread"); @@ -67,7 +65,7 @@ public: { // Cancel() gets called when the Worker thread is being shutdown. We have // nothing to do here because IPCStreamChild handles this case via - // the WorkerHolder. + // the WorkerRef. return NS_OK; } @@ -103,9 +101,7 @@ NS_IMPL_ISUPPORTS(IPCStreamSource::Callback, nsIInputStreamCallback, nsICancelableRunnable); IPCStreamSource::IPCStreamSource(nsIAsyncInputStream* aInputStream) - : WorkerHolder("IPCStreamSource") - , mStream(aInputStream) - , mWorkerPrivate(nullptr) + : mStream(aInputStream) , mState(ePending) { MOZ_ASSERT(aInputStream); @@ -116,7 +112,7 @@ IPCStreamSource::~IPCStreamSource() NS_ASSERT_OWNINGTHREAD(IPCStreamSource); MOZ_ASSERT(mState == eClosed); MOZ_ASSERT(!mCallback); - MOZ_ASSERT(!mWorkerPrivate); + MOZ_ASSERT(!mWorkerRef); } bool @@ -133,19 +129,20 @@ IPCStreamSource::Initialize() // A source can be used on any thread, but we only support IPCStream on // main thread, Workers and PBackground thread right now. This is due // to the requirement that the thread be guaranteed to live long enough to - // receive messages. We can enforce this guarantee with a WorkerHolder on + // receive messages. We can enforce this guarantee with a StrongWorkerRef on // worker threads, but not other threads. Main-thread and PBackground thread // do not need anything special in order to be kept alive. - WorkerPrivate* workerPrivate = nullptr; if (!NS_IsMainThread()) { - workerPrivate = mozilla::dom::GetCurrentThreadWorkerPrivate(); + mozilla::dom::WorkerPrivate* workerPrivate = + mozilla::dom::GetCurrentThreadWorkerPrivate(); if (workerPrivate) { - bool result = HoldWorker(workerPrivate, WorkerStatus::Canceling); - if (!result) { + RefPtr workerRef = + mozilla::dom::StrongWorkerRef::Create(workerPrivate, "IPCStreamSource"); + if (NS_WARN_IF(!workerRef)) { return false; } - mWorkerPrivate = workerPrivate; + mWorkerRef = std::move(workerRef); } else { AssertIsOnBackgroundThread(); } @@ -161,15 +158,6 @@ IPCStreamSource::ActorConstructed() mState = eActorConstructed; } -bool -IPCStreamSource::Notify(WorkerStatus aStatus) -{ - NS_ASSERT_OWNINGTHREAD(IPCStreamSource); - - // Keep the worker thread alive until the stream is finished. - return true; -} - void IPCStreamSource::ActorDestroyed() { @@ -182,10 +170,7 @@ IPCStreamSource::ActorDestroyed() mCallback = nullptr; } - if (mWorkerPrivate) { - ReleaseWorker(); - mWorkerPrivate = nullptr; - } + mWorkerRef = nullptr; } void diff --git a/ipc/glue/IPCStreamSource.h b/ipc/glue/IPCStreamSource.h index ef3fb6ab006d..8001af6508b1 100644 --- a/ipc/glue/IPCStreamSource.h +++ b/ipc/glue/IPCStreamSource.h @@ -8,8 +8,7 @@ #define mozilla_ipc_IPCStreamSource_h #include "mozilla/AlreadyAddRefed.h" -#include "mozilla/dom/WorkerHolder.h" -#include "mozilla/dom/WorkerPrivate.h" +#include "mozilla/dom/WorkerRef.h" class nsIAsyncInputStream; @@ -56,7 +55,7 @@ class PParentToChildStreamParent; // // In general you should probably use the AutoIPCStreamSource RAII class // defined in InputStreamUtils.h instead of using IPCStreamSource directly. -class IPCStreamSource : public dom::WorkerHolder +class IPCStreamSource { public: // Create a IPCStreamSource using a PContent IPC manager on the @@ -123,10 +122,6 @@ protected: private: class Callback; - // WorkerHolder methods - virtual bool - Notify(dom::WorkerStatus aStatus) override; - void DoRead(); void Wait(); @@ -136,10 +131,7 @@ private: nsCOMPtr mStream; RefPtr mCallback; - // Raw pointer because this IPCStreamSource keeps the worker alive using a - // WorkerHolder. The worker is kept alive when the actor is created and, - // released when the actor is destroyed. - dom::WorkerPrivate* mWorkerPrivate; + RefPtr mWorkerRef; #ifdef DEBUG protected: