diff --git a/modules/libpr0n/src/ImageCache.cpp b/modules/libpr0n/src/ImageCache.cpp index 8fc2be74e36..1bf38be2c76 100644 --- a/modules/libpr0n/src/ImageCache.cpp +++ b/modules/libpr0n/src/ImageCache.cpp @@ -156,4 +156,5 @@ PRBool ImageCache::Remove(nsIURI *aKey) return PR_TRUE; } + #endif /* MOZ_NEW_CACHE */ diff --git a/modules/libpr0n/src/imgLoader.cpp b/modules/libpr0n/src/imgLoader.cpp index d6ff7608c2f..db9e81a00ea 100644 --- a/modules/libpr0n/src/imgLoader.cpp +++ b/modules/libpr0n/src/imgLoader.cpp @@ -42,36 +42,22 @@ #include "nsCOMPtr.h" -#ifdef LOADER_THREADSAFE -#include "nsAutoLock.h" -#endif - #include "ImageLogging.h" static NS_DEFINE_CID(kImageRequestCID, NS_IMGREQUEST_CID); static NS_DEFINE_CID(kImageRequestProxyCID, NS_IMGREQUESTPROXY_CID); -#ifdef LOADER_THREADSAFE -NS_IMPL_THREADSAFE_ISUPPORTS1(imgLoader, imgILoader) -#else NS_IMPL_ISUPPORTS1(imgLoader, imgILoader) -#endif imgLoader::imgLoader() { NS_INIT_ISUPPORTS(); /* member initializers and constructor code */ -#ifdef LOADER_THREADSAFE - mLock = PR_NewLock(); -#endif } imgLoader::~imgLoader() { /* destructor code */ -#ifdef LOADER_THREADSAFE - PR_DestroyLock(mLock); -#endif } /* imgIRequest loadImage (in nsIURI uri, in nsILoadGroup aLoadGroup, in imgIDecoderObserver aObserver, in nsISupports cx); */ @@ -124,9 +110,7 @@ NS_IMETHODIMP imgLoader::LoadImage(nsIURI *aURI, nsILoadGroup *aLoadGroup, imgID #endif if (!request) { -#ifdef LOADER_THREADSAFE - nsAutoLock lock(mLock); // lock when we are adding things to the cache -#endif + /* no request from the cache. do a new load */ LOG_SCOPE(gImgLog, "imgLoader::LoadImage |cache miss|"); nsCOMPtr ioserv(do_GetService("@mozilla.org/network/io-service;1")); @@ -149,7 +133,11 @@ NS_IMETHODIMP imgLoader::LoadImage(nsIURI *aURI, nsILoadGroup *aLoadGroup, imgID PR_LOG(gImgLog, PR_LOG_DEBUG, ("[this=%p] imgLoader::LoadImage -- Created new imgRequest [request=%p]\n", this, request)); - request->Init(newChannel); +#ifdef MOZ_NEW_CACHE + request->Init(newChannel, entry); +#else + request->Init(newChannel, nsnull); +#endif #ifdef MOZ_NEW_CACHE ImageCache::Put(aURI, request, getter_AddRefs(entry)); @@ -161,6 +149,7 @@ NS_IMETHODIMP imgLoader::LoadImage(nsIURI *aURI, nsILoadGroup *aLoadGroup, imgID newChannel->AsyncOpen(NS_STATIC_CAST(nsIStreamListener *, request), nsnull); } else { + /* request found in cache. use it */ PR_LOG(gImgLog, PR_LOG_DEBUG, ("[this=%p] imgLoader::LoadImage |cache hit| [request=%p]\n", this, request)); @@ -171,11 +160,8 @@ NS_IMETHODIMP imgLoader::LoadImage(nsIURI *aURI, nsILoadGroup *aLoadGroup, imgID nsCOMPtr proxyRequest(do_CreateInstance(kImageRequestProxyCID)); // init adds itself to imgRequest's list of observers -#ifdef MOZ_NEW_CACHE - NS_REINTERPRET_CAST(imgRequestProxy*, proxyRequest.get())->Init(request, aLoadGroup, aObserver, cx, entry); -#else - NS_REINTERPRET_CAST(imgRequestProxy*, proxyRequest.get())->Init(request, aLoadGroup, aObserver, cx, nsnull); -#endif + NS_REINTERPRET_CAST(imgRequestProxy*, proxyRequest.get())->Init(request, aLoadGroup, aObserver, cx); + NS_RELEASE(request); *_retval = proxyRequest; @@ -208,16 +194,18 @@ NS_IMETHODIMP imgLoader::LoadImageWithChannel(nsIChannel *channel, imgIDecoderOb *listener = nsnull; // give them back a null nsIStreamListener } else { -#ifdef LOADER_THREADSAFE - nsAutoLock lock(mLock); // lock when we are adding things to the cache -#endif - nsCOMPtr req(do_CreateInstance(kImageRequestCID)); request = NS_REINTERPRET_CAST(imgRequest*, req.get()); NS_ADDREF(request); - request->Init(channel); + + +#ifdef MOZ_NEW_CACHE + request->Init(channel, entry); +#else + request->Init(channel, nsnull); +#endif #ifdef MOZ_NEW_CACHE ImageCache::Put(uri, request, getter_AddRefs(entry)); @@ -230,11 +218,8 @@ NS_IMETHODIMP imgLoader::LoadImageWithChannel(nsIChannel *channel, imgIDecoderOb nsCOMPtr proxyRequest(do_CreateInstance(kImageRequestProxyCID)); // init adds itself to imgRequest's list of observers -#ifdef MOZ_NEW_CACHE - NS_REINTERPRET_CAST(imgRequestProxy*, proxyRequest.get())->Init(request, nsnull, aObserver, cx, entry); -#else - NS_REINTERPRET_CAST(imgRequestProxy*, proxyRequest.get())->Init(request, nsnull, aObserver, cx, nsnull); -#endif + NS_REINTERPRET_CAST(imgRequestProxy*, proxyRequest.get())->Init(request, nsnull, aObserver, cx); + NS_RELEASE(request); *_retval = proxyRequest; diff --git a/modules/libpr0n/src/imgLoader.h b/modules/libpr0n/src/imgLoader.h index bf079805383..24685bff84a 100644 --- a/modules/libpr0n/src/imgLoader.h +++ b/modules/libpr0n/src/imgLoader.h @@ -21,8 +21,6 @@ * Stuart Parmenter */ -//#define LOADER_THREADSAFE 1 - #include "imgILoader.h" #ifdef LOADER_THREADSAFE @@ -47,7 +45,4 @@ public: virtual ~imgLoader(); private: -#ifdef LOADER_THREADSAFE - PRLock *mLock; -#endif }; diff --git a/modules/libpr0n/src/imgRequest.cpp b/modules/libpr0n/src/imgRequest.cpp index 9d086b19bae..ae199e2d062 100644 --- a/modules/libpr0n/src/imgRequest.cpp +++ b/modules/libpr0n/src/imgRequest.cpp @@ -62,7 +62,7 @@ imgRequest::~imgRequest() } -nsresult imgRequest::Init(nsIChannel *aChannel) +nsresult imgRequest::Init(nsIChannel *aChannel, nsICacheEntryDescriptor *aCacheEntry) { // XXX we should save off the thread we are getting called on here so that we can proxy all calls to mDecoder to it. @@ -74,6 +74,10 @@ nsresult imgRequest::Init(nsIChannel *aChannel) mChannel = aChannel; +#ifdef MOZ_NEW_CACHE + mCacheEntry = aCacheEntry; +#endif + // XXX do not init the image here. this has to be done from the image decoder. mImage = do_CreateInstance("@mozilla.org/image/container;1"); @@ -101,12 +105,12 @@ nsresult imgRequest::AddObserver(imgIDecoderObserver *observer) if (mObservers.Count() == 1) { PRUint32 nframes; mImage->GetNumFrames(&nframes); - //if (nframes > 1) { + //if (nframes > 1) { PR_LOG(gImgLog, PR_LOG_DEBUG, ("[this=%p] imgRequest::AddObserver -- starting animation\n", this)); mImage->StartAnimation(); - //} + //} } if (mState & onStopRequest) { @@ -152,19 +156,17 @@ PRBool imgRequest::RemoveFromCache() { LOG_SCOPE(gImgLog, "imgRequest::RemoveFromCache"); - if (mChannel) { - mChannel->GetOriginalURI(getter_AddRefs(mURI)); - } +#ifdef MOZ_NEW_CACHE -#if defined(PR_LOGGING) - nsXPIDLCString spec; - mURI->GetSpec(getter_Copies(spec)); + if (mCacheEntry) + mCacheEntry->Doom(); + else + NS_WARNING("imgRequest::RemoveFromCache -- no entry!"); - PR_LOG(gImgLog, PR_LOG_DEBUG, - ("[this=%p] imgRequest::RemoveFromCache -- removing %s from cache\n", this, spec.get())); + mCacheEntry = nsnull; #endif - return ImageCache::Remove(mURI); + return PR_TRUE; } @@ -361,6 +363,23 @@ NS_IMETHODIMP imgRequest::OnStopFrame(imgIRequest *request, nsISupports *cx, gfx PRInt32 i = -1; PRInt32 count = mObservers.Count(); +#ifdef MOZ_NEW_CACHE + if (mCacheEntry) { + PRUint32 cacheSize = 0; + + mCacheEntry->GetDataSize(&cacheSize); + + PRUint32 imageSize = 0; + PRUint32 alphaSize = 0; + + frame->GetImageDataLength(&imageSize); + frame->GetAlphaDataLength(&alphaSize); + + mCacheEntry->SetDataSize(cacheSize + imageSize + alphaSize); + printf("%p size is %d\n", this, cacheSize + imageSize + alphaSize); + } +#endif + while (++i < count) { imgIDecoderObserver *ob = NS_STATIC_CAST(imgIDecoderObserver*, mObservers[i]); if (ob) ob->OnStopFrame(request, cx, frame); @@ -463,14 +482,29 @@ NS_IMETHODIMP imgRequest::OnStartRequest(nsIRequest *aRequest, nsISupports *ctxt return NS_BINDING_ABORTED; } - /* get the http expires date */ - nsCOMPtr expiresAtom(dont_AddRef(NS_NewAtom(NS_LITERAL_STRING("Expires")))); - nsXPIDLCString expires; - httpChannel->GetResponseHeader(expiresAtom, getter_Copies(expires)); - if (expires.get()) { - printf("%s\n", expires.get()); + } + + /* get the expires info */ +#if defined(MOZ_NEW_CACHE) && defined(HAVE_CACHING_CHANNEL) + if (mCacheEntry) { + nsCOMPtr cacheChannel(do_QueryInterface(chan)); + if (cacheChannel) { + nsCOMPtr cacheToken; + cacheChannel->GetCacheToken(getter_AddRefs(cacheToken)); + if (cacheToken) { + nsCOMPtr entryDesc(do_QueryInterface(cacheToken)); + if (entryDesc) { + PRTime expiration; + /* get the expiration time from the caching channel's token */ + entryDesc->GetExpirationTime(&expiration); + + /* set the expiration time on our entry */ + mCacheEntry->SetExpirationTime(expiration); + } + } } } +#endif return NS_OK; } @@ -678,8 +712,8 @@ imgRequest::SniffMimeType(const char *buf, PRUint32 len) /* or how about ART? */ /* ART begins with JG (4A 47). Major version offset 2. - Minor version offset 3. Offset 4 must be NULL. - */ + * Minor version offset 3. Offset 4 must be NULL. + */ if (len >= 5 && ((unsigned char) buf[0])==0x4a && ((unsigned char) buf[1])==0x47 && diff --git a/modules/libpr0n/src/imgRequest.h b/modules/libpr0n/src/imgRequest.h index 09c39817919..f820b944c45 100644 --- a/modules/libpr0n/src/imgRequest.h +++ b/modules/libpr0n/src/imgRequest.h @@ -40,6 +40,12 @@ #include "nsString.h" +#ifdef MOZ_NEW_CACHE +#include "nsICacheEntryDescriptor.h" +#else +class nsICacheEntryDescriptor; +#endif + #define NS_IMGREQUEST_CID \ { /* 9f733dd6-1dd1-11b2-8cdf-effb70d1ea71 */ \ 0x9f733dd6, \ @@ -48,7 +54,6 @@ {0x8c, 0xdf, 0xef, 0xfb, 0x70, 0xd1, 0xea, 0x71} \ } - enum { onStartDecode = 0x1, onStartContainer = 0x2, @@ -66,7 +71,7 @@ public: virtual ~imgRequest(); /* additional members */ - nsresult Init(nsIChannel *aChannel); + nsresult Init(nsIChannel *aChannel, nsICacheEntryDescriptor *aCacheEntry); nsresult AddObserver(imgIDecoderObserver *observer); nsresult RemoveObserver(imgIDecoderObserver *observer, nsresult status); @@ -97,6 +102,10 @@ private: PRUint32 mState; nsCString mContentType; + +#ifdef MOZ_NEW_CACHE + nsCOMPtr mCacheEntry; /* we hold on to this to this so long as we have observers */ +#endif }; #endif diff --git a/modules/libpr0n/src/imgRequestProxy.cpp b/modules/libpr0n/src/imgRequestProxy.cpp index e765aa5f425..34fbe483a6f 100644 --- a/modules/libpr0n/src/imgRequestProxy.cpp +++ b/modules/libpr0n/src/imgRequestProxy.cpp @@ -62,7 +62,7 @@ imgRequestProxy::~imgRequestProxy() -nsresult imgRequestProxy::Init(imgRequest *request, nsILoadGroup *aLoadGroup, imgIDecoderObserver *aObserver, nsISupports *cx, nsICacheEntryDescriptor *aCacheEntry) +nsresult imgRequestProxy::Init(imgRequest *request, nsILoadGroup *aLoadGroup, imgIDecoderObserver *aObserver, nsISupports *cx) { PR_ASSERT(request); @@ -89,10 +89,6 @@ nsresult imgRequestProxy::Init(imgRequest *request, nsILoadGroup *aLoadGroup, im loadGroup->AddRequest(mDummyChannel, cx); } -#ifdef MOZ_NEW_CACHE - mCacheEntry = aCacheEntry; -#endif - request->AddObserver(this); PR_LOG(gImgLog, PR_LOG_DEBUG, diff --git a/modules/libpr0n/src/imgRequestProxy.h b/modules/libpr0n/src/imgRequestProxy.h index d4d2b24ba69..50191128f21 100644 --- a/modules/libpr0n/src/imgRequestProxy.h +++ b/modules/libpr0n/src/imgRequestProxy.h @@ -31,12 +31,6 @@ #include "nsILoadGroup.h" #include "nsCOMPtr.h" -#ifdef MOZ_NEW_CACHE -#include "nsICacheEntryDescriptor.h" -#else -class nsICacheEntryDescriptor; -#endif - #define NS_IMGREQUESTPROXY_CID \ { /* 20557898-1dd2-11b2-8f65-9c462ee2bc95 */ \ 0x20557898, \ @@ -61,7 +55,7 @@ public: virtual ~imgRequestProxy(); /* additional members */ - nsresult Init(imgRequest *request, nsILoadGroup *aLoadGroup, imgIDecoderObserver *aObserver, nsISupports *cx, nsICacheEntryDescriptor *aCacheEntry); + nsresult Init(imgRequest *request, nsILoadGroup *aLoadGroup, imgIDecoderObserver *aObserver, nsISupports *cx); private: nsCOMPtr mObserver; @@ -72,11 +66,5 @@ private: nsCOMPtr mDummyChannel; -#ifdef MOZ_NEW_CACHE - nsCOMPtr mCacheEntry; /* we hold on to this to give the cache an - * acurate count of people holding on to an entry - */ -#endif - PRBool mCanceled; };