2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The multiplex stream concatenates a list of input streams into a single
|
|
|
|
* stream.
|
|
|
|
*/
|
|
|
|
|
2012-08-23 12:33:46 -07:00
|
|
|
#include "base/basictypes.h"
|
2010-10-21 11:36:13 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsMultiplexInputStream.h"
|
2012-08-23 12:33:46 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIMultiplexInputStream.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
2010-10-21 11:36:13 -07:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2012-08-15 21:02:32 -07:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-05 16:51:58 -07:00
|
|
|
class nsMultiplexInputStream MOZ_FINAL : public nsIMultiplexInputStream,
|
|
|
|
public nsISeekableStream,
|
2012-08-15 21:02:32 -07:00
|
|
|
public nsIIPCSerializableInputStream
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsMultiplexInputStream();
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSIMULTIPLEXINPUTSTREAM
|
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
2012-08-15 21:02:32 -07:00
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
~nsMultiplexInputStream() {}
|
|
|
|
|
|
|
|
struct ReadSegmentsState {
|
|
|
|
nsIInputStream* mThisStream;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsWriteSegmentFun mWriter;
|
|
|
|
void* mClosure;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDone;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static NS_METHOD ReadSegCb(nsIInputStream* aIn, void* aClosure,
|
2012-08-22 08:56:38 -07:00
|
|
|
const char* aFromRawSegment, uint32_t aToOffset,
|
|
|
|
uint32_t aCount, uint32_t *aWriteCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-04 14:53:52 -07:00
|
|
|
nsTArray<nsCOMPtr<nsIInputStream> > mStreams;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mCurrentStream;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mStartedReadingCurrent;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult mStatus;
|
|
|
|
};
|
|
|
|
|
2010-10-21 11:36:13 -07:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsMultiplexInputStream)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsMultiplexInputStream)
|
|
|
|
|
|
|
|
NS_IMPL_CLASSINFO(nsMultiplexInputStream, NULL, nsIClassInfo::THREADSAFE,
|
|
|
|
NS_MULTIPLEXINPUTSTREAM_CID)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 19:13:54 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE4_CI(nsMultiplexInputStream,
|
2010-10-21 11:36:13 -07:00
|
|
|
nsIMultiplexInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsISeekableStream,
|
2012-08-15 21:02:32 -07:00
|
|
|
nsIIPCSerializableInputStream)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER3(nsMultiplexInputStream,
|
2010-10-21 11:36:13 -07:00
|
|
|
nsIMultiplexInputStream,
|
|
|
|
nsIInputStream,
|
2012-08-15 21:02:32 -07:00
|
|
|
nsISeekableStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsMultiplexInputStream::nsMultiplexInputStream()
|
|
|
|
: mCurrentStream(0),
|
2011-10-17 07:59:28 -07:00
|
|
|
mStartedReadingCurrent(false),
|
2007-03-22 10:30:00 -07:00
|
|
|
mStatus(NS_OK)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute unsigned long count; */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMultiplexInputStream::GetCount(uint32_t *aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-04 14:53:52 -07:00
|
|
|
*aCount = mStreams.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void appendStream (in nsIInputStream stream); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::AppendStream(nsIInputStream *aStream)
|
|
|
|
{
|
2012-09-04 14:53:52 -07:00
|
|
|
return mStreams.AppendElement(aStream) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* void insertStream (in nsIInputStream stream, in unsigned long index); */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMultiplexInputStream::InsertStream(nsIInputStream *aStream, uint32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-04 14:53:52 -07:00
|
|
|
bool result = mStreams.InsertElementAt(aIndex, aStream);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(result, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
if (mCurrentStream > aIndex ||
|
|
|
|
(mCurrentStream == aIndex && mStartedReadingCurrent))
|
|
|
|
++mCurrentStream;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void removeStream (in unsigned long index); */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMultiplexInputStream::RemoveStream(uint32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-04 14:53:52 -07:00
|
|
|
mStreams.RemoveElementAt(aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mCurrentStream > aIndex)
|
|
|
|
--mCurrentStream;
|
|
|
|
else if (mCurrentStream == aIndex)
|
2011-10-17 07:59:28 -07:00
|
|
|
mStartedReadingCurrent = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nsIInputStream getStream (in unsigned long index); */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMultiplexInputStream::GetStream(uint32_t aIndex, nsIInputStream **_retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-04 14:53:52 -07:00
|
|
|
*_retval = mStreams.SafeElementAt(aIndex, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(*_retval, NS_ERROR_NOT_AVAILABLE);
|
|
|
|
|
|
|
|
NS_ADDREF(*_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void close (); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::Close()
|
|
|
|
{
|
|
|
|
mStatus = NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-09-04 14:53:52 -07:00
|
|
|
uint32_t len = mStreams.Length();
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv2 = mStreams[i]->Close();
|
|
|
|
// We still want to close all streams, but we should return an error
|
|
|
|
if (NS_FAILED(rv2))
|
|
|
|
rv = rv2;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-10 19:44:11 -07:00
|
|
|
/* unsigned long long available (); */
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMultiplexInputStream::Available(uint64_t *_retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t avail = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-04 14:53:52 -07:00
|
|
|
uint32_t len = mStreams.Length();
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = mCurrentStream; i < len; i++) {
|
|
|
|
uint64_t streamAvail;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mStreams[i]->Available(&streamAvail);
|
|
|
|
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 08:56:38 -07:00
|
|
|
nsMultiplexInputStream::Read(char * aBuf, uint32_t aCount, uint32_t *_retval)
|
2007-03-22 10:30:00 -07: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).
|
|
|
|
|
|
|
|
*_retval = 0;
|
|
|
|
|
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED)
|
|
|
|
return NS_OK;
|
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-09-04 14:53:52 -07:00
|
|
|
uint32_t len = mStreams.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
while (mCurrentStream < len && aCount) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t read;
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
else if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (read == 0) {
|
|
|
|
++mCurrentStream;
|
2011-10-17 07:59:28 -07:00
|
|
|
mStartedReadingCurrent = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(aCount >= read, "Read more than requested");
|
|
|
|
*_retval += read;
|
|
|
|
aCount -= read;
|
|
|
|
aBuf += read;
|
2011-10-17 07:59:28 -07:00
|
|
|
mStartedReadingCurrent = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *_retval ? NS_OK : rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [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 08:56:38 -07:00
|
|
|
uint32_t aCount, uint32_t *_retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED) {
|
|
|
|
*_retval = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
|
|
|
NS_ASSERTION(aWriter, "missing aWriter");
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
ReadSegmentsState state;
|
|
|
|
state.mThisStream = this;
|
|
|
|
state.mOffset = 0;
|
|
|
|
state.mWriter = aWriter;
|
|
|
|
state.mClosure = aClosure;
|
2011-10-17 07:59:28 -07:00
|
|
|
state.mDone = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-04 14:53:52 -07:00
|
|
|
uint32_t len = mStreams.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
while (mCurrentStream < len && aCount) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t read;
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if |aWriter| decided to stop reading segments...
|
|
|
|
if (state.mDone || NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// if stream is empty, then advance to the next stream.
|
|
|
|
if (read == 0) {
|
|
|
|
++mCurrentStream;
|
2011-10-17 07:59:28 -07:00
|
|
|
mStartedReadingCurrent = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(aCount >= read, "Read more than requested");
|
|
|
|
state.mOffset += read;
|
|
|
|
aCount -= read;
|
2011-10-17 07:59:28 -07:00
|
|
|
mStartedReadingCurrent = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we successfully read some data, then this call succeeded.
|
|
|
|
*_retval = state.mOffset;
|
|
|
|
return state.mOffset ? NS_OK : rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsMultiplexInputStream::ReadSegCb(nsIInputStream* aIn, void* aClosure,
|
|
|
|
const char* aFromRawSegment,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aToOffset, uint32_t aCount,
|
|
|
|
uint32_t *aWriteCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
ReadSegmentsState* state = (ReadSegmentsState*)aClosure;
|
|
|
|
rv = (state->mWriter)(state->mThisStream,
|
|
|
|
state->mClosure,
|
|
|
|
aFromRawSegment,
|
|
|
|
aToOffset + state->mOffset,
|
|
|
|
aCount,
|
|
|
|
aWriteCount);
|
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 07:59:28 -07:00
|
|
|
state->mDone = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute boolean nonBlocking; */
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsMultiplexInputStream::IsNonBlocking(bool *aNonBlocking)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-09-04 14:53:52 -07:00
|
|
|
uint32_t len = mStreams.Length();
|
2012-02-07 12:28:06 -08: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 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = mStreams[i]->IsNonBlocking(aNonBlocking);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// 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")
|
|
|
|
if (*aNonBlocking)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
/* void seek (in int32_t whence, in int32_t offset); */
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMultiplexInputStream::Seek(int32_t aWhence, int64_t aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-09-04 14:53:52 -07:00
|
|
|
uint32_t oldCurrentStream = mCurrentStream;
|
|
|
|
bool oldStartedReadingCurrent = mStartedReadingCurrent;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-04 14:53:52 -07: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);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get position in current stream
|
|
|
|
int64_t streamPos;
|
|
|
|
if (i > oldCurrentStream ||
|
|
|
|
(i == oldCurrentStream && !oldStartedReadingCurrent)) {
|
|
|
|
streamPos = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = stream->Tell(&streamPos);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we need to seek current stream forward or backward
|
|
|
|
if (remaining < streamPos) {
|
|
|
|
rv = stream->Seek(NS_SEEK_SET, remaining);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = remaining != 0;
|
|
|
|
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
else if (remaining > streamPos) {
|
|
|
|
if (i < oldCurrentStream) {
|
|
|
|
// We're already at end so no need to seek this stream
|
|
|
|
remaining -= streamPos;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uint64_t avail;
|
|
|
|
rv = mStreams[i]->Available(&avail);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
int64_t newPos = streamPos +
|
|
|
|
NS_MIN((int64_t)avail, remaining);
|
|
|
|
|
|
|
|
rv = stream->Seek(NS_SEEK_SET, newPos);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = true;
|
|
|
|
|
|
|
|
remaining -= newPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(remaining == streamPos, "Huh?");
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
|
|
|
uint64_t avail;
|
|
|
|
rv = mStreams[i]->Available(&avail);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
int64_t seek = NS_MIN((int64_t)avail, remaining);
|
|
|
|
|
|
|
|
rv = stream->Seek(NS_SEEK_CUR, seek);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-04 14:53:52 -07:00
|
|
|
|
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = true;
|
|
|
|
|
|
|
|
remaining -= seek;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-09-04 14:53:52 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
|
|
|
int64_t pos;
|
|
|
|
rv = stream->Tell(&pos);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
int64_t seek = NS_MIN(pos, remaining);
|
|
|
|
|
|
|
|
rv = stream->Seek(NS_SEEK_CUR, -seek);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = seek != -pos;
|
|
|
|
|
|
|
|
remaining -= seek;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWhence == NS_SEEK_CUR) {
|
|
|
|
NS_ASSERTION(aOffset == 0, "Should have handled all non-zero values");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get position in current stream
|
|
|
|
int64_t streamPos;
|
|
|
|
if (i < oldCurrentStream) {
|
|
|
|
streamPos = 0;
|
|
|
|
} else {
|
|
|
|
uint64_t avail;
|
|
|
|
rv = mStreams[i]->Available(&avail);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
streamPos = avail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we have enough data in the current stream.
|
|
|
|
if (NS_ABS(remaining) < streamPos) {
|
|
|
|
rv = stream->Seek(NS_SEEK_END, remaining);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = true;
|
|
|
|
|
|
|
|
remaining = 0;
|
|
|
|
} else if (NS_ABS(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 = stream->Tell(&avail);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
int64_t newPos = streamPos + NS_MIN(avail, NS_ABS(remaining));
|
|
|
|
|
|
|
|
rv = stream->Seek(NS_SEEK_END, -newPos);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mCurrentStream = i;
|
|
|
|
mStartedReadingCurrent = true;
|
|
|
|
|
|
|
|
remaining += newPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ASSERTION(remaining == streamPos, "Huh?");
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// other Seeks not implemented yet
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
/* uint32_t tell (); */
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMultiplexInputStream::Tell(int64_t *_retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return mStatus;
|
|
|
|
|
|
|
|
nsresult rv;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t ret64 = 0;
|
|
|
|
uint32_t i, last;
|
2007-03-22 10:30:00 -07: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);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t pos;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = stream->Tell(&pos);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
ret64 += pos;
|
|
|
|
}
|
|
|
|
*_retval = ret64;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* void setEOF (); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMultiplexInputStream::SetEOF()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMultiplexInputStreamConstructor(nsISupports *outer,
|
|
|
|
REFNSIID iid,
|
|
|
|
void **result)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (outer)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
2010-07-05 02:42:18 -07:00
|
|
|
nsMultiplexInputStream *inst = new nsMultiplexInputStream();
|
2007-03-22 10:30:00 -07: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 11:36:13 -07:00
|
|
|
|
2012-08-15 21:02:32 -07:00
|
|
|
void
|
|
|
|
nsMultiplexInputStream::Serialize(InputStreamParams& aParams)
|
|
|
|
{
|
|
|
|
MultiplexInputStreamParams params;
|
|
|
|
|
2012-09-04 14:53:52 -07:00
|
|
|
uint32_t streamCount = mStreams.Length();
|
2012-08-15 21:02:32 -07:00
|
|
|
|
|
|
|
if (streamCount) {
|
|
|
|
InfallibleTArray<InputStreamParams>& streams = params.streams();
|
|
|
|
|
|
|
|
streams.SetCapacity(streamCount);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t index = 0; index < streamCount; index++) {
|
2012-08-15 21:02:32 -07:00
|
|
|
nsCOMPtr<nsIIPCSerializableInputStream> serializable =
|
2012-09-04 14:53:52 -07:00
|
|
|
do_QueryInterface(mStreams[index]);
|
2012-08-15 21:02:32 -07: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 08:56:38 -07:00
|
|
|
uint32_t streamCount = streams.Length();
|
|
|
|
for (uint32_t index = 0; index < streamCount; index++) {
|
2012-08-15 21:02:32 -07: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;
|
|
|
|
}
|