Back to index

lightning-sunbird  0.9+nobinonly
nsUnicharStreamLoader.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *    Boris Zbarsky <bzbarsky@mit.edu>  (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsUnicharStreamLoader.h"
00040 #include "nsIPipe.h"
00041 #include "nsIChannel.h"
00042 #include "nsNetUtil.h"
00043 #include "nsProxiedService.h"
00044 #include "nsIChannel.h"
00045 #include "nsIUnicharInputStream.h"
00046 #include "nsIConverterInputStream.h"
00047 #include "nsIPipe.h"
00048 
00049 #ifdef DEBUG // needed for IsASCII assertion
00050 #include "nsReadableUtils.h"
00051 #endif // DEBUG
00052 
00053 static NS_DEFINE_CID(kProxyObjectManagerCID, NS_PROXYEVENT_MANAGER_CID);
00054 
00055 NS_IMETHODIMP
00056 nsUnicharStreamLoader::Init(nsIChannel *aChannel,
00057                             nsIUnicharStreamLoaderObserver *aObserver,
00058                             nsISupports *aContext,
00059                             PRUint32 aSegmentSize)
00060 {
00061   NS_ENSURE_ARG_POINTER(aChannel);
00062   NS_ENSURE_ARG_POINTER(aObserver);
00063 
00064   if (aSegmentSize <= 0) {
00065     aSegmentSize = nsIUnicharStreamLoader::DEFAULT_SEGMENT_SIZE;
00066   }
00067   
00068   nsresult rv = aChannel->AsyncOpen(this, aContext);
00069 
00070   if (NS_FAILED(rv)) {
00071     // don't callback synchronously as it puts the caller
00072     // in a recursive situation and breaks the asynchronous
00073     // semantics of nsIStreamLoader
00074     nsresult rv2 = NS_OK;
00075     nsCOMPtr<nsIProxyObjectManager> pIProxyObjectManager = 
00076       do_GetService(kProxyObjectManagerCID, &rv2);
00077     if (NS_FAILED(rv2))
00078       return rv2;
00079 
00080     nsCOMPtr<nsIUnicharStreamLoaderObserver> pObserver;
00081     rv2 =
00082       pIProxyObjectManager->GetProxyForObject(NS_CURRENT_EVENTQ, 
00083                                               NS_GET_IID(nsIUnicharStreamLoaderObserver),
00084                                               aObserver, 
00085                                               PROXY_ASYNC | PROXY_ALWAYS,
00086                                               getter_AddRefs(pObserver));
00087     if (NS_FAILED(rv2))
00088       return rv2;
00089 
00090     rv = pObserver->OnStreamComplete(this, aContext, rv, nsnull);
00091   }
00092 
00093   mObserver = aObserver;
00094   mContext = aContext;
00095   mCharset.Truncate();
00096   mChannel = nsnull; // Leave this null till OnStopRequest
00097   mSegmentSize = aSegmentSize;
00098   return rv;
00099 }
00100 
00101 NS_METHOD
00102 nsUnicharStreamLoader::Create(nsISupports *aOuter,
00103                               REFNSIID aIID,
00104                               void **aResult)
00105 {
00106   if (aOuter) return NS_ERROR_NO_AGGREGATION;
00107 
00108   nsUnicharStreamLoader* it = new nsUnicharStreamLoader();
00109   if (it == nsnull)
00110     return NS_ERROR_OUT_OF_MEMORY;
00111   NS_ADDREF(it);
00112   nsresult rv = it->QueryInterface(aIID, aResult);
00113   NS_RELEASE(it);
00114   return rv;
00115 }
00116 
00117 NS_IMPL_ISUPPORTS3(nsUnicharStreamLoader, nsIUnicharStreamLoader,
00118                    nsIRequestObserver, nsIStreamListener)
00119 
00120 /* readonly attribute nsIChannel channel; */
00121 NS_IMETHODIMP 
00122 nsUnicharStreamLoader::GetChannel(nsIChannel **aChannel)
00123 {
00124   NS_IF_ADDREF(*aChannel = mChannel);
00125   return NS_OK;
00126 }
00127 
00128 /* readonly attribute nsACString charset */
00129 NS_IMETHODIMP
00130 nsUnicharStreamLoader::GetCharset(nsACString& aCharset)
00131 {
00132   aCharset = mCharset;
00133   return NS_OK;
00134 }
00135 
00136 /* nsIRequestObserver implementation */
00137 NS_IMETHODIMP
00138 nsUnicharStreamLoader::OnStartRequest(nsIRequest* request,
00139                                       nsISupports *ctxt)
00140 {
00141   return NS_OK;
00142 }
00143 
00144 NS_IMETHODIMP 
00145 nsUnicharStreamLoader::OnStopRequest(nsIRequest *request,
00146                                      nsISupports *ctxt,
00147                                      nsresult aStatus)
00148 {
00149   // if we trigger this assertion, then it means that the channel called
00150   // OnStopRequest before returning from AsyncOpen, which is totally
00151   // unexpected behavior.
00152   if (!mObserver) {
00153     NS_ERROR("No way we should not have an mObserver here!");
00154     return NS_ERROR_UNEXPECTED;
00155   }
00156 
00157   if (mInputStream) {
00158     nsresult rv;
00159     // We got some data at some point.  I guess we should tell our
00160     // observer about it or something....
00161 
00162     // Make sure mChannel points to the channel that we ended up with
00163     mChannel = do_QueryInterface(request);
00164 
00165     // Determine the charset
00166     PRUint32 readCount = 0;
00167     rv = mInputStream->ReadSegments(WriteSegmentFun,
00168                                     this,
00169                                     mSegmentSize, 
00170                                     &readCount);
00171     if (NS_FAILED(rv)) {
00172       rv = mObserver->OnStreamComplete(this, mContext, rv, nsnull);
00173       goto cleanup;
00174     }
00175 
00176     nsCOMPtr<nsIConverterInputStream> uin =
00177       do_CreateInstance("@mozilla.org/intl/converter-input-stream;1",
00178                         &rv);
00179     if (NS_FAILED(rv)) {
00180       rv = mObserver->OnStreamComplete(this, mContext, rv, nsnull);
00181       goto cleanup;
00182     }
00183 
00184     rv = uin->Init(mInputStream,
00185                    mCharset.get(),
00186                    mSegmentSize,
00187                    nsIConverterInputStream::DEFAULT_REPLACEMENT_CHARACTER);
00188     
00189     if (NS_FAILED(rv)) {
00190       rv = mObserver->OnStreamComplete(this, mContext, rv, nsnull);
00191       goto cleanup;
00192     }
00193     
00194     mObserver->OnStreamComplete(this, mContext, aStatus, uin);
00195 
00196   } else {
00197     // We never got any data, so just tell our observer that we are
00198     // done and give them no stream
00199     mObserver->OnStreamComplete(this, mContext, aStatus, nsnull);
00200   }
00201   
00202   // Clean up.
00203  cleanup:
00204   mObserver = nsnull;
00205   mChannel = nsnull;
00206   mContext = nsnull;
00207   mInputStream = nsnull;
00208   mOutputStream = nsnull;
00209   return NS_OK;
00210 }
00211 
00212 /* nsIStreamListener implementation */
00213 NS_METHOD
00214 nsUnicharStreamLoader::WriteSegmentFun(nsIInputStream *aInputStream,
00215                                        void *aClosure,
00216                                        const char *aSegment,
00217                                        PRUint32 aToOffset,
00218                                        PRUint32 aCount,
00219                                        PRUint32 *aWriteCount)
00220 {
00221   nsUnicharStreamLoader *self = (nsUnicharStreamLoader *) aClosure;
00222   if (self->mCharset.IsEmpty()) {
00223     // First time through.  Call our observer.
00224     NS_ASSERTION(self->mObserver, "This should never be possible");
00225 
00226     nsresult rv = self->mObserver->OnDetermineCharset(self,
00227                                                       self->mContext,
00228                                                       aSegment,
00229                                                       aCount,
00230                                                       self->mCharset);
00231     
00232     if (NS_FAILED(rv) || self->mCharset.IsEmpty()) {
00233       // The observer told us nothing useful
00234       self->mCharset.AssignLiteral("ISO-8859-1");
00235     }
00236 
00237     NS_ASSERTION(IsASCII(self->mCharset),
00238                  "Why is the charset name non-ascii?  Whose bright idea was that?");
00239   }
00240   // Don't consume any data
00241   *aWriteCount = 0;
00242   return NS_BASE_STREAM_WOULD_BLOCK;
00243 }
00244 
00245 
00246 NS_IMETHODIMP
00247 nsUnicharStreamLoader::OnDataAvailable(nsIRequest *aRequest,
00248                                        nsISupports *aContext,
00249                                        nsIInputStream *aInputStream, 
00250                                        PRUint32 aSourceOffset,
00251                                        PRUint32 aCount)
00252 {
00253   nsresult rv = NS_OK;
00254   if (!mInputStream) {
00255     // We are not initialized.  Time to set things up.
00256     NS_ASSERTION(!mOutputStream, "Why are we sorta-initialized?");
00257     rv = NS_NewPipe(getter_AddRefs(mInputStream),
00258                     getter_AddRefs(mOutputStream),
00259                     mSegmentSize,
00260                     PRUint32(-1),  // give me all the data you can!
00261                     PR_TRUE,  // non-blocking input
00262                     PR_TRUE); // non-blocking output
00263     if (NS_FAILED(rv))
00264       return rv;
00265   }
00266 
00267   PRUint32 writeCount = 0;
00268   do {
00269     rv = mOutputStream->WriteFrom(aInputStream, aCount, &writeCount);
00270     if (NS_FAILED(rv)) return rv;
00271     aCount -= writeCount;
00272   } while (aCount > 0);
00273 
00274   return NS_OK;
00275 }