bug 104906 r=pavlov sr=tor a=asa

imgRequest::SniffMimeType and nsUnknownDecoder::SniffForImageMimeType should be one function
This commit is contained in:
cbiesinger%web.de 2002-03-23 13:21:33 +00:00
Родитель 1e8c92fe00
Коммит 09f543c624
15 изменённых файлов: 142 добавлений и 188 удалений

Просмотреть файл

@ -64,6 +64,7 @@ REQUIRES = xpcom \
webbrwsr \
caps \
xpconnect \
imglib2 \
$(NULL)
CPPSRCS = \

Просмотреть файл

@ -55,6 +55,7 @@ REQUIRES = xpcom \
content_xul \
caps \
xpconnect \
imglib2 \
$(NULL)
LCFLAGS = \

Просмотреть файл

@ -62,7 +62,7 @@
#include "nsIXULContentSink.h"
#include "nsIDocStreamLoaderFactory.h"
#include "nsString.h"
#include "imgILoader.h"
// URL for the "user agent" style sheet
#define UA_CSS_URL "resource:/res/ua.css"
@ -262,11 +262,9 @@ nsContentDLF::CreateInstance(const char* aCommand,
}
// Try image types
nsCOMPtr<nsIComponentRegistrar> reg;
NS_GetComponentRegistrar(getter_AddRefs(reg));
nsCOMPtr<imgILoader> loader(do_GetService("@mozilla.org/image/loader;1"));
PRBool isReg = PR_FALSE;
nsCAutoString decoderId(NS_LITERAL_CSTRING("@mozilla.org/image/decoder;2?type=") + nsDependentCString(aContentType));
reg->IsContractIDRegistered(decoderId.get(), &isReg);
loader->SupportImageWithMimeType(aContentType, &isReg);
if (isReg) {
return CreateDocument(aCommand,
aChannel, aLoadGroup,

Просмотреть файл

@ -62,7 +62,7 @@
#include "nsIXULContentSink.h"
#include "nsIDocStreamLoaderFactory.h"
#include "nsString.h"
#include "imgILoader.h"
// URL for the "user agent" style sheet
#define UA_CSS_URL "resource:/res/ua.css"
@ -262,11 +262,9 @@ nsContentDLF::CreateInstance(const char* aCommand,
}
// Try image types
nsCOMPtr<nsIComponentRegistrar> reg;
NS_GetComponentRegistrar(getter_AddRefs(reg));
nsCOMPtr<imgILoader> loader(do_GetService("@mozilla.org/image/loader;1"));
PRBool isReg = PR_FALSE;
nsCAutoString decoderId(NS_LITERAL_CSTRING("@mozilla.org/image/decoder;2?type=") + nsDependentCString(aContentType));
reg->IsContractIDRegistered(decoderId.get(), &isReg);
loader->SupportImageWithMimeType(aContentType, &isReg);
if (isReg) {
return CreateDocument(aCommand,
aChannel, aLoadGroup,

Просмотреть файл

@ -123,7 +123,7 @@
#include "nsIMIMEService.h"
#include "nsCExternalHandlerService.h"
#include "nsICategoryManager.h"
#include "nsIComponentRegistrar.h"
#include "imgILoader.h"
#include "nsObjectFrame.h"
#include "nsIObjectFrame.h"
@ -435,13 +435,8 @@ void nsObjectFrame::IsSupportedImage(nsIContent* aContent, PRBool* aImage)
nsresult rv = aContent->GetAttr(kNameSpaceID_HTML, nsHTMLAtoms::type, type);
if((rv == NS_CONTENT_ATTR_HAS_VALUE) && (type.Length() > 0))
{
nsCOMPtr<nsIComponentRegistrar> reg;
NS_GetComponentRegistrar(getter_AddRefs(reg));
PRBool isReg = PR_FALSE;
nsCAutoString decoderId(NS_LITERAL_CSTRING("@mozilla.org/image/decoder;2?type=") + NS_LossyConvertUCS2toASCII(type));
reg->IsContractIDRegistered(decoderId.get(), &isReg);
if (isReg)
*aImage = PR_TRUE;
nsCOMPtr<imgILoader> loader(do_GetService("@mozilla.org/image/loader;1"));
loader->SupportImageWithMimeType(NS_LossyConvertUCS2toASCII(type).get(), aImage);
return;
}

Просмотреть файл

@ -123,7 +123,7 @@
#include "nsIMIMEService.h"
#include "nsCExternalHandlerService.h"
#include "nsICategoryManager.h"
#include "nsIComponentRegistrar.h"
#include "imgILoader.h"
#include "nsObjectFrame.h"
#include "nsIObjectFrame.h"
@ -435,13 +435,8 @@ void nsObjectFrame::IsSupportedImage(nsIContent* aContent, PRBool* aImage)
nsresult rv = aContent->GetAttr(kNameSpaceID_HTML, nsHTMLAtoms::type, type);
if((rv == NS_CONTENT_ATTR_HAS_VALUE) && (type.Length() > 0))
{
nsCOMPtr<nsIComponentRegistrar> reg;
NS_GetComponentRegistrar(getter_AddRefs(reg));
PRBool isReg = PR_FALSE;
nsCAutoString decoderId(NS_LITERAL_CSTRING("@mozilla.org/image/decoder;2?type=") + NS_LossyConvertUCS2toASCII(type));
reg->IsContractIDRegistered(decoderId.get(), &isReg);
if (isReg)
*aImage = PR_TRUE;
nsCOMPtr<imgILoader> loader(do_GetService("@mozilla.org/image/loader;1"));
loader->SupportImageWithMimeType(NS_LossyConvertUCS2toASCII(type).get(), aImage);
return;
}

Просмотреть файл

@ -61,6 +61,7 @@ REQUIRES = xpcom \
xpconnect \
nkcache \
pipnss \
imglib2 \
$(NULL)
ifdef BUILD_SMIME

Просмотреть файл

@ -52,6 +52,7 @@ REQUIRES = xpcom \
!if defined(BUILD_SMIME)
msgsmime \
!endif
imglib2 \
$(NULL)
include <$(DEPTH)\config\config.mak>

Просмотреть файл

@ -78,7 +78,7 @@
#include "nsMimeStringResources.h"
#include "nsMimeTypes.h"
#include "nsMsgUtils.h"
#include "nsIComponentRegistrar.h"
#include "imgILoader.h"
#define IMAP_EXTERNAL_CONTENT_HEADER "X-Mozilla-IMAP-Part"
@ -444,11 +444,9 @@ mime_find_class (const char *content_type, MimeHeaders *hdrs,
/* The magic image types which we are able to display internally...
*/
else if (!nsCRT::strncasecmp(content_type, "image/", 6)) {
nsCOMPtr<nsIComponentRegistrar> reg;
NS_GetComponentRegistrar(getter_AddRefs(reg));
nsCOMPtr<imgILoader> loader(do_GetService("@mozilla.org/image/loader;1"));
PRBool isReg = PR_FALSE;
nsCAutoString decoderId(NS_LITERAL_CSTRING("@mozilla.org/image/decoder;2?type=") + nsDependentCString(content_type));
reg->IsContractIDRegistered(decoderId.get(), &isReg);
loader->SupportImageWithMimeType(content_type, &isReg);
if (isReg)
clazz = (MimeObjectClass *)&mimeInlineImageClass;
}

Просмотреть файл

@ -22,7 +22,6 @@
*/
#include "nsISupports.idl"
#include "gfxtypes.idl"
interface imgIDecoderObserver;
interface imgIRequest;
@ -40,7 +39,7 @@ interface nsISimpleEnumerator;
* imgILoader interface
*
* @author Stuart Parmenter <pavlov@netscape.com>
* @version 0.2
* @version 0.3
* @see imagelib2
*/
[scriptable, uuid(4c8cf1e0-1dd2-11b2-aff9-c51cdbfcb6da)]
@ -70,4 +69,19 @@ interface imgILoader : nsISupports
* @param cx some random data
*/
imgIRequest loadImageWithChannel(in nsIChannel aChannel, in imgIDecoderObserver aObserver, in nsISupports cx, out nsIStreamListener aListener);
/**
* Checks if a decoder for the an image with the given mime type is available
* @param mimeType The type to find a decoder for
* @return true if a decoder is available, false otherwise
*/
boolean supportImageWithMimeType(in string mimeType);
/**
* Checks if a decoder for the given contents is available.
* @param contents The first few bytes of the image
* @param length The length of contents
* @return The Content Type of the Image or null if no decoder was found.
*/
string supportImageWithContents(in string contents, in unsigned long length);
};

Просмотреть файл

@ -43,6 +43,8 @@
#include "ImageLogging.h"
#include "nsIComponentRegistrar.h"
#ifdef DEBUG_pavlov
#include "nsIEnumerator.h"
#include "nsISupportsPrimitives.h"
@ -522,6 +524,100 @@ imgLoader::CreateNewProxyForRequest(imgRequest *aRequest, nsILoadGroup *aLoadGro
return NS_OK;
}
NS_IMETHODIMP imgLoader::SupportImageWithMimeType(const char* aMimeType, PRBool *_retval)
{
*_retval = PR_FALSE;
nsCOMPtr<nsIComponentRegistrar> reg;
nsresult rv = NS_GetComponentRegistrar(getter_AddRefs(reg));
if (NS_FAILED(rv))
return rv;
nsCAutoString decoderId(NS_LITERAL_CSTRING("@mozilla.org/image/decoder;2?type=") + nsDependentCString(aMimeType));
return reg->IsContractIDRegistered(decoderId.get(), _retval);
}
NS_IMETHODIMP imgLoader::SupportImageWithContents(const char* aContents, PRUint32 aLength, char** aContentType)
{
*aContentType = nsnull;
/* Is it a GIF? */
if (aLength >= 4 && !nsCRT::strncmp(aContents, "GIF8", 4)) {
*aContentType = nsCRT::strndup("image/gif", 9);
return NS_OK;
}
/* or a PNG? */
if (aLength >= 4 && ((unsigned char)aContents[0]==0x89 &&
(unsigned char)aContents[1]==0x50 &&
(unsigned char)aContents[2]==0x4E &&
(unsigned char)aContents[3]==0x47))
{
*aContentType = nsCRT::strndup("image/png", 9);
return NS_OK;
}
/* maybe a JPEG (JFIF)? */
/* JFIF files start with SOI APP0 but older files can start with SOI DQT
* so we test for SOI followed by any marker, i.e. FF D8 FF
* this will also work for SPIFF JPEG files if they appear in the future.
*
* (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
*/
if (aLength >= 3 &&
((unsigned char)aContents[0])==0xFF &&
((unsigned char)aContents[1])==0xD8 &&
((unsigned char)aContents[2])==0xFF)
{
*aContentType = nsCRT::strndup("image/jpeg", 10);
return NS_OK;
}
/* or how about ART? */
/* ART begins with JG (4A 47). Major version offset 2.
* Minor version offset 3. Offset 4 must be NULL.
*/
if (aLength >= 5 &&
((unsigned char) aContents[0])==0x4a &&
((unsigned char) aContents[1])==0x47 &&
((unsigned char) aContents[4])==0x00 )
{
*aContentType = nsCRT::strndup("image/x-jg", 10);
return NS_OK;
}
if (aLength >= 2 && !nsCRT::strncmp(aContents, "BM", 2)) {
*aContentType = nsCRT::strndup("image/bmp", 9);
return NS_OK;
}
// ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
if (aLength >= 4 && !memcmp(aContents, "\000\000\001\000", 4)) {
*aContentType = nsCRT::strndup("image/x-icon", 12);
return NS_OK;
}
if (aLength >= 4 && ((unsigned char)aContents[0]==0x8A &&
(unsigned char)aContents[1]==0x4D &&
(unsigned char)aContents[2]==0x4E &&
(unsigned char)aContents[3]==0x47))
{
*aContentType = nsCRT::strndup("video/x-mng", 11);
return NS_OK;
}
if (aLength >= 4 && ((unsigned char)aContents[0]==0x8B &&
(unsigned char)aContents[1]==0x4A &&
(unsigned char)aContents[2]==0x4E &&
(unsigned char)aContents[3]==0x47))
{
*aContentType = nsCRT::strndup("image/x-jng", 11);
return NS_OK;
}
/* none of the above? I give up */
/* don't raise an exception, simply return null */
return NS_OK;
}
/**
* proxy stream listener class used to handle multipart/x-mixed-replace
*/

Просмотреть файл

@ -633,7 +633,7 @@ NS_IMETHODIMP imgRequest::OnStopRequest(nsIRequest *aRequest, nsISupports *ctxt,
mDecoder->Close();
mDecoder = nsnull; // release the decoder so that it can rest peacefully ;)
}
// if there was an error loading the image, (mState & onStopDecode) won't be true.
// Send an onStopDecode message
if (!(mState & onStopDecode)) {
@ -781,82 +781,6 @@ static NS_METHOD sniff_mimetype_callback(nsIInputStream* in,
void
imgRequest::SniffMimeType(const char *buf, PRUint32 len)
{
/* Is it a GIF? */
mContentType.Truncate();
if (len >= 4 && !nsCRT::strncmp(buf, "GIF8", 4)) {
mContentType = NS_LITERAL_CSTRING("image/gif");
return;
}
/* or a PNG? */
if (len >= 4 && ((unsigned char)buf[0]==0x89 &&
(unsigned char)buf[1]==0x50 &&
(unsigned char)buf[2]==0x4E &&
(unsigned char)buf[3]==0x47))
{
mContentType = NS_LITERAL_CSTRING("image/png");
return;
}
/* maybe a JPEG (JFIF)? */
/* JFIF files start with SOI APP0 but older files can start with SOI DQT
* so we test for SOI followed by any marker, i.e. FF D8 FF
* this will also work for SPIFF JPEG files if they appear in the future.
*
* (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
*/
if (len >= 3 &&
((unsigned char)buf[0])==0xFF &&
((unsigned char)buf[1])==0xD8 &&
((unsigned char)buf[2])==0xFF)
{
mContentType = NS_LITERAL_CSTRING("image/jpeg");
return;
}
/* or how about ART? */
/* ART begins with JG (4A 47). Major version offset 2.
* Minor version offset 3. Offset 4 must be NULL.
*/
if (len >= 5 &&
((unsigned char) buf[0])==0x4a &&
((unsigned char) buf[1])==0x47 &&
((unsigned char) buf[4])==0x00 )
{
mContentType = NS_LITERAL_CSTRING("image/x-jg");
return;
}
if (len >= 2 && !nsCRT::strncmp(buf, "BM", 2)) {
mContentType = NS_LITERAL_CSTRING("image/bmp");
return;
}
// ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
if (len >= 4 && !memcmp(buf, "\000\000\001\000", 4)) {
mContentType = NS_LITERAL_CSTRING("image/x-icon");
return;
}
if (len >= 4 && ((unsigned char)buf[0]==0x8A &&
(unsigned char)buf[1]==0x4D &&
(unsigned char)buf[2]==0x4E &&
(unsigned char)buf[3]==0x47))
{
mContentType = NS_LITERAL_CSTRING("video/x-mng");
return;
}
if (len >= 4 && ((unsigned char)buf[0]==0x8B &&
(unsigned char)buf[1]==0x4A &&
(unsigned char)buf[2]==0x4E &&
(unsigned char)buf[3]==0x47))
{
mContentType = NS_LITERAL_CSTRING("image/x-jng");
return;
}
/* none of the above? I give up */
nsCOMPtr<imgILoader> loader(do_GetService("@mozilla.org/image/loader;1"));
loader->SupportImageWithContents(buf, len, getter_Copies(mContentType));
}

Просмотреть файл

@ -118,7 +118,7 @@ private:
PRUint32 mImageStatus;
PRUint32 mState;
nsCString mContentType;
nsXPIDLCString mContentType;
nsCOMPtr<nsICacheEntryDescriptor> mCacheEntry; /* we hold on to this to this so long as we have observers */

Просмотреть файл

@ -38,6 +38,7 @@ REQUIRES = xpcom \
pref \
uconv \
exthandler \
imglib2 \
$(ZLIB_REQUIRES) \
$(NULL)

Просмотреть файл

@ -46,6 +46,7 @@
#include "netCore.h"
#include "nsXPIDLString.h"
#include "nsIPref.h"
#include "imgILoader.h"
#include "prcpucfg.h" // To get IS_LITTLE_ENDIAN / IS_BIG_ENDIAN
@ -393,81 +394,11 @@ void nsUnknownDecoder::DetermineContentType(nsIRequest* request)
// the following routine was ripped directly from some code in libpr0n...
void nsUnknownDecoder::SniffForImageMimeType(const char *buf, PRUint32 len)
{
/* Is it a GIF? */
if (len >= 4 && !nsCRT::strncmp(buf, "GIF8", 4)) {
mContentType = NS_LITERAL_CSTRING("image/gif");
return;
}
/* or a PNG? */
if (len >= 4 && ((unsigned char)buf[0]==0x89 &&
(unsigned char)buf[1]==0x50 &&
(unsigned char)buf[2]==0x4E &&
(unsigned char)buf[3]==0x47))
{
mContentType = NS_LITERAL_CSTRING("image/png");
return;
}
/* maybe a JPEG (JFIF)? */
/* JFIF files start with SOI APP0 but older files can start with SOI DQT
* so we test for SOI followed by any marker, i.e. FF D8 FF
* this will also work for SPIFF JPEG files if they appear in the future.
*
* (JFIF is 0XFF 0XD8 0XFF 0XE0 <skip 2> 0X4A 0X46 0X49 0X46 0X00)
*/
if (len >= 3 &&
((unsigned char)buf[0])==0xFF &&
((unsigned char)buf[1])==0xD8 &&
((unsigned char)buf[2])==0xFF)
{
mContentType = NS_LITERAL_CSTRING("image/jpeg");
return;
}
/* or how about ART? */
/* ART begins with JG (4A 47). Major version offset 2.
* Minor version offset 3. Offset 4 must be NULL.
*/
if (len >= 5 &&
((unsigned char) buf[0])==0x4a &&
((unsigned char) buf[1])==0x47 &&
((unsigned char) buf[4])==0x00 )
{
mContentType = NS_LITERAL_CSTRING("image/x-jg");
return;
}
if (len >= 2 && !nsCRT::strncmp(buf, "BM", 2)) {
mContentType = NS_LITERAL_CSTRING("image/bmp");
return;
}
// ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
if (len >= 4 && !memcmp(buf, "\000\000\001\000", 4)) {
mContentType = NS_LITERAL_CSTRING("image/x-icon");
return;
}
if (len >= 4 && ((unsigned char)buf[0]==0x8A &&
(unsigned char)buf[1]==0x4D &&
(unsigned char)buf[2]==0x4E &&
(unsigned char)buf[3]==0x47))
{
mContentType = NS_LITERAL_CSTRING("video/x-mng");
return;
}
if (len >= 4 && ((unsigned char)buf[0]==0x8B &&
(unsigned char)buf[1]==0x4A &&
(unsigned char)buf[2]==0x4E &&
(unsigned char)buf[3]==0x47))
{
mContentType = NS_LITERAL_CSTRING("image/x-jng");
return;
}
/* none of the above? I give up */
nsCOMPtr<imgILoader> loader(do_GetService("@mozilla.org/image/loader;1"));
char* temp;
loader->SupportImageWithContents(buf, len, &temp);
if (temp)
mContentType.Adopt(temp);
}