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 : #include "nsBaseContentStream.h"
39 : #include "nsStreamUtils.h"
40 :
41 : //-----------------------------------------------------------------------------
42 :
43 : void
44 37 : nsBaseContentStream::DispatchCallback(bool async)
45 : {
46 37 : if (!mCallback)
47 17 : return;
48 :
49 : // It's important to clear mCallback and mCallbackTarget up-front because the
50 : // OnInputStreamReady implementation may call our AsyncWait method.
51 :
52 40 : nsCOMPtr<nsIInputStreamCallback> callback;
53 20 : if (async) {
54 1 : NS_NewInputStreamReadyEvent(getter_AddRefs(callback), mCallback,
55 1 : mCallbackTarget);
56 1 : if (!callback)
57 : return; // out of memory!
58 1 : mCallback = nsnull;
59 : } else {
60 19 : callback.swap(mCallback);
61 : }
62 20 : mCallbackTarget = nsnull;
63 :
64 20 : callback->OnInputStreamReady(this);
65 : }
66 :
67 : //-----------------------------------------------------------------------------
68 : // nsBaseContentStream::nsISupports
69 :
70 149 : NS_IMPL_THREADSAFE_ADDREF(nsBaseContentStream)
71 149 : NS_IMPL_THREADSAFE_RELEASE(nsBaseContentStream)
72 :
73 : // We only support nsIAsyncInputStream when we are in non-blocking mode.
74 73 : NS_INTERFACE_MAP_BEGIN(nsBaseContentStream)
75 73 : NS_INTERFACE_MAP_ENTRY(nsIInputStream)
76 36 : NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStream, mNonBlocking)
77 17 : NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
78 17 : NS_INTERFACE_MAP_END_THREADSAFE
79 :
80 : //-----------------------------------------------------------------------------
81 : // nsBaseContentStream::nsIInputStream
82 :
83 : NS_IMETHODIMP
84 18 : nsBaseContentStream::Close()
85 : {
86 18 : return IsClosed() ? NS_OK : CloseWithStatus(NS_BASE_STREAM_CLOSED);
87 : }
88 :
89 : NS_IMETHODIMP
90 2 : nsBaseContentStream::Available(PRUint32 *result)
91 : {
92 2 : *result = 0;
93 2 : return mStatus;
94 : }
95 :
96 : NS_IMETHODIMP
97 17 : nsBaseContentStream::Read(char *buf, PRUint32 count, PRUint32 *result)
98 : {
99 17 : return ReadSegments(NS_CopySegmentToBuffer, buf, count, result);
100 : }
101 :
102 : NS_IMETHODIMP
103 0 : nsBaseContentStream::ReadSegments(nsWriteSegmentFun fun, void *closure,
104 : PRUint32 count, PRUint32 *result)
105 : {
106 0 : *result = 0;
107 :
108 0 : if (mStatus == NS_BASE_STREAM_CLOSED)
109 0 : return NS_OK;
110 :
111 : // No data yet
112 0 : if (!IsClosed() && IsNonBlocking())
113 0 : return NS_BASE_STREAM_WOULD_BLOCK;
114 :
115 0 : return mStatus;
116 : }
117 :
118 : NS_IMETHODIMP
119 18 : nsBaseContentStream::IsNonBlocking(bool *result)
120 : {
121 18 : *result = mNonBlocking;
122 18 : return NS_OK;
123 : }
124 :
125 : //-----------------------------------------------------------------------------
126 : // nsBaseContentStream::nsIAsyncInputStream
127 :
128 : NS_IMETHODIMP
129 18 : nsBaseContentStream::CloseWithStatus(nsresult status)
130 : {
131 18 : if (IsClosed())
132 0 : return NS_OK;
133 :
134 18 : NS_ENSURE_ARG(NS_FAILED(status));
135 18 : mStatus = status;
136 :
137 18 : DispatchCallback();
138 18 : return NS_OK;
139 : }
140 :
141 : NS_IMETHODIMP
142 20 : nsBaseContentStream::AsyncWait(nsIInputStreamCallback *callback,
143 : PRUint32 flags, PRUint32 requestedCount,
144 : nsIEventTarget *target)
145 : {
146 : // Our _only_ consumer is nsInputStreamPump, so we simplify things here by
147 : // making assumptions about how we will be called.
148 20 : NS_ASSERTION(target, "unexpected parameter");
149 20 : NS_ASSERTION(flags == 0, "unexpected parameter");
150 20 : NS_ASSERTION(requestedCount == 0, "unexpected parameter");
151 :
152 : #ifdef DEBUG
153 : bool correctThread;
154 20 : target->IsOnCurrentThread(&correctThread);
155 20 : NS_ASSERTION(correctThread, "event target must be on the current thread");
156 : #endif
157 :
158 20 : mCallback = callback;
159 20 : mCallbackTarget = target;
160 :
161 20 : if (!mCallback)
162 0 : return NS_OK;
163 :
164 : // If we're already closed, then dispatch this callback immediately.
165 20 : if (IsClosed()) {
166 0 : DispatchCallback();
167 0 : return NS_OK;
168 : }
169 :
170 20 : OnCallbackPending();
171 20 : return NS_OK;
172 : }
|