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
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.
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"
47 #ifdef DEBUG // needed for IsASCII assertion
48 #include "nsReadableUtils.h"
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
;
63 mChannel
= nsnull
; // Leave this null till OnStopRequest
64 mSegmentSize
= aSegmentSize
;
69 nsUnicharStreamLoader::Create(nsISupports
*aOuter
,
73 if (aOuter
) return NS_ERROR_NO_AGGREGATION
;
75 nsUnicharStreamLoader
* it
= new nsUnicharStreamLoader();
77 return NS_ERROR_OUT_OF_MEMORY
;
79 nsresult rv
= it
->QueryInterface(aIID
, aResult
);
84 NS_IMPL_ISUPPORTS3(nsUnicharStreamLoader
, nsIUnicharStreamLoader
,
85 nsIRequestObserver
, nsIStreamListener
)
87 /* readonly attribute nsIChannel channel; */
89 nsUnicharStreamLoader::GetChannel(nsIChannel
**aChannel
)
91 NS_IF_ADDREF(*aChannel
= mChannel
);
95 /* readonly attribute nsACString charset */
97 nsUnicharStreamLoader::GetCharset(nsACString
& aCharset
)
103 /* nsIRequestObserver implementation */
105 nsUnicharStreamLoader::OnStartRequest(nsIRequest
* request
,
113 nsUnicharStreamLoader::OnStopRequest(nsIRequest
*request
,
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.
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
);
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
,
140 rv
= mObserver
->OnStreamComplete(this, mContext
, rv
, nsnull
);
144 nsCOMPtr
<nsIConverterInputStream
> uin
=
145 do_CreateInstance("@mozilla.org/intl/converter-input-stream;1",
148 rv
= mObserver
->OnStreamComplete(this, mContext
, rv
, nsnull
);
152 rv
= uin
->Init(mInputStream
,
155 nsIConverterInputStream::DEFAULT_REPLACEMENT_CHARACTER
);
158 rv
= mObserver
->OnStreamComplete(this, mContext
, rv
, nsnull
);
162 mObserver
->OnStreamComplete(this, mContext
, aStatus
, uin
);
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
);
175 mInputStream
= nsnull
;
176 mOutputStream
= nsnull
;
180 /* nsIStreamListener implementation */
182 nsUnicharStreamLoader::WriteSegmentFun(nsIInputStream
*aInputStream
,
184 const char *aSegment
,
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
,
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
210 return NS_BASE_STREAM_WOULD_BLOCK
;
215 nsUnicharStreamLoader::OnDataAvailable(nsIRequest
*aRequest
,
216 nsISupports
*aContext
,
217 nsIInputStream
*aInputStream
,
218 PRUint32 aSourceOffset
,
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
),
228 PRUint32(-1), // give me all the data you can!
229 PR_TRUE
, // non-blocking input
230 PR_TRUE
); // non-blocking output
235 PRUint32 writeCount
= 0;
237 rv
= mOutputStream
->WriteFrom(aInputStream
, aCount
, &writeCount
);
238 if (NS_FAILED(rv
)) return rv
;
239 aCount
-= writeCount
;
240 } while (aCount
> 0);