Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / netwerk / base / src / nsSyncStreamListener.cpp
blob74d35e9729550ef56c5f18408a9df43015ac3c1b
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
12 * License.
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.
20 * Contributor(s):
21 * IBM Corp.
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"
38 #include "nsIPipe.h"
39 #include "nsNetSegmentUtils.h"
41 nsresult
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
48 PR_FALSE,
49 PR_FALSE);
52 nsresult
53 nsSyncStreamListener::WaitForData()
55 mKeepWaiting = PR_TRUE;
57 while (mKeepWaiting)
58 NS_ENSURE_STATE(NS_ProcessNextEvent(NS_GetCurrentThread()));
60 return NS_OK;
63 //-----------------------------------------------------------------------------
64 // nsSyncStreamListener::nsISupports
65 //-----------------------------------------------------------------------------
67 NS_IMPL_ISUPPORTS4(nsSyncStreamListener,
68 nsIStreamListener,
69 nsIRequestObserver,
70 nsIInputStream,
71 nsISyncStreamListener)
73 //-----------------------------------------------------------------------------
74 // nsSyncStreamListener::nsISyncStreamListener
75 //-----------------------------------------------------------------------------
77 NS_IMETHODIMP
78 nsSyncStreamListener::GetInputStream(nsIInputStream **result)
80 NS_ADDREF(*result = this);
81 return NS_OK;
84 //-----------------------------------------------------------------------------
85 // nsSyncStreamListener::nsIStreamListener
86 //-----------------------------------------------------------------------------
88 NS_IMETHODIMP
89 nsSyncStreamListener::OnStartRequest(nsIRequest *request,
90 nsISupports *context)
92 return NS_OK;
95 NS_IMETHODIMP
96 nsSyncStreamListener::OnDataAvailable(nsIRequest *request,
97 nsISupports *context,
98 nsIInputStream *stream,
99 PRUint32 offset,
100 PRUint32 count)
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.
110 if (NS_FAILED(rv))
111 return rv;
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
118 return NS_OK;
121 NS_IMETHODIMP
122 nsSyncStreamListener::OnStopRequest(nsIRequest *request,
123 nsISupports *context,
124 nsresult status)
126 mStatus = status;
127 mKeepWaiting = PR_FALSE; // unblock Read
128 mDone = PR_TRUE;
129 return NS_OK;
132 //-----------------------------------------------------------------------------
133 // nsSyncStreamListener::nsIInputStream
134 //-----------------------------------------------------------------------------
136 NS_IMETHODIMP
137 nsSyncStreamListener::Close()
139 mStatus = NS_BASE_STREAM_CLOSED;
140 mDone = PR_TRUE;
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.
145 if (mPipeIn) {
146 mPipeIn->Close();
147 mPipeIn = nsnull;
149 return NS_OK;
152 NS_IMETHODIMP
153 nsSyncStreamListener::Available(PRUint32 *result)
155 if (NS_FAILED(mStatus))
156 return 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);
164 return mStatus;
167 NS_IMETHODIMP
168 nsSyncStreamListener::Read(char *buf,
169 PRUint32 bufLen,
170 PRUint32 *result)
172 if (mStatus == NS_BASE_STREAM_CLOSED) {
173 *result = 0;
174 return NS_OK;
177 PRUint32 avail;
178 if (NS_FAILED(Available(&avail)))
179 return mStatus;
181 avail = PR_MIN(avail, bufLen);
182 mStatus = mPipeIn->Read(buf, avail, result);
183 return mStatus;
186 NS_IMETHODIMP
187 nsSyncStreamListener::ReadSegments(nsWriteSegmentFun writer,
188 void *closure,
189 PRUint32 count,
190 PRUint32 *result)
192 if (mStatus == NS_BASE_STREAM_CLOSED) {
193 *result = 0;
194 return NS_OK;
197 PRUint32 avail;
198 if (NS_FAILED(Available(&avail)))
199 return mStatus;
201 avail = PR_MIN(avail, count);
202 mStatus = mPipeIn->ReadSegments(writer, closure, avail, result);
203 return mStatus;
206 NS_IMETHODIMP
207 nsSyncStreamListener::IsNonBlocking(PRBool *result)
209 *result = PR_FALSE;
210 return NS_OK;