Skip to content

Commit

Permalink
Bug 822846 - Use nsMimeTypes.h instead of working with literal string…
Browse files Browse the repository at this point in the history
…s in imagelib. r=joe
  • Loading branch information
sethfowler committed Dec 19, 2012
1 parent 1673a08 commit b930adf
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 55 deletions.
31 changes: 16 additions & 15 deletions image/build/nsImageModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/ModuleUtils.h"
#include "nsMimeTypes.h"

#include "RasterImage.h"

Expand Down Expand Up @@ -61,25 +62,25 @@ static const mozilla::Module::ContractIDEntry kImageContracts[] = {
{ "@mozilla.org/image/loader;1", &kNS_IMGLOADER_CID },
{ "@mozilla.org/image/request;1", &kNS_IMGREQUESTPROXY_CID },
{ "@mozilla.org/image/tools;1", &kNS_IMGTOOLS_CID },
{ "@mozilla.org/image/encoder;2?type=image/vnd.microsoft.icon", &kNS_ICOENCODER_CID },
{ "@mozilla.org/image/encoder;2?type=image/jpeg", &kNS_JPEGENCODER_CID },
{ "@mozilla.org/image/encoder;2?type=image/png", &kNS_PNGENCODER_CID },
{ "@mozilla.org/image/encoder;2?type=image/bmp", &kNS_BMPENCODER_CID },
{ "@mozilla.org/image/encoder;2?type=" IMAGE_ICO_MS, &kNS_ICOENCODER_CID },
{ "@mozilla.org/image/encoder;2?type=" IMAGE_JPEG, &kNS_JPEGENCODER_CID },
{ "@mozilla.org/image/encoder;2?type=" IMAGE_PNG, &kNS_PNGENCODER_CID },
{ "@mozilla.org/image/encoder;2?type=" IMAGE_BMP, &kNS_BMPENCODER_CID },
{ NULL }
};

static const mozilla::Module::CategoryEntry kImageCategories[] = {
{ "Gecko-Content-Viewers", "image/gif", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", "image/jpeg", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", "image/pjpeg", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", "image/jpg", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", "image/x-icon", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", "image/vnd.microsoft.icon", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", "image/bmp", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", "image/x-ms-bmp", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", "image/icon", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", "image/png", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", "image/x-png", "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_GIF, "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_JPEG, "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_PJPEG, "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_JPG, "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_ICO, "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_ICO_MS, "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_BMP, "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_BMP_MS, "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_ICON, "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_PNG, "@mozilla.org/content/document-loader-factory;1" },
{ "Gecko-Content-Viewers", IMAGE_X_PNG, "@mozilla.org/content/document-loader-factory;1" },
{ "content-sniffing-services", "@mozilla.org/image/loader;1", "@mozilla.org/image/loader;1" },
{ NULL }
};
Expand Down
3 changes: 2 additions & 1 deletion image/decoders/icon/android/nsIconChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <stdlib.h>
#include "mozilla/dom/ContentChild.h"
#include "nsMimeTypes.h"
#include "nsIURL.h"
#include "nsXULAppAPI.h"
#include "AndroidBridge.h"
Expand Down Expand Up @@ -102,7 +103,7 @@ moz_icon_to_channel(nsIURI *aURI, const nsACString& aFileExt, uint32_t aIconSize
NS_ENSURE_SUCCESS(rv, rv);

return NS_NewInputStreamChannel(aChannel, aURI, stream,
NS_LITERAL_CSTRING("image/icon"));
NS_LITERAL_CSTRING(IMAGE_ICON));
}

nsresult
Expand Down
3 changes: 2 additions & 1 deletion image/decoders/icon/gtk/nsIconChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {

#include <gtk/gtk.h>

#include "nsMimeTypes.h"
#include "nsIMIMEService.h"

#include "nsIStringBundle.h"
Expand Down Expand Up @@ -125,7 +126,7 @@ moz_gdk_pixbuf_to_channel(GdkPixbuf* aPixbuf, nsIURI *aURI,
NS_ENSURE_SUCCESS(rv, rv);

rv = NS_NewInputStreamChannel(aChannel, aURI, stream,
NS_LITERAL_CSTRING("image/icon"));
NS_LITERAL_CSTRING(IMAGE_ICON));
return rv;
}

