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 "IPCBlobInputStreamParent.h"
|
2017-04-24 13:09:40 +03:00
|
|
|
#include "IPCBlobInputStreamStorage.h"
|
2017-05-23 19:06:52 +03:00
|
|
|
#include "mozilla/ipc/IPCStreamUtils.h"
|
2018-05-23 08:12:35 +03:00
|
|
|
#include "mozilla/InputStreamLengthHelper.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
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-04-24 13:09:40 +03:00
|
|
|
template <typename M>
|
2018-05-23 08:12:35 +03:00
|
|
|
/* static */ already_AddRefed<IPCBlobInputStreamParent>
|
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
|
|
|
IPCBlobInputStreamParent::Create(nsIInputStream* aInputStream, uint64_t aSize,
|
2017-05-31 08:41:11 +03:00
|
|
|
uint64_t aChildID, nsresult* aRv,
|
|
|
|
M* aManager) {
|
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
|
|
|
MOZ_ASSERT(aInputStream);
|
|
|
|
MOZ_ASSERT(aRv);
|
|
|
|
|
|
|
|
nsID id;
|
|
|
|
*aRv = nsContentUtils::GenerateUUIDInPlace(id);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(*aRv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-09-11 18:29:44 +03:00
|
|
|
IPCBlobInputStreamStorage::Get()->AddStream(aInputStream, id, aSize,
|
|
|
|
aChildID);
|
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
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
RefPtr<IPCBlobInputStreamParent> parent =
|
|
|
|
new IPCBlobInputStreamParent(id, aSize, aManager);
|
|
|
|
return parent.forget();
|
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
|
|
|
}
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
/* static */ already_AddRefed<IPCBlobInputStreamParent>
|
2017-05-31 08:41:10 +03:00
|
|
|
IPCBlobInputStreamParent::Create(const nsID& aID, uint64_t aSize,
|
|
|
|
PBackgroundParent* aManager) {
|
2018-05-23 08:12:35 +03:00
|
|
|
RefPtr<IPCBlobInputStreamParent> actor =
|
2017-05-31 08:41:10 +03:00
|
|
|
new IPCBlobInputStreamParent(aID, aSize, aManager);
|
|
|
|
|
|
|
|
actor->mCallback = IPCBlobInputStreamStorage::Get()->TakeCallback(aID);
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
return actor.forget();
|
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
|
|
|
IPCBlobInputStreamParent::IPCBlobInputStreamParent(const nsID& aID,
|
2017-04-24 13:09:40 +03:00
|
|
|
uint64_t aSize,
|
2019-02-25 19:19:38 +03:00
|
|
|
nsIContentParent* aManager)
|
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
|
|
|
: mID(aID),
|
|
|
|
mSize(aSize),
|
2017-04-24 13:09:40 +03:00
|
|
|
mContentManager(aManager),
|
|
|
|
mPBackgroundManager(nullptr),
|
2017-05-31 08:41:10 +03:00
|
|
|
mMigrating(false) {}
|
2017-04-24 13:09:40 +03:00
|
|
|
|
|
|
|
IPCBlobInputStreamParent::IPCBlobInputStreamParent(const nsID& aID,
|
|
|
|
uint64_t aSize,
|
|
|
|
PBackgroundParent* aManager)
|
|
|
|
: mID(aID),
|
|
|
|
mSize(aSize),
|
|
|
|
mContentManager(nullptr),
|
|
|
|
mPBackgroundManager(aManager),
|
2017-05-31 08:41:10 +03:00
|
|
|
mMigrating(false) {}
|
2018-11-30 13:46:48 +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
|
|
|
void IPCBlobInputStreamParent::ActorDestroy(
|
|
|
|
IProtocol::ActorDestroyReason aReason) {
|
2017-04-24 13:09:40 +03:00
|
|
|
MOZ_ASSERT(mContentManager || mPBackgroundManager);
|
|
|
|
|
|
|
|
mContentManager = nullptr;
|
|
|
|
mPBackgroundManager = nullptr;
|
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
RefPtr<IPCBlobInputStreamParentCallback> callback;
|
|
|
|
mCallback.swap(callback);
|
2017-05-18 10:50:57 +03:00
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
RefPtr<IPCBlobInputStreamStorage> storage = IPCBlobInputStreamStorage::Get();
|
2017-05-18 10:50:57 +03:00
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
if (mMigrating) {
|
|
|
|
if (callback && storage) {
|
|
|
|
// We need to assign this callback to the next parent.
|
|
|
|
IPCBlobInputStreamStorage::Get()->StoreCallback(mID, callback);
|
2017-05-31 08:41:10 +03:00
|
|
|
}
|
2017-05-31 08:41:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (storage) {
|
|
|
|
storage->ForgetStream(mID);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
callback->ActorDestroyed(mID);
|
2017-05-18 10:50:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPCBlobInputStreamParent::SetCallback(
|
|
|
|
IPCBlobInputStreamParentCallback* aCallback) {
|
|
|
|
MOZ_ASSERT(aCallback);
|
|
|
|
MOZ_ASSERT(!mCallback);
|
|
|
|
|
|
|
|
mCallback = aCallback;
|
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-24 13:09:40 +03:00
|
|
|
mozilla::ipc::IPCResult IPCBlobInputStreamParent::RecvStreamNeeded() {
|
|
|
|
MOZ_ASSERT(mContentManager || mPBackgroundManager);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2017-09-11 18:29:44 +03:00
|
|
|
IPCBlobInputStreamStorage::Get()->GetStream(mID, 0, mSize,
|
|
|
|
getter_AddRefs(stream));
|
2017-04-24 13:09:40 +03:00
|
|
|
if (!stream) {
|
2019-02-22 21:46:14 +03:00
|
|
|
if (!SendStreamReady(Nothing())) {
|
2017-04-24 13:09:40 +03:00
|
|
|
return IPC_FAIL(this, "SendStreamReady failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2017-05-23 19:06:52 +03:00
|
|
|
mozilla::ipc::AutoIPCStream ipcStream;
|
2017-04-24 13:09:40 +03:00
|
|
|
bool ok = false;
|
|
|
|
|
|
|
|
if (mContentManager) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
ok = ipcStream.Serialize(stream, mContentManager);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mPBackgroundManager);
|
|
|
|
ok = ipcStream.Serialize(stream, mPBackgroundManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!ok)) {
|
|
|
|
return IPC_FAIL(this, "SendStreamReady failed");
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:46:14 +03:00
|
|
|
if (!SendStreamReady(Some(ipcStream.TakeValue()))) {
|
2017-04-24 13:09:40 +03:00
|
|
|
return IPC_FAIL(this, "SendStreamReady failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
mozilla::ipc::IPCResult IPCBlobInputStreamParent::RecvLengthNeeded() {
|
|
|
|
MOZ_ASSERT(mContentManager || mPBackgroundManager);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
IPCBlobInputStreamStorage::Get()->GetStream(mID, 0, mSize,
|
|
|
|
getter_AddRefs(stream));
|
|
|
|
if (!stream) {
|
|
|
|
if (!SendLengthReady(-1)) {
|
|
|
|
return IPC_FAIL(this, "SendLengthReady failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2018-05-23 08:12:36 +03:00
|
|
|
int64_t length = -1;
|
|
|
|
if (InputStreamLengthHelper::GetSyncLength(stream, &length)) {
|
|
|
|
Unused << SendLengthReady(length);
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2018-05-23 08:12:35 +03:00
|
|
|
RefPtr<IPCBlobInputStreamParent> self = this;
|
2018-05-23 08:12:36 +03:00
|
|
|
InputStreamLengthHelper::GetAsyncLength(stream, [self](int64_t aLength) {
|
2018-05-23 08:12:35 +03:00
|
|
|
if (self->mContentManager || self->mPBackgroundManager) {
|
|
|
|
Unused << self->SendLengthReady(aLength);
|
2018-05-23 08:12:35 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2017-05-19 15:00:31 +03:00
|
|
|
mozilla::ipc::IPCResult IPCBlobInputStreamParent::RecvClose() {
|
|
|
|
MOZ_ASSERT(mContentManager || mPBackgroundManager);
|
|
|
|
|
|
|
|
Unused << Send__delete__(this);
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
2017-05-31 08:41:10 +03:00
|
|
|
mozilla::ipc::IPCResult IPCBlobInputStreamParent::Recv__delete__() {
|
|
|
|
MOZ_ASSERT(mContentManager || mPBackgroundManager);
|
|
|
|
mMigrating = true;
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPCBlobInputStreamParent::HasValidStream() const {
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2017-09-11 18:29:44 +03:00
|
|
|
IPCBlobInputStreamStorage::Get()->GetStream(mID, 0, mSize,
|
|
|
|
getter_AddRefs(stream));
|
2017-05-31 08:41:10 +03:00
|
|
|
return !!stream;
|
|
|
|
}
|
|
|
|
|
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
|