2015-03-02 13:20:00 +00: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 04:09:32 +00:00
|
|
|
#include "mozilla/Unused.h"
|
2015-03-02 13:20:00 +00:00
|
|
|
#include "mozilla/dom/cache/CacheStreamControlChild.h"
|
|
|
|
#include "mozilla/dom/cache/CacheStreamControlParent.h"
|
2015-04-16 19:00:15 +00:00
|
|
|
#include "mozilla/dom/cache/CacheTypes.h"
|
2016-05-15 17:32:09 +00:00
|
|
|
#include "mozilla/ipc/IPCStreamUtils.h"
|
2015-03-02 13:20:00 +00:00
|
|
|
#include "mozilla/SnappyUncompressInputStream.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
2020-11-23 16:21:38 +00:00
|
|
|
#include "nsIThread.h"
|
2017-09-15 19:25:41 +00:00
|
|
|
#include "nsStringStream.h"
|
2015-03-02 13:20:00 +00:00
|
|
|
#include "nsTArray.h"
|
|
|
|
|
2020-11-04 17:04:01 +00:00
|
|
|
namespace mozilla::dom::cache {
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-11-02 05:53:26 +00:00
|
|
|
using mozilla::Unused;
|
2015-03-02 13:20:00 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-20 18:01:57 +00: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 16:28:04 +00:00
|
|
|
class ReadStream::Inner final : public ReadStream::Controllable {
|
2015-03-02 13:20:00 +00:00
|
|
|
public:
|
2015-03-20 18:01:57 +00:00
|
|
|
Inner(StreamControl* aControl, const nsID& aId, nsIInputStream* aStream);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2022-05-13 14:16:13 +00:00
|
|
|
void Serialize(Maybe<CacheReadStream>* aReadStreamOut, ErrorResult& aRv);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2022-05-13 14:16:13 +00:00
|
|
|
void Serialize(CacheReadStream* aReadStreamOut, ErrorResult& aRv);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
// ReadStream::Controllable methods
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual void CloseStream() override;
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
virtual void CloseStreamWithoutReporting() override;
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-04-16 19:00:15 +00:00
|
|
|
virtual bool HasEverBeenRead() const override;
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
// Simulate nsIInputStream methods, but we don't actually inherit from it
|
|
|
|
nsresult Close();
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
nsresult Available(uint64_t* aNumAvailableOut);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
Bug 1818305 - Part 2: Add a streamStatus method to nsIInputStream, r=xpcom-reviewers,necko-reviewers,geckoview-reviewers,valentin,jesup,m_kato,mccr8
This is semantically similar to the existing available() method, however will
not block, and doesn't need to do the work to actually determine the number of
available bytes.
As part of this patch, I also fixed one available() implementation which was
incorrectly throwing NS_BASE_STREAM_WOULD_BLOCK.
Differential Revision: https://phabricator.services.mozilla.com/D170697
2023-03-15 19:52:34 +00:00
|
|
|
nsresult StreamStatus();
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
nsresult Read(char* aBuf, uint32_t aCount, uint32_t* aNumReadOut);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
nsresult ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
|
|
|
uint32_t aCount, uint32_t* aNumReadOut);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
nsresult IsNonBlocking(bool* aNonBlockingOut);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2020-06-23 13:34:52 +00:00
|
|
|
NS_DECL_OWNINGTHREAD;
|
|
|
|
|
|
|
|
~Inner();
|
|
|
|
|
2015-03-02 13:20:00 +00:00
|
|
|
private:
|
2015-03-20 18:01:57 +00:00
|
|
|
class NoteClosedRunnable;
|
|
|
|
class ForgetRunnable;
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
void NoteClosed();
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
void Forget();
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
void NoteClosedOnOwningThread();
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
void ForgetOnOwningThread();
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2017-09-15 19:25:41 +00:00
|
|
|
nsIInputStream* EnsureStream();
|
|
|
|
|
|
|
|
void AsyncOpenStreamOnOwningThread();
|
|
|
|
|
|
|
|
void MaybeAbortAsyncOpenStream();
|
|
|
|
|
|
|
|
void OpenStreamFailed();
|
|
|
|
|
2020-06-23 13:34:52 +00:00
|
|
|
inline SafeRefPtr<Inner> SafeRefPtrFromThis() {
|
|
|
|
return Controllable::SafeRefPtrFromThis().downcast<Inner>();
|
|
|
|
}
|
|
|
|
|
2015-03-20 18:01:57 +00: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 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
const nsID mId;
|
2017-06-01 20:42:05 +00:00
|
|
|
nsCOMPtr<nsISerialEventTarget> mOwningEventTarget;
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
enum State { Open, Closed, NumStates };
|
|
|
|
Atomic<State> mState;
|
2015-04-16 19:00:15 +00:00
|
|
|
Atomic<bool> mHasEverBeenRead;
|
2017-09-15 19:25:41 +00:00
|
|
|
bool mAsyncOpenStarted;
|
2017-01-18 14:30:24 +00: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.
|
2022-03-16 18:47:08 +00:00
|
|
|
Mutex mMutex MOZ_UNANNOTATED;
|
2017-09-15 19:25:41 +00:00
|
|
|
CondVar mCondVar;
|
2017-01-18 14:30:24 +00:00
|
|
|
nsCOMPtr<nsIInputStream> mStream;
|
|
|
|
nsCOMPtr<nsIInputStream> mSnappyStream;
|
2015-03-02 13:20:00 +00: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 18:40:06 +00:00
|
|
|
class ReadStream::Inner::NoteClosedRunnable final : public CancelableRunnable {
|
2015-03-02 13:20:00 +00:00
|
|
|
public:
|
2020-06-23 13:34:52 +00:00
|
|
|
explicit NoteClosedRunnable(SafeRefPtr<ReadStream::Inner> aStream)
|
2017-06-12 19:34:10 +00:00
|
|
|
: CancelableRunnable("dom::cache::ReadStream::Inner::NoteClosedRunnable"),
|
2020-06-23 13:34:52 +00:00
|
|
|
mStream(std::move(aStream)) {}
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2016-08-08 02:18:10 +00:00
|
|
|
NS_IMETHOD Run() override {
|
2020-06-09 18:21:34 +00:00
|
|
|
mStream->NoteClosedOnOwningThread();
|
2015-03-02 13:20:00 +00: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 02:18:10 +00:00
|
|
|
nsresult Cancel() override {
|
2015-03-02 13:20:00 +00:00
|
|
|
Run();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-02-20 15:49:27 +00:00
|
|
|
~NoteClosedRunnable() = default;
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2020-06-23 13:34:52 +00:00
|
|
|
const SafeRefPtr<ReadStream::Inner> mStream;
|
2015-03-02 13:20:00 +00:00
|
|
|
};
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-02 13:20:00 +00: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 18:40:06 +00:00
|
|
|
class ReadStream::Inner::ForgetRunnable final : public CancelableRunnable {
|
2015-03-02 13:20:00 +00:00
|
|
|
public:
|
2020-06-23 13:34:52 +00:00
|
|
|
explicit ForgetRunnable(SafeRefPtr<ReadStream::Inner> aStream)
|
2017-06-12 19:34:10 +00:00
|
|
|
: CancelableRunnable("dom::cache::ReadStream::Inner::ForgetRunnable"),
|
2020-06-23 13:34:52 +00:00
|
|
|
mStream(std::move(aStream)) {}
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2016-08-08 02:18:10 +00:00
|
|
|
NS_IMETHOD Run() override {
|
2020-06-09 18:21:34 +00:00
|
|
|
mStream->ForgetOnOwningThread();
|
2015-03-02 13:20:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note, we must proceed with the Run() method so that we properly
|
|
|
|
// call RemoveListener on the actor.
|
2016-08-08 02:18:10 +00:00
|
|
|
nsresult Cancel() override {
|
2015-03-02 13:20:00 +00:00
|
|
|
Run();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-02-20 15:49:27 +00:00
|
|
|
~ForgetRunnable() = default;
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2020-06-23 13:34:52 +00:00
|
|
|
const SafeRefPtr<ReadStream::Inner> mStream;
|
2015-03-02 13:20:00 +00:00
|
|
|
};
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2015-03-20 21:03:27 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
ReadStream::Inner::Inner(StreamControl* aControl, const nsID& aId,
|
|
|
|
nsIInputStream* aStream)
|
|
|
|
: mControl(aControl),
|
|
|
|
mId(aId),
|
2020-06-23 05:05:36 +00:00
|
|
|
mOwningEventTarget(GetCurrentSerialEventTarget()),
|
2015-03-20 18:01:57 +00:00
|
|
|
mState(Open),
|
2017-01-18 14:30:24 +00:00
|
|
|
mHasEverBeenRead(false),
|
2017-09-15 19:25:41 +00:00
|
|
|
mAsyncOpenStarted(false),
|
2017-01-18 14:30:24 +00:00
|
|
|
mMutex("dom::cache::ReadStream"),
|
2017-09-15 19:25:41 +00:00
|
|
|
mCondVar(mMutex, "dom::cache::ReadStream"),
|
2017-01-18 14:30:24 +00:00
|
|
|
mStream(aStream),
|
2017-09-15 19:25:41 +00:00
|
|
|
mSnappyStream(aStream ? new SnappyUncompressInputStream(aStream)
|
|
|
|
: nullptr) {
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mControl);
|
2020-06-23 13:34:52 +00:00
|
|
|
mControl->AddReadStream(SafeRefPtrFromThis());
|
2015-03-02 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 14:16:13 +00:00
|
|
|
void ReadStream::Inner::Serialize(Maybe<CacheReadStream>* aReadStreamOut,
|
|
|
|
ErrorResult& aRv) {
|
2017-06-01 20:42:05 +00:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
|
2019-03-21 11:09:44 +00:00
|
|
|
aReadStreamOut->emplace(CacheReadStream());
|
2022-05-13 14:16:13 +00:00
|
|
|
Serialize(&aReadStreamOut->ref(), aRv);
|
2015-03-02 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 14:16:13 +00:00
|
|
|
void ReadStream::Inner::Serialize(CacheReadStream* aReadStreamOut,
|
|
|
|
ErrorResult& aRv) {
|
2017-06-01 20:42:05 +00:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
|
2016-05-12 14:29:20 +00:00
|
|
|
|
|
|
|
if (mState != Open) {
|
2019-09-20 02:19:18 +00:00
|
|
|
aRv.ThrowTypeError(
|
2020-03-06 21:04:58 +00:00
|
|
|
"Response body is a cache file stream that has already been closed.");
|
2016-05-12 14:29:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mControl);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
|
|
|
aReadStreamOut->id() = mId;
|
2015-03-20 18:01:57 +00:00
|
|
|
mControl->SerializeControl(aReadStreamOut);
|
2017-01-18 14:30:24 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2022-05-13 14:16:13 +00:00
|
|
|
mControl->SerializeStream(aReadStreamOut, mStream);
|
2017-01-18 14:30:24 +00:00
|
|
|
}
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2022-05-13 14:16:09 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut->stream().isNothing() ||
|
|
|
|
aReadStreamOut->stream().ref().stream().type() !=
|
|
|
|
mozilla::ipc::InputStreamParams::T__None);
|
2015-03-02 13:20:00 +00:00
|
|
|
|
|
|
|
// We're passing ownership across the IPC barrier with the control, so
|
|
|
|
// do not signal that the stream is closed here.
|
|
|
|
Forget();
|
|
|
|
}
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
void ReadStream::Inner::CloseStream() {
|
2017-06-01 20:42:05 +00:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2021-10-07 09:11:35 +00:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(Close());
|
2015-03-02 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
void ReadStream::Inner::CloseStreamWithoutReporting() {
|
2017-06-01 20:42:05 +00:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2015-03-02 13:20:00 +00:00
|
|
|
Forget();
|
|
|
|
}
|
|
|
|
|
2015-04-16 19:00:15 +00:00
|
|
|
bool ReadStream::Inner::HasEverBeenRead() const {
|
2017-06-01 20:42:05 +00:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2015-04-16 19:00:15 +00:00
|
|
|
return mHasEverBeenRead;
|
|
|
|
}
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
nsresult ReadStream::Inner::Close() {
|
|
|
|
// stream ops can happen on any thread
|
2017-01-18 14:30:24 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-09-15 19:25:41 +00:00
|
|
|
if (mSnappyStream) {
|
|
|
|
rv = mSnappyStream->Close();
|
|
|
|
}
|
2017-01-18 14:30:24 +00:00
|
|
|
}
|
2015-03-20 18:01:57 +00:00
|
|
|
NoteClosed();
|
|
|
|
return rv;
|
2015-03-20 18:01:57 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
nsresult ReadStream::Inner::Available(uint64_t* aNumAvailableOut) {
|
|
|
|
// stream ops can happen on any thread
|
2017-01-18 14:30:24 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-09-15 19:25:41 +00:00
|
|
|
rv = EnsureStream()->Available(aNumAvailableOut);
|
2017-01-18 14:30:24 +00:00
|
|
|
}
|
2015-03-20 18:01:57 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
Bug 1818305 - Part 2: Add a streamStatus method to nsIInputStream, r=xpcom-reviewers,necko-reviewers,geckoview-reviewers,valentin,jesup,m_kato,mccr8
This is semantically similar to the existing available() method, however will
not block, and doesn't need to do the work to actually determine the number of
available bytes.
As part of this patch, I also fixed one available() implementation which was
incorrectly throwing NS_BASE_STREAM_WOULD_BLOCK.
Differential Revision: https://phabricator.services.mozilla.com/D170697
2023-03-15 19:52:34 +00:00
|
|
|
nsresult ReadStream::Inner::StreamStatus() {
|
|
|
|
// stream ops can happen on any thread
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
rv = EnsureStream()->StreamStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
nsresult ReadStream::Inner::Read(char* aBuf, uint32_t aCount,
|
|
|
|
uint32_t* aNumReadOut) {
|
|
|
|
// stream ops can happen on any thread
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aNumReadOut);
|
2015-03-20 18:01:57 +00:00
|
|
|
|
2017-01-18 14:30:24 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-09-15 19:25:41 +00:00
|
|
|
rv = EnsureStream()->Read(aBuf, aCount, aNumReadOut);
|
2017-01-18 14:30:24 +00:00
|
|
|
}
|
2015-03-20 18:01:57 +00:00
|
|
|
|
|
|
|
if ((NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK) ||
|
|
|
|
*aNumReadOut == 0) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2015-04-16 19:00:15 +00:00
|
|
|
mHasEverBeenRead = true;
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ReadStream::Inner::ReadSegments(nsWriteSegmentFun aWriter,
|
|
|
|
void* aClosure, uint32_t aCount,
|
|
|
|
uint32_t* aNumReadOut) {
|
|
|
|
// stream ops can happen on any thread
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aNumReadOut);
|
2015-03-20 18:01:57 +00:00
|
|
|
|
2015-04-16 19:00:15 +00:00
|
|
|
if (aCount) {
|
|
|
|
mHasEverBeenRead = true;
|
|
|
|
}
|
|
|
|
|
2017-01-18 14:30:24 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
2017-09-15 19:25:41 +00:00
|
|
|
rv = EnsureStream()->ReadSegments(aWriter, aClosure, aCount, aNumReadOut);
|
2017-01-18 14:30:24 +00:00
|
|
|
}
|
2015-03-20 18:01:57 +00:00
|
|
|
|
|
|
|
if ((NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK &&
|
|
|
|
rv != NS_ERROR_NOT_IMPLEMENTED) ||
|
|
|
|
*aNumReadOut == 0) {
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2015-04-16 19:00:15 +00: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 18:01:57 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult ReadStream::Inner::IsNonBlocking(bool* aNonBlockingOut) {
|
|
|
|
// stream ops can happen on any thread
|
2017-01-18 14:30:24 +00:00
|
|
|
MutexAutoLock lock(mMutex);
|
2017-09-15 19:25:41 +00:00
|
|
|
if (mSnappyStream) {
|
|
|
|
return mSnappyStream->IsNonBlocking(aNonBlockingOut);
|
|
|
|
}
|
|
|
|
*aNonBlockingOut = false;
|
|
|
|
return NS_OK;
|
2015-03-20 18:01:57 +00:00
|
|
|
}
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
ReadStream::Inner::~Inner() {
|
|
|
|
// Any thread
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState == Closed);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mControl);
|
2015-03-02 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
void ReadStream::Inner::NoteClosed() {
|
|
|
|
// Any thread
|
|
|
|
if (mState == Closed) {
|
2015-03-02 13:20:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-01 20:42:05 +00:00
|
|
|
if (mOwningEventTarget->IsOnCurrentThread()) {
|
2015-03-02 13:20:00 +00:00
|
|
|
NoteClosedOnOwningThread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-23 13:34:52 +00:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new NoteClosedRunnable(SafeRefPtrFromThis());
|
2017-06-01 20:42:05 +00:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mOwningEventTarget->Dispatch(runnable.forget(),
|
|
|
|
nsIThread::DISPATCH_NORMAL));
|
2015-03-02 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
void ReadStream::Inner::Forget() {
|
|
|
|
// Any thread
|
|
|
|
if (mState == Closed) {
|
2015-03-02 13:20:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-01 20:42:05 +00:00
|
|
|
if (mOwningEventTarget->IsOnCurrentThread()) {
|
2015-03-02 13:20:00 +00:00
|
|
|
ForgetOnOwningThread();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-23 13:34:52 +00:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new ForgetRunnable(SafeRefPtrFromThis());
|
2017-06-01 20:42:05 +00:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(mOwningEventTarget->Dispatch(runnable.forget(),
|
|
|
|
nsIThread::DISPATCH_NORMAL));
|
2015-03-02 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
void ReadStream::Inner::NoteClosedOnOwningThread() {
|
2017-06-01 20:42:05 +00:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2015-03-20 18:01:57 +00:00
|
|
|
|
|
|
|
// Mark closed and do nothing if we were already closed
|
|
|
|
if (!mState.compareExchange(Open, Closed)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-15 19:25:41 +00:00
|
|
|
MaybeAbortAsyncOpenStream();
|
|
|
|
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mControl);
|
2020-06-23 13:34:52 +00:00
|
|
|
mControl->NoteClosed(SafeRefPtrFromThis(), mId);
|
2015-03-20 18:01:57 +00:00
|
|
|
mControl = nullptr;
|
2015-03-20 18:01:57 +00:00
|
|
|
}
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
void ReadStream::Inner::ForgetOnOwningThread() {
|
2017-06-01 20:42:05 +00:00
|
|
|
MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
// Mark closed and do nothing if we were already closed
|
|
|
|
if (!mState.compareExchange(Open, Closed)) {
|
|
|
|
return;
|
2015-03-20 18:01:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 19:25:41 +00:00
|
|
|
MaybeAbortAsyncOpenStream();
|
|
|
|
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mControl);
|
2020-06-23 13:34:52 +00:00
|
|
|
mControl->ForgetReadStream(SafeRefPtrFromThis());
|
2015-03-20 18:01:57 +00:00
|
|
|
mControl = nullptr;
|
2015-03-20 18:01:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 19:25:41 +00: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());
|
|
|
|
|
2022-06-07 22:12:39 +00:00
|
|
|
if (mSnappyStream) {
|
|
|
|
// Different threads might request opening the stream at the same time. If
|
|
|
|
// the earlier request succeeded, then use the result.
|
|
|
|
mCondVar.NotifyAll();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-15 19:25:41 +00:00
|
|
|
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 {
|
2018-05-30 19:15:35 +00:00
|
|
|
self->mStream = std::move(aStream);
|
2017-09-15 19:25:41 +00:00
|
|
|
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();
|
2020-09-23 15:17:15 +00:00
|
|
|
Unused << NS_NewCStringInputStream(getter_AddRefs(mStream), ""_ns);
|
2017-09-15 19:25:41 +00:00
|
|
|
mSnappyStream = mStream;
|
|
|
|
mStream->Close();
|
|
|
|
NoteClosed();
|
|
|
|
}
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(cache::ReadStream, nsIInputStream, ReadStream);
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<ReadStream> ReadStream::Create(
|
2019-03-21 11:09:44 +00:00
|
|
|
const Maybe<CacheReadStream>& aMaybeReadStream) {
|
|
|
|
if (aMaybeReadStream.isNothing()) {
|
2015-03-20 18:01:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-03-20 18:01:57 +00:00
|
|
|
|
2019-03-21 11:09:44 +00:00
|
|
|
return Create(aMaybeReadStream.ref());
|
2015-03-20 18:01:57 +00:00
|
|
|
}
|
2015-03-20 18:01:57 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
// static
|
|
|
|
already_AddRefed<ReadStream> ReadStream::Create(
|
2015-04-16 19:00:15 +00:00
|
|
|
const CacheReadStream& aReadStream) {
|
2015-03-20 18:01:57 +00: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.
|
2023-03-20 15:40:31 +00:00
|
|
|
if (!aReadStream.control()) {
|
2015-03-20 18:01:57 +00:00
|
|
|
return nullptr;
|
2015-03-20 21:03:27 +00:00
|
|
|
}
|
2015-03-20 18:01:57 +00:00
|
|
|
|
2022-05-13 14:16:09 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aReadStream.stream().isNothing() ||
|
|
|
|
aReadStream.stream().ref().stream().type() !=
|
|
|
|
mozilla::ipc::InputStreamParams::T__None);
|
2015-03-22 06:52:12 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
// Control is guaranteed to survive this method as ActorDestroy() cannot
|
|
|
|
// run on this thread until we complete.
|
|
|
|
StreamControl* control;
|
2023-03-20 15:40:31 +00:00
|
|
|
if (aReadStream.control().IsChild()) {
|
2015-03-20 18:01:57 +00:00
|
|
|
auto actor =
|
2023-03-20 15:40:31 +00:00
|
|
|
static_cast<CacheStreamControlChild*>(aReadStream.control().AsChild());
|
2015-03-20 18:01:57 +00:00
|
|
|
control = actor;
|
|
|
|
} else {
|
2023-03-20 15:40:31 +00:00
|
|
|
auto actor = static_cast<CacheStreamControlParent*>(
|
|
|
|
aReadStream.control().AsParent());
|
2015-03-20 18:01:57 +00:00
|
|
|
control = actor;
|
|
|
|
}
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(control);
|
2015-03-20 18:01:57 +00:00
|
|
|
|
2016-05-15 17:32:09 +00:00
|
|
|
nsCOMPtr<nsIInputStream> stream = DeserializeIPCStream(aReadStream.stream());
|
2015-03-20 18:01:57 +00:00
|
|
|
|
|
|
|
// Currently we expect all cache read streams to be blocking file streams.
|
2018-03-13 13:47:07 +00:00
|
|
|
#if defined(MOZ_DIAGNOSTIC_ASSERT_ENABLED)
|
2017-09-15 19:25:41 +00:00
|
|
|
if (stream) {
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(stream);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!asyncStream);
|
|
|
|
}
|
2015-03-20 18:01:57 +00:00
|
|
|
#endif
|
|
|
|
|
2020-06-23 13:34:52 +00:00
|
|
|
return MakeAndAddRef<ReadStream>(MakeSafeRefPtr<ReadStream::Inner>(
|
|
|
|
std::move(control), aReadStream.id(), stream));
|
2015-03-20 18:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<ReadStream> ReadStream::Create(
|
|
|
|
PCacheStreamControlParent* aControl, const nsID& aId,
|
|
|
|
nsIInputStream* aStream) {
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aControl);
|
2020-06-23 13:34:52 +00:00
|
|
|
|
|
|
|
return MakeAndAddRef<ReadStream>(MakeSafeRefPtr<ReadStream::Inner>(
|
|
|
|
static_cast<CacheStreamControlParent*>(aControl), aId, aStream));
|
2015-03-20 18:01:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 14:16:13 +00:00
|
|
|
void ReadStream::Serialize(Maybe<CacheReadStream>* aReadStreamOut,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
mInner->Serialize(aReadStreamOut, aRv);
|
2015-03-20 18:01:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 14:16:13 +00:00
|
|
|
void ReadStream::Serialize(CacheReadStream* aReadStreamOut, ErrorResult& aRv) {
|
|
|
|
mInner->Serialize(aReadStreamOut, aRv);
|
2015-03-20 18:01:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 13:34:52 +00:00
|
|
|
ReadStream::ReadStream(SafeRefPtr<ReadStream::Inner> aInner)
|
|
|
|
: mInner(std::move(aInner)) {
|
2017-01-06 20:41:15 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mInner);
|
2015-03-20 18:01:57 +00: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 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-03-20 18:01:57 +00:00
|
|
|
ReadStream::Close() { return mInner->Close(); }
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ReadStream::Available(uint64_t* aNumAvailableOut) {
|
|
|
|
return mInner->Available(aNumAvailableOut);
|
|
|
|
}
|
2015-03-02 13:20:00 +00:00
|
|
|
|
Bug 1818305 - Part 2: Add a streamStatus method to nsIInputStream, r=xpcom-reviewers,necko-reviewers,geckoview-reviewers,valentin,jesup,m_kato,mccr8
This is semantically similar to the existing available() method, however will
not block, and doesn't need to do the work to actually determine the number of
available bytes.
As part of this patch, I also fixed one available() implementation which was
incorrectly throwing NS_BASE_STREAM_WOULD_BLOCK.
Differential Revision: https://phabricator.services.mozilla.com/D170697
2023-03-15 19:52:34 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ReadStream::StreamStatus() { return mInner->StreamStatus(); }
|
|
|
|
|
2015-03-20 18:01:57 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ReadStream::Read(char* aBuf, uint32_t aCount, uint32_t* aNumReadOut) {
|
|
|
|
return mInner->Read(aBuf, aCount, aNumReadOut);
|
|
|
|
}
|
2015-03-02 13:20:00 +00:00
|
|
|
|
2015-03-20 18:01:57 +00: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 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ReadStream::IsNonBlocking(bool* aNonBlockingOut) {
|
2015-03-20 18:01:57 +00:00
|
|
|
return mInner->IsNonBlocking(aNonBlockingOut);
|
2015-03-02 13:20:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 17:04:01 +00:00
|
|
|
} // namespace mozilla::dom::cache
|