Expand Down
4 changes: 2 additions & 2 deletions image/decoders/icon/mac/nsIconChannelCocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@

NS_IMETHODIMP nsIconChannel::GetContentType(nsACString &aContentType)
{
aContentType.AssignLiteral("image/icon");
aContentType.AssignLiteral(IMAGE_ICON);
return NS_OK;
}

Expand All @@ -359,7 +359,7 @@

NS_IMETHODIMP nsIconChannel::GetContentCharset(nsACString &aContentCharset)
{
aContentCharset.AssignLiteral("image/icon");
aContentCharset.AssignLiteral(IMAGE_ICON);
return NS_OK;
}

Expand Down
3 changes: 2 additions & 1 deletion image/decoders/icon/os2/nsIconChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "nsReadableUtils.h"
#include "nsMemory.h"
#include "nsNetUtil.h"
#include "nsMimeTypes.h"
#include "nsIFile.h"
#include "nsIFileURL.h"
#include "nsDirectoryServiceDefs.h"
Expand Down Expand Up @@ -573,7 +574,7 @@ void ConvertMaskBitMap(uint8_t *inBuf, PBITMAPINFO2 pBMInfo,

NS_IMETHODIMP nsIconChannel::GetContentType(nsACString &aContentType)
{
aContentType.AssignLiteral("image/icon");
aContentType.AssignLiteral(IMAGE_ICON);
return NS_OK;
}

Expand Down
3 changes: 2 additions & 1 deletion image/decoders/icon/qt/nsIconChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdlib.h>
#include <unistd.h>

#include "nsMimeTypes.h"
#include "nsIMIMEService.h"

#include "nsIStringBundle.h"
Expand Down Expand Up @@ -84,7 +85,7 @@ moz_qicon_to_channel(QImage *image, nsIURI *aURI,
NS_ENSURE_SUCCESS(rv, rv);

return NS_NewInputStreamChannel(aChannel, aURI, stream,
NS_LITERAL_CSTRING("image/icon"));
NS_LITERAL_CSTRING(IMAGE_ICON));
}

