netwerk/base/src/nsBaseContentStream.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/base/src/nsBaseContentStream.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,82 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsBaseContentStream_h__
    1.10 +#define nsBaseContentStream_h__
    1.11 +
    1.12 +#include "nsIAsyncInputStream.h"
    1.13 +#include "nsIEventTarget.h"
    1.14 +#include "nsCOMPtr.h"
    1.15 +
    1.16 +//-----------------------------------------------------------------------------
    1.17 +// nsBaseContentStream is designed to be subclassed with the intention of being
    1.18 +// used to satisfy the nsBaseChannel::OpenContentStream method.
    1.19 +//
    1.20 +// The subclass typically overrides the default Available, ReadSegments and
    1.21 +// CloseWithStatus methods.  By default, Read is implemented in terms of 
    1.22 +// ReadSegments, and Close is implemented in terms of CloseWithStatus.  If
    1.23 +// CloseWithStatus is overriden, then the subclass will usually want to call
    1.24 +// the base class' CloseWithStatus method before returning.
    1.25 +//
    1.26 +// If the stream is non-blocking, then readSegments may return the exception
    1.27 +// NS_BASE_STREAM_WOULD_BLOCK if there is no data available and the stream is
    1.28 +// not at the "end-of-file" or already closed.  This error code must not be
    1.29 +// returned from the Available implementation.  When the caller receives this
    1.30 +// error code, he may choose to call the stream's AsyncWait method, in which
    1.31 +// case the base stream will have a non-null PendingCallback.  When the stream
    1.32 +// has data or encounters an error, it should be sure to dispatch a pending
    1.33 +// callback if one exists (see DispatchCallback).  The implementation of the
    1.34 +// base stream's CloseWithStatus (and Close) method will ensure that any
    1.35 +// pending callback is dispatched.  It is the responsibility of the subclass
    1.36 +// to ensure that the pending callback is dispatched when it wants to have its
    1.37 +// ReadSegments method called again.
    1.38 +
    1.39 +class nsBaseContentStream : public nsIAsyncInputStream
    1.40 +{
    1.41 +public: 
    1.42 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.43 +  NS_DECL_NSIINPUTSTREAM
    1.44 +  NS_DECL_NSIASYNCINPUTSTREAM
    1.45 +
    1.46 +  nsBaseContentStream(bool nonBlocking)
    1.47 +    : mStatus(NS_OK)
    1.48 +    , mNonBlocking(nonBlocking) {
    1.49 +  }
    1.50 +
    1.51 +  nsresult Status() { return mStatus; }
    1.52 +  bool IsNonBlocking() { return mNonBlocking; }
    1.53 +  bool IsClosed() { return NS_FAILED(mStatus); }
    1.54 +
    1.55 +  // Called to test if the stream has a pending callback.
    1.56 +  bool HasPendingCallback() { return mCallback != nullptr; }
    1.57 +
    1.58 +  // The current dispatch target (may be null) for the pending callback if any.
    1.59 +  nsIEventTarget *CallbackTarget() { return mCallbackTarget; }
    1.60 +
    1.61 +  // Called to dispatch a pending callback.  If there is no pending callback,
    1.62 +  // then this function does nothing.  Pass true to this function to cause the
    1.63 +  // callback to occur asynchronously; otherwise, the callback will happen 
    1.64 +  // before this function returns.
    1.65 +  void DispatchCallback(bool async = true);
    1.66 +
    1.67 +  // Helper function to make code more self-documenting.
    1.68 +  void DispatchCallbackSync() { DispatchCallback(false); }
    1.69 +
    1.70 +protected:
    1.71 +  virtual ~nsBaseContentStream() {}
    1.72 +
    1.73 +private:
    1.74 +  // Called from the base stream's AsyncWait method when a pending callback
    1.75 +  // is installed on the stream.
    1.76 +  virtual void OnCallbackPending() {}
    1.77 +
    1.78 +private:
    1.79 +  nsCOMPtr<nsIInputStreamCallback> mCallback;
    1.80 +  nsCOMPtr<nsIEventTarget>         mCallbackTarget;
    1.81 +  nsresult                         mStatus;
    1.82 +  bool                             mNonBlocking;
    1.83 +};
    1.84 +
    1.85 +#endif // nsBaseContentStream_h__

mercurial