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"
|
2017-09-15 22:25:41 +03:00
|
|
|
#include "nsStringStream.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#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;
|
2017-09-15 22:25:41 +03:00
|
|
|
using mozilla::ipc::OptionalIPCStream;
|
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
|
|
|
|
2017-09-15 22:25:41 +03:00
|
|
|
nsIInputStream*
|
|
|
|
EnsureStream();
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncOpenStreamOnOwningThread();
|
|
|
|
|
|
|
|
void
|
|
|
|
MaybeAbortAsyncOpenStream();
|
|
|
|
|
|
|
|
void
|
|
|
|
OpenStreamFailed();
|
|
|
|
|
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;
|
2017-06-01 23:42:05 +03:00
|
|
|
nsCOMPtr<nsISerialEventTarget> mOwningEventTarget;
|
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;
|
2017-09-15 22:25:41 +03:00
|
|
|
bool mAsyncOpenStarted;
|
2017-01-18 17:30:24 +03:00
|
|
|
|
|
|
|
// The wrapped stream objects may not be threadsafe. We need to be able
|
|
|
|
// to close a stream on our owning thread while an IO thread is simultaneously
|
|
|
|
// reading the same stream. Therefore, protect all access to these stream
|
|
|
|
// objects with a mutex.
|
|
|
|
Mutex mMutex;
|
2017-09-15 22:25:41 +03:00
|
|
|
CondVar mCondVar;
|
2017-01-18 17:30:24 +03:00
|
|
|
nsCOMPtr<nsIInputStream> mStream;
|
|
|
|
nsCOMPtr<nsIInputStream> mSnappyStream;
|
|
|
|
|
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)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("dom::cache::ReadStream::Inner::NoteClosedRunnable")
|
|
|
|
, mStream(aStream)
|
2015-03-02 16:20:00 +03:00
|
|
|
{ }
|
|
|
|
|
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)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("dom::cache::ReadStream::Inner::ForgetRunnable")
|
|
|
|
, mStream(aStream)
|
2015-03-02 16:20:00 +03:00
|
|
|
{ }
|
|
|
|
|
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)
|
2017-06-01 23:42:05 +03:00
|
|
|
, mOwningEventTarget(GetCurrentThreadSerialEventTarget())
|
2015-03-20 21:01:57 +03:00
|
|
|
, mState(Open)
|
2017-01-18 17:30:24 +03:00
|
|
|
, mHasEverBeenRead(false)
|
2017-09-15 22:25:41 +03:00
|
|
|
, mAsyncOpenStarted(false)
|
2017-01-18 17:30:24 +03:00
|
|
|
, mMutex("dom::cache::ReadStream")
|
2017-09-15 22:25:41 +03:00
|
|
|
, mCondVar(mMutex, "dom::cache::ReadStream")
|
2017-01-18 17:30:24 +03:00
|
|
|
, mStream(aStream)
|
2017-09-15 22:25:41 +03:00
|
|
|
, mSnappyStream(aStream ? new SnappyUncompressInputStream(aStream) : nullptr)
|
2015-03-21 00:03:27 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mControl);
|
2015-03-20 21:01:57 +03:00
|
|
|
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
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_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
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
|
2016-05-12 17:29:20 +03:00
|
|
|
|
|
|
|
if (mState != Open) {
|
|
|
|
aRv.ThrowTypeError<MSG_CACHE_STREAM_CLOSED>();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mControl);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
aReadStreamOut->id() = mId;
|
2015-03-20 21:01:57 +03:00
|
|
|
mControl->SerializeControl(aReadStreamOut);
|
2017-01-18 17:30:24 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mControl->SerializeStream(aReadStreamOut, mStream, aStreamCleanupList);
|
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2017-09-15 22:25:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut->stream().type() == OptionalIPCStream::Tvoid_t ||
|
|
|
|
aReadStreamOut->stream().get_IPCStream().type() ==
|
2017-01-06 23:41:15 +03:00
|
|
|
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
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
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
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
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
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
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
|
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2015-04-16 22:00:15 +03:00
|
|
|
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
|
2017-01-18 17:30:24 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-09-15 22:25:41 +03:00
|
|
|
if (mSnappyStream) {
|
|
|
|
rv = mSnappyStream->Close();
|
|
|
|
}
|
2017-01-18 17:30:24 +03:00
|
|
|
}
|
2015-03-20 21:01:57 +03:00
|
|
|
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
|
2017-01-18 17:30:24 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-09-15 22:25:41 +03:00
|
|
|
rv = EnsureStream()->Available(aNumAvailableOut);
|
2017-01-18 17:30:24 +03:00
|
|
|
}
|
2015-03-20 21:01:57 +03:00
|
|
|
|
|
|
|
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
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aNumReadOut);
|
2015-03-20 21:01:57 +03:00
|
|
|
|
2017-01-18 17:30:24 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-09-15 22:25:41 +03:00
|
|
|
rv = EnsureStream()->Read(aBuf, aCount, aNumReadOut);
|
2017-01-18 17:30:24 +03:00
|
|
|
}
|
2015-03-20 21:01:57 +03:00
|
|
|
|
|
|
|
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
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aNumReadOut);
|
2015-03-20 21:01:57 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
if (aCount) {
|
|
|
|
mHasEverBeenRead = true;
|
|
|
|
}
|
|
|
|
|
2017-01-18 17:30:24 +03:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-09-15 22:25:41 +03:00
|
|
|
rv = EnsureStream()->ReadSegments(aWriter, aClosure, aCount, aNumReadOut);
|
2017-01-18 17:30:24 +03:00
|
|
|
}
|
2015-03-20 21:01:57 +03:00
|
|
|
|
|
|
|
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
|
2017-01-18 17:30:24 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-09-15 22:25:41 +03:00
|
|
|
if (mSnappyStream) {
|
|
|
|
return mSnappyStream->IsNonBlocking(aNonBlockingOut);
|
|
|
|
}
|
|
|
|
*aNonBlockingOut = false;
|
|
|
|
return NS_OK;
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-03-20 21:01:57 +03:00
|
|
|
ReadStream::Inner::~Inner()
|
|
|
|
{
|
|
|
|
// Any thread
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == Closed);
|
|
|
|
MOZ_DIAGNOSTIC_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;
|
|
|
|
}
|
|
|
|
|
2017-06-01 23:42:05 +03:00
|
|
|
if (mOwningEventTarget->IsOnCurrentThread()) {
|
2015-03-02 16:20:00 +03:00
|
|
|
NoteClosedOnOwningThread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new NoteClosedRunnable(this);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
2017-06-01 23:42:05 +03:00
|
|
|
mOwningEventTarget->Dispatch(runnable.forget(), 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;
|
|
|
|
}
|
|
|
|
|
2017-06-01 23:42:05 +03:00
|
|
|
if (mOwningEventTarget->IsOnCurrentThread()) {
|
2015-03-02 16:20:00 +03:00
|
|
|
ForgetOnOwningThread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> runnable = new ForgetRunnable(this);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(
|
2017-06-01 23:42:05 +03:00
|
|
|
mOwningEventTarget->Dispatch(runnable.forget(), 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
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2015-03-20 21:01:57 +03:00
|
|
|
|
|
|
|
// Mark closed and do nothing if we were already closed
|
|
|
|
if (!mState.compareExchange(Open, Closed)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:25:41 +03:00
|
|
|
MaybeAbortAsyncOpenStream();
|
|
|
|
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mControl);
|
2015-03-20 21:01:57 +03:00
|
|
|
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
|
|
|
{
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
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
|
|
|
}
|
|
|
|
|
2017-09-15 22:25:41 +03:00
|
|
|
MaybeAbortAsyncOpenStream();
|
|
|
|
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mControl);
|
2015-03-20 21:01:57 +03:00
|
|
|
mControl->ForgetReadStream(this);
|
|
|
|
mControl = nullptr;
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 22:25:41 +03:00
|
|
|
nsIInputStream*
|
|
|
|
ReadStream::Inner::EnsureStream()
|
|
|
|
{
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
// We need to block the current thread while we open the stream. We
|
|
|
|
// cannot do this safely from the main owning thread since it would
|
|
|
|
// trigger deadlock. This should be ok, though, since a blocking
|
|
|
|
// stream like this should never be read on the owning thread anyway.
|
|
|
|
if (mOwningEventTarget->IsOnCurrentThread()) {
|
|
|
|
MOZ_CRASH("Blocking read on the js/ipc owning thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSnappyStream) {
|
|
|
|
return mSnappyStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r =
|
|
|
|
NewCancelableRunnableMethod("ReadStream::Inner::AsyncOpenStreamOnOwningThread",
|
|
|
|
this,
|
|
|
|
&ReadStream::Inner::AsyncOpenStreamOnOwningThread);
|
|
|
|
nsresult rv = mOwningEventTarget->Dispatch(r.forget(),
|
|
|
|
nsIThread::DISPATCH_NORMAL);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
OpenStreamFailed();
|
|
|
|
return mSnappyStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCondVar.Wait();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mSnappyStream);
|
|
|
|
|
|
|
|
return mSnappyStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReadStream::Inner::AsyncOpenStreamOnOwningThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
|
|
|
|
|
|
|
if (!mControl || mState == Closed) {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
OpenStreamFailed();
|
|
|
|
mCondVar.NotifyAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mAsyncOpenStarted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mAsyncOpenStarted = true;
|
|
|
|
|
|
|
|
RefPtr<ReadStream::Inner> self = this;
|
|
|
|
mControl->OpenStream(mId, [self](nsCOMPtr<nsIInputStream>&& aStream) {
|
|
|
|
MutexAutoLock lock(self->mMutex);
|
|
|
|
self->mAsyncOpenStarted = false;
|
|
|
|
if (!self->mStream) {
|
|
|
|
if (!aStream) {
|
|
|
|
self->OpenStreamFailed();
|
|
|
|
} else {
|
|
|
|
self->mStream = Move(aStream);
|
|
|
|
self->mSnappyStream = new SnappyUncompressInputStream(self->mStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self->mCondVar.NotifyAll();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReadStream::Inner::MaybeAbortAsyncOpenStream()
|
|
|
|
{
|
|
|
|
if (!mAsyncOpenStarted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
OpenStreamFailed();
|
|
|
|
mCondVar.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReadStream::Inner::OpenStreamFailed()
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mStream);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mSnappyStream);
|
|
|
|
mMutex.AssertCurrentThreadOwns();
|
|
|
|
Unused << NS_NewCStringInputStream(getter_AddRefs(mStream), EmptyCString());
|
|
|
|
mSnappyStream = mStream;
|
|
|
|
mStream->Close();
|
|
|
|
NoteClosed();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-09-15 22:25:41 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aReadStream.stream().type() == OptionalIPCStream::Tvoid_t ||
|
|
|
|
aReadStream.stream().get_IPCStream().type() ==
|
2017-01-06 23:41:15 +03:00
|
|
|
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;
|
|
|
|
}
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(control);
|
2015-03-20 21:01:57 +03:00
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream = DeserializeIPCStream(aReadStream.stream());
|
2015-03-20 21:01:57 +03:00
|
|
|
|
|
|
|
// Currently we expect all cache read streams to be blocking file streams.
|
2017-01-06 23:41:15 +03:00
|
|
|
#if !defined(RELEASE_OR_BETA)
|
2017-09-15 22:25:41 +03:00
|
|
|
if (stream) {
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(stream);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!asyncStream);
|
|
|
|
}
|
2015-03-20 21:01:57 +03:00
|
|
|
#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)
|
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aControl);
|
2015-03-20 21:01:57 +03:00
|
|
|
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)
|
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mInner);
|
2015-03-20 21:01:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|