mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-28 07:13:20 +00:00
Bug 1455256 - Port more components to WorkerRef - part 2 - IPCStream, r=asuth
This commit is contained in:
parent
037d3a703f
commit
53a373287d
@ -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<mozilla::dom::StrongWorkerRef> 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
|
||||
|
@ -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<nsIAsyncInputStream> mStream;
|
||||
RefPtr<Callback> 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<dom::StrongWorkerRef> mWorkerRef;
|
||||
|
||||
#ifdef DEBUG
|
||||
protected:
|
||||
|
Loading…
Reference in New Issue
Block a user