From ca37abdac236bedbef8b5e679a5ff072dd2eab62 Mon Sep 17 00:00:00 2001 From: "pavlov%pavlov.net" Date: Wed, 23 Mar 2005 21:09:02 +0000 Subject: [PATCH] add api to allow one to get the mime type and content disposition from imagelib. bug 287286. r=bz sr=shaver git-svn-id: svn://10.0.0.236/trunk@171069 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/modules/libpr0n/public/imgICache.idl | 19 ++++++++++-- mozilla/modules/libpr0n/src/imgCache.cpp | 24 +++++++++++++++ mozilla/modules/libpr0n/src/imgRequest.cpp | 31 ++++++++++++++++++++ mozilla/modules/libpr0n/src/imgRequest.h | 6 ++++ 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/mozilla/modules/libpr0n/public/imgICache.idl b/mozilla/modules/libpr0n/public/imgICache.idl index 95ffcab59eb..6aad8586c65 100644 --- a/mozilla/modules/libpr0n/public/imgICache.idl +++ b/mozilla/modules/libpr0n/public/imgICache.idl @@ -40,15 +40,17 @@ #include "nsISupports.idl" interface nsIURI; +interface imgIRequest; +interface nsIProperties; /** * imgICache interface * * @author Stuart Parmenter - * @version 0.0 + * @version 0.1 * @see imagelib2 */ -[scriptable, uuid(075e424c-1dd2-11b2-97bc-e5beb94b874e)] +[scriptable, uuid(f1b74aae-5661-4753-a21c-66dd644afebc)] interface imgICache : nsISupports { /** @@ -67,4 +69,17 @@ interface imgICache : nsISupports * NS_ERROR_NOT_AVAILABLE if \a uri was unable to be removed from the cache. */ void removeEntry(in nsIURI uri); + + /** + * Find Properties + * Used to get properties such as 'type' and 'content-disposition' + * 'type' is a nsISupportsCString containing the images' mime type such as 'image/png' + * 'content-disposition' will be a nsISupportsCString containing the header + * If you call this before any data has been loaded from a URI, it will succeed, + * but come back empty. + * + * @param uri The URI to look up. + * @returns NULL if the URL was not found in the cache + */ + nsIProperties findEntryProperties(in nsIURI uri); }; diff --git a/mozilla/modules/libpr0n/src/imgCache.cpp b/mozilla/modules/libpr0n/src/imgCache.cpp index 04bca68b5ba..b5fee2113d8 100644 --- a/mozilla/modules/libpr0n/src/imgCache.cpp +++ b/mozilla/modules/libpr0n/src/imgCache.cpp @@ -103,6 +103,30 @@ NS_IMETHODIMP imgCache::RemoveEntry(nsIURI *uri) return NS_ERROR_NOT_AVAILABLE; } +/* imgIRequest findEntry(in nsIURI uri); */ +NS_IMETHODIMP imgCache::FindEntryProperties(nsIURI *uri, nsIProperties **_retval) +{ + PRBool expired; + // This is an owning reference that must be released. + imgRequest *request = nsnull; + nsCOMPtr entry; + + // addrefs request + imgCache::Get(uri, &expired, &request, getter_AddRefs(entry)); + + *_retval = nsnull; + + if (request) { + *_retval = request->Properties(); + NS_ADDREF(*_retval); + } + + NS_IF_RELEASE(request); + + return NS_OK; +} + + static nsCOMPtr gSession = nsnull; static nsCOMPtr gChromeSession = nsnull; diff --git a/mozilla/modules/libpr0n/src/imgRequest.cpp b/mozilla/modules/libpr0n/src/imgRequest.cpp index beb02b211a6..ceea65ce2a7 100644 --- a/mozilla/modules/libpr0n/src/imgRequest.cpp +++ b/mozilla/modules/libpr0n/src/imgRequest.cpp @@ -61,6 +61,7 @@ #include "nsIComponentManager.h" #include "nsIProxyObjectManager.h" #include "nsIServiceManager.h" +#include "nsISupportsPrimitives.h" #include "nsAutoLock.h" #include "nsString.h" @@ -100,6 +101,10 @@ nsresult imgRequest::Init(nsIChannel *aChannel, NS_ASSERTION(!mImage, "imgRequest::Init -- Multiple calls to init"); NS_ASSERTION(aChannel, "imgRequest::Init -- No channel"); + mProperties = do_CreateInstance("@mozilla.org/properties;1"); + if (!mProperties) + return NS_ERROR_OUT_OF_MEMORY; + mChannel = aChannel; /* set our loading flag to true here. @@ -813,6 +818,32 @@ NS_IMETHODIMP imgRequest::OnDataAvailable(nsIRequest *aRequest, nsISupports *ctx LOG_MSG(gImgLog, "imgRequest::OnDataAvailable", "Got content type from the channel"); } + /* set our mimetype as a property */ + nsCOMPtr contentType(do_CreateInstance("@mozilla.org/supports-cstring;1")); + if (contentType) { + contentType->SetData(mContentType); + mProperties->Set("type", contentType); + } + + /* set our content disposition as a property */ + nsCAutoString disposition; + nsCOMPtr httpChannel(do_QueryInterface(aRequest)); + if (httpChannel) { + httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("content-disposition"), disposition); + } else { + nsCOMPtr multiPartChannel(do_QueryInterface(aRequest)); + if (multiPartChannel) { + multiPartChannel->GetContentDisposition(disposition); + } + } + if (!disposition.IsEmpty()) { + nsCOMPtr contentDisposition(do_CreateInstance("@mozilla.org/supports-cstring;1")); + if (contentDisposition) { + contentDisposition->SetData(disposition); + mProperties->Set("content-disposition", contentDisposition); + } + } + LOG_MSG_WITH_PARAM(gImgLog, "imgRequest::OnDataAvailable", "content type", mContentType.get()); nsCAutoString conid(NS_LITERAL_CSTRING("@mozilla.org/image/decoder;2?type=") + mContentType); diff --git a/mozilla/modules/libpr0n/src/imgRequest.h b/mozilla/modules/libpr0n/src/imgRequest.h index d3a4640951c..c42c4a79928 100644 --- a/mozilla/modules/libpr0n/src/imgRequest.h +++ b/mozilla/modules/libpr0n/src/imgRequest.h @@ -48,6 +48,7 @@ #include "nsICacheEntryDescriptor.h" #include "nsIChannel.h" +#include "nsIProperties.h" #include "nsIStreamListener.h" #include "nsIURI.h" @@ -99,6 +100,7 @@ private: friend class imgRequestProxy; friend class imgLoader; friend class imgCacheValidator; + friend class imgCache; inline void SetLoadId(void *aLoadId) { mLoadId = aLoadId; @@ -112,6 +114,9 @@ private: inline const char *GetMimeType() const { return mContentType.get(); } + inline nsIProperties *Properties() { + return mProperties; + } // Return true if at least one of our proxies, excluding // aProxyToIgnore, has an observer. aProxyToIgnore may be null. @@ -137,6 +142,7 @@ private: nsCOMPtr mURI; nsCOMPtr mImage; nsCOMPtr mDecoder; + nsCOMPtr mProperties; nsVoidArray mObservers;