nsresult
Expand Down
2 changes: 1 addition & 1 deletion image/decoders/icon/win/nsIconChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ nsresult nsIconChannel::MakeInputStream(nsIInputStream** _retval, bool nonBlocki

NS_IMETHODIMP nsIconChannel::GetContentType(nsACString &aContentType)
{
aContentType.AssignLiteral("image/x-icon");
aContentType.AssignLiteral(IMAGE_ICO);
return NS_OK;
}

Expand Down
24 changes: 13 additions & 11 deletions image/src/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsMimeTypes.h"

#include "Image.h"

namespace mozilla {
Expand Down Expand Up @@ -49,39 +51,39 @@ Image::GetDecoderType(const char *aMimeType)
eDecoderType rv = eDecoderType_unknown;

// PNG
if (!strcmp(aMimeType, "image/png"))
if (!strcmp(aMimeType, IMAGE_PNG))
rv = eDecoderType_png;
else if (!strcmp(aMimeType, "image/x-png"))
else if (!strcmp(aMimeType, IMAGE_X_PNG))
rv = eDecoderType_png;

// GIF
else if (!strcmp(aMimeType, "image/gif"))
else if (!strcmp(aMimeType, IMAGE_GIF))
rv = eDecoderType_gif;


// JPEG
else if (!strcmp(aMimeType, "image/jpeg"))
else if (!strcmp(aMimeType, IMAGE_JPEG))
rv = eDecoderType_jpeg;
else if (!strcmp(aMimeType, "image/pjpeg"))
else if (!strcmp(aMimeType, IMAGE_PJPEG))
rv = eDecoderType_jpeg;
else if (!strcmp(aMimeType, "image/jpg"))
else if (!strcmp(aMimeType, IMAGE_JPG))
rv = eDecoderType_jpeg;

// BMP
else if (!strcmp(aMimeType, "image/bmp"))
else if (!strcmp(aMimeType, IMAGE_BMP))
rv = eDecoderType_bmp;
else if (!strcmp(aMimeType, "image/x-ms-bmp"))
else if (!strcmp(aMimeType, IMAGE_BMP_MS))
rv = eDecoderType_bmp;


// ICO
else if (!strcmp(aMimeType, "image/x-icon"))
else if (!strcmp(aMimeType, IMAGE_ICO))
rv = eDecoderType_ico;
else if (!strcmp(aMimeType, "image/vnd.microsoft.icon"))
else if (!strcmp(aMimeType, IMAGE_ICO_MS))
rv = eDecoderType_ico;

// Icon
else if (!strcmp(aMimeType, "image/icon"))
else if (!strcmp(aMimeType, IMAGE_ICON))
rv = eDecoderType_icon;

return rv;
Expand Down
5 changes: 2 additions & 3 deletions image/src/ImageFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "nsIHttpChannel.h"
#include "nsSimpleURI.h"
#include "nsMimeTypes.h"

#include "RasterImage.h"
#include "VectorImage.h"
Expand All @@ -18,8 +19,6 @@
namespace mozilla {
namespace image {

const char* SVG_MIMETYPE = "image/svg+xml";

// Global preferences related to image containers.
static bool gInitializedPrefCaches = false;
static bool gDecodeOnDraw = false;
Expand Down Expand Up @@ -89,7 +88,7 @@ ImageFactory::CreateImage(nsIRequest* aRequest,
uint32_t imageFlags = ComputeImageFlags(aURI, aIsMultiPart);

// Select the type of image to create based on MIME type.
if (aMimeType.Equals(SVG_MIMETYPE)) {
if (aMimeType.EqualsLiteral(IMAGE_SVG_XML)) {
return CreateVectorImage(aRequest, aStatusTracker, aMimeType,
aURI, imageFlags, aInnerWindowId);
} else {
Expand Down
2 changes: 0 additions & 2 deletions image/src/ImageFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
namespace mozilla {
namespace image {

extern const char* SVG_MIMETYPE;

struct ImageFactory
{
/**
Expand Down
4 changes: 2 additions & 2 deletions image/src/SVGDocumentWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ SVGDocumentWrapper::SetupViewer(nsIRequest* aRequest,
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
NS_ENSURE_TRUE(catMan, NS_ERROR_NOT_AVAILABLE);
nsXPIDLCString contractId;
nsresult rv = catMan->GetCategoryEntry("Gecko-Content-Viewers", SVG_MIMETYPE,
nsresult rv = catMan->GetCategoryEntry("Gecko-Content-Viewers", IMAGE_SVG_XML,
getter_Copies(contractId));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDocumentLoaderFactory> docLoaderFactory =
Expand All @@ -353,7 +353,7 @@ SVGDocumentWrapper::SetupViewer(nsIRequest* aRequest,
nsCOMPtr<nsIStreamListener> listener;
rv = docLoaderFactory->CreateInstance("external-resource", chan,
newLoadGroup,
SVG_MIMETYPE, nullptr, nullptr,
IMAGE_SVG_XML, nullptr, nullptr,
getter_AddRefs(listener),
getter_AddRefs(viewer));
NS_ENSURE_SUCCESS(rv, rv);
Expand Down
2 changes: 1 addition & 1 deletion image/src/SVGDocumentWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "nsIObserver.h"
#include "nsIContentViewer.h"
#include "nsWeakReference.h"
#include "nsMimeTypes.h"

class nsIAtom;
class nsIPresShell;
Expand All @@ -24,7 +25,6 @@ class nsIFrame;
struct nsIntSize;
class nsSVGSVGElement;

#define SVG_MIMETYPE "image/svg+xml"
#define OBSERVER_SVC_CID "@mozilla.org/observer-service;1"


Expand Down
3 changes: 2 additions & 1 deletion image/src/VectorImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "nsIObserverService.h"
#include "nsIPresShell.h"
#include "nsIStreamListener.h"
#include "nsMimeTypes.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsSVGUtils.h" // for nsSVGUtils::ConvertToSurfaceSize
Expand Down Expand Up @@ -204,7 +205,7 @@ VectorImage::Init(imgDecoderObserver* aObserver,
if (aObserver) {
mObserver = aObserver->asWeakPtr();
}
NS_ABORT_IF_FALSE(!strcmp(aMimeType, SVG_MIMETYPE), "Unexpected mimetype");
NS_ABORT_IF_FALSE(!strcmp(aMimeType, IMAGE_SVG_XML), "Unexpected mimetype");

mIsInitialized = true;

Expand Down
15 changes: 8 additions & 7 deletions image/src/imgLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "nsContentUtils.h"
#include "nsCrossSiteListenerProxy.h"
#include "nsNetUtil.h"
#include "nsMimeTypes.h"
#include "nsStreamUtils.h"
#include "nsIHttpChannel.h"
#include "nsICachingChannel.h"
Expand Down Expand Up @@ -957,7 +958,7 @@ void imgLoader::ReadAcceptHeaderPref()
if (accept)
mAcceptHeader = accept;
else
mAcceptHeader = "image/png,image/*;q=0.8,*/*;q=0.5";
mAcceptHeader = IMAGE_PNG "," IMAGE_WILDCARD ";q=0.8," ANY_WILDCARD ";q=0.5";
}

/* void clearCache (in boolean chrome); */
Expand Down Expand Up @@ -1991,7 +1992,7 @@ nsresult imgLoader::GetMimeTypeFromContent(const char* aContents, uint32_t aLeng
if (aLength >= 6 && (!nsCRT::strncmp(aContents, "GIF87a", 6) ||
!nsCRT::strncmp(aContents, "GIF89a", 6)))
{
aContentType.AssignLiteral("image/gif");
aContentType.AssignLiteral(IMAGE_GIF);
}

/* or a PNG? */
Expand All @@ -2004,7 +2005,7 @@ nsresult imgLoader::GetMimeTypeFromContent(const char* aContents, uint32_t aLeng
(unsigned char)aContents[6]==0x1A &&
(unsigned char)aContents[7]==0x0A))
{
aContentType.AssignLiteral("image/png");
aContentType.AssignLiteral(IMAGE_PNG);
}

/* maybe a JPEG (JFIF)? */
Expand All @@ -2019,7 +2020,7 @@ nsresult imgLoader::GetMimeTypeFromContent(const char* aContents, uint32_t aLeng
((unsigned char)aContents[1])==0xD8 &&
((unsigned char)aContents[2])==0xFF)
{
aContentType.AssignLiteral("image/jpeg");
aContentType.AssignLiteral(IMAGE_JPEG);
}

/* or how about ART? */
Expand All @@ -2031,18 +2032,18 @@ nsresult imgLoader::GetMimeTypeFromContent(const char* aContents, uint32_t aLeng
((unsigned char) aContents[1])==0x47 &&
((unsigned char) aContents[4])==0x00 )
{
aContentType.AssignLiteral("image/x-jg");
aContentType.AssignLiteral(IMAGE_ART);
}

else if (aLength >= 2 && !nsCRT::strncmp(aContents, "BM", 2)) {
aContentType.AssignLiteral("image/bmp");
aContentType.AssignLiteral(IMAGE_BMP);
}

// ICOs always begin with a 2-byte 0 followed by a 2-byte 1.
// CURs begin with 2-byte 0 followed by 2-byte 2.
else if (aLength >= 4 && (!memcmp(aContents, "\000\000\001\000", 4) ||
!memcmp(aContents, "\000\000\002\000", 4))) {
aContentType.AssignLiteral("image/x-icon");
aContentType.AssignLiteral(IMAGE_ICO);
}

else {
Expand Down
3 changes: 2 additions & 1 deletion image/src/imgRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "nsIHttpChannel.h"
#include "nsIApplicationCache.h"
#include "nsIApplicationCacheChannel.h"
#include "nsMimeTypes.h"

#include "nsIComponentManager.h"
#include "nsIInterfaceRequestorUtils.h"
Expand Down Expand Up @@ -726,7 +727,7 @@ imgRequest::OnDataAvailable(nsIRequest *aRequest, nsISupports *ctxt,
// type and decoder.
// We always reinitialize for SVGs, because they have no way of
// reinitializing themselves.
if (mContentType != newType || newType.Equals(SVG_MIMETYPE)) {
if (mContentType != newType || newType.EqualsLiteral(IMAGE_SVG_XML)) {
mContentType = newType;

// If we've resniffed our MIME type and it changed, we need to create a
Expand Down
Loading

0 comments on commit b930adf

Please sign in to comment.