Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +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/. */
|
|
|
|
|
|
|
|
#include "IPCBlobInputStreamChild.h"
|
2017-05-31 08:41:10 +03:00
|
|
|
#include "IPCBlobInputStreamThread.h"
|
2017-05-23 19:06:52 +03:00
|
|
|
|
|
|
|
#include "mozilla/ipc/IPCStreamUtils.h"
|
2018-03-14 17:24:38 +03:00
|
|
|
#include "mozilla/dom/WorkerCommon.h"
|
|
|
|
#include "mozilla/dom/WorkerRef.h"
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This runnable is used in case the last stream is forgotten on the 'wrong'
|
|
|
|
// thread.
|
2017-05-22 14:33:00 +03:00
|
|
|
class ShutdownRunnable final : public CancelableRunnable {
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
public:
|
2017-05-19 15:00:31 +03:00
|
|
|
explicit ShutdownRunnable(IPCBlobInputStreamChild* aActor)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("dom::ShutdownRunnable"), mActor(aActor) {}
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
2017-05-19 15:00:31 +03:00
|
|
|
mActor->Shutdown();
|
2017-04-24 13:09:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<IPCBlobInputStreamChild> mActor;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This runnable is used in case StreamNeeded() has been called on a non-owning
|
|
|
|
// thread.
|
2017-05-22 14:33:00 +03:00
|
|
|
class StreamNeededRunnable final : public CancelableRunnable {
|
2017-04-24 13:09:40 +03:00
|
|
|
public:
|
|
|
|
explicit StreamNeededRunnable(IPCBlobInputStreamChild* aActor)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("dom::StreamNeededRunnable"), mActor(aActor) {}
|
2017-04-24 13:09:40 +03:00
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
2017-05-31 08:41:10 +03:00
|
|
|
MOZ_ASSERT(mActor->State() != IPCBlobInputStreamChild::eActiveMigrating &&
|
|
|
|
mActor->State() != IPCBlobInputStreamChild::eInactiveMigrating);
|
|
|
|
if (mActor->State() == IPCBlobInputStreamChild::eActive) {
|
2017-04-24 13:09:40 +03:00
|
|
|
mActor->SendStreamNeeded();
|
|
|
|
}
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<IPCBlobInputStreamChild> mActor;
|
|
|
|
};
|
|
|
|
|
2017-04-24 13:09:40 +03:00
|
|
|
// When the stream has been received from the parent, we inform the
|
|
|
|
// IPCBlobInputStream.
|
|
|
|
class StreamReadyRunnable final : public CancelableRunnable {
|
|
|
|
public:
|
|
|
|
StreamReadyRunnable(IPCBlobInputStream* aDestinationStream,
|
2017-10-05 08:38:48 +03:00
|
|
|
already_AddRefed<nsIInputStream> aCreatedStream)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("dom::StreamReadyRunnable"),
|
|
|
|
mDestinationStream(aDestinationStream),
|
2018-05-30 22:15:35 +03:00
|
|
|
mCreatedStream(std::move(aCreatedStream)) {
|
2017-04-24 13:09:40 +03:00
|
|
|
MOZ_ASSERT(mDestinationStream);
|
|
|
|
// mCreatedStream can be null.
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
2017-10-05 08:38:48 +03:00
|
|
|
mDestinationStream->StreamReady(mCreatedStream.forget());
|
2017-04-24 13:09:40 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<IPCBlobInputStream> mDestinationStream;
|
|
|
|
nsCOMPtr<nsIInputStream> mCreatedStream;
|
|
|
|
};
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
// This runnable is used in case LengthNeeded() has been called on a non-owning
|
|
|
|
// thread.
|
|
|
|
class LengthNeededRunnable final : public CancelableRunnable {
|
|
|
|
public:
|
|
|
|
explicit LengthNeededRunnable(IPCBlobInputStreamChild* aActor)
|
|
|
|
: CancelableRunnable("dom::LengthNeededRunnable"), mActor(aActor) {}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
MOZ_ASSERT(mActor->State() != IPCBlobInputStreamChild::eActiveMigrating &&
|
|
|
|
mActor->State() != IPCBlobInputStreamChild::eInactiveMigrating);
|
|
|
|
if (mActor->State() == IPCBlobInputStreamChild::eActive) {
|
|
|
|
mActor->SendLengthNeeded();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<IPCBlobInputStreamChild> mActor;
|
|
|
|
};
|
|
|
|
|
|
|
|
// When the stream has been received from the parent, we inform the
|
|
|
|
// IPCBlobInputStream.
|
|
|
|
class LengthReadyRunnable final : public CancelableRunnable {
|
|
|
|
public:
|
|
|
|
LengthReadyRunnable(IPCBlobInputStream* aDestinationStream, int64_t aSize)
|
|
|
|
: CancelableRunnable("dom::LengthReadyRunnable"),
|
|
|
|
mDestinationStream(aDestinationStream),
|
|
|
|
mSize(aSize) {
|
|
|
|
MOZ_ASSERT(mDestinationStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override {
|
|
|
|
mDestinationStream->LengthReady(mSize);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<IPCBlobInputStream> mDestinationStream;
|
|
|
|
int64_t mSize;
|
|
|
|
};
|
|
|
|
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
IPCBlobInputStreamChild::IPCBlobInputStreamChild(const nsID& aID,
|
|
|
|
uint64_t aSize)
|
|
|
|
: mMutex("IPCBlobInputStreamChild::mMutex"),
|
|
|
|
mID(aID),
|
|
|
|
mSize(aSize),
|
2017-05-31 08:41:10 +03:00
|
|
|
mState(eActive),
|
2017-06-01 23:42:05 +03:00
|
|
|
mOwningEventTarget(GetCurrentThreadSerialEventTarget()) {
|
2017-05-22 14:33:00 +03:00
|
|
|
// If we are running in a worker, we need to send a Close() to the parent side
|
|
|
|
// before the thread is released.
|
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
2018-03-14 17:24:38 +03:00
|
|
|
if (!workerPrivate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<StrongWorkerRef> workerRef =
|
|
|
|
StrongWorkerRef::Create(workerPrivate, "IPCBlobInputStreamChild");
|
|
|
|
if (!workerRef) {
|
|
|
|
return;
|
2017-05-22 14:33:00 +03:00
|
|
|
}
|
2018-03-14 17:24:38 +03:00
|
|
|
|
|
|
|
// We must keep the worker alive until the migration is completed.
|
|
|
|
mWorkerRef = new ThreadSafeWorkerRef(workerRef);
|
2017-05-22 14:33:00 +03:00
|
|
|
}
|
|
|
|
}
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
|
|
|
|
IPCBlobInputStreamChild::~IPCBlobInputStreamChild() {}
|
|
|
|
|
2017-05-19 15:00:31 +03:00
|
|
|
void IPCBlobInputStreamChild::Shutdown() {
|
2017-05-19 10:59:55 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-05-19 15:00:31 +03:00
|
|
|
|
|
|
|
RefPtr<IPCBlobInputStreamChild> kungFuDeathGrip = this;
|
|
|
|
|
2018-03-14 17:24:38 +03:00
|
|
|
mWorkerRef = nullptr;
|
2017-05-19 15:00:31 +03:00
|
|
|
mPendingOperations.Clear();
|
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
if (mState == eActive) {
|
2017-05-19 15:00:31 +03:00
|
|
|
SendClose();
|
2017-05-31 08:41:10 +03:00
|
|
|
mState = eInactive;
|
2017-05-19 15:00:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPCBlobInputStreamChild::ActorDestroy(
|
|
|
|
IProtocol::ActorDestroyReason aReason) {
|
2017-05-31 08:41:10 +03:00
|
|
|
bool migrating = false;
|
|
|
|
|
2017-05-19 15:00:31 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-05-31 08:41:10 +03:00
|
|
|
migrating = mState == eActiveMigrating;
|
|
|
|
mState = migrating ? eInactiveMigrating : eInactive;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (migrating) {
|
|
|
|
// We were waiting for this! Now we can migrate the actor in the correct
|
|
|
|
// thread.
|
|
|
|
RefPtr<IPCBlobInputStreamThread> thread =
|
|
|
|
IPCBlobInputStreamThread::GetOrCreate();
|
2017-09-14 15:28:29 +03:00
|
|
|
MOZ_ASSERT(thread, "We cannot continue without DOMFile thread.");
|
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
ResetManager();
|
|
|
|
thread->MigrateActor(this);
|
|
|
|
return;
|
2017-05-19 15:00:31 +03:00
|
|
|
}
|
|
|
|
|
2018-03-14 17:24:38 +03:00
|
|
|
// Let's cleanup the workerRef and the pending operation queue.
|
2017-05-19 15:00:31 +03:00
|
|
|
Shutdown();
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
}
|
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
IPCBlobInputStreamChild::ActorState IPCBlobInputStreamChild::State() {
|
2017-04-24 13:09:40 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-05-31 08:41:10 +03:00
|
|
|
return mState;
|
2017-04-24 13:09:40 +03:00
|
|
|
}
|
|
|
|
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
already_AddRefed<IPCBlobInputStream> IPCBlobInputStreamChild::CreateStream() {
|
2017-05-31 08:41:10 +03:00
|
|
|
bool shouldMigrate = false;
|
|
|
|
|
|
|
|
RefPtr<IPCBlobInputStream> stream = new IPCBlobInputStream(this);
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mState == eInactive) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The stream is active but maybe it is not running in the DOM-File thread.
|
|
|
|
// We should migrate it there.
|
|
|
|
if (mState == eActive &&
|
2017-06-01 23:42:05 +03:00
|
|
|
!IPCBlobInputStreamThread::IsOnFileEventTarget(mOwningEventTarget)) {
|
2017-05-31 08:41:10 +03:00
|
|
|
MOZ_ASSERT(mStreams.IsEmpty());
|
|
|
|
shouldMigrate = true;
|
|
|
|
mState = eActiveMigrating;
|
|
|
|
}
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
mStreams.AppendElement(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send__delete__ will call ActorDestroy(). mMutex cannot be locked at this
|
|
|
|
// time.
|
|
|
|
if (shouldMigrate) {
|
|
|
|
Send__delete__(this);
|
2017-05-19 15:00:31 +03:00
|
|
|
}
|
|
|
|
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPCBlobInputStreamChild::ForgetStream(IPCBlobInputStream* aStream) {
|
|
|
|
MOZ_ASSERT(aStream);
|
|
|
|
|
2017-05-19 15:00:31 +03:00
|
|
|
RefPtr<IPCBlobInputStreamChild> kungFuDeathGrip = this;
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mStreams.RemoveElement(aStream);
|
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
if (!mStreams.IsEmpty() || mState != eActive) {
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-01 23:42:05 +03:00
|
|
|
if (mOwningEventTarget->IsOnCurrentThread()) {
|
2017-05-19 15:00:31 +03:00
|
|
|
Shutdown();
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-19 15:00:31 +03:00
|
|
|
RefPtr<ShutdownRunnable> runnable = new ShutdownRunnable(this);
|
2017-06-01 23:42:05 +03:00
|
|
|
mOwningEventTarget->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 23:23:46 +03:00
|
|
|
void IPCBlobInputStreamChild::StreamNeeded(IPCBlobInputStream* aStream,
|
|
|
|
nsIEventTarget* aEventTarget) {
|
2017-04-24 13:09:40 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-05-19 15:00:31 +03:00
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
if (mState == eInactive) {
|
2017-05-19 15:00:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-24 13:09:40 +03:00
|
|
|
MOZ_ASSERT(mStreams.Contains(aStream));
|
|
|
|
|
|
|
|
PendingOperation* opt = mPendingOperations.AppendElement();
|
|
|
|
opt->mStream = aStream;
|
2017-09-13 16:29:39 +03:00
|
|
|
opt->mEventTarget = aEventTarget;
|
2018-05-23 08:12:35 +03:00
|
|
|
opt->mOp = PendingOperation::eStreamNeeded;
|
2017-04-24 13:09:40 +03:00
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
if (mState == eActiveMigrating || mState == eInactiveMigrating) {
|
|
|
|
// This operation will be continued when the migration is completed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == eActive);
|
|
|
|
|
2017-06-01 23:42:05 +03:00
|
|
|
if (mOwningEventTarget->IsOnCurrentThread()) {
|
2017-04-24 13:09:40 +03:00
|
|
|
SendStreamNeeded();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<StreamNeededRunnable> runnable = new StreamNeededRunnable(this);
|
2017-06-01 23:42:05 +03:00
|
|
|
mOwningEventTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
2017-04-24 13:09:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult IPCBlobInputStreamChild::RecvStreamReady(
|
|
|
|
const OptionalIPCStream& aStream) {
|
2017-05-23 19:06:52 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream = mozilla::ipc::DeserializeIPCStream(aStream);
|
2017-04-24 13:09:40 +03:00
|
|
|
|
2017-05-04 09:37:54 +03:00
|
|
|
RefPtr<IPCBlobInputStream> pendingStream;
|
|
|
|
nsCOMPtr<nsIEventTarget> eventTarget;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-08-29 12:10:57 +03:00
|
|
|
|
|
|
|
// We have been shutdown in the meantime.
|
|
|
|
if (mState == eInactive) {
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2017-05-04 09:37:54 +03:00
|
|
|
MOZ_ASSERT(!mPendingOperations.IsEmpty());
|
2017-05-31 08:41:10 +03:00
|
|
|
MOZ_ASSERT(mState == eActive);
|
2017-04-24 13:09:40 +03:00
|
|
|
|
2017-05-04 09:37:54 +03:00
|
|
|
pendingStream = mPendingOperations[0].mStream;
|
|
|
|
eventTarget = mPendingOperations[0].mEventTarget;
|
2018-05-23 08:12:35 +03:00
|
|
|
MOZ_ASSERT(mPendingOperations[0].mOp == PendingOperation::eStreamNeeded);
|
2017-05-04 09:37:54 +03:00
|
|
|
|
|
|
|
mPendingOperations.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<StreamReadyRunnable> runnable =
|
2017-10-05 08:38:48 +03:00
|
|
|
new StreamReadyRunnable(pendingStream, stream.forget());
|
2017-09-13 16:29:39 +03:00
|
|
|
|
|
|
|
// If IPCBlobInputStream::AsyncWait() has been executed without passing an
|
|
|
|
// event target, we run the callback synchronous because any thread could be
|
|
|
|
// result to be the wrong one. See more in nsIAsyncInputStream::asyncWait
|
|
|
|
// documentation.
|
|
|
|
if (eventTarget) {
|
|
|
|
eventTarget->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
|
|
|
} else {
|
|
|
|
runnable->Run();
|
|
|
|
}
|
2017-04-24 13:09:40 +03:00
|
|
|
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
void IPCBlobInputStreamChild::LengthNeeded(IPCBlobInputStream* aStream,
|
|
|
|
nsIEventTarget* aEventTarget) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (mState == eInactive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mStreams.Contains(aStream));
|
|
|
|
|
|
|
|
PendingOperation* opt = mPendingOperations.AppendElement();
|
|
|
|
opt->mStream = aStream;
|
|
|
|
opt->mEventTarget = aEventTarget;
|
|
|
|
opt->mOp = PendingOperation::eLengthNeeded;
|
|
|
|
|
|
|
|
if (mState == eActiveMigrating || mState == eInactiveMigrating) {
|
|
|
|
// This operation will be continued when the migration is completed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == eActive);
|
|
|
|
|
|
|
|
if (mOwningEventTarget->IsOnCurrentThread()) {
|
|
|
|
SendLengthNeeded();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<LengthNeededRunnable> runnable = new LengthNeededRunnable(this);
|
|
|
|
mOwningEventTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult IPCBlobInputStreamChild::RecvLengthReady(
|
|
|
|
const int64_t& aLength) {
|
|
|
|
RefPtr<IPCBlobInputStream> pendingStream;
|
|
|
|
nsCOMPtr<nsIEventTarget> eventTarget;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2018-08-29 12:10:57 +03:00
|
|
|
|
|
|
|
// We have been shutdown in the meantime.
|
|
|
|
if (mState == eInactive) {
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
MOZ_ASSERT(!mPendingOperations.IsEmpty());
|
|
|
|
MOZ_ASSERT(mState == eActive);
|
|
|
|
|
|
|
|
pendingStream = mPendingOperations[0].mStream;
|
|
|
|
eventTarget = mPendingOperations[0].mEventTarget;
|
|
|
|
MOZ_ASSERT(mPendingOperations[0].mOp == PendingOperation::eLengthNeeded);
|
|
|
|
|
|
|
|
mPendingOperations.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<LengthReadyRunnable> runnable =
|
|
|
|
new LengthReadyRunnable(pendingStream, aLength);
|
|
|
|
|
|
|
|
MOZ_ASSERT(eventTarget);
|
|
|
|
eventTarget->Dispatch(runnable, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
2017-05-31 08:41:10 +03:00
|
|
|
void IPCBlobInputStreamChild::Migrated() {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
MOZ_ASSERT(mState == eInactiveMigrating);
|
|
|
|
|
2018-03-14 17:24:38 +03:00
|
|
|
mWorkerRef = nullptr;
|
2017-05-31 08:41:11 +03:00
|
|
|
|
2017-06-01 23:42:05 +03:00
|
|
|
mOwningEventTarget = GetCurrentThreadSerialEventTarget();
|
|
|
|
MOZ_ASSERT(IPCBlobInputStreamThread::IsOnFileEventTarget(mOwningEventTarget));
|
2017-05-31 08:41:10 +03:00
|
|
|
|
|
|
|
// Maybe we have no reasons to keep this actor alive.
|
|
|
|
if (mStreams.IsEmpty()) {
|
|
|
|
mState = eInactive;
|
|
|
|
SendClose();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = eActive;
|
|
|
|
|
|
|
|
// Let's processing the pending operations. We need a stream for each pending
|
|
|
|
// operation.
|
|
|
|
for (uint32_t i = 0; i < mPendingOperations.Length(); ++i) {
|
2018-05-23 08:12:35 +03:00
|
|
|
if (mPendingOperations[i].mOp == PendingOperation::eStreamNeeded) {
|
|
|
|
SendStreamNeeded();
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mPendingOperations[i].mOp == PendingOperation::eLengthNeeded);
|
|
|
|
SendLengthNeeded();
|
|
|
|
}
|
2017-05-31 08:41:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1353629 - PBlob refactoring - part 4 - IPCBlobInputStream, r=smaug
IPCBlobInputStream is a new type of nsIInputStream that is used only in content
process when a Blob is sent from parent to child. This inputStream is for now,
just cloneable.
When the parent process sends a Blob to a content process, it has the Blob and
its inputStream. With its inputStream it creates a IPCBlobInputStreamParent
actor. This actor keeps the inputStream alive for following uses (not part of
this patch).
On the child side we will have, of course, a IPCBlobInputStreamChild actor.
This actor is able to create a IPCBlobInputStream when CreateStream() is
called. This means that 1 IPCBlobInputStreamChild can manage multiple
IPCBlobInputStreams each time one of them is cloned. When the last one of this
stream is released, the child actor sends a __delete__ request to the parent
side; the parent will be deleted, and the original inputStream, on the parent
side, will be released as well.
IPCBlobInputStream is a special inputStream because each method, except for
Available() fails. Basically, this inputStream cannot be used on the content
process for nothing else than knowing the size of the original stream.
In the following patches, I'll introduce an async way to use it.
2017-04-24 13:09:40 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|