From a7e9ed4e7fb76fb9e50a44f06f9d2db59076e98b Mon Sep 17 00:00:00 2001 From: "darin%meer.net" Date: Sat, 11 Mar 2006 20:32:47 +0000 Subject: [PATCH] fixes bug 328903 "Image requests need to cancel properly" r=biesi sr=bzbarsky git-svn-id: svn://10.0.0.236/trunk@192217 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/modules/libpr0n/src/imgLoader.cpp | 22 +++++++++-- mozilla/modules/libpr0n/src/imgRequest.cpp | 43 +++++++--------------- mozilla/modules/libpr0n/src/imgRequest.h | 7 ++-- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/mozilla/modules/libpr0n/src/imgLoader.cpp b/mozilla/modules/libpr0n/src/imgLoader.cpp index 9136997e632..310483b4086 100644 --- a/mozilla/modules/libpr0n/src/imgLoader.cpp +++ b/mozilla/modules/libpr0n/src/imgLoader.cpp @@ -502,7 +502,13 @@ NS_IMETHODIMP imgLoader::LoadImage(nsIURI *aURI, imgCache::Put(aURI, request, getter_AddRefs(entry)); } - request->Init(newChannel, entry, cacheId, aCX); + // Create a loadgroup for this new channel. This way if the channel + // is redirected, we'll have a way to cancel the resulting channel. + nsCOMPtr loadGroup = + do_CreateInstance(NS_LOADGROUP_CONTRACTID); + newChannel->SetLoadGroup(loadGroup); + + request->Init(aURI, loadGroup, entry, cacheId, aCX); // create the proxy listener ProxyListener *pl = new ProxyListener(NS_STATIC_CAST(nsIStreamListener *, request)); @@ -648,7 +654,12 @@ NS_IMETHODIMP imgLoader::LoadImageWithChannel(nsIChannel *channel, imgIDecoderOb imgCache::Put(uri, request, getter_AddRefs(entry)); - request->Init(channel, entry, activeQ.get(), aCX); + // XXX(darin): I'm not sure that using the original URI is correct here. + // Perhaps we should use the same URI that indexes the cache? Or, perhaps + // the cache should use the original URI? See bug 89419. + nsCOMPtr originalURI; + channel->GetOriginalURI(getter_AddRefs(originalURI)); + request->Init(originalURI, channel, entry, activeQ.get(), aCX); ProxyListener *pl = new ProxyListener(NS_STATIC_CAST(nsIStreamListener *, request)); if (!pl) { @@ -981,7 +992,12 @@ NS_IMETHODIMP imgCacheValidator::OnStartRequest(nsIRequest *aRequest, nsISupport imgCache::Put(uri, request, getter_AddRefs(entry)); - request->Init(channel, entry, activeQ.get(), mContext); + // XXX(darin): I'm not sure that using the original URI is correct here. + // Perhaps we should use the same URI that indexes the cache? Or, perhaps + // the cache should use the original URI? See bug 89419. + nsCOMPtr originalURI; + channel->GetOriginalURI(getter_AddRefs(originalURI)); + request->Init(originalURI, channel, entry, activeQ.get(), mContext); ProxyListener *pl = new ProxyListener(NS_STATIC_CAST(nsIStreamListener *, request)); if (!pl) { diff --git a/mozilla/modules/libpr0n/src/imgRequest.cpp b/mozilla/modules/libpr0n/src/imgRequest.cpp index 8a3d222bf32..bdd123abe9a 100644 --- a/mozilla/modules/libpr0n/src/imgRequest.cpp +++ b/mozilla/modules/libpr0n/src/imgRequest.cpp @@ -91,21 +91,24 @@ imgRequest::~imgRequest() /* destructor code */ } -nsresult imgRequest::Init(nsIChannel *aChannel, +nsresult imgRequest::Init(nsIURI *aURI, + nsIRequest *aRequest, nsICacheEntryDescriptor *aCacheEntry, void *aCacheId, void *aLoadId) { LOG_FUNC(gImgLog, "imgRequest::Init"); - NS_ASSERTION(!mImage, "imgRequest::Init -- Multiple calls to init"); - NS_ASSERTION(aChannel, "imgRequest::Init -- No channel"); + NS_ASSERTION(!mImage, "Multiple calls to init"); + NS_ASSERTION(aURI, "No uri"); + NS_ASSERTION(aRequest, "No request"); mProperties = do_CreateInstance("@mozilla.org/properties;1"); if (!mProperties) return NS_ERROR_OUT_OF_MEMORY; - mChannel = aChannel; + mURI = aURI; + mRequest = aRequest; /* set our loading flag to true here. Setting it here lets checks to see if the load is in progress @@ -172,7 +175,7 @@ nsresult imgRequest::RemoveProxy(imgRequestProxy *proxy, nsresult aStatus, PRBoo This way, if a proxy is destroyed without calling cancel on it, it won't leak and won't leave a bad pointer in mObservers. */ - if (mChannel && mLoading && NS_FAILED(aStatus)) { + if (mRequest && mLoading && NS_FAILED(aStatus)) { LOG_MSG(gImgLog, "imgRequest::RemoveProxy", "load in progress. canceling"); mImageStatus |= imgIRequest::STATUS_LOAD_PARTIAL; @@ -285,17 +288,14 @@ void imgRequest::Cancel(nsresult aStatus) RemoveFromCache(); - if (mChannel && mLoading) - mChannel->Cancel(aStatus); + if (mRequest && mLoading) + mRequest->Cancel(aStatus); } nsresult imgRequest::GetURI(nsIURI **aURI) { LOG_FUNC(gImgLog, "imgRequest::GetURI"); - if (mChannel) - return mChannel->GetOriginalURI(aURI); - if (mURI) { *aURI = mURI; NS_ADDREF(*aURI); @@ -334,7 +334,7 @@ PRBool imgRequest::HaveProxyWithObserver(imgRequestProxy* aProxyToIgnore) const PRInt32 imgRequest::Priority() const { PRInt32 priority = nsISupportsPriority::PRIORITY_NORMAL; - nsCOMPtr p = do_QueryInterface(mChannel); + nsCOMPtr p = do_QueryInterface(mRequest); if (p) p->GetPriority(&priority); return priority; @@ -352,7 +352,7 @@ void imgRequest::AdjustPriority(imgRequestProxy *proxy, PRInt32 delta) if (mObservers[0] != proxy) return; - nsCOMPtr p = do_QueryInterface(mChannel); + nsCOMPtr p = do_QueryInterface(mRequest); if (p) p->AdjustPriority(delta); } @@ -610,21 +610,7 @@ NS_IMETHODIMP imgRequest::OnStartRequest(nsIRequest *aRequest, nsISupports *ctxt NS_ASSERTION(!mDecoder, "imgRequest::OnStartRequest -- we already have a decoder"); - /* if mChannel isn't set here, use aRequest. - Having mChannel set is important for Canceling the load, and since we set - mChannel to null in OnStopRequest. Since with multipart/x-mixed-replace, you - can get multiple OnStartRequests, we need to reinstance mChannel so that when/if - Cancel() gets called, we have a channel to cancel and we don't leave the channel - open forever. - */ nsCOMPtr mpchan(do_QueryInterface(aRequest)); - if (!mChannel) { - if (mpchan) - mpchan->GetBaseChannel(getter_AddRefs(mChannel)); - else - mChannel = do_QueryInterface(aRequest); - } - if (mpchan) mIsMultiPartChannel = PR_TRUE; @@ -722,10 +708,7 @@ NS_IMETHODIMP imgRequest::OnStopRequest(nsIRequest *aRequest, nsISupports *ctxt, /* set our processing flag to false */ mProcessing = PR_FALSE; - if (mChannel) { - mChannel->GetOriginalURI(getter_AddRefs(mURI)); - mChannel = nsnull; // we no longer need the channel - } + mRequest = nsnull; // we no longer need the request // If mImage is still null, we didn't properly load the image. if (NS_FAILED(status) || !mImage) { diff --git a/mozilla/modules/libpr0n/src/imgRequest.h b/mozilla/modules/libpr0n/src/imgRequest.h index c42c4a79928..5474612de89 100644 --- a/mozilla/modules/libpr0n/src/imgRequest.h +++ b/mozilla/modules/libpr0n/src/imgRequest.h @@ -47,7 +47,7 @@ #include "imgIDecoderObserver.h" #include "nsICacheEntryDescriptor.h" -#include "nsIChannel.h" +#include "nsIRequest.h" #include "nsIProperties.h" #include "nsIStreamListener.h" #include "nsIURI.h" @@ -80,7 +80,8 @@ public: NS_DECL_ISUPPORTS - nsresult Init(nsIChannel *aChannel, + nsresult Init(nsIURI *aURI, + nsIRequest *aRequest, nsICacheEntryDescriptor *aCacheEntry, void *aCacheId, void *aLoadId); @@ -138,7 +139,7 @@ public: NS_DECL_NSIREQUESTOBSERVER private: - nsCOMPtr mChannel; + nsCOMPtr mRequest; nsCOMPtr mURI; nsCOMPtr mImage; nsCOMPtr mDecoder;