зеркало из https://github.com/mozilla/pjs.git
changes wrt where we cache images. now setting the size and expiration date information on the cache entry descriptor
This commit is contained in:
Родитель
8a3139502f
Коммит
5ba1615414
|
@ -156,4 +156,5 @@ PRBool ImageCache::Remove(nsIURI *aKey)
|
|||
|
||||
return PR_TRUE;
|
||||
}
|
||||
|
||||
#endif /* MOZ_NEW_CACHE */
|
||||
|
|
|
@ -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<nsIIOService> 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<imgIRequest> 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<imgIRequest> 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<imgIRequest> 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;
|
||||
|
|
|
@ -21,8 +21,6 @@
|
|||
* Stuart Parmenter <pavlov@netscape.com>
|
||||
*/
|
||||
|
||||
//#define LOADER_THREADSAFE 1
|
||||
|
||||
#include "imgILoader.h"
|
||||
|
||||
#ifdef LOADER_THREADSAFE
|
||||
|
@ -47,7 +45,4 @@ public:
|
|||
virtual ~imgLoader();
|
||||
|
||||
private:
|
||||
#ifdef LOADER_THREADSAFE
|
||||
PRLock *mLock;
|
||||
#endif
|
||||
};
|
||||
|
|
|
@ -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");
|
||||
|
||||
|
@ -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<nsIAtom> 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<nsICachingChannel> cacheChannel(do_QueryInterface(chan));
|
||||
if (cacheChannel) {
|
||||
nsCOMPtr<nsISupports> cacheToken;
|
||||
cacheChannel->GetCacheToken(getter_AddRefs(cacheToken));
|
||||
if (cacheToken) {
|
||||
nsCOMPtr<nsICacheEntryDescriptor> 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,7 +712,7 @@ 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 &&
|
||||
|
|
|
@ -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<nsICacheEntryDescriptor> mCacheEntry; /* we hold on to this to this so long as we have observers */
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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<imgIDecoderObserver> mObserver;
|
||||
|
@ -72,11 +66,5 @@ private:
|
|||
|
||||
nsCOMPtr<nsIChannel> mDummyChannel;
|
||||
|
||||
#ifdef MOZ_NEW_CACHE
|
||||
nsCOMPtr<nsICacheEntryDescriptor> mCacheEntry; /* we hold on to this to give the cache an
|
||||
* acurate count of people holding on to an entry
|
||||
*/
|
||||
#endif
|
||||
|
||||
PRBool mCanceled;
|
||||
};
|
||||
|
|
Загрузка…
Ссылка в новой задаче