From 2d7539378cf11e7511bc5880b4b22983a248452d Mon Sep 17 00:00:00 2001 From: "bzbarsky%mit.edu" Date: Tue, 24 Oct 2006 21:47:40 +0000 Subject: [PATCH] Make nsIStreamLoader and nsIUnicharStreamLoader be stream listeners. Bug 281153, patch by Son Le , r=biesi, sr=darin git-svn-id: svn://10.0.0.236/trunk@214092 18797224-902f-48f8-a5cc-f745e15eee43 --- .../camino/src/browser/RemoteDataProvider.mm | 6 +++- mozilla/content/base/src/nsDocument.h | 1 + mozilla/content/base/src/nsScriptLoader.cpp | 4 ++- mozilla/layout/style/nsCSSLoader.cpp | 5 ++- .../netwerk/base/public/nsIStreamLoader.idl | 31 +++++++----------- .../base/public/nsIUnicharStreamLoader.idl | 32 +++++++++---------- mozilla/netwerk/base/public/nsNetUtil.h | 14 ++++---- mozilla/netwerk/base/src/nsPACMan.cpp | 24 +++++++------- mozilla/netwerk/base/src/nsStreamLoader.cpp | 29 +++++++---------- mozilla/netwerk/base/src/nsStreamLoader.h | 4 +-- .../base/src/nsUnicharStreamLoader.cpp | 14 ++------ .../netwerk/base/src/nsUnicharStreamLoader.h | 4 +-- mozilla/netwerk/test/TestStreamLoader.cpp | 6 +++- .../manager/ssl/src/nsNSSCallbacks.cpp | 7 ++-- 14 files changed, 84 insertions(+), 97 deletions(-) diff --git a/mozilla/camino/src/browser/RemoteDataProvider.mm b/mozilla/camino/src/browser/RemoteDataProvider.mm index 32d29dad466..440be9cdb4e 100644 --- a/mozilla/camino/src/browser/RemoteDataProvider.mm +++ b/mozilla/camino/src/browser/RemoteDataProvider.mm @@ -221,7 +221,11 @@ nsresult RemoteURILoadManager::RequestURILoad(const nsAString& inURI, id streamLoader; - rv = NS_NewStreamLoader(getter_AddRefs(streamLoader), channel, this, loaderContext) ; // , mLoadGroup, nsnull, loadFlags); + rv = NS_NewStreamLoader(getter_AddRefs(streamLoader), this); + + if (NS_SUCCEEDED(rv)) + rv = channel->AsyncOpen(streamLoader, loaderContext); + if (NS_FAILED(rv)) { #if DEBUG diff --git a/mozilla/content/base/src/nsDocument.h b/mozilla/content/base/src/nsDocument.h index 635885766c7..65b86325348 100644 --- a/mozilla/content/base/src/nsDocument.h +++ b/mozilla/content/base/src/nsDocument.h @@ -90,6 +90,7 @@ #include "nsILoadGroup.h" #include "nsTObserverArray.h" #include "nsStubMutationObserver.h" +#include "nsIChannel.h" // Put these here so all document impls get them automatically #include "nsHTMLStyleSheet.h" diff --git a/mozilla/content/base/src/nsScriptLoader.cpp b/mozilla/content/base/src/nsScriptLoader.cpp index f11b7cfe1ac..f3aa461e085 100644 --- a/mozilla/content/base/src/nsScriptLoader.cpp +++ b/mozilla/content/base/src/nsScriptLoader.cpp @@ -607,7 +607,9 @@ nsScriptLoader::DoProcessScriptElement(nsIScriptElement *aElement, PR_FALSE); httpChannel->SetReferrer(mDocument->GetDocumentURI()); } - rv = NS_NewStreamLoader(getter_AddRefs(loader), channel, this, request); + rv = NS_NewStreamLoader(getter_AddRefs(loader), this); + if (NS_SUCCEEDED(rv)) + rv = channel->AsyncOpen(loader, request); } if (NS_FAILED(rv)) { mPendingRequests.RemoveObject(request); diff --git a/mozilla/layout/style/nsCSSLoader.cpp b/mozilla/layout/style/nsCSSLoader.cpp index c8c1a5b2d44..dc056f08a66 100644 --- a/mozilla/layout/style/nsCSSLoader.cpp +++ b/mozilla/layout/style/nsCSSLoader.cpp @@ -1360,7 +1360,10 @@ CSSLoaderImpl::LoadSheet(SheetLoadData* aLoadData, StyleSheetState aSheetState) // which owns us nsCOMPtr streamLoader; rv = NS_NewUnicharStreamLoader(getter_AddRefs(streamLoader), - channel, aLoadData); + aLoadData); + + if (NS_SUCCEEDED(rv)) + rv = channel->AsyncOpen(streamLoader, nsnull); #ifdef DEBUG mSyncCallback = PR_FALSE; diff --git a/mozilla/netwerk/base/public/nsIStreamLoader.idl b/mozilla/netwerk/base/public/nsIStreamLoader.idl index 07ea734820b..d0a1e7b7c87 100644 --- a/mozilla/netwerk/base/public/nsIStreamLoader.idl +++ b/mozilla/netwerk/base/public/nsIStreamLoader.idl @@ -35,14 +35,10 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsISupports.idl" -#include "nsIChannel.idl" +#include "nsIStreamListener.idl" interface nsIRequest; -interface nsIURI; -interface nsILoadGroup; interface nsIStreamLoader; -interface nsIInterfaceRequestor; [scriptable, uuid(359F7990-D4E9-11d3-A1A5-0050041CAF44)] interface nsIStreamLoaderObserver : nsISupports @@ -51,7 +47,7 @@ interface nsIStreamLoaderObserver : nsISupports * Called when the entire stream has been loaded. * * @param loader the stream loader that loaded the stream. - * @param context the aContext parameter passed to the loader's init method + * @param ctxt the context parameter of the underlying channel * @param status the status of the underlying channel * @param resultLength the length of the data loaded * @param result the data @@ -70,29 +66,24 @@ interface nsIStreamLoaderObserver : nsISupports /** * Asynchronously loads a channel into a memory buffer. * + * To use this interface, first call init() with a nsIStreamLoaderObserver + * that will be notified when the data has been loaded. Then call asyncOpen() + * on the channel with the nsIStreamLoader as the listener. The context + * argument in the asyncOpen() call will be passed to the onStreamComplete() + * callback. + * * XXX define behaviour for sizes >4 GB */ -[scriptable, uuid(31d37360-8e5a-11d3-93ad-00104ba0fd40)] -interface nsIStreamLoader : nsISupports +[scriptable, uuid(8ea7e890-8211-11d9-8bde-f66bad1e3f3a)] +interface nsIStreamLoader : nsIStreamListener { /** * Initialize this stream loader, and start loading the data. * - * @param aChannel - * A Channel to load data from. This must not be asyncOpen'd yet! * @param aObserver * An observer that will be notified when the data is complete. - * @param aContext - * May be null. Will be passed to the observer. - * - * @note Failure to open the channel will cause init() to throw. A - * successful call to init() means that onStreamComplete is - * guaranteed to be called on aObserver asynchronously (i.e. after - * init() returns). */ - void init(in nsIChannel aChannel, - in nsIStreamLoaderObserver aObserver, - in nsISupports aContext); + void init(in nsIStreamLoaderObserver aObserver); /** * Gets the number of bytes read so far. diff --git a/mozilla/netwerk/base/public/nsIUnicharStreamLoader.idl b/mozilla/netwerk/base/public/nsIUnicharStreamLoader.idl index 63d3e175aa2..6de2465bd5b 100644 --- a/mozilla/netwerk/base/public/nsIUnicharStreamLoader.idl +++ b/mozilla/netwerk/base/public/nsIUnicharStreamLoader.idl @@ -36,20 +36,20 @@ * * ***** END LICENSE BLOCK ***** */ -#include "nsISupports.idl" +#include "nsIStreamListener.idl" interface nsIUnicharInputStream; interface nsIUnicharStreamLoader; interface nsIChannel; -[scriptable, uuid(e06e8b08-8cdd-4503-a0a0-6f3b943602af)] +[scriptable, uuid(6bb3e55b-69c0-4fc9-87e5-bed780d997ce)] interface nsIUnicharStreamLoaderObserver : nsISupports { /** * Called when the first full segment of data if available. * * @param aLoader the unichar stream loader - * @param aContext the aContext parameter passed to the loader's init method + * @param aContext the context parameter of the underlying channel * @param aFirstSegment the raw bytes of the first full data segment * @param aLength the length of aFirstSegment * @@ -64,7 +64,7 @@ interface nsIUnicharStreamLoaderObserver : nsISupports * Called when the entire stream has been loaded. * * @param aLoader the unichar stream loader - * @param aContext the aContext parameter passed to the loader's init method + * @param aContext the context parameter of the underlying channel * @param aStatus the status of the underlying channel * @param aUnicharData the unichar input stream containing the data. This * can be null in some failure conditions. @@ -79,29 +79,29 @@ interface nsIUnicharStreamLoaderObserver : nsISupports in nsIUnicharInputStream aUnicharData); }; -[scriptable, uuid(8a3eca16-167e-443d-9485-7e84ed822e95)] -interface nsIUnicharStreamLoader : nsISupports +/** + * Asynchronously load a channel, converting the data to UTF-16. + * + * To use this interface, first call init() with a + * nsIUnicharStreamLoaderObserver that will be notified when the data has been + * loaded. Then call asyncOpen() on the channel with the nsIUnicharStreamLoader + * as the listener. The context argument in the asyncOpen() call will be + * passed to the onStreamComplete() callback. + */ +[scriptable, uuid(9037f476-7c08-4729-b690-3e425269802b)] +interface nsIUnicharStreamLoader : nsIStreamListener { const unsigned long DEFAULT_SEGMENT_SIZE = 4096; /** * Initializes the unichar stream loader * - * @param aChannel the channel to read data from. This should _not_ be - * opened; the loader will open the channel itself. * @param aObserver the observer to notify when a charset is needed and when * the load is complete - * @param aContext an opaque context pointer * @param aSegmentSize the size of the segments to use for the data, in bytes - * - * @note Failure to open the channel will cause init() to throw. A - * successful call to init() means that onStreamComplete is guaranteed - * to be called on aObserver asynchronously (i.e. after init() returns). */ - void init(in nsIChannel aChannel, - in nsIUnicharStreamLoaderObserver aObserver, - in nsISupports aContext, + void init(in nsIUnicharStreamLoaderObserver aObserver, in unsigned long aSegmentSize); /** diff --git a/mozilla/netwerk/base/public/nsNetUtil.h b/mozilla/netwerk/base/public/nsNetUtil.h index 123603141fd..e073b356dfc 100644 --- a/mozilla/netwerk/base/public/nsNetUtil.h +++ b/mozilla/netwerk/base/public/nsNetUtil.h @@ -465,15 +465,13 @@ NS_NewDownloader(nsIStreamListener **result, inline nsresult NS_NewStreamLoader(nsIStreamLoader **result, - nsIChannel *channel, - nsIStreamLoaderObserver *observer, - nsISupports *context) + nsIStreamLoaderObserver *observer) { nsresult rv; nsCOMPtr loader = do_CreateInstance(NS_STREAMLOADER_CONTRACTID, &rv); if (NS_SUCCEEDED(rv)) { - rv = loader->Init(channel, observer, context); + rv = loader->Init(observer); if (NS_SUCCEEDED(rv)) { *result = nsnull; loader.swap(*result); @@ -504,23 +502,23 @@ NS_NewStreamLoader(nsIStreamLoader **result, nsCOMPtr httpChannel(do_QueryInterface(channel)); if (httpChannel) httpChannel->SetReferrer(referrer); - rv = NS_NewStreamLoader(result, channel, observer, context); + rv = NS_NewStreamLoader(result, observer); + if (NS_SUCCEEDED(rv)) + rv = channel->AsyncOpen(*result, context); } return rv; } inline nsresult NS_NewUnicharStreamLoader(nsIUnicharStreamLoader **result, - nsIChannel *channel, nsIUnicharStreamLoaderObserver *observer, - nsISupports *context = nsnull, PRUint32 segmentSize = nsIUnicharStreamLoader::DEFAULT_SEGMENT_SIZE) { nsresult rv; nsCOMPtr loader = do_CreateInstance(NS_UNICHARSTREAMLOADER_CONTRACTID, &rv); if (NS_SUCCEEDED(rv)) { - rv = loader->Init(channel, observer, context, segmentSize); + rv = loader->Init(observer, segmentSize); if (NS_SUCCEEDED(rv)) { *result = nsnull; loader.swap(*result); diff --git a/mozilla/netwerk/base/src/nsPACMan.cpp b/mozilla/netwerk/base/src/nsPACMan.cpp index 3a30b28165a..8b6261965f1 100644 --- a/mozilla/netwerk/base/src/nsPACMan.cpp +++ b/mozilla/netwerk/base/src/nsPACMan.cpp @@ -298,19 +298,21 @@ nsPACMan::StartLoading() return; } - // Always hit the origin server when loading PAC. - nsCOMPtr ios = do_GetIOService(); - if (ios) { - nsCOMPtr channel; + if (NS_SUCCEEDED(mLoader->Init(this))) { + // Always hit the origin server when loading PAC. + nsCOMPtr ios = do_GetIOService(); + if (ios) { + nsCOMPtr channel; - // NOTE: This results in GetProxyForURI being called - ios->NewChannelFromURI(mPACURI, getter_AddRefs(channel)); + // NOTE: This results in GetProxyForURI being called + ios->NewChannelFromURI(mPACURI, getter_AddRefs(channel)); - if (channel) { - channel->SetLoadFlags(nsIRequest::LOAD_BYPASS_CACHE); - channel->SetNotificationCallbacks(this); - if (NS_SUCCEEDED(mLoader->Init(channel, this, nsnull))) - return; + if (channel) { + channel->SetLoadFlags(nsIRequest::LOAD_BYPASS_CACHE); + channel->SetNotificationCallbacks(this); + if (NS_SUCCEEDED(channel->AsyncOpen(mLoader, nsnull))) + return; + } } } diff --git a/mozilla/netwerk/base/src/nsStreamLoader.cpp b/mozilla/netwerk/base/src/nsStreamLoader.cpp index 9c8b8f86d56..1954e72c0be 100644 --- a/mozilla/netwerk/base/src/nsStreamLoader.cpp +++ b/mozilla/netwerk/base/src/nsStreamLoader.cpp @@ -40,19 +40,11 @@ #include "nsIChannel.h" NS_IMETHODIMP -nsStreamLoader::Init(nsIChannel *channel, - nsIStreamLoaderObserver* observer, - nsISupports* context) +nsStreamLoader::Init(nsIStreamLoaderObserver* observer) { - NS_ENSURE_ARG_POINTER(channel); NS_ENSURE_ARG_POINTER(observer); - - nsresult rv = channel->AsyncOpen(this, context); - NS_ENSURE_SUCCESS(rv, rv); - mObserver = observer; - mContext = context; - return rv; + return NS_OK; } NS_METHOD @@ -83,8 +75,8 @@ nsStreamLoader::GetNumBytesRead(PRUint32* aNumBytes) NS_IMETHODIMP nsStreamLoader::GetRequest(nsIRequest **aRequest) { - NS_IF_ADDREF(*aRequest = mRequest); - return NS_OK; + NS_IF_ADDREF(*aRequest = mRequest); + return NS_OK; } NS_IMETHODIMP @@ -92,13 +84,14 @@ nsStreamLoader::OnStartRequest(nsIRequest* request, nsISupports *ctxt) { nsCOMPtr chan( do_QueryInterface(request) ); if (chan) { - PRInt32 contentLength = -1; - chan->GetContentLength(&contentLength); - if (contentLength >= 0) { - // preallocate buffer - mData.SetCapacity(contentLength + 1); - } + PRInt32 contentLength = -1; + chan->GetContentLength(&contentLength); + if (contentLength >= 0) { + // preallocate buffer + mData.SetCapacity(contentLength + 1); + } } + mContext = ctxt; return NS_OK; } diff --git a/mozilla/netwerk/base/src/nsStreamLoader.h b/mozilla/netwerk/base/src/nsStreamLoader.h index 7a99bc7b69d..6edf656f725 100644 --- a/mozilla/netwerk/base/src/nsStreamLoader.h +++ b/mozilla/netwerk/base/src/nsStreamLoader.h @@ -39,12 +39,10 @@ #define nsStreamLoader_h__ #include "nsIStreamLoader.h" -#include "nsIStreamListener.h" #include "nsCOMPtr.h" #include "nsString.h" -class nsStreamLoader : public nsIStreamLoader, - public nsIStreamListener +class nsStreamLoader : public nsIStreamLoader { public: NS_DECL_ISUPPORTS diff --git a/mozilla/netwerk/base/src/nsUnicharStreamLoader.cpp b/mozilla/netwerk/base/src/nsUnicharStreamLoader.cpp index f34555271e3..8c27c02bcd7 100644 --- a/mozilla/netwerk/base/src/nsUnicharStreamLoader.cpp +++ b/mozilla/netwerk/base/src/nsUnicharStreamLoader.cpp @@ -37,8 +37,6 @@ * ***** END LICENSE BLOCK ***** */ #include "nsUnicharStreamLoader.h" -#include "nsIPipe.h" -#include "nsIChannel.h" #include "nsNetUtil.h" #include "nsProxiedService.h" #include "nsIChannel.h" @@ -51,27 +49,20 @@ #endif // DEBUG NS_IMETHODIMP -nsUnicharStreamLoader::Init(nsIChannel *aChannel, - nsIUnicharStreamLoaderObserver *aObserver, - nsISupports *aContext, +nsUnicharStreamLoader::Init(nsIUnicharStreamLoaderObserver *aObserver, PRUint32 aSegmentSize) { - NS_ENSURE_ARG_POINTER(aChannel); NS_ENSURE_ARG_POINTER(aObserver); if (aSegmentSize <= 0) { aSegmentSize = nsIUnicharStreamLoader::DEFAULT_SEGMENT_SIZE; } - nsresult rv = aChannel->AsyncOpen(this, aContext); - NS_ENSURE_SUCCESS(rv, rv); - mObserver = aObserver; - mContext = aContext; mCharset.Truncate(); mChannel = nsnull; // Leave this null till OnStopRequest mSegmentSize = aSegmentSize; - return rv; + return NS_OK; } NS_METHOD @@ -114,6 +105,7 @@ NS_IMETHODIMP nsUnicharStreamLoader::OnStartRequest(nsIRequest* request, nsISupports *ctxt) { + mContext = ctxt; return NS_OK; } diff --git a/mozilla/netwerk/base/src/nsUnicharStreamLoader.h b/mozilla/netwerk/base/src/nsUnicharStreamLoader.h index 24ee64cd1e7..ca32fd1d1fc 100644 --- a/mozilla/netwerk/base/src/nsUnicharStreamLoader.h +++ b/mozilla/netwerk/base/src/nsUnicharStreamLoader.h @@ -40,15 +40,13 @@ #define nsUnicharStreamLoader_h__ #include "nsIUnicharStreamLoader.h" -#include "nsIStreamListener.h" #include "nsCOMPtr.h" #include "nsIChannel.h" #include "nsString.h" #include "nsIInputStream.h" #include "nsIOutputStream.h" -class nsUnicharStreamLoader : public nsIUnicharStreamLoader, - public nsIStreamListener +class nsUnicharStreamLoader : public nsIUnicharStreamLoader { public: NS_DECL_ISUPPORTS diff --git a/mozilla/netwerk/test/TestStreamLoader.cpp b/mozilla/netwerk/test/TestStreamLoader.cpp index 55e56219aa5..80aeb3e8952 100644 --- a/mozilla/netwerk/test/TestStreamLoader.cpp +++ b/mozilla/netwerk/test/TestStreamLoader.cpp @@ -72,7 +72,11 @@ int main(int argc, char **argv) return -1; nsCOMPtr loader; - rv = NS_NewStreamLoader(getter_AddRefs(loader), chan, observer, nsnull); + rv = NS_NewStreamLoader(getter_AddRefs(loader), observer); + if (NS_FAILED(rv)) + return -1; + + rv = chan->AsyncOpen(loader, nsnull); if (NS_FAILED(rv)) return -1; diff --git a/mozilla/security/manager/ssl/src/nsNSSCallbacks.cpp b/mozilla/security/manager/ssl/src/nsNSSCallbacks.cpp index a2194c8aed0..e751a00d39c 100644 --- a/mozilla/security/manager/ssl/src/nsNSSCallbacks.cpp +++ b/mozilla/security/manager/ssl/src/nsNSSCallbacks.cpp @@ -148,9 +148,10 @@ nsHTTPDownloadEvent::Run() mListener->mResponsibleForDoneSignal = PR_TRUE; rv = NS_NewStreamLoader(getter_AddRefs(mListener->mLoader), - hchan, - mListener, - nsnull); + mListener); + + if (NS_SUCCEEDED(rv)) + rv = hchan->AsyncOpen(mListener->mLoader, nsnull); if (NS_FAILED(rv)) { mListener->mResponsibleForDoneSignal = PR_FALSE;