2015-03-02 16:20:00 +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 "mozilla/dom/cache/ReadStream.h"
|
|
|
|
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/cache/CacheStreamControlChild.h"
|
|
|
|
#include "mozilla/dom/cache/CacheStreamControlParent.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
#include "mozilla/dom/cache/CacheTypes.h"
|
2016-05-15 20:32:09 +03:00
|
|
|
#include "mozilla/ipc/IPCStreamUtils.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/SnappyUncompressInputStream.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
using mozilla::Unused;
|
2016-05-15 20:32:09 +03:00
|
|
|
using mozilla::ipc::AutoIPCStream;
|
|
|
|
using mozilla::ipc::IPCStream;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// The inner stream class. This is where all of the real work is done. As
|
|
|
|
// an invariant Inner::Close() must be called before ~Inner(). This is
|
|
|
|
// guaranteed by our outer ReadStream class.
|
2015-03-21 19:28:04 +03:00
|
|
|
class ReadStream::Inner final : public ReadStream::Controllable
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-03-20 21:01:57 +03:00
|
|
|
Inner(StreamControl* aControl, const nsID& aId,
|
|
|
|
nsIInputStream* aStream);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
void
|
2016-05-15 20:32:09 +03:00
|
|
|
Serialize(CacheReadStreamOrVoid* aReadStreamOut,
|
|
|
|
nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
|
|
|
|
ErrorResult& aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
void
|
2016-05-15 20:32:09 +03:00
|
|
|
Serialize(CacheReadStream* aReadStreamOut,
|
|
|
|
nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
|
|
|
|
ErrorResult& aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// ReadStream::Controllable methods
|
|
|
|
virtual void
|
2015-03-21 19:28:04 +03:00
|
|
|
CloseStream() override;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
virtual void
|
2015-03-21 19:28:04 +03:00
|
|
|
CloseStreamWithoutReporting() override;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
virtual bool
|
2015-03-21 19:28:04 +03:00
|
|
|
MatchId(const nsID& aId) const override;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
virtual bool
|
|
|
|
HasEverBeenRead() const override;
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// Simulate nsIInputStream methods, but we don't actually inherit from it
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-03-20 21:01:57 +03:00
|
|
|
Close();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-03-20 21:01:57 +03:00
|
|
|
Available(uint64_t *aNumAvailableOut);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-03-20 21:01:57 +03:00
|
|
|
Read(char *aBuf, uint32_t aCount, uint32_t *aNumReadOut);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadSegments(nsWriteSegmentFun aWriter, void *aClosure, uint32_t aCount,
|
|
|
|
uint32_t *aNumReadOut);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-03-20 21:01:57 +03:00
|
|
|
IsNonBlocking(bool *aNonBlockingOut);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
private:
|
2015-03-20 21:01:57 +03:00
|
|
|
class NoteClosedRunnable;
|
|
|
|
class ForgetRunnable;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
~Inner();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
void
|
|
|
|
NoteClosed();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
void
|
|
|
|
Forget();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
void
|
|
|
|
NoteClosedOnOwningThread();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
void
|
|
|
|
ForgetOnOwningThread();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// Weak ref to the stream control actor. The actor will always call either
|
|
|
|
// CloseStream() or CloseStreamWithoutReporting() before it's destroyed. The
|
|
|
|
// weak ref is cleared in the resulting NoteClosedOnOwningThread() or
|
|
|
|
// ForgetOnOwningThread() method call.
|
|
|
|
StreamControl* mControl;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
const nsID mId;
|
|
|
|
nsCOMPtr<nsIInputStream> mStream;
|
|
|
|
nsCOMPtr<nsIInputStream> mSnappyStream;
|
|
|
|
nsCOMPtr<nsIThread> mOwningThread;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
enum State
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
Open,
|
|
|
|
Closed,
|
|
|
|
NumStates
|
|
|
|
};
|
|
|
|
Atomic<State> mState;
|
2015-04-16 22:00:15 +03:00
|
|
|
Atomic<bool> mHasEverBeenRead;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(cache::ReadStream::Inner, override)
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Runnable to notify actors that the ReadStream has closed. This must
|
|
|
|
// be done on the thread associated with the PBackground actor. Must be
|
|
|
|
// cancelable to execute on Worker threads (which can occur when the
|
|
|
|
// ReadStream is constructed on a child process Worker thread).
|
2016-04-11 21:40:06 +03:00
|
|
|
class ReadStream::Inner::NoteClosedRunnable final : public CancelableRunnable
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-03-20 21:01:57 +03:00
|
|
|
explicit NoteClosedRunnable(ReadStream::Inner* aStream)
|
2015-03-02 16:20:00 +03:00
|
|
|
: mStream(aStream)
|
|
|
|
{ }
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
mStream->NoteClosedOnOwningThread();
|
2015-03-20 21:01:57 +03:00
|
|
|
mStream = nullptr;
|
2015-03-02 16:20:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note, we must proceed with the Run() method since our actor will not
|
|
|
|
// clean itself up until we note that the stream is closed.
|
2016-08-08 05:18:10 +03:00
|
|
|
nsresult Cancel() override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
Run();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~NoteClosedRunnable() { }
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ReadStream::Inner> mStream;
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
// Runnable to clear actors without reporting that the ReadStream has
|
|
|
|
// closed. Since this can trigger actor destruction, we need to do
|
|
|
|
// it on the thread associated with the PBackground actor. Must be
|
|
|
|
// cancelable to execute on Worker threads (which can occur when the
|
|
|
|
// ReadStream is constructed on a child process Worker thread).
|
2016-04-11 21:40:06 +03:00
|
|
|
class ReadStream::Inner::ForgetRunnable final : public CancelableRunnable
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-03-20 21:01:57 +03:00
|
|
|
explicit ForgetRunnable(ReadStream::Inner* aStream)
|
2015-03-02 16:20:00 +03:00
|
|
|
: mStream(aStream)
|
|
|
|
{ }
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
mStream->ForgetOnOwningThread();
|
2015-03-20 21:01:57 +03:00
|
|
|
mStream = nullptr;
|
2015-03-02 16:20:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note, we must proceed with the Run() method so that we properly
|
|
|
|
// call RemoveListener on the actor.
|
2016-08-08 05:18:10 +03:00
|
|
|
nsresult Cancel() override
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
|
|
|
Run();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~ForgetRunnable() { }
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ReadStream::Inner> mStream;
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-03-21 00:03:27 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::Inner(StreamControl* aControl, const nsID& aId,
|
|
|
|
nsIInputStream* aStream)
|
|
|
|
: mControl(aControl)
|
|
|
|
, mId(aId)
|
|
|
|
, mStream(aStream)
|
|
|
|
, mSnappyStream(new SnappyUncompressInputStream(aStream))
|
|
|
|
, mOwningThread(NS_GetCurrentThread())
|
|
|
|
, mState(Open)
|
2015-03-21 00:03:27 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(mStream);
|
|
|
|
MOZ_ASSERT(mControl);
|
|
|
|
mControl->AddReadStream(this);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-12 17:29:20 +03:00
|
|
|
ReadStream::Inner::Serialize(CacheReadStreamOrVoid* aReadStreamOut,
|
2016-05-15 20:32:09 +03:00
|
|
|
nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
|
2016-05-12 17:29:20 +03:00
|
|
|
ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
2015-03-02 16:20:00 +03:00
|
|
|
MOZ_ASSERT(aReadStreamOut);
|
2016-05-15 20:32:09 +03:00
|
|
|
*aReadStreamOut = CacheReadStream();
|
|
|
|
Serialize(&aReadStreamOut->get_CacheReadStream(), aStreamCleanupList, aRv);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-15 20:32:09 +03:00
|
|
|
ReadStream::Inner::Serialize(CacheReadStream* aReadStreamOut,
|
|
|
|
nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
|
|
|
|
ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
2015-03-02 16:20:00 +03:00
|
|
|
MOZ_ASSERT(aReadStreamOut);
|
2016-05-12 17:29:20 +03:00
|
|
|
|
|
|
|
if (mState != Open) {
|
|
|
|
aRv.ThrowTypeError<MSG_CACHE_STREAM_CLOSED>();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(mControl);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
aReadStreamOut->id() = mId;
|
2015-03-20 21:01:57 +03:00
|
|
|
mControl->SerializeControl(aReadStreamOut);
|
2016-05-15 20:32:09 +03:00
|
|
|
mControl->SerializeStream(aReadStreamOut, mStream, aStreamCleanupList);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
MOZ_ASSERT(aReadStreamOut->stream().type() ==
|
|
|
|
IPCStream::TInputStreamParamsWithFds);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// We're passing ownership across the IPC barrier with the control, so
|
|
|
|
// do not signal that the stream is closed here.
|
|
|
|
Forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::CloseStream()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
2015-03-02 16:20:00 +03:00
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::CloseStreamWithoutReporting()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
2015-03-02 16:20:00 +03:00
|
|
|
Forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::MatchId(const nsID& aId) const
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
2015-03-02 16:20:00 +03:00
|
|
|
return mId.Equals(aId);
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
bool
|
|
|
|
ReadStream::Inner::HasEverBeenRead() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
|
|
|
return mHasEverBeenRead;
|
|
|
|
}
|
|
|
|
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::Close()
|
2015-03-20 21:01:57 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
// stream ops can happen on any thread
|
|
|
|
nsresult rv = mStream->Close();
|
|
|
|
NoteClosed();
|
|
|
|
return rv;
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
|
|
|
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::Available(uint64_t* aNumAvailableOut)
|
|
|
|
{
|
|
|
|
// stream ops can happen on any thread
|
|
|
|
nsresult rv = mSnappyStream->Available(aNumAvailableOut);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::Read(char* aBuf, uint32_t aCount, uint32_t* aNumReadOut)
|
|
|
|
{
|
|
|
|
// stream ops can happen on any thread
|
|
|
|
MOZ_ASSERT(aNumReadOut);
|
|
|
|
|
|
|
|
nsresult rv = mSnappyStream->Read(aBuf, aCount, aNumReadOut);
|
|
|
|
|
|
|
|
if ((NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) ||
|
|
|
|
*aNumReadOut == 0) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
mHasEverBeenRead = true;
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
|
|
|
uint32_t aCount, uint32_t* aNumReadOut)
|
|
|
|
{
|
|
|
|
// stream ops can happen on any thread
|
|
|
|
MOZ_ASSERT(aNumReadOut);
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
if (aCount) {
|
|
|
|
mHasEverBeenRead = true;
|
|
|
|
}
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
nsresult rv = mSnappyStream->ReadSegments(aWriter, aClosure, aCount,
|
|
|
|
aNumReadOut);
|
|
|
|
|
|
|
|
if ((NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK &&
|
|
|
|
rv != NS_ERROR_NOT_IMPLEMENTED) || *aNumReadOut == 0) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
// Verify bytes were actually read before marking as being ever read. For
|
|
|
|
// example, code can test if the stream supports ReadSegments() by calling
|
|
|
|
// this method with a dummy callback which doesn't read anything. We don't
|
|
|
|
// want to trigger on that.
|
|
|
|
if (*aNumReadOut) {
|
|
|
|
mHasEverBeenRead = true;
|
|
|
|
}
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-08-16 05:12:30 +03:00
|
|
|
nsresult
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::IsNonBlocking(bool* aNonBlockingOut)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
// stream ops can happen on any thread
|
|
|
|
return mSnappyStream->IsNonBlocking(aNonBlockingOut);
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::~Inner()
|
|
|
|
{
|
|
|
|
// Any thread
|
|
|
|
MOZ_ASSERT(mState == Closed);
|
|
|
|
MOZ_ASSERT(!mControl);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::NoteClosed()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
// Any thread
|
|
|
|
if (mState == Closed) {
|
2015-03-02 16:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_GetCurrentThread() == mOwningThread) {
|
|
|
|
NoteClosedOnOwningThread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new NoteClosedRunnable(this);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
mOwningThread->Dispatch(runnable, nsIThread::DISPATCH_NORMAL));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::Forget()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
// Any thread
|
|
|
|
if (mState == Closed) {
|
2015-03-02 16:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_GetCurrentThread() == mOwningThread) {
|
|
|
|
ForgetOnOwningThread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new ForgetRunnable(this);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
|
|
|
mOwningThread->Dispatch(runnable, nsIThread::DISPATCH_NORMAL));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
void
|
|
|
|
ReadStream::Inner::NoteClosedOnOwningThread()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
|
|
|
|
|
|
|
// Mark closed and do nothing if we were already closed
|
|
|
|
if (!mState.compareExchange(Open, Closed)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mControl);
|
|
|
|
mControl->NoteClosed(this, mId);
|
|
|
|
mControl = nullptr;
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
void
|
|
|
|
ReadStream::Inner::ForgetOnOwningThread()
|
2015-03-20 21:01:57 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(NS_GetCurrentThread() == mOwningThread);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// Mark closed and do nothing if we were already closed
|
|
|
|
if (!mState.compareExchange(Open, Closed)) {
|
|
|
|
return;
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(mControl);
|
|
|
|
mControl->ForgetReadStream(this);
|
|
|
|
mControl = nullptr;
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(cache::ReadStream, nsIInputStream, ReadStream);
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<ReadStream>
|
2015-04-16 22:00:15 +03:00
|
|
|
ReadStream::Create(const CacheReadStreamOrVoid& aReadStreamOrVoid)
|
2015-03-20 21:01:57 +03:00
|
|
|
{
|
2015-04-16 22:00:15 +03:00
|
|
|
if (aReadStreamOrVoid.type() == CacheReadStreamOrVoid::Tvoid_t) {
|
2015-03-20 21:01:57 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-03-20 21:01:57 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
return Create(aReadStreamOrVoid.get_CacheReadStream());
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
2015-03-20 21:01:57 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<ReadStream>
|
2015-04-16 22:00:15 +03:00
|
|
|
ReadStream::Create(const CacheReadStream& aReadStream)
|
2015-03-20 21:01:57 +03:00
|
|
|
{
|
|
|
|
// The parameter may or may not be for a Cache created stream. The way we
|
|
|
|
// tell is by looking at the stream control actor. If the actor exists,
|
|
|
|
// then we know the Cache created it.
|
|
|
|
if (!aReadStream.controlChild() && !aReadStream.controlParent()) {
|
|
|
|
return nullptr;
|
2015-03-21 00:03:27 +03:00
|
|
|
}
|
2015-03-20 21:01:57 +03:00
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
MOZ_ASSERT(aReadStream.stream().type() ==
|
|
|
|
IPCStream::TInputStreamParamsWithFds);
|
2015-03-22 09:52:12 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
// Control is guaranteed to survive this method as ActorDestroy() cannot
|
|
|
|
// run on this thread until we complete.
|
|
|
|
StreamControl* control;
|
|
|
|
if (aReadStream.controlChild()) {
|
|
|
|
auto actor = static_cast<CacheStreamControlChild*>(aReadStream.controlChild());
|
|
|
|
control = actor;
|
|
|
|
} else {
|
|
|
|
auto actor = static_cast<CacheStreamControlParent*>(aReadStream.controlParent());
|
|
|
|
control = actor;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(control);
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream = DeserializeIPCStream(aReadStream.stream());
|
2015-03-20 21:01:57 +03:00
|
|
|
MOZ_ASSERT(stream);
|
|
|
|
|
|
|
|
// Currently we expect all cache read streams to be blocking file streams.
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(stream);
|
|
|
|
MOZ_ASSERT(!asyncStream);
|
|
|
|
#endif
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Inner> inner = new Inner(control, aReadStream.id(), stream);
|
|
|
|
RefPtr<ReadStream> ref = new ReadStream(inner);
|
2015-03-20 21:01:57 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<ReadStream>
|
|
|
|
ReadStream::Create(PCacheStreamControlParent* aControl, const nsID& aId,
|
|
|
|
nsIInputStream* aStream)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aControl);
|
|
|
|
auto actor = static_cast<CacheStreamControlParent*>(aControl);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Inner> inner = new Inner(actor, aId, aStream);
|
|
|
|
RefPtr<ReadStream> ref = new ReadStream(inner);
|
2015-03-20 21:01:57 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-15 20:32:09 +03:00
|
|
|
ReadStream::Serialize(CacheReadStreamOrVoid* aReadStreamOut,
|
|
|
|
nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
|
|
|
|
ErrorResult& aRv)
|
2015-03-20 21:01:57 +03:00
|
|
|
{
|
2016-05-15 20:32:09 +03:00
|
|
|
mInner->Serialize(aReadStreamOut, aStreamCleanupList, aRv);
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-15 20:32:09 +03:00
|
|
|
ReadStream::Serialize(CacheReadStream* aReadStreamOut,
|
|
|
|
nsTArray<UniquePtr<AutoIPCStream>>& aStreamCleanupList,
|
|
|
|
ErrorResult& aRv)
|
2015-03-20 21:01:57 +03:00
|
|
|
{
|
2016-05-15 20:32:09 +03:00
|
|
|
mInner->Serialize(aReadStreamOut, aStreamCleanupList, aRv);
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ReadStream::ReadStream(ReadStream::Inner* aInner)
|
|
|
|
: mInner(aInner)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mInner);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadStream::~ReadStream()
|
|
|
|
{
|
|
|
|
// Explicitly close the inner stream so that it does not have to
|
|
|
|
// deal with implicitly closing at destruction time.
|
|
|
|
mInner->Close();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Close()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
return mInner->Close();
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ReadStream::Available(uint64_t* aNumAvailableOut)
|
|
|
|
{
|
|
|
|
return mInner->Available(aNumAvailableOut);
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ReadStream::Read(char* aBuf, uint32_t aCount, uint32_t* aNumReadOut)
|
|
|
|
{
|
|
|
|
return mInner->Read(aBuf, aCount, aNumReadOut);
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ReadStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
|
|
|
uint32_t aCount, uint32_t* aNumReadOut)
|
|
|
|
{
|
|
|
|
return mInner->ReadSegments(aWriter, aClosure, aCount, aNumReadOut);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ReadStream::IsNonBlocking(bool* aNonBlockingOut)
|
|
|
|
{
|
2015-03-20 21:01:57 +03:00
|
|
|
return mInner->IsNonBlocking(aNonBlockingOut);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|