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"
|
|
|
|
|
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"
|
|
|
|
#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"
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2013-03-06 03:43:30 +04:00
|
|
|
using mozilla::DeprecatedAbs;
|
2013-02-16 07:55:36 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
class nsMultiplexInputStream MOZ_FINAL
|
|
|
|
: public nsIMultiplexInputStream
|
|
|
|
, public nsISeekableStream
|
|
|
|
, public nsIIPCSerializableInputStream
|
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
|
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
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
struct ReadSegmentsState
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
nsIInputStream* mThisStream;
|
|
|
|
uint32_t mOffset;
|
|
|
|
nsWriteSegmentFun mWriter;
|
|
|
|
void* mClosure;
|
|
|
|
bool mDone;
|
|
|
|
};
|
|
|
|
|
|
|
|
static NS_METHOD ReadSegCb(nsIInputStream* aIn, void* aClosure,
|
|
|
|
const char* aFromRawSegment, uint32_t aToOffset,
|
2014-05-15 00:15:46 +04:00
|
|
|
uint32_t aCount, uint32_t* aWriteCount);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsTArray<nsCOMPtr<nsIInputStream>> mStreams;
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t mCurrentStream;
|
|
|
|
bool mStartedReadingCurrent;
|
|
|
|
nsresult mStatus;
|
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
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsMultiplexInputStream,
|
|
|
|
nsIMultiplexInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsISeekableStream,
|
|
|
|
nsIIPCSerializableInputStream)
|
|
|
|
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
|
2014-05-15 00:15:46 +04:00
|
|
|
AvailableMaybeSeek(nsIInputStream* aStream, uint64_t* aResult)
|
2014-05-05 08:22:16 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = aStream->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.
|
2014-05-15 00:15:46 +04:00
|
|
|
nsCOMPtr<nsISeekableStream> seekable = do_QueryInterface(aStream);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (seekable) {
|
|
|
|
nsresult rv = seekable->Seek(nsISeekableStream::NS_SEEK_CUR, 0);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-15 00:15:46 +04:00
|
|
|
rv = aStream->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.
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = aSeekable->Seek(nsISeekableStream::NS_SEEK_CUR, 0);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
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()
|
2014-05-05 21:30:43 +04:00
|
|
|
: mCurrentStream(0),
|
|
|
|
mStartedReadingCurrent(false),
|
|
|
|
mStatus(NS_OK)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute unsigned long count; */
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::GetCount(uint32_t* aCount)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
*aCount = mStreams.Length();
|
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
2013-03-04 19:40:24 +04:00
|
|
|
#ifdef DEBUG
|
2012-10-19 23:29:41 +04:00
|
|
|
static bool
|
2014-05-15 00:15:46 +04:00
|
|
|
SeekableStreamAtBeginning(nsIInputStream* aStream)
|
2012-10-19 23:29:41 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
int64_t streamPos;
|
|
|
|
nsCOMPtr<nsISeekableStream> stream = do_QueryInterface(aStream);
|
|
|
|
if (stream && NS_SUCCEEDED(stream->Tell(&streamPos)) && streamPos != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-10-19 23:29:41 +04:00
|
|
|
}
|
2013-03-04 19:40:24 +04:00
|
|
|
#endif
|
2012-10-19 23:29:41 +04:00
|
|
|
|
2002-02-16 04:19:24 +03:00
|
|
|
/* void appendStream (in nsIInputStream stream); */
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::AppendStream(nsIInputStream* aStream)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(SeekableStreamAtBeginning(aStream), "Appended stream not at beginning.");
|
|
|
|
return mStreams.AppendElement(aStream) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* void insertStream (in nsIInputStream stream, in unsigned long index); */
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::InsertStream(nsIInputStream* aStream, uint32_t aIndex)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(SeekableStreamAtBeginning(aStream), "Inserted stream not at beginning.");
|
|
|
|
mStreams.InsertElementAt(aIndex, aStream);
|
|
|
|
if (mCurrentStream > aIndex ||
|
2014-05-15 00:15:46 +04:00
|
|
|
(mCurrentStream == aIndex && mStartedReadingCurrent)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
++mCurrentStream;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* void removeStream (in unsigned long index); */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsMultiplexInputStream::RemoveStream(uint32_t aIndex)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
mStreams.RemoveElementAt(aIndex);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mCurrentStream > aIndex) {
|
2014-05-05 21:30:43 +04:00
|
|
|
--mCurrentStream;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (mCurrentStream == aIndex) {
|
2014-05-05 21:30:43 +04:00
|
|
|
mStartedReadingCurrent = false;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIInputStream getStream (in unsigned long index); */
|
|
|
|
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
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult = mStreams.SafeElementAt(aIndex, nullptr);
|
|
|
|
if (NS_WARN_IF(!*aResult)) {
|
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
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ADDREF(*aResult);
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* void close (); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::Close()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
mStatus = NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
uint32_t len = mStreams.Length();
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
|
|
|
nsresult rv2 = mStreams[i]->Close();
|
|
|
|
// 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
|
|
|
}
|
|
|
|
return rv;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
2012-08-11 06:44:11 +04:00
|
|
|
/* unsigned long long available (); */
|
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
|
|
|
{
|
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;
|
|
|
|
uint64_t avail = 0;
|
|
|
|
|
|
|
|
uint32_t len = mStreams.Length();
|
|
|
|
for (uint32_t i = mCurrentStream; i < len; i++) {
|
|
|
|
uint64_t streamAvail;
|
|
|
|
rv = AvailableMaybeSeek(mStreams[i], &streamAvail);
|
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
|
|
|
avail += streamAvail;
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult = avail;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2002-02-16 04:19:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [noscript] unsigned long read (in charPtr buf, in unsigned long count); */
|
|
|
|
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
|
|
|
{
|
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;
|
|
|
|
rv = mStreams[mCurrentStream]->Read(aBuf, aCount, &read);
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
/* [noscript] unsigned long readSegments (in nsWriteSegmentFun writer,
|
|
|
|
* in voidPtr closure,
|
|
|
|
* in unsigned long count); */
|
|
|
|
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
|
|
|
{
|
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;
|
|
|
|
state.mThisStream = this;
|
|
|
|
state.mOffset = 0;
|
|
|
|
state.mWriter = aWriter;
|
|
|
|
state.mClosure = aClosure;
|
|
|
|
state.mDone = false;
|
|
|
|
|
|
|
|
uint32_t len = mStreams.Length();
|
|
|
|
while (mCurrentStream < len && aCount) {
|
|
|
|
uint32_t read;
|
|
|
|
rv = mStreams[mCurrentStream]->ReadSegments(ReadSegCb, &state, aCount, &read);
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute boolean nonBlocking; */
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream::IsNonBlocking(bool* aNonBlocking)
|
2002-02-16 04:19:24 +03:00
|
|
|
{
|
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.
|
|
|
|
// On the other hand we'll never return NS_BASE_STREAM_WOULD_BLOCK,
|
|
|
|
// so maybe we should claim to be blocking? It probably doesn't
|
|
|
|
// matter in practice.
|
|
|
|
*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) {
|
|
|
|
nsresult rv = mStreams[i]->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
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
// If one is non-blocking the entire stream becomes non-blocking
|
|
|
|
// (except that we don't implement nsIAsyncInputStream, so there's
|
|
|
|
// not much for the caller to do if Read returns "would block")
|
2014-05-15 00:15:46 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* void seek (in int32_t whence, in int32_t offset); */
|
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
|
|
|
{
|
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) {
|
|
|
|
nsCOMPtr<nsISeekableStream> stream =
|
|
|
|
do_QueryInterface(mStreams[i]);
|
|
|
|
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?");
|
|
|
|
remaining = 0;
|
|
|
|
}
|
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) {
|
|
|
|
nsCOMPtr<nsISeekableStream> stream =
|
|
|
|
do_QueryInterface(mStreams[i]);
|
2012-09-05 01:53:52 +04:00
|
|
|
|
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
|
|
|
}
|
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
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = stream->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) {
|
|
|
|
nsCOMPtr<nsISeekableStream> stream =
|
|
|
|
do_QueryInterface(mStreams[i]);
|
2012-09-05 01:53:52 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
int64_t pos;
|
|
|
|
rv = TellMaybeSeek(stream, &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
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = stream->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) {
|
|
|
|
nsCOMPtr<nsISeekableStream> stream =
|
|
|
|
do_QueryInterface(mStreams[i]);
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
/* uint32_t tell (); */
|
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
|
|
|
{
|
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) {
|
|
|
|
nsCOMPtr<nsISeekableStream> stream = do_QueryInterface(mStreams[i]);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!stream)) {
|
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;
|
|
|
|
rv = TellMaybeSeek(stream, &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
|
|
|
}
|
|
|
|
|
|
|
|
/* void setEOF (); */
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsMultiplexInputStream* inst = new nsMultiplexInputStream();
|
|
|
|
if (!inst) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ADDREF(inst);
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = inst->QueryInterface(aIID, aResult);
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_RELEASE(inst);
|
2002-02-16 04:19:24 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
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
|
|
|
{
|
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;
|
|
|
|
SerializeInputStream(mStreams[index], 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 =
|
|
|
|
DeserializeInputStream(streams[index], aFileDescriptors);
|
|
|
|
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
|
|
|
}
|