1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Mozilla.
16 * The Initial Developer of the Original Code is IBM Corporation.
17 * Portions created by IBM Corporation are Copyright (C) 2003
18 * IBM Corporation. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "nsSyncStreamListener.h"
39 #include "nsNetSegmentUtils.h"
42 nsSyncStreamListener::Init()
44 return NS_NewPipe(getter_AddRefs(mPipeIn
),
45 getter_AddRefs(mPipeOut
),
46 NET_DEFAULT_SEGMENT_SIZE
,
47 PR_UINT32_MAX
, // no size limit
53 nsSyncStreamListener::WaitForData()
55 mKeepWaiting
= PR_TRUE
;
58 NS_ENSURE_STATE(NS_ProcessNextEvent(NS_GetCurrentThread()));
63 //-----------------------------------------------------------------------------
64 // nsSyncStreamListener::nsISupports
65 //-----------------------------------------------------------------------------
67 NS_IMPL_ISUPPORTS4(nsSyncStreamListener
,
71 nsISyncStreamListener
)
73 //-----------------------------------------------------------------------------
74 // nsSyncStreamListener::nsISyncStreamListener
75 //-----------------------------------------------------------------------------
78 nsSyncStreamListener::GetInputStream(nsIInputStream
**result
)
80 NS_ADDREF(*result
= this);
84 //-----------------------------------------------------------------------------
85 // nsSyncStreamListener::nsIStreamListener
86 //-----------------------------------------------------------------------------
89 nsSyncStreamListener::OnStartRequest(nsIRequest
*request
,
96 nsSyncStreamListener::OnDataAvailable(nsIRequest
*request
,
98 nsIInputStream
*stream
,
102 PRUint32 bytesWritten
;
104 nsresult rv
= mPipeOut
->WriteFrom(stream
, count
, &bytesWritten
);
106 // if we get an error, then return failure. this will cause the
107 // channel to be canceled, and as a result our OnStopRequest method
108 // will be called immediately. because of this we do not need to
109 // set mStatus or mKeepWaiting here.
113 // we expect that all data will be written to the pipe because
114 // the pipe was created to have "infinite" room.
115 NS_ASSERTION(bytesWritten
== count
, "did not write all data");
117 mKeepWaiting
= PR_FALSE
; // unblock Read
122 nsSyncStreamListener::OnStopRequest(nsIRequest
*request
,
123 nsISupports
*context
,
127 mKeepWaiting
= PR_FALSE
; // unblock Read
132 //-----------------------------------------------------------------------------
133 // nsSyncStreamListener::nsIInputStream
134 //-----------------------------------------------------------------------------
137 nsSyncStreamListener::Close()
139 mStatus
= NS_BASE_STREAM_CLOSED
;
142 // It'd be nice if we could explicitly cancel the request at this point,
143 // but we don't have a reference to it, so the best we can do is close the
144 // pipe so that the next OnDataAvailable event will fail.
153 nsSyncStreamListener::Available(PRUint32
*result
)
155 if (NS_FAILED(mStatus
))
158 mStatus
= mPipeIn
->Available(result
);
159 if (NS_SUCCEEDED(mStatus
) && (*result
== 0) && !mDone
) {
160 mStatus
= WaitForData();
161 if (NS_SUCCEEDED(mStatus
))
162 mStatus
= mPipeIn
->Available(result
);
168 nsSyncStreamListener::Read(char *buf
,
172 if (mStatus
== NS_BASE_STREAM_CLOSED
) {
178 if (NS_FAILED(Available(&avail
)))
181 avail
= PR_MIN(avail
, bufLen
);
182 mStatus
= mPipeIn
->Read(buf
, avail
, result
);
187 nsSyncStreamListener::ReadSegments(nsWriteSegmentFun writer
,
192 if (mStatus
== NS_BASE_STREAM_CLOSED
) {
198 if (NS_FAILED(Available(&avail
)))
201 avail
= PR_MIN(avail
, count
);
202 mStatus
= mPipeIn
->ReadSegments(writer
, closure
, avail
, result
);
207 nsSyncStreamListener::IsNonBlocking(PRBool
*result
)