1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is Google Inc.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
22 * Darin Fisher <darin@meer.net>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsBaseContentStream_h__
39 #define nsBaseContentStream_h__
41 #include "nsIAsyncInputStream.h"
42 #include "nsIEventTarget.h"
45 //-----------------------------------------------------------------------------
46 // nsBaseContentStream is designed to be subclassed with the intention of being
47 // used to satisfy the nsBaseChannel::OpenContentStream method.
49 // The subclass typically overrides the default Available, ReadSegments and
50 // CloseWithStatus methods. By default, Read is implemented in terms of
51 // ReadSegments, and Close is implemented in terms of CloseWithStatus. If
52 // CloseWithStatus is overriden, then the subclass will usually want to call
53 // the base class' CloseWithStatus method before returning.
55 // If the stream is non-blocking, then readSegments may return the exception
56 // NS_BASE_STREAM_WOULD_BLOCK if there is no data available and the stream is
57 // not at the "end-of-file" or already closed. This error code must not be
58 // returned from the Available implementation. When the caller receives this
59 // error code, he may choose to call the stream's AsyncWait method, in which
60 // case the base stream will have a non-null PendingCallback. When the stream
61 // has data or encounters an error, it should be sure to dispatch a pending
62 // callback if one exists (see DispatchCallback). The implementation of the
63 // base stream's CloseWithStatus (and Close) method will ensure that any
64 // pending callback is dispatched. It is the responsibility of the subclass
65 // to ensure that the pending callback is dispatched when it wants to have its
66 // ReadSegments method called again.
68 class nsBaseContentStream
: public nsIAsyncInputStream
72 NS_DECL_NSIINPUTSTREAM
73 NS_DECL_NSIASYNCINPUTSTREAM
75 nsBaseContentStream(PRBool nonBlocking
)
77 , mNonBlocking(nonBlocking
) {
80 nsresult
Status() { return mStatus
; }
81 PRBool
IsNonBlocking() { return mNonBlocking
; }
82 PRBool
IsClosed() { return NS_FAILED(mStatus
); }
84 // Called to test if the stream has a pending callback.
85 PRBool
HasPendingCallback() { return mCallback
!= nsnull
; }
87 // The current dispatch target (may be null) for the pending callback if any.
88 nsIEventTarget
*CallbackTarget() { return mCallbackTarget
; }
90 // Called to dispatch a pending callback. If there is no pending callback,
91 // then this function does nothing. Pass true to this function to cause the
92 // callback to occur asynchronously; otherwise, the callback will happen
93 // before this function returns.
94 void DispatchCallback(PRBool async
= PR_TRUE
);
96 // Helper function to make code more self-documenting.
97 void DispatchCallbackSync() { DispatchCallback(PR_FALSE
); }
100 virtual ~nsBaseContentStream() {}
103 // Called from the base stream's AsyncWait method when a pending callback
104 // is installed on the stream.
105 virtual void OnCallbackPending() {}
108 nsCOMPtr
<nsIInputStreamCallback
> mCallback
;
109 nsCOMPtr
<nsIEventTarget
> mCallbackTarget
;
111 PRPackedBool mNonBlocking
;
114 #endif // nsBaseContentStream_h__