Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / netwerk / base / src / nsUnicharStreamLoader.cpp
blobb511bc3eb49e9f284ec318240f940f116524959c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Boris Zbarsky <bzbarsky@mit.edu> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsUnicharStreamLoader.h"
40 #include "nsNetUtil.h"
41 #include "nsProxiedService.h"
42 #include "nsIChannel.h"
43 #include "nsIUnicharInputStream.h"
44 #include "nsIConverterInputStream.h"
45 #include "nsIPipe.h"
47 #ifdef DEBUG // needed for IsASCII assertion
48 #include "nsReadableUtils.h"
49 #endif // DEBUG
51 NS_IMETHODIMP
52 nsUnicharStreamLoader::Init(nsIUnicharStreamLoaderObserver *aObserver,
53 PRUint32 aSegmentSize)
55 NS_ENSURE_ARG_POINTER(aObserver);
57 if (aSegmentSize <= 0) {
58 aSegmentSize = nsIUnicharStreamLoader::DEFAULT_SEGMENT_SIZE;
61 mObserver = aObserver;
62 mCharset.Truncate();
63 mChannel = nsnull; // Leave this null till OnStopRequest
64 mSegmentSize = aSegmentSize;
65 return NS_OK;
68 nsresult
69 nsUnicharStreamLoader::Create(nsISupports *aOuter,
70 REFNSIID aIID,
71 void **aResult)
73 if (aOuter) return NS_ERROR_NO_AGGREGATION;
75 nsUnicharStreamLoader* it = new nsUnicharStreamLoader();
76 if (it == nsnull)
77 return NS_ERROR_OUT_OF_MEMORY;
78 NS_ADDREF(it);
79 nsresult rv = it->QueryInterface(aIID, aResult);
80 NS_RELEASE(it);
81 return rv;
84 NS_IMPL_ISUPPORTS3(nsUnicharStreamLoader, nsIUnicharStreamLoader,
85 nsIRequestObserver, nsIStreamListener)
87 /* readonly attribute nsIChannel channel; */
88 NS_IMETHODIMP
89 nsUnicharStreamLoader::GetChannel(nsIChannel **aChannel)
91 NS_IF_ADDREF(*aChannel = mChannel);
92 return NS_OK;
95 /* readonly attribute nsACString charset */
96 NS_IMETHODIMP
97 nsUnicharStreamLoader::GetCharset(nsACString& aCharset)
99 aCharset = mCharset;
100 return NS_OK;
103 /* nsIRequestObserver implementation */
104 NS_IMETHODIMP
105 nsUnicharStreamLoader::OnStartRequest(nsIRequest* request,
106 nsISupports *ctxt)
108 mContext = ctxt;
109 return NS_OK;
112 NS_IMETHODIMP
113 nsUnicharStreamLoader::OnStopRequest(nsIRequest *request,
114 nsISupports *ctxt,
115 nsresult aStatus)
117 // if we trigger this assertion, then it means that the channel called
118 // OnStopRequest before returning from AsyncOpen, which is totally
119 // unexpected behavior.
120 if (!mObserver) {
121 NS_ERROR("No way we should not have an mObserver here!");
122 return NS_ERROR_UNEXPECTED;
125 // Make sure mChannel points to the channel that we ended up with
126 mChannel = do_QueryInterface(request);
128 if (mInputStream) {
129 nsresult rv;
130 // We got some data at some point. I guess we should tell our
131 // observer about it or something....
133 // Determine the charset
134 PRUint32 readCount = 0;
135 rv = mInputStream->ReadSegments(WriteSegmentFun,
136 this,
137 mSegmentSize,
138 &readCount);
139 if (NS_FAILED(rv)) {
140 rv = mObserver->OnStreamComplete(this, mContext, rv, nsnull);
141 goto cleanup;
144 nsCOMPtr<nsIConverterInputStream> uin =
145 do_CreateInstance("@mozilla.org/intl/converter-input-stream;1",
146 &rv);
147 if (NS_FAILED(rv)) {
148 rv = mObserver->OnStreamComplete(this, mContext, rv, nsnull);
149 goto cleanup;
152 rv = uin->Init(mInputStream,
153 mCharset.get(),
154 mSegmentSize,
155 nsIConverterInputStream::DEFAULT_REPLACEMENT_CHARACTER);
157 if (NS_FAILED(rv)) {
158 rv = mObserver->OnStreamComplete(this, mContext, rv, nsnull);
159 goto cleanup;
162 mObserver->OnStreamComplete(this, mContext, aStatus, uin);
164 } else {
165 // We never got any data, so just tell our observer that we are
166 // done and give them no stream
167 mObserver->OnStreamComplete(this, mContext, aStatus, nsnull);
170 // Clean up.
171 cleanup:
172 mObserver = nsnull;
173 mChannel = nsnull;
174 mContext = nsnull;
175 mInputStream = nsnull;
176 mOutputStream = nsnull;
177 return NS_OK;
180 /* nsIStreamListener implementation */
181 NS_METHOD
182 nsUnicharStreamLoader::WriteSegmentFun(nsIInputStream *aInputStream,
183 void *aClosure,
184 const char *aSegment,
185 PRUint32 aToOffset,
186 PRUint32 aCount,
187 PRUint32 *aWriteCount)
189 nsUnicharStreamLoader *self = (nsUnicharStreamLoader *) aClosure;
190 if (self->mCharset.IsEmpty()) {
191 // First time through. Call our observer.
192 NS_ASSERTION(self->mObserver, "This should never be possible");
194 nsresult rv = self->mObserver->OnDetermineCharset(self,
195 self->mContext,
196 aSegment,
197 aCount,
198 self->mCharset);
200 if (NS_FAILED(rv) || self->mCharset.IsEmpty()) {
201 // The observer told us nothing useful
202 self->mCharset.AssignLiteral("ISO-8859-1");
205 NS_ASSERTION(IsASCII(self->mCharset),
206 "Why is the charset name non-ascii? Whose bright idea was that?");
208 // Don't consume any data
209 *aWriteCount = 0;
210 return NS_BASE_STREAM_WOULD_BLOCK;
214 NS_IMETHODIMP
215 nsUnicharStreamLoader::OnDataAvailable(nsIRequest *aRequest,
216 nsISupports *aContext,
217 nsIInputStream *aInputStream,
218 PRUint32 aSourceOffset,
219 PRUint32 aCount)
221 nsresult rv = NS_OK;
222 if (!mInputStream) {
223 // We are not initialized. Time to set things up.
224 NS_ASSERTION(!mOutputStream, "Why are we sorta-initialized?");
225 rv = NS_NewPipe(getter_AddRefs(mInputStream),
226 getter_AddRefs(mOutputStream),
227 mSegmentSize,
228 PRUint32(-1), // give me all the data you can!
229 PR_TRUE, // non-blocking input
230 PR_TRUE); // non-blocking output
231 if (NS_FAILED(rv))
232 return rv;
235 PRUint32 writeCount = 0;
236 do {
237 rv = mOutputStream->WriteFrom(aInputStream, aCount, &writeCount);
238 if (NS_FAILED(rv)) return rv;
239 aCount -= writeCount;
240 } while (aCount > 0);
242 return NS_OK;