2005-11-12 18:17:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
#ifndef nsBaseContentStream_h__
|
|
|
|
#define nsBaseContentStream_h__
|
|
|
|
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsBaseContentStream is designed to be subclassed with the intention of being
|
|
|
|
// used to satisfy the nsBaseChannel::OpenContentStream method.
|
|
|
|
//
|
|
|
|
// The subclass typically overrides the default Available, ReadSegments and
|
|
|
|
// CloseWithStatus methods. By default, Read is implemented in terms of
|
|
|
|
// ReadSegments, and Close is implemented in terms of CloseWithStatus. If
|
|
|
|
// CloseWithStatus is overriden, then the subclass will usually want to call
|
|
|
|
// the base class' CloseWithStatus method before returning.
|
|
|
|
//
|
|
|
|
// If the stream is non-blocking, then readSegments may return the exception
|
|
|
|
// NS_BASE_STREAM_WOULD_BLOCK if there is no data available and the stream is
|
|
|
|
// not at the "end-of-file" or already closed. This error code must not be
|
|
|
|
// returned from the Available implementation. When the caller receives this
|
|
|
|
// error code, he may choose to call the stream's AsyncWait method, in which
|
|
|
|
// case the base stream will have a non-null PendingCallback. When the stream
|
|
|
|
// has data or encounters an error, it should be sure to dispatch a pending
|
|
|
|
// callback if one exists (see DispatchCallback). The implementation of the
|
|
|
|
// base stream's CloseWithStatus (and Close) method will ensure that any
|
|
|
|
// pending callback is dispatched. It is the responsibility of the subclass
|
|
|
|
// to ensure that the pending callback is dispatched when it wants to have its
|
|
|
|
// ReadSegments method called again.
|
|
|
|
|
|
|
|
class nsBaseContentStream : public nsIAsyncInputStream
|
|
|
|
{
|
|
|
|
public:
|
2013-07-19 02:24:13 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2005-11-12 18:17:19 +00:00
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSIASYNCINPUTSTREAM
|
|
|
|
|
2014-08-05 13:20:24 +00:00
|
|
|
explicit nsBaseContentStream(bool nonBlocking)
|
2005-11-12 18:17:19 +00:00
|
|
|
: mStatus(NS_OK)
|
2006-01-02 02:30:32 +00:00
|
|
|
, mNonBlocking(nonBlocking) {
|
2005-11-12 18:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Status() { return mStatus; }
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsNonBlocking() { return mNonBlocking; }
|
|
|
|
bool IsClosed() { return NS_FAILED(mStatus); }
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
// Called to test if the stream has a pending callback.
|
2012-07-30 14:20:58 +00:00
|
|
|
bool HasPendingCallback() { return mCallback != nullptr; }
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
// The current dispatch target (may be null) for the pending callback if any.
|
2005-11-12 18:17:19 +00:00
|
|
|
nsIEventTarget *CallbackTarget() { return mCallbackTarget; }
|
|
|
|
|
|
|
|
// Called to dispatch a pending callback. If there is no pending callback,
|
|
|
|
// then this function does nothing. Pass true to this function to cause the
|
|
|
|
// callback to occur asynchronously; otherwise, the callback will happen
|
|
|
|
// before this function returns.
|
2011-09-29 06:19:26 +00:00
|
|
|
void DispatchCallback(bool async = true);
|
2005-11-12 18:17:19 +00:00
|
|
|
|
|
|
|
// Helper function to make code more self-documenting.
|
2011-10-17 14:59:28 +00:00
|
|
|
void DispatchCallbackSync() { DispatchCallback(false); }
|
2005-11-12 18:17:19 +00:00
|
|
|
|
2006-03-03 16:27:35 +00:00
|
|
|
protected:
|
|
|
|
virtual ~nsBaseContentStream() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Called from the base stream's AsyncWait method when a pending callback
|
|
|
|
// is installed on the stream.
|
|
|
|
virtual void OnCallbackPending() {}
|
|
|
|
|
2005-11-12 18:17:19 +00:00
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIInputStreamCallback> mCallback;
|
|
|
|
nsCOMPtr<nsIEventTarget> mCallbackTarget;
|
|
|
|
nsresult mStatus;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mNonBlocking;
|
2005-11-12 18:17:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsBaseContentStream_h__
|