2014-05-05 10:30:43 -07: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 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
|
|
|
|
2012-06-05 16:51:58 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2011-04-29 12:21:57 -07:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIPipe.h"
|
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsISeekableStream.h"
|
2007-03-25 00:31:45 -07:00
|
|
|
#include "nsIProgrammingLanguage.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsSegmentedBuffer.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "prlog.h"
|
2007-07-24 13:17:11 -07:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2011-06-02 05:56:50 -07:00
|
|
|
#include "nsAlgorithm.h"
|
2013-08-22 03:32:11 -07:00
|
|
|
#include "nsMemory.h"
|
2013-09-23 10:29:27 -07:00
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
2007-03-22 10:30:00 -07: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-03-31 21:29:02 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-11-18 18:34:00 -08:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=nsPipe:5
|
|
|
|
//
|
2014-05-14 13:15:46 -07:00
|
|
|
static PRLogModuleInfo*
|
2012-10-29 16:32:10 -07:00
|
|
|
GetPipeLog()
|
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
static PRLogModuleInfo* sLog;
|
|
|
|
if (!sLog) {
|
2012-10-29 16:32:10 -07:00
|
|
|
sLog = PR_NewLogModule("nsPipe");
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2012-10-29 16:32:10 -07:00
|
|
|
return sLog;
|
|
|
|
}
|
|
|
|
#define LOG(args) PR_LOG(GetPipeLog(), PR_LOG_DEBUG, args)
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
|
|
|
#define LOG(args)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEFAULT_SEGMENT_SIZE 4096
|
|
|
|
#define DEFAULT_SEGMENT_COUNT 16
|
|
|
|
|
|
|
|
class nsPipe;
|
|
|
|
class nsPipeEvents;
|
|
|
|
class nsPipeInputStream;
|
|
|
|
class nsPipeOutputStream;
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
class nsPipeEvents
|
|
|
|
{
|
|
|
|
public:
|
2014-05-05 10:30:43 -07:00
|
|
|
nsPipeEvents() { }
|
|
|
|
~nsPipeEvents();
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
inline void NotifyInputReady(nsIAsyncInputStream* aStream,
|
|
|
|
nsIInputStreamCallback* aCallback)
|
2014-05-05 10:30:43 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!mInputCallback, "already have an input event");
|
2014-05-14 13:15:46 -07:00
|
|
|
mInputStream = aStream;
|
|
|
|
mInputCallback = aCallback;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
inline void NotifyOutputReady(nsIAsyncOutputStream* aStream,
|
|
|
|
nsIOutputStreamCallback* aCallback)
|
2014-05-05 10:30:43 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!mOutputCallback, "already have an output event");
|
2014-05-14 13:15:46 -07:00
|
|
|
mOutputStream = aStream;
|
|
|
|
mOutputCallback = aCallback;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsIAsyncInputStream> mInputStream;
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> mInputCallback;
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mOutputStream;
|
|
|
|
nsCOMPtr<nsIOutputStreamCallback> mOutputCallback;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// the input end of a pipe (allocated as a member of the pipe).
|
2014-05-14 13:15:46 -07:00
|
|
|
class nsPipeInputStream
|
|
|
|
: public nsIAsyncInputStream
|
|
|
|
, public nsISeekableStream
|
|
|
|
, public nsISearchableInputStream
|
|
|
|
, public nsIClassInfo
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-05-05 10:30:43 -07: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_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
|
|
|
NS_DECL_NSISEARCHABLEINPUTSTREAM
|
|
|
|
NS_DECL_NSICLASSINFO
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream(nsPipe* aPipe)
|
|
|
|
: mPipe(aPipe)
|
2014-05-05 10:30:43 -07:00
|
|
|
, mReaderRefCnt(0)
|
|
|
|
, mLogicalOffset(0)
|
|
|
|
, mBlocking(true)
|
|
|
|
, mBlocked(false)
|
|
|
|
, mAvailable(0)
|
|
|
|
, mCallbackFlags(0)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsresult Fill();
|
2014-05-14 13:15:46 -07:00
|
|
|
void SetNonBlocking(bool aNonBlocking)
|
|
|
|
{
|
|
|
|
mBlocking = !aNonBlocking;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
uint32_t Available()
|
|
|
|
{
|
|
|
|
return mAvailable;
|
|
|
|
}
|
|
|
|
void ReduceAvailable(uint32_t aAvail)
|
|
|
|
{
|
|
|
|
mAvailable -= aAvail;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// synchronously wait for the pipe to become readable.
|
|
|
|
nsresult Wait();
|
|
|
|
|
|
|
|
// these functions return true to indicate that the pipe's monitor should
|
|
|
|
// be notified, to wake up a blocked reader if any.
|
2014-05-14 13:15:46 -07:00
|
|
|
bool OnInputReadable(uint32_t aBytesWritten, nsPipeEvents&);
|
|
|
|
bool OnInputException(nsresult, nsPipeEvents&);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe* mPipe;
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// separate refcnt so that we know when to close the consumer
|
|
|
|
mozilla::ThreadSafeAutoRefCnt mReaderRefCnt;
|
|
|
|
int64_t mLogicalOffset;
|
|
|
|
bool mBlocking;
|
|
|
|
|
|
|
|
// these variables can only be accessed while inside the pipe's monitor
|
|
|
|
bool mBlocked;
|
|
|
|
uint32_t mAvailable;
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> mCallback;
|
|
|
|
uint32_t mCallbackFlags;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// the output end of a pipe (allocated as a member of the pipe).
|
2014-05-14 13:15:46 -07:00
|
|
|
class nsPipeOutputStream
|
|
|
|
: public nsIAsyncOutputStream
|
|
|
|
, public nsIClassInfo
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-05-05 10:30:43 -07: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-05-14 13:15:46 -07:00
|
|
|
nsPipeOutputStream(nsPipe* aPipe)
|
|
|
|
: mPipe(aPipe)
|
2014-05-05 10:30:43 -07:00
|
|
|
, mWriterRefCnt(0)
|
|
|
|
, mLogicalOffset(0)
|
|
|
|
, mBlocking(true)
|
|
|
|
, mBlocked(false)
|
|
|
|
, mWritable(true)
|
|
|
|
, mCallbackFlags(0)
|
|
|
|
{ }
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
void SetNonBlocking(bool aNonBlocking)
|
|
|
|
{
|
|
|
|
mBlocking = !aNonBlocking;
|
|
|
|
}
|
|
|
|
void SetWritable(bool aWritable)
|
|
|
|
{
|
|
|
|
mWritable = aWritable;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// synchronously wait for the pipe to become writable.
|
|
|
|
nsresult Wait();
|
|
|
|
|
|
|
|
// these functions return true to indicate that the pipe's monitor should
|
|
|
|
// be notified, to wake up a blocked writer if any.
|
2014-05-14 13:15:46 -07:00
|
|
|
bool OnOutputWritable(nsPipeEvents&);
|
|
|
|
bool OnOutputException(nsresult, nsPipeEvents&);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe* mPipe;
|
2014-05-05 10:30:43 -07: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;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-06-05 16:51:58 -07:00
|
|
|
class nsPipe MOZ_FINAL : public nsIPipe
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-05-05 10:30:43 -07:00
|
|
|
friend class nsPipeInputStream;
|
|
|
|
friend class nsPipeOutputStream;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIPIPE
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// nsPipe methods:
|
|
|
|
nsPipe();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2014-05-05 10:30:43 -07:00
|
|
|
~nsPipe();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
public:
|
2014-05-05 10:30:43 -07:00
|
|
|
//
|
|
|
|
// methods below may only be called while inside the pipe's monitor
|
|
|
|
//
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
void PeekSegment(uint32_t aIndex, char*& aCursor, char*& aLimit);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
//
|
|
|
|
// methods below may be called while outside the pipe's monitor
|
|
|
|
//
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult GetReadSegment(const char*& aSegment, uint32_t& aSegmentLen);
|
|
|
|
void AdvanceReadCursor(uint32_t aCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult GetWriteSegment(char*& aSegment, uint32_t& aSegmentLen);
|
|
|
|
void AdvanceWriteCursor(uint32_t aCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
void OnPipeException(nsresult aReason, bool aOutputOnly = false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
protected:
|
2014-05-05 10:30:43 -07: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.
|
|
|
|
nsPipeInputStream mInput;
|
|
|
|
nsPipeOutputStream mOutput;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
ReentrantMonitor mReentrantMonitor;
|
|
|
|
nsSegmentedBuffer mBuffer;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
char* mReadCursor;
|
|
|
|
char* mReadLimit;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
int32_t mWriteSegment;
|
|
|
|
char* mWriteCursor;
|
|
|
|
char* mWriteLimit;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult mStatus;
|
|
|
|
bool mInited;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// NOTES on buffer architecture:
|
|
|
|
//
|
|
|
|
// +-----------------+ - - mBuffer.GetSegment(0)
|
|
|
|
// | |
|
|
|
|
// + - - - - - - - - + - - mReadCursor
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// +-----------------+ - - mReadLimit
|
|
|
|
// |
|
|
|
|
// +-----------------+
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// +-----------------+
|
|
|
|
// |
|
|
|
|
// +-----------------+ - - mBuffer.GetSegment(mWriteSegment)
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// |/////////////////|
|
|
|
|
// + - - - - - - - - + - - mWriteCursor
|
|
|
|
// | |
|
|
|
|
// | |
|
|
|
|
// +-----------------+ - - mWriteLimit
|
|
|
|
//
|
|
|
|
// (shaded region contains data)
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPipe methods:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsPipe::nsPipe()
|
2014-05-05 10:30:43 -07:00
|
|
|
: mInput(MOZ_THIS_IN_INITIALIZER_LIST())
|
|
|
|
, mOutput(MOZ_THIS_IN_INITIALIZER_LIST())
|
|
|
|
, mReentrantMonitor("nsPipe.mReentrantMonitor")
|
|
|
|
, mReadCursor(nullptr)
|
|
|
|
, mReadLimit(nullptr)
|
|
|
|
, mWriteSegment(-1)
|
|
|
|
, mWriteCursor(nullptr)
|
|
|
|
, mWriteLimit(nullptr)
|
|
|
|
, mStatus(NS_OK)
|
|
|
|
, mInited(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPipe::~nsPipe()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsPipe, nsIPipe)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe::Init(bool aNonBlockingIn,
|
|
|
|
bool aNonBlockingOut,
|
|
|
|
uint32_t aSegmentSize,
|
|
|
|
uint32_t aSegmentCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
mInited = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aSegmentSize == 0) {
|
|
|
|
aSegmentSize = DEFAULT_SEGMENT_SIZE;
|
|
|
|
}
|
|
|
|
if (aSegmentCount == 0) {
|
|
|
|
aSegmentCount = DEFAULT_SEGMENT_COUNT;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// protect against overflow
|
2014-05-14 13:15:46 -07:00
|
|
|
uint32_t maxCount = uint32_t(-1) / aSegmentSize;
|
|
|
|
if (aSegmentCount > maxCount) {
|
|
|
|
aSegmentCount = maxCount;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = mBuffer.Init(aSegmentSize, aSegmentSize * aSegmentCount);
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return rv;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
mInput.SetNonBlocking(aNonBlockingIn);
|
|
|
|
mOutput.SetNonBlocking(aNonBlockingOut);
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe::GetInputStream(nsIAsyncInputStream** aInputStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_ADDREF(*aInputStream = &mInput);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe::GetOutputStream(nsIAsyncOutputStream** aOutputStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_WARN_IF(!mInited)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_ADDREF(*aOutputStream = &mOutput);
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe::PeekSegment(uint32_t aIndex, char*& aCursor, char*& aLimit)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aIndex == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_ASSERTION(!mReadCursor || mBuffer.GetSegmentCount(), "unexpected state");
|
2014-05-14 13:15:46 -07:00
|
|
|
aCursor = mReadCursor;
|
|
|
|
aLimit = mReadLimit;
|
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
uint32_t numSegments = mBuffer.GetSegmentCount();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aIndex >= numSegments) {
|
|
|
|
aCursor = aLimit = nullptr;
|
|
|
|
} else {
|
|
|
|
aCursor = mBuffer.GetSegment(aIndex);
|
|
|
|
if (mWriteSegment == (int32_t)aIndex) {
|
|
|
|
aLimit = mWriteCursor;
|
|
|
|
} else {
|
|
|
|
aLimit = aCursor + mBuffer.GetSegmentSize();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe::GetReadSegment(const char*& aSegment, uint32_t& aSegmentLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mReadCursor == mReadLimit) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_FAILED(mStatus) ? mStatus : NS_BASE_STREAM_WOULD_BLOCK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
aSegment = mReadCursor;
|
|
|
|
aSegmentLen = mReadLimit - mReadCursor;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe::AdvanceReadCursor(uint32_t aBytesRead)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_ASSERTION(aBytesRead, "don't call if no bytes read");
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
nsPipeEvents events;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
LOG(("III advancing read cursor by %u\n", aBytesRead));
|
|
|
|
NS_ASSERTION(aBytesRead <= mBuffer.GetSegmentSize(), "read too much");
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
mReadCursor += aBytesRead;
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_ASSERTION(mReadCursor <= mReadLimit, "read cursor exceeds limit");
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
mInput.ReduceAvailable(aBytesRead);
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
if (mReadCursor == mReadLimit) {
|
|
|
|
// we've reached the limit of how much we can read from this segment.
|
|
|
|
// if at the end of this segment, then we must discard this segment.
|
|
|
|
|
|
|
|
// if still writing in this segment then bail because we're not done
|
|
|
|
// with the segment and have to wait for now...
|
|
|
|
if (mWriteSegment == 0 && mWriteLimit > mWriteCursor) {
|
|
|
|
NS_ASSERTION(mReadLimit == mWriteCursor, "unexpected state");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// shift write segment index (-1 indicates an empty buffer).
|
|
|
|
--mWriteSegment;
|
|
|
|
|
|
|
|
// done with this segment
|
|
|
|
mBuffer.DeleteFirstSegment();
|
|
|
|
LOG(("III deleting first segment\n"));
|
|
|
|
|
|
|
|
if (mWriteSegment == -1) {
|
|
|
|
// buffer is completely empty
|
|
|
|
mReadCursor = nullptr;
|
|
|
|
mReadLimit = nullptr;
|
|
|
|
mWriteCursor = nullptr;
|
|
|
|
mWriteLimit = nullptr;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
// advance read cursor and limit to next buffer segment
|
|
|
|
mReadCursor = mBuffer.GetSegment(0);
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mWriteSegment == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mReadLimit = mWriteCursor;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
mReadLimit = mReadCursor + mBuffer.GetSegmentSize();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// we've free'd up a segment, so notify output stream that pipe has
|
|
|
|
// room for a new segment.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mOutput.OnOutputWritable(events)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mon.Notify();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe::GetWriteSegment(char*& aSegment, uint32_t& aSegmentLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return mStatus;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// write cursor and limit may both be null indicating an empty buffer.
|
|
|
|
if (mWriteCursor == mWriteLimit) {
|
2014-05-14 13:15:46 -07:00
|
|
|
char* seg = mBuffer.AppendNewSegment();
|
2014-05-05 10:30:43 -07:00
|
|
|
// pipe is full
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!seg) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
LOG(("OOO appended new segment\n"));
|
|
|
|
mWriteCursor = seg;
|
|
|
|
mWriteLimit = mWriteCursor + mBuffer.GetSegmentSize();
|
|
|
|
++mWriteSegment;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure read cursor is initialized
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!mReadCursor) {
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_ASSERTION(mWriteSegment == 0, "unexpected null read cursor");
|
|
|
|
mReadCursor = mReadLimit = mWriteCursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (mReadCursor == mWriteCursor && mWriteSegment == 0) {
|
2014-05-14 13:15:46 -07:00
|
|
|
char* head = mBuffer.GetSegment(0);
|
2014-05-05 10:30:43 -07:00
|
|
|
LOG(("OOO rolling back write cursor %u bytes\n", mWriteCursor - head));
|
|
|
|
mWriteCursor = mReadCursor = mReadLimit = head;
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
aSegment = mWriteCursor;
|
|
|
|
aSegmentLen = mWriteLimit - mWriteCursor;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
void
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe::AdvanceWriteCursor(uint32_t aBytesWritten)
|
2014-05-05 10:30:43 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_ASSERTION(aBytesWritten, "don't call if no bytes written");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsPipeEvents events;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
LOG(("OOO advancing write cursor by %u\n", aBytesWritten));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
char* newWriteCursor = mWriteCursor + aBytesWritten;
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_ASSERTION(newWriteCursor <= mWriteLimit, "write cursor exceeds limit");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// update read limit if reading in the same segment
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mWriteSegment == 0 && mReadLimit == mWriteCursor) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mReadLimit = newWriteCursor;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
mWriteCursor = newWriteCursor;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// 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.)
|
|
|
|
NS_ASSERTION(mReadCursor != mWriteCursor ||
|
|
|
|
(mBuffer.GetSegment(0) == mReadCursor &&
|
|
|
|
mWriteCursor == mWriteLimit),
|
|
|
|
"read cursor is bad");
|
|
|
|
|
|
|
|
// update the writable flag on the output stream
|
|
|
|
if (mWriteCursor == mWriteLimit) {
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mBuffer.GetSize() >= mBuffer.GetMaxSize()) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mOutput.SetWritable(false);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
// notify input stream that pipe now contains additional data
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mInput.OnInputReadable(aBytesWritten, events)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mon.Notify();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe::OnPipeException(nsresult aReason, bool aOutputOnly)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
LOG(("PPP nsPipe::OnPipeException [reason=%x output-only=%d]\n",
|
2014-05-14 13:15:46 -07:00
|
|
|
aReason, aOutputOnly));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsPipeEvents events;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// if we've already hit an exception, then ignore this one.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
mStatus = aReason;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// an output-only exception applies to the input end if the pipe has
|
|
|
|
// zero bytes available.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aOutputOnly && !mInput.Available()) {
|
|
|
|
aOutputOnly = false;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!aOutputOnly)
|
|
|
|
if (mInput.OnInputException(aReason, events)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mon.Notify();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (mOutput.OnOutputException(aReason, events)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mon.Notify();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPipeEvents methods:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsPipeEvents::~nsPipeEvents()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// dispatch any pending events
|
|
|
|
|
|
|
|
if (mInputCallback) {
|
|
|
|
mInputCallback->OnInputStreamReady(mInputStream);
|
|
|
|
mInputCallback = 0;
|
|
|
|
mInputStream = 0;
|
|
|
|
}
|
|
|
|
if (mOutputCallback) {
|
|
|
|
mOutputCallback->OnOutputStreamReady(mOutputStream);
|
|
|
|
mOutputCallback = 0;
|
|
|
|
mOutputStream = 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPipeInputStream methods:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsPipeInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsIAsyncInputStream,
|
|
|
|
nsISeekableStream,
|
|
|
|
nsISearchableInputStream,
|
|
|
|
nsIClassInfo)
|
2007-03-25 00:31:45 -07:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsPipeInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsIAsyncInputStream,
|
|
|
|
nsISeekableStream,
|
|
|
|
nsISearchableInputStream)
|
2007-03-25 00:31:45 -07:00
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_CI(nsPipeInputStream)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsPipeInputStream::Wait()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_ASSERTION(mBlocking, "wait on non-blocking pipe input stream");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
while (NS_SUCCEEDED(mPipe->mStatus) && (mAvailable == 0)) {
|
|
|
|
LOG(("III pipe input: waiting for data\n"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
mBlocked = true;
|
|
|
|
mon.Wait();
|
|
|
|
mBlocked = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
LOG(("III pipe input: woke up [pipe-status=%x available=%u]\n",
|
|
|
|
mPipe->mStatus, mAvailable));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return mPipe->mStatus == NS_BASE_STREAM_CLOSED ? NS_OK : mPipe->mStatus;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::OnInputReadable(uint32_t aBytesWritten, nsPipeEvents& aEvents)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
bool result = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
mAvailable += aBytesWritten;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (mCallback && !(mCallbackFlags & WAIT_CLOSURE_ONLY)) {
|
2014-05-14 13:15:46 -07:00
|
|
|
aEvents.NotifyInputReady(this, mCallback);
|
2014-05-05 10:30:43 -07:00
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else if (mBlocked) {
|
2014-05-05 10:30:43 -07:00
|
|
|
result = true;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return result;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::OnInputException(nsresult aReason, nsPipeEvents& aEvents)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
LOG(("nsPipeInputStream::OnInputException [this=%x reason=%x]\n",
|
2014-05-14 13:15:46 -07:00
|
|
|
this, aReason));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
bool result = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_ASSERTION(NS_FAILED(aReason), "huh? successful exception");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// force count of available bytes to zero.
|
|
|
|
mAvailable = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (mCallback) {
|
2014-05-14 13:15:46 -07:00
|
|
|
aEvents.NotifyInputReady(this, mCallback);
|
2014-05-05 10:30:43 -07:00
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else if (mBlocked) {
|
2014-05-05 10:30:43 -07:00
|
|
|
result = true;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return result;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-27 13:38:33 -07:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPipeInputStream::AddRef(void)
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
++mReaderRefCnt;
|
|
|
|
return mPipe->AddRef();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-27 13:38:33 -07:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPipeInputStream::Release(void)
|
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (--mReaderRefCnt == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
Close();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return mPipe->Release();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::CloseWithStatus(nsresult aReason)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
LOG(("III CloseWithStatus [this=%x reason=%x]\n", this, aReason));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(aReason)) {
|
|
|
|
aReason = NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
mPipe->OnPipeException(aReason);
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeInputStream::Close()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
return CloseWithStatus(NS_BASE_STREAM_CLOSED);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::Available(uint64_t* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// nsPipeInputStream supports under 4GB stream only
|
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// return error if pipe closed
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!mAvailable && NS_FAILED(mPipe->mStatus)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return mPipe->mStatus;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aResult = (uint64_t)mAvailable;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::ReadSegments(nsWriteSegmentFun aWriter,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aReadCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
LOG(("III ReadSegments [this=%x count=%u]\n", this, aCount));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
const char* segment;
|
2014-05-05 10:30:43 -07:00
|
|
|
uint32_t segmentLen;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aReadCount = 0;
|
|
|
|
while (aCount) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = mPipe->GetReadSegment(segment, segmentLen);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// ignore this error if we've already read something.
|
2014-05-14 13:15:46 -07:00
|
|
|
if (*aReadCount > 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
|
|
|
|
// pipe is empty
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!mBlocking) {
|
2014-05-05 10:30:43 -07:00
|
|
|
break;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
// wait for some data to be written to the pipe
|
|
|
|
rv = Wait();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
continue;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
// ignore this error, just return.
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mPipe->OnPipeException(rv);
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
// read no more than aCount
|
|
|
|
if (segmentLen > aCount) {
|
|
|
|
segmentLen = aCount;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
uint32_t writeCount, originalLen = segmentLen;
|
|
|
|
while (segmentLen) {
|
|
|
|
writeCount = 0;
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
rv = aWriter(this, aClosure, segment, *aReadCount, segmentLen, &writeCount);
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) || writeCount == 0) {
|
2014-05-14 13:15:46 -07:00
|
|
|
aCount = 0;
|
2014-05-05 10:30:43 -07:00
|
|
|
// any errors returned from the writer end here: do not
|
|
|
|
// propagate to the caller of ReadSegments.
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(writeCount <= segmentLen, "wrote more than expected");
|
|
|
|
segment += writeCount;
|
|
|
|
segmentLen -= writeCount;
|
2014-05-14 13:15:46 -07:00
|
|
|
aCount -= writeCount;
|
|
|
|
*aReadCount += writeCount;
|
2014-05-05 10:30:43 -07:00
|
|
|
mLogicalOffset += writeCount;
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (segmentLen < originalLen) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mPipe->AdvanceReadCursor(originalLen - segmentLen);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::Read(char* aToBuf, uint32_t aBufLen, uint32_t* aReadCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, aToBuf, aBufLen, aReadCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::IsNonBlocking(bool* aNonBlocking)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
*aNonBlocking = !mBlocking;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::AsyncWait(nsIInputStreamCallback* aCallback,
|
|
|
|
uint32_t aFlags,
|
|
|
|
uint32_t aRequestedCount,
|
|
|
|
nsIEventTarget* aTarget)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
LOG(("III AsyncWait [this=%x]\n", this));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsPipeEvents pipeEvents;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// replace a pending callback
|
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!aCallback) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsIInputStreamCallback> proxy;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aTarget) {
|
|
|
|
proxy = NS_NewInputStreamReadyEvent(aCallback, aTarget);
|
|
|
|
aCallback = proxy;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (NS_FAILED(mPipe->mStatus) ||
|
2014-05-14 13:15:46 -07:00
|
|
|
(mAvailable && !(aFlags & WAIT_CLOSURE_ONLY))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// stream is already closed or readable; post event.
|
2014-05-14 13:15:46 -07:00
|
|
|
pipeEvents.NotifyInputReady(this, aCallback);
|
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
// queue up callback object to be notified when data becomes available
|
2014-05-14 13:15:46 -07:00
|
|
|
mCallback = aCallback;
|
|
|
|
mCallbackFlags = aFlags;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::Seek(int32_t aWhence, int64_t aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_NOTREACHED("nsPipeInputStream::Seek");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::Tell(int64_t* aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// return error if pipe closed
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!mAvailable && NS_FAILED(mPipe->mStatus)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return mPipe->mStatus;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aOffset = mLogicalOffset;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeInputStream::SetEOF()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_NOTREACHED("nsPipeInputStream::SetEOF");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
#define COMPARE(s1, s2, i) \
|
2014-05-14 13:15:46 -07:00
|
|
|
(aIgnoreCase \
|
2014-05-05 10:30:43 -07:00
|
|
|
? nsCRT::strncasecmp((const char *)s1, (const char *)s2, (uint32_t)i) \
|
|
|
|
: nsCRT::strncmp((const char *)s1, (const char *)s2, (uint32_t)i))
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeInputStream::Search(const char* aForString,
|
|
|
|
bool aIgnoreCase,
|
|
|
|
bool* aFound,
|
|
|
|
uint32_t* aOffsetSearchedTo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
LOG(("III Search [for=%s ic=%u]\n", aForString, aIgnoreCase));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
char* cursor1;
|
|
|
|
char* limit1;
|
2014-05-05 10:30:43 -07:00
|
|
|
uint32_t index = 0, offset = 0;
|
2014-05-14 13:15:46 -07:00
|
|
|
uint32_t strLen = strlen(aForString);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
mPipe->PeekSegment(0, cursor1, limit1);
|
|
|
|
if (cursor1 == limit1) {
|
2014-05-14 13:15:46 -07:00
|
|
|
*aFound = false;
|
|
|
|
*aOffsetSearchedTo = 0;
|
|
|
|
LOG((" result [aFound=%u offset=%u]\n", *aFound, *aOffsetSearchedTo));
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
uint32_t i, len1 = limit1 - cursor1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// check if the string is in the buffer segment
|
|
|
|
for (i = 0; i < len1 - strLen + 1; i++) {
|
2014-05-14 13:15:46 -07:00
|
|
|
if (COMPARE(&cursor1[i], aForString, strLen) == 0) {
|
|
|
|
*aFound = true;
|
|
|
|
*aOffsetSearchedTo = offset + i;
|
|
|
|
LOG((" result [aFound=%u offset=%u]\n", *aFound, *aOffsetSearchedTo));
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// get the next segment
|
2014-05-14 13:15:46 -07:00
|
|
|
char* cursor2;
|
|
|
|
char* limit2;
|
2014-05-05 10:30:43 -07:00
|
|
|
uint32_t len2;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
index++;
|
|
|
|
offset += len1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
mPipe->PeekSegment(index, cursor2, limit2);
|
|
|
|
if (cursor2 == limit2) {
|
2014-05-14 13:15:46 -07:00
|
|
|
*aFound = false;
|
|
|
|
*aOffsetSearchedTo = offset - strLen + 1;
|
|
|
|
LOG((" result [aFound=%u offset=%u]\n", *aFound, *aOffsetSearchedTo));
|
2014-05-05 10:30:43 -07: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-14 13:15:46 -07:00
|
|
|
const char* strPart2 = &aForString[strLen - strPart2Len];
|
2014-05-05 10:30:43 -07:00
|
|
|
uint32_t bufSeg1Offset = len1 - strPart1Len;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (COMPARE(&cursor1[bufSeg1Offset], aForString, strPart1Len) == 0 &&
|
2014-05-05 10:30:43 -07:00
|
|
|
COMPARE(cursor2, strPart2, strPart2Len) == 0) {
|
2014-05-14 13:15:46 -07:00
|
|
|
*aFound = true;
|
|
|
|
*aOffsetSearchedTo = offset - strPart1Len;
|
|
|
|
LOG((" result [aFound=%u offset=%u]\n", *aFound, *aOffsetSearchedTo));
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// finally continue with the next buffer
|
|
|
|
cursor1 = cursor2;
|
|
|
|
limit1 = limit2;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("can't get here");
|
|
|
|
return NS_ERROR_UNEXPECTED; // keep compiler happy
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsPipeOutputStream methods:
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE(nsPipeOutputStream,
|
|
|
|
nsIOutputStream,
|
|
|
|
nsIAsyncOutputStream,
|
|
|
|
nsIClassInfo)
|
2007-03-25 00:31:45 -07:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsPipeOutputStream,
|
|
|
|
nsIOutputStream,
|
|
|
|
nsIAsyncOutputStream)
|
2007-03-25 00:31:45 -07:00
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_CI(nsPipeOutputStream)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsPipeOutputStream::Wait()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_ASSERTION(mBlocking, "wait on non-blocking pipe output stream");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07: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));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return mPipe->mStatus == NS_BASE_STREAM_CLOSED ? NS_OK : mPipe->mStatus;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeOutputStream::OnOutputWritable(nsPipeEvents& aEvents)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
bool result = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
mWritable = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (mCallback && !(mCallbackFlags & WAIT_CLOSURE_ONLY)) {
|
2014-05-14 13:15:46 -07:00
|
|
|
aEvents.NotifyOutputReady(this, mCallback);
|
2014-05-05 10:30:43 -07:00
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else if (mBlocked) {
|
2014-05-05 10:30:43 -07:00
|
|
|
result = true;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return result;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeOutputStream::OnOutputException(nsresult aReason, nsPipeEvents& aEvents)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
LOG(("nsPipeOutputStream::OnOutputException [this=%x reason=%x]\n",
|
2014-05-14 13:15:46 -07:00
|
|
|
this, aReason));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
bool result = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_ASSERTION(NS_FAILED(aReason), "huh? successful exception");
|
2014-05-05 10:30:43 -07:00
|
|
|
mWritable = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (mCallback) {
|
2014-05-14 13:15:46 -07:00
|
|
|
aEvents.NotifyOutputReady(this, mCallback);
|
2014-05-05 10:30:43 -07:00
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else if (mBlocked) {
|
2014-05-05 10:30:43 -07:00
|
|
|
result = true;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
return result;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-27 13:38:33 -07:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPipeOutputStream::AddRef()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
++mWriterRefCnt;
|
|
|
|
return mPipe->AddRef();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-03-27 13:38:33 -07:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPipeOutputStream::Release()
|
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (--mWriterRefCnt == 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
Close();
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
return mPipe->Release();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeOutputStream::CloseWithStatus(nsresult aReason)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
LOG(("OOO CloseWithStatus [this=%x reason=%x]\n", this, aReason));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(aReason)) {
|
|
|
|
aReason = NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// input stream may remain open
|
2014-05-14 13:15:46 -07:00
|
|
|
mPipe->OnPipeException(aReason, true);
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeOutputStream::Close()
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
return CloseWithStatus(NS_BASE_STREAM_CLOSED);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeOutputStream::WriteSegments(nsReadSegmentFun aReader,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aWriteCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
LOG(("OOO WriteSegments [this=%x count=%u]\n", this, aCount));
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
char* segment;
|
2014-05-05 10:30:43 -07:00
|
|
|
uint32_t segmentLen;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aWriteCount = 0;
|
|
|
|
while (aCount) {
|
2014-05-05 10:30:43 -07: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-14 13:15:46 -07:00
|
|
|
if (*aWriteCount > 0) {
|
2014-05-05 10:30:43 -07:00
|
|
|
rv = NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
// wait for the pipe to have an empty segment.
|
|
|
|
rv = Wait();
|
2014-05-14 13:15:46 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-05 10:30:43 -07:00
|
|
|
continue;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
mPipe->OnPipeException(rv);
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
// write no more than aCount
|
|
|
|
if (segmentLen > aCount) {
|
|
|
|
segmentLen = aCount;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
uint32_t readCount, originalLen = segmentLen;
|
|
|
|
while (segmentLen) {
|
|
|
|
readCount = 0;
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
rv = aReader(this, aClosure, segment, *aWriteCount, segmentLen, &readCount);
|
2014-05-05 10:30:43 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) || readCount == 0) {
|
2014-05-14 13:15:46 -07:00
|
|
|
aCount = 0;
|
|
|
|
// any errors returned from the aReader end here: do not
|
2014-05-05 10:30:43 -07: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-14 13:15:46 -07:00
|
|
|
aCount -= readCount;
|
|
|
|
*aWriteCount += readCount;
|
2014-05-05 10:30:43 -07:00
|
|
|
mLogicalOffset += readCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (segmentLen < originalLen) {
|
2014-05-05 10:30:43 -07:00
|
|
|
mPipe->AdvanceWriteCursor(originalLen - segmentLen);
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static NS_METHOD
|
2014-05-14 13:15:46 -07:00
|
|
|
nsReadFromRawBuffer(nsIOutputStream* aOutStr,
|
|
|
|
void* aClosure,
|
|
|
|
char* aToRawSegment,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aReadCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
const char* fromBuf = (const char*)aClosure;
|
|
|
|
memcpy(aToRawSegment, &fromBuf[aOffset], aCount);
|
|
|
|
*aReadCount = aCount;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeOutputStream::Write(const char* aFromBuf,
|
|
|
|
uint32_t aBufLen,
|
|
|
|
uint32_t* aWriteCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
return WriteSegments(nsReadFromRawBuffer, (void*)aFromBuf, aBufLen, aWriteCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipeOutputStream::Flush(void)
|
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
// nothing to do
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static NS_METHOD
|
2014-05-14 13:15:46 -07:00
|
|
|
nsReadFromInputStream(nsIOutputStream* aOutStr,
|
|
|
|
void* aClosure,
|
|
|
|
char* aToRawSegment,
|
|
|
|
uint32_t aOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aReadCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
nsIInputStream* fromStream = (nsIInputStream*)aClosure;
|
|
|
|
return fromStream->Read(aToRawSegment, aCount, aReadCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeOutputStream::WriteFrom(nsIInputStream* aFromStream,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aWriteCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
return WriteSegments(nsReadFromInputStream, aFromStream, aCount, aWriteCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeOutputStream::IsNonBlocking(bool* aNonBlocking)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
*aNonBlocking = !mBlocking;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeOutputStream::AsyncWait(nsIOutputStreamCallback* aCallback,
|
|
|
|
uint32_t aFlags,
|
|
|
|
uint32_t aRequestedCount,
|
|
|
|
nsIEventTarget* aTarget)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
LOG(("OOO AsyncWait [this=%x]\n", this));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsPipeEvents pipeEvents;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mPipe->mReentrantMonitor);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
// replace a pending callback
|
|
|
|
mCallback = 0;
|
|
|
|
mCallbackFlags = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
if (!aCallback) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
nsCOMPtr<nsIOutputStreamCallback> proxy;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aTarget) {
|
|
|
|
proxy = NS_NewOutputStreamReadyEvent(aCallback, aTarget);
|
|
|
|
aCallback = proxy;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-05 10:30:43 -07:00
|
|
|
if (NS_FAILED(mPipe->mStatus) ||
|
2014-05-14 13:15:46 -07:00
|
|
|
(mWritable && !(aFlags & WAIT_CLOSURE_ONLY))) {
|
2014-05-05 10:30:43 -07:00
|
|
|
// stream is already closed or writable; post event.
|
2014-05-14 13:15:46 -07:00
|
|
|
pipeEvents.NotifyOutputReady(this, aCallback);
|
|
|
|
} else {
|
2014-05-05 10:30:43 -07:00
|
|
|
// queue up callback object to be notified when data becomes available
|
2014-05-14 13:15:46 -07:00
|
|
|
mCallback = aCallback;
|
|
|
|
mCallbackFlags = aFlags;
|
2014-05-05 10:30:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_NewPipe(nsIInputStream** aPipeIn,
|
|
|
|
nsIOutputStream** aPipeOut,
|
|
|
|
uint32_t aSegmentSize,
|
|
|
|
uint32_t aMaxSize,
|
|
|
|
bool aNonBlockingInput,
|
|
|
|
bool aNonBlockingOutput)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aSegmentSize == 0) {
|
|
|
|
aSegmentSize = DEFAULT_SEGMENT_SIZE;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
// Handle aMaxSize of UINT32_MAX as a special case
|
2014-05-05 10:30:43 -07:00
|
|
|
uint32_t segmentCount;
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aMaxSize == UINT32_MAX) {
|
2014-05-05 10:30:43 -07:00
|
|
|
segmentCount = UINT32_MAX;
|
2014-05-14 13:15:46 -07:00
|
|
|
} else {
|
|
|
|
segmentCount = aMaxSize / aSegmentSize;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsIAsyncInputStream* in;
|
|
|
|
nsIAsyncOutputStream* out;
|
|
|
|
nsresult rv = NS_NewPipe2(&in, &out, aNonBlockingInput, aNonBlockingOutput,
|
|
|
|
aSegmentSize, segmentCount);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
*aPipeIn = in;
|
|
|
|
*aPipeOut = out;
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
NS_NewPipe2(nsIAsyncInputStream** aPipeIn,
|
|
|
|
nsIAsyncOutputStream** aPipeOut,
|
|
|
|
bool aNonBlockingInput,
|
|
|
|
bool aNonBlockingOutput,
|
|
|
|
uint32_t aSegmentSize,
|
|
|
|
uint32_t aSegmentCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-05 10:30:43 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipe* pipe = new nsPipe();
|
|
|
|
if (!pipe) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
rv = pipe->Init(aNonBlockingInput,
|
|
|
|
aNonBlockingOutput,
|
|
|
|
aSegmentSize,
|
|
|
|
aSegmentCount);
|
2014-05-05 10:30:43 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_ADDREF(pipe);
|
|
|
|
NS_RELEASE(pipe);
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-14 13:15:46 -07:00
|
|
|
pipe->GetInputStream(aPipeIn);
|
|
|
|
pipe->GetOutputStream(aPipeOut);
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2014-05-14 13:15:46 -07:00
|
|
|
nsPipeConstructor(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-14 13:15:46 -07:00
|
|
|
if (aOuter) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
|
|
|
nsPipe* pipe = new nsPipe();
|
|
|
|
if (!pipe) {
|
2014-05-05 10:30:43 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-14 13:15:46 -07:00
|
|
|
}
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_ADDREF(pipe);
|
2014-05-14 13:15:46 -07:00
|
|
|
nsresult rv = pipe->QueryInterface(aIID, aResult);
|
2014-05-05 10:30:43 -07:00
|
|
|
NS_RELEASE(pipe);
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|