Back to index

lightning-sunbird  0.9+nobinonly
nsStreamLoader.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsStreamLoader.h"
00039 #include "nsIInputStream.h"
00040 #include "nsIChannel.h"
00041 #include "nsProxiedService.h"
00042 
00043 static NS_DEFINE_CID(kProxyObjectManagerCID, NS_PROXYEVENT_MANAGER_CID);
00044 
00045 NS_IMETHODIMP
00046 nsStreamLoader::Init(nsIChannel *channel,
00047                      nsIStreamLoaderObserver* observer,
00048                      nsISupports* context)
00049 {
00050   NS_ENSURE_ARG_POINTER(channel);
00051   NS_ENSURE_ARG_POINTER(observer);
00052 
00053   nsresult rv = channel->AsyncOpen(this, context);
00054 
00055   if (NS_FAILED(rv) && observer) {
00056     // don't callback synchronously as it puts the caller
00057     // in a recursive situation and breaks the asynchronous
00058     // semantics of nsIStreamLoader
00059     nsresult rv2 = NS_OK;
00060     nsCOMPtr<nsIProxyObjectManager> pIProxyObjectManager = 
00061              do_GetService(kProxyObjectManagerCID, &rv2);
00062     if (NS_FAILED(rv2)) return rv2;
00063 
00064     nsCOMPtr<nsIStreamLoaderObserver> pObserver;
00065     rv2 = pIProxyObjectManager->GetProxyForObject(NS_CURRENT_EVENTQ, 
00066               NS_GET_IID(nsIStreamLoaderObserver), observer, 
00067               PROXY_ASYNC | PROXY_ALWAYS, getter_AddRefs(pObserver));
00068     if (NS_FAILED(rv2)) return rv2;
00069 
00070     rv = pObserver->OnStreamComplete(this, context, rv, 0, nsnull);
00071   }
00072 
00073   mObserver = observer;
00074   mContext  = context;
00075   return rv;
00076 }
00077 
00078 NS_METHOD
00079 nsStreamLoader::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00080 {
00081   if (aOuter) return NS_ERROR_NO_AGGREGATION;
00082 
00083   nsStreamLoader* it = new nsStreamLoader();
00084   if (it == nsnull)
00085     return NS_ERROR_OUT_OF_MEMORY;
00086   NS_ADDREF(it);
00087   nsresult rv = it->QueryInterface(aIID, aResult);
00088   NS_RELEASE(it);
00089   return rv;
00090 }
00091 
00092 NS_IMPL_ISUPPORTS3(nsStreamLoader, nsIStreamLoader,
00093                    nsIRequestObserver, nsIStreamListener)
00094 
00095 NS_IMETHODIMP 
00096 nsStreamLoader::GetNumBytesRead(PRUint32* aNumBytes)
00097 {
00098   *aNumBytes = mData.Length();
00099   return NS_OK;
00100 }
00101 
00102 /* readonly attribute nsIRequest request; */
00103 NS_IMETHODIMP 
00104 nsStreamLoader::GetRequest(nsIRequest **aRequest)
00105 {
00106     NS_IF_ADDREF(*aRequest = mRequest);
00107     return NS_OK;
00108 }
00109 
00110 NS_IMETHODIMP 
00111 nsStreamLoader::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
00112 {
00113   nsCOMPtr<nsIChannel> chan( do_QueryInterface(request) );
00114   if (chan) {
00115       PRInt32 contentLength = -1;
00116       chan->GetContentLength(&contentLength);
00117       if (contentLength >= 0) {
00118           // preallocate buffer
00119           mData.SetCapacity(contentLength + 1);
00120       }
00121   }
00122   return NS_OK;
00123 }
00124 
00125 NS_IMETHODIMP 
00126 nsStreamLoader::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
00127                               nsresult aStatus)
00128 {
00129   if (mObserver) {
00130     // provide nsIStreamLoader::request during call to OnStreamComplete
00131     mRequest = request;
00132     mObserver->OnStreamComplete(this, mContext, aStatus, 
00133                                 mData.Length(),
00134                                 NS_REINTERPRET_CAST(const PRUint8*,
00135                                                     mData.get()));
00136     // done.. cleanup
00137     mRequest = 0;
00138     mObserver = 0;
00139     mContext = 0;
00140   }
00141   return NS_OK;
00142 }
00143 
00144 NS_METHOD
00145 nsStreamLoader::WriteSegmentFun(nsIInputStream *inStr,
00146                                 void *closure,
00147                                 const char *fromSegment,
00148                                 PRUint32 toOffset,
00149                                 PRUint32 count,
00150                                 PRUint32 *writeCount)
00151 {
00152   nsStreamLoader *self = (nsStreamLoader *) closure;
00153 
00154   self->mData.Append(fromSegment, count);
00155   *writeCount = count;
00156 
00157   return NS_OK;
00158 }
00159 
00160 NS_IMETHODIMP 
00161 nsStreamLoader::OnDataAvailable(nsIRequest* request, nsISupports *ctxt, 
00162                                 nsIInputStream *inStr, 
00163                                 PRUint32 sourceOffset, PRUint32 count)
00164 {
00165   PRUint32 countRead;
00166   return inStr->ReadSegments(WriteSegmentFun, this, count, &countRead);
00167 }