2002-02-16 01:19:24 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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 01:19:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The multiplex stream concatenates a list of input streams into a single
|
|
|
|
* stream.
|
|
|
|
*/
|
|
|
|
|
2010-10-21 18:36:13 +00:00
|
|
|
#include "IPC/IPCMessageUtils.h"
|
2012-06-05 23:51:58 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-10-21 18:36:13 +00:00
|
|
|
|
2002-02-16 01:19:24 +00:00
|
|
|
#include "nsMultiplexInputStream.h"
|
|
|
|
#include "nsIMultiplexInputStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
2006-01-02 02:30:32 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
2012-08-16 04:02:32 +00:00
|
|
|
#include "nsIIPCSerializableObsolete.h"
|
2010-10-21 18:36:13 +00:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2012-08-16 04:02:32 +00:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
|
|
|
#include "mozilla/ipc/IPCSerializableParams.h"
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2012-06-05 23:51:58 +00:00
|
|
|
class nsMultiplexInputStream MOZ_FINAL : public nsIMultiplexInputStream,
|
|
|
|
public nsISeekableStream,
|
2012-08-16 04:02:32 +00:00
|
|
|
public nsIIPCSerializableInputStream
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsMultiplexInputStream();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSIMULTIPLEXINPUTSTREAM
|
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
2012-08-16 04:02:32 +00:00
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
private:
|
2004-01-15 06:14:18 +00:00
|
|
|
~nsMultiplexInputStream() {}
|
|
|
|
|
2002-02-16 01:19:24 +00:00
|
|
|
struct ReadSegmentsState {
|
|
|
|
nsIInputStream* mThisStream;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mOffset;
|
2002-02-16 01:19:24 +00:00
|
|
|
nsWriteSegmentFun mWriter;
|
|
|
|
void* mClosure;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mDone;
|
2002-02-16 01:19:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static NS_METHOD ReadSegCb(nsIInputStream* aIn, void* aClosure,
|
2012-08-22 15:56:38 +00:00
|
|
|
const char* aFromRawSegment, uint32_t aToOffset,
|
|
|
|
uint32_t aCount, uint32_t *aWriteCount);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2012-08-23 10:42:14 +00:00
|
|
|
nsCOMArray<nsIInputStream> mStreams;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mCurrentStream;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mStartedReadingCurrent;
|
2006-01-02 02:30:32 +00:00
|
|
|
nsresult mStatus;
|
2002-02-16 01:19:24 +00:00
|
|
|
};
|
|
|
|
|
2010-10-21 18:36:13 +00:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsMultiplexInputStream)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsMultiplexInputStream)
|
|
|
|
|
|
|
|
NS_IMPL_CLASSINFO(nsMultiplexInputStream, NULL, nsIClassInfo::THREADSAFE,
|
|
|
|
NS_MULTIPLEXINPUTSTREAM_CID)
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2012-08-23 02:13:54 +00:00
|
|
|
NS_IMPL_QUERY_INTERFACE4_CI(nsMultiplexInputStream,
|
2010-10-21 18:36:13 +00:00
|
|
|
nsIMultiplexInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsISeekableStream,
|
2012-08-16 04:02:32 +00:00
|
|
|
nsIIPCSerializableInputStream)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER3(nsMultiplexInputStream,
|
2010-10-21 18:36:13 +00:00
|
|
|
nsIMultiplexInputStream,
|
|
|
|
nsIInputStream,
|
2012-08-16 04:02:32 +00:00
|
|
|
nsISeekableStream)
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
nsMultiplexInputStream::nsMultiplexInputStream()
|
|
|
|
: mCurrentStream(0),
|
2011-10-17 14:59:28 +00:00
|
|
|
mStartedReadingCurrent(false),
|
2006-01-02 02:30:32 +00:00
|
|
|
mStatus(NS_OK)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute unsigned long count; */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMultiplexInputStream::GetCount(uint32_t *aCount)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2012-08-23 10:42:14 +00:00
|
|
|
*aCount = mStreams.Count();
|
2002-02-16 01:19:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void appendStream (in nsIInputStream stream); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::AppendStream(nsIInputStream *aStream)
|
|
|
|
{
|
2012-08-23 10:42:14 +00:00
|
|
|
return mStreams.AppendObject(aStream) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* void insertStream (in nsIInputStream stream, in unsigned long index); */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMultiplexInputStream::InsertStream(nsIInputStream *aStream, uint32_t aIndex)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2012-08-23 10:42:14 +00:00
|
|
|
bool result = mStreams.InsertObjectAt(aStream, aIndex);
|
2006-01-02 02:30:32 +00:00
|
|
|
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
|
2002-02-16 01:19:24 +00:00
|
|
|
if (mCurrentStream > aIndex ||
|
|
|
|
(mCurrentStream == aIndex && mStartedReadingCurrent))
|
|
|
|
++mCurrentStream;
|
2006-01-02 02:30:32 +00:00
|
|
|
return NS_OK;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* void removeStream (in unsigned long index); */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMultiplexInputStream::RemoveStream(uint32_t aIndex)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2012-08-23 10:42:14 +00:00
|
|
|
bool result = mStreams.RemoveObjectAt(aIndex);
|
|
|
|
NS_ENSURE_TRUE(result, NS_ERROR_NOT_AVAILABLE);
|
2002-02-16 01:19:24 +00:00
|
|
|
if (mCurrentStream > aIndex)
|
|
|
|
--mCurrentStream;
|
|
|
|
else if (mCurrentStream == aIndex)
|
2011-10-17 14:59:28 +00:00
|
|
|
mStartedReadingCurrent = false;
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2006-01-02 02:30:32 +00:00
|
|
|
return NS_OK;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIInputStream getStream (in unsigned long index); */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMultiplexInputStream::GetStream(uint32_t aIndex, nsIInputStream **_retval)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2012-08-23 10:42:14 +00:00
|
|
|
*_retval = mStreams.SafeObjectAt(aIndex);
|
2006-01-02 02:30:32 +00:00
|
|
|
NS_ENSURE_TRUE(*_retval, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
NS_ADDREF(*_retval);
|
|
|
|
return NS_OK;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* void close (); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::Close()
|
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
mStatus = NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2002-02-16 01:19:24 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-08-23 12:12:53 +00:00
|
|
|
uint32_t len = mStreams.Count();
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2006-01-02 02:30:32 +00:00
|
|
|
nsresult rv2 = mStreams[i]->Close();
|
2002-02-16 01:19:24 +00:00
|
|
|
// We still want to close all streams, but we should return an error
|
|
|
|
if (NS_FAILED(rv2))
|
|
|
|
rv = rv2;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-11 02:44:11 +00:00
|
|
|
/* unsigned long long available (); */
|
2002-02-16 01:19:24 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMultiplexInputStream::Available(uint64_t *_retval)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
2002-02-16 01:19:24 +00:00
|
|
|
nsresult rv;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t avail = 0;
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2012-08-23 12:12:53 +00:00
|
|
|
uint32_t len = mStreams.Count();
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = mCurrentStream; i < len; i++) {
|
|
|
|
uint64_t streamAvail;
|
2006-01-02 02:30:32 +00:00
|
|
|
rv = mStreams[i]->Available(&streamAvail);
|
2002-02-16 01:19:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
avail += streamAvail;
|
|
|
|
}
|
|
|
|
*_retval = avail;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [noscript] unsigned long read (in charPtr buf, in unsigned long count); */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMultiplexInputStream::Read(char * aBuf, uint32_t aCount, uint32_t *_retval)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00: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).
|
|
|
|
|
2003-01-29 06:40:16 +00:00
|
|
|
*_retval = 0;
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2006-01-02 02:30:32 +00:00
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED)
|
|
|
|
return NS_OK;
|
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-08-23 12:12:53 +00:00
|
|
|
uint32_t len = mStreams.Count();
|
2003-01-29 06:40:16 +00:00
|
|
|
while (mCurrentStream < len && aCount) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t read;
|
2006-01-02 02:30:32 +00:00
|
|
|
rv = mStreams[mCurrentStream]->Read(aBuf, aCount, &read);
|
2003-01-29 06:40:16 +00:00
|
|
|
|
|
|
|
// XXX some streams return NS_BASE_STREAM_CLOSED to indicate EOF.
|
2006-01-02 02:30:32 +00:00
|
|
|
// (This is a bug in those stream implementations)
|
2003-01-29 06:40:16 +00:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
2006-01-02 02:30:32 +00:00
|
|
|
NS_NOTREACHED("Input stream's Read method returned NS_BASE_STREAM_CLOSED");
|
2003-01-29 06:40:16 +00:00
|
|
|
rv = NS_OK;
|
|
|
|
read = 0;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
2003-01-29 06:40:16 +00:00
|
|
|
else if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (read == 0) {
|
2002-02-16 01:19:24 +00:00
|
|
|
++mCurrentStream;
|
2011-10-17 14:59:28 +00:00
|
|
|
mStartedReadingCurrent = false;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2003-01-29 06:40:16 +00:00
|
|
|
NS_ASSERTION(aCount >= read, "Read more than requested");
|
|
|
|
*_retval += read;
|
|
|
|
aCount -= read;
|
|
|
|
aBuf += read;
|
2011-10-17 14:59:28 +00:00
|
|
|
mStartedReadingCurrent = true;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-29 06:40:16 +00:00
|
|
|
return *_retval ? NS_OK : rv;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [noscript] unsigned long readSegments (in nsWriteSegmentFun writer,
|
|
|
|
* in voidPtr closure,
|
|
|
|
* in unsigned long count); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::ReadSegments(nsWriteSegmentFun aWriter, void *aClosure,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aCount, uint32_t *_retval)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED) {
|
|
|
|
*_retval = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
2002-02-16 01:19:24 +00:00
|
|
|
NS_ASSERTION(aWriter, "missing aWriter");
|
|
|
|
|
2003-01-29 06:40:16 +00:00
|
|
|
nsresult rv = NS_OK;
|
2002-02-16 01:19:24 +00:00
|
|
|
ReadSegmentsState state;
|
|
|
|
state.mThisStream = this;
|
|
|
|
state.mOffset = 0;
|
|
|
|
state.mWriter = aWriter;
|
|
|
|
state.mClosure = aClosure;
|
2011-10-17 14:59:28 +00:00
|
|
|
state.mDone = false;
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2012-08-23 12:12:53 +00:00
|
|
|
uint32_t len = mStreams.Count();
|
2003-01-29 06:40:16 +00:00
|
|
|
while (mCurrentStream < len && aCount) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t read;
|
2006-01-02 02:30:32 +00:00
|
|
|
rv = mStreams[mCurrentStream]->ReadSegments(ReadSegCb, &state, aCount, &read);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2003-01-29 06:40:16 +00:00
|
|
|
// XXX some streams return NS_BASE_STREAM_CLOSED to indicate EOF.
|
2006-01-02 02:30:32 +00:00
|
|
|
// (This is a bug in those stream implementations)
|
2002-04-18 22:02:09 +00:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
2006-01-02 02:30:32 +00:00
|
|
|
NS_NOTREACHED("Input stream's Read method returned NS_BASE_STREAM_CLOSED");
|
2003-01-29 06:40:16 +00:00
|
|
|
rv = NS_OK;
|
|
|
|
read = 0;
|
2002-04-01 05:06:59 +00:00
|
|
|
}
|
2002-04-18 22:02:09 +00:00
|
|
|
|
2003-01-29 06:40:16 +00:00
|
|
|
// if |aWriter| decided to stop reading segments...
|
|
|
|
if (state.mDone || NS_FAILED(rv))
|
2002-02-16 01:19:24 +00:00
|
|
|
break;
|
2003-01-29 06:40:16 +00:00
|
|
|
|
|
|
|
// if stream is empty, then advance to the next stream.
|
2002-04-18 22:02:09 +00:00
|
|
|
if (read == 0) {
|
|
|
|
++mCurrentStream;
|
2011-10-17 14:59:28 +00:00
|
|
|
mStartedReadingCurrent = false;
|
2002-04-18 22:02:09 +00:00
|
|
|
}
|
2003-01-29 06:40:16 +00:00
|
|
|
else {
|
|
|
|
NS_ASSERTION(aCount >= read, "Read more than requested");
|
|
|
|
state.mOffset += read;
|
|
|
|
aCount -= read;
|
2011-10-17 14:59:28 +00:00
|
|
|
mStartedReadingCurrent = true;
|
2003-01-29 06:40:16 +00:00
|
|
|
}
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
2002-04-18 22:02:09 +00:00
|
|
|
|
2003-01-29 06:40:16 +00:00
|
|
|
// if we successfully read some data, then this call succeeded.
|
2002-02-16 01:19:24 +00:00
|
|
|
*_retval = state.mOffset;
|
2003-01-29 06:40:16 +00:00
|
|
|
return state.mOffset ? NS_OK : rv;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsMultiplexInputStream::ReadSegCb(nsIInputStream* aIn, void* aClosure,
|
|
|
|
const char* aFromRawSegment,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aToOffset, uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
ReadSegmentsState* state = (ReadSegmentsState*)aClosure;
|
|
|
|
rv = (state->mWriter)(state->mThisStream,
|
|
|
|
state->mClosure,
|
|
|
|
aFromRawSegment,
|
|
|
|
aToOffset + state->mOffset,
|
|
|
|
aCount,
|
|
|
|
aWriteCount);
|
2003-01-29 06:40:16 +00:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 14:59:28 +00:00
|
|
|
state->mDone = true;
|
2002-02-16 01:19:24 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute boolean nonBlocking; */
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsMultiplexInputStream::IsNonBlocking(bool *aNonBlocking)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2012-08-23 12:12:53 +00:00
|
|
|
uint32_t len = mStreams.Count();
|
2012-02-07 20:28:06 +00:00
|
|
|
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;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2006-01-02 02:30:32 +00:00
|
|
|
nsresult rv = mStreams[i]->IsNonBlocking(aNonBlocking);
|
2002-02-16 01:19:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// If one is non-blocking the entire stream becomes non-blocking
|
2006-01-02 02:30:32 +00:00
|
|
|
// (except that we don't implement nsIAsyncInputStream, so there's
|
|
|
|
// not much for the caller to do if Read returns "would block")
|
2002-02-16 01:19:24 +00:00
|
|
|
if (*aNonBlocking)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
/* void seek (in int32_t whence, in int32_t offset); */
|
2002-02-16 01:19:24 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
2002-02-16 01:19:24 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2012-08-23 10:42:14 +00:00
|
|
|
// rewinding to start is easy, and should be the most common case
|
|
|
|
if (aWhence == NS_SEEK_SET && aOffset == 0)
|
|
|
|
{
|
2012-08-23 12:12:53 +00:00
|
|
|
uint32_t i, last;
|
2012-08-23 10:42:14 +00:00
|
|
|
last = mStartedReadingCurrent ? mCurrentStream+1 : mCurrentStream;
|
|
|
|
for (i = 0; i < last; ++i) {
|
|
|
|
nsCOMPtr<nsISeekableStream> stream = do_QueryInterface(mStreams[i]);
|
|
|
|
NS_ENSURE_TRUE(stream, NS_ERROR_NO_INTERFACE);
|
2002-02-16 01:19:24 +00:00
|
|
|
|
2012-08-23 10:42:14 +00:00
|
|
|
rv = stream->Seek(NS_SEEK_SET, 0);
|
2002-02-16 01:19:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-08-23 10:42:14 +00:00
|
|
|
mCurrentStream = 0;
|
|
|
|
mStartedReadingCurrent = false;
|
2002-02-16 01:19:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// other Seeks not implemented yet
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
/* uint32_t tell (); */
|
2002-02-16 01:19:24 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMultiplexInputStream::Tell(int64_t *_retval)
|
2002-02-16 01:19:24 +00:00
|
|
|
{
|
2006-01-02 02:30:32 +00:00
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
2002-02-16 01:19:24 +00:00
|
|
|
nsresult rv;
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t ret64 = 0;
|
|
|
|
uint32_t i, last;
|
2002-02-16 01:19:24 +00:00
|
|
|
last = mStartedReadingCurrent ? mCurrentStream+1 : mCurrentStream;
|
|
|
|
for (i = 0; i < last; ++i) {
|
2006-01-02 02:30:32 +00:00
|
|
|
nsCOMPtr<nsISeekableStream> stream = do_QueryInterface(mStreams[i]);
|
2002-02-16 01:19:24 +00:00
|
|
|
NS_ENSURE_TRUE(stream, NS_ERROR_NO_INTERFACE);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t pos;
|
2002-02-16 01:19:24 +00:00
|
|
|
rv = stream->Tell(&pos);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-04-13 14:37:53 +00:00
|
|
|
ret64 += pos;
|
2002-02-16 01:19:24 +00:00
|
|
|
}
|
2004-04-13 14:37:53 +00:00
|
|
|
*_retval = ret64;
|
|
|
|
|
2002-02-16 01:19:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void setEOF (); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::SetEOF()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2010-06-10 18:11:11 +00:00
|
|
|
nsresult
|
2002-02-16 01:19:24 +00:00
|
|
|
nsMultiplexInputStreamConstructor(nsISupports *outer,
|
|
|
|
REFNSIID iid,
|
|
|
|
void **result)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
*result = nullptr;
|
2002-02-16 01:19:24 +00:00
|
|
|
|
|
|
|
if (outer)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
2010-07-05 09:42:18 +00:00
|
|
|
nsMultiplexInputStream *inst = new nsMultiplexInputStream();
|
2002-02-16 01:19:24 +00:00
|
|
|
if (!inst)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(inst);
|
|
|
|
nsresult rv = inst->QueryInterface(iid, result);
|
|
|
|
NS_RELEASE(inst);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2010-10-21 18:36:13 +00:00
|
|
|
|
2012-08-16 04:02:32 +00:00
|
|
|
void
|
|
|
|
nsMultiplexInputStream::Serialize(InputStreamParams& aParams)
|
|
|
|
{
|
|
|
|
MultiplexInputStreamParams params;
|
|
|
|
|
2012-08-23 12:12:53 +00:00
|
|
|
uint32_t streamCount = mStreams.Count();
|
2012-08-16 04:02:32 +00:00
|
|
|
|
|
|
|
if (streamCount) {
|
|
|
|
InfallibleTArray<InputStreamParams>& streams = params.streams();
|
|
|
|
|
|
|
|
streams.SetCapacity(streamCount);
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t index = 0; index < streamCount; index++) {
|
2012-08-16 04:02:32 +00:00
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> serializable =
|
2012-08-23 10:42:14 +00:00
|
|
|
do_QueryInterface(mStreams.ObjectAt(index));
|
2012-08-16 04:02:32 +00:00
|
|
|
NS_ASSERTION(serializable, "Child stream isn't serializable!");
|
|
|
|
|
|
|
|
if (serializable) {
|
|
|
|
InputStreamParams childStreamParams;
|
|
|
|
serializable->Serialize(childStreamParams);
|
|
|
|
|
|
|
|
NS_ASSERTION(childStreamParams.type() !=
|
|
|
|
InputStreamParams::T__None,
|
|
|
|
"Serialize failed!");
|
|
|
|
|
|
|
|
streams.AppendElement(childStreamParams);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
params.currentStream() = mCurrentStream;
|
|
|
|
params.status() = mStatus;
|
|
|
|
params.startedReadingCurrent() = mStartedReadingCurrent;
|
|
|
|
|
|
|
|
aParams = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsMultiplexInputStream::Deserialize(const InputStreamParams& aParams)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t streamCount = streams.Length();
|
|
|
|
for (uint32_t index = 0; index < streamCount; index++) {
|
2012-08-16 04:02:32 +00:00
|
|
|
nsCOMPtr<nsIInputStream> stream =
|
|
|
|
DeserializeInputStream(streams[index]);
|
|
|
|
if (!stream) {
|
|
|
|
NS_WARNING("Deserialize failed!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(AppendStream(stream))) {
|
|
|
|
NS_WARNING("AppendStream failed!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mCurrentStream = params.currentStream();
|
|
|
|
mStatus = params.status();
|
|
|
|
mStartedReadingCurrent = params.startedReadingCurrent();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|