From 491475514e5f368aa1cd495dc97ef6e67678fbc5 Mon Sep 17 00:00:00 2001 From: Gavin Sharp Date: Thu, 22 Oct 2009 19:10:20 -0400 Subject: [PATCH] Bug 523646: make favicon optimization size and file limit configurable using a pref, r=mak --- .../places/src/nsFaviconService.cpp | 23 ++++++++++++++----- .../components/places/src/nsFaviconService.h | 12 +++++++--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/toolkit/components/places/src/nsFaviconService.cpp b/toolkit/components/places/src/nsFaviconService.cpp index f7966c0fae00..cf48a840f0a7 100644 --- a/toolkit/components/places/src/nsFaviconService.cpp +++ b/toolkit/components/places/src/nsFaviconService.cpp @@ -69,6 +69,7 @@ #include "mozIStorageStatementCallback.h" #include "mozIStorageError.h" #include "nsPlacesTables.h" +#include "nsIPrefService.h" // For favicon optimization #include "imgITools.h" @@ -81,10 +82,13 @@ #define CONTENT_SNIFFING_SERVICES "content-sniffing-services" -// If favicon is bigger than this size we will try to optimize it into a -// 16x16 png. An uncompressed 16x16 RGBA image is 1024 bytes, and almost all -// sensible 16x16 icons are under 1024 bytes. -#define OPTIMIZED_FAVICON_SIZE 1024 + +// Default value for mOptimizedIconDimension +#define OPTIMIZED_FAVICON_DIMENSION 16 + +// Most icons will be smaller than this rough estimate of the size of an +// uncompressed 16x16 RGBA image of the same dimensions. +#define MAX_ICON_FILESIZE(s) ((PRUint32) s*s*4) /** * The maximum time we will keep a favicon around. We always ask the cache, if @@ -143,6 +147,7 @@ NS_IMPL_ISUPPORTS1( // nsFaviconService::nsFaviconService nsFaviconService::nsFaviconService() : mExpirationRunning(false) + , mOptimizedIconDimension(OPTIMIZED_FAVICON_DIMENSION) , mFailedFaviconSerial(0) { NS_ASSERTION(! gFaviconService, "ATTEMPTING TO CREATE TWO FAVICON SERVICES!"); @@ -221,6 +226,10 @@ nsFaviconService::Init() if (! mFailedFavicons.Init(MAX_FAVICON_CACHE_SIZE)) return NS_ERROR_OUT_OF_MEMORY; + nsCOMPtr pb = do_GetService(NS_PREFSERVICE_CONTRACTID); + if (pb) + pb->GetIntPref("places.favicons.optimizeToDimension", &mOptimizedIconDimension); + return NS_OK; } @@ -697,7 +706,7 @@ nsFaviconService::SetFaviconData(nsIURI* aFaviconURI, const PRUint8* aData, // If the page provided a large image for the favicon (eg, a highres image // or a multiresolution .ico file), we don't want to store more data than // needed. - if (aDataLen > OPTIMIZED_FAVICON_SIZE) { + if (aDataLen > MAX_ICON_FILESIZE(mOptimizedIconDimension)) { rv = OptimizeFaviconImage(aData, aDataLen, aMimeType, newData, newMimeType); if (NS_SUCCEEDED(rv) && newData.Length() < aDataLen) { data = reinterpret_cast(const_cast(newData.get())), @@ -1102,7 +1111,9 @@ nsFaviconService::OptimizeFaviconImage(const PRUint8* aData, PRUint32 aDataLen, // scale and recompress nsCOMPtr iconStream; - rv = imgtool->EncodeScaledImage(container, aNewMimeType, 16, 16, + rv = imgtool->EncodeScaledImage(container, aNewMimeType, + mOptimizedIconDimension, + mOptimizedIconDimension, getter_AddRefs(iconStream)); NS_ENSURE_SUCCESS(rv, rv); diff --git a/toolkit/components/places/src/nsFaviconService.h b/toolkit/components/places/src/nsFaviconService.h index a6ac125d5704..98ced722eb66 100644 --- a/toolkit/components/places/src/nsFaviconService.h +++ b/toolkit/components/places/src/nsFaviconService.h @@ -95,9 +95,9 @@ public: nsresult GetFaviconLinkForIconString(const nsCString& aIcon, nsIURI** aOutput); void GetFaviconSpecForIconString(const nsCString& aIcon, nsACString& aOutput); - static nsresult OptimizeFaviconImage(const PRUint8* aData, PRUint32 aDataLen, - const nsACString& aMimeType, - nsACString& aNewData, nsACString& aNewMimeType); + nsresult OptimizeFaviconImage(const PRUint8* aData, PRUint32 aDataLen, + const nsACString& aMimeType, + nsACString& aNewData, nsACString& aNewMimeType); /** * Obtains the favicon data asynchronously. @@ -146,6 +146,12 @@ private: // till expiration has finished. bool mExpirationRunning; + // The target dimension, in pixels, for favicons we optimize. + // If we find images that are as large or larger than an uncompressed RGBA + // image of this size (mOptimizedIconDimension*mOptimizedIconDimension*4), + // we will try to optimize it. + PRInt32 mOptimizedIconDimension; + PRUint32 mFailedFaviconSerial; nsDataHashtable mFailedFavicons;