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/. */
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
#include <algorithm>
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-04-29 23:21:57 +04:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2016-05-15 20:32:09 +03:00
|
|
|
#include "nsIBufferedStreams.h"
|
2015-02-11 07:55:43 +03:00
|
|
|
#include "nsICloneableInputStream.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsIPipe.h"
|
2003-10-06 05:46:31 +04:00
|
|
|
#include "nsIEventTarget.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsISeekableStream.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsSegmentedBuffer.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCRT.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2007-07-25 00:17:11 +04:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2011-06-02 16:56:50 +04:00
|
|
|
#include "nsAlgorithm.h"
|
2013-08-22 14:32:11 +04:00
|
|
|
#include "nsMemory.h"
|
2013-09-23 21:29:27 +04:00
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-11-19 06:34:00 +04:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
2016-05-26 01:26:10 +03:00
|
|
|
// set MOZ_LOG=nsPipe:5
|
2003-01-18 04:27:53 +03:00
|
|
|
//
|
2015-10-19 22:50:14 +03:00
|
|
|
static LazyLogModule sPipeLog("nsPipe");
|
|
|
|
#define LOG(args) MOZ_LOG(sPipeLog, mozilla::LogLevel::Debug, args)
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
#define DEFAULT_SEGMENT_SIZE 4096
|
|
|
|
#define DEFAULT_SEGMENT_COUNT 16
|
|
|
|
|
|
|
|
class nsPipe;
|
|
|
|
class nsPipeEvents;
|
|
|
|
class nsPipeInputStream;
|
|
|
|
class nsPipeOutputStream;
|
2015-02-21 17:51:17 +03:00
|
|
|
class AutoReadSegment;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum MonitorAction
|
|
|
|
{
|
|
|
|
DoNotNotifyMonitor,
|
|
|
|
NotifyMonitor
|
|
|
|
};
|
|
|
|
|
|
|
|
enum SegmentChangeResult
|
|
|
|
{
|
|
|
|
SegmentNotChanged,
|
|
|
|
SegmentDeleted
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
// this class is used to delay notifications until the end of a particular
|
|
|
|
// scope. it helps avoid the complexity of issuing callbacks while inside
|
|
|
|
// a critical section.
|
2003-01-18 04:27:53 +03:00
|
|
|
class nsPipeEvents
|
|
|
|
{
|
|
|
|
public:
|
2014-05-05 21:30:43 +04:00
|
|
|
nsPipeEvents() { }
|
|
|
|
~nsPipeEvents();
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
inline void NotifyInputReady(nsIAsyncInputStream* aStream,
|
|
|
|
nsIInputStreamCallback* aCallback)
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
2015-09-14 18:49:09 +03:00
|
|
|
mInputList.AppendElement(InputEntry(aStream, aCallback));
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
inline void NotifyOutputReady(nsIAsyncOutputStream* aStream,
|
|
|
|
nsIOutputStreamCallback* aCallback)
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!mOutputCallback, "already have an output event");
|
2014-05-15 00:15:46 +04:00
|
|
|
mOutputStream = aStream;
|
|
|
|
mOutputCallback = aCallback;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
private:
|
2015-09-14 18:49:09 +03:00
|
|
|
struct InputEntry
|
|
|
|
{
|
|
|
|
InputEntry(nsIAsyncInputStream* aStream, nsIInputStreamCallback* aCallback)
|
|
|
|
: mStream(aStream)
|
|
|
|
, mCallback(aCallback)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mStream);
|
|
|
|
MOZ_ASSERT(mCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mStream;
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsTArray<InputEntry> mInputList;
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mOutputStream;
|
|
|
|
nsCOMPtr<nsIOutputStreamCallback> mOutputCallback;
|
2003-01-18 04:27:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
// This class is used to maintain input stream state. Its broken out from the
|
|
|
|
// nsPipeInputStream class because generally the nsPipe should be modifying
|
|
|
|
// this state and not the input stream itself.
|
|
|
|
struct nsPipeReadState
|
|
|
|
{
|
|
|
|
nsPipeReadState()
|
|
|
|
: mReadCursor(nullptr)
|
|
|
|
, mReadLimit(nullptr)
|
|
|
|
, mSegment(0)
|
2015-02-21 17:51:17 +03:00
|
|
|
, mAvailable(0)
|
|
|
|
, mActiveRead(false)
|
|
|
|
, mNeedDrain(false)
|
2015-02-11 07:55:43 +03:00
|
|
|
{ }
|
|
|
|
|
|
|
|
char* mReadCursor;
|
|
|
|
char* mReadLimit;
|
|
|
|
int32_t mSegment;
|
2015-02-21 17:51:17 +03:00
|
|
|
uint32_t mAvailable;
|
|
|
|
|
|
|
|
// This flag is managed using the AutoReadSegment RAII stack class.
|
|
|
|
bool mActiveRead;
|
|
|
|
|
|
|
|
// Set to indicate that the input stream has closed and should be drained,
|
|
|
|
// but that drain has been delayed due to an active read. When the read
|
|
|
|
// completes, this flag indicate the drain should then be performed.
|
|
|
|
bool mNeedDrain;
|
2015-02-11 07:55:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// an input end of a pipe (maintained as a list of refs within the pipe)
|
2015-03-21 21:35:18 +03:00
|
|
|
class nsPipeInputStream final
|
2014-05-15 00:15:46 +04:00
|
|
|
: public nsIAsyncInputStream
|
|
|
|
, public nsISeekableStream
|
|
|
|
, public nsISearchableInputStream
|
2015-02-11 07:55:43 +03:00
|
|
|
, public nsICloneableInputStream
|
2014-05-15 00:15:46 +04:00
|
|
|
, public nsIClassInfo
|
2016-05-15 20:32:09 +03:00
|
|
|
, public nsIBufferedInputStream
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
public:
|
2015-02-11 07:55:43 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
|
|
|
NS_DECL_NSISEARCHABLEINPUTSTREAM
|
2015-02-11 07:55:43 +03:00
|
|
|
NS_DECL_NSICLONEABLEINPUTSTREAM
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_DECL_NSICLASSINFO
|
2016-05-15 20:32:09 +03:00
|
|
|
NS_DECL_NSIBUFFEREDINPUTSTREAM
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit nsPipeInputStream(nsPipe* aPipe)
|
2014-05-15 00:15:46 +04:00
|
|
|
: mPipe(aPipe)
|
2014-05-05 21:30:43 +04:00
|
|
|
, mLogicalOffset(0)
|
2015-02-11 07:55:43 +03:00
|
|
|
, mInputStatus(NS_OK)
|
2014-05-05 21:30:43 +04:00
|
|
|
, mBlocking(true)
|
|
|
|
, mBlocked(false)
|
|
|
|
, mCallbackFlags(0)
|
|
|
|
{ }
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
explicit nsPipeInputStream(const nsPipeInputStream& aOther)
|
|
|
|
: mPipe(aOther.mPipe)
|
|
|
|
, mLogicalOffset(aOther.mLogicalOffset)
|
|
|
|
, mInputStatus(aOther.mInputStatus)
|
|
|
|
, mBlocking(aOther.mBlocking)
|
|
|
|
, mBlocked(false)
|
|
|
|
, mCallbackFlags(0)
|
|
|
|
, mReadState(aOther.mReadState)
|
|
|
|
{ }
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult Fill();
|
2014-05-15 00:15:46 +04:00
|
|
|
void SetNonBlocking(bool aNonBlocking)
|
|
|
|
{
|
|
|
|
mBlocking = !aNonBlocking;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
uint32_t Available();
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// synchronously wait for the pipe to become readable.
|
|
|
|
nsresult Wait();
|
|
|
|
|
2016-02-17 13:22:28 +03:00
|
|
|
// These two don't acquire the monitor themselves. Instead they
|
|
|
|
// expect their caller to have done so and to pass the monitor as
|
|
|
|
// evidence.
|
|
|
|
MonitorAction OnInputReadable(uint32_t aBytesWritten, nsPipeEvents&,
|
|
|
|
const ReentrantMonitorAutoEnter& ev);
|
|
|
|
MonitorAction OnInputException(nsresult, nsPipeEvents&,
|
|
|
|
const ReentrantMonitorAutoEnter& ev);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
nsPipeReadState& ReadState()
|
|
|
|
{
|
|
|
|
return mReadState;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsPipeReadState& ReadState() const
|
|
|
|
{
|
|
|
|
return mReadState;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Status() const;
|
|
|
|
|
2016-02-17 13:22:28 +03:00
|
|
|
// A version of Status() that doesn't acquire the monitor.
|
|
|
|
nsresult Status(const ReentrantMonitorAutoEnter& ev) const;
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
private:
|
2015-02-11 07:55:43 +03:00
|
|
|
virtual ~nsPipeInputStream();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPipe> mPipe;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
int64_t mLogicalOffset;
|
2015-02-11 07:55:43 +03:00
|
|
|
// Individual input streams can be closed without effecting the rest of the
|
2016-02-17 13:22:28 +03:00
|
|
|
// pipe. So track individual input stream status separately. |mInputStatus|
|
|
|
|
// is protected by |mPipe->mReentrantMonitor|.
|
2015-02-11 07:55:43 +03:00
|
|
|
nsresult mInputStatus;
|
2014-05-05 21:30:43 +04:00
|
|
|
bool mBlocking;
|
|
|
|
|
|
|
|
// these variables can only be accessed while inside the pipe's monitor
|
|
|
|
bool mBlocked;
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> mCallback;
|
|
|
|
uint32_t mCallbackFlags;
|
2015-02-11 07:55:43 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
// requires pipe's monitor; usually treat as an opaque token to pass to nsPipe
|
2015-02-11 07:55:43 +03:00
|
|
|
nsPipeReadState mReadState;
|
2003-01-18 04:27:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
// the output end of a pipe (allocated as a member of the pipe).
|
2014-05-15 00:15:46 +04:00
|
|
|
class nsPipeOutputStream
|
|
|
|
: public nsIAsyncOutputStream
|
|
|
|
, public nsIClassInfo
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-05-05 21:30:43 +04:00
|
|
|
// since this class will be allocated as a member of the pipe, we do not
|
|
|
|
// need our own ref count. instead, we share the lifetime (the ref count)
|
|
|
|
// of the entire pipe. this macro is just convenience since it does not
|
|
|
|
// declare a mRefCount variable; however, don't let the name fool you...
|
|
|
|
// we are not inheriting from nsPipe ;-)
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
NS_DECL_NSIOUTPUTSTREAM
|
|
|
|
NS_DECL_NSIASYNCOUTPUTSTREAM
|
|
|
|
NS_DECL_NSICLASSINFO
|
|
|
|
|
2014-08-05 17:36:59 +04:00
|
|
|
explicit nsPipeOutputStream(nsPipe* aPipe)
|
2014-05-15 00:15:46 +04:00
|
|
|
: mPipe(aPipe)
|
2014-05-05 21:30:43 +04:00
|
|
|
, mWriterRefCnt(0)
|
|
|
|
, mLogicalOffset(0)
|
|
|
|
, mBlocking(true)
|
|
|
|
, mBlocked(false)
|
|
|
|
, mWritable(true)
|
|
|
|
, mCallbackFlags(0)
|
|
|
|
{ }
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
void SetNonBlocking(bool aNonBlocking)
|
|
|
|
{
|
|
|
|
mBlocking = !aNonBlocking;
|
|
|
|
}
|
|
|
|
void SetWritable(bool aWritable)
|
|
|
|
{
|
|
|
|
mWritable = aWritable;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// synchronously wait for the pipe to become writable.
|
|
|
|
nsresult Wait();
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
MonitorAction OnOutputWritable(nsPipeEvents&);
|
|
|
|
MonitorAction OnOutputException(nsresult, nsPipeEvents&);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
private:
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipe* mPipe;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// separate refcnt so that we know when to close the producer
|
|
|
|
mozilla::ThreadSafeAutoRefCnt mWriterRefCnt;
|
|
|
|
int64_t mLogicalOffset;
|
|
|
|
bool mBlocking;
|
|
|
|
|
|
|
|
// these variables can only be accessed while inside the pipe's monitor
|
|
|
|
bool mBlocked;
|
|
|
|
bool mWritable;
|
|
|
|
nsCOMPtr<nsIOutputStreamCallback> mCallback;
|
|
|
|
uint32_t mCallbackFlags;
|
2003-01-18 04:27:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsPipe final : public nsIPipe
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-05-05 21:30:43 +04:00
|
|
|
friend class nsPipeInputStream;
|
|
|
|
friend class nsPipeOutputStream;
|
2015-02-21 17:51:17 +03:00
|
|
|
friend class AutoReadSegment;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIPIPE
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// nsPipe methods:
|
|
|
|
nsPipe();
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2004-02-11 09:19:03 +03:00
|
|
|
private:
|
2014-05-05 21:30:43 +04:00
|
|
|
~nsPipe();
|
2004-02-11 09:19:03 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
//
|
|
|
|
// methods below may only be called while inside the pipe's monitor
|
|
|
|
//
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
void PeekSegment(const nsPipeReadState& aReadState, uint32_t aIndex,
|
|
|
|
char*& aCursor, char*& aLimit);
|
2015-02-21 17:51:17 +03:00
|
|
|
SegmentChangeResult AdvanceReadSegment(nsPipeReadState& aReadState);
|
|
|
|
bool ReadSegmentBeingWritten(nsPipeReadState& aReadState);
|
|
|
|
uint32_t CountSegmentReferences(int32_t aSegment);
|
|
|
|
void SetAllNullReadCursors();
|
|
|
|
bool AllReadCursorsMatchWriteCursor();
|
|
|
|
void RollBackAllReadCursors(char* aWriteCursor);
|
|
|
|
void UpdateAllReadCursors(char* aWriteCursor);
|
|
|
|
void ValidateAllReadCursors();
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
//
|
|
|
|
// methods below may be called while outside the pipe's monitor
|
|
|
|
//
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
void DrainInputStream(nsPipeReadState& aReadState, nsPipeEvents& aEvents);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult GetWriteSegment(char*& aSegment, uint32_t& aSegmentLen);
|
|
|
|
void AdvanceWriteCursor(uint32_t aCount);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
void OnInputStreamException(nsPipeInputStream* aStream, nsresult aReason);
|
2014-05-15 00:15:46 +04:00
|
|
|
void OnPipeException(nsresult aReason, bool aOutputOnly = false);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
nsresult CloneInputStream(nsPipeInputStream* aOriginal,
|
|
|
|
nsIInputStream** aCloneOut);
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
// methods below should only be called by AutoReadSegment
|
|
|
|
nsresult GetReadSegment(nsPipeReadState& aReadState, const char*& aSegment,
|
|
|
|
uint32_t& aLength);
|
|
|
|
void ReleaseReadSegment(nsPipeReadState& aReadState,
|
|
|
|
nsPipeEvents& aEvents);
|
|
|
|
void AdvanceReadCursor(nsPipeReadState& aReadState, uint32_t aCount);
|
2015-02-11 07:55:43 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// We can't inherit from both nsIInputStream and nsIOutputStream
|
|
|
|
// because they collide on their Close method. Consequently we nest their
|
|
|
|
// implementations to avoid the extra object allocation.
|
|
|
|
nsPipeOutputStream mOutput;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
// Since the input stream can be cloned, we may have more than one. Use
|
|
|
|
// a weak reference as the streams will clear their entry here in their
|
|
|
|
// destructor. Using a strong reference would create a reference cycle.
|
|
|
|
// Only usable while mReentrantMonitor is locked.
|
|
|
|
nsTArray<nsPipeInputStream*> mInputList;
|
|
|
|
|
|
|
|
// But hold a strong ref to our original input stream. For backward
|
|
|
|
// compatibility we need to be able to consistently return this same
|
|
|
|
// object from GetInputStream(). Note, mOriginalInput is also stored
|
|
|
|
// in mInputList as a weak ref.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPipeInputStream> mOriginalInput;
|
2015-02-11 07:55:43 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
ReentrantMonitor mReentrantMonitor;
|
|
|
|
nsSegmentedBuffer mBuffer;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
int32_t mWriteSegment;
|
|
|
|
char* mWriteCursor;
|
|
|
|
char* mWriteLimit;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-02-17 13:22:28 +03:00
|
|
|
// |mStatus| is protected by |mReentrantMonitor|.
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult mStatus;
|
|
|
|
bool mInited;
|
2003-01-18 04:27:53 +03:00
|
|
|
};
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// RAII class representing an active read segment. When it goes out of scope
|
|
|
|
// it automatically updates the read cursor and releases the read segment.
|
2015-03-21 19:28:04 +03:00
|
|
|
class MOZ_STACK_CLASS AutoReadSegment final
|
2015-02-21 17:51:17 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoReadSegment(nsPipe* aPipe, nsPipeReadState& aReadState,
|
|
|
|
uint32_t aMaxLength)
|
|
|
|
: mPipe(aPipe)
|
|
|
|
, mReadState(aReadState)
|
|
|
|
, mStatus(NS_ERROR_FAILURE)
|
|
|
|
, mSegment(nullptr)
|
|
|
|
, mLength(0)
|
|
|
|
, mOffset(0)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPipe);
|
|
|
|
MOZ_ASSERT(!mReadState.mActiveRead);
|
|
|
|
mStatus = mPipe->GetReadSegment(mReadState, mSegment, mLength);
|
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
|
|
|
MOZ_ASSERT(mReadState.mActiveRead);
|
|
|
|
MOZ_ASSERT(mSegment);
|
|
|
|
mLength = std::min(mLength, aMaxLength);
|
|
|
|
MOZ_ASSERT(mLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoReadSegment()
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
|
|
|
if (mOffset) {
|
|
|
|
mPipe->AdvanceReadCursor(mReadState, mOffset);
|
|
|
|
} else {
|
|
|
|
nsPipeEvents events;
|
|
|
|
mPipe->ReleaseReadSegment(mReadState, events);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(!mReadState.mActiveRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Status() const
|
|
|
|
{
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* Data() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mStatus));
|
|
|
|
MOZ_ASSERT(mSegment);
|
|
|
|
return mSegment + mOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Length() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mStatus));
|
|
|
|
MOZ_ASSERT(mLength >= mOffset);
|
|
|
|
return mLength - mOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Advance(uint32_t aCount)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mStatus));
|
|
|
|
MOZ_ASSERT(aCount <= (mLength - mOffset));
|
|
|
|
mOffset += aCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPipeReadState&
|
|
|
|
ReadState() const
|
|
|
|
{
|
|
|
|
return mReadState;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// guaranteed to remain alive due to limited stack lifetime of AutoReadSegment
|
|
|
|
nsPipe* mPipe;
|
|
|
|
nsPipeReadState& mReadState;
|
|
|
|
nsresult mStatus;
|
|
|
|
const char* mSegment;
|
|
|
|
uint32_t mLength;
|
|
|
|
uint32_t mOffset;
|
|
|
|
};
|
|
|
|
|
2003-01-23 05:39:01 +03:00
|
|
|
//
|
|
|
|
// NOTES on buffer architecture:
|
|
|
|
//
|
|
|
|
// +-----------------+ - - mBuffer.GetSegment(0)
|
|
|
|
// | |
|
2015-02-11 07:55:43 +03:00
|
|
|
// + - - - - - - - - + - - nsPipeReadState.mReadCursor
|
2003-01-23 05:39:01 +03:00
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
2015-02-11 07:55:43 +03:00
|
|
|
// +-----------------+ - - nsPipeReadState.mReadLimit
|
2003-01-23 05:39:01 +03:00
|
|
|
// |
|
|
|
|
// +-----------------+
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// +-----------------+
|
|
|
|
// |
|
|
|
|
// +-----------------+ - - mBuffer.GetSegment(mWriteSegment)
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// + - - - - - - - - + - - mWriteCursor
|
|
|
|
// | |
|
|
|
|
// | |
|
|
|
|
// +-----------------+ - - mWriteLimit
|
|
|
|
//
|
|
|
|
// (shaded region contains data)
|
|
|
|
//
|
2015-02-11 07:55:43 +03:00
|
|
|
// NOTE: Each input stream produced by the nsPipe contains its own, separate
|
|
|
|
// nsPipeReadState. This means there are multiple mReadCursor and
|
|
|
|
// mReadLimit values in play. The pipe cannot discard old data until
|
|
|
|
// all mReadCursors have moved beyond that point in the stream.
|
|
|
|
//
|
2003-04-05 06:02:09 +04:00
|
|
|
// NOTE: on some systems (notably OS/2), the heap allocator uses an arena for
|
|
|
|
// small allocations (e.g., 64 byte allocations). this means that buffers may
|
|
|
|
// be allocated back-to-back. in the diagram above, for example, mReadLimit
|
|
|
|
// would actually be pointing at the beginning of the next segment. when
|
|
|
|
// making changes to this file, please keep this fact in mind.
|
|
|
|
//
|
2003-01-23 05:39:01 +03:00
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPipe methods:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsPipe::nsPipe()
|
2015-02-11 07:55:43 +03:00
|
|
|
: mOutput(this)
|
|
|
|
, mOriginalInput(new nsPipeInputStream(this))
|
2014-05-05 21:30:43 +04:00
|
|
|
, mReentrantMonitor("nsPipe.mReentrantMonitor")
|
|
|
|
, mWriteSegment(-1)
|
|
|
|
, mWriteCursor(nullptr)
|
|
|
|
, mWriteLimit(nullptr)
|
|
|
|
, mStatus(NS_OK)
|
|
|
|
, mInited(false)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2015-02-11 07:55:43 +03:00
|
|
|
mInputList.AppendElement(mOriginalInput);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPipe::~nsPipe()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
NS_IMPL_ADDREF(nsPipe)
|
|
|
|
NS_IMPL_QUERY_INTERFACE(nsPipe, nsIPipe)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
|
|
|
nsPipe::Release()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(int32_t(mRefCnt) > 0, "dup release");
|
|
|
|
nsrefcnt count = --mRefCnt;
|
|
|
|
NS_LOG_RELEASE(this, count, "nsPipe");
|
|
|
|
if (count == 0) {
|
|
|
|
delete (this);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-17 13:22:28 +03:00
|
|
|
// Avoid racing on |mOriginalInput| by only looking at it when
|
|
|
|
// the refcount is 1, that is, we are the only pointer (hence only
|
|
|
|
// thread) to access it.
|
|
|
|
if (count == 1 && mOriginalInput) {
|
2015-02-11 07:55:43 +03:00
|
|
|
mOriginalInput = nullptr;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipe::Init(bool aNonBlockingIn,
|
|
|
|
bool aNonBlockingOut,
|
|
|
|
uint32_t aSegmentSize,
|
|
|
|
uint32_t aSegmentCount)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
mInited = true;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aSegmentSize == 0) {
|
|
|
|
aSegmentSize = DEFAULT_SEGMENT_SIZE;
|
|
|
|
}
|
|
|
|
if (aSegmentCount == 0) {
|
|
|
|
aSegmentCount = DEFAULT_SEGMENT_COUNT;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// protect against overflow
|
2014-05-15 00:15:46 +04:00
|
|
|
uint32_t maxCount = uint32_t(-1) / aSegmentSize;
|
|
|
|
if (aSegmentCount > maxCount) {
|
|
|
|
aSegmentCount = maxCount;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = mBuffer.Init(aSegmentSize, aSegmentSize * aSegmentCount);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
mOutput.SetNonBlocking(aNonBlockingOut);
|
2015-02-11 07:55:43 +03:00
|
|
|
mOriginalInput->SetNonBlocking(aNonBlockingIn);
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipe::GetInputStream(nsIAsyncInputStream** aInputStream)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2016-08-17 09:52:48 +03:00
|
|
|
if (NS_WARN_IF(!mInited)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPipeInputStream> ref = mOriginalInput;
|
2015-02-11 07:55:43 +03:00
|
|
|
ref.forget(aInputStream);
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipe::GetOutputStream(nsIAsyncOutputStream** aOutputStream)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!mInited)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ADDREF(*aOutputStream = &mOutput);
|
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2003-01-23 05:39:01 +03:00
|
|
|
void
|
2015-02-11 07:55:43 +03:00
|
|
|
nsPipe::PeekSegment(const nsPipeReadState& aReadState, uint32_t aIndex,
|
|
|
|
char*& aCursor, char*& aLimit)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aIndex == 0) {
|
2015-02-11 07:55:43 +03:00
|
|
|
NS_ASSERTION(!aReadState.mReadCursor || mBuffer.GetSegmentCount(),
|
|
|
|
"unexpected state");
|
|
|
|
aCursor = aReadState.mReadCursor;
|
|
|
|
aLimit = aReadState.mReadLimit;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2015-02-11 07:55:43 +03:00
|
|
|
uint32_t absoluteIndex = aReadState.mSegment + aIndex;
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t numSegments = mBuffer.GetSegmentCount();
|
2015-02-11 07:55:43 +03:00
|
|
|
if (absoluteIndex >= numSegments) {
|
2014-05-15 00:15:46 +04:00
|
|
|
aCursor = aLimit = nullptr;
|
|
|
|
} else {
|
2015-02-11 07:55:43 +03:00
|
|
|
aCursor = mBuffer.GetSegment(absoluteIndex);
|
|
|
|
if (mWriteSegment == (int32_t)absoluteIndex) {
|
2014-05-15 00:15:46 +04:00
|
|
|
aLimit = mWriteCursor;
|
|
|
|
} else {
|
|
|
|
aLimit = aCursor + mBuffer.GetSegmentSize();
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-02-21 17:51:17 +03:00
|
|
|
nsPipe::GetReadSegment(nsPipeReadState& aReadState, const char*& aSegment,
|
|
|
|
uint32_t& aLength)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
if (aReadState.mReadCursor == aReadState.mReadLimit) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_FAILED(mStatus) ? mStatus : NS_BASE_STREAM_WOULD_BLOCK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
// The input stream locks the pipe while getting the buffer to read from,
|
|
|
|
// but then unlocks while actual data copying is taking place. In
|
|
|
|
// order to avoid deleting the buffer out from under this lockless read
|
|
|
|
// set a flag to indicate a read is active. This flag is only modified
|
|
|
|
// while the lock is held.
|
|
|
|
MOZ_ASSERT(!aReadState.mActiveRead);
|
|
|
|
aReadState.mActiveRead = true;
|
|
|
|
|
|
|
|
aSegment = aReadState.mReadCursor;
|
|
|
|
aLength = aReadState.mReadLimit - aReadState.mReadCursor;
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-02-21 17:51:17 +03:00
|
|
|
nsPipe::ReleaseReadSegment(nsPipeReadState& aReadState, nsPipeEvents& aEvents)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
|
|
|
MOZ_ASSERT(aReadState.mActiveRead);
|
|
|
|
aReadState.mActiveRead = false;
|
|
|
|
|
|
|
|
// When a read completes and releases the mActiveRead flag, we may have blocked
|
|
|
|
// a drain from completing. This occurs when the input stream is closed during
|
|
|
|
// the read. In these cases, we need to complete the drain as soon as the
|
|
|
|
// active read completes.
|
|
|
|
if (aReadState.mNeedDrain) {
|
|
|
|
aReadState.mNeedDrain = false;
|
|
|
|
DrainInputStream(aReadState, aEvents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPipe::AdvanceReadCursor(nsPipeReadState& aReadState, uint32_t aBytesRead)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(aBytesRead, "don't call if no bytes read");
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsPipeEvents events;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
LOG(("III advancing read cursor by %u\n", aBytesRead));
|
|
|
|
NS_ASSERTION(aBytesRead <= mBuffer.GetSegmentSize(), "read too much");
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
aReadState.mReadCursor += aBytesRead;
|
|
|
|
NS_ASSERTION(aReadState.mReadCursor <= aReadState.mReadLimit,
|
|
|
|
"read cursor exceeds limit");
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
MOZ_ASSERT(aReadState.mAvailable >= aBytesRead);
|
|
|
|
aReadState.mAvailable -= aBytesRead;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
// Check to see if we're at the end of the available read data. If we
|
|
|
|
// are, and this segment is not still being written, then we can possibly
|
|
|
|
// free up the segment.
|
|
|
|
if (aReadState.mReadCursor == aReadState.mReadLimit &&
|
|
|
|
!ReadSegmentBeingWritten(aReadState)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
// Check to see if we can free up any segments. If we can, then notify
|
|
|
|
// the output stream that the pipe has room for a new segment.
|
|
|
|
if (AdvanceReadSegment(aReadState) == SegmentDeleted &&
|
|
|
|
mOutput.OnOutputWritable(events) == NotifyMonitor) {
|
|
|
|
mon.NotifyAll();
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2015-02-21 17:51:17 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
ReleaseReadSegment(aReadState, events);
|
|
|
|
}
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
SegmentChangeResult
|
|
|
|
nsPipe::AdvanceReadSegment(nsPipeReadState& aReadState)
|
|
|
|
{
|
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2015-02-11 07:55:43 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
int32_t currentSegment = aReadState.mSegment;
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
// Move to the next segment to read
|
|
|
|
aReadState.mSegment += 1;
|
2015-02-21 17:51:17 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
SegmentChangeResult result = SegmentNotChanged;
|
2015-02-21 17:51:17 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
// If this was the last reference to the first segment, then remove it.
|
|
|
|
if (currentSegment == 0 && CountSegmentReferences(currentSegment) == 0) {
|
2015-02-21 17:51:17 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
// shift write and read segment index (-1 indicates an empty buffer).
|
|
|
|
mWriteSegment -= 1;
|
2015-02-21 17:51:17 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
// Directly modify the current read state. If the associated input
|
|
|
|
// stream is closed simultaneous with reading, then it may not be
|
|
|
|
// in the mInputList any more.
|
|
|
|
aReadState.mSegment -= 1;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
|
|
|
// Skip the current read state structure since we modify it manually
|
|
|
|
// before entering this loop.
|
|
|
|
if (&mInputList[i]->ReadState() == &aReadState) {
|
|
|
|
continue;
|
2015-02-25 21:26:41 +03:00
|
|
|
}
|
2015-02-21 17:51:17 +03:00
|
|
|
mInputList[i]->ReadState().mSegment -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// done with this segment
|
|
|
|
mBuffer.DeleteFirstSegment();
|
|
|
|
LOG(("III deleting first segment\n"));
|
|
|
|
|
|
|
|
result = SegmentDeleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWriteSegment < aReadState.mSegment) {
|
|
|
|
// read cursor has hit the end of written data, so reset it
|
|
|
|
MOZ_ASSERT(mWriteSegment == (aReadState.mSegment - 1));
|
|
|
|
aReadState.mReadCursor = nullptr;
|
|
|
|
aReadState.mReadLimit = nullptr;
|
|
|
|
// also, the buffer is completely empty, so reset the write cursor
|
|
|
|
if (mWriteSegment == -1) {
|
|
|
|
mWriteCursor = nullptr;
|
|
|
|
mWriteLimit = nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// advance read cursor and limit to next buffer segment
|
|
|
|
aReadState.mReadCursor = mBuffer.GetSegment(aReadState.mSegment);
|
|
|
|
if (mWriteSegment == aReadState.mSegment) {
|
|
|
|
aReadState.mReadLimit = mWriteCursor;
|
|
|
|
} else {
|
|
|
|
aReadState.mReadLimit = aReadState.mReadCursor + mBuffer.GetSegmentSize();
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2015-02-21 17:51:17 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPipe::DrainInputStream(nsPipeReadState& aReadState, nsPipeEvents& aEvents)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
|
|
|
// If a segment is actively being read in ReadSegments() for this input
|
|
|
|
// stream, then we cannot drain the stream. This can happen because
|
|
|
|
// ReadSegments() does not hold the lock while copying from the buffer.
|
|
|
|
// If we detect this condition, simply note that we need a drain once
|
|
|
|
// the read completes and return immediately.
|
|
|
|
if (aReadState.mActiveRead) {
|
|
|
|
MOZ_ASSERT(!aReadState.mNeedDrain);
|
|
|
|
aReadState.mNeedDrain = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aReadState.mAvailable = 0;
|
|
|
|
|
|
|
|
SegmentChangeResult result = SegmentNotChanged;
|
|
|
|
while(mWriteSegment >= aReadState.mSegment) {
|
|
|
|
|
|
|
|
// If the last segment to free is still being written to, we're done
|
|
|
|
// draining. We can't free any more.
|
|
|
|
if (ReadSegmentBeingWritten(aReadState)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AdvanceReadSegment(aReadState) == SegmentDeleted) {
|
|
|
|
result = SegmentDeleted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we've free'd up a segment, notify output stream that pipe has
|
|
|
|
// room for a new segment.
|
|
|
|
if (result == SegmentDeleted &&
|
|
|
|
mOutput.OnOutputWritable(aEvents) == NotifyMonitor) {
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsPipe::ReadSegmentBeingWritten(nsPipeReadState& aReadState)
|
|
|
|
{
|
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
|
|
|
bool beingWritten = mWriteSegment == aReadState.mSegment &&
|
|
|
|
mWriteLimit > mWriteCursor;
|
|
|
|
NS_ASSERTION(!beingWritten || aReadState.mReadLimit == mWriteCursor,
|
|
|
|
"unexpected state");
|
|
|
|
return beingWritten;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipe::GetWriteSegment(char*& aSegment, uint32_t& aSegmentLen)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
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
|
|
|
|
|
|
|
// write cursor and limit may both be null indicating an empty buffer.
|
|
|
|
if (mWriteCursor == mWriteLimit) {
|
2014-05-15 00:15:46 +04:00
|
|
|
char* seg = mBuffer.AppendNewSegment();
|
2014-05-05 21:30:43 +04:00
|
|
|
// pipe is full
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!seg) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
LOG(("OOO appended new segment\n"));
|
|
|
|
mWriteCursor = seg;
|
|
|
|
mWriteLimit = mWriteCursor + mBuffer.GetSegmentSize();
|
|
|
|
++mWriteSegment;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure read cursor is initialized
|
2015-02-11 07:55:43 +03:00
|
|
|
SetAllNullReadCursors();
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// check to see if we can roll-back our read and write cursors to the
|
|
|
|
// beginning of the current/first segment. this is purely an optimization.
|
2015-02-11 07:55:43 +03:00
|
|
|
if (mWriteSegment == 0 && AllReadCursorsMatchWriteCursor()) {
|
2014-05-15 00:15:46 +04:00
|
|
|
char* head = mBuffer.GetSegment(0);
|
2014-05-05 21:30:43 +04:00
|
|
|
LOG(("OOO rolling back write cursor %u bytes\n", mWriteCursor - head));
|
2015-02-11 07:55:43 +03:00
|
|
|
RollBackAllReadCursors(head);
|
|
|
|
mWriteCursor = head;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
aSegment = mWriteCursor;
|
|
|
|
aSegmentLen = mWriteLimit - mWriteCursor;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
void
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipe::AdvanceWriteCursor(uint32_t aBytesWritten)
|
2014-05-05 21:30:43 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(aBytesWritten, "don't call if no bytes written");
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsPipeEvents events;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2003-01-23 05:39:01 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
LOG(("OOO advancing write cursor by %u\n", aBytesWritten));
|
2003-01-23 05:39:01 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
char* newWriteCursor = mWriteCursor + aBytesWritten;
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(newWriteCursor <= mWriteLimit, "write cursor exceeds limit");
|
2003-04-05 06:02:09 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// update read limit if reading in the same segment
|
2015-02-11 07:55:43 +03:00
|
|
|
UpdateAllReadCursors(newWriteCursor);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
mWriteCursor = newWriteCursor;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
ValidateAllReadCursors();
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// update the writable flag on the output stream
|
|
|
|
if (mWriteCursor == mWriteLimit) {
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mBuffer.GetSize() >= mBuffer.GetMaxSize()) {
|
2014-05-05 21:30:43 +04:00
|
|
|
mOutput.SetWritable(false);
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// notify input stream that pipe now contains additional data
|
2015-02-11 07:55:43 +03:00
|
|
|
bool needNotify = false;
|
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
2016-02-17 13:22:28 +03:00
|
|
|
if (mInputList[i]->OnInputReadable(aBytesWritten, events, mon)
|
|
|
|
== NotifyMonitor) {
|
2015-02-11 07:55:43 +03:00
|
|
|
needNotify = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needNotify) {
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPipe::OnInputStreamException(nsPipeInputStream* aStream, nsresult aReason)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_FAILED(aReason));
|
|
|
|
|
|
|
|
nsPipeEvents events;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
|
|
|
// Its possible to re-enter this method when we call OnPipeException() or
|
|
|
|
// OnInputExection() below. If there is a caller stuck in our synchronous
|
|
|
|
// Wait() method, then they will get woken up with a failure code which
|
|
|
|
// re-enters this method. Therefore, gracefully handle unknown streams
|
|
|
|
// here.
|
|
|
|
|
|
|
|
// If we only have one stream open and it is the given stream, then shut
|
|
|
|
// down the entire pipe.
|
|
|
|
if (mInputList.Length() == 1) {
|
|
|
|
if (mInputList[0] == aStream) {
|
|
|
|
OnPipeException(aReason);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise just close the particular stream that hit an exception.
|
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
|
|
|
if (mInputList[i] != aStream) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-17 13:22:28 +03:00
|
|
|
MonitorAction action = mInputList[i]->OnInputException(aReason, events,
|
|
|
|
mon);
|
2015-02-11 07:55:43 +03:00
|
|
|
mInputList.RemoveElementAt(i);
|
|
|
|
|
|
|
|
// Notify after element is removed in case we re-enter as a result.
|
2015-02-21 17:51:17 +03:00
|
|
|
if (action == NotifyMonitor) {
|
|
|
|
mon.NotifyAll();
|
2015-02-11 07:55:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipe::OnPipeException(nsresult aReason, bool aOutputOnly)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
LOG(("PPP nsPipe::OnPipeException [reason=%x output-only=%d]\n",
|
2014-05-15 00:15:46 +04:00
|
|
|
aReason, aOutputOnly));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsPipeEvents events;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// if we've already hit an exception, then ignore this one.
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
mStatus = aReason;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
bool needNotify = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
nsTArray<nsPipeInputStream*> tmpInputList;
|
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
|
|
|
// an output-only exception applies to the input end if the pipe has
|
|
|
|
// zero bytes available.
|
|
|
|
if (aOutputOnly && mInputList[i]->Available()) {
|
|
|
|
tmpInputList.AppendElement(mInputList[i]);
|
|
|
|
continue;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-02-17 13:22:28 +03:00
|
|
|
if (mInputList[i]->OnInputException(aReason, events, mon)
|
|
|
|
== NotifyMonitor) {
|
2015-02-11 07:55:43 +03:00
|
|
|
needNotify = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mInputList = tmpInputList;
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
if (mOutput.OnOutputException(aReason, events) == NotifyMonitor) {
|
2015-02-11 07:55:43 +03:00
|
|
|
needNotify = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify after we have removed any input streams from mInputList
|
|
|
|
if (needNotify) {
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPipe::CloneInputStream(nsPipeInputStream* aOriginal,
|
|
|
|
nsIInputStream** aCloneOut)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPipeInputStream> ref = new nsPipeInputStream(*aOriginal);
|
2015-02-11 07:55:43 +03:00
|
|
|
mInputList.AppendElement(ref);
|
2016-05-15 20:32:09 +03:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> downcast = ref.forget();
|
|
|
|
downcast.forget(aCloneOut);
|
2015-02-11 07:55:43 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
nsPipe::CountSegmentReferences(int32_t aSegment)
|
|
|
|
{
|
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
|
|
|
uint32_t count = 0;
|
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
|
|
|
if (aSegment >= mInputList[i]->ReadState().mSegment) {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPipe::SetAllNullReadCursors()
|
|
|
|
{
|
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
|
|
|
nsPipeReadState& readState = mInputList[i]->ReadState();
|
|
|
|
if (!readState.mReadCursor) {
|
|
|
|
NS_ASSERTION(mWriteSegment == readState.mSegment,
|
|
|
|
"unexpected null read cursor");
|
|
|
|
readState.mReadCursor = readState.mReadLimit = mWriteCursor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsPipe::AllReadCursorsMatchWriteCursor()
|
|
|
|
{
|
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
|
|
|
const nsPipeReadState& readState = mInputList[i]->ReadState();
|
|
|
|
if (readState.mSegment != mWriteSegment ||
|
|
|
|
readState.mReadCursor != mWriteCursor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPipe::RollBackAllReadCursors(char* aWriteCursor)
|
|
|
|
{
|
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
|
|
|
nsPipeReadState& readState = mInputList[i]->ReadState();
|
|
|
|
MOZ_ASSERT(mWriteSegment == readState.mSegment);
|
|
|
|
MOZ_ASSERT(mWriteCursor == readState.mReadCursor);
|
|
|
|
MOZ_ASSERT(mWriteCursor == readState.mReadLimit);
|
|
|
|
readState.mReadCursor = aWriteCursor;
|
|
|
|
readState.mReadLimit = aWriteCursor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPipe::UpdateAllReadCursors(char* aWriteCursor)
|
|
|
|
{
|
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
|
|
|
nsPipeReadState& readState = mInputList[i]->ReadState();
|
|
|
|
if (mWriteSegment == readState.mSegment &&
|
|
|
|
readState.mReadLimit == mWriteCursor) {
|
|
|
|
readState.mReadLimit = aWriteCursor;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
void
|
|
|
|
nsPipe::ValidateAllReadCursors()
|
|
|
|
{
|
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
|
|
|
// The only way mReadCursor == mWriteCursor is if:
|
|
|
|
//
|
|
|
|
// - mReadCursor is at the start of a segment (which, based on how
|
|
|
|
// nsSegmentedBuffer works, means that this segment is the "first"
|
|
|
|
// segment)
|
|
|
|
// - mWriteCursor points at the location past the end of the current
|
|
|
|
// write segment (so the current write filled the current write
|
|
|
|
// segment, so we've incremented mWriteCursor to point past the end
|
|
|
|
// of it)
|
|
|
|
// - the segment to which data has just been written is located
|
|
|
|
// exactly one segment's worth of bytes before the first segment
|
|
|
|
// where mReadCursor is located
|
|
|
|
//
|
|
|
|
// Consequently, the byte immediately after the end of the current
|
|
|
|
// write segment is the first byte of the first segment, so
|
|
|
|
// mReadCursor == mWriteCursor. (Another way to think about this is
|
|
|
|
// to consider the buffer architecture diagram above, but consider it
|
|
|
|
// with an arena allocator which allocates from the *end* of the
|
|
|
|
// arena to the *beginning* of the arena.)
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
|
|
|
const nsPipeReadState& state = mInputList[i]->ReadState();
|
|
|
|
NS_ASSERTION(state.mReadCursor != mWriteCursor ||
|
|
|
|
(mBuffer.GetSegment(state.mSegment) == state.mReadCursor &&
|
|
|
|
mWriteCursor == mWriteLimit),
|
|
|
|
"read cursor is bad");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPipeEvents methods:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsPipeEvents::~nsPipeEvents()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
// dispatch any pending events
|
|
|
|
|
2015-09-14 18:49:09 +03:00
|
|
|
for (uint32_t i = 0; i < mInputList.Length(); ++i) {
|
|
|
|
mInputList[i].mCallback->OnInputStreamReady(mInputList[i].mStream);
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2015-09-14 18:49:09 +03:00
|
|
|
mInputList.Clear();
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (mOutputCallback) {
|
|
|
|
mOutputCallback->OnOutputStreamReady(mOutputStream);
|
2015-09-14 18:49:09 +03:00
|
|
|
mOutputCallback = nullptr;
|
|
|
|
mOutputStream = nullptr;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPipeInputStream methods:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
NS_IMPL_ADDREF(nsPipeInputStream);
|
|
|
|
NS_IMPL_RELEASE(nsPipeInputStream);
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
NS_INTERFACE_TABLE_HEAD(nsPipeInputStream)
|
|
|
|
NS_INTERFACE_TABLE_BEGIN
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsPipeInputStream, nsIAsyncInputStream)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsPipeInputStream, nsISeekableStream)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsPipeInputStream, nsISearchableInputStream)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsPipeInputStream, nsICloneableInputStream)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsPipeInputStream, nsIBufferedInputStream)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY(nsPipeInputStream, nsIClassInfo)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(nsPipeInputStream, nsIInputStream,
|
|
|
|
nsIAsyncInputStream)
|
|
|
|
NS_INTERFACE_TABLE_ENTRY_AMBIGUOUS(nsPipeInputStream, nsISupports,
|
|
|
|
nsIAsyncInputStream)
|
|
|
|
NS_INTERFACE_TABLE_END
|
|
|
|
NS_INTERFACE_TABLE_TAIL
|
2007-03-25 11:31:45 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsPipeInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsIAsyncInputStream,
|
|
|
|
nsISeekableStream,
|
2015-02-11 07:55:43 +03:00
|
|
|
nsISearchableInputStream,
|
2016-05-15 20:32:09 +03:00
|
|
|
nsICloneableInputStream,
|
|
|
|
nsIBufferedInputStream)
|
2007-03-25 11:31:45 +04:00
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_CI(nsPipeInputStream)
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeInputStream::Init(nsIInputStream*, uint32_t)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("nsPipeInputStream should never be initialized with "
|
|
|
|
"nsIBufferedInputStream::Init!\n");
|
|
|
|
}
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
uint32_t
|
|
|
|
nsPipeInputStream::Available()
|
|
|
|
{
|
|
|
|
mPipe->mReentrantMonitor.AssertCurrentThreadIn();
|
|
|
|
return mReadState.mAvailable;
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
nsresult
|
|
|
|
nsPipeInputStream::Wait()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(mBlocking, "wait on non-blocking pipe input stream");
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-02-17 13:22:28 +03:00
|
|
|
while (NS_SUCCEEDED(Status(mon)) && (mReadState.mAvailable == 0)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
LOG(("III pipe input: waiting for data\n"));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
mBlocked = true;
|
|
|
|
mon.Wait();
|
|
|
|
mBlocked = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
LOG(("III pipe input: woke up [status=%x available=%u]\n",
|
2016-02-17 13:22:28 +03:00
|
|
|
Status(mon), mReadState.mAvailable));
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-02-17 13:22:28 +03:00
|
|
|
return Status(mon) == NS_BASE_STREAM_CLOSED ? NS_OK : Status(mon);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
MonitorAction
|
2016-02-17 13:22:28 +03:00
|
|
|
nsPipeInputStream::OnInputReadable(uint32_t aBytesWritten,
|
|
|
|
nsPipeEvents& aEvents,
|
|
|
|
const ReentrantMonitorAutoEnter& ev)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2015-02-21 17:51:17 +03:00
|
|
|
MonitorAction result = DoNotNotifyMonitor;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
mPipe->mReentrantMonitor.AssertCurrentThreadIn();
|
|
|
|
mReadState.mAvailable += aBytesWritten;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (mCallback && !(mCallbackFlags & WAIT_CLOSURE_ONLY)) {
|
2014-05-15 00:15:46 +04:00
|
|
|
aEvents.NotifyInputReady(this, mCallback);
|
2014-05-05 21:30:43 +04:00
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (mBlocked) {
|
2015-02-21 17:51:17 +03:00
|
|
|
result = NotifyMonitor;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return result;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
MonitorAction
|
2016-02-17 13:22:28 +03:00
|
|
|
nsPipeInputStream::OnInputException(nsresult aReason, nsPipeEvents& aEvents,
|
|
|
|
const ReentrantMonitorAutoEnter& ev)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
LOG(("nsPipeInputStream::OnInputException [this=%x reason=%x]\n",
|
2014-05-15 00:15:46 +04:00
|
|
|
this, aReason));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
MonitorAction result = DoNotNotifyMonitor;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(NS_FAILED(aReason), "huh? successful exception");
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
if (NS_SUCCEEDED(mInputStatus)) {
|
|
|
|
mInputStatus = aReason;
|
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// force count of available bytes to zero.
|
2015-02-21 17:51:17 +03:00
|
|
|
mPipe->DrainInputStream(mReadState, aEvents);
|
2003-10-06 05:46:31 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (mCallback) {
|
2014-05-15 00:15:46 +04:00
|
|
|
aEvents.NotifyInputReady(this, mCallback);
|
2014-05-05 21:30:43 +04:00
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (mBlocked) {
|
2015-02-21 17:51:17 +03:00
|
|
|
result = NotifyMonitor;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return result;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeInputStream::CloseWithStatus(nsresult aReason)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
LOG(("III CloseWithStatus [this=%x reason=%x]\n", this, aReason));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-02-17 13:22:28 +03:00
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
if (NS_FAILED(mInputStatus)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_SUCCEEDED(aReason)) {
|
|
|
|
aReason = NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
mPipe->OnInputStreamException(this, aReason);
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeInputStream::Close()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
return CloseWithStatus(NS_BASE_STREAM_CLOSED);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeInputStream::Available(uint64_t* aResult)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
// nsPipeInputStream supports under 4GB stream only
|
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
// return error if closed
|
2016-02-17 13:22:28 +03:00
|
|
|
if (!mReadState.mAvailable && NS_FAILED(Status(mon))) {
|
|
|
|
return Status(mon);
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
*aResult = (uint64_t)mReadState.mAvailable;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeInputStream::ReadSegments(nsWriteSegmentFun aWriter,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aReadCount)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
LOG(("III ReadSegments [this=%x count=%u]\n", this, aCount));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
*aReadCount = 0;
|
|
|
|
while (aCount) {
|
2015-02-21 17:51:17 +03:00
|
|
|
AutoReadSegment segment(mPipe, mReadState, aCount);
|
|
|
|
rv = segment.Status();
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// ignore this error if we've already read something.
|
2014-05-15 00:15:46 +04:00
|
|
|
if (*aReadCount > 0) {
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
|
|
|
|
// pipe is empty
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!mBlocking) {
|
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
|
|
|
// wait for some data to be written to the pipe
|
|
|
|
rv = Wait();
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
continue;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
// ignore this error, just return.
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
2015-02-11 07:55:43 +03:00
|
|
|
mPipe->OnInputStreamException(this, rv);
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
uint32_t writeCount;
|
|
|
|
while (segment.Length()) {
|
2014-05-05 21:30:43 +04:00
|
|
|
writeCount = 0;
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
rv = aWriter(static_cast<nsIAsyncInputStream*>(this), aClosure,
|
|
|
|
segment.Data(), *aReadCount, segment.Length(), &writeCount);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) || writeCount == 0) {
|
2014-05-15 00:15:46 +04:00
|
|
|
aCount = 0;
|
2014-05-05 21:30:43 +04:00
|
|
|
// any errors returned from the writer end here: do not
|
|
|
|
// propagate to the caller of ReadSegments.
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
NS_ASSERTION(writeCount <= segment.Length(), "wrote more than expected");
|
|
|
|
segment.Advance(writeCount);
|
2014-05-15 00:15:46 +04:00
|
|
|
aCount -= writeCount;
|
|
|
|
*aReadCount += writeCount;
|
2014-05-05 21:30:43 +04:00
|
|
|
mLogicalOffset += writeCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeInputStream::Read(char* aToBuf, uint32_t aBufLen, uint32_t* aReadCount)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, aToBuf, aBufLen, aReadCount);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeInputStream::IsNonBlocking(bool* aNonBlocking)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
*aNonBlocking = !mBlocking;
|
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeInputStream::AsyncWait(nsIInputStreamCallback* aCallback,
|
|
|
|
uint32_t aFlags,
|
|
|
|
uint32_t aRequestedCount,
|
|
|
|
nsIEventTarget* aTarget)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
LOG(("III AsyncWait [this=%x]\n", this));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsPipeEvents pipeEvents;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// replace a pending callback
|
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!aCallback) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2006-03-03 19:27:35 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> proxy;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aTarget) {
|
|
|
|
proxy = NS_NewInputStreamReadyEvent(aCallback, aTarget);
|
|
|
|
aCallback = proxy;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-02-17 13:22:28 +03:00
|
|
|
if (NS_FAILED(Status(mon)) ||
|
2015-02-21 17:51:17 +03:00
|
|
|
(mReadState.mAvailable && !(aFlags & WAIT_CLOSURE_ONLY))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
// stream is already closed or readable; post event.
|
2014-05-15 00:15:46 +04:00
|
|
|
pipeEvents.NotifyInputReady(this, aCallback);
|
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
// queue up callback object to be notified when data becomes available
|
2014-05-15 00:15:46 +04:00
|
|
|
mCallback = aCallback;
|
|
|
|
mCallbackFlags = aFlags;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeInputStream::Seek(int32_t aWhence, int64_t aOffset)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_NOTREACHED("nsPipeInputStream::Seek");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeInputStream::Tell(int64_t* aOffset)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
// return error if closed
|
2016-02-17 13:22:28 +03:00
|
|
|
if (!mReadState.mAvailable && NS_FAILED(Status(mon))) {
|
|
|
|
return Status(mon);
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
*aOffset = mLogicalOffset;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeInputStream::SetEOF()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_NOTREACHED("nsPipeInputStream::SetEOF");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2015-06-07 12:26:40 +03:00
|
|
|
static bool strings_equal(bool aIgnoreCase,
|
|
|
|
const char* aS1, const char* aS2, uint32_t aLen)
|
|
|
|
{
|
|
|
|
return aIgnoreCase
|
|
|
|
? !nsCRT::strncasecmp(aS1, aS2, aLen) : !nsCRT::strncmp(aS1, aS2, aLen);
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeInputStream::Search(const char* aForString,
|
|
|
|
bool aIgnoreCase,
|
|
|
|
bool* aFound,
|
|
|
|
uint32_t* aOffsetSearchedTo)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
LOG(("III Search [for=%s ic=%u]\n", aForString, aIgnoreCase));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
char* cursor1;
|
|
|
|
char* limit1;
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t index = 0, offset = 0;
|
2014-05-15 00:15:46 +04:00
|
|
|
uint32_t strLen = strlen(aForString);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
mPipe->PeekSegment(mReadState, 0, cursor1, limit1);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (cursor1 == limit1) {
|
2014-05-15 00:15:46 +04:00
|
|
|
*aFound = false;
|
|
|
|
*aOffsetSearchedTo = 0;
|
|
|
|
LOG((" result [aFound=%u offset=%u]\n", *aFound, *aOffsetSearchedTo));
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
uint32_t i, len1 = limit1 - cursor1;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// check if the string is in the buffer segment
|
|
|
|
for (i = 0; i < len1 - strLen + 1; i++) {
|
2015-06-07 12:26:40 +03:00
|
|
|
if (strings_equal(aIgnoreCase, &cursor1[i], aForString, strLen)) {
|
2014-05-15 00:15:46 +04:00
|
|
|
*aFound = true;
|
|
|
|
*aOffsetSearchedTo = offset + i;
|
|
|
|
LOG((" result [aFound=%u offset=%u]\n", *aFound, *aOffsetSearchedTo));
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// get the next segment
|
2014-05-15 00:15:46 +04:00
|
|
|
char* cursor2;
|
|
|
|
char* limit2;
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t len2;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
index++;
|
|
|
|
offset += len1;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
mPipe->PeekSegment(mReadState, index, cursor2, limit2);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (cursor2 == limit2) {
|
2014-05-15 00:15:46 +04:00
|
|
|
*aFound = false;
|
|
|
|
*aOffsetSearchedTo = offset - strLen + 1;
|
|
|
|
LOG((" result [aFound=%u offset=%u]\n", *aFound, *aOffsetSearchedTo));
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
len2 = limit2 - cursor2;
|
|
|
|
|
|
|
|
// check if the string is straddling the next buffer segment
|
|
|
|
uint32_t lim = XPCOM_MIN(strLen, len2 + 1);
|
|
|
|
for (i = 0; i < lim; ++i) {
|
|
|
|
uint32_t strPart1Len = strLen - i - 1;
|
|
|
|
uint32_t strPart2Len = strLen - strPart1Len;
|
2014-05-15 00:15:46 +04:00
|
|
|
const char* strPart2 = &aForString[strLen - strPart2Len];
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t bufSeg1Offset = len1 - strPart1Len;
|
2015-06-07 12:26:40 +03:00
|
|
|
if (strings_equal(aIgnoreCase, &cursor1[bufSeg1Offset], aForString, strPart1Len) &&
|
|
|
|
strings_equal(aIgnoreCase, cursor2, strPart2, strPart2Len)) {
|
2014-05-15 00:15:46 +04:00
|
|
|
*aFound = true;
|
|
|
|
*aOffsetSearchedTo = offset - strPart1Len;
|
|
|
|
LOG((" result [aFound=%u offset=%u]\n", *aFound, *aOffsetSearchedTo));
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// finally continue with the next buffer
|
|
|
|
cursor1 = cursor2;
|
|
|
|
limit1 = limit2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("can't get here");
|
|
|
|
return NS_ERROR_UNEXPECTED; // keep compiler happy
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeInputStream::GetCloneable(bool* aCloneableOut)
|
|
|
|
{
|
|
|
|
*aCloneableOut = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeInputStream::Clone(nsIInputStream** aCloneOut)
|
|
|
|
{
|
|
|
|
return mPipe->CloneInputStream(this, aCloneOut);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-02-17 13:22:28 +03:00
|
|
|
nsPipeInputStream::Status(const ReentrantMonitorAutoEnter& ev) const
|
2015-02-11 07:55:43 +03:00
|
|
|
{
|
2016-09-28 10:32:00 +03:00
|
|
|
if (NS_FAILED(mInputStatus)) {
|
|
|
|
return mInputStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mReadState.mAvailable) {
|
|
|
|
// Still something to read and this input stream state is OK.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to read, just fall through to the pipe's state that
|
|
|
|
// may reflect state of its output stream side (already closed).
|
|
|
|
return mPipe->mStatus;
|
2015-02-11 07:55:43 +03:00
|
|
|
}
|
|
|
|
|
2016-02-17 13:22:28 +03:00
|
|
|
nsresult
|
|
|
|
nsPipeInputStream::Status() const
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
|
|
|
return Status(mon);
|
|
|
|
}
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
nsPipeInputStream::~nsPipeInputStream()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPipeOutputStream methods:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsPipeOutputStream,
|
|
|
|
nsIOutputStream,
|
|
|
|
nsIAsyncOutputStream,
|
|
|
|
nsIClassInfo)
|
2007-03-25 11:31:45 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsPipeOutputStream,
|
|
|
|
nsIOutputStream,
|
|
|
|
nsIAsyncOutputStream)
|
2007-03-25 11:31:45 +04:00
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_CI(nsPipeOutputStream)
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
nsresult
|
|
|
|
nsPipeOutputStream::Wait()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(mBlocking, "wait on non-blocking pipe output stream");
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_SUCCEEDED(mPipe->mStatus) && !mWritable) {
|
|
|
|
LOG(("OOO pipe output: waiting for space\n"));
|
|
|
|
mBlocked = true;
|
|
|
|
mon.Wait();
|
|
|
|
mBlocked = false;
|
|
|
|
LOG(("OOO pipe output: woke up [pipe-status=%x writable=%u]\n",
|
|
|
|
mPipe->mStatus, mWritable));
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return mPipe->mStatus == NS_BASE_STREAM_CLOSED ? NS_OK : mPipe->mStatus;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
MonitorAction
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeOutputStream::OnOutputWritable(nsPipeEvents& aEvents)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2015-02-21 17:51:17 +03:00
|
|
|
MonitorAction result = DoNotNotifyMonitor;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
mWritable = true;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (mCallback && !(mCallbackFlags & WAIT_CLOSURE_ONLY)) {
|
2014-05-15 00:15:46 +04:00
|
|
|
aEvents.NotifyOutputReady(this, mCallback);
|
2014-05-05 21:30:43 +04:00
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (mBlocked) {
|
2015-02-21 17:51:17 +03:00
|
|
|
result = NotifyMonitor;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return result;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
MonitorAction
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeOutputStream::OnOutputException(nsresult aReason, nsPipeEvents& aEvents)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
LOG(("nsPipeOutputStream::OnOutputException [this=%x reason=%x]\n",
|
2014-05-15 00:15:46 +04:00
|
|
|
this, aReason));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2015-02-21 17:51:17 +03:00
|
|
|
MonitorAction result = DoNotNotifyMonitor;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(NS_FAILED(aReason), "huh? successful exception");
|
2014-05-05 21:30:43 +04:00
|
|
|
mWritable = false;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (mCallback) {
|
2014-05-15 00:15:46 +04:00
|
|
|
aEvents.NotifyOutputReady(this, mCallback);
|
2014-05-05 21:30:43 +04:00
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else if (mBlocked) {
|
2015-02-21 17:51:17 +03:00
|
|
|
result = NotifyMonitor;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return result;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2003-01-18 04:27:53 +03:00
|
|
|
nsPipeOutputStream::AddRef()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
++mWriterRefCnt;
|
|
|
|
return mPipe->AddRef();
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2003-01-18 04:27:53 +03:00
|
|
|
nsPipeOutputStream::Release()
|
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (--mWriterRefCnt == 0) {
|
2014-05-05 21:30:43 +04:00
|
|
|
Close();
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
return mPipe->Release();
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeOutputStream::CloseWithStatus(nsresult aReason)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
LOG(("OOO CloseWithStatus [this=%x reason=%x]\n", this, aReason));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_SUCCEEDED(aReason)) {
|
|
|
|
aReason = NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// input stream may remain open
|
2014-05-15 00:15:46 +04:00
|
|
|
mPipe->OnPipeException(aReason, true);
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeOutputStream::Close()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
return CloseWithStatus(NS_BASE_STREAM_CLOSED);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeOutputStream::WriteSegments(nsReadSegmentFun aReader,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aWriteCount)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
LOG(("OOO WriteSegments [this=%x count=%u]\n", this, aCount));
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
char* segment;
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t segmentLen;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
*aWriteCount = 0;
|
|
|
|
while (aCount) {
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = mPipe->GetWriteSegment(segment, segmentLen);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
|
|
|
|
// pipe is full
|
|
|
|
if (!mBlocking) {
|
|
|
|
// ignore this error if we've already written something
|
2014-05-15 00:15:46 +04:00
|
|
|
if (*aWriteCount > 0) {
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
// wait for the pipe to have an empty segment.
|
|
|
|
rv = Wait();
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
continue;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
mPipe->OnPipeException(rv);
|
|
|
|
break;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
// write no more than aCount
|
|
|
|
if (segmentLen > aCount) {
|
|
|
|
segmentLen = aCount;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
uint32_t readCount, originalLen = segmentLen;
|
|
|
|
while (segmentLen) {
|
|
|
|
readCount = 0;
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
rv = aReader(this, aClosure, segment, *aWriteCount, segmentLen, &readCount);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) || readCount == 0) {
|
2014-05-15 00:15:46 +04:00
|
|
|
aCount = 0;
|
|
|
|
// any errors returned from the aReader end here: do not
|
2014-05-05 21:30:43 +04:00
|
|
|
// propagate to the caller of WriteSegments.
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(readCount <= segmentLen, "read more than expected");
|
|
|
|
segment += readCount;
|
|
|
|
segmentLen -= readCount;
|
2014-05-15 00:15:46 +04:00
|
|
|
aCount -= readCount;
|
|
|
|
*aWriteCount += readCount;
|
2014-05-05 21:30:43 +04:00
|
|
|
mLogicalOffset += readCount;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (segmentLen < originalLen) {
|
2014-05-05 21:30:43 +04:00
|
|
|
mPipe->AdvanceWriteCursor(originalLen - segmentLen);
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
static nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
nsReadFromRawBuffer(nsIOutputStream* aOutStr,
|
|
|
|
void* aClosure,
|
|
|
|
char* aToRawSegment,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aReadCount)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
const char* fromBuf = (const char*)aClosure;
|
|
|
|
memcpy(aToRawSegment, &fromBuf[aOffset], aCount);
|
|
|
|
*aReadCount = aCount;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeOutputStream::Write(const char* aFromBuf,
|
|
|
|
uint32_t aBufLen,
|
|
|
|
uint32_t* aWriteCount)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
return WriteSegments(nsReadFromRawBuffer, (void*)aFromBuf, aBufLen, aWriteCount);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeOutputStream::Flush(void)
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
// nothing to do
|
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
static nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
nsReadFromInputStream(nsIOutputStream* aOutStr,
|
|
|
|
void* aClosure,
|
|
|
|
char* aToRawSegment,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aReadCount)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
nsIInputStream* fromStream = (nsIInputStream*)aClosure;
|
|
|
|
return fromStream->Read(aToRawSegment, aCount, aReadCount);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeOutputStream::WriteFrom(nsIInputStream* aFromStream,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aWriteCount)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
return WriteSegments(nsReadFromInputStream, aFromStream, aCount, aWriteCount);
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeOutputStream::IsNonBlocking(bool* aNonBlocking)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
*aNonBlocking = !mBlocking;
|
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeOutputStream::AsyncWait(nsIOutputStreamCallback* aCallback,
|
|
|
|
uint32_t aFlags,
|
|
|
|
uint32_t aRequestedCount,
|
|
|
|
nsIEventTarget* aTarget)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
LOG(("OOO AsyncWait [this=%x]\n", this));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsPipeEvents pipeEvents;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// replace a pending callback
|
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!aCallback) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2006-03-03 19:27:35 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsCOMPtr<nsIOutputStreamCallback> proxy;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aTarget) {
|
|
|
|
proxy = NS_NewOutputStreamReadyEvent(aCallback, aTarget);
|
|
|
|
aCallback = proxy;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_FAILED(mPipe->mStatus) ||
|
2014-05-15 00:15:46 +04:00
|
|
|
(mWritable && !(aFlags & WAIT_CLOSURE_ONLY))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
// stream is already closed or writable; post event.
|
2014-05-15 00:15:46 +04:00
|
|
|
pipeEvents.NotifyOutputReady(this, aCallback);
|
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
// queue up callback object to be notified when data becomes available
|
2014-05-15 00:15:46 +04:00
|
|
|
mCallback = aCallback;
|
|
|
|
mCallbackFlags = aFlags;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_NewPipe(nsIInputStream** aPipeIn,
|
|
|
|
nsIOutputStream** aPipeOut,
|
|
|
|
uint32_t aSegmentSize,
|
|
|
|
uint32_t aMaxSize,
|
|
|
|
bool aNonBlockingInput,
|
|
|
|
bool aNonBlockingOutput)
|
2006-03-22 23:42:55 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aSegmentSize == 0) {
|
|
|
|
aSegmentSize = DEFAULT_SEGMENT_SIZE;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
// Handle aMaxSize of UINT32_MAX as a special case
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t segmentCount;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aMaxSize == UINT32_MAX) {
|
2014-05-05 21:30:43 +04:00
|
|
|
segmentCount = UINT32_MAX;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
|
|
|
segmentCount = aMaxSize / aSegmentSize;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsIAsyncInputStream* in;
|
|
|
|
nsIAsyncOutputStream* out;
|
|
|
|
nsresult rv = NS_NewPipe2(&in, &out, aNonBlockingInput, aNonBlockingOutput,
|
|
|
|
aSegmentSize, segmentCount);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
*aPipeIn = in;
|
|
|
|
*aPipeOut = out;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2006-03-22 23:42:55 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_NewPipe2(nsIAsyncInputStream** aPipeIn,
|
|
|
|
nsIAsyncOutputStream** aPipeOut,
|
|
|
|
bool aNonBlockingInput,
|
|
|
|
bool aNonBlockingOutput,
|
|
|
|
uint32_t aSegmentSize,
|
|
|
|
uint32_t aSegmentCount)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipe* pipe = new nsPipe();
|
2016-08-17 09:52:48 +03:00
|
|
|
nsresult rv = pipe->Init(aNonBlockingInput,
|
|
|
|
aNonBlockingOutput,
|
|
|
|
aSegmentSize,
|
|
|
|
aSegmentCount);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ADDREF(pipe);
|
|
|
|
NS_RELEASE(pipe);
|
|
|
|
return rv;
|
|
|
|
}
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2016-08-17 09:52:48 +03:00
|
|
|
// These always succeed because the pipe is initialized above.
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(pipe->GetInputStream(aPipeIn));
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(pipe->GetOutputStream(aPipeOut));
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2003-01-18 04:27:53 +03:00
|
|
|
}
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
nsPipeConstructor(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
2005-07-19 03:39:56 +04: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
|
|
|
}
|
|
|
|
nsPipe* pipe = new nsPipe();
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ADDREF(pipe);
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = pipe->QueryInterface(aIID, aResult);
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_RELEASE(pipe);
|
|
|
|
return rv;
|
2005-07-19 03:39:56 +04:00
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|