2014-05-05 21:30:43 +04: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: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The multiplex stream concatenates a list of input streams into a single
|
|
|
|
* stream.
|
|
|
|
*/
|
|
|
|
|
2013-02-16 07:55:36 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2015-05-28 20:23:56 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2017-05-04 15:44:35 +03:00
|
|
|
#include "mozilla/SystemGroup.h"
|
2013-02-16 07:55:36 +04:00
|
|
|
|
2012-08-23 23:33:46 +04:00
|
|
|
#include "base/basictypes.h"
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
#include "nsMultiplexInputStream.h"
|
2015-06-03 02:12:57 +03:00
|
|
|
#include "nsICloneableInputStream.h"
|
2002-02-16 04:19:24 +03:00
|
|
|
#include "nsIMultiplexInputStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
2006-01-02 05:30:32 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
2010-10-21 22:36:13 +04:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2012-08-16 08:02:32 +04:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2017-05-04 15:44:35 +03:00
|
|
|
#include "nsIAsyncInputStream.h"
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2015-05-28 22:00:36 +03:00
|
|
|
using namespace mozilla;
|
2012-08-16 08:02:32 +04:00
|
|
|
using namespace mozilla::ipc;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2013-03-06 03:43:30 +04:00
|
|
|
using mozilla::DeprecatedAbs;
|
2016-09-29 07:20:00 +03:00
|
|
|
using mozilla::Maybe;
|
|
|
|
using mozilla::Nothing;
|
|
|
|
using mozilla::Some;
|
2013-02-16 07:55:36 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsMultiplexInputStream final
|
2014-05-15 00:15:46 +04:00
|
|
|
: public nsIMultiplexInputStream
|
|
|
|
, public nsISeekableStream
|
|
|
|
, public nsIIPCSerializableInputStream
|
2015-06-03 02:12:57 +03:00
|
|
|
, public nsICloneableInputStream
|
2017-05-04 15:44:35 +03:00
|
|
|
, public nsIAsyncInputStream
|
2017-12-13 14:11:11 +03:00
|
|
|
, public nsIInputStreamCallback
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-05-05 21:30:43 +04:00
|
|
|
nsMultiplexInputStream();
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSIMULTIPLEXINPUTSTREAM
|
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
2015-06-03 02:12:57 +03:00
|
|
|
NS_DECL_NSICLONEABLEINPUTSTREAM
|
2017-05-04 15:44:35 +03:00
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
2017-12-13 14:11:11 +03:00
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
2017-05-04 15:44:35 +03:00
|
|
|
|
|
|
|
void AsyncWaitCompleted();
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2017-11-30 22:00:23 +03:00
|
|
|
struct StreamData
|
|
|
|
{
|
|
|
|
void Initialize(nsIInputStream* aStream)
|
|
|
|
{
|
|
|
|
mStream = aStream;
|
|
|
|
mAsyncStream = do_QueryInterface(aStream);
|
|
|
|
mSeekableStream = do_QueryInterface(aStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> mStream;
|
|
|
|
|
|
|
|
// This can be null.
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mAsyncStream;
|
|
|
|
// This can be null.
|
|
|
|
nsCOMPtr<nsISeekableStream> mSeekableStream;
|
|
|
|
};
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
private:
|
2014-05-15 00:15:46 +04:00
|
|
|
~nsMultiplexInputStream()
|
|
|
|
{
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2017-12-13 14:11:12 +03:00
|
|
|
nsresult
|
|
|
|
AsyncWaitInternal();
|
|
|
|
|
2017-09-07 19:39:10 +03:00
|
|
|
// This method updates mSeekableStreams, mIPCSerializableStreams,
|
|
|
|
// mCloneableStreams and mAsyncInputStreams values.
|
2017-11-30 22:00:23 +03:00
|
|
|
void UpdateQIMap(StreamData& aStream, int32_t aCount);
|
2017-08-25 16:32:13 +03:00
|
|
|
|
2015-01-18 05:00:34 +03:00
|
|
|
struct MOZ_STACK_CLASS ReadSegmentsState
|
2014-05-15 00:15:46 +04:00
|
|
|
{
|
2015-01-18 05:00:34 +03:00
|
|
|
nsCOMPtr<nsIInputStream> mThisStream;
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t mOffset;
|
|
|
|
nsWriteSegmentFun mWriter;
|
|
|
|
void* mClosure;
|
|
|
|
bool mDone;
|
|
|
|
};
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
static nsresult ReadSegCb(nsIInputStream* aIn, void* aClosure,
|
|
|
|
const char* aFromRawSegment, uint32_t aToOffset,
|
|
|
|
uint32_t aCount, uint32_t* aWriteCount);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2017-09-07 19:39:10 +03:00
|
|
|
bool IsSeekable() const;
|
|
|
|
bool IsIPCSerializable() const;
|
|
|
|
bool IsCloneable() const;
|
|
|
|
bool IsAsyncInputStream() const;
|
|
|
|
|
2015-05-14 14:55:57 +03:00
|
|
|
Mutex mLock; // Protects access to all data members.
|
2017-11-30 22:00:23 +03:00
|
|
|
|
|
|
|
nsTArray<StreamData> mStreams;
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t mCurrentStream;
|
|
|
|
bool mStartedReadingCurrent;
|
|
|
|
nsresult mStatus;
|
2017-05-04 15:44:35 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> mAsyncWaitCallback;
|
2017-12-13 14:11:12 +03:00
|
|
|
uint32_t mAsyncWaitFlags;
|
|
|
|
uint32_t mAsyncWaitRequestedCount;
|
|
|
|
nsCOMPtr<nsIEventTarget> mAsyncWaitEventTarget;
|
2017-08-25 16:32:13 +03:00
|
|
|
|
2017-09-07 19:39:10 +03:00
|
|
|
uint32_t mSeekableStreams;
|
|
|
|
uint32_t mIPCSerializableStreams;
|
|
|
|
uint32_t mCloneableStreams;
|
|
|
|
uint32_t mAsyncInputStreams;
|
2002-02-16 04:19:24 +03:00
|
|
|
};
|
|
|
|
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_IMPL_ADDREF(nsMultiplexInputStream)
|
|
|
|
NS_IMPL_RELEASE(nsMultiplexInputStream)
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2013-10-11 00:41:39 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsMultiplexInputStream, nullptr, nsIClassInfo::THREADSAFE,
|
2010-10-21 22:36:13 +04:00
|
|
|
NS_MULTIPLEXINPUTSTREAM_CID)
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2017-03-08 09:48:16 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsMultiplexInputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMultiplexInputStream)
|
2017-09-19 17:26:21 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
|
2017-09-07 19:39:10 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsISeekableStream, IsSeekable())
|
2017-03-08 09:48:16 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIIPCSerializableInputStream,
|
2017-09-07 19:39:10 +03:00
|
|
|
IsIPCSerializable())
|
2017-03-08 09:48:16 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream,
|
2017-09-07 19:39:10 +03:00
|
|
|
IsCloneable())
|
2017-05-04 15:44:35 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStream,
|
2017-09-07 19:39:10 +03:00
|
|
|
IsAsyncInputStream())
|
2017-12-13 14:11:11 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIInputStreamCallback,
|
|
|
|
IsAsyncInputStream())
|
2017-03-08 09:48:16 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMultiplexInputStream)
|
|
|
|
NS_IMPL_QUERY_CLASSINFO(nsMultiplexInputStream)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsMultiplexInputStream,
|
2010-10-21 22:36:13 +04:00
|
|
|
nsIMultiplexInputStream,
|
|
|
|
nsIInputStream,
|
2014-04-27 11:06:00 +04:00
|
|
|
nsISeekableStream)
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2014-05-05 08:22:16 +04:00
|
|
|
static nsresult
|
2017-11-30 22:00:23 +03:00
|
|
|
AvailableMaybeSeek(nsMultiplexInputStream::StreamData& aStream,
|
|
|
|
uint64_t* aResult)
|
2014-05-05 08:22:16 +04:00
|
|
|
{
|
2017-11-30 22:00:23 +03:00
|
|
|
nsresult rv = aStream.mStream->Available(aResult);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
// Blindly seek to the current position if Available() returns
|
|
|
|
// NS_BASE_STREAM_CLOSED.
|
|
|
|
// If nsIFileInputStream is closed in Read() due to CLOSE_ON_EOF flag,
|
|
|
|
// Seek() could reopen the file if REOPEN_ON_REWIND flag is set.
|
2017-11-30 22:00:23 +03:00
|
|
|
if (aStream.mSeekableStream) {
|
|
|
|
nsresult rvSeek =
|
|
|
|
aStream.mSeekableStream->Seek(nsISeekableStream::NS_SEEK_CUR, 0);
|
2015-09-08 09:56:16 +03:00
|
|
|
if (NS_SUCCEEDED(rvSeek)) {
|
2017-11-30 22:00:23 +03:00
|
|
|
rv = aStream.mStream->Available(aResult);
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2014-05-05 08:22:16 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2014-05-05 08:22:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
TellMaybeSeek(nsISeekableStream* aSeekable, int64_t* aResult)
|
2014-05-05 08:22:16 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = aSeekable->Tell(aResult);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
// Blindly seek to the current position if Tell() returns
|
|
|
|
// NS_BASE_STREAM_CLOSED.
|
|
|
|
// If nsIFileInputStream is closed in Read() due to CLOSE_ON_EOF flag,
|
|
|
|
// Seek() could reopen the file if REOPEN_ON_REWIND flag is set.
|
2015-09-08 09:56:16 +03:00
|
|
|
nsresult rvSeek = aSeekable->Seek(nsISeekableStream::NS_SEEK_CUR, 0);
|
|
|
|
if (NS_SUCCEEDED(rvSeek)) {
|
2014-05-15 00:15:46 +04:00
|
|
|
rv = aSeekable->Tell(aResult);
|
2014-05-05 08:22:16 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
return rv;
|
2014-05-05 08:22:16 +04:00
|
|
|
}
|
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
nsMultiplexInputStream::nsMultiplexInputStream()
|
2017-09-07 19:39:10 +03:00
|
|
|
: mLock("nsMultiplexInputStream lock")
|
|
|
|
, mCurrentStream(0)
|
|
|
|
, mStartedReadingCurrent(false)
|
|
|
|
, mStatus(NS_OK)
|
2017-12-13 14:11:12 +03:00
|
|
|
, mAsyncWaitFlags(0)
|
|
|
|
, mAsyncWaitRequestedCount(0)
|
2017-09-07 19:39:10 +03:00
|
|
|
, mSeekableStreams(0)
|
|
|
|
, mIPCSerializableStreams(0)
|
|
|
|
, mCloneableStreams(0)
|
|
|
|
, mAsyncInputStreams(0)
|
|
|
|
{}
|
2002-02-16 04:19:24 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::GetCount(uint32_t* aCount)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2015-05-14 14:55:57 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-05 21:30:43 +04:00
|
|
|
*aCount = mStreams.Length();
|
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::AppendStream(nsIInputStream* aStream)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2015-05-14 14:55:57 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2017-11-30 22:00:23 +03:00
|
|
|
|
|
|
|
StreamData* streamData = mStreams.AppendElement();
|
|
|
|
if (NS_WARN_IF(!streamData)) {
|
2017-08-25 16:32:13 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2017-11-30 22:00:23 +03:00
|
|
|
streamData->Initialize(aStream);
|
|
|
|
|
|
|
|
UpdateQIMap(*streamData, 1);
|
|
|
|
|
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED) {
|
|
|
|
// We were closed, but now we have more data to read.
|
|
|
|
mStatus = NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-08-25 16:32:13 +03:00
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::GetStream(uint32_t aIndex, nsIInputStream** aResult)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2015-05-14 14:55:57 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2017-11-30 22:00:23 +03:00
|
|
|
|
|
|
|
if (aIndex >= mStreams.Length()) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2017-11-30 22:00:23 +03:00
|
|
|
StreamData& streamData = mStreams.ElementAt(aIndex);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream = streamData.mStream;
|
|
|
|
stream.forget(aResult);
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::Close()
|
|
|
|
{
|
2017-12-13 14:11:11 +03:00
|
|
|
nsTArray<nsCOMPtr<nsIInputStream>> streams;
|
|
|
|
|
|
|
|
// Let's take a copy of the streams becuase, calling close() it could trigger
|
|
|
|
// a nsIInputStreamCallback immediately and we don't want to create a deadlock
|
|
|
|
// with mutex.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
uint32_t len = mStreams.Length();
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
|
|
|
streams.AppendElement(mStreams[i].mStream);
|
|
|
|
}
|
|
|
|
mStatus = NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2017-12-13 14:11:11 +03:00
|
|
|
uint32_t len = streams.Length();
|
2014-05-05 21:30:43 +04:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2017-12-13 14:11:11 +03:00
|
|
|
nsresult rv2 = streams[i]->Close();
|
2014-05-05 21:30:43 +04:00
|
|
|
// We still want to close all streams, but we should return an error
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(rv2)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = rv2;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2017-05-04 15:44:35 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::Available(uint64_t* aResult)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2017-11-30 22:00:23 +03:00
|
|
|
*aResult = 0;
|
|
|
|
|
2015-05-14 14:55:57 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return mStatus;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
uint64_t avail = 0;
|
2017-11-30 22:00:23 +03:00
|
|
|
nsresult rv = NS_BASE_STREAM_CLOSED;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
uint32_t len = mStreams.Length();
|
|
|
|
for (uint32_t i = mCurrentStream; i < len; i++) {
|
|
|
|
uint64_t streamAvail;
|
2017-11-30 22:00:23 +03:00
|
|
|
rv = AvailableMaybeSeek(mStreams[i], &streamAvail);
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
// If a stream is closed, we continue with the next one.
|
2017-12-13 14:11:12 +03:00
|
|
|
// If this is the current stream we move to the following stream.
|
|
|
|
if (mCurrentStream == i) {
|
|
|
|
++mCurrentStream;
|
|
|
|
}
|
|
|
|
|
2017-11-30 22:00:23 +03:00
|
|
|
// If this is the last stream, we want to return this error code.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mStatus = rv;
|
2015-12-05 12:36:51 +03:00
|
|
|
return mStatus;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2017-11-30 22:00:23 +03:00
|
|
|
|
|
|
|
// If the current stream is async, we have to return what we have so far
|
|
|
|
// without processing the following streams. This is needed because
|
|
|
|
// ::Available should return only what is currently available. In case of an
|
|
|
|
// nsIAsyncInputStream, we have to call AsyncWait() in order to read more.
|
|
|
|
if (mStreams[i].mAsyncStream) {
|
|
|
|
avail += streamAvail;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (streamAvail == 0) {
|
|
|
|
// Nothing to read for this stream. Let's move to the next one.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
avail += streamAvail;
|
|
|
|
}
|
2017-11-30 22:00:23 +03:00
|
|
|
|
|
|
|
// We still have something to read. We don't want to return an error code yet.
|
|
|
|
if (avail) {
|
|
|
|
*aResult = avail;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's propagate the last error message.
|
|
|
|
mStatus = rv;
|
|
|
|
return rv;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::Read(char* aBuf, uint32_t aCount, uint32_t* aResult)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2015-05-14 14:55:57 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
2014-05-05 21:30:43 +04:00
|
|
|
// It is tempting to implement this method in terms of ReadSegments, but
|
|
|
|
// that would prevent this class from being used with streams that only
|
|
|
|
// implement Read (e.g., file streams).
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult = 0;
|
2003-01-29 09:40:16 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return mStatus;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
uint32_t len = mStreams.Length();
|
|
|
|
while (mCurrentStream < len && aCount) {
|
|
|
|
uint32_t read;
|
2017-11-30 22:00:23 +03:00
|
|
|
rv = mStreams[mCurrentStream].mStream->Read(aBuf, aCount, &read);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// XXX some streams return NS_BASE_STREAM_CLOSED to indicate EOF.
|
|
|
|
// (This is a bug in those stream implementations)
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
NS_NOTREACHED("Input stream's Read method returned NS_BASE_STREAM_CLOSED");
|
|
|
|
rv = NS_OK;
|
|
|
|
read = 0;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (NS_FAILED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
if (read == 0) {
|
|
|
|
++mCurrentStream;
|
|
|
|
mStartedReadingCurrent = false;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(aCount >= read, "Read more than requested");
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult += read;
|
2014-05-05 21:30:43 +04:00
|
|
|
aCount -= read;
|
|
|
|
aBuf += read;
|
|
|
|
mStartedReadingCurrent = true;
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
return *aResult ? NS_OK : rv;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
|
|
|
uint32_t aCount, uint32_t* aResult)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2015-05-14 14:55:57 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED) {
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult = 0;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return mStatus;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(aWriter, "missing aWriter");
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
ReadSegmentsState state;
|
2017-09-19 17:26:21 +03:00
|
|
|
state.mThisStream = this;
|
2014-05-05 21:30:43 +04:00
|
|
|
state.mOffset = 0;
|
|
|
|
state.mWriter = aWriter;
|
|
|
|
state.mClosure = aClosure;
|
|
|
|
state.mDone = false;
|
|
|
|
|
|
|
|
uint32_t len = mStreams.Length();
|
|
|
|
while (mCurrentStream < len && aCount) {
|
|
|
|
uint32_t read;
|
2017-11-30 22:00:23 +03:00
|
|
|
rv = mStreams[mCurrentStream].mStream->ReadSegments(ReadSegCb, &state,
|
|
|
|
aCount, &read);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// XXX some streams return NS_BASE_STREAM_CLOSED to indicate EOF.
|
|
|
|
// (This is a bug in those stream implementations)
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
NS_NOTREACHED("Input stream's Read method returned NS_BASE_STREAM_CLOSED");
|
|
|
|
rv = NS_OK;
|
|
|
|
read = 0;
|
2006-01-02 05:30:32 +03:00
|
|
|
}
|
2002-04-19 02:02:09 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// if |aWriter| decided to stop reading segments...
|
2014-05-15 00:15:46 +04:00
|
|
|
if (state.mDone || NS_FAILED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-29 09:40:16 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// if stream is empty, then advance to the next stream.
|
|
|
|
if (read == 0) {
|
|
|
|
++mCurrentStream;
|
|
|
|
mStartedReadingCurrent = false;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(aCount >= read, "Read more than requested");
|
|
|
|
state.mOffset += read;
|
|
|
|
aCount -= read;
|
|
|
|
mStartedReadingCurrent = true;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2002-04-19 02:02:09 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// if we successfully read some data, then this call succeeded.
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult = state.mOffset;
|
2014-05-05 21:30:43 +04:00
|
|
|
return state.mOffset ? NS_OK : rv;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
nsresult
|
2002-02-16 04:19:24 +03:00
|
|
|
nsMultiplexInputStream::ReadSegCb(nsIInputStream* aIn, void* aClosure,
|
|
|
|
const char* aFromRawSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aToOffset, uint32_t aCount,
|
2014-05-15 00:15:46 +04:00
|
|
|
uint32_t* aWriteCount)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult rv;
|
|
|
|
ReadSegmentsState* state = (ReadSegmentsState*)aClosure;
|
|
|
|
rv = (state->mWriter)(state->mThisStream,
|
|
|
|
state->mClosure,
|
|
|
|
aFromRawSegment,
|
|
|
|
aToOffset + state->mOffset,
|
|
|
|
aCount,
|
|
|
|
aWriteCount);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
state->mDone = true;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::IsNonBlocking(bool* aNonBlocking)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2015-05-14 14:55:57 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t len = mStreams.Length();
|
|
|
|
if (len == 0) {
|
|
|
|
// Claim to be non-blocking, since we won't block the caller.
|
|
|
|
*aNonBlocking = true;
|
2002-02-16 04:19:24 +03:00
|
|
|
return NS_OK;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2017-11-30 22:00:23 +03:00
|
|
|
nsresult rv = mStreams[i].mStream->IsNonBlocking(aNonBlocking);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2017-12-12 15:01:18 +03:00
|
|
|
// If one is blocking the entire stream becomes blocking.
|
|
|
|
if (!*aNonBlocking) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2015-05-14 14:55:57 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return mStatus;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult rv;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t oldCurrentStream = mCurrentStream;
|
|
|
|
bool oldStartedReadingCurrent = mStartedReadingCurrent;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (aWhence == NS_SEEK_SET) {
|
|
|
|
int64_t remaining = aOffset;
|
|
|
|
if (aOffset == 0) {
|
|
|
|
mCurrentStream = 0;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2017-11-30 22:00:23 +03:00
|
|
|
nsCOMPtr<nsISeekableStream> stream = mStreams[i].mSeekableStream;
|
2014-05-05 21:30:43 +04:00
|
|
|
if (!stream) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if all remaining streams should be rewound
|
|
|
|
if (remaining == 0) {
|
|
|
|
if (i < oldCurrentStream ||
|
|
|
|
(i == oldCurrentStream && oldStartedReadingCurrent)) {
|
|
|
|
rv = stream->Seek(NS_SEEK_SET, 0);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
continue;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2012-09-05 01:53:52 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get position in current stream
|
|
|
|
int64_t streamPos;
|
|
|
|
if (i > oldCurrentStream ||
|
|
|
|
(i == oldCurrentStream && !oldStartedReadingCurrent)) {
|
|
|
|
streamPos = 0;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = TellMaybeSeek(stream, &streamPos);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// See if we need to seek current stream forward or backward
|
|
|
|
if (remaining < streamPos) {
|
|
|
|
rv = stream->Seek(NS_SEEK_SET, remaining);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = remaining != 0;
|
|
|
|
|
|
|
|
remaining = 0;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (remaining > streamPos) {
|
2014-05-05 21:30:43 +04:00
|
|
|
if (i < oldCurrentStream) {
|
|
|
|
// We're already at end so no need to seek this stream
|
|
|
|
remaining -= streamPos;
|
|
|
|
NS_ASSERTION(remaining >= 0, "Remaining invalid");
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
uint64_t avail;
|
|
|
|
rv = AvailableMaybeSeek(mStreams[i], &avail);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
int64_t newPos = XPCOM_MIN(remaining, streamPos + (int64_t)avail);
|
|
|
|
|
|
|
|
rv = stream->Seek(NS_SEEK_SET, newPos);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = true;
|
|
|
|
|
|
|
|
remaining -= newPos;
|
|
|
|
NS_ASSERTION(remaining >= 0, "Remaining invalid");
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(remaining == streamPos, "Huh?");
|
2017-05-25 04:38:01 +03:00
|
|
|
MOZ_ASSERT(remaining != 0, "Zero remaining should be handled earlier");
|
2014-05-05 21:30:43 +04:00
|
|
|
remaining = 0;
|
2017-05-25 04:38:01 +03:00
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = true;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2012-09-05 01:53:52 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (aWhence == NS_SEEK_CUR && aOffset > 0) {
|
|
|
|
int64_t remaining = aOffset;
|
|
|
|
for (uint32_t i = mCurrentStream; remaining && i < mStreams.Length(); ++i) {
|
|
|
|
uint64_t avail;
|
|
|
|
rv = AvailableMaybeSeek(mStreams[i], &avail);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
int64_t seek = XPCOM_MIN((int64_t)avail, remaining);
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2017-11-30 22:00:23 +03:00
|
|
|
rv = mStreams[i].mSeekableStream->Seek(NS_SEEK_CUR, seek);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = true;
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
remaining -= seek;
|
2012-09-05 01:53:52 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (aWhence == NS_SEEK_CUR && aOffset < 0) {
|
|
|
|
int64_t remaining = -aOffset;
|
|
|
|
for (uint32_t i = mCurrentStream; remaining && i != (uint32_t)-1; --i) {
|
|
|
|
int64_t pos;
|
2017-11-30 22:00:23 +03:00
|
|
|
rv = TellMaybeSeek(mStreams[i].mSeekableStream, &pos);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
int64_t seek = XPCOM_MIN(pos, remaining);
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2017-11-30 22:00:23 +03:00
|
|
|
rv = mStreams[i].mSeekableStream->Seek(NS_SEEK_CUR, -seek);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = seek != -pos;
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
remaining -= seek;
|
2012-09-05 01:53:52 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWhence == NS_SEEK_CUR) {
|
|
|
|
NS_ASSERTION(aOffset == 0, "Should have handled all non-zero values");
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (aWhence == NS_SEEK_END) {
|
|
|
|
if (aOffset > 0) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
int64_t remaining = aOffset;
|
|
|
|
for (uint32_t i = mStreams.Length() - 1; i != (uint32_t)-1; --i) {
|
2017-11-30 22:00:23 +03:00
|
|
|
nsCOMPtr<nsISeekableStream> stream = mStreams[i].mSeekableStream;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// See if all remaining streams should be seeked to end
|
|
|
|
if (remaining == 0) {
|
|
|
|
if (i >= oldCurrentStream) {
|
|
|
|
rv = stream->Seek(NS_SEEK_END, 0);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2012-09-05 01:53:52 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get position in current stream
|
|
|
|
int64_t streamPos;
|
|
|
|
if (i < oldCurrentStream) {
|
|
|
|
streamPos = 0;
|
|
|
|
} else {
|
|
|
|
uint64_t avail;
|
|
|
|
rv = AvailableMaybeSeek(mStreams[i], &avail);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
streamPos = avail;
|
|
|
|
}
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// See if we have enough data in the current stream.
|
|
|
|
if (DeprecatedAbs(remaining) < streamPos) {
|
|
|
|
rv = stream->Seek(NS_SEEK_END, remaining);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = true;
|
|
|
|
|
|
|
|
remaining = 0;
|
|
|
|
} else if (DeprecatedAbs(remaining) > streamPos) {
|
|
|
|
if (i > oldCurrentStream ||
|
|
|
|
(i == oldCurrentStream && !oldStartedReadingCurrent)) {
|
|
|
|
// We're already at start so no need to seek this stream
|
|
|
|
remaining += streamPos;
|
|
|
|
} else {
|
|
|
|
int64_t avail;
|
|
|
|
rv = TellMaybeSeek(stream, &avail);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
int64_t newPos = streamPos + XPCOM_MIN(avail, DeprecatedAbs(remaining));
|
|
|
|
|
|
|
|
rv = stream->Seek(NS_SEEK_END, -newPos);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = true;
|
|
|
|
|
|
|
|
remaining += newPos;
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(remaining == streamPos, "Huh?");
|
|
|
|
remaining = 0;
|
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// other Seeks not implemented yet
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::Tell(int64_t* aResult)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2015-05-14 14:55:57 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return mStatus;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
int64_t ret64 = 0;
|
|
|
|
uint32_t i, last;
|
2014-05-15 00:15:46 +04:00
|
|
|
last = mStartedReadingCurrent ? mCurrentStream + 1 : mCurrentStream;
|
2014-05-05 21:30:43 +04:00
|
|
|
for (i = 0; i < last; ++i) {
|
2017-11-30 22:00:23 +03:00
|
|
|
if (NS_WARN_IF(!mStreams[i].mSeekableStream)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NO_INTERFACE;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
int64_t pos;
|
2017-11-30 22:00:23 +03:00
|
|
|
rv = TellMaybeSeek(mStreams[i].mSeekableStream, &pos);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
ret64 += pos;
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult = ret64;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::SetEOF()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
2017-05-04 15:44:35 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::CloseWithStatus(nsresult aStatus)
|
|
|
|
{
|
|
|
|
return Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// This class is used to inform nsMultiplexInputStream that it's time to execute
|
|
|
|
// the asyncWait callback.
|
2017-10-25 21:18:58 +03:00
|
|
|
class AsyncWaitRunnable final : public CancelableRunnable
|
2017-05-04 15:44:35 +03:00
|
|
|
{
|
|
|
|
RefPtr<nsMultiplexInputStream> mStream;
|
|
|
|
|
|
|
|
public:
|
2017-12-13 14:11:11 +03:00
|
|
|
static void
|
|
|
|
Create(nsMultiplexInputStream* aStream, nsIEventTarget* aEventTarget)
|
2017-05-04 15:44:35 +03:00
|
|
|
{
|
2017-12-13 14:11:11 +03:00
|
|
|
RefPtr<AsyncWaitRunnable> runnable = new AsyncWaitRunnable(aStream);
|
|
|
|
if (aEventTarget) {
|
|
|
|
aEventTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
|
|
|
|
} else {
|
|
|
|
runnable->Run();
|
|
|
|
}
|
2017-05-04 15:44:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
mStream->AsyncWaitCompleted();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-12-13 14:11:11 +03:00
|
|
|
explicit AsyncWaitRunnable(nsMultiplexInputStream* aStream)
|
|
|
|
: CancelableRunnable("AsyncWaitRunnable")
|
2017-05-04 15:44:35 +03:00
|
|
|
, mStream(aStream)
|
|
|
|
{
|
2017-12-13 14:11:11 +03:00
|
|
|
MOZ_ASSERT(aStream);
|
2017-05-04 15:44:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::AsyncWait(nsIInputStreamCallback* aCallback,
|
|
|
|
uint32_t aFlags,
|
|
|
|
uint32_t aRequestedCount,
|
|
|
|
nsIEventTarget* aEventTarget)
|
|
|
|
{
|
2017-12-13 14:11:11 +03:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
2017-05-04 15:44:35 +03:00
|
|
|
|
2017-12-13 14:11:11 +03:00
|
|
|
// We must execute the callback also when the stream is closed.
|
|
|
|
if (NS_FAILED(mStatus) && mStatus != NS_BASE_STREAM_CLOSED) {
|
2017-12-13 14:11:11 +03:00
|
|
|
return mStatus;
|
|
|
|
}
|
2017-05-04 15:44:35 +03:00
|
|
|
|
2017-12-13 14:11:11 +03:00
|
|
|
if (mAsyncWaitCallback && aCallback) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-05-04 15:44:35 +03:00
|
|
|
|
2017-12-13 14:11:11 +03:00
|
|
|
mAsyncWaitCallback = aCallback;
|
2017-12-13 14:11:12 +03:00
|
|
|
mAsyncWaitFlags = aFlags;
|
|
|
|
mAsyncWaitRequestedCount = aRequestedCount;
|
|
|
|
mAsyncWaitEventTarget = aEventTarget;
|
2017-05-04 15:44:35 +03:00
|
|
|
|
2017-12-13 14:11:11 +03:00
|
|
|
if (!mAsyncWaitCallback) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-12-13 14:11:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return AsyncWaitInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMultiplexInputStream::AsyncWaitInternal()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> stream;
|
|
|
|
uint32_t asyncWaitFlags = 0;
|
|
|
|
uint32_t asyncWaitRequestedCount = 0;
|
|
|
|
nsCOMPtr<nsIEventTarget> asyncWaitEventTarget;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// Let's take the first async stream if we are not already closed, and if
|
|
|
|
// it has data to read or if it async.
|
|
|
|
if (mStatus != NS_BASE_STREAM_CLOSED) {
|
|
|
|
for (; mCurrentStream < mStreams.Length(); ++mCurrentStream) {
|
|
|
|
stream = mStreams[mCurrentStream].mAsyncStream;
|
|
|
|
if (stream) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t avail = 0;
|
|
|
|
nsresult rv = AvailableMaybeSeek(mStreams[mCurrentStream], &avail);
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED || (NS_SUCCEEDED(rv) && avail == 0)) {
|
|
|
|
// Nothing to read here. Let's move on.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2017-05-04 15:44:35 +03:00
|
|
|
|
2017-12-13 14:11:12 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-05-04 15:44:35 +03:00
|
|
|
}
|
2017-12-13 14:11:12 +03:00
|
|
|
|
|
|
|
asyncWaitFlags = mAsyncWaitFlags;
|
|
|
|
asyncWaitRequestedCount = mAsyncWaitRequestedCount;
|
|
|
|
asyncWaitEventTarget = mAsyncWaitEventTarget;
|
2017-05-04 15:44:35 +03:00
|
|
|
}
|
|
|
|
|
2017-12-13 14:11:11 +03:00
|
|
|
MOZ_ASSERT_IF(stream, NS_SUCCEEDED(mStatus));
|
|
|
|
|
|
|
|
// If we are here it's because we are already closed, or if the current stream
|
|
|
|
// is not async. In both case we have to execute the callback.
|
2017-12-13 14:11:11 +03:00
|
|
|
if (!stream) {
|
2017-12-13 14:11:12 +03:00
|
|
|
AsyncWaitRunnable::Create(this, asyncWaitEventTarget);
|
2017-12-13 14:11:11 +03:00
|
|
|
return NS_OK;
|
2017-05-04 15:44:35 +03:00
|
|
|
}
|
|
|
|
|
2017-12-13 14:11:12 +03:00
|
|
|
return stream->AsyncWait(this, asyncWaitFlags, asyncWaitRequestedCount,
|
|
|
|
asyncWaitEventTarget);
|
2017-12-13 14:11:11 +03:00
|
|
|
}
|
2017-05-04 15:44:35 +03:00
|
|
|
|
2017-12-13 14:11:11 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::OnInputStreamReady(nsIAsyncInputStream* aStream)
|
|
|
|
{
|
2017-12-13 14:11:12 +03:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> callback;
|
|
|
|
|
|
|
|
// When OnInputStreamReady is called, we could be in 2 scenarios:
|
|
|
|
// a. there is something to read;
|
|
|
|
// b. the stream is closed.
|
|
|
|
// But if the stream is closed and it was not the last one, we must proceed
|
|
|
|
// with the following stream in order to have something to read by the callee.
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// The callback has been nullified in the meantime.
|
|
|
|
if (!mAsyncWaitCallback) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
|
|
|
uint64_t avail = 0;
|
|
|
|
nsresult rv = aStream->Available(&avail);
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED || avail == 0) {
|
|
|
|
// This stream is closed or empty, let's move to the following one.
|
|
|
|
++mCurrentStream;
|
|
|
|
MutexAutoUnlock unlock(mLock);
|
|
|
|
return AsyncWaitInternal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mAsyncWaitCallback.swap(callback);
|
|
|
|
mAsyncWaitEventTarget = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback->OnInputStreamReady(this);
|
2017-05-04 15:44:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMultiplexInputStream::AsyncWaitCompleted()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> callback;
|
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
// The callback has been nullified in the meantime.
|
|
|
|
if (!mAsyncWaitCallback) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAsyncWaitCallback.swap(callback);
|
2017-12-13 14:11:12 +03:00
|
|
|
mAsyncWaitEventTarget = nullptr;
|
2017-05-04 15:44:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
callback->OnInputStreamReady(this);
|
|
|
|
}
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStreamConstructor(nsISupports* aOuter,
|
|
|
|
REFNSIID aIID,
|
|
|
|
void** aResult)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult = nullptr;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aOuter) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsMultiplexInputStream> inst = new nsMultiplexInputStream();
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2015-07-01 19:27:43 +03:00
|
|
|
return inst->QueryInterface(aIID, aResult);
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2012-08-16 08:02:32 +04:00
|
|
|
void
|
2014-03-25 22:37:13 +04:00
|
|
|
nsMultiplexInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& aFileDescriptors)
|
2012-08-16 08:02:32 +04:00
|
|
|
{
|
2015-05-14 14:55:57 +03:00
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
MultiplexInputStreamParams params;
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t streamCount = mStreams.Length();
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (streamCount) {
|
|
|
|
InfallibleTArray<InputStreamParams>& streams = params.streams();
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
streams.SetCapacity(streamCount);
|
|
|
|
for (uint32_t index = 0; index < streamCount; index++) {
|
|
|
|
InputStreamParams childStreamParams;
|
2017-11-30 22:00:23 +03:00
|
|
|
InputStreamHelper::SerializeInputStream(mStreams[index].mStream,
|
2017-03-21 09:47:25 +03:00
|
|
|
childStreamParams,
|
|
|
|
aFileDescriptors);
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
streams.AppendElement(childStreamParams);
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
params.currentStream() = mCurrentStream;
|
|
|
|
params.status() = mStatus;
|
|
|
|
params.startedReadingCurrent() = mStartedReadingCurrent;
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
aParams = params;
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-03-25 22:37:13 +04:00
|
|
|
nsMultiplexInputStream::Deserialize(const InputStreamParams& aParams,
|
|
|
|
const FileDescriptorArray& aFileDescriptors)
|
2012-08-16 08:02:32 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
if (aParams.type() !=
|
|
|
|
InputStreamParams::TMultiplexInputStreamParams) {
|
|
|
|
NS_ERROR("Received unknown parameters from the other process!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MultiplexInputStreamParams& params =
|
|
|
|
aParams.get_MultiplexInputStreamParams();
|
|
|
|
|
|
|
|
const InfallibleTArray<InputStreamParams>& streams = params.streams();
|
|
|
|
|
|
|
|
uint32_t streamCount = streams.Length();
|
|
|
|
for (uint32_t index = 0; index < streamCount; index++) {
|
|
|
|
nsCOMPtr<nsIInputStream> stream =
|
2017-03-21 09:47:25 +03:00
|
|
|
InputStreamHelper::DeserializeInputStream(streams[index],
|
|
|
|
aFileDescriptors);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (!stream) {
|
|
|
|
NS_WARNING("Deserialize failed!");
|
|
|
|
return false;
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_FAILED(AppendStream(stream))) {
|
|
|
|
NS_WARNING("AppendStream failed!");
|
|
|
|
return false;
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
mCurrentStream = params.currentStream();
|
|
|
|
mStatus = params.status();
|
|
|
|
mStartedReadingCurrent = params.startedReadingCurrent();
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return true;
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|
2015-06-03 02:12:57 +03:00
|
|
|
|
2016-09-29 07:20:00 +03:00
|
|
|
Maybe<uint64_t>
|
|
|
|
nsMultiplexInputStream::ExpectedSerializedLength()
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
bool lengthValueExists = false;
|
|
|
|
uint64_t expectedLength = 0;
|
|
|
|
uint32_t streamCount = mStreams.Length();
|
|
|
|
for (uint32_t index = 0; index < streamCount; index++) {
|
2017-11-30 22:00:23 +03:00
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> stream =
|
|
|
|
do_QueryInterface(mStreams[index].mStream);
|
2016-09-29 07:20:00 +03:00
|
|
|
if (!stream) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Maybe<uint64_t> length = stream->ExpectedSerializedLength();
|
|
|
|
if (length.isNothing()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lengthValueExists = true;
|
|
|
|
expectedLength += length.value();
|
|
|
|
}
|
|
|
|
return lengthValueExists ? Some(expectedLength) : Nothing();
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:12:57 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::GetCloneable(bool* aCloneable)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
//XXXnsm Cloning a multiplex stream which has started reading is not permitted
|
|
|
|
//right now.
|
|
|
|
if (mCurrentStream > 0 || mStartedReadingCurrent) {
|
|
|
|
*aCloneable = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t len = mStreams.Length();
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2017-11-30 22:00:23 +03:00
|
|
|
nsCOMPtr<nsICloneableInputStream> cis =
|
|
|
|
do_QueryInterface(mStreams[i].mStream);
|
2015-06-03 02:12:57 +03:00
|
|
|
if (!cis || !cis->GetCloneable()) {
|
|
|
|
*aCloneable = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aCloneable = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::Clone(nsIInputStream** aClone)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
//XXXnsm Cloning a multiplex stream which has started reading is not permitted
|
|
|
|
//right now.
|
|
|
|
if (mCurrentStream > 0 || mStartedReadingCurrent) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-09-19 17:26:21 +03:00
|
|
|
RefPtr<nsMultiplexInputStream> clone = new nsMultiplexInputStream();
|
2015-06-03 02:12:57 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
uint32_t len = mStreams.Length();
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2017-11-30 22:00:23 +03:00
|
|
|
nsCOMPtr<nsICloneableInputStream> substream =
|
|
|
|
do_QueryInterface(mStreams[i].mStream);
|
2015-06-03 02:12:57 +03:00
|
|
|
if (NS_WARN_IF(!substream)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> clonedSubstream;
|
|
|
|
rv = substream->Clone(getter_AddRefs(clonedSubstream));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = clone->AppendStream(clonedSubstream);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clone.forget(aClone);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-03-08 09:48:16 +03:00
|
|
|
|
2017-11-30 22:00:23 +03:00
|
|
|
#define MAYBE_UPDATE_VALUE_REAL(x, y) \
|
|
|
|
if (y) { \
|
|
|
|
if (aCount == 1) { \
|
|
|
|
++x; \
|
|
|
|
} else if (x > 0) { \
|
|
|
|
--x; \
|
|
|
|
} else { \
|
|
|
|
MOZ_CRASH("A nsIInputStream changed QI map when stored in a nsMultiplexInputStream!"); \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAYBE_UPDATE_VALUE(x, y) \
|
|
|
|
{ \
|
|
|
|
nsCOMPtr<y> substream = do_QueryInterface(aStream.mStream); \
|
|
|
|
MAYBE_UPDATE_VALUE_REAL(x, substream) \
|
2017-09-07 19:39:10 +03:00
|
|
|
}
|
|
|
|
|
2017-08-25 16:32:13 +03:00
|
|
|
void
|
2017-11-30 22:00:23 +03:00
|
|
|
nsMultiplexInputStream::UpdateQIMap(StreamData& aStream, int32_t aCount)
|
2017-03-08 09:48:16 +03:00
|
|
|
{
|
2017-09-07 19:39:10 +03:00
|
|
|
MOZ_ASSERT(aCount == -1 || aCount == 1);
|
2017-03-08 09:48:16 +03:00
|
|
|
|
2017-11-30 22:00:23 +03:00
|
|
|
MAYBE_UPDATE_VALUE_REAL(mSeekableStreams, aStream.mSeekableStream)
|
|
|
|
MAYBE_UPDATE_VALUE(mIPCSerializableStreams, nsIIPCSerializableInputStream)
|
|
|
|
MAYBE_UPDATE_VALUE(mCloneableStreams, nsICloneableInputStream)
|
|
|
|
MAYBE_UPDATE_VALUE_REAL(mAsyncInputStreams, aStream.mAsyncStream)
|
2017-09-07 19:39:10 +03:00
|
|
|
}
|
2017-03-08 09:48:16 +03:00
|
|
|
|
2017-09-07 19:39:10 +03:00
|
|
|
#undef MAYBE_UPDATE_VALUE
|
2017-05-04 15:44:35 +03:00
|
|
|
|
2017-09-07 19:39:10 +03:00
|
|
|
bool
|
|
|
|
nsMultiplexInputStream::IsSeekable() const
|
|
|
|
{
|
|
|
|
return mStreams.Length() == mSeekableStreams;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsMultiplexInputStream::IsIPCSerializable() const
|
|
|
|
{
|
|
|
|
return mStreams.Length() == mIPCSerializableStreams;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsMultiplexInputStream::IsCloneable() const
|
|
|
|
{
|
|
|
|
return mStreams.Length() == mCloneableStreams;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsMultiplexInputStream::IsAsyncInputStream() const
|
|
|
|
{
|
|
|
|
// nsMultiplexInputStream is nsIAsyncInputStream if at least 1 of the
|
|
|
|
// substream implements that interface.
|
|
|
|
return !!mAsyncInputStreams;
|
2017-05-04 15:44:35 +03:00
|
|
|
}
|