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
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
20 : *
21 : * Contributor(s):
22 : * Darin Fisher <darin@meer.net>
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsBaseContentStream_h__
39 : #define nsBaseContentStream_h__
40 :
41 : #include "nsIAsyncInputStream.h"
42 : #include "nsIEventTarget.h"
43 : #include "nsCOMPtr.h"
44 :
45 : //-----------------------------------------------------------------------------
46 : // nsBaseContentStream is designed to be subclassed with the intention of being
47 : // used to satisfy the nsBaseChannel::OpenContentStream method.
48 : //
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.
54 : //
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.
67 :
68 : class nsBaseContentStream : public nsIAsyncInputStream
69 : {
70 : public:
71 : NS_DECL_ISUPPORTS
72 : NS_DECL_NSIINPUTSTREAM
73 : NS_DECL_NSIASYNCINPUTSTREAM
74 :
75 18 : nsBaseContentStream(bool nonBlocking)
76 : : mStatus(NS_OK)
77 18 : , mNonBlocking(nonBlocking) {
78 18 : }
79 :
80 : nsresult Status() { return mStatus; }
81 1 : bool IsNonBlocking() { return mNonBlocking; }
82 74 : bool IsClosed() { return NS_FAILED(mStatus); }
83 :
84 : // Called to test if the stream has a pending callback.
85 36 : bool HasPendingCallback() { return mCallback != nsnull; }
86 :
87 : // The current dispatch target (may be null) for the pending callback if any.
88 19 : nsIEventTarget *CallbackTarget() { return mCallbackTarget; }
89 :
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(bool async = true);
95 :
96 : // Helper function to make code more self-documenting.
97 19 : void DispatchCallbackSync() { DispatchCallback(false); }
98 :
99 : protected:
100 36 : virtual ~nsBaseContentStream() {}
101 :
102 : private:
103 : // Called from the base stream's AsyncWait method when a pending callback
104 : // is installed on the stream.
105 1 : virtual void OnCallbackPending() {}
106 :
107 : private:
108 : nsCOMPtr<nsIInputStreamCallback> mCallback;
109 : nsCOMPtr<nsIEventTarget> mCallbackTarget;
110 : nsresult mStatus;
111 : bool mNonBlocking;
112 : };
113 :
114 : #endif // nsBaseContentStream_h__
|