Bug 1353475 - Implement AutoIPCStream with delayed start - part 2 - delayed start, r=smaug

This commit is contained in:
Andrea Marchesini committed 2017-04-24 12:09:39 +02:00
1 parent 1f0f35f669
commit 03ce8cb8ee
9 files changed
+412 -37

No files matched your search

+6
View File
@@ -27,6 +27,12 @@ union IPCRemoteStreamType
struct IPCRemoteStream
{
// If this is true, the stream will send data only when the first operation
// is done on the destination side. The benefit of this is that we do not
// send data if not needed. On the other hand, it could have a performance
// issue.
bool delayedStart;
IPCRemoteStreamType stream;
};
+14
View File
@@ -47,6 +47,13 @@ public:
ActorDestroyed();
}
IPCResult
RecvStartReading() override
{
Start();
return IPC_OK();
}
IPCResult
RecvRequestClose(const nsresult& aRv) override
{
@@ -167,6 +174,13 @@ private:
// IPCStreamDestination methods
void
StartReading() override
{
MOZ_ASSERT(HasDelayedStart());
Unused << SendStartReading();
}
void
RequestClose(nsresult aRv) override
{
+275
View File
@@ -5,14 +5,256 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "IPCStreamDestination.h"
#include "mozilla/Mutex.h"
#include "nsIAsyncInputStream.h"
#include "nsIAsyncOutputStream.h"
#include "nsIBufferedStreams.h"
#include "nsICloneableInputStream.h"
#include "nsIPipe.h"
namespace mozilla {
namespace ipc {
// ----------------------------------------------------------------------------
// IPCStreamDestination::DelayedStartInputStream
//
// When AutoIPCStream is used with delayedStart, we need to ask for data at the
// first real use of the nsIInputStream. In order to do so, we wrap the
// nsIInputStream, created by the nsIPipe, with this wrapper.
class IPCStreamDestination::DelayedStartInputStream final
: public nsIAsyncInputStream
, public nsISearchableInputStream
, public nsICloneableInputStream
, public nsIBufferedInputStream
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
DelayedStartInputStream(IPCStreamDestination* aDestination,
already_AddRefed<nsIAsyncInputStream>&& aStream)
: mDestination(aDestination)
, mStream(aStream)
, mMutex("IPCStreamDestination::DelayedStartInputStream::mMutex")
{
MOZ_ASSERT(mDestination);
MOZ_ASSERT(mStream);
}
void
DestinationShutdown()
{
MutexAutoLock lock(mMutex);
mDestination = nullptr;
}
// nsIInputStream interface
NS_IMETHOD
Close() override
{
MaybeCloseDestination();
return mStream->Close();
}
NS_IMETHOD
Available(uint64_t* aLength) override
{
MaybeStartReading();
return mStream->Available(aLength);
}
NS_IMETHOD
Read(char* aBuffer, uint32_t aCount, uint32_t* aReadCount) override
{
MaybeStartReading();
return mStream->Read(aBuffer, aCount, aReadCount);
}
NS_IMETHOD
ReadSegments(nsWriteSegmentFun aWriter, void* aClosure, uint32_t aCount,
uint32_t *aResult) override
{
MaybeStartReading();
return mStream->ReadSegments(aWriter, aClosure, aCount, aResult);
}
NS_IMETHOD
IsNonBlocking(bool* aNonBlocking) override
{
MaybeStartReading();
return mStream->IsNonBlocking(aNonBlocking);
}
// nsIAsyncInputStream interface
NS_IMETHOD
CloseWithStatus(nsresult aReason) override
{
MaybeCloseDestination();
return mStream->CloseWithStatus(aReason);
}
NS_IMETHOD
AsyncWait(nsIInputStreamCallback* aCallback, uint32_t aFlags,
uint32_t aRequestedCount, nsIEventTarget* aTarget) override
{
MaybeStartReading();
return mStream->AsyncWait(aCallback, aFlags, aRequestedCount, aTarget);
}
NS_IMETHOD
Search(const char* aForString, bool aIgnoreCase, bool* aFound,
uint32_t* aOffsetSearchedTo) override
{
MaybeStartReading();
nsCOMPtr<nsISearchableInputStream> searchable = do_QueryInterface(mStream);
MOZ_ASSERT(searchable);
return searchable->Search(aForString, aIgnoreCase, aFound, aOffsetSearchedTo);
}
// nsICloneableInputStream interface
NS_IMETHOD
GetCloneable(bool* aCloneable) override
{
MaybeStartReading();
nsCOMPtr<nsICloneableInputStream> cloneable = do_QueryInterface(mStream);
MOZ_ASSERT(cloneable);
return cloneable->GetCloneable(aCloneable);
}
NS_IMETHOD
Clone(nsIInputStream** aResult) override
{
MaybeStartReading();
nsCOMPtr<nsICloneableInputStream> cloneable = do_QueryInterface(mStream);
MOZ_ASSERT(cloneable);
return cloneable->Clone(aResult);
}
// nsIBufferedInputStream
NS_IMETHOD
Init(nsIInputStream* aStream, uint32_t aBufferSize) override
{
MaybeStartReading();
nsCOMPtr<nsIBufferedInputStream> stream = do_QueryInterface(mStream);
MOZ_ASSERT(stream);
return stream->Init(aStream, aBufferSize);
}
void
MaybeStartReading();
void
MaybeCloseDestination();
private:
~DelayedStartInputStream() = default;
IPCStreamDestination* mDestination;
nsCOMPtr<nsIAsyncInputStream> mStream;
// This protects mDestination: any method can be called by any thread.
Mutex mMutex;
class HelperRunnable;
};
class IPCStreamDestination::DelayedStartInputStream::HelperRunnable final
: public Runnable
{
public:
enum Op {
eStartReading,
eCloseDestination,
};
HelperRunnable(IPCStreamDestination::DelayedStartInputStream* aDelayedStartInputStream,
Op aOp)
: mDelayedStartInputStream(aDelayedStartInputStream)
, mOp(aOp)
{
MOZ_ASSERT(aDelayedStartInputStream);
}
NS_IMETHOD
Run() override
{
switch (mOp) {
case eStartReading:
mDelayedStartInputStream->MaybeStartReading();
break;
case eCloseDestination:
mDelayedStartInputStream->MaybeCloseDestination();
break;
}
return NS_OK;
}
private:
RefPtr<IPCStreamDestination::DelayedStartInputStream> mDelayedStartInputStream;
Op mOp;
};
void
IPCStreamDestination::DelayedStartInputStream::MaybeStartReading()
{
MutexAutoLock lock(mMutex);
if (!mDestination) {
return;
}
if (mDestination->IsOnOwningThread()) {
mDestination->StartReading();
mDestination = nullptr;
return;
}
RefPtr<Runnable> runnable =
new HelperRunnable(this, HelperRunnable::eStartReading);
mDestination->DispatchRunnable(runnable.forget());
}
void
IPCStreamDestination::DelayedStartInputStream::MaybeCloseDestination()
{
MutexAutoLock lock(mMutex);
if (!mDestination) {
return;
}
if (mDestination->IsOnOwningThread()) {
mDestination->RequestClose(NS_ERROR_ABORT);
mDestination = nullptr;
return;
}
RefPtr<Runnable> runnable =
new HelperRunnable(this, HelperRunnable::eCloseDestination);
mDestination->DispatchRunnable(runnable.forget());
}
NS_IMPL_ADDREF(IPCStreamDestination::DelayedStartInputStream);
NS_IMPL_RELEASE(IPCStreamDestination::DelayedStartInputStream);
NS_INTERFACE_MAP_BEGIN(IPCStreamDestination::DelayedStartInputStream)
NS_INTERFACE_MAP_ENTRY(nsIAsyncInputStream)
NS_INTERFACE_MAP_ENTRY(nsISearchableInputStream)
NS_INTERFACE_MAP_ENTRY(nsICloneableInputStream)
NS_INTERFACE_MAP_ENTRY(nsIBufferedInputStream)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIInputStream, nsIAsyncInputStream)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIAsyncInputStream)
NS_INTERFACE_MAP_END
// ----------------------------------------------------------------------------
// IPCStreamDestination
IPCStreamDestination::IPCStreamDestination()
: mOwningThread(NS_GetCurrentThread())
, mDelayedStart(false)
{
}
@@ -45,13 +287,41 @@ IPCStreamDestination::Initialize()
return NS_OK;
}
void
IPCStreamDestination::SetDelayedStart(bool aDelayedStart)
{
mDelayedStart = aDelayedStart;
}
already_AddRefed<nsIInputStream>
IPCStreamDestination::TakeReader()
{
MOZ_ASSERT(mReader);
MOZ_ASSERT(!mDelayedStartInputStream);
if (mDelayedStart) {
mDelayedStartInputStream =
new DelayedStartInputStream(this, mReader.forget());
RefPtr<nsIAsyncInputStream> inputStream = mDelayedStartInputStream;
return inputStream.forget();
}
return mReader.forget();
}
bool
IPCStreamDestination::IsOnOwningThread() const
{
return mOwningThread == NS_GetCurrentThread();
}
void
IPCStreamDestination::DispatchRunnable(already_AddRefed<nsIRunnable>&& aRunnable)
{
nsCOMPtr<nsIRunnable> runnable = aRunnable;
mOwningThread->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
}
void
IPCStreamDestination::ActorDestroyed()
{
@@ -62,6 +332,11 @@ IPCStreamDestination::ActorDestroyed()
// effect. This just aborts the writer in the case where the child process
// crashes.
mWriter->CloseWithStatus(NS_ERROR_ABORT);
if (mDelayedStartInputStream) {
mDelayedStartInputStream->DestinationShutdown();
mDelayedStartInputStream = nullptr;
}
}
void
+31 -1
View File
@@ -31,9 +31,18 @@ public:
static IPCStreamDestination*
Cast(PParentToChildStreamChild* aActor);
virtual already_AddRefed<nsIInputStream>
void
SetDelayedStart(bool aDelayedStart);
already_AddRefed<nsIInputStream>
TakeReader();
bool
IsOnOwningThread() const;
void
DispatchRunnable(already_AddRefed<nsIRunnable>&& aRunnable);
protected:
IPCStreamDestination();
virtual ~IPCStreamDestination();
@@ -51,16 +60,37 @@ protected:
void
CloseReceived(nsresult aRv);
#ifdef DEBUG
bool
HasDelayedStart() const
{
return mDelayedStart;
}
#endif
// These methods will be implemented by the actor.
virtual void
StartReading() = 0;
virtual void
RequestClose(nsresult aRv) = 0;
virtual void
TerminateDestination() = 0;
private:
nsCOMPtr<nsIAsyncInputStream> mReader;
nsCOMPtr<nsIAsyncOutputStream> mWriter;
// This is created by TakeReader() if we need to delay the reading of data.
// We keep a reference to the stream in order to inform it when the actor goes
// away. If that happens, the reading of data will not be possible anymore.
class DelayedStartInputStream;
RefPtr<DelayedStartInputStream> mDelayedStartInputStream;
nsCOMPtr<nsIThread> mOwningThread;
bool mDelayedStart;
};
} // namespace ipc
+14
View File
@@ -45,6 +45,13 @@ public:
ActorDestroyed();
}
IPCResult
RecvStartReading() override
{
Start();
return IPC_OK();
}
IPCResult
RecvRequestClose(const nsresult& aRv) override
{
@@ -167,6 +174,13 @@ private:
// IPCStreamDestination methods
void
StartReading() override
{
MOZ_ASSERT(HasDelayedStart());
Unused << SendStartReading();
}
void
RequestClose(nsresult aRv) override
{
+51 -33
View File
@@ -131,7 +131,8 @@ SerializeInputStreamWithFdsParent(nsIIPCSerializableInputStream* aStream,
template<typename M>
bool
SerializeInputStream(nsIInputStream* aStream, IPCStream& aValue, M* aManager)
SerializeInputStream(nsIInputStream* aStream, IPCStream& aValue, M* aManager,
bool aDelayedStart)
{
MOZ_ASSERT(aStream);
MOZ_ASSERT(aManager);
@@ -165,6 +166,7 @@ SerializeInputStream(nsIInputStream* aStream, IPCStream& aValue, M* aManager)
MOZ_ASSERT(asyncStream);
IPCRemoteStream remoteStream;
remoteStream.delayedStart() = aDelayedStart;
remoteStream.stream() = IPCStreamSource::Create(asyncStream, aManager);
aValue = remoteStream;
@@ -175,7 +177,8 @@ template<typename M>
bool
SerializeInputStreamChild(nsIInputStream* aStream, M* aManager,
IPCStream* aValue,
OptionalIPCStream* aOptionalValue)
OptionalIPCStream* aOptionalValue,
bool aDelayedStart)
{
MOZ_ASSERT(aStream);
MOZ_ASSERT(aManager);
@@ -202,17 +205,18 @@ SerializeInputStreamChild(nsIInputStream* aStream, M* aManager,
}
if (aValue) {
return SerializeInputStream(aStream, *aValue, aManager);
return SerializeInputStream(aStream, *aValue, aManager, aDelayedStart);
}
return SerializeInputStream(aStream, *aOptionalValue, aManager);
return SerializeInputStream(aStream, *aOptionalValue, aManager, aDelayedStart);
}
template<typename M>
bool
SerializeInputStreamParent(nsIInputStream* aStream, M* aManager,
IPCStream* aValue,
OptionalIPCStream* aOptionalValue)
OptionalIPCStream* aOptionalValue,
bool aDelayedStart)
{
MOZ_ASSERT(aStream);
MOZ_ASSERT(aManager);
@@ -236,14 +240,14 @@ SerializeInputStreamParent(nsIInputStream* aStream, M* aManager,
}
if (aValue) {
return SerializeInputStream(aStream, *aValue, aManager);
return SerializeInputStream(aStream, *aValue, aManager, aDelayedStart);
}
return SerializeInputStream(aStream, *aOptionalValue, aManager);
return SerializeInputStream(aStream, *aOptionalValue, aManager, aDelayedStart);
}
void
CleanupIPCStream(IPCStream& aValue, bool aConsumedByIPC)
CleanupIPCStream(IPCStream& aValue, bool aConsumedByIPC, bool aDelayedStart)
{
if (aValue.type() == IPCStream::T__None) {
return;
@@ -309,24 +313,29 @@ CleanupIPCStream(IPCStream& aValue, bool aConsumedByIPC)
MOZ_ASSERT(source);
// If the source stream has not been taken to be sent to the other side, we
// can destroy it.
if (!aConsumedByIPC) {
source->StartDestroy();
return;
}
// If the source stream was taken to be sent to the other side, then we need
// to start it before forgetting about it.
source->Start();
if (!aDelayedStart) {
// If we don't need to do a delayedStart, we start it now. Otherwise, the
// Start() will be called at the first use by the
// IPCStreamDestination::DelayedStartInputStream.
source->Start();
}
}
void
CleanupIPCStream(OptionalIPCStream& aValue, bool aConsumedByIPC)
CleanupIPCStream(OptionalIPCStream& aValue, bool aConsumedByIPC, bool aDelayedStart)
{
if (aValue.type() == OptionalIPCStream::Tvoid_t) {
return;
}
CleanupIPCStream(aValue.get_IPCStream(), aConsumedByIPC);
CleanupIPCStream(aValue.get_IPCStream(), aConsumedByIPC, aDelayedStart);
}
// Returns false if the serialization should not proceed. This means that the
@@ -356,19 +365,21 @@ already_AddRefed<nsIInputStream>
DeserializeIPCStream(const IPCStream& aValue)
{
if (aValue.type() == IPCStream::TIPCRemoteStream) {
const IPCRemoteStreamType& remoteInputStream =
aValue.get_IPCRemoteStream().stream();
if (remoteInputStream.type() == IPCRemoteStreamType::TPChildToParentStreamParent) {
auto sendStream =
IPCStreamDestination::Cast(remoteInputStream.get_PChildToParentStreamParent());
return sendStream->TakeReader();
const IPCRemoteStream& remoteStream = aValue.get_IPCRemoteStream();
const IPCRemoteStreamType& remoteStreamType = remoteStream.stream();
IPCStreamDestination* destinationStream;
if (remoteStreamType.type() == IPCRemoteStreamType::TPChildToParentStreamParent) {
destinationStream =
IPCStreamDestination::Cast(remoteStreamType.get_PChildToParentStreamParent());
} else {
MOZ_ASSERT(remoteStreamType.type() == IPCRemoteStreamType::TPParentToChildStreamChild);
destinationStream =
IPCStreamDestination::Cast(remoteStreamType.get_PParentToChildStreamChild());
}
MOZ_ASSERT(remoteInputStream.type() == IPCRemoteStreamType::TPParentToChildStreamChild);
auto sendStream =
IPCStreamDestination::Cast(remoteInputStream.get_PParentToChildStreamChild());
return sendStream->TakeReader();
destinationStream->SetDelayedStart(remoteStream.delayedStart());
return destinationStream->TakeReader();
}
// Note, we explicitly do not support deserializing the PChildToParentStream actor on
@@ -419,27 +430,30 @@ DeserializeIPCStream(const OptionalIPCStream& aValue)
return DeserializeIPCStream(aValue.get_IPCStream());
}
AutoIPCStream::AutoIPCStream()
AutoIPCStream::AutoIPCStream(bool aDelayedStart)
: mInlineValue(void_t())
, mValue(nullptr)
, mOptionalValue(&mInlineValue)
, mTaken(false)
, mDelayedStart(aDelayedStart)
{
}
AutoIPCStream::AutoIPCStream(IPCStream& aTarget)
AutoIPCStream::AutoIPCStream(IPCStream& aTarget, bool aDelayedStart)
: mInlineValue(void_t())
, mValue(&aTarget)
, mOptionalValue(nullptr)
, mTaken(false)
, mDelayedStart(aDelayedStart)
{
}
AutoIPCStream::AutoIPCStream(OptionalIPCStream& aTarget)
AutoIPCStream::AutoIPCStream(OptionalIPCStream& aTarget, bool aDelayedStart)
: mInlineValue(void_t())
, mValue(nullptr)
, mOptionalValue(&aTarget)
, mTaken(false)
, mDelayedStart(aDelayedStart)
{
*mOptionalValue = void_t();
}
@@ -448,9 +462,9 @@ AutoIPCStream::~AutoIPCStream()
{
MOZ_ASSERT(mValue || mOptionalValue);
if (mValue && IsSet()) {
CleanupIPCStream(*mValue, mTaken);
CleanupIPCStream(*mValue, mTaken, mDelayedStart);
} else {
CleanupIPCStream(*mOptionalValue, mTaken);
CleanupIPCStream(*mOptionalValue, mTaken, mDelayedStart);
}
}
@@ -468,7 +482,8 @@ AutoIPCStream::Serialize(nsIInputStream* aStream, dom::nsIContentChild* aManager
return true;
}
if (!SerializeInputStreamChild(aStream, aManager, mValue, mOptionalValue)) {
if (!SerializeInputStreamChild(aStream, aManager, mValue, mOptionalValue,
mDelayedStart)) {
MOZ_CRASH("IPCStream creation failed!");
}
@@ -495,7 +510,8 @@ AutoIPCStream::Serialize(nsIInputStream* aStream, PBackgroundChild* aManager)
return true;
}
if (!SerializeInputStreamChild(aStream, aManager, mValue, mOptionalValue)) {
if (!SerializeInputStreamChild(aStream, aManager, mValue, mOptionalValue,
mDelayedStart)) {
MOZ_CRASH("IPCStream creation failed!");
}
@@ -523,7 +539,8 @@ AutoIPCStream::Serialize(nsIInputStream* aStream,
return true;
}
if (!SerializeInputStreamParent(aStream, aManager, mValue, mOptionalValue)) {
if (!SerializeInputStreamParent(aStream, aManager, mValue, mOptionalValue,
mDelayedStart)) {
return false;
}
@@ -550,7 +567,8 @@ AutoIPCStream::Serialize(nsIInputStream* aStream, PBackgroundParent* aManager)
return true;
}
if (!SerializeInputStreamParent(aStream, aManager, mValue, mOptionalValue)) {
if (!SerializeInputStreamParent(aStream, aManager, mValue, mOptionalValue,
mDelayedStart)) {
return false;
}
+5 -3
View File
@@ -126,6 +126,7 @@ class AutoIPCStream final
IPCStream* mValue;
OptionalIPCStream* mOptionalValue;
bool mTaken;
bool mDelayedStart;
bool
IsSet() const;
@@ -133,16 +134,17 @@ class AutoIPCStream final
public:
// Implicitly create an OptionalIPCStream value. Either
// TakeValue() or TakeOptionalValue() can be used.
AutoIPCStream();
explicit AutoIPCStream(bool aDelayedStart = false);
// Wrap an existing IPCStream. Only TakeValue() may be
// used. If a nullptr nsIInputStream is passed to SerializeOrSend() then
// a crash will be forced.
explicit AutoIPCStream(IPCStream& aTarget);
explicit AutoIPCStream(IPCStream& aTarget, bool aDelayedStart = false);
// Wrap an existing OptionalIPCStream. Either TakeValue()
// or TakeOptionalValue can be used.
explicit AutoIPCStream(OptionalIPCStream& aTarget);
explicit AutoIPCStream(OptionalIPCStream& aTarget,
bool aDelayedStart = false);
~AutoIPCStream();
+8
View File
@@ -20,6 +20,14 @@ parent:
async Close(nsresult aRv);
child:
// The remote stream can be used in 2 ways: it can start receiving data
// immediately after the creation of the child actor, or it can wait until
// the child stream is actually used. This second configuration is enabled by
// passing 'true' to delayedStart in AutoIPCStream CTOR.
// If we are delaying the reading, at the first use of the remote stream, we
// must activate the sending of data. This happens by calling this method.
async StartReading();
// The parent side has hit an error condition and has requested the child
// actor issue a Close() message. The close must be initiated by the child
// to avoid racing with an in-flight Buffer() message.
+8
View File
@@ -20,6 +20,14 @@ child:
async Close(nsresult aRv);
parent:
// The remote stream can be used in 2 ways: it can start receiving data
// immediately after the creation of the child actor, or it can wait until
// the child stream is actually used. This second configuration is enabled by
// passing 'true' to delayedStart in AutoIPCStream CTOR.
// If we are delaying the reading, at the first use of the remote stream, we
// must activate the sending of data. This happens by calling this method.
async StartReading();
// The parent side has hit an error condition and has requested the child
// actor issue a Close() message. The close must be initiated by the child
// to avoid racing with an in-flight Buffer() message.